#include<bits/stdc++.h>
using namespace std;
#define task "test"
#define REP(i,a,b) for(int i = (a); i<=(b); ++i)
#define REPD(i,a,b) for(int i = (a); i>=(b); --i)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) (int)(x).size()
#define BIT(x,i) ((x>>i)&1LL)
#define MASK(i) (1LL<<i)
#define fi first
#define se second
#define vi vector<int>
#define pii pair<int,int>
//-----------------------------------------------------------------------//
const int oo = 1e9,LOG = 19,MAXN = 5e5+7,N = 1e2+3;
const int MOD = 1e9+7,MOD1 = 1e9+207,MOD2 = 1e9+407,MOD3 = 998244353;
//-----------------------------------------------------------------------//
template<typename T> bool mini(T &a, T b){if(a>b){a=b;return true;} return false;}
template<typename T> bool maxi(T &a, T b){if(a<b){a=b;return true;} return false;}
int a[MAXN];
int n,q;
void sub3(){
while(q--){
int res = 0,l,r; cin >> l >> r;
stack<int> st; vi dp(n*2+8,0);
REP(i,l,r){
while(SZ(st)!=0 && a[st.top()]<a[i]){
//maxi(dp[2*i-st.top()],a[i]+a[st.top()]);
st.pop();
}
if(SZ(st)!=0) maxi(dp[2*i-st.top()],a[i]+a[st.top()]);
st.push(i);
}
REP(i,l,r){
maxi(dp[i],dp[i-1]);
maxi(res,dp[i]+a[i]);
}
cout << res << "\n";
}
}
void sub4(){
}
void solve(){
cin >> n;
REP(i,1,n){
cin >> a[i];
}
cin >> q;
sub3(); return;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if(fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
solve();
return 0;
}
Compilation message
remittance.cpp: In function 'int main()':
remittance.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
remittance.cpp:63:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |