101042024-03-26 21:03:36szilJancsi és Juliska kitalálós játékacpp17Wrong answer 0/10014ms4752 KiB
#include <bits/stdc++.h>

using ll = long long;
using namespace std;

const int MAXN = 302;

int f[MAXN], dp[MAXN][MAXN], bef[MAXN], aft[MAXN];

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

    stack<int> mt;
    for (int i = 1; i <= n; i++) {
        while (!mt.empty() && f[mt.top()] < f[i]) mt.pop();
        bef[i] = mt.empty() ? 1 : mt.top();
        mt.push(i);
    }
    mt = stack<int>();
    for (int i = n; i >= 1; i--) {
        while (!mt.empty() && f[mt.top()] < f[i]) mt.pop();
        aft[i] = mt.empty() ? n : mt.top();
        mt.push(i);
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i > j) dp[i][j] = 0;
            else if (i == j) dp[i][j] = f[i];
            else dp[i][j] = 1e9;
        }
    }
    for (int i = n; i >= 1; i--) {
        for (int j = i+1; j <= n; j++) {
            for (int k = i; k <= j; k++) {
                dp[i][j] = min(dp[i][j], dp[i][bef[k]-1] + f[k] + dp[aft[k]+1][j]);
            }
        }
    }
    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
base0/100
1Accepted0/03ms1996 KiB
2Wrong answer0/014ms2924 KiB
3Wrong answer0/43ms2512 KiB
4Wrong answer0/53ms2512 KiB
5Wrong answer0/53ms2524 KiB
6Wrong answer0/53ms2656 KiB
7Wrong answer0/53ms3068 KiB
8Wrong answer0/53ms3024 KiB
9Wrong answer0/54ms3156 KiB
10Wrong answer0/54ms3348 KiB
11Wrong answer0/54ms3556 KiB
12Wrong answer0/54ms3732 KiB
13Wrong answer0/56ms3880 KiB
14Wrong answer0/56ms3956 KiB
15Wrong answer0/58ms3968 KiB
16Wrong answer0/59ms3980 KiB
17Wrong answer0/510ms4148 KiB
18Wrong answer0/512ms4172 KiB
19Wrong answer0/513ms4412 KiB
20Wrong answer0/514ms4500 KiB
21Wrong answer0/514ms4752 KiB
22Wrong answer0/614ms4712 KiB