結果

問題 No.1433 Two color sequence
ユーザー tentententen
提出日時 2023-08-03 13:26:37
言語 Java21
(openjdk 21)
結果
AC  
実行時間 362 ms / 2,000 ms
コード長 2,163 bytes
コンパイル時間 2,563 ms
コンパイル使用メモリ 92,116 KB
実行使用メモリ 55,444 KB
最終ジャッジ日時 2024-04-21 11:34:09
合計ジャッジ時間 11,015 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
37,116 KB
testcase_01 AC 51 ms
36,796 KB
testcase_02 AC 51 ms
37,056 KB
testcase_03 AC 249 ms
48,172 KB
testcase_04 AC 252 ms
47,760 KB
testcase_05 AC 52 ms
37,060 KB
testcase_06 AC 51 ms
37,048 KB
testcase_07 AC 51 ms
37,108 KB
testcase_08 AC 319 ms
54,928 KB
testcase_09 AC 358 ms
55,356 KB
testcase_10 AC 354 ms
54,904 KB
testcase_11 AC 325 ms
54,412 KB
testcase_12 AC 362 ms
55,204 KB
testcase_13 AC 360 ms
55,444 KB
testcase_14 AC 310 ms
54,668 KB
testcase_15 AC 344 ms
54,272 KB
testcase_16 AC 360 ms
54,784 KB
testcase_17 AC 353 ms
54,160 KB
testcase_18 AC 361 ms
55,216 KB
testcase_19 AC 348 ms
54,980 KB
testcase_20 AC 322 ms
54,588 KB
testcase_21 AC 357 ms
54,920 KB
testcase_22 AC 359 ms
54,628 KB
testcase_23 AC 358 ms
54,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        char[] colors = sc.next().toCharArray();
        long[] sums = new long[n + 1];
        long min = 0;
        long max = 0;
        long plus = Long.MIN_VALUE;
        long minus = Long.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();
            if (colors[i] == 'B') {
                sums[i + 1] = sums[i] + x;
            } else {
                sums[i + 1] = sums[i] - x;
            }
            plus = Math.max(plus, sums[i + 1] - min);
            minus = Math.min(minus, sums[i + 1] - max);
            min = Math.min(min, sums[i + 1]);
            max = Math.max(max, sums[i + 1]);
        }
        minus *= -1;
        System.out.println(Math.max(plus, minus));
    }
} 
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