#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) 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 |
81 ms |
196300 KB |
Output is correct |
2 |
Correct |
71 ms |
196380 KB |
Output is correct |
3 |
Correct |
71 ms |
196360 KB |
Output is correct |
4 |
Correct |
80 ms |
196300 KB |
Output is correct |
5 |
Correct |
77 ms |
196376 KB |
Output is correct |
6 |
Correct |
77 ms |
196280 KB |
Output is correct |
7 |
Correct |
74 ms |
196356 KB |
Output is correct |
8 |
Correct |
69 ms |
196296 KB |
Output is correct |
9 |
Correct |
75 ms |
196384 KB |
Output is correct |
10 |
Correct |
71 ms |
196284 KB |
Output is correct |
11 |
Correct |
69 ms |
196300 KB |
Output is correct |
12 |
Correct |
70 ms |
196324 KB |
Output is correct |
13 |
Correct |
75 ms |
196304 KB |
Output is correct |
14 |
Correct |
71 ms |
196332 KB |
Output is correct |
15 |
Correct |
73 ms |
196328 KB |
Output is correct |
16 |
Correct |
74 ms |
196348 KB |
Output is correct |
17 |
Correct |
73 ms |
196284 KB |
Output is correct |
18 |
Correct |
72 ms |
196364 KB |
Output is correct |
19 |
Correct |
71 ms |
196384 KB |
Output is correct |
20 |
Correct |
71 ms |
196300 KB |
Output is correct |
21 |
Correct |
75 ms |
196292 KB |
Output is correct |
22 |
Correct |
82 ms |
196288 KB |
Output is correct |
23 |
Correct |
75 ms |
196292 KB |
Output is correct |
24 |
Correct |
72 ms |
196280 KB |
Output is correct |
25 |
Correct |
72 ms |
196380 KB |
Output is correct |
26 |
Correct |
75 ms |
196392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
196300 KB |
Output is correct |
2 |
Correct |
71 ms |
196380 KB |
Output is correct |
3 |
Correct |
71 ms |
196360 KB |
Output is correct |
4 |
Correct |
80 ms |
196300 KB |
Output is correct |
5 |
Correct |
77 ms |
196376 KB |
Output is correct |
6 |
Correct |
77 ms |
196280 KB |
Output is correct |
7 |
Correct |
74 ms |
196356 KB |
Output is correct |
8 |
Correct |
69 ms |
196296 KB |
Output is correct |
9 |
Correct |
75 ms |
196384 KB |
Output is correct |
10 |
Correct |
71 ms |
196284 KB |
Output is correct |
11 |
Correct |
69 ms |
196300 KB |
Output is correct |
12 |
Correct |
70 ms |
196324 KB |
Output is correct |
13 |
Correct |
75 ms |
196304 KB |
Output is correct |
14 |
Correct |
71 ms |
196332 KB |
Output is correct |
15 |
Correct |
73 ms |
196328 KB |
Output is correct |
16 |
Correct |
74 ms |
196348 KB |
Output is correct |
17 |
Correct |
73 ms |
196284 KB |
Output is correct |
18 |
Correct |
72 ms |
196364 KB |
Output is correct |
19 |
Correct |
71 ms |
196384 KB |
Output is correct |
20 |
Correct |
71 ms |
196300 KB |
Output is correct |
21 |
Correct |
75 ms |
196292 KB |
Output is correct |
22 |
Correct |
82 ms |
196288 KB |
Output is correct |
23 |
Correct |
75 ms |
196292 KB |
Output is correct |
24 |
Correct |
72 ms |
196280 KB |
Output is correct |
25 |
Correct |
72 ms |
196380 KB |
Output is correct |
26 |
Correct |
75 ms |
196392 KB |
Output is correct |
27 |
Correct |
80 ms |
196412 KB |
Output is correct |
28 |
Incorrect |
74 ms |
196324 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
196300 KB |
Output is correct |
2 |
Correct |
71 ms |
196380 KB |
Output is correct |
3 |
Correct |
71 ms |
196360 KB |
Output is correct |
4 |
Correct |
80 ms |
196300 KB |
Output is correct |
5 |
Correct |
77 ms |
196376 KB |
Output is correct |
6 |
Correct |
77 ms |
196280 KB |
Output is correct |
7 |
Correct |
74 ms |
196356 KB |
Output is correct |
8 |
Correct |
69 ms |
196296 KB |
Output is correct |
9 |
Correct |
75 ms |
196384 KB |
Output is correct |
10 |
Correct |
71 ms |
196284 KB |
Output is correct |
11 |
Correct |
69 ms |
196300 KB |
Output is correct |
12 |
Correct |
70 ms |
196324 KB |
Output is correct |
13 |
Correct |
75 ms |
196304 KB |
Output is correct |
14 |
Correct |
71 ms |
196332 KB |
Output is correct |
15 |
Correct |
73 ms |
196328 KB |
Output is correct |
16 |
Correct |
74 ms |
196348 KB |
Output is correct |
17 |
Correct |
73 ms |
196284 KB |
Output is correct |
18 |
Correct |
72 ms |
196364 KB |
Output is correct |
19 |
Correct |
71 ms |
196384 KB |
Output is correct |
20 |
Correct |
71 ms |
196300 KB |
Output is correct |
21 |
Correct |
75 ms |
196292 KB |
Output is correct |
22 |
Correct |
82 ms |
196288 KB |
Output is correct |
23 |
Correct |
75 ms |
196292 KB |
Output is correct |
24 |
Correct |
72 ms |
196280 KB |
Output is correct |
25 |
Correct |
72 ms |
196380 KB |
Output is correct |
26 |
Correct |
75 ms |
196392 KB |
Output is correct |
27 |
Correct |
80 ms |
196412 KB |
Output is correct |
28 |
Incorrect |
74 ms |
196324 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
196300 KB |
Output is correct |
2 |
Correct |
71 ms |
196380 KB |
Output is correct |
3 |
Correct |
71 ms |
196360 KB |
Output is correct |
4 |
Correct |
80 ms |
196300 KB |
Output is correct |
5 |
Correct |
77 ms |
196376 KB |
Output is correct |
6 |
Correct |
77 ms |
196280 KB |
Output is correct |
7 |
Correct |
74 ms |
196356 KB |
Output is correct |
8 |
Correct |
69 ms |
196296 KB |
Output is correct |
9 |
Correct |
75 ms |
196384 KB |
Output is correct |
10 |
Correct |
71 ms |
196284 KB |
Output is correct |
11 |
Correct |
69 ms |
196300 KB |
Output is correct |
12 |
Correct |
70 ms |
196324 KB |
Output is correct |
13 |
Correct |
75 ms |
196304 KB |
Output is correct |
14 |
Correct |
71 ms |
196332 KB |
Output is correct |
15 |
Correct |
73 ms |
196328 KB |
Output is correct |
16 |
Correct |
74 ms |
196348 KB |
Output is correct |
17 |
Correct |
73 ms |
196284 KB |
Output is correct |
18 |
Correct |
72 ms |
196364 KB |
Output is correct |
19 |
Correct |
71 ms |
196384 KB |
Output is correct |
20 |
Correct |
71 ms |
196300 KB |
Output is correct |
21 |
Correct |
75 ms |
196292 KB |
Output is correct |
22 |
Correct |
82 ms |
196288 KB |
Output is correct |
23 |
Correct |
75 ms |
196292 KB |
Output is correct |
24 |
Correct |
72 ms |
196280 KB |
Output is correct |
25 |
Correct |
72 ms |
196380 KB |
Output is correct |
26 |
Correct |
75 ms |
196392 KB |
Output is correct |
27 |
Correct |
80 ms |
196412 KB |
Output is correct |
28 |
Incorrect |
74 ms |
196324 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
196300 KB |
Output is correct |
2 |
Correct |
71 ms |
196380 KB |
Output is correct |
3 |
Correct |
71 ms |
196360 KB |
Output is correct |
4 |
Correct |
80 ms |
196300 KB |
Output is correct |
5 |
Correct |
77 ms |
196376 KB |
Output is correct |
6 |
Correct |
77 ms |
196280 KB |
Output is correct |
7 |
Correct |
74 ms |
196356 KB |
Output is correct |
8 |
Correct |
69 ms |
196296 KB |
Output is correct |
9 |
Correct |
75 ms |
196384 KB |
Output is correct |
10 |
Correct |
71 ms |
196284 KB |
Output is correct |
11 |
Correct |
69 ms |
196300 KB |
Output is correct |
12 |
Correct |
70 ms |
196324 KB |
Output is correct |
13 |
Correct |
75 ms |
196304 KB |
Output is correct |
14 |
Correct |
71 ms |
196332 KB |
Output is correct |
15 |
Correct |
73 ms |
196328 KB |
Output is correct |
16 |
Correct |
74 ms |
196348 KB |
Output is correct |
17 |
Correct |
73 ms |
196284 KB |
Output is correct |
18 |
Correct |
72 ms |
196364 KB |
Output is correct |
19 |
Correct |
71 ms |
196384 KB |
Output is correct |
20 |
Correct |
71 ms |
196300 KB |
Output is correct |
21 |
Correct |
75 ms |
196292 KB |
Output is correct |
22 |
Correct |
82 ms |
196288 KB |
Output is correct |
23 |
Correct |
75 ms |
196292 KB |
Output is correct |
24 |
Correct |
72 ms |
196280 KB |
Output is correct |
25 |
Correct |
72 ms |
196380 KB |
Output is correct |
26 |
Correct |
75 ms |
196392 KB |
Output is correct |
27 |
Correct |
80 ms |
196412 KB |
Output is correct |
28 |
Incorrect |
74 ms |
196324 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |