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
args = input().split()
n, s = int(args[0]), int(args[1])

bricks = [None]*n
for i in range(n):
    args = input().split()
    a, b = int(args[0]), int(args[1])
    bricks[i] = (a,b)
bricks.sort(reverse=True)

#print(bricks)

maximal_value = 0

for i in range(2**n):
    combination = format(i, '#0%db' % (n+2))[2:]
    total = 0
    last_picked = None
    for k in range(len(combination)):
        if combination[k] == '1':
            if last_picked != None and bricks[k][0] == bricks[last_picked][0]:
                total = 0
                break
            total += bricks[k][0]
            if last_picked != None and bricks[k][1] != bricks[last_picked][1]:
                total -= s
            last_picked = k
    #print(total, "for", combination)
    if total > maximal_value: maximal_value = total

print(maximal_value)