結果

問題 No.2423 Merge Stones
ユーザー hiromi_ayasehiromi_ayase
提出日時 2023-08-12 16:11:26
言語 Java21
(openjdk 21)
結果
AC  
実行時間 557 ms / 4,000 ms
コード長 3,156 bytes
コンパイル時間 2,477 ms
コンパイル使用メモリ 78,596 KB
実行使用メモリ 55,792 KB
最終ジャッジ日時 2024-04-30 11:02:54
合計ジャッジ時間 30,915 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
50,424 KB
testcase_01 AC 61 ms
50,744 KB
testcase_02 AC 61 ms
50,748 KB
testcase_03 AC 61 ms
50,408 KB
testcase_04 AC 58 ms
50,444 KB
testcase_05 AC 61 ms
50,812 KB
testcase_06 AC 62 ms
50,716 KB
testcase_07 AC 60 ms
50,748 KB
testcase_08 AC 60 ms
50,396 KB
testcase_09 AC 59 ms
50,716 KB
testcase_10 AC 61 ms
50,728 KB
testcase_11 AC 557 ms
55,136 KB
testcase_12 AC 230 ms
54,852 KB
testcase_13 AC 497 ms
54,864 KB
testcase_14 AC 338 ms
54,648 KB
testcase_15 AC 285 ms
54,772 KB
testcase_16 AC 469 ms
55,016 KB
testcase_17 AC 344 ms
54,408 KB
testcase_18 AC 445 ms
54,568 KB
testcase_19 AC 234 ms
54,852 KB
testcase_20 AC 334 ms
54,476 KB
testcase_21 AC 499 ms
55,024 KB
testcase_22 AC 356 ms
54,764 KB
testcase_23 AC 299 ms
54,920 KB
testcase_24 AC 299 ms
54,908 KB
testcase_25 AC 310 ms
54,548 KB
testcase_26 AC 301 ms
54,932 KB
testcase_27 AC 420 ms
55,104 KB
testcase_28 AC 504 ms
54,844 KB
testcase_29 AC 459 ms
54,844 KB
testcase_30 AC 345 ms
54,592 KB
testcase_31 AC 339 ms
54,568 KB
testcase_32 AC 513 ms
54,744 KB
testcase_33 AC 306 ms
54,836 KB
testcase_34 AC 414 ms
54,964 KB
testcase_35 AC 330 ms
54,492 KB
testcase_36 AC 331 ms
54,668 KB
testcase_37 AC 289 ms
54,676 KB
testcase_38 AC 302 ms
54,736 KB
testcase_39 AC 501 ms
54,768 KB
testcase_40 AC 457 ms
54,912 KB
testcase_41 AC 433 ms
54,912 KB
testcase_42 AC 501 ms
54,856 KB
testcase_43 AC 241 ms
54,904 KB
testcase_44 AC 522 ms
54,940 KB
testcase_45 AC 266 ms
54,448 KB
testcase_46 AC 427 ms
54,800 KB
testcase_47 AC 301 ms
54,728 KB
testcase_48 AC 279 ms
54,928 KB
testcase_49 AC 223 ms
54,644 KB
testcase_50 AC 230 ms
54,872 KB
testcase_51 AC 467 ms
55,084 KB
testcase_52 AC 469 ms
55,152 KB
testcase_53 AC 515 ms
55,792 KB
testcase_54 AC 461 ms
55,020 KB
testcase_55 AC 511 ms
54,968 KB
testcase_56 AC 456 ms
54,916 KB
testcase_57 AC 451 ms
55,008 KB
testcase_58 AC 492 ms
55,076 KB
testcase_59 AC 494 ms
54,600 KB
testcase_60 AC 452 ms
54,928 KB
testcase_61 AC 504 ms
55,032 KB
testcase_62 AC 502 ms
55,108 KB
testcase_63 AC 505 ms
55,008 KB
testcase_64 AC 507 ms
54,844 KB
testcase_65 AC 521 ms
55,160 KB
testcase_66 AC 513 ms
55,104 KB
testcase_67 AC 514 ms
55,048 KB
testcase_68 AC 525 ms
55,244 KB
testcase_69 AC 513 ms
55,296 KB
testcase_70 AC 514 ms
54,988 KB
testcase_71 AC 501 ms
54,920 KB
testcase_72 AC 514 ms
54,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

@SuppressWarnings("unused")
public class Main {

  private static void solve() {
    int n = ni() * 2;
    int k = ni();
    int[] a = new int[n];
    int[] c = new int[n];
    for (int i = 0; i < n / 2; i++) {
      a[i] = a[i + n / 2] = ni();
    }
    for (int i = 0; i < n / 2; i++) {
      c[i] = c[i + n / 2] = ni();
    }

    long[][] dp = new long[n][n + 1];

    long ret = 0;
    for (int len = 1; len <= n / 2; len++) {
      for (int l = 0; l < n; l++) {
        int r = l + len;
        if (r > n)
          continue;
        if (len == 1) {
          dp[l][r] = 1L << c[l];
        } else {
          for (int j = l + 1; j < r; j++) {
            for (int d = 0; d <= k; d++) {
              dp[l][r] |= dp[l][j] & (dp[j][r] << d);
              dp[l][r] |= dp[l][j] & (dp[j][r] >> d);
              dp[l][r] |= (dp[l][j] << d) & dp[j][r];
              dp[l][r] |= (dp[l][j] >> d) & dp[j][r];
            }
          }
        }
      }
    }

    for (int l = 0; l < n; l++) {
      for (int r = l + 1; r <= n; r++) {
        long cur = 0;
        if (Long.bitCount(dp[l][r]) > 0) {
          for (int s = l; s < r; s++) {
            cur += a[s];
          }
          ret = Math.max(ret, cur);
        }
      }
    }
    // 28 11 32 60 53 6 28 96 8 23 19 76 62 76
    // 19 5 3 11 14 5 3 19 10 10 16 13 3 8
    //System.out.println(Arrays.deepToString(dp));
    System.out.println(ret);
  }

  public static void main(String[] args) {
    new Thread(null, new Runnable() {
      @Override
      public void run() {
        solve();
        out.flush();
      }
    }, "", 64000000).start();
  }

  private static PrintWriter out = new PrintWriter(System.out);
  private static StringTokenizer tokenizer = null;
  private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 32768);

  public static String next() {
    while (tokenizer == null || !tokenizer.hasMoreTokens()) {
      try {
        tokenizer = new java.util.StringTokenizer(reader.readLine());
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
    return tokenizer.nextToken();
  }

  private static double nd() {
    return Double.parseDouble(next());
  }

  private static long nl() {
    return Long.parseLong(next());
  }

  private static int[] na(int n) {
    int[] a = new int[n];
    for (int i = 0; i < n; i++)
      a[i] = ni();
    return a;
  }

  private static char[] ns() {
    return next().toCharArray();
  }

  private static long[] nal(int n) {
    long[] a = new long[n];
    for (int i = 0; i < n; i++)
      a[i] = nl();
    return a;
  }

  private static int[][] ntable(int n, int m) {
    int[][] table = new int[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        table[i][j] = ni();
      }
    }
    return table;
  }

  private static int[][] nlist(int n, int m) {
    int[][] table = new int[m][n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        table[j][i] = ni();
      }
    }
    return table;
  }

  private static int ni() {
    return Integer.parseInt(next());
  }
}
0