// Om Namah Shivaya
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a, b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a, b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
/*
refs:
https://codeforces.com/blog/entry/68269?#comment-527139
edi
choose l <= a < b < c <= r s.t:
b-a <= c-b
arr[a]+arr[b]+arr[c] is max
let's say we fix (a,b)
when can we consider (a,b) as an option
if there is a guy in between with a higher val, then we dont have to consider pair (a,b)
if there exists k s.t a < k < b with arr[k] >= arr[a] or arr[k] >= arr[b], then we can do this:
arr[k] >= arr[a]: set a = k
arr[k] >= arr[b]: set b = k
by doing this, we reduce the jump distance from a to b
so we have more options for c
in short, consider pair (a,b) only if there is nobody in between with a higher or equal value
how many such pairs are there?
iterate over b from 1 to n and find all valid a
maintain all valid a values when increasing b
once some a becomes bad (some >= guy appears in between), he never becomes good for bigger b
we want to find all a for whom nobody >= them has appeared in [a,b] so far
when moving to a new b, we can make pairs (a,b) for all active a
then we will remove all a values for which arr[a] <= arr[b]
(yet to complete explanation)
*/
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
void solve(int test_case)
{
ll n; cin >> n;
vector<ll> arr(n+5);
rep1(i,n) cin >> arr[i];
vector<ll> good;
vector<pll> pairs;
rep1(b,n){
while(!good.empty() and arr[b] >= arr[good.back()]){
pairs.pb({good.back(), b});
good.pop_back();
}
if(!good.empty()){
pairs.pb({good.back(), b});
}
good.pb(b);
}
vector<ll> suff(n+5);
rev(i,n,1) suff[i] = max(suff[i+1], arr[i]);
ll q; cin >> q;
while(q--){
ll ans = 0;
for(auto [a, b] : pairs){
ll dis = b - a;
ll min_c = b + dis;
if(min_c <= n){
ll val = arr[a] + arr[b] + suff[min_c];
amax(ans, val);
}
}
cout << ans << endl;
}
}
int main()
{
fastio;
int t = 1;
// cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
10208 KB |
Output is correct |
2 |
Correct |
23 ms |
6492 KB |
Output is correct |
3 |
Correct |
25 ms |
10308 KB |
Output is correct |
4 |
Correct |
37 ms |
11928 KB |
Output is correct |
5 |
Correct |
30 ms |
11976 KB |
Output is correct |
6 |
Correct |
26 ms |
11272 KB |
Output is correct |
7 |
Correct |
25 ms |
11136 KB |
Output is correct |
8 |
Correct |
26 ms |
11140 KB |
Output is correct |
9 |
Correct |
27 ms |
11496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |