#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2,fma")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define ll long long
#define f first
#define s second
#define pb push_back
using namespace std;
int n,a[1000002];
ll dp[1000002];
int rm[1000002][21],po[2000002],pw[200],raod,mn,lo,pr[2000002];
int lef[1000002],rig[1000002];
vector<int>del[1000002];
int get(int x,int y){
return pr[y];
}
int ind(int x,int y){
if(y == n)return -1;
ll l = y + 1,r = n,mid,ind = -1;
while(r >= l){
mid = (l + r) / 2;
if(dp[x] + (ll)get(x + 1 , mid) * (n - x) > dp[y] + (ll)get(y + 1 , mid) * (n - y)){
r = mid - 1;
ind = mid;
}
else
l = mid + 1;
}
return ind;
}
void delet(int x){
if(lef[x] == rig[x] && lef[x] == -1)return;
if(lef[x] == -1){
lef[rig[x]] = -1;
mn = rig[x];
rig[x] = -1;
return;
}
rig[lef[x]] = rig[x];
lef[rig[x]] = lef[x];
int cur = ind(lef[x] , rig[x]);
if(cur == -1){
lef[x] = rig[x] = -1;
return;
}
del[cur].pb(lef[x]);
lef[x] = rig[x] = -1;
}
int main(){
scanf("%d",&n);
for(int i=1; i<=n; i++){
scanf("%d",&a[i]);
pr[i] = max(a[i] , pr[i - 1]);
}
for(int i=0; i<=n; i++)
lef[i] = rig[i] = -1;
for(int i=1; i<=n; i++){
for(int j=0; j<del[i].size(); j++){
delet(del[i][j]);
}
dp[i] = dp[mn] + (ll)get(mn + 1 , i) * (ll)(n - mn);
rig[i - 1] = i;
lef[i] = i - 1;
ll cur = ind(i - 1 , i);
if(cur != -1)
del[cur].pb(i - 1);
}
//cout << "time: " << clock() - cl << " " << raod << '\n';
cout << dp[n];
return 0;
}
Compilation message
Discharging.cpp:5: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
5 | #pragma GCC optimization ("O3")
|
Discharging.cpp:6: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
6 | #pragma GCC optimization ("unroll-loops")
|
Discharging.cpp: In function 'int main()':
Discharging.cpp:61:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int j=0; j<del[i].size(); j++){
| ~^~~~~~~~~~~~~~
Discharging.cpp:53:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
53 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
Discharging.cpp:55:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
55 | scanf("%d",&a[i]);
| ~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23900 KB |
Output is correct |
2 |
Correct |
16 ms |
23912 KB |
Output is correct |
3 |
Correct |
16 ms |
23808 KB |
Output is correct |
4 |
Correct |
17 ms |
23808 KB |
Output is correct |
5 |
Correct |
17 ms |
23808 KB |
Output is correct |
6 |
Correct |
19 ms |
23808 KB |
Output is correct |
7 |
Correct |
19 ms |
23808 KB |
Output is correct |
8 |
Correct |
16 ms |
23936 KB |
Output is correct |
9 |
Correct |
16 ms |
23808 KB |
Output is correct |
10 |
Correct |
15 ms |
23808 KB |
Output is correct |
11 |
Correct |
19 ms |
23808 KB |
Output is correct |
12 |
Correct |
19 ms |
23808 KB |
Output is correct |
13 |
Correct |
15 ms |
23808 KB |
Output is correct |
14 |
Correct |
15 ms |
23808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23936 KB |
Output is correct |
2 |
Correct |
19 ms |
23936 KB |
Output is correct |
3 |
Correct |
17 ms |
23936 KB |
Output is correct |
4 |
Correct |
19 ms |
23936 KB |
Output is correct |
5 |
Correct |
17 ms |
23936 KB |
Output is correct |
6 |
Correct |
17 ms |
23936 KB |
Output is correct |
7 |
Correct |
17 ms |
23928 KB |
Output is correct |
8 |
Correct |
18 ms |
23928 KB |
Output is correct |
9 |
Correct |
17 ms |
23936 KB |
Output is correct |
10 |
Correct |
17 ms |
23928 KB |
Output is correct |
11 |
Correct |
17 ms |
23936 KB |
Output is correct |
12 |
Correct |
17 ms |
23936 KB |
Output is correct |
13 |
Correct |
17 ms |
23936 KB |
Output is correct |
14 |
Correct |
17 ms |
23936 KB |
Output is correct |
15 |
Correct |
17 ms |
23936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23936 KB |
Output is correct |
2 |
Correct |
19 ms |
23936 KB |
Output is correct |
3 |
Correct |
17 ms |
23936 KB |
Output is correct |
4 |
Correct |
19 ms |
23936 KB |
Output is correct |
5 |
Correct |
17 ms |
23936 KB |
Output is correct |
6 |
Correct |
17 ms |
23936 KB |
Output is correct |
7 |
Correct |
17 ms |
23928 KB |
Output is correct |
8 |
Correct |
18 ms |
23928 KB |
Output is correct |
9 |
Correct |
17 ms |
23936 KB |
Output is correct |
10 |
Correct |
17 ms |
23928 KB |
Output is correct |
11 |
Correct |
17 ms |
23936 KB |
Output is correct |
12 |
Correct |
17 ms |
23936 KB |
Output is correct |
13 |
Correct |
17 ms |
23936 KB |
Output is correct |
14 |
Correct |
17 ms |
23936 KB |
Output is correct |
15 |
Correct |
17 ms |
23936 KB |
Output is correct |
16 |
Correct |
553 ms |
82064 KB |
Output is correct |
17 |
Correct |
631 ms |
78944 KB |
Output is correct |
18 |
Correct |
508 ms |
81908 KB |
Output is correct |
19 |
Correct |
573 ms |
77944 KB |
Output is correct |
20 |
Correct |
624 ms |
77816 KB |
Output is correct |
21 |
Correct |
629 ms |
78800 KB |
Output is correct |
22 |
Correct |
574 ms |
79176 KB |
Output is correct |
23 |
Correct |
673 ms |
76024 KB |
Output is correct |
24 |
Correct |
612 ms |
77108 KB |
Output is correct |
25 |
Correct |
612 ms |
76660 KB |
Output is correct |
26 |
Correct |
683 ms |
74748 KB |
Output is correct |
27 |
Correct |
528 ms |
80508 KB |
Output is correct |
28 |
Correct |
602 ms |
78456 KB |
Output is correct |
29 |
Correct |
608 ms |
75892 KB |
Output is correct |
30 |
Correct |
645 ms |
75436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
367 ms |
78584 KB |
Output is correct |
2 |
Correct |
358 ms |
78584 KB |
Output is correct |
3 |
Correct |
383 ms |
78584 KB |
Output is correct |
4 |
Correct |
365 ms |
78584 KB |
Output is correct |
5 |
Correct |
385 ms |
78748 KB |
Output is correct |
6 |
Correct |
366 ms |
78584 KB |
Output is correct |
7 |
Correct |
365 ms |
78584 KB |
Output is correct |
8 |
Correct |
370 ms |
78712 KB |
Output is correct |
9 |
Correct |
361 ms |
78584 KB |
Output is correct |
10 |
Correct |
364 ms |
78584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23900 KB |
Output is correct |
2 |
Correct |
16 ms |
23912 KB |
Output is correct |
3 |
Correct |
16 ms |
23808 KB |
Output is correct |
4 |
Correct |
17 ms |
23808 KB |
Output is correct |
5 |
Correct |
17 ms |
23808 KB |
Output is correct |
6 |
Correct |
19 ms |
23808 KB |
Output is correct |
7 |
Correct |
19 ms |
23808 KB |
Output is correct |
8 |
Correct |
16 ms |
23936 KB |
Output is correct |
9 |
Correct |
16 ms |
23808 KB |
Output is correct |
10 |
Correct |
15 ms |
23808 KB |
Output is correct |
11 |
Correct |
19 ms |
23808 KB |
Output is correct |
12 |
Correct |
19 ms |
23808 KB |
Output is correct |
13 |
Correct |
15 ms |
23808 KB |
Output is correct |
14 |
Correct |
15 ms |
23808 KB |
Output is correct |
15 |
Correct |
17 ms |
23936 KB |
Output is correct |
16 |
Correct |
19 ms |
23936 KB |
Output is correct |
17 |
Correct |
17 ms |
23936 KB |
Output is correct |
18 |
Correct |
19 ms |
23936 KB |
Output is correct |
19 |
Correct |
17 ms |
23936 KB |
Output is correct |
20 |
Correct |
17 ms |
23936 KB |
Output is correct |
21 |
Correct |
17 ms |
23928 KB |
Output is correct |
22 |
Correct |
18 ms |
23928 KB |
Output is correct |
23 |
Correct |
17 ms |
23936 KB |
Output is correct |
24 |
Correct |
17 ms |
23928 KB |
Output is correct |
25 |
Correct |
17 ms |
23936 KB |
Output is correct |
26 |
Correct |
17 ms |
23936 KB |
Output is correct |
27 |
Correct |
17 ms |
23936 KB |
Output is correct |
28 |
Correct |
17 ms |
23936 KB |
Output is correct |
29 |
Correct |
17 ms |
23936 KB |
Output is correct |
30 |
Correct |
17 ms |
23936 KB |
Output is correct |
31 |
Correct |
17 ms |
23936 KB |
Output is correct |
32 |
Correct |
17 ms |
23936 KB |
Output is correct |
33 |
Correct |
17 ms |
23932 KB |
Output is correct |
34 |
Correct |
17 ms |
23936 KB |
Output is correct |
35 |
Correct |
20 ms |
23928 KB |
Output is correct |
36 |
Correct |
17 ms |
24064 KB |
Output is correct |
37 |
Correct |
17 ms |
23936 KB |
Output is correct |
38 |
Correct |
20 ms |
23936 KB |
Output is correct |
39 |
Correct |
16 ms |
23936 KB |
Output is correct |
40 |
Correct |
17 ms |
23936 KB |
Output is correct |
41 |
Correct |
18 ms |
23936 KB |
Output is correct |
42 |
Correct |
20 ms |
23936 KB |
Output is correct |
43 |
Correct |
18 ms |
23928 KB |
Output is correct |
44 |
Correct |
17 ms |
23936 KB |
Output is correct |
45 |
Correct |
16 ms |
23936 KB |
Output is correct |
46 |
Correct |
16 ms |
23936 KB |
Output is correct |
47 |
Correct |
17 ms |
23936 KB |
Output is correct |
48 |
Correct |
17 ms |
23936 KB |
Output is correct |
49 |
Correct |
17 ms |
23936 KB |
Output is correct |
50 |
Correct |
17 ms |
23936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23900 KB |
Output is correct |
2 |
Correct |
16 ms |
23912 KB |
Output is correct |
3 |
Correct |
16 ms |
23808 KB |
Output is correct |
4 |
Correct |
17 ms |
23808 KB |
Output is correct |
5 |
Correct |
17 ms |
23808 KB |
Output is correct |
6 |
Correct |
19 ms |
23808 KB |
Output is correct |
7 |
Correct |
19 ms |
23808 KB |
Output is correct |
8 |
Correct |
16 ms |
23936 KB |
Output is correct |
9 |
Correct |
16 ms |
23808 KB |
Output is correct |
10 |
Correct |
15 ms |
23808 KB |
Output is correct |
11 |
Correct |
19 ms |
23808 KB |
Output is correct |
12 |
Correct |
19 ms |
23808 KB |
Output is correct |
13 |
Correct |
15 ms |
23808 KB |
Output is correct |
14 |
Correct |
15 ms |
23808 KB |
Output is correct |
15 |
Correct |
17 ms |
23936 KB |
Output is correct |
16 |
Correct |
19 ms |
23936 KB |
Output is correct |
17 |
Correct |
17 ms |
23936 KB |
Output is correct |
18 |
Correct |
19 ms |
23936 KB |
Output is correct |
19 |
Correct |
17 ms |
23936 KB |
Output is correct |
20 |
Correct |
17 ms |
23936 KB |
Output is correct |
21 |
Correct |
17 ms |
23928 KB |
Output is correct |
22 |
Correct |
18 ms |
23928 KB |
Output is correct |
23 |
Correct |
17 ms |
23936 KB |
Output is correct |
24 |
Correct |
17 ms |
23928 KB |
Output is correct |
25 |
Correct |
17 ms |
23936 KB |
Output is correct |
26 |
Correct |
17 ms |
23936 KB |
Output is correct |
27 |
Correct |
17 ms |
23936 KB |
Output is correct |
28 |
Correct |
17 ms |
23936 KB |
Output is correct |
29 |
Correct |
17 ms |
23936 KB |
Output is correct |
30 |
Correct |
553 ms |
82064 KB |
Output is correct |
31 |
Correct |
631 ms |
78944 KB |
Output is correct |
32 |
Correct |
508 ms |
81908 KB |
Output is correct |
33 |
Correct |
573 ms |
77944 KB |
Output is correct |
34 |
Correct |
624 ms |
77816 KB |
Output is correct |
35 |
Correct |
629 ms |
78800 KB |
Output is correct |
36 |
Correct |
574 ms |
79176 KB |
Output is correct |
37 |
Correct |
673 ms |
76024 KB |
Output is correct |
38 |
Correct |
612 ms |
77108 KB |
Output is correct |
39 |
Correct |
612 ms |
76660 KB |
Output is correct |
40 |
Correct |
683 ms |
74748 KB |
Output is correct |
41 |
Correct |
528 ms |
80508 KB |
Output is correct |
42 |
Correct |
602 ms |
78456 KB |
Output is correct |
43 |
Correct |
608 ms |
75892 KB |
Output is correct |
44 |
Correct |
645 ms |
75436 KB |
Output is correct |
45 |
Correct |
367 ms |
78584 KB |
Output is correct |
46 |
Correct |
358 ms |
78584 KB |
Output is correct |
47 |
Correct |
383 ms |
78584 KB |
Output is correct |
48 |
Correct |
365 ms |
78584 KB |
Output is correct |
49 |
Correct |
385 ms |
78748 KB |
Output is correct |
50 |
Correct |
366 ms |
78584 KB |
Output is correct |
51 |
Correct |
365 ms |
78584 KB |
Output is correct |
52 |
Correct |
370 ms |
78712 KB |
Output is correct |
53 |
Correct |
361 ms |
78584 KB |
Output is correct |
54 |
Correct |
364 ms |
78584 KB |
Output is correct |
55 |
Correct |
17 ms |
23936 KB |
Output is correct |
56 |
Correct |
17 ms |
23936 KB |
Output is correct |
57 |
Correct |
17 ms |
23936 KB |
Output is correct |
58 |
Correct |
17 ms |
23932 KB |
Output is correct |
59 |
Correct |
17 ms |
23936 KB |
Output is correct |
60 |
Correct |
20 ms |
23928 KB |
Output is correct |
61 |
Correct |
17 ms |
24064 KB |
Output is correct |
62 |
Correct |
17 ms |
23936 KB |
Output is correct |
63 |
Correct |
20 ms |
23936 KB |
Output is correct |
64 |
Correct |
16 ms |
23936 KB |
Output is correct |
65 |
Correct |
17 ms |
23936 KB |
Output is correct |
66 |
Correct |
18 ms |
23936 KB |
Output is correct |
67 |
Correct |
20 ms |
23936 KB |
Output is correct |
68 |
Correct |
18 ms |
23928 KB |
Output is correct |
69 |
Correct |
17 ms |
23936 KB |
Output is correct |
70 |
Correct |
16 ms |
23936 KB |
Output is correct |
71 |
Correct |
16 ms |
23936 KB |
Output is correct |
72 |
Correct |
17 ms |
23936 KB |
Output is correct |
73 |
Correct |
17 ms |
23936 KB |
Output is correct |
74 |
Correct |
17 ms |
23936 KB |
Output is correct |
75 |
Correct |
17 ms |
23936 KB |
Output is correct |
76 |
Correct |
454 ms |
83772 KB |
Output is correct |
77 |
Correct |
388 ms |
83568 KB |
Output is correct |
78 |
Correct |
387 ms |
83952 KB |
Output is correct |
79 |
Correct |
385 ms |
83616 KB |
Output is correct |
80 |
Correct |
387 ms |
83436 KB |
Output is correct |
81 |
Correct |
385 ms |
83824 KB |
Output is correct |
82 |
Correct |
397 ms |
83588 KB |
Output is correct |
83 |
Correct |
385 ms |
83212 KB |
Output is correct |
84 |
Correct |
390 ms |
83440 KB |
Output is correct |
85 |
Correct |
386 ms |
83440 KB |
Output is correct |
86 |
Correct |
381 ms |
83440 KB |
Output is correct |
87 |
Correct |
384 ms |
83476 KB |
Output is correct |
88 |
Correct |
392 ms |
83336 KB |
Output is correct |
89 |
Correct |
390 ms |
83696 KB |
Output is correct |
90 |
Correct |
379 ms |
83696 KB |
Output is correct |
91 |
Correct |
382 ms |
83440 KB |
Output is correct |
92 |
Correct |
635 ms |
74892 KB |
Output is correct |
93 |
Correct |
586 ms |
76216 KB |
Output is correct |
94 |
Correct |
379 ms |
83056 KB |
Output is correct |
95 |
Correct |
392 ms |
82288 KB |
Output is correct |
96 |
Correct |
383 ms |
82536 KB |
Output is correct |
97 |
Correct |
384 ms |
82800 KB |
Output is correct |
98 |
Correct |
394 ms |
82928 KB |
Output is correct |
99 |
Correct |
383 ms |
82156 KB |
Output is correct |
100 |
Correct |
384 ms |
82412 KB |
Output is correct |