結果

問題 No.1433 Two color sequence
ユーザー terasaterasa
提出日時 2022-06-05 11:40:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 336 ms / 2,000 ms
コード長 2,347 bytes
コンパイル時間 1,171 ms
コンパイル使用メモリ 81,504 KB
実行使用メモリ 115,500 KB
最終ジャッジ日時 2023-10-21 03:09:32
合計ジャッジ時間 9,676 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,632 KB
testcase_01 AC 41 ms
55,632 KB
testcase_02 AC 40 ms
55,632 KB
testcase_03 AC 277 ms
113,124 KB
testcase_04 AC 264 ms
113,148 KB
testcase_05 AC 40 ms
55,644 KB
testcase_06 AC 41 ms
55,644 KB
testcase_07 AC 41 ms
55,644 KB
testcase_08 AC 261 ms
115,500 KB
testcase_09 AC 290 ms
113,884 KB
testcase_10 AC 303 ms
111,652 KB
testcase_11 AC 286 ms
113,272 KB
testcase_12 AC 302 ms
113,896 KB
testcase_13 AC 274 ms
113,892 KB
testcase_14 AC 299 ms
114,096 KB
testcase_15 AC 298 ms
114,100 KB
testcase_16 AC 307 ms
114,096 KB
testcase_17 AC 331 ms
114,092 KB
testcase_18 AC 314 ms
114,092 KB
testcase_19 AC 311 ms
114,100 KB
testcase_20 AC 317 ms
114,108 KB
testcase_21 AC 299 ms
114,100 KB
testcase_22 AC 299 ms
114,096 KB
testcase_23 AC 336 ms
114,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import pypyjit
import itertools
import heapq
import math
from collections import deque, defaultdict
import bisect

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
pypyjit.set_param('max_unroll_recursion=-1')


def index_lt(a, x):
    'return largest index s.t. A[i] < x or -1 if it does not exist'
    return bisect.bisect_left(a, x) - 1


def index_le(a, x):
    'return largest index s.t. A[i] <= x or -1 if it does not exist'
    return bisect.bisect_right(a, x) - 1


def index_gt(a, x):
    'return smallest index s.t. A[i] > x or len(a) if it does not exist'
    return bisect.bisect_right(a, x)


def index_ge(a, x):
    'return smallest index s.t. A[i] >= x or len(a) if it does not exist'
    return bisect.bisect_left(a, x)


class SegTree:
    def __init__(self, N, func, e):
        self.N = N
        self.func = func
        self.X = [e] * (N << 1)
        self.e = e

    def build(self, seq):
        for i in range(self.N):
            self.X[self.N + i] = seq[i]
        for i in range(self.N)[::-1]:
            self.X[i] = self.func(self.X[i << 1], self.X[i << 1 | 1])

    def add(self, i, x):
        i += self.N
        self.X[i] += x
        while i > 1:
            i >>= 1
            self.X[i] = self.func(self.X[i << 1], self.X[i << 1 | 1])

    def update(self, i, x):
        i += self.N
        self.X[i] = x
        while i > 1:
            i >>= 1
            self.X[i] = self.func(self.X[i << 1], self.X[i << 1 | 1])

    def query(self, L, R):
        L += self.N
        R += self.N
        vL = self.e
        vR = self.e
        while L < R:
            if L & 1:
                vL = self.func(vL, self.X[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.func(self.X[R], vR)
            L >>= 1
            R >>= 1
        return self.func(vL, vR)


N = int(input())
T = input()[:-1]
A = list(map(int, input().split()))
A = [A[i] if T[i] == 'R' else -A[i] for i in range(N)]

S = [0] * N
S[0] = A[0]
for i in range(1, N):
    S[i] = S[i - 1] + A[i]

INF = 1 << 30
stmin = SegTree(N, min, INF)
stmax = SegTree(N, max, -INF)
stmin.build(S)
stmax.build(S)

ans = -INF
for i in range(N):
    ma = stmax.query(i, N)
    mi = stmin.query(i, N)
    s = S[i - 1] if i > 0 else 0
    ans = max(ans, ma - s)
    ans = max(ans, -(mi - s))
print(ans)
0