結果

問題 No.1433 Two color sequence
ユーザー neko_the_shadowneko_the_shadow
提出日時 2021-04-12 23:26:12
言語 Java21
(openjdk 21)
結果
AC  
実行時間 454 ms / 2,000 ms
コード長 6,205 bytes
コンパイル時間 2,468 ms
コンパイル使用メモリ 91,624 KB
実行使用メモリ 62,080 KB
最終ジャッジ日時 2024-06-28 18:42:17
合計ジャッジ時間 12,127 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
37,928 KB
testcase_01 AC 61 ms
38,044 KB
testcase_02 AC 58 ms
37,572 KB
testcase_03 AC 332 ms
58,116 KB
testcase_04 AC 336 ms
58,144 KB
testcase_05 AC 57 ms
37,792 KB
testcase_06 AC 57 ms
37,824 KB
testcase_07 AC 57 ms
37,228 KB
testcase_08 AC 389 ms
61,840 KB
testcase_09 AC 442 ms
61,648 KB
testcase_10 AC 442 ms
61,496 KB
testcase_11 AC 380 ms
60,664 KB
testcase_12 AC 407 ms
61,880 KB
testcase_13 AC 408 ms
62,080 KB
testcase_14 AC 406 ms
61,332 KB
testcase_15 AC 454 ms
61,412 KB
testcase_16 AC 376 ms
60,972 KB
testcase_17 AC 431 ms
61,092 KB
testcase_18 AC 422 ms
61,256 KB
testcase_19 AC 424 ms
61,288 KB
testcase_20 AC 408 ms
61,088 KB
testcase_21 AC 374 ms
61,240 KB
testcase_22 AC 385 ms
61,216 KB
testcase_23 AC 409 ms
61,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package _1433;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.function.Supplier;
import java.util.regex.Pattern;

public class Main {
    public void exec() {
        int n = stdin.nextInt();
        String s = stdin.nextString();
        long[] a = stdin.nextLongArray(n);
        
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == 'B') a[i] *= -1;
        }
        long max = MaximumSubarray.solve(a).getSum();
        
        for (int i = 0; i < n; i++) a[i] *= -1;
        long min = MaximumSubarray.solve(a).getSum();
        
        stdout.println(Math.max(max, min));
    }
    
    /**
     * Kadane's algorithmを用いて、最大部分配列和を求める。
     */
    public static class MaximumSubarray {
        public static MaximumSubarray solve(long[] array) {
            long bestSum = 0;
            long currentSum = 0;

            int currentStart = 0;
            int bestStart = -1;
            int bestEnd = -1;

            for (int currentEnd = 0; currentEnd < array.length; currentEnd++) {
                if (currentSum < 0) {
                    currentStart = currentEnd;
                    currentSum = array[currentEnd];
                } else {
                    currentSum += array[currentEnd];
                }

                if (currentSum > bestSum) {
                    bestSum = currentSum;
                    bestStart = currentStart;
                    bestEnd = currentEnd;
                }
            }

            MaximumSubarray maximumSubarray = new MaximumSubarray();
            maximumSubarray.sum = bestSum;
            maximumSubarray.start = bestStart;
            maximumSubarray.end = bestEnd;
            return maximumSubarray;
        }

        private MaximumSubarray() {}

        private long sum;
        private int start;
        private int end;

        public long getSum() {
            return sum;
        }

        public int getStart() {
            return start;
        }

        public int getEnd() {
            return end;
        }
    }

    private static final Stdin stdin = new Stdin(System.in);
    private static final Stdout stdout = new Stdout(System.out);

    public static void main(String[] args) {
        try {
            new Main().exec();
        } finally {
            stdout.flush();
        }
    }

    public static class Stdin {
        private Deque<String> queue;
        private BufferedReader in;
        private Pattern space;

        public Stdin(InputStream in) {
            this.queue = new ArrayDeque<>();
            this.in = new BufferedReader(new InputStreamReader(in));
            this.space = Pattern.compile(" ");
        }

        public String nextString() {
            if (queue.isEmpty()) {
                try {
                    String line = in.readLine();
                    if (line == null) {
                        throw new EOFException();
                    }
                    space.splitAsStream(line).forEach(this.queue::addLast);
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
            return queue.removeFirst();
        }

        public int nextInt() {
            return Integer.parseInt(nextString());
        }

        public double nextDouble() {
            return Double.parseDouble(nextString());
        }

        public long nextLong() {
            return Long.parseLong(nextString());
        }
        
        public BigInteger nextBigInteger() {
            return new BigInteger(nextString());
        }

        public String[] nextStringArray(int n) {
            String[] a = new String[n];
            for (int i = 0; i < n; i++) a[i] = nextString();
            return a;
        }

        public int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) a[i] = nextInt();
            return a;
        }

        public double[] nextDoubleArray(int n) {
            double[] a = new double[n];
            for (int i = 0; i < n; i++) a[i] = nextDouble();
            return a;
        }

        public long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++) a[i] = nextLong();
            return a;
        }
        
        public BigInteger[] nexBigIntegerArray(int n) {
            BigInteger[] a = new BigInteger[n];
            for (int i = 0; i < n; i++) a[i] = nextBigInteger();
            return a;
        }
        
        public List<Integer> nextIntegerList(int n) {
            return nextList(n, this::nextInt);
        }
        
        public List<Long> nextLongList(int n) {
            return nextList(n, this::nextLong);
        }
        
        public List<Double> nextDoubleList(int n) {
            return nextList(n, this::nextDouble);
        }
        
        public List<String> nextStringList(int n) {
            return nextList(n, this::nextString);
        }
        
        public List<BigInteger> nextBigIntegerList(int n) {
            return nextList(n, this::nextBigInteger);
        }
        
        private <T> List<T> nextList(int n, Supplier<T> supplier) {
            List<T> a = new ArrayList<>();
            for (int i = 0; i < n; i++) a.add(supplier.get());
            return a;
        }
    }

    public static class Stdout {
        private PrintWriter stdout;

        public Stdout(PrintStream stdout) {
            this.stdout =  new PrintWriter(stdout, false);
        }

        public void println(Object ... objs) {
            for (int i = 0, len = objs.length; i < len; i++) {
                stdout.print(objs[i]);
                if (i != len-1) stdout.print(' ');
            }
            stdout.println();
        }

        public void flush() {
            stdout.flush();
        }
    }
}
0