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 sp " "
#define endl "\n"
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define pii pair<int, int>
#define st first
#define nd second
#define N 5005
#define modulo 1000000007
#define int long long
int dp[N][N];
vector<int> pg;
int arr[N], n;
int f(int time, int ind)
{
if (time > 2 * (n - 1) || ind > n) return 0;
if (arr[ind] == -1) return dp[time][ind] = f(time + 1, ind + 1);
if (dp[time][ind] != -1) return dp[time][ind];
auto it = lower_bound(pg.begin(), pg.end(), ind);
int nxt = modulo, prv = - modulo;
if (it != pg.end()) nxt = *it;
if (it != pg.begin())
{
it--;
prv = *it;
}
int dist = min(ind - prv, nxt - ind);
int tmp = time, inc = 0, ans = 0;
while(tmp <= ind * 2 - 2 && inc < arr[ind])
{
if (nxt <= n)
{
for (int i = ind + 1; i <= nxt; i++)//for the last candy we buy, we can go to the next pg and come back to a forward shop
{
ans = max(ans, f(tmp + nxt - ind + nxt - i, i) + inc + 1);
}
}
ans = max(ans, f(tmp + 1, ind + 1) + inc); // we can just continue to the next place without buying more candies
ans = max(ans, inc + 1);//buy it and finish the process
tmp += 2 * dist;
inc++;
}
ans = max(ans, f(tmp + 1, ind + 1) + inc);
ans = max(ans, f(time + 1, ind + 1));
//cout<<time<<sp<<ind<<sp<<ans<<endl;
return dp[time][ind] = ans;
}
int32_t main()
{
fastio();
memset(dp, -1, sizeof(dp));
cin>>n;
int sum = 0;
for (int i = 1; i <= n; i++)
{
cin>>arr[i];
if (arr[i] == -1) pg.pb(i);
else sum += arr[i];
}
cout<<sum - f(0, 1)<<endl;
cerr<<"time taken: "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |