#include <bits/stdc++.h>
#define ll long long
#define pii pair <int,int>
#define pll pair <ll,ll>
#define fi first
#define se second
#define ull unsigned long long
#define umap unordered_map <ll,int>
#define uset unordered_set <ll>
#define endl "\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define setpr(x) cout<<setprecision(x)<<fixed
#define MOD 1000000007
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a*b / gcd(a,b);}
inline ll powmod(ll a,ll n) {ll res=1; while (n) { if (n % 2!=0) res=((res % MOD)*(a % MOD)) % MOD; a=((a % MOD)*(a % MOD)) % MOD; n/=2; } return res;}
using namespace std;
const int maxn=1e6+5;
const int oo=1e9;
const int dx[]={-1,0,1,0,1,-1,1,-1};
const int dy[]={0,1,0,-1,-1,1,1,-1};
int n,m;
ll a[maxn],tree[4*maxn];
void build(ll id,ll l,ll r)
{
if (l==r)
{
tree[id]=a[r];
return;
}
ll mid=(l+r) / 2;
build(2*id,l,mid);
build(2*id+1,mid+1,r);
tree[id]=max(tree[2*id],tree[2*id+1]);
}
ll get(ll id,ll l,ll r,ll u,ll v)
{
if (v<l || u>r) return -1e18;
if (u<=l && r<=v) return tree[id];
ll mid=(l+r) / 2;
return max(get(2*id,l,mid,u,v),get(2*id+1,mid+1,r,u,v));
}
int main()
{
fast;
cin>>n;
for(int i=1; i<=n; i++) cin>>a[i];
build(1,1,n);
cin>>m;
while (m--)
{
ll l,r;
cin>>l>>r;
ll ans=0;
for(int x=l; x<=r; x++)
for(int y=x+1; y<=r; y++)
if (get(1,1,n,(y-x)+y,r)!=-1e18) ans=max(ans,a[x]+a[y]+get(1,1,n,(y-x)+y,r));
cout<<ans<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
476 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
476 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
11 |
Execution timed out |
4069 ms |
604 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4057 ms |
7772 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
476 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
11 |
Execution timed out |
4069 ms |
604 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |