#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
using namespace std;
ll n,a[1000002],dp[1000002],rm[1000002][21],po[2000002],pw[200];
vector<ll>del[1000002];
set<ll>st;
set<ll>::iterator it;
ll get(ll x,ll y){
ll lo = po[y - x + 1];
return max(rm[y][lo] , rm[x + pw[lo] - 1][lo]);
}
ll ind(ll x,ll 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] + get(x + 1 , mid) * (n - x) >= dp[y] + get(y + 1 , mid) * (n - y)){
r = mid - 1;
ind = mid;
}
else
l = mid + 1;
}
return ind;
}
void delet(ll x){
if(st.find(x) == st.end())return;
if(st.find(x) == st.begin()){
st.erase(st.begin());
return;
}
if(st.upper_bound(x) == st.end()){
st.erase(st.find(x));
return;
}
st.erase(st.find(x));
it = st.upper_bound(x);
ll y = (*it);
it--;
x = (*it);
ll cur = ind(x , y);
if(cur == -1)return;
del[cur].pb(x);
}
int main(){
ios::sync_with_stdio(false);
cin >> n;
for(int i=1; i<=n; i++){
cin >> a[i];
}
pw[0] = 1;
for(int i=1; i<=21; i++){
pw[i] = pw[i - 1] * 2;
for(int j=pw[i - 1]; j<pw[i]; j++){
if(j <= 1000000)
po[j] = i - 1;
}
}
for(int i=1; i<=n; i++){
rm[i][0] = a[i];
for(int j=1; j<=20; j++){
if(i - pw[j] + 1 >= 1)
rm[i][j] = max(rm[i][j - 1] , rm[i - pw[j - 1]][j - 1]);
}
}
st.insert(0);
for(int i=1; i<=n; i++){
for(int j=0; j<del[i].size(); j++){
delet(del[i][j]);
}
ll x = (*st.begin());
dp[i] = dp[x] + get(x + 1 , i) * (n - x);
it = st.end();
it--;
st.insert(i);
ll cur = ind((*it) , i);
if(cur != -1)
del[cur].pb((*it));
}
cout << dp[n];
return 0;
}
Compilation message
Discharging.cpp: In function 'int main()':
Discharging.cpp:77:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for(int j=0; j<del[i].size(); j++){
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
31616 KB |
Output is correct |
2 |
Correct |
22 ms |
31744 KB |
Output is correct |
3 |
Correct |
23 ms |
31616 KB |
Output is correct |
4 |
Correct |
23 ms |
31744 KB |
Output is correct |
5 |
Correct |
23 ms |
31616 KB |
Output is correct |
6 |
Correct |
23 ms |
31616 KB |
Output is correct |
7 |
Correct |
22 ms |
31616 KB |
Output is correct |
8 |
Correct |
21 ms |
31616 KB |
Output is correct |
9 |
Correct |
22 ms |
31616 KB |
Output is correct |
10 |
Correct |
22 ms |
31616 KB |
Output is correct |
11 |
Correct |
23 ms |
31616 KB |
Output is correct |
12 |
Correct |
21 ms |
31744 KB |
Output is correct |
13 |
Correct |
29 ms |
31744 KB |
Output is correct |
14 |
Correct |
23 ms |
31744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
31992 KB |
Output is correct |
2 |
Correct |
23 ms |
32000 KB |
Output is correct |
3 |
Correct |
22 ms |
32000 KB |
Output is correct |
4 |
Correct |
24 ms |
32000 KB |
Output is correct |
5 |
Correct |
25 ms |
32000 KB |
Output is correct |
6 |
Correct |
22 ms |
32032 KB |
Output is correct |
7 |
Correct |
24 ms |
32000 KB |
Output is correct |
8 |
Correct |
26 ms |
32000 KB |
Output is correct |
9 |
Correct |
26 ms |
31992 KB |
Output is correct |
10 |
Correct |
24 ms |
32000 KB |
Output is correct |
11 |
Correct |
24 ms |
32000 KB |
Output is correct |
12 |
Correct |
25 ms |
32000 KB |
Output is correct |
13 |
Correct |
25 ms |
31988 KB |
Output is correct |
14 |
Correct |
25 ms |
32000 KB |
Output is correct |
15 |
Correct |
25 ms |
32000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
31992 KB |
Output is correct |
2 |
Correct |
23 ms |
32000 KB |
Output is correct |
3 |
Correct |
22 ms |
32000 KB |
Output is correct |
4 |
Correct |
24 ms |
32000 KB |
Output is correct |
5 |
Correct |
25 ms |
32000 KB |
Output is correct |
6 |
Correct |
22 ms |
32032 KB |
Output is correct |
7 |
Correct |
24 ms |
32000 KB |
Output is correct |
8 |
Correct |
26 ms |
32000 KB |
Output is correct |
9 |
Correct |
26 ms |
31992 KB |
Output is correct |
10 |
Correct |
24 ms |
32000 KB |
Output is correct |
11 |
Correct |
24 ms |
32000 KB |
Output is correct |
12 |
Correct |
25 ms |
32000 KB |
Output is correct |
13 |
Correct |
25 ms |
31988 KB |
Output is correct |
14 |
Correct |
25 ms |
32000 KB |
Output is correct |
15 |
Correct |
25 ms |
32000 KB |
Output is correct |
16 |
Execution timed out |
1100 ms |
233524 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1093 ms |
239704 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
31616 KB |
Output is correct |
2 |
Correct |
22 ms |
31744 KB |
Output is correct |
3 |
Correct |
23 ms |
31616 KB |
Output is correct |
4 |
Correct |
23 ms |
31744 KB |
Output is correct |
5 |
Correct |
23 ms |
31616 KB |
Output is correct |
6 |
Correct |
23 ms |
31616 KB |
Output is correct |
7 |
Correct |
22 ms |
31616 KB |
Output is correct |
8 |
Correct |
21 ms |
31616 KB |
Output is correct |
9 |
Correct |
22 ms |
31616 KB |
Output is correct |
10 |
Correct |
22 ms |
31616 KB |
Output is correct |
11 |
Correct |
23 ms |
31616 KB |
Output is correct |
12 |
Correct |
21 ms |
31744 KB |
Output is correct |
13 |
Correct |
29 ms |
31744 KB |
Output is correct |
14 |
Correct |
23 ms |
31744 KB |
Output is correct |
15 |
Correct |
30 ms |
31992 KB |
Output is correct |
16 |
Correct |
23 ms |
32000 KB |
Output is correct |
17 |
Correct |
22 ms |
32000 KB |
Output is correct |
18 |
Correct |
24 ms |
32000 KB |
Output is correct |
19 |
Correct |
25 ms |
32000 KB |
Output is correct |
20 |
Correct |
22 ms |
32032 KB |
Output is correct |
21 |
Correct |
24 ms |
32000 KB |
Output is correct |
22 |
Correct |
26 ms |
32000 KB |
Output is correct |
23 |
Correct |
26 ms |
31992 KB |
Output is correct |
24 |
Correct |
24 ms |
32000 KB |
Output is correct |
25 |
Correct |
24 ms |
32000 KB |
Output is correct |
26 |
Correct |
25 ms |
32000 KB |
Output is correct |
27 |
Correct |
25 ms |
31988 KB |
Output is correct |
28 |
Correct |
25 ms |
32000 KB |
Output is correct |
29 |
Correct |
25 ms |
32000 KB |
Output is correct |
30 |
Correct |
27 ms |
32000 KB |
Output is correct |
31 |
Correct |
27 ms |
32000 KB |
Output is correct |
32 |
Correct |
24 ms |
32000 KB |
Output is correct |
33 |
Correct |
23 ms |
32000 KB |
Output is correct |
34 |
Correct |
23 ms |
32000 KB |
Output is correct |
35 |
Correct |
26 ms |
32000 KB |
Output is correct |
36 |
Correct |
23 ms |
32000 KB |
Output is correct |
37 |
Correct |
24 ms |
32000 KB |
Output is correct |
38 |
Correct |
25 ms |
32060 KB |
Output is correct |
39 |
Correct |
24 ms |
31992 KB |
Output is correct |
40 |
Correct |
24 ms |
32000 KB |
Output is correct |
41 |
Correct |
25 ms |
32000 KB |
Output is correct |
42 |
Correct |
23 ms |
32000 KB |
Output is correct |
43 |
Correct |
25 ms |
32000 KB |
Output is correct |
44 |
Correct |
26 ms |
32000 KB |
Output is correct |
45 |
Correct |
27 ms |
31992 KB |
Output is correct |
46 |
Correct |
24 ms |
32000 KB |
Output is correct |
47 |
Correct |
25 ms |
31992 KB |
Output is correct |
48 |
Correct |
23 ms |
31992 KB |
Output is correct |
49 |
Correct |
24 ms |
32000 KB |
Output is correct |
50 |
Correct |
24 ms |
31992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
31616 KB |
Output is correct |
2 |
Correct |
22 ms |
31744 KB |
Output is correct |
3 |
Correct |
23 ms |
31616 KB |
Output is correct |
4 |
Correct |
23 ms |
31744 KB |
Output is correct |
5 |
Correct |
23 ms |
31616 KB |
Output is correct |
6 |
Correct |
23 ms |
31616 KB |
Output is correct |
7 |
Correct |
22 ms |
31616 KB |
Output is correct |
8 |
Correct |
21 ms |
31616 KB |
Output is correct |
9 |
Correct |
22 ms |
31616 KB |
Output is correct |
10 |
Correct |
22 ms |
31616 KB |
Output is correct |
11 |
Correct |
23 ms |
31616 KB |
Output is correct |
12 |
Correct |
21 ms |
31744 KB |
Output is correct |
13 |
Correct |
29 ms |
31744 KB |
Output is correct |
14 |
Correct |
23 ms |
31744 KB |
Output is correct |
15 |
Correct |
30 ms |
31992 KB |
Output is correct |
16 |
Correct |
23 ms |
32000 KB |
Output is correct |
17 |
Correct |
22 ms |
32000 KB |
Output is correct |
18 |
Correct |
24 ms |
32000 KB |
Output is correct |
19 |
Correct |
25 ms |
32000 KB |
Output is correct |
20 |
Correct |
22 ms |
32032 KB |
Output is correct |
21 |
Correct |
24 ms |
32000 KB |
Output is correct |
22 |
Correct |
26 ms |
32000 KB |
Output is correct |
23 |
Correct |
26 ms |
31992 KB |
Output is correct |
24 |
Correct |
24 ms |
32000 KB |
Output is correct |
25 |
Correct |
24 ms |
32000 KB |
Output is correct |
26 |
Correct |
25 ms |
32000 KB |
Output is correct |
27 |
Correct |
25 ms |
31988 KB |
Output is correct |
28 |
Correct |
25 ms |
32000 KB |
Output is correct |
29 |
Correct |
25 ms |
32000 KB |
Output is correct |
30 |
Execution timed out |
1100 ms |
233524 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |