//雪花飄飄北風嘯嘯
//天地一片蒼茫
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl;
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
ll MAX(ll a){return a;}
ll MIN(ll a){return a;}
template<typename... Args>
ll MAX(ll a,Args... args){return max(a,MAX(args...));}
template<typename... Args>
ll MIN(ll a,Args... args){return min(a,MIN(args...));}
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
struct sparse{
long long ST[200005][20];
sparse(){}
long long func(long long i,long long j){return max(i,j);}
///change this
void build(int n,long long arr[]){ ///build sparse table
for (int x=0;x<n;x++){
ST[x][0]=arr[x];
}
int j=1,logj=1;
while (j<=(n>>1)){
for (int x=0;x<=(n-(j<<1));x++){
ST[x][logj]=func(ST[x][logj-1],ST[x+j][logj-1]);
}
j<<=1;
logj++;
}
}
long long query(int i, int j){ ///in range [i,j]
if (i>j) return -1e18;
j=j-i+1;
int row=31-__builtin_clz(j);
j-=1<<row;
return func(ST[i][row],ST[i+j][row]);
}
} ST;
int n,q;
ll arr[200005];
vector<int> stk;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
rep(x,0,n) cin>>arr[x];
ST.build(n,arr);
ll ans=0;
rep(x,0,n){
while (!stk.empty() && arr[stk.back()]<=arr[x]){
ans=max(ans,arr[stk.back()]+arr[x]+ST.query(x+(x-stk.back()),n-1));
stk.pop_back();
}
if (!stk.empty()) ans=max(ans,arr[stk.back()]+arr[x]+ST.query(x+(x-stk.back()),n-1));
stk.push_back(x);
}
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
33272 KB |
Output is correct |
2 |
Correct |
81 ms |
33272 KB |
Output is correct |
3 |
Correct |
76 ms |
34420 KB |
Output is correct |
4 |
Correct |
79 ms |
33144 KB |
Output is correct |
5 |
Correct |
81 ms |
33144 KB |
Output is correct |
6 |
Correct |
77 ms |
33280 KB |
Output is correct |
7 |
Correct |
73 ms |
33152 KB |
Output is correct |
8 |
Correct |
77 ms |
33152 KB |
Output is correct |
9 |
Correct |
79 ms |
33272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |