#include #include #include using namespace std; template class MinCostFlow { public: MinCostFlow(int n) : n(n), capacity(n, vector(n)), cost(n, vector(n)), edge(n), prev(n) {} void add_edge(int src, int dst, T cap, T cos) { capacity[src][dst] = cap; capacity[dst][src] = 0; cost[src][dst] = cos; cost[dst][src] = -cos; edge[src].push_back(dst); edge[dst].push_back(src); } T min_cost_flow(int s, int t, T f) { T res = 0; h.assign(n, 0); while (f > 0) { if (!dijkstra(s, t)) return -1; for (int i = 0; i < n; ++i) h[i] += min_cost[i]; T d = f; for (int v = t; v != s; v = prev[v]) d = min(d, capacity[prev[v]][v]); f -= d; res += d * h[t]; for (int v = t; v != s; v = prev[v]) { capacity[prev[v]][v] -= d; capacity[v][prev[v]] += d; } } return res; } private: int n; T inf = numeric_limits::max() / 3; vector> capacity, cost; vector> edge; vector min_cost, h; vector prev; bool dijkstra(int s, int t) { min_cost.assign(n, inf); min_cost[s] = 0; priority_queue, vector>, greater>> pq; pq.emplace(0, s); while (!pq.empty()) { int c = pq.top().first; int v = pq.top().second; pq.pop(); if (min_cost[v] < c) continue; for (int nv : edge[v]) { if (capacity[v][nv] > 0 && min_cost[nv] > min_cost[v] + cost[v][nv] + h[v] - h[nv]) { min_cost[nv] = min_cost[v] + cost[v][nv] + h[v] - h[nv]; prev[nv] = v; pq.emplace(min_cost[nv], nv); } } } return min_cost[t] != inf; } }; int g, c, p, n, a[4], b[4]; string s; int main() { cin >> a[0] >> a[1] >> a[2] >> s; n = (int)s.size(); for (int i = 0; i < n; ++i) { if (s[i] == 'G') ++b[0]; if (s[i] == 'C') ++b[1]; if (s[i] == 'P') ++b[2]; } MinCostFlow mcf(8); int INF = 333; int s = 6; int t = 7; for (int i = 0; i < 3; ++i) { mcf.add_edge(s, i, a[i], 0); mcf.add_edge(i + 3, t, b[i], 0); mcf.add_edge(i, i + 3, INF, -1); mcf.add_edge(i, (i + 4) % 3 + 3, INF, -3); mcf.add_edge(i, (i + 5) % 3 + 3, INF, 0); } cout << -mcf.min_cost_flow(s, t, n) << endl; return 0; }