#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAX_VAL=500*1000+5,INFINI=1000*1000*1000;
int nbVal,nbReq,debReq,finReq,rep;
int val[MAX_VAL];
int maxi[MAX_VAL];
vector<int> enCours;
vector<pair<int,int>> paireInter;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>nbVal;
for (int i=1;i<=nbVal;i++) {
cin>>val[i];
}
enCours.push_back(1);
for (int i=2;i<=nbVal;i++) {
while (!enCours.empty() and val[enCours.back()]<=val[i]) {
if (nbVal-i>=i-enCours.back()) {
paireInter.push_back({enCours.back(),i});
}
enCours.pop_back();
}
if (!enCours.empty()) {
if (nbVal-i>=i-enCours.back()) {
paireInter.push_back({enCours.back(),i});
}
enCours.pop_back();
}
enCours.push_back(i);
}
cin>>nbReq;
for (int ireq=1;ireq<=nbReq;ireq++) {
cin>>debReq>>finReq;
for (int i=finReq+1;i<=nbVal+1;i++) {
maxi[i]=-INFINI;
}
for (int i=finReq;i>=debReq;i--) {
maxi[i]=max(maxi[i+1],val[i]);
}
rep=0;
for (auto i:paireInter) {
if (i.first>=debReq) {
rep=max(rep,val[i.first]+val[i.second]+maxi[2*i.second-i.first]);
}
}
cout<<rep<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
7212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |