This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define INF 1000000007
#define MAXN 5300013
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
int N, Q;
int arr[MAXN];
int sparse[20][MAXN];
vpi cand;
vpi pos;
int ans;
int query(int l, int r)
{
if (l > r) return -INF;
int sz = 31 - __builtin_clz(r - l + 1);
return max(sparse[sz][l], sparse[sz][r - (1 << sz) + 1]);
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N;
FOR(i, 0, N) cin >> arr[i];
cin >> Q;
FOR(i, 0, N)
{
sparse[0][i] = arr[i];
}
FOR(i, 0, 20)
{
FOR(j, 0, N)
{
if (j + (1 << i) < N)
{
sparse[i + 1][j] = max(sparse[i][j], sparse[i][j + (1 << i)]);
}
}
}
//everything in between (l..r) must be less than a[l] and a[r]
//find all (l..r) such that everything between them is <= them
FORD(i, N, 0)
{
int x = arr[i];
while(!pos.empty() && pos.back().fi <= arr[i])
{
cand.PB({i, pos.back().se});
pos.pop_back();
}
if (!pos.empty())
{
cand.PB({i, pos.back().se});
}
pos.PB({x, i});
}
for (pii p : cand)
{
int a = p.fi, b = p.se;
// cerr << a << ' ' << b << endl;
ckmax(ans, arr[a] + arr[b] + query(2 * b - a, N - 1));
}
cout << ans << '\n';
return 0;
}
# | 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... |