#!/usr/bin/env python3 import sys from dataclasses import dataclass def main(): N, C = [int(x) for x in input().split()] blocks = [] all_w = {} for i in range(N): row = [int(x) for x in input().split()] blocks.append(row) all_w[row[1]] = True blocks.sort(reverse=True) blocks_count = len(blocks) w_count = len(all_w) result = 0 @dataclass class Ctx: score: int curr_a: int curr_w: int def f(at, ctx): nonlocal result if result < ctx.score: result = ctx.score used_w = {} for i in range(at, blocks_count): a, w = blocks[i] if ctx.curr_a != a and w not in used_w: used_w[w] = True score = ctx.score + a if ctx.curr_w != w and ctx.curr_w != -1: score -= C f(i + 1, Ctx(score, a, w)) if len(used_w) == w_count: break f(0, Ctx(0, -1, -1)) print(result) if __name__ == "__main__": if len(sys.argv) == 2: sys.stdin = open(sys.argv[1]) main()
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | #!/usr/bin/env python3 import sys from dataclasses import dataclass def main(): N, C = [int(x) for x in input().split()] blocks = [] all_w = {} for i in range(N): row = [int(x) for x in input().split()] blocks.append(row) all_w[row[1]] = True blocks.sort(reverse=True) blocks_count = len(blocks) w_count = len(all_w) result = 0 @dataclass class Ctx: score: int curr_a: int curr_w: int def f(at, ctx): nonlocal result if result < ctx.score: result = ctx.score used_w = {} for i in range(at, blocks_count): a, w = blocks[i] if ctx.curr_a != a and w not in used_w: used_w[w] = True score = ctx.score + a if ctx.curr_w != w and ctx.curr_w != -1: score -= C f(i + 1, Ctx(score, a, w)) if len(used_w) == w_count: break f(0, Ctx(0, -1, -1)) print(result) if __name__ == "__main__": if len(sys.argv) == 2: sys.stdin = open(sys.argv[1]) main() |