from collections import defaultdict
n, k = map(int, input().split())
last_color = defaultdict(int)
prev_h = 0
score = [ 0 ]
best = [ 0 ]
ofs = 0
for i in range(1, n + 1):
h, c = map(int, input().split())
if h == prev_h:
ofs += 1
else:
ofs = 0
prev_h = h
prev = i - 1 - ofs
keep_color = score[last_color[c]] + h
switch_color = best[prev] + h - k
# duplicate box. should not really happen. just ignore this box / force color change.
if last_color[c] > prev:
keep_color = switch_color
else:
last_color[c] = i
# best if we include this box
score.append(max(keep_color, switch_color))
# best for this color
# best for all colors until this point
best.append(max(best[-1], score[-1]))
print(best[-1])
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | from collections import defaultdict n, k = map(int, input().split()) last_color = defaultdict(int) prev_h = 0 score = [ 0 ] best = [ 0 ] ofs = 0 for i in range(1, n + 1): h, c = map(int, input().split()) if h == prev_h: ofs += 1 else: ofs = 0 prev_h = h prev = i - 1 - ofs keep_color = score[last_color[c]] + h switch_color = best[prev] + h - k # duplicate box. should not really happen. just ignore this box / force color change. if last_color[c] > prev: keep_color = switch_color else: last_color[c] = i # best if we include this box score.append(max(keep_color, switch_color)) # best for this color # best for all colors until this point best.append(max(best[-1], score[-1])) print(best[-1]) |
English