#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++;
}
if (tmp <= ind * 2 - 2) ans = max(ans, f(tmp + 1, ind + 1) + inc);
//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 |
1 |
Correct |
72 ms |
196332 KB |
Output is correct |
2 |
Correct |
73 ms |
196304 KB |
Output is correct |
3 |
Correct |
88 ms |
196360 KB |
Output is correct |
4 |
Correct |
73 ms |
196288 KB |
Output is correct |
5 |
Correct |
72 ms |
196300 KB |
Output is correct |
6 |
Correct |
76 ms |
196364 KB |
Output is correct |
7 |
Correct |
75 ms |
196300 KB |
Output is correct |
8 |
Correct |
77 ms |
196324 KB |
Output is correct |
9 |
Correct |
74 ms |
196392 KB |
Output is correct |
10 |
Correct |
76 ms |
196368 KB |
Output is correct |
11 |
Correct |
82 ms |
196304 KB |
Output is correct |
12 |
Correct |
79 ms |
196372 KB |
Output is correct |
13 |
Correct |
76 ms |
196296 KB |
Output is correct |
14 |
Correct |
79 ms |
196300 KB |
Output is correct |
15 |
Correct |
73 ms |
196296 KB |
Output is correct |
16 |
Incorrect |
77 ms |
196384 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
196332 KB |
Output is correct |
2 |
Correct |
73 ms |
196304 KB |
Output is correct |
3 |
Correct |
88 ms |
196360 KB |
Output is correct |
4 |
Correct |
73 ms |
196288 KB |
Output is correct |
5 |
Correct |
72 ms |
196300 KB |
Output is correct |
6 |
Correct |
76 ms |
196364 KB |
Output is correct |
7 |
Correct |
75 ms |
196300 KB |
Output is correct |
8 |
Correct |
77 ms |
196324 KB |
Output is correct |
9 |
Correct |
74 ms |
196392 KB |
Output is correct |
10 |
Correct |
76 ms |
196368 KB |
Output is correct |
11 |
Correct |
82 ms |
196304 KB |
Output is correct |
12 |
Correct |
79 ms |
196372 KB |
Output is correct |
13 |
Correct |
76 ms |
196296 KB |
Output is correct |
14 |
Correct |
79 ms |
196300 KB |
Output is correct |
15 |
Correct |
73 ms |
196296 KB |
Output is correct |
16 |
Incorrect |
77 ms |
196384 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
196332 KB |
Output is correct |
2 |
Correct |
73 ms |
196304 KB |
Output is correct |
3 |
Correct |
88 ms |
196360 KB |
Output is correct |
4 |
Correct |
73 ms |
196288 KB |
Output is correct |
5 |
Correct |
72 ms |
196300 KB |
Output is correct |
6 |
Correct |
76 ms |
196364 KB |
Output is correct |
7 |
Correct |
75 ms |
196300 KB |
Output is correct |
8 |
Correct |
77 ms |
196324 KB |
Output is correct |
9 |
Correct |
74 ms |
196392 KB |
Output is correct |
10 |
Correct |
76 ms |
196368 KB |
Output is correct |
11 |
Correct |
82 ms |
196304 KB |
Output is correct |
12 |
Correct |
79 ms |
196372 KB |
Output is correct |
13 |
Correct |
76 ms |
196296 KB |
Output is correct |
14 |
Correct |
79 ms |
196300 KB |
Output is correct |
15 |
Correct |
73 ms |
196296 KB |
Output is correct |
16 |
Incorrect |
77 ms |
196384 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
196332 KB |
Output is correct |
2 |
Correct |
73 ms |
196304 KB |
Output is correct |
3 |
Correct |
88 ms |
196360 KB |
Output is correct |
4 |
Correct |
73 ms |
196288 KB |
Output is correct |
5 |
Correct |
72 ms |
196300 KB |
Output is correct |
6 |
Correct |
76 ms |
196364 KB |
Output is correct |
7 |
Correct |
75 ms |
196300 KB |
Output is correct |
8 |
Correct |
77 ms |
196324 KB |
Output is correct |
9 |
Correct |
74 ms |
196392 KB |
Output is correct |
10 |
Correct |
76 ms |
196368 KB |
Output is correct |
11 |
Correct |
82 ms |
196304 KB |
Output is correct |
12 |
Correct |
79 ms |
196372 KB |
Output is correct |
13 |
Correct |
76 ms |
196296 KB |
Output is correct |
14 |
Correct |
79 ms |
196300 KB |
Output is correct |
15 |
Correct |
73 ms |
196296 KB |
Output is correct |
16 |
Incorrect |
77 ms |
196384 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
196332 KB |
Output is correct |
2 |
Correct |
73 ms |
196304 KB |
Output is correct |
3 |
Correct |
88 ms |
196360 KB |
Output is correct |
4 |
Correct |
73 ms |
196288 KB |
Output is correct |
5 |
Correct |
72 ms |
196300 KB |
Output is correct |
6 |
Correct |
76 ms |
196364 KB |
Output is correct |
7 |
Correct |
75 ms |
196300 KB |
Output is correct |
8 |
Correct |
77 ms |
196324 KB |
Output is correct |
9 |
Correct |
74 ms |
196392 KB |
Output is correct |
10 |
Correct |
76 ms |
196368 KB |
Output is correct |
11 |
Correct |
82 ms |
196304 KB |
Output is correct |
12 |
Correct |
79 ms |
196372 KB |
Output is correct |
13 |
Correct |
76 ms |
196296 KB |
Output is correct |
14 |
Correct |
79 ms |
196300 KB |
Output is correct |
15 |
Correct |
73 ms |
196296 KB |
Output is correct |
16 |
Incorrect |
77 ms |
196384 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |