#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const ll mxn = 1e6+10;
vector<pll> v;
ll sum[mxn],dp;
deque<pll> dq;
int n;
ll total = 0;
inline ll calc(pll line,ll val){
return line.fs*val+line.sc;
}
inline bool out(pll a,pll b,pll c){
return (a.sc-b.sc)*(c.fs-b.fs)>=(b.sc-c.sc)*(b.fs-a.fs);
}
main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
for(int i = 0;i<n;i++){
int k;
cin>>k;
if(v.empty()||v.back().fs < k)v.push_back({k,1});
else v.back().sc++;
}
dq.push_back(make_pair(0ll,0ll));
sum[0] = v[0].sc;
for(int i = 1;i<v.size();i++){
sum[i] = sum[i-1]+v[i].sc;
}
for(int i = 0;i<v.size();i++){
pll p = v[i];
while(dq.size()>1&&calc(dq[1],p.fs)<=calc(dq[0],p.fs))dq.pop_front();
dp = p.fs*n+calc(dq.front(),p.fs);
pll now = make_pair(-sum[i],dp);
while(dq.size()>1&&out(dq.end()[-2],dq.end()[-1],now))dq.pop_back();
dq.push_back(make_pair(-sum[i],dp));
}
cout<<dp;
}
Compilation message
Discharging.cpp:27:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
27 | main(){
| ^~~~
Discharging.cpp: In function 'int main()':
Discharging.cpp:38:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int i = 1;i<v.size();i++){
| ~^~~~~~~~~
Discharging.cpp:41:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(int i = 0;i<v.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
63 ms |
15976 KB |
Output is correct |
17 |
Correct |
68 ms |
18356 KB |
Output is correct |
18 |
Correct |
57 ms |
13488 KB |
Output is correct |
19 |
Correct |
68 ms |
17444 KB |
Output is correct |
20 |
Correct |
70 ms |
18624 KB |
Output is correct |
21 |
Correct |
67 ms |
17748 KB |
Output is correct |
22 |
Correct |
71 ms |
18384 KB |
Output is correct |
23 |
Correct |
76 ms |
24740 KB |
Output is correct |
24 |
Correct |
72 ms |
23740 KB |
Output is correct |
25 |
Correct |
76 ms |
25108 KB |
Output is correct |
26 |
Correct |
81 ms |
23832 KB |
Output is correct |
27 |
Correct |
61 ms |
16660 KB |
Output is correct |
28 |
Correct |
69 ms |
17844 KB |
Output is correct |
29 |
Correct |
75 ms |
23692 KB |
Output is correct |
30 |
Correct |
86 ms |
24412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
436 KB |
Output is correct |
2 |
Correct |
69 ms |
348 KB |
Output is correct |
3 |
Correct |
69 ms |
440 KB |
Output is correct |
4 |
Correct |
69 ms |
440 KB |
Output is correct |
5 |
Correct |
69 ms |
348 KB |
Output is correct |
6 |
Correct |
69 ms |
348 KB |
Output is correct |
7 |
Correct |
69 ms |
348 KB |
Output is correct |
8 |
Correct |
68 ms |
344 KB |
Output is correct |
9 |
Correct |
68 ms |
444 KB |
Output is correct |
10 |
Correct |
77 ms |
436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
472 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
464 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
344 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
63 ms |
15976 KB |
Output is correct |
31 |
Correct |
68 ms |
18356 KB |
Output is correct |
32 |
Correct |
57 ms |
13488 KB |
Output is correct |
33 |
Correct |
68 ms |
17444 KB |
Output is correct |
34 |
Correct |
70 ms |
18624 KB |
Output is correct |
35 |
Correct |
67 ms |
17748 KB |
Output is correct |
36 |
Correct |
71 ms |
18384 KB |
Output is correct |
37 |
Correct |
76 ms |
24740 KB |
Output is correct |
38 |
Correct |
72 ms |
23740 KB |
Output is correct |
39 |
Correct |
76 ms |
25108 KB |
Output is correct |
40 |
Correct |
81 ms |
23832 KB |
Output is correct |
41 |
Correct |
61 ms |
16660 KB |
Output is correct |
42 |
Correct |
69 ms |
17844 KB |
Output is correct |
43 |
Correct |
75 ms |
23692 KB |
Output is correct |
44 |
Correct |
86 ms |
24412 KB |
Output is correct |
45 |
Correct |
68 ms |
436 KB |
Output is correct |
46 |
Correct |
69 ms |
348 KB |
Output is correct |
47 |
Correct |
69 ms |
440 KB |
Output is correct |
48 |
Correct |
69 ms |
440 KB |
Output is correct |
49 |
Correct |
69 ms |
348 KB |
Output is correct |
50 |
Correct |
69 ms |
348 KB |
Output is correct |
51 |
Correct |
69 ms |
348 KB |
Output is correct |
52 |
Correct |
68 ms |
344 KB |
Output is correct |
53 |
Correct |
68 ms |
444 KB |
Output is correct |
54 |
Correct |
77 ms |
436 KB |
Output is correct |
55 |
Correct |
1 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
472 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
0 ms |
348 KB |
Output is correct |
68 |
Correct |
0 ms |
348 KB |
Output is correct |
69 |
Correct |
0 ms |
464 KB |
Output is correct |
70 |
Correct |
0 ms |
348 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
344 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
60 ms |
6332 KB |
Output is correct |
77 |
Correct |
59 ms |
6448 KB |
Output is correct |
78 |
Correct |
59 ms |
5716 KB |
Output is correct |
79 |
Correct |
56 ms |
5968 KB |
Output is correct |
80 |
Correct |
60 ms |
6736 KB |
Output is correct |
81 |
Correct |
56 ms |
5968 KB |
Output is correct |
82 |
Correct |
55 ms |
5712 KB |
Output is correct |
83 |
Correct |
58 ms |
6480 KB |
Output is correct |
84 |
Correct |
63 ms |
6228 KB |
Output is correct |
85 |
Correct |
59 ms |
6324 KB |
Output is correct |
86 |
Correct |
55 ms |
5716 KB |
Output is correct |
87 |
Correct |
59 ms |
5716 KB |
Output is correct |
88 |
Correct |
57 ms |
5968 KB |
Output is correct |
89 |
Correct |
57 ms |
5972 KB |
Output is correct |
90 |
Correct |
60 ms |
5928 KB |
Output is correct |
91 |
Correct |
64 ms |
5460 KB |
Output is correct |
92 |
Correct |
75 ms |
19388 KB |
Output is correct |
93 |
Correct |
68 ms |
17592 KB |
Output is correct |
94 |
Correct |
54 ms |
5292 KB |
Output is correct |
95 |
Correct |
66 ms |
6740 KB |
Output is correct |
96 |
Correct |
54 ms |
5552 KB |
Output is correct |
97 |
Correct |
55 ms |
5968 KB |
Output is correct |
98 |
Correct |
57 ms |
6064 KB |
Output is correct |
99 |
Correct |
58 ms |
5808 KB |
Output is correct |
100 |
Correct |
63 ms |
6200 KB |
Output is correct |