# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1089064 | DeathIsAwe | Growing Vegetable is Fun 3 (JOI19_ho_t3) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define minim(X, Y) (X = min(X, Y))
#define F(X, Y) for(int X = 0; X < Y; X++)
#define g(X) (X ? (X > 1 ? z : y) : x)
int Z = 202, INF = 1e9;
int N, A[Z*2], DP[Z][Z][Z][3], p[3][Z*2], h[3][Z*2], c[3], x, y, z;
int main() {
cin >> N, getchar();
F(i, N) {
char v = getchar();
A[i] = (v < 82) + (v < 89);
}
F(i, Z) F(j, Z) F(k, Z) F(l, 3)
DP[i][j][k][l] = (i || j || k) ? INF : 0;
F(i, N) {
if(++c[A[i]] >= Z-1) break;
p[A[i]][g(A[i]) = c[A[i]]] = i;
F(j, 3) h[j][i] = c[j];
F(j, i+1) F(k, i+1) F(o, 3) {
(A[i] ? x : y) = j;
(A[i] < 2 ? z : y) = k;
if(x > c[0] || y > c[1] || z > c[2]) continue;
int &w = g(o), a = INF, b = 0;
if(!w) continue; --w;
F(r, 3) minim(a, r != o ? DP[x][y][z][r] : INF); ++w;
F(r, 3) b += r != o ? max(0, g(r) - h[r][p[o][w]]) : 0;
minim(DP[x][y][z][o], a + b);
}
}
int res = INF;
F(i, 3) minim(res, DP[c[0]][c[1]][c[2]][i]);
cout << (res == INF ? -1 : res);
}