#include #define be(v) (v).begin(),(v).end() #define pb(q) push_back(q) #define rep(i, n) for(int i=0;i node; ll ID = 0; // min -> INF, max, sum -> 0 inline ll func(ll a, ll b){return a + b;} ///////// public: SegmentTree(vector v) { int sz = v.size(); n = 1; while(n < sz) n *= 2; node.resize(2*n-1, ID); for(int i=0; i=0; i--) node[i] = func(node[2*i+1], node[2*i+2]); } void update(ll x, ll val) { x += (n - 1); node[x] = val; while(x > 0) { x = (x - 1) / 2; node[x] = func(node[2*x+1], node[2*x+2]); } } ll get(ll a, ll b, ll k=0, ll l=0, ll r=-1) { if(r < 0) r = n; if(r <= a || b <= l) return ID; if(a <= l && r <= b) return node[k]; ll vl = get(a, b, 2*k+1, l, (l+r)/2); ll vr = get(a, b, 2*k+2, (l+r)/2, r); return func(vl, vr); } }; int ss(ll a, ll p) { a %= p; a += p; a %= p; return a; } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); int n; cin >> n; SegmentTree seg(vector (n, 1)); int count = 0; string t = "AGCT"; string s; cin >> s; vector cnt(26, 0); rep(i, n) { rep(j, 4) if(s[i] == t[j]) count++; cnt[s[i] - 'A']++; } if(count == 0) { cout << 0 << endl; return 0; } seg.update(count - 1, 0); ll num = --cnt[s[count - 1] - 'A']; vector nx(26); ll flip = num; rep(i, 26) { nx[(i + num) % 26] = cnt[i]; } swap(nx, cnt); while(1) { ll cc = 0; rep(i, 4) { cc += cnt[ss(ll(t[i] - 'A') - flip, 26)]; } if(!cc) break; ll le = 0, ri = cc; while(ri - le > 1) { ll mid = (ri + le ) / 2; if(seg.get(0, mid) >= cc) ri = mid; else le = mid; } seg.update(ri - 1, 0); num = --cnt[s[ri - 1] - 'A']; flip += num; rep(i, 26) { nx[(i + num) % 26] = cnt[i]; } swap(nx, cnt); } cout << n - seg.get(0, n) << endl; return 0; }