You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
771 B
C++

#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
#define CR(t, x) const t &x
using namespace std;
typedef long long ll;
const int N = 1e5 + 50;
ll a[N];
int main()
{
for (int n, q; ~scanf("%d%d", &n, &q);)
{
int blksz = sqrt(n);
for (int i = 1; i <= n; i++) scanf("%lld", a + i);
for (int i = 0, l, r; i < q; i++)
{
scanf("%d%d", &l, &r);
vector<ll> v(a + l, a + r + 1);
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
ll ans = -1;
if (v.size() >= 3 && v[0] <= v[1] + v[2]) ans = v[0] + v[1] + v[2];
printf("%lld\n", ans);
}
}
return 0;
}