1
0

More than 1 year has passed since last update.

ABC135 C - City Savers から学んだ

Last updated at Posted at 2021-09-21

abc135_1.png
abc135_2.png
abc135_3.png
abc135_4.png

うーん、なるほど
DP っぽいなー。。って書いたら
全然違った(笑)

CitySavers.py
N = int(input())

A = list(map(int,input().split()))
B = list(map(int,input().split()))

score = 0
marg = 0
for i in range(N):
    num = A[i]-marg
    if num > 0:
        score += marg
        A[i] -= marg
        if B[i] >= A[i]:
            score += A[i]
            marg = B[i]-A[i]
        else:
            score += B[i]
            marg = 0 # <= ここを忘れて WA (泣)
    else:
        score += A[i]
        marg = B[i]

if A[-1]-marg >= 0:
    score += marg
else:
    score += A[-1]
print(score)#123ms

for 文で、どこで抜けても
処理している引数は全て意図した
形式になるように意識が必要だと
再認識した。


再チャレンジ。
記述を少し最適化できて
かつ WA を食らわずに一発 OK!!
ちょっと時間をかけて慎重にやったから当然か。。

次回はサクッと出来るようになってることを祈る。

abc135c.py
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

sup = 0
cnt = 0
for i in range(N):
    a,b = A[i],B[i]
    if a <= b:
        cnt += a
        if A[i+1] >= b-a:
            A[i+1] -= b-a
            cnt += b-a
        else:
            cnt += A[i+1]
            A[i+1] = 0
    else:
        cnt += b
print(cnt)#119ms
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0