|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +class IOController { |
| 6 | + BufferedReader br; |
| 7 | + BufferedWriter bw; |
| 8 | + StringTokenizer st; |
| 9 | + |
| 10 | + public IOController() { |
| 11 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 13 | + st = new StringTokenizer(""); |
| 14 | + } |
| 15 | + |
| 16 | + String nextLine() throws Exception { |
| 17 | + String line = br.readLine(); |
| 18 | + st = new StringTokenizer(line); |
| 19 | + return line; |
| 20 | + } |
| 21 | + |
| 22 | + String nextToken() throws Exception { |
| 23 | + while (!st.hasMoreTokens()) nextLine(); |
| 24 | + return st.nextToken(); |
| 25 | + } |
| 26 | + |
| 27 | + int nextInt() throws Exception { |
| 28 | + return Integer.parseInt(nextToken()); |
| 29 | + } |
| 30 | + |
| 31 | + long nextLong() throws Exception { |
| 32 | + return Long.parseLong(nextToken()); |
| 33 | + } |
| 34 | + |
| 35 | + double nextDouble() throws Exception { |
| 36 | + return Double.parseDouble(nextToken()); |
| 37 | + } |
| 38 | + |
| 39 | + void close() throws Exception { |
| 40 | + bw.flush(); |
| 41 | + bw.close(); |
| 42 | + } |
| 43 | + |
| 44 | + void write(String content) throws Exception { |
| 45 | + bw.write(content); |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +public class Main { |
| 51 | + |
| 52 | + static IOController io; |
| 53 | + |
| 54 | + // |
| 55 | + |
| 56 | + static final long INF = -(long)1e18 - 7; |
| 57 | + |
| 58 | + static int N, M; |
| 59 | + static long[] a, b; |
| 60 | + |
| 61 | + public static void main(String[] args) throws Exception { |
| 62 | + |
| 63 | + io = new IOController(); |
| 64 | + |
| 65 | + init(); |
| 66 | + solve(); |
| 67 | + |
| 68 | + io.close(); |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + static void init() throws Exception { |
| 73 | + |
| 74 | + N = io.nextInt(); |
| 75 | + M = io.nextInt(); |
| 76 | + a = new long[N+1]; |
| 77 | + for(int i=1;i<=N;i++) a[i] = a[i-1] + io.nextInt(); |
| 78 | + b = new long[N+1]; |
| 79 | + for(int i=1;i<=N;i++) b[i] = io.nextInt(); |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + static void solve() throws Exception { |
| 84 | + |
| 85 | + long[] success = new long[N+1]; |
| 86 | + long[] fail = new long[N+1]; |
| 87 | + long[] bonus = new long[N+1]; |
| 88 | + Arrays.fill(bonus, INF); |
| 89 | + |
| 90 | + for(int i=1;i<=N;i++) { |
| 91 | + success[i] = Math.max(bonus[i-1], Math.max(success[i-1], fail[i-1])) + a[i]-a[i-1]; |
| 92 | + fail[i] = Math.max(bonus[i-1], Math.max(success[i-1], fail[i-1])) - (a[i]-a[i-1]); |
| 93 | + if(i >= M) bonus[i] = Math.max(fail[i-M], bonus[i-M]) + a[i]-a[i-M] + b[i]; |
| 94 | + } |
| 95 | + |
| 96 | + io.write(Math.max(bonus[N], Math.max(success[N], fail[N])) + "\n"); |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | +} |
| 101 | +``` |
0 commit comments