Deloitte | COGNIZANT | IBM | CAPGEMINI
import java.util.*;
import java.io.*;
class SolutionClass {
private static final class FastScanner {
private final InputStream in;
private final byte[] buffer = new byte[1 << 16];
private int ptr = 0, len = 0;
FastScanner(InputStream is) { in = is; }
private int read() throws IOException {
if (ptr >= len) {
len = in.read(buffer);
ptr = 0;
if (len <= 0) return -1;
}
return buffer[ptr++];
}
int nextInt() throws IOException {
int c, sgn = 1, x = 0;
do c = read(); while (c <= ' ');
if (c == '-') { sgn = -1; c = read(); }
while (c > ' ') { x = x * 10 + (c - '0'); c = read(); }
return x * sgn;
}
}
public static void main(String[] args) throws Exception {
FastScanner fs = new FastScanner(System.in);
int N = fs.nextInt();
int[] a = new int[N + 1];
for (int i = 1; i <= N; i++) a[i] = fs.nextInt();
int L = fs.nextInt();
int R = fs.nextInt();
long[] pref = new long[N + 1];
for (int i = 1; i <= N; i++) pref[i] = pref[i - 1] + a[i];
TreeMap<Long, Integer> multiset = new TreeMap<>();
long best = Long.MAX_VALUE;
for (int j = 1; j <= N; j++) {
int addIdx = j - L;
if (addIdx >= 0) {
long val = pref[addIdx];
multiset.put(val, multiset.getOrDefault(val, 0) + 1);
}
int remIdx = j - R - 1;
if (remIdx >= 0) {
long val = pref[remIdx];
int cnt = multiset.getOrDefault(val, 0);
if (cnt <= 1) multiset.remove(val);
else multiset.put(val, cnt - 1);
}
if (!multiset.isEmpty()) {
Long candidateStart = multiset.lowerKey(pref[j]);
if (candidateStart != null) {
long sum = pref[j] - candidateStart;
if (sum < best) best = sum;
}
}
}
System.out.println(best == Long.MAX_VALUE ? -1 : best);
}
}
//sequence of consecutive stones✅
1.3K ·