結果

問題 No.1288 yuki collection
ユーザー tenten
提出日時 2023-06-07 11:37:29
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 3,805 bytes
コンパイル時間 3,883 ms
コンパイル使用メモリ 87,160 KB
実行使用メモリ 745,416 KB
最終ジャッジ日時 2024-12-29 17:01:48
合計ジャッジ時間 82,915 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 5 TLE * 7 MLE * 28
権限があれば一括ダウンロードができます

ソースコード

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