結果

問題 No.1433 Two color sequence
ユーザー shun_skycrewshun_skycrew
提出日時 2021-03-22 17:58:47
言語 Java21
(openjdk 21)
結果
AC  
実行時間 211 ms / 2,000 ms
コード長 23,263 bytes
コンパイル時間 3,660 ms
コンパイル使用メモリ 96,576 KB
実行使用メモリ 44,284 KB
最終ジャッジ日時 2024-11-24 04:53:44
合計ジャッジ時間 9,078 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
38,748 KB
testcase_01 AC 74 ms
38,852 KB
testcase_02 AC 75 ms
38,748 KB
testcase_03 AC 190 ms
43,676 KB
testcase_04 AC 180 ms
43,900 KB
testcase_05 AC 76 ms
38,712 KB
testcase_06 AC 76 ms
38,384 KB
testcase_07 AC 76 ms
38,612 KB
testcase_08 AC 195 ms
43,736 KB
testcase_09 AC 199 ms
43,556 KB
testcase_10 AC 206 ms
44,080 KB
testcase_11 AC 205 ms
43,792 KB
testcase_12 AC 211 ms
44,284 KB
testcase_13 AC 205 ms
44,188 KB
testcase_14 AC 198 ms
43,608 KB
testcase_15 AC 204 ms
44,092 KB
testcase_16 AC 207 ms
43,768 KB
testcase_17 AC 207 ms
43,912 KB
testcase_18 AC 199 ms
43,588 KB
testcase_19 AC 200 ms
43,928 KB
testcase_20 AC 204 ms
44,104 KB
testcase_21 AC 210 ms
43,700 KB
testcase_22 AC 199 ms
44,252 KB
testcase_23 AC 207 ms
43,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
import java.math.*;
import java.util.function.*;
public class Main implements Runnable {
	static boolean DEBUG;
	public static void main(String[] args) {
		DEBUG = args.length > 0 && args[0].equals("-DEBUG");
		Thread.setDefaultUncaughtExceptionHandler((t, e) -> { e.printStackTrace(); System.exit(1); });
		new Thread(null, new Main(), "", 1 << 31).start();
	}

	public void run() { Solver solver = new Solver(); solver.solve(); solver.exit(); }

	static class FastScanner {
		private final InputStream in = System.in;
		private final byte[] buffer = new byte[1024];
		private int pointer = 0;
		private int buflen = 0;
		private boolean hasNextByte() {
			if(pointer < buflen) return true;
			else {
				pointer = 0;
				try { buflen = in.read(buffer);
				}catch (IOException e) { e.printStackTrace(); }
				return buflen > 0;
			}
		}
		private int readByte() { if(hasNextByte()) return buffer[pointer ++]; else return -1; }
		private boolean isPrintableChar(int c) { return isPrintableChar(c, false); }
		private boolean isPrintableChar(int c, boolean includingSpace) { return (includingSpace ? 32 : 33) <= c && c <= 126; }
		private void skipUnprintable() { skipUnprintable(false); }
		private void skipUnprintable(boolean includingSpace) { while(hasNextByte() && !isPrintableChar(buffer[pointer], includingSpace)) pointer ++; }
		private boolean hasNext() { return hasNext(false); }
		private boolean hasNext(boolean includingSpace) { skipUnprintable(includingSpace); return hasNextByte(); }
		private StringBuilder sb = new StringBuilder();
		public String next() { return next(false); }
		public String next(boolean includingSpace) {
			if(!hasNext(includingSpace)) throw new NoSuchElementException();
			sb.setLength(0);
			int b = readByte();
			while(isPrintableChar(b, includingSpace)) { sb.appendCodePoint(b); b = readByte(); }
			return sb.toString();
		}
		public long nextLong() {
			if(!hasNext()) throw new NoSuchElementException();
			long n = 0;
			boolean minus = false;
			int b = readByte();
			if(b == '-') { minus = true; b = readByte(); }
			if(b < '0' || '9' < b) throw new NumberFormatException();
			while(true) {
				if('0' <= b && b <= '9') n = n * 10 + b - '0';
				else if(b == -1 || !isPrintableChar(b)) return minus ? - n : n;
				else throw new NumberFormatException();
				b = readByte();
			}
		}
	}

	static class Solver {
		final FastScanner sc = new FastScanner();
		public Solver() { }

		final String ns() { return ns(false); }
		final String ns(boolean includingSpace) { return sc.next(includingSpace); }
		final String[] ns(int n) { return ns(n, false); }
		final String[] ns(int n, boolean includingSpace) { String a[] = new String[n]; for(int i = 0; i < n; i ++) a[i] = ns(includingSpace); return a; }
		final String[][] ns(int n, int m) { return ns(n, m, false); }
		final String[][] ns(int n, int m, boolean includingSpace) { String a[][] = new String[n][m]; for(int i = 0; i < n; i ++) a[i] = ns(m, includingSpace); return a; }
		final char nc() { return ns().charAt(0); }
		final char[] nc(int n) {
			String str = ns();
			if(n < 0) n = str.length();
			char a[] = new char[n];
			for(int i = 0; i < n; i ++) a[i] = str.charAt(i);
			return a;
		}
		final char[][] nc(int n, int m) { char a[][] = new char[n][m]; for(int i = 0; i < n; i ++) a[i] = nc(m); return a; }
		final boolean[] nb(int n, char t) {
			char c[] = nc(-1);
			if(n < 0) n = c.length;
			boolean a[] = new boolean[n];
			for(int i = 0; i < n; i ++) a[i] = c[i] == t;
			return a;
		}
		final boolean[][] nb(int n, int m, char t) { boolean a[][] = new boolean[n][m]; for(int i = 0; i < n; i ++) a[i] = nb(m, t); return a; }
		final int ni() { return Math.toIntExact(sc.nextLong()); }
		final int[] ni(int n) { int a[] = new int[n]; for(int i = 0; i < n; i ++) a[i] = ni(); return a; }
		final int[][] ni(int n, int m) { int a[][] = new int[n][m]; for(int i = 0; i < n; i ++) a[i] = ni(m); return a; }
		final long nl() { return sc.nextLong(); }
		final long[] nl(int n) { long a[] = new long[n]; for(int i = 0; i < n; i ++) a[i] = nl(); return a; }
		final long[][] nl(int n, int m) { long a[][] = new long[n][m]; for(int i = 0; i < n; i ++) a[i] = nl(m); return a; }
		final double nd() { return Double.parseDouble(sc.next()); }
		final double[] nd(int n) { double a[] = new double[n]; for(int i = 0; i < n; i ++) a[i] = nd(); return a; }
		final double[][] nd(int n, int m) { double a[][] = new double[n][m]; for(int i = 0; i < n; i ++) a[i] = nd(m); return a; }

		final String booleanToString(boolean b) { return b ? "#" : "."; }

		final PrintWriter out = new PrintWriter(System.out);
		final PrintWriter err = new PrintWriter(System.err);
		final StringBuilder sb4prtln = new StringBuilder();
		final void prt() { out.print(""); }
		final <T> void prt(T a) { out.print(a); }
		final void prtln() { out.println(""); }
		final <T> void prtln(T a) { out.println(a); }
		final void prtln(int... a) { sb4prtln.setLength(0); for(int ele : a) { sb4prtln.append(ele); sb4prtln.append(" "); } prtln(sb4prtln.toString().trim()); }
		final void prtln(long... a) { sb4prtln.setLength(0); for(long ele : a) { sb4prtln.append(ele); sb4prtln.append(" "); } prtln(sb4prtln.toString().trim()); }
		final void prtln(double... a) { sb4prtln.setLength(0); for(double ele : a) { sb4prtln.append(ele); sb4prtln.append(" "); } prtln(sb4prtln.toString().trim()); }
		final void prtln(String... a) { sb4prtln.setLength(0); for(String ele : a) { sb4prtln.append(ele); sb4prtln.append(" "); } prtln(sb4prtln.toString().trim()); }
		final void prtln(char... a) { sb4prtln.setLength(0); for(char ele : a) sb4prtln.append(ele); prtln(sb4prtln.toString()); }
		final void prtln(boolean... a) { sb4prtln.setLength(0); for(boolean ele : a) sb4prtln.append(booleanToString(ele)); prtln(sb4prtln.toString()); }
		final void prtlns(int... a) { sb4prtln.setLength(0); for(int ele : a) { sb4prtln.append(ele); sb4prtln.append("\n"); } prt(sb4prtln.toString()); }
		final void prtlns(long... a) { sb4prtln.setLength(0); for(long ele : a) { sb4prtln.append(ele); sb4prtln.append("\n"); } prt(sb4prtln.toString()); }
		final void prtlns(double... a) { sb4prtln.setLength(0); for(double ele : a) { sb4prtln.append(ele); sb4prtln.append("\n"); } prt(sb4prtln.toString()); }
		final void prtlns(String... a) { sb4prtln.setLength(0); for(String ele : a) { sb4prtln.append(ele); sb4prtln.append("\n"); } prt(sb4prtln.toString()); }
		final void prtlns(char... a) { sb4prtln.setLength(0); for(char ele : a) { sb4prtln.append(ele); sb4prtln.append("\n"); } prt(sb4prtln.toString()); }
		final void prtlns(boolean... a) { sb4prtln.setLength(0); for(boolean ele : a) { sb4prtln.append(booleanToString(ele)); sb4prtln.append("\n"); } prt(sb4prtln.toString()); }
		final void prtln(int[][] a) { for(int[] ele : a) prtln(ele); }
		final void prtln(long[][] a) { for(long[] ele : a) prtln(ele); }
		final void prtln(double[][] a) { for(double[] ele : a) prtln(ele); }
		final void prtln(String[][] a) { for(String[] ele : a) prtln(ele); }
		final void prtln(char[][] a) { for(char[] ele : a) prtln(ele); }
		final void prtln(boolean[][] a) { for(boolean[] ele : a) prtln(ele); }

		final String errconvert(int a) { return isINF(a) ? "_" : String.valueOf(a); }
		final String errconvert(long a) { return isINF(a) ? "_" : String.valueOf(a); }
		final void errprt(int a) { if(DEBUG) err.print(errconvert(a)); }
		final void errprt(long a) { if(DEBUG) err.print(errconvert(a)); }
		final void errprt() { if(DEBUG) err.print(""); }
		final <T> void errprt(T a) { if(DEBUG) err.print(a); }
		final void errprt(boolean a) { if(DEBUG) errprt(booleanToString(a)); }
		final void errprtln() { if(DEBUG) err.println(""); }
		final void errprtln(int a) { if(DEBUG) err.println(errconvert(a)); }
		final void errprtln(long a) { if(DEBUG) err.println(errconvert(a)); }
		final <T> void errprtln(T a) { if(DEBUG) err.println(a); }
		final void errprtln(boolean a) { if(DEBUG) errprtln(booleanToString(a)); }
		final void errprtln(int... a) { if(DEBUG) { sb4prtln.setLength(0); for(int ele : a) { sb4prtln.append(errconvert(ele)); sb4prtln.append(" "); } errprtln(sb4prtln.toString().trim()); } }
		final void errprtln(long... a) { if(DEBUG) { sb4prtln.setLength(0); for(long ele : a) { sb4prtln.append(errconvert(ele)); sb4prtln.append(" "); } errprtln(sb4prtln.toString().trim()); } }
		final void errprtln(double... a) { if(DEBUG) { sb4prtln.setLength(0); for(double ele : a) { sb4prtln.append(ele); sb4prtln.append(" "); } errprtln(sb4prtln.toString().trim()); } }
		final void errprtln(String... a) { if(DEBUG) { sb4prtln.setLength(0); for(String ele : a) { sb4prtln.append(ele); sb4prtln.append(" "); } errprtln(sb4prtln.toString().trim()); } }
		final void errprtln(char... a) { if(DEBUG) { sb4prtln.setLength(0); for(char ele : a) sb4prtln.append(ele); errprtln(sb4prtln.toString()); } }
		final void errprtln(boolean... a) { if(DEBUG) { sb4prtln.setLength(0); for(boolean ele : a) sb4prtln.append(booleanToString(ele)); errprtln(sb4prtln.toString()); } }
		final void errprtlns(int... a) { if(DEBUG) { sb4prtln.setLength(0); for(int ele : a) { sb4prtln.append(errconvert(ele)); sb4prtln.append("\n"); } errprt(sb4prtln.toString()); } }
		final void errprtlns(long... a) { if(DEBUG) { sb4prtln.setLength(0); for(long ele : a) { sb4prtln.append(errconvert(ele)); sb4prtln.append("\n"); } errprt(sb4prtln.toString()); } }
		final void errprtlns(double... a) { if(DEBUG) { sb4prtln.setLength(0); for(double ele : a) { sb4prtln.append(ele); sb4prtln.append("\n"); } errprt(sb4prtln.toString()); } }
		final void errprtlns(String... a) { if(DEBUG) { sb4prtln.setLength(0); for(String ele : a) { sb4prtln.append(ele); sb4prtln.append("\n"); } errprt(sb4prtln.toString()); } }
		final void errprtlns(char... a) { if(DEBUG) { sb4prtln.setLength(0); for(char ele : a) { sb4prtln.append(ele); sb4prtln.append("\n"); } errprt(sb4prtln.toString()); } }
		final void errprtlns(boolean... a) { if(DEBUG) { sb4prtln.setLength(0); for(boolean ele : a) { sb4prtln.append(booleanToString(ele)); sb4prtln.append("\n"); } errprt(sb4prtln.toString()); } }
		final void errprtln(Object[] a) { if(DEBUG) for(Object ele : a) errprtln(ele); }
		final void errprtln(int[][] a) { if(DEBUG) for(int[] ele : a) errprtln(ele); }
		final void errprtln(long[][] a) { if(DEBUG) for(long[] ele : a) errprtln(ele); }
		final void errprtln(double[][] a) { if(DEBUG) for(double[] ele : a) errprtln(ele); }
		final void errprtln(String[][] a) { if(DEBUG) for(String[] ele : a) errprtln(ele); }
		final void errprtln(char[][] a) { if(DEBUG) for(char[] ele : a) errprtln(ele); }
		final void errprtln(boolean[][] a) { if(DEBUG) for(boolean[] ele : a) errprtln(ele); }
		final void errprtln(Object[][] a) { if(DEBUG) for(Object ele : a) { errprtln(ele); errprtln(); } }

		final void reply(boolean b) { prtln(b ? "Yes" : "No"); }
		final void REPLY(boolean b) { prtln(b ? "YES" : "NO"); }

		final void flush() { out.flush(); if(DEBUG) err.flush(); }
		final void assertion(boolean b) { if(!b) { flush(); throw new AssertionError(); } }
		final <T> void assertion(boolean b, T a) { if(!b) { errprtln(a); flush(); throw new AssertionError(); } }
		final void assertion(boolean b, int... a) { if(!b) { errprtln(a); flush(); throw new AssertionError(); } }
		final void assertion(boolean b, long... a) { if(!b) { errprtln(a); flush(); throw new AssertionError(); } }
		final void assertion(boolean b, double... a) { if(!b) { errprtln(a); flush(); throw new AssertionError(); } }
		final void assertion(boolean b, String... a) { if(!b) { errprtln(a); flush(); throw new AssertionError(); } }
		final void assertion(boolean b, char... a) { if(!b) { errprtln(a); flush(); throw new AssertionError(); } }
		final void assertion(boolean b, boolean... a) { if(!b) { errprtln(a); flush(); throw new AssertionError(); } }
		final void assertion(boolean b, int[][] a) { if(!b) { errprtln(a); flush(); throw new AssertionError(); } }
		final void assertion(boolean b, long[][] a) { if(!b) { errprtln(a); flush(); throw new AssertionError(); } }
		final void assertion(boolean b, double[][] a) { if(!b) { errprtln(a); flush(); throw new AssertionError(); } }
		final void assertion(boolean b, String[][] a) { if(!b) { errprtln(a); flush(); throw new AssertionError(); } }
		final void assertion(boolean b, char[][] a) { if(!b) { errprtln(a); flush(); throw new AssertionError(); } }
		final void assertion(boolean b, boolean[][] a) { if(!b) { errprtln(a); flush(); throw new AssertionError(); } }
		final void inclusiveRangeCheck(int i, int max) { inclusiveRangeCheck(i, 0, max); }
		final void inclusiveRangeCheck(int i, int min, int max) { rangeCheck(i, min, max + 1); }
		final void rangeCheck(int i, int max) { rangeCheck(i, 0, max); }
		final void rangeCheck(int i, int min, int max) { if(i < min || i >= max) throw new IndexOutOfBoundsException(String.format("Index %d out of bounds for length %d", i, max)); }
		final void nonNegativeCheck(long x) { nonNegativeCheck(x, "the argument"); }
		final void nonNegativeCheck(long x, String attribute) { if(x < 0) throw new IllegalArgumentException(String.format("%s %d is negative", attribute, x)); }
		final void positiveCheck(long x) { positiveCheck(x, "the argument"); }
		final void positiveCheck(long x, String attribute) { if(x <= 0) throw new IllegalArgumentException(String.format("%s %d is negative", attribute, x)); }

		final void exit() { flush(); System.exit(0); }
		final <T> void exit(T a) { prtln(a); exit(); }
		final void exit(int... a) { prtln(a); exit(); }
		final void exit(long... a) { prtln(a); exit(); }
		final void exit(double... a) { prtln(a); exit(); }
		final void exit(String... a) { prtln(a); exit(); }
		final void exit(char... a) { prtln(a); exit(); }
		final void exit(boolean... a) { prtln(a); exit(); }
		final void exit(int[][] a) { prtln(a); exit(); }
		final void exit(long[][] a) { prtln(a); exit(); }
		final void exit(double[][] a) { prtln(a); exit(); }
		final void exit(String[][] a) { prtln(a); exit(); }
		final void exit(char[][] a) { prtln(a); exit(); }
		final void exit(boolean[][] a) { prtln(a); exit(); }


		final long INF = (long)1e18 + 7;
		final boolean isPlusINF(long a) { return a > INF / 10; }
		final boolean isMinusINF(long a) { return isPlusINF(- a); }
		final boolean isINF(long a) { return isPlusINF(a) || isMinusINF(a); }
		final int I_INF = (int)1e9 + 7;
		final boolean isPlusINF(int a) { return a > I_INF / 10; }
		final boolean isMinusINF(int a) { return isPlusINF(- a); }
		final boolean isINF(int a) { return isPlusINF(a) || isMinusINF(a); }


		final int min(int a, int b) { return Math.min(a, b); }
		final long min(long a, long b) { return Math.min(a, b); }
		final double min(double a, double b) { return Math.min(a, b); }
		final <T extends Comparable<T>> T min(T a, T b) { return a.compareTo(b) <= 0 ? a : b; }
		final int min(int... x) { int min = x[0]; for(int val : x) min = min(min, val); return min; }
		final long min(long... x) { long min = x[0]; for(long val : x) min = min(min, val); return min; }
		final double min(double... x) { double min = x[0]; for(double val : x) min = min(min, val); return min; }
		final int max(int a, int b) { return Math.max(a, b); }
		final long max(long a, long b) { return Math.max(a, b); }
		final double max(double a, double b) { return Math.max(a, b); }
		final <T extends Comparable<T>> T max(T a, T b) { return a.compareTo(b) >= 0 ? a : b; }
		final int max(int... x) { int max = x[0]; for(int val : x) max = max(max, val); return max; }
		final long max(long... x) { long max = x[0]; for(long val : x) max = max(max, val); return max; }
		final double max(double... x) { double max = x[0]; for(double val : x) max = max(max, val); return max; }
		final <T extends Comparable<T>> T max(T[] x) { T max = x[0]; for(T val : x) max = max(max, val); return max; }
		final int max(int[][] a) { int max = a[0][0]; for(int[] ele : a) max = max(max, max(ele)); return max; }
		final long max(long[][] a) { long max = a[0][0]; for(long[] ele : a) max = max(max, max(ele)); return max; }
		final double max(double[][] a) { double max = a[0][0]; for(double[] ele : a) max = max(max, max(ele)); return max; }
		final <T extends Comparable<T>> T max(T[][] a) { T max = a[0][0]; for(T[] ele : a) max = max(max, max(ele)); return max; }
		final long sum(int... a) { long sum = 0; for(int ele : a) sum += ele; return sum; }
		final long sum(long... a) { long sum = 0; for(long ele : a) sum += ele; return sum; }
		final double sum(double... a) { double sum = 0; for(double ele : a) sum += ele; return sum; }
		final long sum(boolean... a) { long sum = 0; for(boolean ele : a) sum += ele ? 1 : 0; return sum; }
		final long[] sums(int[] a) { long sum[] = new long[a.length + 1]; sum[0] = 0; for(int i = 0; i < a.length; i ++) sum[i + 1] = sum[i] + a[i]; return sum; }
		final long[] sums(long[] a) { long sum[] = new long[a.length + 1]; sum[0] = 0; for(int i = 0; i < a.length; i ++) sum[i + 1] = sum[i] + a[i]; return sum; }
		final double[] sums(double[] a) { double sum[] = new double[a.length + 1]; sum[0] = 0; for(int i = 0; i < a.length; i ++) sum[i + 1] = sum[i] + a[i]; return sum; }
		final long[] sums(boolean[] a) { long sum[] = new long[a.length + 1]; sum[0] = 0; for(int i = 0; i < a.length; i ++) sum[i + 1] = sum[i] + (a[i] ? 1 : 0); return sum; }

		final int constrain(int x, int l, int r) { return min(max(x, min(l, r)), max(l, r)); }
		final long constrain(long x, long l, long r) { return min(max(x, min(l, r)), max(l, r)); }
		final double constrain(double x, double l, double r) { return min(max(x, min(l, r)), max(l, r)); }
		final int abs(int x) { return x >= 0 ? x : - x; }
		final long abs(long x) { return x >= 0 ? x : - x; }
		final double abs(double x) { return x >= 0 ? x : - x; }
		final int signum(int x) { return x > 0 ? 1 : x < 0 ? -1 : 0; }
		final int signum(long x) { return x > 0 ? 1 : x < 0 ? -1 : 0; }
		final int signum(double x) { return x > 0 ? 1 : x < 0 ? -1 : 0; }
		final long round(double x) { return Math.round(x); }
		final long floor(double x) { return (long)Math.floor(x); }
		final int divfloor(int a, int b) { return signum(a) == signum(b) ? a / b : - divceil(abs(a), abs(b)); }
		final long divfloor(long a, long b) { return signum(a) == signum(b) ? a / b : - divceil(abs(a), abs(b)); }
		final long ceil(double x) { return (long)Math.ceil(x); }
		final int divceil(int a, int b) { return a >= 0 && b > 0 ? (a + b - 1) / b : a < 0 && b < 0 ? divceil(abs(a), abs(b)) : - divfloor(abs(a), abs(b)); }
		final long divceil(long a, long b) { return a >= 0 && b > 0 ? (a + b - 1) / b : a < 0 && b < 0 ? divceil(abs(a), abs(b)) : - divfloor(abs(a), abs(b)); }
		final boolean mulGreater(long a, long b, long c) { return b == 0 ? c < 0 : b < 0 ? mulLess(a, - b, - c) : a > divfloor(c, b); } // a * b > c
		final boolean mulGreaterEquals(long a, long b, long c) { return b == 0 ? c <= 0 : b < 0 ? mulLessEquals(a, - b, - c) : a >= divceil(c, b); } // a * b >= c
		final boolean mulLess(long a, long b, long c) { return !mulGreaterEquals(a, b, c); } // a * b < c
		final boolean mulLessEquals(long a, long b, long c) { return !mulGreater(a, b, c); } // a * b <= c
		final double sqrt(int x) { return Math.sqrt((double)x); }
		final double sqrt(long x) { return Math.sqrt((double)x); }
		final double sqrt(double x) { return Math.sqrt(x); }
		final long fact(int n) { long ans = 1; for(int i = 1; i <= n; i ++) ans = Math.multiplyExact(ans, i); return ans; }
		final long naiveP(long n, long r) { long ans = 1; for(int i = 0; i < r; i ++) ans = Math.multiplyExact(ans, n - i); return ans; }
		final long naiveC(long n, long r) { long ans = 1; for(int i = 0; i < r; i ++) { ans = Math.multiplyExact(ans, n - i); ans /= (i + 1); } return ans; }
		final double pow(double x, double y) { return Math.pow(x, y); }
		final long pow(long x, long y) {
			long ans = 1;
			while(true) {
				if((y & 1) != 0) ans = Math.multiplyExact(ans, x);
				y /= 2;
				if(y <= 0) return ans;
				x = Math.multiplyExact(x, x);
			}
		}
		final int gcd(int a, int b) { while(true) { if(b == 0) return a; int tmp = a; a = b; b = tmp % b; } }
		final long gcd(long a, long b) { while(true) { if(b == 0) return a; long tmp = a; a = b; b = tmp % b; } }
		final long lcm(long a, long b) { return a / gcd(a, b) * b; }
		final int gcd(int... a) { int gcd = 0; for(int i = 0; i < a.length; i ++) gcd = gcd(gcd, a[i]); return gcd; }
		final long gcd(long... a) { long gcd = 0; for(int i = 0; i < a.length; i ++) gcd = gcd(gcd, a[i]); return gcd; }


		final void fill(int[] a, int x) { Arrays.fill(a, x); }
		final void fill(long[] a, long x) { Arrays.fill(a, x); }
		final void fill(double[] a, double x) { Arrays.fill(a, x); }
		final void fill(char[] a, char x) { Arrays.fill(a, x); }
		final void fill(boolean[] a, boolean x) { Arrays.fill(a, x); }
		final void fill(int[][] a, int x) { for(int[] ele : a) fill(ele, x); }
		final void fill(long[][] a, long x) { for(long[] ele : a) fill(ele, x); }
		final void fill(double[][] a, double x) { for(double[] ele : a) fill(ele, x); }
		final void fill(char[][] a, char x) { for(char[] ele : a) fill(ele, x); }
		final void fill(boolean[][] a, boolean x) { for(boolean[] ele : a) fill(ele, x); }
		final void fill(int[][][] a, int x) { for(int[][] ele : a) fill(ele, x); }
		final void fill(long[][][] a, long x) { for(long[][] ele : a) fill(ele, x); }
		final void fill(double[][][] a, double x) { for(double[][] ele : a) fill(ele, x); }
		final void fill(char[][][] a, char x) { for(char[][] ele : a) fill(ele, x); }
		final void fill(boolean[][][] a, boolean x) { for(boolean[][] ele : a) fill(ele, x); }

		final int[] resize(int[] a, int m, int x) { nonNegativeCheck(m); int resized[] = new int[m]; for(int i = max(0, - x); i < a.length && i + x < m; i ++) resized[i + x] = a[i]; return resized; }
		final long[] resize(long[] a, int m, int x) { nonNegativeCheck(m); long resized[] = new long[m]; for(int i = max(0, - x); i < a.length && i + x < m; i ++) resized[i + x] = a[i]; return resized; }
		final double[] resize(double[] a, int m, int x) { nonNegativeCheck(m); double resized[] = new double[m]; for(int i = max(0, - x); i < a.length && i + x < m; i ++) resized[i + x] = a[i]; return resized; }
		final char[] resize(char[] a, int m, int x) { nonNegativeCheck(m); char resized[] = new char[m]; for(int i = max(0, - x); i < a.length && i + x < m; i ++) resized[i + x] = a[i]; return resized; }
		final boolean[] resize(boolean[] a, int m, int x) { nonNegativeCheck(m); boolean resized[] = new boolean[m]; for(int i = max(0, - x); i < a.length && i + x < m; i ++) resized[i + x] = a[i]; return resized; }
		final Object[] resize(Object[] a, int m, int x) { nonNegativeCheck(m); Object resized[] = new Object[m]; for(int i = max(0, - x); i < a.length && i + x < m; i ++) resized[i + x] = a[i]; return resized; }


public void solve() {
	int n = ni();
	boolean b[] = nb(n, 'R');
	long a[] = nl(n);
	for(int i = 0; i < n; i ++) if(!b[i]) a[i] *= -1;
	long s = 0;
	long max = 0;
	for(int i = 0; i < n; i ++) { s = max(s + a[i], a[i]); max = max(max, s); }
	s = 0;
	long min = 0;
	for(int i = 0; i < n; i ++) { s = min(s + a[i], a[i]); min = min(min, s); }
	prtln(max(max, abs(min)));
}

	}
}
0