#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
constexpr int INF = 1e4;
struct Vertex{
int v, now, tot, p, on, tor;
Vertex(){}
Vertex(int _v, int _n, int _tot, int _p, int _on, int _tor): v(_v), now(_n), tot(_tot), p(_p), on(_on), tor(_tor) {}
bool operator < (const Vertex &V) const{return tor > V.tor;}
};
int a[500500], L[500500], R[500500], nxt[500500];
short dist[303][303][303][3][2];
vector<int> V;
int get_left(int i){
assert(a[i]!=-1);
if (i < V[0]) return -INF;
return (*--lower_bound(V.begin(), V.end(), i));
}
int get_right(int i){
assert(a[i]!=-1);
if (i > V.back()) return INF;
return (*lower_bound(V.begin(), V.end(), i));
}
int main(){
int n, s = INF;
scanf("%d", &n);
ll ans = 0;
for (int i=0;i<n;i++){
scanf("%d", a+i);
if (a[i]==-1) V.push_back(i);
else ans += a[i], s = min(s, i);
}
if (s==INF) {printf("0\n"); return 0;}
///init
int prv = -1;
for (int i=0;i<n;i++) if (a[i]!=-1){
L[i] = get_left(i), R[i] = get_right(i);
nxt[i] = INF;
//printf("%d -> %d %d %d\n", i, L[i], R[i], nxt[i]);
if (prv!=-1) nxt[prv] = i;
prv = i;
}
for (int i=0;i<303;i++){
for (int j=0;j<303;j++){
for (int k=0;k<303;k++){
for (int l=0;l<3;l++){
for (int z=0;z<2;z++) dist[i][j][k][l][z] = INF;
}
}
}
}
priority_queue<Vertex> pq;
dist[s][0][0][0][0] = s;
pq.emplace(s, 0, 0, 0, 0, s);
int rans = 0;
while(!pq.empty()){
auto [v, now, tot, p, on, tor] = pq.top(); pq.pop();
if (dist[v][now][tot][p][on] != tor) continue;
//printf("%d %d %d %d %d -> %d\n", v, now, tot, p, on, tor);
rans = max(rans, tot);
int nv = nxt[v], rv = v;
if (p==1) rv = L[v];
if (p==2) rv = R[v];
if (nv!=INF && dist[nv][0][tot][0][on] > tor + abs(nv-rv)){
dist[nv][0][tot][0][on] = tor + abs(nv-rv);
pq.emplace(nv, 0, tot, 0, on, tor + abs(nv-rv));
}
if (!on){
if (p==0){
if (now < a[v] && tor <= v*2){
if (dist[v][now+1][tot+1][0][1] > tor){
dist[v][now+1][tot+1][0][1] = tor;
pq.emplace(v, now+1, tot+1, 0, 1, tor);
}
}
}
else{
if (dist[v][now][tot][0][0] > tor + abs(rv-v)){
dist[v][now][tot][0][0] = tor + abs(rv-v);
pq.emplace(v, now, tot, 0, 0, tor + abs(rv-v));
}
}
}
else{
if (p==0){
if (dist[v][now][tot][1][1] > tor + abs(L[v]-v)){
dist[v][now][tot][1][1] = tor + abs(L[v]-v);
pq.emplace(v, now, tot, 1, 1, tor + abs(L[v]-v));
}
if (dist[v][now][tot][2][1] > tor + abs(R[v]-v)){
dist[v][now][tot][2][1] = tor + abs(R[v]-v);
pq.emplace(v, now, tot, 2, 1, tor + abs(R[v]-v));
}
}
else{
if (dist[v][now][tot][p][0] > tor){
dist[v][now][tot][p][0] = tor;
pq.emplace(v, now, tot, p, 0, tor);
}
}
}
}
printf("%lld\n", ans - rans);
return 0;
}
Compilation message
tortoise.cpp: In function 'int main()':
tortoise.cpp:31:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
tortoise.cpp:35:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | scanf("%d", a+i);
| ~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
179 ms |
326892 KB |
Output is correct |
2 |
Correct |
173 ms |
326844 KB |
Output is correct |
3 |
Correct |
173 ms |
326868 KB |
Output is correct |
4 |
Correct |
183 ms |
326912 KB |
Output is correct |
5 |
Correct |
187 ms |
326932 KB |
Output is correct |
6 |
Correct |
172 ms |
326944 KB |
Output is correct |
7 |
Correct |
184 ms |
326840 KB |
Output is correct |
8 |
Correct |
183 ms |
326984 KB |
Output is correct |
9 |
Correct |
168 ms |
326860 KB |
Output is correct |
10 |
Correct |
195 ms |
326916 KB |
Output is correct |
11 |
Correct |
165 ms |
326876 KB |
Output is correct |
12 |
Correct |
198 ms |
326960 KB |
Output is correct |
13 |
Correct |
168 ms |
326872 KB |
Output is correct |
14 |
Correct |
215 ms |
326908 KB |
Output is correct |
15 |
Correct |
162 ms |
326880 KB |
Output is correct |
16 |
Correct |
214 ms |
326872 KB |
Output is correct |
17 |
Correct |
165 ms |
326952 KB |
Output is correct |
18 |
Correct |
180 ms |
326856 KB |
Output is correct |
19 |
Correct |
174 ms |
326828 KB |
Output is correct |
20 |
Correct |
192 ms |
326880 KB |
Output is correct |
21 |
Correct |
190 ms |
326948 KB |
Output is correct |
22 |
Correct |
182 ms |
326932 KB |
Output is correct |
23 |
Correct |
164 ms |
326892 KB |
Output is correct |
24 |
Correct |
174 ms |
326848 KB |
Output is correct |
25 |
Correct |
185 ms |
326948 KB |
Output is correct |
26 |
Correct |
201 ms |
326836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
179 ms |
326892 KB |
Output is correct |
2 |
Correct |
173 ms |
326844 KB |
Output is correct |
3 |
Correct |
173 ms |
326868 KB |
Output is correct |
4 |
Correct |
183 ms |
326912 KB |
Output is correct |
5 |
Correct |
187 ms |
326932 KB |
Output is correct |
6 |
Correct |
172 ms |
326944 KB |
Output is correct |
7 |
Correct |
184 ms |
326840 KB |
Output is correct |
8 |
Correct |
183 ms |
326984 KB |
Output is correct |
9 |
Correct |
168 ms |
326860 KB |
Output is correct |
10 |
Correct |
195 ms |
326916 KB |
Output is correct |
11 |
Correct |
165 ms |
326876 KB |
Output is correct |
12 |
Correct |
198 ms |
326960 KB |
Output is correct |
13 |
Correct |
168 ms |
326872 KB |
Output is correct |
14 |
Correct |
215 ms |
326908 KB |
Output is correct |
15 |
Correct |
162 ms |
326880 KB |
Output is correct |
16 |
Correct |
214 ms |
326872 KB |
Output is correct |
17 |
Correct |
165 ms |
326952 KB |
Output is correct |
18 |
Correct |
180 ms |
326856 KB |
Output is correct |
19 |
Correct |
174 ms |
326828 KB |
Output is correct |
20 |
Correct |
192 ms |
326880 KB |
Output is correct |
21 |
Correct |
190 ms |
326948 KB |
Output is correct |
22 |
Correct |
182 ms |
326932 KB |
Output is correct |
23 |
Correct |
164 ms |
326892 KB |
Output is correct |
24 |
Correct |
174 ms |
326848 KB |
Output is correct |
25 |
Correct |
185 ms |
326948 KB |
Output is correct |
26 |
Correct |
201 ms |
326836 KB |
Output is correct |
27 |
Correct |
182 ms |
326936 KB |
Output is correct |
28 |
Correct |
191 ms |
327100 KB |
Output is correct |
29 |
Correct |
189 ms |
326964 KB |
Output is correct |
30 |
Correct |
186 ms |
327036 KB |
Output is correct |
31 |
Correct |
190 ms |
326984 KB |
Output is correct |
32 |
Correct |
210 ms |
327040 KB |
Output is correct |
33 |
Correct |
197 ms |
327004 KB |
Output is correct |
34 |
Correct |
194 ms |
327028 KB |
Output is correct |
35 |
Correct |
203 ms |
326984 KB |
Output is correct |
36 |
Correct |
202 ms |
326924 KB |
Output is correct |
37 |
Correct |
193 ms |
327016 KB |
Output is correct |
38 |
Correct |
172 ms |
326996 KB |
Output is correct |
39 |
Correct |
203 ms |
327184 KB |
Output is correct |
40 |
Correct |
182 ms |
326928 KB |
Output is correct |
41 |
Correct |
186 ms |
327036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
179 ms |
326892 KB |
Output is correct |
2 |
Correct |
173 ms |
326844 KB |
Output is correct |
3 |
Correct |
173 ms |
326868 KB |
Output is correct |
4 |
Correct |
183 ms |
326912 KB |
Output is correct |
5 |
Correct |
187 ms |
326932 KB |
Output is correct |
6 |
Correct |
172 ms |
326944 KB |
Output is correct |
7 |
Correct |
184 ms |
326840 KB |
Output is correct |
8 |
Correct |
183 ms |
326984 KB |
Output is correct |
9 |
Correct |
168 ms |
326860 KB |
Output is correct |
10 |
Correct |
195 ms |
326916 KB |
Output is correct |
11 |
Correct |
165 ms |
326876 KB |
Output is correct |
12 |
Correct |
198 ms |
326960 KB |
Output is correct |
13 |
Correct |
168 ms |
326872 KB |
Output is correct |
14 |
Correct |
215 ms |
326908 KB |
Output is correct |
15 |
Correct |
162 ms |
326880 KB |
Output is correct |
16 |
Correct |
214 ms |
326872 KB |
Output is correct |
17 |
Correct |
165 ms |
326952 KB |
Output is correct |
18 |
Correct |
180 ms |
326856 KB |
Output is correct |
19 |
Correct |
174 ms |
326828 KB |
Output is correct |
20 |
Correct |
192 ms |
326880 KB |
Output is correct |
21 |
Correct |
190 ms |
326948 KB |
Output is correct |
22 |
Correct |
182 ms |
326932 KB |
Output is correct |
23 |
Correct |
164 ms |
326892 KB |
Output is correct |
24 |
Correct |
174 ms |
326848 KB |
Output is correct |
25 |
Correct |
185 ms |
326948 KB |
Output is correct |
26 |
Correct |
201 ms |
326836 KB |
Output is correct |
27 |
Correct |
182 ms |
326936 KB |
Output is correct |
28 |
Correct |
191 ms |
327100 KB |
Output is correct |
29 |
Correct |
189 ms |
326964 KB |
Output is correct |
30 |
Correct |
186 ms |
327036 KB |
Output is correct |
31 |
Correct |
190 ms |
326984 KB |
Output is correct |
32 |
Correct |
210 ms |
327040 KB |
Output is correct |
33 |
Correct |
197 ms |
327004 KB |
Output is correct |
34 |
Correct |
194 ms |
327028 KB |
Output is correct |
35 |
Correct |
203 ms |
326984 KB |
Output is correct |
36 |
Correct |
202 ms |
326924 KB |
Output is correct |
37 |
Correct |
193 ms |
327016 KB |
Output is correct |
38 |
Correct |
172 ms |
326996 KB |
Output is correct |
39 |
Correct |
203 ms |
327184 KB |
Output is correct |
40 |
Correct |
182 ms |
326928 KB |
Output is correct |
41 |
Correct |
186 ms |
327036 KB |
Output is correct |
42 |
Correct |
189 ms |
326952 KB |
Output is correct |
43 |
Correct |
185 ms |
327024 KB |
Output is correct |
44 |
Correct |
198 ms |
326928 KB |
Output is correct |
45 |
Correct |
181 ms |
327032 KB |
Output is correct |
46 |
Correct |
170 ms |
326920 KB |
Output is correct |
47 |
Correct |
193 ms |
327028 KB |
Output is correct |
48 |
Correct |
179 ms |
327216 KB |
Output is correct |
49 |
Correct |
190 ms |
327072 KB |
Output is correct |
50 |
Correct |
298 ms |
327380 KB |
Output is correct |
51 |
Correct |
298 ms |
327400 KB |
Output is correct |
52 |
Correct |
215 ms |
327516 KB |
Output is correct |
53 |
Correct |
372 ms |
327848 KB |
Output is correct |
54 |
Correct |
569 ms |
327864 KB |
Output is correct |
55 |
Correct |
271 ms |
327756 KB |
Output is correct |
56 |
Correct |
203 ms |
327208 KB |
Output is correct |
57 |
Correct |
203 ms |
327180 KB |
Output is correct |
58 |
Correct |
186 ms |
327200 KB |
Output is correct |
59 |
Correct |
199 ms |
327220 KB |
Output is correct |
60 |
Correct |
249 ms |
327736 KB |
Output is correct |
61 |
Correct |
176 ms |
327000 KB |
Output is correct |
62 |
Correct |
215 ms |
327372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
179 ms |
326892 KB |
Output is correct |
2 |
Correct |
173 ms |
326844 KB |
Output is correct |
3 |
Correct |
173 ms |
326868 KB |
Output is correct |
4 |
Correct |
183 ms |
326912 KB |
Output is correct |
5 |
Correct |
187 ms |
326932 KB |
Output is correct |
6 |
Correct |
172 ms |
326944 KB |
Output is correct |
7 |
Correct |
184 ms |
326840 KB |
Output is correct |
8 |
Correct |
183 ms |
326984 KB |
Output is correct |
9 |
Correct |
168 ms |
326860 KB |
Output is correct |
10 |
Correct |
195 ms |
326916 KB |
Output is correct |
11 |
Correct |
165 ms |
326876 KB |
Output is correct |
12 |
Correct |
198 ms |
326960 KB |
Output is correct |
13 |
Correct |
168 ms |
326872 KB |
Output is correct |
14 |
Correct |
215 ms |
326908 KB |
Output is correct |
15 |
Correct |
162 ms |
326880 KB |
Output is correct |
16 |
Correct |
214 ms |
326872 KB |
Output is correct |
17 |
Correct |
165 ms |
326952 KB |
Output is correct |
18 |
Correct |
180 ms |
326856 KB |
Output is correct |
19 |
Correct |
174 ms |
326828 KB |
Output is correct |
20 |
Correct |
192 ms |
326880 KB |
Output is correct |
21 |
Correct |
190 ms |
326948 KB |
Output is correct |
22 |
Correct |
182 ms |
326932 KB |
Output is correct |
23 |
Correct |
164 ms |
326892 KB |
Output is correct |
24 |
Correct |
174 ms |
326848 KB |
Output is correct |
25 |
Correct |
185 ms |
326948 KB |
Output is correct |
26 |
Correct |
201 ms |
326836 KB |
Output is correct |
27 |
Correct |
182 ms |
326936 KB |
Output is correct |
28 |
Correct |
191 ms |
327100 KB |
Output is correct |
29 |
Correct |
189 ms |
326964 KB |
Output is correct |
30 |
Correct |
186 ms |
327036 KB |
Output is correct |
31 |
Correct |
190 ms |
326984 KB |
Output is correct |
32 |
Correct |
210 ms |
327040 KB |
Output is correct |
33 |
Correct |
197 ms |
327004 KB |
Output is correct |
34 |
Correct |
194 ms |
327028 KB |
Output is correct |
35 |
Correct |
203 ms |
326984 KB |
Output is correct |
36 |
Correct |
202 ms |
326924 KB |
Output is correct |
37 |
Correct |
193 ms |
327016 KB |
Output is correct |
38 |
Correct |
172 ms |
326996 KB |
Output is correct |
39 |
Correct |
203 ms |
327184 KB |
Output is correct |
40 |
Correct |
182 ms |
326928 KB |
Output is correct |
41 |
Correct |
186 ms |
327036 KB |
Output is correct |
42 |
Correct |
189 ms |
326952 KB |
Output is correct |
43 |
Correct |
185 ms |
327024 KB |
Output is correct |
44 |
Correct |
198 ms |
326928 KB |
Output is correct |
45 |
Correct |
181 ms |
327032 KB |
Output is correct |
46 |
Correct |
170 ms |
326920 KB |
Output is correct |
47 |
Correct |
193 ms |
327028 KB |
Output is correct |
48 |
Correct |
179 ms |
327216 KB |
Output is correct |
49 |
Correct |
190 ms |
327072 KB |
Output is correct |
50 |
Correct |
298 ms |
327380 KB |
Output is correct |
51 |
Correct |
298 ms |
327400 KB |
Output is correct |
52 |
Correct |
215 ms |
327516 KB |
Output is correct |
53 |
Correct |
372 ms |
327848 KB |
Output is correct |
54 |
Correct |
569 ms |
327864 KB |
Output is correct |
55 |
Correct |
271 ms |
327756 KB |
Output is correct |
56 |
Correct |
203 ms |
327208 KB |
Output is correct |
57 |
Correct |
203 ms |
327180 KB |
Output is correct |
58 |
Correct |
186 ms |
327200 KB |
Output is correct |
59 |
Correct |
199 ms |
327220 KB |
Output is correct |
60 |
Correct |
249 ms |
327736 KB |
Output is correct |
61 |
Correct |
176 ms |
327000 KB |
Output is correct |
62 |
Correct |
215 ms |
327372 KB |
Output is correct |
63 |
Incorrect |
192 ms |
327048 KB |
Output isn't correct |
64 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
179 ms |
326892 KB |
Output is correct |
2 |
Correct |
173 ms |
326844 KB |
Output is correct |
3 |
Correct |
173 ms |
326868 KB |
Output is correct |
4 |
Correct |
183 ms |
326912 KB |
Output is correct |
5 |
Correct |
187 ms |
326932 KB |
Output is correct |
6 |
Correct |
172 ms |
326944 KB |
Output is correct |
7 |
Correct |
184 ms |
326840 KB |
Output is correct |
8 |
Correct |
183 ms |
326984 KB |
Output is correct |
9 |
Correct |
168 ms |
326860 KB |
Output is correct |
10 |
Correct |
195 ms |
326916 KB |
Output is correct |
11 |
Correct |
165 ms |
326876 KB |
Output is correct |
12 |
Correct |
198 ms |
326960 KB |
Output is correct |
13 |
Correct |
168 ms |
326872 KB |
Output is correct |
14 |
Correct |
215 ms |
326908 KB |
Output is correct |
15 |
Correct |
162 ms |
326880 KB |
Output is correct |
16 |
Correct |
214 ms |
326872 KB |
Output is correct |
17 |
Correct |
165 ms |
326952 KB |
Output is correct |
18 |
Correct |
180 ms |
326856 KB |
Output is correct |
19 |
Correct |
174 ms |
326828 KB |
Output is correct |
20 |
Correct |
192 ms |
326880 KB |
Output is correct |
21 |
Correct |
190 ms |
326948 KB |
Output is correct |
22 |
Correct |
182 ms |
326932 KB |
Output is correct |
23 |
Correct |
164 ms |
326892 KB |
Output is correct |
24 |
Correct |
174 ms |
326848 KB |
Output is correct |
25 |
Correct |
185 ms |
326948 KB |
Output is correct |
26 |
Correct |
201 ms |
326836 KB |
Output is correct |
27 |
Correct |
182 ms |
326936 KB |
Output is correct |
28 |
Correct |
191 ms |
327100 KB |
Output is correct |
29 |
Correct |
189 ms |
326964 KB |
Output is correct |
30 |
Correct |
186 ms |
327036 KB |
Output is correct |
31 |
Correct |
190 ms |
326984 KB |
Output is correct |
32 |
Correct |
210 ms |
327040 KB |
Output is correct |
33 |
Correct |
197 ms |
327004 KB |
Output is correct |
34 |
Correct |
194 ms |
327028 KB |
Output is correct |
35 |
Correct |
203 ms |
326984 KB |
Output is correct |
36 |
Correct |
202 ms |
326924 KB |
Output is correct |
37 |
Correct |
193 ms |
327016 KB |
Output is correct |
38 |
Correct |
172 ms |
326996 KB |
Output is correct |
39 |
Correct |
203 ms |
327184 KB |
Output is correct |
40 |
Correct |
182 ms |
326928 KB |
Output is correct |
41 |
Correct |
186 ms |
327036 KB |
Output is correct |
42 |
Correct |
189 ms |
326952 KB |
Output is correct |
43 |
Correct |
185 ms |
327024 KB |
Output is correct |
44 |
Correct |
198 ms |
326928 KB |
Output is correct |
45 |
Correct |
181 ms |
327032 KB |
Output is correct |
46 |
Correct |
170 ms |
326920 KB |
Output is correct |
47 |
Correct |
193 ms |
327028 KB |
Output is correct |
48 |
Correct |
179 ms |
327216 KB |
Output is correct |
49 |
Correct |
190 ms |
327072 KB |
Output is correct |
50 |
Correct |
298 ms |
327380 KB |
Output is correct |
51 |
Correct |
298 ms |
327400 KB |
Output is correct |
52 |
Correct |
215 ms |
327516 KB |
Output is correct |
53 |
Correct |
372 ms |
327848 KB |
Output is correct |
54 |
Correct |
569 ms |
327864 KB |
Output is correct |
55 |
Correct |
271 ms |
327756 KB |
Output is correct |
56 |
Correct |
203 ms |
327208 KB |
Output is correct |
57 |
Correct |
203 ms |
327180 KB |
Output is correct |
58 |
Correct |
186 ms |
327200 KB |
Output is correct |
59 |
Correct |
199 ms |
327220 KB |
Output is correct |
60 |
Correct |
249 ms |
327736 KB |
Output is correct |
61 |
Correct |
176 ms |
327000 KB |
Output is correct |
62 |
Correct |
215 ms |
327372 KB |
Output is correct |
63 |
Incorrect |
192 ms |
327048 KB |
Output isn't correct |
64 |
Halted |
0 ms |
0 KB |
- |