#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
bool ok(int i, int j, int k) {
vector <int> r = {i, j, k};
sort(r.begin(), r.end());
i = r[0], j = r[1], k = r[2];
if (i == j || j == k) return false;
return k - j >= j - i;
}
int solve(vector <int> a) {
vector <pair <int, int>> b;
int n = a.size();
for (int i = 0; i < n; ++i) b.push_back({a[i], i});
sort(b.rbegin(), b.rend());
int ans = 0;
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
for (int k = 0; k < n; ++k) {
if (ok(b[i].second, b[j].second, k)) {
ans = max(ans, b[i].first + b[j].first + a[k]);
}
}
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n;
cin >> n;
vector <int> A(n);
for (int i = 0; i < n; ++i) {
cin >> A[i];
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
--l, --r;
vector <int> a;
for (int i = l; i <= r; ++i) a.push_back(A[i]);
cout << solve(a) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
7 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
7 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
296 ms |
4972 KB |
Output is correct |
2 |
Correct |
282 ms |
4972 KB |
Output is correct |
3 |
Correct |
278 ms |
4976 KB |
Output is correct |
4 |
Incorrect |
300 ms |
4976 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
7 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |