結果
問題 | No.324 落ちてた閉路グラフ |
ユーザー | Grenache |
提出日時 | 2015-12-18 16:50:40 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 216 ms / 5,000 ms |
コード長 | 3,287 bytes |
コンパイル時間 | 4,225 ms |
コンパイル使用メモリ | 78,416 KB |
実行使用メモリ | 53,276 KB |
最終ジャッジ日時 | 2024-11-15 19:34:51 |
合計ジャッジ時間 | 8,692 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 56 ms
50,452 KB |
testcase_01 | AC | 56 ms
50,288 KB |
testcase_02 | AC | 55 ms
50,372 KB |
testcase_03 | AC | 56 ms
50,352 KB |
testcase_04 | AC | 149 ms
53,100 KB |
testcase_05 | AC | 216 ms
53,012 KB |
testcase_06 | AC | 137 ms
52,448 KB |
testcase_07 | AC | 206 ms
53,212 KB |
testcase_08 | AC | 139 ms
52,452 KB |
testcase_09 | AC | 126 ms
52,696 KB |
testcase_10 | AC | 138 ms
52,704 KB |
testcase_11 | AC | 121 ms
52,564 KB |
testcase_12 | AC | 67 ms
50,980 KB |
testcase_13 | AC | 63 ms
50,408 KB |
testcase_14 | AC | 110 ms
52,596 KB |
testcase_15 | AC | 126 ms
53,276 KB |
testcase_16 | AC | 57 ms
49,952 KB |
testcase_17 | AC | 57 ms
50,064 KB |
testcase_18 | AC | 57 ms
50,036 KB |
testcase_19 | AC | 57 ms
50,272 KB |
testcase_20 | AC | 57 ms
50,648 KB |
testcase_21 | AC | 56 ms
50,436 KB |
testcase_22 | AC | 56 ms
50,460 KB |
testcase_23 | AC | 58 ms
50,304 KB |
testcase_24 | AC | 57 ms
50,252 KB |
testcase_25 | AC | 57 ms
50,292 KB |
testcase_26 | AC | 58 ms
50,348 KB |
testcase_27 | AC | 59 ms
50,404 KB |
testcase_28 | AC | 57 ms
50,492 KB |
testcase_29 | AC | 56 ms
50,200 KB |
testcase_30 | AC | 55 ms
50,204 KB |
testcase_31 | AC | 55 ms
50,088 KB |
testcase_32 | AC | 212 ms
52,944 KB |
testcase_33 | AC | 198 ms
52,952 KB |
testcase_34 | AC | 143 ms
52,836 KB |
testcase_35 | AC | 61 ms
49,932 KB |
testcase_36 | AC | 117 ms
52,820 KB |
testcase_37 | AC | 57 ms
50,320 KB |
ソースコード
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Iterator; public class Main_yukicoder324_1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Printer pr = new Printer(System.out); final int INF = Integer.MAX_VALUE / 2; int n = sc.nextInt(); int m = sc.nextInt(); int[] w = new int[n]; for (int i = 0; i < n; i++) { w[i] = sc.nextInt(); } if (m == 0) { pr.println(0); pr.close(); sc.close(); return; } int[][][] dp = new int[2][m + 1][2]; int[][][] dp2 = new int[2][m + 1][2]; for (int i = 0; i < 2; i++) { for (int j = 0; j <= m; j++) { Arrays.fill(dp[i][j], -INF); Arrays.fill(dp2[i][j], -INF); } } for (int i = 0; i < 2; i++) { dp[i][0][0] = 0; } dp2[0][1][1] = 0; for (int i = 1; i < n; i++) { int tmpi = i % 2; int tmpim = (i + 1) % 2; for (int j = 1; j <= m; j++) { if (i != n - 1) { dp[tmpi][j][0] = Math.max(dp[tmpim][j][0], dp[tmpim][j][1]); dp[tmpi][j][1] = Math.max(dp[tmpim][j - 1][0], dp[tmpim][j - 1][1] + w[i - 1]); dp2[tmpi][j][0] = Math.max(dp2[tmpim][j][0], dp2[tmpim][j][1]); dp2[tmpi][j][1] = Math.max(dp2[tmpim][j - 1][0], dp2[tmpim][j - 1][1] + w[i - 1]); } else { dp[tmpi][j][0] = Math.max(dp[tmpim][j][0], dp[tmpim][j][1]); dp[tmpi][j][1] = Math.max(dp[tmpim][j - 1][0], dp[tmpim][j - 1][1] + w[i - 1]); dp2[tmpi][j][0] = Math.max(dp2[tmpim][j][0], dp2[tmpim][j][1]); dp2[tmpi][j][1] = Math.max(dp2[tmpim][j - 1][0] + w[i], dp2[tmpim][j - 1][1] + w[i - 1] + w[i]); } } } int max = -INF; for (int i = 0; i < 2; i++) { max = Math.max(max, dp[(n - 1) % 2][m][i]); max = Math.max(max, dp2[(n - 1) % 2][m][i]); } pr.println(max); pr.close(); sc.close(); } @SuppressWarnings("unused") private static class Scanner { BufferedReader br; Iterator<String> it; Scanner (InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } String next() throws RuntimeException { try { if (it == null || !it.hasNext()) { it = Arrays.asList(br.readLine().split(" ")).iterator(); } return it.next(); } catch (IOException e) { throw new IllegalStateException(); } } int nextInt() throws RuntimeException { return Integer.parseInt(next()); } long nextLong() throws RuntimeException { return Long.parseLong(next()); } float nextFloat() throws RuntimeException { return Float.parseFloat(next()); } double nextDouble() throws RuntimeException { return Double.parseDouble(next()); } void close() { try { br.close(); } catch (IOException e) { // throw new IllegalStateException(); } } } private static class Printer extends PrintWriter { Printer(PrintStream out) { super(out); } } }