#pragma GCC optimize("Ofast") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long int ll; typedef unsigned long long ull; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } inline double time() { return static_cast(chrono::duration_cast(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9; } // 1-indexed template struct BIT{ int n; vector bit; BIT(int n_=0):n(n_),bit(n+1){} T sum(int i){ T res=0; for(;i>0;i-=(i&-i))res+=bit[i]; return res; } void add(int i,T a){ if(i==0)return; for(;i<=n;i+=(i&-i)){bit[i]+=a;} } int lower_bound(T k){ // k<=sum(res) if(k<=0)return 0; int res=0,i=1; while((i<<1)<=n)i<<=1; for(;i;i>>=1){ if(res+i<=n&&bit[res+i]> n; string s; cin >> s; int res = 0; for(char c:s){ cnt[c-'A']++; } BIT bit(n); for(int i=0;iint{ ll r = (c-'A')-u; r %= 26; if(r < 0) r += 26; return r; }; while(1){ ll sum = cnt[gt2(u,'A')] + cnt[gt2(u,'G')] + cnt[gt2(u,'C')] + cnt[gt2(u,'T')]; if(sum == 0)break; int idx = bit.lower_bound(sum); bit.add(idx,-1); char c = s[idx-1]; cnt[c-'A']--; u += cnt[c-'A']; res++; } cout << res << endl; }