Submission #1329705

#TimeUsernameProblemLanguageResultExecution timeMemory
1329705foxsergTortoise (CEOI21_tortoise)C++20
48 / 100
192 ms431812 KiB
#include <bits/stdc++.h>
// #include <bits/extc++.h>

// #pragma GCC optimize("Ofast,unroll-loops,O3")
// #pragma GCC target("avx,avx2,fma")

using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;

// template <typename T>
// using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
// template <typename T>
// using ordered_multiset = tree <T, null_type, less_equal <T>, rb_tree_tag, tree_order_statistics_node_update>;

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uint = unsigned int;
using i128 = __int128_t;

istream& operator>>(istream& is, i128& x) {
    long long a;
    is >> a;
    x = (i128) a;
    return is;
}
ostream& operator<<(ostream& os, i128& x) {
    long long a = (long long) x;
    os << a;
    return os;
}

template <typename T>
ostream& operator<<(ostream& is, vector <T>& a) {
    for (uint i = 0; i < a.size(); ++i) is << a[i] << " ";
    return is;
}

#ifdef LOCAL
    # define DEBUG(x) cerr << "(" << __LINE__ << ") " << #x << ":  " << x << endl;
#else
    # define DEBUG(x)
#endif

const ll inf = 1e18 + 1e16;
const int inf_t = 1e9 + 7;
const ll mod = 1e18 + 7;

#define int long long

const int MAXN = 302;
int dp[2 * MAXN][MAXN][MAXN + 1];

signed main() {
    #ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int n;
    cin >> n;
    vector <int> a(n);

    for (int i = 0; i < n; ++i) cin >> a[i];
    int nxt[n];
    int prev[n];

    nxt[n - 1] = inf_t;
    for (int i = n - 2; i >= 0; --i) {
        nxt[i] = nxt[i + 1];
        if (a[i + 1] == -1) {
            nxt[i] = i + 1;
        }
    }

    prev[0] = -inf_t;
    for (int i = 1; i < n; ++i) {
        prev[i] = prev[i - 1];
        if (a[i - 1] == -1) {
            prev[i] = i - 1;
        }
    }

    int s = 0;
    for (int x : a) {
        if (x != -1) s += x;
    }

    for (int &x : a) {
        x = min(x, n);
    }

    for (int time = 0; time < 2 * n; ++time) {
        for (int place = (time + 1) / 2; place < n; ++place) {
            if (a[place] <= 0) continue;
            for (int ost = a[place]; ost >= 0; --ost) {
                dp[time][place][ost] = -inf_t;
            }
        }
    }
    int st = 0;
    while (st < n && a[st] <= 0) ++st;
    if (st < n) dp[st][st][a[st] - 1] = 1;
    for (int time = st; time < 2 * n; ++time) {
        for (int place = (time + 1) / 2; place < n; ++place) {
            if (a[place] <= 0) continue;
            for (int ost = a[place] - 1; ost >= 0; --ost) {
                if (dp[time][place][ost] < 0) continue;
                for (int to = place; to < n; ++to) {
                    if (a[to] <= 0) continue;
                    int diff = inf_t;
                    int i = nxt[place];
                    int j = prev[to];
                    if (i <= j) {
                        diff = to - place;
                    } else {
                        diff = min(i + i - place - to, place + to - j - j);
                    }
                    int cur = a[to] - 1;
                    if (to == place) {
                        if (ost == 0) continue;
                        cur = ost - 1;
                    }

                    if (diff + time < 2 * n) {
                        dp[diff + time][to][cur] = max(dp[diff + time][to][cur], dp[time][place][ost] + 1);
                    }
                }
            }
        }
    }

    int ans = 0;
    for (int time = 0; time < 2 * n; ++time) {
        for (int place = (time + 1) / 2; place < n; ++place) {
            if (a[place] <= 0) continue;
            for (int ost = a[place]; ost >= 0; --ost) {
                ans = max(ans, dp[time][place][ost]);
            }
        }
    }

    cout << s - ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...