0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ABC203 C - Friends and Travel costs

Posted at

abc203_1.png
abc203_2.png
abc203_3.png
abc203_4.png

約半年前、難解な問題文だと嘆き、悶絶しながらコードを書いていたころが懐かし良い。

abc203c.py
N,K = map(int,input().split())

#与えられる Ai,Bi は昇順になっていないケースがある
#以下で並び替え
lis = []
for i in range(N):
    a,b = map(int,input().split())
    lis.append([a,b])
lis.sort()

#ソートしたリストを小さい順から確認していく
plot = 0
ans = 0
for a,b in lis:
    target,charge = a,b
    #目的時と現在地が同じ場合は K だけチャージ
    if target == plot:
        K += charge
    else:
        #目的地と現在地の差が K 以下なら
        #給油ポイントに辿り着ける
        if target-plot <= K:
            K =K-(target-plot)+charge
            plot = target
        #たどり着けなかったら break
        else:
            break

#現在地の plot と残りの手持ち金額 K だけ進める
print(plot+K)
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?