結果

問題 No.2006 Decrease All to Zero
ユーザー chineristACchineristAC
提出日時 2022-02-23 03:03:06
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 3,137 ms / 4,000 ms
コード長 2,477 bytes
コンパイル時間 272 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 179,328 KB
最終ジャッジ日時 2023-12-23 04:09:41
合計ジャッジ時間 30,789 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
58,208 KB
testcase_01 AC 47 ms
58,212 KB
testcase_02 AC 329 ms
82,992 KB
testcase_03 AC 52 ms
63,500 KB
testcase_04 AC 48 ms
58,208 KB
testcase_05 AC 47 ms
58,212 KB
testcase_06 AC 47 ms
58,208 KB
testcase_07 AC 147 ms
78,180 KB
testcase_08 AC 69 ms
70,520 KB
testcase_09 AC 65 ms
68,444 KB
testcase_10 AC 2,977 ms
178,960 KB
testcase_11 AC 3,134 ms
179,328 KB
testcase_12 AC 2,888 ms
177,852 KB
testcase_13 AC 2,915 ms
178,460 KB
testcase_14 AC 3,072 ms
179,164 KB
testcase_15 AC 3,137 ms
179,056 KB
testcase_16 AC 1,326 ms
115,584 KB
testcase_17 AC 2,316 ms
155,472 KB
testcase_18 AC 47 ms
58,208 KB
testcase_19 AC 47 ms
58,208 KB
testcase_20 AC 47 ms
58,212 KB
testcase_21 AC 47 ms
58,208 KB
testcase_22 AC 48 ms
58,208 KB
testcase_23 AC 47 ms
58,208 KB
testcase_24 AC 46 ms
58,208 KB
testcase_25 AC 47 ms
58,212 KB
testcase_26 AC 2,778 ms
179,216 KB
testcase_27 AC 1,398 ms
178,244 KB
testcase_28 AC 1,403 ms
178,244 KB
testcase_29 AC 319 ms
81,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class DualSegmentTree:
    def __init__(self, n, segfunc, ide_ele):
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num

    def update(self,l,r,x):
        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                self.tree[l] = self.segfunc(self.tree[l],x)
                l += 1
            if r & 1:
                self.tree[r-1] = self.segfunc(self.tree[r-1],x)
            l >>= 1
            r >>= 1

    def __getitem__(self,idx):
        idx += self.num
        res = self.ide_ele
        while idx:
            res = self.segfunc(res,self.tree[idx])
            idx>>=1
        return res

import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import log,gcd

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

INF = 10**18

N = int(input())
X = li()
A = li()
M = max(A)

#assert M <= 200

if N==1:
    if A[0]==1:
        print(0)
    else:
        print(-1)
    exit()

dp = [[INF]*(M+3) for p in range(3)]
dp[0][A[0]] = (X[1]-X[0]) * (2 * A[0] - 2)
dp[1][A[0]] = (X[1]-X[0]) * (2 * A[0] - 1)
dp[2][A[0]] = (X[1]-X[0]) * (2 * A[0])

for i in range(1,N-1):
    #ndp = [[INF]*(M+3) for p in range(3)]
    ndp = [DualSegmentTree(M+3,min,INF) for p in range(3)]
    for j in range(1,M+3):
        #端におかない
        if j!=1:
            l = A[i]+j-2*min(A[i],j-1)
            r = min(M+2,A[i]+j)
            for p in range(3):
                ndp[p].update(l,r+1,dp[p][j]+(X[i+1]-X[i])*(p-2))
        
        #片端におく
        
        l = A[i]+j-2*min(A[i],j)+1
        r = min(M+2,A[i]+j)
        for p in range(2):
            ndp[p].update(l,r+1,dp[1][j]+(X[i+1]-X[i])*(p-2))
        for p in range(1,3):
            ndp[p].update(l,r+1,dp[2][j]+(X[i+1]-X[i])*(p-2))
        
        #p2,両端におく
        if 2 <= A[i]:
            l = A[i]+j-2*min(A[i],j+1)+2
            r = min(M+2,A[i]+j)
            for p in range(3):
                ndp[p].update(l,r+1,dp[2][j]+(X[i+1]-X[i])*(p-2))
        
    dp = [[ndp[p][j]+(X[i+1]-X[i])*2*j for j in range(M+3)] for p in range(3)]

res = 10**17
for p in range(3):
    if p <= A[-1]:
        res = min(res,dp[p][A[-1]-p+1])

if res==10**17:
    print(-1)
else:
    print(res)
        
        





0