#include #include #include #include #include using namespace std; typedef int lld; char s[10002], build[10002]; lld len, sz=0; int main () { freopen("code.in", "r", stdin); freopen("code.out", "w", stdout); lld p1, p2; scanf("%d", &len); scanf("%s", s); p1 = 0; p2 = len-1; while (p1 < p2) { if (s[p1] != s[p2]) { if (s[p1] < s[p2]) { build[++sz] = s[p1++]; } else { build[++sz] = s[p2--]; } continue; } char ns1='-', ns2; lld cnt1=0, cnt2=0, i1, i2; for (lld i=p1; ip1; i--) { cnt2++; if (s[p2] == s[i]) continue; i2 = i; ns2 = s[i]; break; } if (ns1 > s[p1] && ns2 > s[p2]) { for (lld i=p1; ii2; i--) { build[++sz] = s[i]; } p1 = i1; p2 = i2; continue; } if (ns1 > s[p1]) { for (lld i=p2; i>=i2; i--) { build[++sz] = s[i]; } p2 = i2-1; continue; } if (ns2 > s[p2]) { for (lld i=p1; i<=i1; i++) { build[++sz] = s[i]; } p1 = i1+1; continue; } if (cnt1 < cnt2) { for (lld i=p1; i<=i1; i++) { build[++sz] = s[i]; } p1 = i1+1; continue; } else if (cnt1 > cnt2) { for (lld i=p2; i>=i2; i--) { build[++sz] = s[i]; } p2 = i2-1; continue; } else { if (ns1 < ns2) { for (lld i=p1; i<=i1; i++) { build[++sz] = s[i]; } p1 = i1+1; continue; } else if (ns1 > ns2) { for (lld i=p2; i>=i2; i--) { build[++sz] = s[i]; } p2 = i2-1; continue; } else { bool first; lld d1 = i1+1, d2 = i2-1; while (d1 < d2) { if (s[d1] == s[d2]) { d1++; d2--; continue; } first = (s[d1] < s[d2]); break; } if (first) { for (lld i=p1; i<=i1; i++) { build[++sz] = s[i]; } p1 = i1+1; continue; } else { for (lld i=p2; i>=i2; i--) { build[++sz] = s[i]; } p2 = i2-1; continue; } } } } if (p1 == p2) { build[++sz] = s[p2]; } for (lld i=1; i<=sz; i++) { printf("%c", build[i]); } printf("\n"); }