This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma optimization_level 3
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordset;
*/
#define fr first
#define sc second
#define vec vector
#define pb push_back
#define pii pair<int, int>
#define forn(x,y) for(int x = 1 ; x <= y ; ++x)
#define all(x) (x).begin(),(x).end()
#define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
#define x real
#define y imag
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef complex<int> point;
const int nmax = 5005, mmax = 80;
const ll linf = 1e18;
const ll mod = 998244353;
const int inf = INT_MAX;
int n, q;
ll a[nmax];
int l , r;
ll maxrange[nmax][nmax], dp[nmax][nmax];
int main(){
	fast;
	cin >> n;
	forn(i,n)cin >> a[i];
	for(int i = 1; i <= n; i ++){
		for(int j = i ; j <= n ; j++){
			maxrange[i][j] = max(maxrange[i][j-1],a[j]);
		}
	}
	for(int lenght = 2 ; lenght <= n ; lenght ++){
		for(int i = 1; i <= n ; i++){
			l = i , r = i + lenght;
			if(r > n)continue;
			int mid = (l + r)/2;
			dp[l][r] = max(dp[l][r-1] , dp[l+1][r]);
			dp[l][r] = max(dp[l][r] , a[l] + a[r] + maxrange[l+1][mid]);
		}
	}
	cin >> q;
	forn(ss,q){
		cin >> l >> r;
		cout << dp[l][r] << '\n';	
	}
	
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |