101072024-03-26 22:09:14szilJancsi és Juliska kitalálós játékacpp17Accepted 100/100686ms5216 KiB
#include <bits/stdc++.h>

using ll = long long;
using namespace std;

const int MAXN = 302;

int f[MAXN], dp[MAXN][MAXN], opt[MAXN][MAXN];

void solve() {
    int n; cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> f[i];
    }

    for (int i = 1; i <= n; i++) {
        opt[i][i] = i;
        for (int j = 1; j <= n; j++) {
            if (i >= j) dp[i][j] = 0;
            else dp[i][j] = 1e9;
        }
    }
    for (int i = n; i >= 1; i--) {
        for (int j = i+1; j <= n; j++) {
            for (int z = max(i, opt[i][j-1]-50); z <= j; z++) {
                int maxe = 0;
                for (int y = z; y <= j; y++) {
                    maxe = max(maxe, f[y]);
                    // if (i == 1 && j == 7) cerr << z << " " << y << " " << dp[i][z-1] << " " << dp[z][y] << " " << dp[y+1][j] << " " << get_max(z, y) << endl;
                    ll val = max(dp[i][z-1], max(dp[z][y], dp[y+1][j])) + maxe;
                    if (dp[i][j] > val) {
                        dp[i][j] = val;
                        opt[i][j] = z;
                    }
                }
            }
        }
    }
    /*cerr << opt[1][8] << endl;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cerr << dp[i][j] << " ";
        }
        cerr << endl;
    }*/
    cout << dp[1][n] << "\n";
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base100/100
1Accepted0/03ms1916 KiB
2Accepted0/0335ms3572 KiB
3Accepted4/43ms2524 KiB
4Accepted5/53ms2692 KiB
5Accepted5/53ms3020 KiB
6Accepted5/53ms2844 KiB
7Accepted5/513ms3488 KiB
8Accepted5/516ms3672 KiB
9Accepted5/528ms3824 KiB
10Accepted5/539ms3948 KiB
11Accepted5/556ms4080 KiB
12Accepted5/597ms4400 KiB
13Accepted5/5108ms4500 KiB
14Accepted5/5114ms4552 KiB
15Accepted5/5256ms4892 KiB
16Accepted5/5214ms4972 KiB
17Accepted5/5441ms5216 KiB
18Accepted5/5521ms5104 KiB
19Accepted5/5384ms4876 KiB
20Accepted5/5686ms4900 KiB
21Accepted5/5402ms4892 KiB
22Accepted6/6501ms4916 KiB