// Muallif: Azimjon Mehmonali o'g'li
#include <bits/stdc++.h>
using namespace std;
#define int long long
const long double PI = 3.1415926535897;
const int mod = 1000000007LL;
const int INF = 1e18;
const int N = 500500;
int a[N], b[N], t[4 * N];
// void build(1)
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
b[i] = a[i];
}
// build();
int q;
cin >> q;
function<int(int, int, int)> f = [&](int l, int r, int first) {
// cerr << l << " " << r << endl;
// if (r - l + 1 <= 2) return 0ll;
vector<pair<int, int>> v;
for (int i = l; i <= r; i++) {
v.emplace_back(a[i], i);
}
sort(v.rbegin(), v.rend());
int tl = v[0].second, tr = v[1].second;
if (tl > tr) {
swap(tl, tr);
}
int re = 0;
for (int i = 1; i <= n; i++) {
vector<int> d{tl, tr, i};
sort(d.begin(), d.end());
if (i == tl || i == tr || l > i || i > r
|| d[2] - d[1] < d[1] - d[0])
continue;
re = max(re, a[i]);
}
int ans = v[0].first + v[1].first + re;
if (!first) return ans;
a[v[0].second] = 0;
ans = max(ans, f(l, r, first - 1));
a[v[1].second] = 0;
ans = max(ans, f(l, r, first - 1));
a[v[0].second] = b[v[0].second];
a[v[1].second] = b[v[1].second];
return ans;
};
auto ff = [&](int l, int r) {
int ans = 0;
for (int i = l; i <= r; i++) {
for (int j = i + 1; j <= r; j++) {
for (int k = j + j - i; k <= r; k++) {
ans = max(ans, a[i] + a[j] + a[k]);
}
}
}
return ans;
};
while (q--) {
int l, r;
cin >> l >> r;
cout << ff(l, r) << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
204 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
204 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
3 ms |
204 KB |
Output is correct |
9 |
Correct |
3 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
204 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
204 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
3 ms |
204 KB |
Output is correct |
9 |
Correct |
3 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Execution timed out |
4067 ms |
332 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4066 ms |
3348 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
204 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
204 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
3 ms |
204 KB |
Output is correct |
9 |
Correct |
3 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Execution timed out |
4067 ms |
332 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |