結果

問題 No.1288 yuki collection
ユーザー tentententen
提出日時 2023-06-07 11:37:29
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,805 bytes
コンパイル時間 2,687 ms
コンパイル使用メモリ 87,164 KB
実行使用メモリ 50,860 KB
最終ジャッジ日時 2023-08-28 14:55:52
合計ジャッジ時間 10,048 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
50,860 KB
testcase_01 AC 45 ms
49,380 KB
testcase_02 AC 48 ms
50,312 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static char[] inputs;
    static int[] values;
    static HashMap<Character, Integer> idxes = new HashMap<>();
    static long[][][][][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        inputs = sc.next().toCharArray();
        char[] yuki = "yuki".toCharArray();
        for (int i = 0; i < yuki.length; i++) {
            idxes.put(yuki[i], i);
        }
        int[] counts = new int[5];
        counts[0] = Integer.MAX_VALUE;
        for (char c : inputs) {
            int idx = idxes.get(c);
            if (counts[idx] > counts[idx + 1]) {
                counts[idx + 1]++;
            }
        }
        int max = counts[4];
        values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
        }
        dp = new long[n][max + 1][max + 1][max + 1][max + 1];
        for (long[][][][] arr1 : dp) {
            for (long[][][] arr2 : arr1) {
                for (long[][] arr3 : arr2) {
                    for (long[] arr4 : arr3) {
                        Arrays.fill(arr4, -1);
                    }
                }
            }
        }
        System.out.println(dfw(n - 1, max, max, max, max));
    }
    
    static long dfw(int idx, int a, int b, int c, int d) {
        if (a < 0 || b < 0 || c < 0 || d < 0) {
            return Long.MIN_VALUE;
        }
        if (a == 0 && b == 0 && c == 0 && d == 0) {
            return 0;
        }
        if (idx < 0) {
            return Long.MIN_VALUE;
        }
        if (dp[idx][a][b][c][d] < 0) {
            dp[idx][a][b][c][d] = dfw(idx - 1, a, b, c, d);
            int i = idxes.get(inputs[idx]);
            if (i == 3) {
                dp[idx][a][b][c][d] = Math.max(dp[idx][a][b][c][d], dfw(idx - 1, a, b, c, d - 1) + values[idx]);
            } else if (i == 2) {
                if (c > d) {
                    dp[idx][a][b][c][d] = Math.max(dp[idx][a][b][c][d], dfw(idx - 1, a, b, c - 1, d) + values[idx]);
                }
            } else if (i == 1) {
                if (b > c) {
                    dp[idx][a][b][c][d] = Math.max(dp[idx][a][b][c][d], dfw(idx - 1, a, b - 1, c, d) + values[idx]);
                }
            } else {
                if (a > b) {
                    dp[idx][a][b][c][d] = Math.max(dp[idx][a][b][c][d], dfw(idx - 1, a - 1, b, c, d) + values[idx]);
                }
            }
        }
        return dp[idx][a][b][c][d];
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0