#include #include #include #include #include #include #include constexpr auto nchars = ('Z' - 'A') + 1; int nAGCT(const std::array a, const int rot) { int res = 0; for (const auto n : {'A', 'G', 'C', 'T'}) res += a[(((n - 'A' - rot) % nchars) + nchars) % nchars]; return res; } int main() { int n; std::cin >> n; std::string s; std::cin >> s; assert(static_cast(std::size(s)) == n); assert(std::all_of(s.cbegin(), s.cend(), [](const char c) { return std::isupper(c); })); std::array cnt; cnt.fill(0); for (const char c : s) ++cnt[c - 'A']; int rot = 0, res = 0; __gnu_pbds::tree, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> t; for (int i = 0; i < n; ++i) t.insert(i); while (true) { int left = nAGCT(cnt, rot); if (left == 0) { std::cout << res << '\n'; return 0; } auto iter = t.find_by_order(left - 1); --cnt[s[*iter] - 'A']; rot += cnt[s[*iter] - 'A']; t.erase(iter); ++res; } }