|
4 | 4 | import java.io.IOException;
|
5 | 5 | import java.io.InputStreamReader;
|
6 | 6 |
|
| 7 | +/** |
| 8 | + * Codeforces Single Use Stones: https://youtu.be/5s0MRxojQQo |
| 9 | + * Problem Statement: http://codeforces.com/contest/965/problem/D |
| 10 | + * Two pointer sliding window solution |
| 11 | + */ |
7 | 12 | public class SingleUseStones {
|
8 | 13 | public static void main(final String args[]) throws IOException {
|
9 | 14 | final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
|
10 | 15 | String line[] = bufferedReader.readLine().split(" ");
|
11 |
| - final int w = Integer.parseInt(line[0]), l = Integer.parseInt(line[1]); |
| 16 | + final int width = Integer.parseInt(line[0]), L = Integer.parseInt(line[1]); |
12 | 17 | line = bufferedReader.readLine().split(" ");
|
13 |
| - final int a[] = new int[line.length]; |
14 |
| - for (int i = 0; i < a.length; i++) { |
15 |
| - a[i] = Integer.parseInt(line[i]); |
| 18 | + final int river[] = new int[line.length]; |
| 19 | + for (int i = 0; i < river.length; i++) { |
| 20 | + river[i] = Integer.parseInt(line[i]); |
16 | 21 | }
|
| 22 | + // Solution starts here |
17 | 23 | int sum = 0;
|
18 |
| - for (int i = 0; i < l; i++) { |
19 |
| - sum += a[i]; |
| 24 | + for (int i = 0; i < L; i++) { |
| 25 | + sum += river[i]; |
20 | 26 | }
|
21 | 27 | int minValue = sum;
|
22 |
| - for (int i = l; i < w - 1; i++) { |
23 |
| - sum = sum - a[i - l] + a[i]; |
| 28 | + for (int i = L; i < width - 1; i++) { |
| 29 | + sum = sum - river[i - L] + river[i]; |
24 | 30 | minValue = Math.min(minValue, sum);
|
25 | 31 | }
|
26 | 32 | System.out.println(minValue);
|
|
0 commit comments