ABC067 をPythonで解く

atcoder.jp

A~Cを解いた。

A

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

if A%3==0 or B%3==0 or (A+B)%3==0:
    ans = "Possible"
else:
    ans ="Impossible"
print(ans)

 

B

N,K = map(int,input().split())
L =[int(i) for i in input().split()]
L.sort(reverse=True)
ans = sum(L[:K])
print(ans)

C

累積和問題。
内包表記内にsum(A)を書いていると毎回計算させることとなりTLEしてしまう。 total変数に格納して演算を一度にする。これだけで処理時間が8分の1になる。

from itertools import accumulate
N = int(input())
A = [int(i) for i in input().split()]
tmp = list(accumulate(A))[:len(A)-1]
total  = sum(A)
ans = min([abs(total-2*a) for a in tmp])
print(ans)