#include<bits/stdc++.h>
using namespace std;
const long long maxn=200000+10;
struct node{
long long x,y,w,l,r;
}allq[maxn];
long long all[maxn],q,n;
vector<long long>sn,si;
pair<long long,long long>val[maxn],fakeval[maxn];
bool cmpn(long long a,long long b){
return allq[a].y<allq[b].y;
}
bool cmpi(long long a,long long b){
if(all[a]==all[b]){
return a<b;
}
return all[a]<all[b];
}
void callr()
{
for(long long i=1;i<=q;i++){
long long l=allq[i].x,r=allq[i].x;
while(l!=0&&all[l]<allq[i].y){
l--;
}
while(r!=n+1&&all[r]<allq[i].y){
r++;
}
allq[i].l=l;
allq[i].r=r;
}
}
long long mainres=0;
void updn(long long ind){
long long res=0,resa=0;
long long maxa=-1;
for(long long i=allq[ind].x;i<allq[ind].r;i++){
if(all[i]>=maxa){
res+=val[i].second;
maxa=all[i];
}
}
maxa=-1;
for(long long i=allq[ind].x;i>allq[ind].l;i--){
if(all[i]>=maxa){
res+=val[i].first;
maxa=all[i];
}
}
res+=resa+allq[ind].w;
fakeval[allq[ind].r].first=max(fakeval[allq[ind].r].first,res);
fakeval[allq[ind].l].second=max(fakeval[allq[ind].l].second,res);
// cout<<ind<<" "<<allq[ind].l<<" "<<allq[ind].r<<" "<<res<<"\n";
mainres=max(mainres,res);
}
void upd(long long ind){
val[ind]=fakeval[ind];
// cout<<ind<<" "<<val[ind].first<<" "<<val[ind].second<<"\n";
long long res=0,maxa=-1;
vector<long long>v;
long long i=ind-1;
while(i!=0&&all[i]<all[ind]){
if(all[i]>maxa){
v.clear();
maxa=all[i];
v.push_back(i);
}
else if(all[i]==maxa){
v.push_back(i);
}
i--;
}
if((long long)v.size()>0){
for(auto x:v){
res+=val[x].second;
}
res+=val[v.back()].first;
val[ind].first=max(val[ind].first,res);
}
res=0,maxa=-1;
v.clear();
i=ind+1;
while(i!=n+1&&all[i]<all[ind]){
if(all[i]>maxa){
v.clear();
maxa=all[i];
v.push_back(i);
}
else if(all[i]==maxa){
v.push_back(i);
}
i++;
}
if((long long)v.size()>0){
for(auto x:v){
res+=val[x].first;
}
res+=val[v.back()].second;
val[ind].second=max(val[ind].second,res);
}
mainres=max(mainres,max(val[ind].first,val[ind].second));
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(long long i=1;i<=n;i++){
cin>>all[i];
si.push_back(i);
}
cin>>q;
long long suma=0;
for(long long i=1;i<=q;i++){
sn.push_back(i);
cin>>allq[i].x>>allq[i].y>>allq[i].w;
suma+=allq[i].w;
}
callr();
sort(sn.begin(),sn.end(),cmpn);
sort(si.begin(),si.end(),cmpi);
long long i=0,j=0;
while(i<(long long)sn.size()&&j<(long long)si.size()){
if(allq[sn[i]].y<=all[si[j]]){
updn(sn[i]);
i++;
}
else{
upd(si[j]);
j++;
}
}
while(i<(long long)sn.size()){
updn(sn[i]);
i++;
}
while(j<(long long)si.size()){
upd(si[j]);
j++;
}
long long now=(long long)si.size()-1;
long long res=val[si[now]].second;
while(now!=0&&all[si[now-1]]==all[si[now]]){
now--;
res+=val[si[now]].second;
}
res+=val[si[now]].first;
mainres=max(mainres,res);
mainres=suma-mainres;
cout<<mainres<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6644 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6608 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6488 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6644 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6608 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6488 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
6748 KB |
Output is correct |
28 |
Correct |
2 ms |
6620 KB |
Output is correct |
29 |
Correct |
2 ms |
6628 KB |
Output is correct |
30 |
Correct |
2 ms |
6748 KB |
Output is correct |
31 |
Correct |
2 ms |
6748 KB |
Output is correct |
32 |
Correct |
6 ms |
6720 KB |
Output is correct |
33 |
Correct |
7 ms |
6748 KB |
Output is correct |
34 |
Correct |
6 ms |
6492 KB |
Output is correct |
35 |
Correct |
6 ms |
7000 KB |
Output is correct |
36 |
Correct |
5 ms |
6492 KB |
Output is correct |
37 |
Correct |
5 ms |
6492 KB |
Output is correct |
38 |
Correct |
5 ms |
6492 KB |
Output is correct |
39 |
Correct |
4 ms |
6492 KB |
Output is correct |
40 |
Correct |
6 ms |
6736 KB |
Output is correct |
41 |
Correct |
4 ms |
6744 KB |
Output is correct |
42 |
Correct |
3 ms |
6740 KB |
Output is correct |
43 |
Correct |
6 ms |
6748 KB |
Output is correct |
44 |
Correct |
4 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6644 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6608 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6488 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
6748 KB |
Output is correct |
28 |
Correct |
2 ms |
6620 KB |
Output is correct |
29 |
Correct |
2 ms |
6628 KB |
Output is correct |
30 |
Correct |
2 ms |
6748 KB |
Output is correct |
31 |
Correct |
2 ms |
6748 KB |
Output is correct |
32 |
Correct |
6 ms |
6720 KB |
Output is correct |
33 |
Correct |
7 ms |
6748 KB |
Output is correct |
34 |
Correct |
6 ms |
6492 KB |
Output is correct |
35 |
Correct |
6 ms |
7000 KB |
Output is correct |
36 |
Correct |
5 ms |
6492 KB |
Output is correct |
37 |
Correct |
5 ms |
6492 KB |
Output is correct |
38 |
Correct |
5 ms |
6492 KB |
Output is correct |
39 |
Correct |
4 ms |
6492 KB |
Output is correct |
40 |
Correct |
6 ms |
6736 KB |
Output is correct |
41 |
Correct |
4 ms |
6744 KB |
Output is correct |
42 |
Correct |
3 ms |
6740 KB |
Output is correct |
43 |
Correct |
6 ms |
6748 KB |
Output is correct |
44 |
Correct |
4 ms |
6492 KB |
Output is correct |
45 |
Correct |
172 ms |
24744 KB |
Output is correct |
46 |
Correct |
170 ms |
24316 KB |
Output is correct |
47 |
Correct |
175 ms |
24904 KB |
Output is correct |
48 |
Correct |
173 ms |
24120 KB |
Output is correct |
49 |
Correct |
169 ms |
24432 KB |
Output is correct |
50 |
Correct |
168 ms |
24180 KB |
Output is correct |
51 |
Correct |
172 ms |
24460 KB |
Output is correct |
52 |
Correct |
175 ms |
24956 KB |
Output is correct |
53 |
Correct |
174 ms |
24396 KB |
Output is correct |
54 |
Execution timed out |
1547 ms |
20460 KB |
Time limit exceeded |
55 |
Halted |
0 ms |
0 KB |
- |