Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- constexpr int N = 1e2 + 5;
- constexpr ll Inf = 1e17;
- int n;
- int a[4][N], cnt[4];
- vector<int> b[4];
- ll dp[2][N][N][N];
- void Read()
- {
- cin >> n;
- for (int _ = 1; _ <= 3; ++_)
- {
- b[_].emplace_back(0);
- for (int i = 1; i <= n; ++i)
- {
- cin >> a[_][i];
- if (a[_][i] == 0)
- ++cnt[_];
- else
- b[_].emplace_back(a[_][i]);
- }
- }
- }
- ll Get(int id, int row, int j)
- {
- return b[row][id - j];
- }
- ll Cost(int id, int j1, int j2, int j3, int t1, int t2, int t3)
- {
- if (t1 || t2 || t3)
- return 0;
- return Get(id, 1, j1) * Get(id, 2, j2) * Get(id, 3, j3);
- }
- void Maximize(ll &x, ll y)
- {
- if (x < y)
- x = y;
- }
- void Solve()
- {
- fill_n(&dp[0][0][0][0], 2 * N * N * N, -Inf);
- dp[0][0][0][0] = 0;
- for (int i = 0; i < n; ++i)
- {
- int p = i & 1; // Rolling dp;
- for (int j1 = 0; j1 <= cnt[1]; ++j1)
- for (int j2 = 0; j2 <= cnt[2]; ++j2)
- for (int j3 = 0; j3 <= cnt[3]; ++j3)
- if (dp[p][j1][j2][j3] != -Inf) // Neu gia tri khac -Inf moi xet tiep
- {
- // for bo chi so t
- for (int t1 = 0; t1 < 2; ++t1)
- if (j1 + t1 <= cnt[1]) // nhanh can
- for (int t2 = 0; t2 < 2; ++t2)
- if (j2 + t2 <= cnt[2]) // nhanh can
- for (int t3 = 0; t3 < 2; ++t3)
- if (j3 + t3 <= cnt[3]) // nhanh can
- {
- Maximize(dp[!p][j1 + t1][j2 + t2][j3 + t3], dp[p][j1][j2][j3] + Cost(i + 1, j1, j2, j3, t1, t2, t3));
- }
- dp[p][j1][j2][j3] = -Inf;
- }
- }
- cout << dp[n & 1][cnt[1]][cnt[2]][cnt[3]];
- }
- int32_t main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- Read();
- Solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment