From a3077a274cbb1eaa2f9817881008d85ae95d2201 Mon Sep 17 00:00:00 2001 From: oncsr Date: Thu, 7 Aug 2025 21:15:15 +0900 Subject: [PATCH] =?UTF-8?q?[20250807]=20BOJ=20/=20P4=20/=20=ED=80=B4?= =?UTF-8?q?=EC=A6=88=EC=87=BC=20/=20=EA=B6=8C=ED=98=81=EC=A4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4 \355\200\264\354\246\210\354\207\274.md" | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 "khj20006/202508/07 BOJ P4 \355\200\264\354\246\210\354\207\274.md" diff --git "a/khj20006/202508/07 BOJ P4 \355\200\264\354\246\210\354\207\274.md" "b/khj20006/202508/07 BOJ P4 \355\200\264\354\246\210\354\207\274.md" new file mode 100644 index 00000000..0c7be9b5 --- /dev/null +++ "b/khj20006/202508/07 BOJ P4 \355\200\264\354\246\210\354\207\274.md" @@ -0,0 +1,101 @@ +```java +import java.util.*; +import java.io.*; + +class IOController { + BufferedReader br; + BufferedWriter bw; + StringTokenizer st; + + public IOController() { + br = new BufferedReader(new InputStreamReader(System.in)); + bw = new BufferedWriter(new OutputStreamWriter(System.out)); + st = new StringTokenizer(""); + } + + String nextLine() throws Exception { + String line = br.readLine(); + st = new StringTokenizer(line); + return line; + } + + String nextToken() throws Exception { + while (!st.hasMoreTokens()) nextLine(); + return st.nextToken(); + } + + int nextInt() throws Exception { + return Integer.parseInt(nextToken()); + } + + long nextLong() throws Exception { + return Long.parseLong(nextToken()); + } + + double nextDouble() throws Exception { + return Double.parseDouble(nextToken()); + } + + void close() throws Exception { + bw.flush(); + bw.close(); + } + + void write(String content) throws Exception { + bw.write(content); + } + +} + +public class Main { + + static IOController io; + + // + + static final long INF = -(long)1e18 - 7; + + static int N, M; + static long[] a, b; + + public static void main(String[] args) throws Exception { + + io = new IOController(); + + init(); + solve(); + + io.close(); + + } + + static void init() throws Exception { + + N = io.nextInt(); + M = io.nextInt(); + a = new long[N+1]; + for(int i=1;i<=N;i++) a[i] = a[i-1] + io.nextInt(); + b = new long[N+1]; + for(int i=1;i<=N;i++) b[i] = io.nextInt(); + + } + + static void solve() throws Exception { + + long[] success = new long[N+1]; + long[] fail = new long[N+1]; + long[] bonus = new long[N+1]; + Arrays.fill(bonus, INF); + + for(int i=1;i<=N;i++) { + success[i] = Math.max(bonus[i-1], Math.max(success[i-1], fail[i-1])) + a[i]-a[i-1]; + fail[i] = Math.max(bonus[i-1], Math.max(success[i-1], fail[i-1])) - (a[i]-a[i-1]); + if(i >= M) bonus[i] = Math.max(fail[i-M], bonus[i-M]) + a[i]-a[i-M] + b[i]; + } + + io.write(Math.max(bonus[N], Math.max(success[N], fail[N])) + "\n"); + + } + +} +```