Problem:


古本屋(Books)  

あなたの町にはJOI 古本店という老舗の古本屋があり，あなたはJOI 古本店をよく利用している．それぞれの本には基準価格が定まっており，JOI 古本店に行けばその価格で買い取ってもらえる．

JOI 古本店では，本を小説,漫画,雑誌など10 種類のジャンルに分類して扱っている．ジャンルには1から10 までの番号が付けられている．JOI 古本店には，同じジャンルの本をまとめて買い取ってもらうと高値で買い取ってくれるというサービスがある．具体的には，同じジャンルの本をまとめてT 冊買い取ってもらう場合，そのジャンルの本の一冊あたりの買取価格が基準価格よりT - 1 円高くなる．例えば，同じジャンルで基準価格100 円，120 円，150 円の本をまとめてJOI 古本店に売ったとすると，買取価格はそれぞれ102 円，122 円，152 円となる．

さて，あなたは一身上の都合で急遽引越しをすることになった．あなたはN 冊の本を持っているが，新しい住居にすべての本を持っていくことは困難なため，N 冊の本のうちK 冊をJOI 古本店に売ることにした．

課題

N 冊の本それぞれの基準価格とジャンルの番号が与えられるとき，合計買取価格の最大値を求めるプログラムを作成せよ．

制限

2 ≤ N ≤ 2000    あなたが持っている本の冊数

1 ≤ K < N    JOI 古本店に売る本の冊数

1 ≤ Ci ≤ 100000 = 105    i 番目の本の基準価格

1 ≤ Gi ≤ 10    i 番目の本のジャンルの番号

入力

標準入力から以下の入力を読み込め．

 1 行目には整数N, K が空白を区切りとして書かれており，あなたの持っている本の冊数がN で，そのうちK 冊をJOI 古本店に売ることを表す．

 続くN 行にはあなたの持っている本の情報が書かれている．i + 1 行目(1 ≤ i ≤ N) には，整数Ci,Giが空白を区切りとして書かれており，i 番目の本の基準価格がCi で，ジャンルの番号がGi であることを表す．

出力

標準出力に，合計買取価格の最大値を表す整数を1 行で出力せよ．

採点基準

採点用データのうち，

配点の20% 分については，N ≤ 20 を満たす．

配点の20% 分については，すべての本のジャンルは1 または2 である．

配点の10% 分については，これら2 つの条件の両方を満たす．

配点の30% 分については，これら2 つの条件の少なくとも一方を満たす．

入出力の例

入力例
  

7 4
14 1
13 2
12 3
14 2
8 2
16 3
11 2

出力例
  

60

この入力例では，2 番目，4 番目，6 番目，7 番目の4 冊の本を売ったとき，ジャンル2 の本の買取価格が1 冊あたり2 円高くなるので，これらの本の買取価格は以下のようになる．

 

  番号  
  基準価格  
  ジャンル  
  買取価格  
 

 

  

2
4
6
7

 
 

13
14
16
11

  

  

2
2
3
2

 
 

15
16
16
13

  
 

よって合計買取価格は15 + 16 + 16 + 13 = 60 円である．このとき合計買取価格は最大となる．

問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。



Sample input:
7 4
14 1
13 2
12 3
14 2
8 2
16 3
11 2


Sample output:
60

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

n, k = map(int, input().split())
books = [[] for _ in range(10)]

while n:
    c, g = map(int, input().split())
    books[g - 1].append(c)
    n -= 1

for q in books:
    q.sort(reverse=True)

books_acc = [[0] + list(accumulate(c + i * 2 for i, c in enumerate(q))) for q in books]


def memoize(f):
    memo = [[-1] * (k + 1) for _ in range(10)]

    def main(x, y):
        result = memo[x][y]
        if result < 0:
            result = memo[x][y] = f(x, y)
        return result

    return main


@memoize
def combi(g, remain):
    if g > 9:
        return 0
    salable = min(remain + 1, len(books[g]))
    book_acc = list(books_acc[g])
    return max([book_acc[i] + combi(g + 1, k - i) for i in range(salable)], default=0)


print(combi(0, k))

Submission status: Wrong Answer

Solution code:
def solve():
    N, K = map(int, input().split())
    a = [[] for _ in [0]*10]
    l = []
    for c, g in (tuple(map(int, input().split())) for _ in [0]*N):
        a[g-1].append(c)

    for i, prices in enumerate(a):
        ln = len(prices)
        if ln == 0:
            continue
        dp = [0]*(K+1)
        for j, price in enumerate(prices):
            _k = j if j < K-1 else K-1
            for k, prev, cur in zip(range(_k, -1, -1), dp[_k::-1], dp[_k+1::-1]):
                if prev+price+k*2 > cur:
                    dp[k+1] = prev+price+k*2
        l.append(dp[1:ln+1])

    dp = [0]*(K+1)
    for prices in l:
        for i, price in enumerate(prices, start=1):
            for j, cur, prev in zip(range(K, -1, -1), dp[::-1], dp[K-i::-1]):
                if prev+price > cur:
                    dp[j] = prev+price

    print(max(dp))


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
def solve():
  n, k = map(int,input().split())
  group_num = 10
  
  book_map = [[] for i in range(group_num)]
  
  for i in range(n):
    c,g = map(int,input().split())
    book_map[g - 1].append(c)
  for i in range(g):
    book_map[i].sort(reverse=True)
    for j in range(len(book_map[i])):
      book_map[i][j] += j * 2
  
  all_books = []
  for mp in book_map:
    all_books.extend(mp)
  all_books.sort(reverse=True)
  
  print(sum(all_books[:k]))
solve()

Submission status: Wrong Answer

Solution code:
def solve():
  n, k = map(int,input().split())
  group_num = 10
  
  book_map = [[] for i in range(group_num)]
  acc_map = [[0] for i in range(group_num)]

  for i in range(n):
    c,g = map(int,input().split())
    book_map[g - 1].append(c)

  for i in range(group_num):
    book_map[i].sort(reverse=True)
    acc = 0
    for j in range(len(book_map[i])):
      book_map[i][j] += j * 2
      acc += book_map[i][j]
      acc_map[i].append(acc)
  print(acc_map)  
  dp = [[0] * (k + 1) for i in range(group_num + 1)]
  for y in range(1,k + 1):
    for x in range(1,group_num + 1):
      for z in range(min(y + 1, len(acc_map[x - 1]))):
        dp[x][y] = max(dp[x][y], dp[x - 1][y - z] + acc_map[x - 1][z])
  
#  print(dp[group_num][k])
solve()

Submission status: Wrong Answer

Solution code:
import sys

def solve():
    file_input = sys.stdin
    N, K = map(int, file_input.readline().split())
    
    G = [[] for i in range(10)]
    for line in file_input:
        c, g = map(int, line.split())
        G[g - 1].append(c)
    
    for genre in G:
        genre.sort(reverse=True)
        for p, i in zip(genre, range(1, len(genre))):
            genre[i] += p + 2 * i # Recording cumulative price
    
    C = [0] * (K + 1)
    for genre in G:
        for n, p in enumerate(genre, start=1):
            for i, vals in enumerate(zip(C[n:], C)):
                v1, v2 = vals
                v1 += p
                if v1 > v2:
                    C[i] = v1
    print(C[0])

solve()

Submission status: Accepted

Solution code:
from itertools import accumulate

n, k = map(int, input().split())
books = [[] for _ in range(10)]

while n:
    c, g = map(int, input().split())
    books[g - 1].append(c)
    n -= 1

books_acc = [[0] + list(accumulate(c + i * 2 for i, c in enumerate(sorted(q, reverse=True)))) for q in books]


def memoize(f):
    memo = [[-1] * (k + 1) for _ in range(10)]

    def main(x, y):
        if x > 9:
            return 0
        result = memo[x][y]
        if result < 0:
            result = memo[x][y] = f(x, y)
        return result

    return main


@memoize
def combi(g, remain):
    book_acc = list(books_acc[g])
    salable = min(remain + 1, len(book_acc))
    return max([book_acc[i] + combi(g + 1, remain - i) for i in range(salable)], default=0)


print(combi(0, k))

Submission status: Accepted

Solution code:
n,k=map(int,raw_input().split())
g=[[] for _ in xrange(10)]
for i in xrange(n):
    v,j=map(int,raw_input().split())
    g[j-1].append(v)
for i in xrange(10):
    g[i].sort()
    g[i].reverse()

books=[[0]*2005 for _ in xrange(10)]
for i in xrange(10):
    for j in xrange(1,len(g[i])+1):
        books[i][j]=books[i][j-1]+g[i][j-1]+2*(j-1)

dp=[[0]*(k+1) for _ in xrange(11)]
for i in xrange(10):
    for j in xrange(1,k+1):
        for l in xrange(j+1):
            dp[i+1][j]=max(dp[i+1][j],dp[i][j-l]+books[i][l])

print(dp[10][k])

Submission status: Accepted

Solution code:
def solve():
    N, K = map(int, input().split())
    a = [[] for _ in [0]*10]
    l = []
    for c, g in (tuple(map(int, input().split())) for _ in [0]*N):
        a[g-1].append(c)

    for i, prices in enumerate(a):
        ln = len(prices)
        if ln == 0:
            continue
        dp = [0]*(K+1)
        for j, price in enumerate(prices):
            _k = j if j < K-1 else K-1
            for k, prev, cur in zip(range(_k, -1, -1), dp[_k::-1], dp[_k+1::-1]):
                if prev+price+k*2 > cur:
                    dp[k+1] = prev+price+k*2
        l.append(dp[1:ln+1])

    dp = [float("-inf")]*(K+1)
    dp[0] = 0
    for prices in l:
        cdp = dp[:]
        for i, price in enumerate(prices, start=1):
            if i > K:
                break
            for j, cur, prev in zip(range(K, -1, -1), cdp[::-1], dp[K-i::-1]):
                if 0 < prev+price > cur:
                    cdp[j] = prev+price
        dp = cdp

    print(max(dp))


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
[N,K]=map(int, raw_input().split())
C=[[] for i in range(11)]
D=[[0 for j in range(N+1)] for i in range(11)]
M=[0 for i in range(11)]

for i in range(N):
    c,g=map(int, raw_input().split())
    C[g].append(c)
    M[g]+=1
for i in range(1,11):
    y=sorted(C[i], reverse=True)
    for j in range(len(y)):
        n = j+1
        D[i][n] =y[j] + D[i][n-1] + 2*(n-1)

DP=[[0 for i in range(K+1)] for j in range(11)]

for g in range(1,11):
    for k in range(K+1):
        DP[g][k] = max( [ DP[g-1][k-x] + D[g][x] for x in range(min( k+1, M[g]+1 ) ) ] + [0] )
print DP[10][K]

Submission status: Accepted

Solution code:
def solve():
  n, k = map(int,input().split())
  group_num = 10
  
  book_map = [[] for i in range(group_num)]
  acc_map = [[0] for i in range(group_num)]

  for i in range(n):
    c,g = map(int,input().split())
    book_map[g - 1].append(c)

  for i in range(group_num):
    book_map[i].sort(reverse=True)
    acc = 0
    for j in range(len(book_map[i])):
      book_map[i][j] += j * 2
      acc += book_map[i][j]
      acc_map[i].append(acc)
#  print(acc_map)  
  dp = [[0] * (k + 1) for i in range(group_num + 1)]
  for y in range(1,k + 1):
    for x in range(1,group_num + 1):
      for z in range(min(y + 1, len(acc_map[x - 1]))):
        dp[x][y] = max(dp[x][y], dp[x - 1][y - z] + acc_map[x - 1][z])
  
  print(dp[group_num][k])
solve()

Submission status: Accepted

Solution code:
def solve():
  n, k = map(int,input().split())
  group_num = 10
  
  book_map = [[] for i in range(group_num)]
  acc_map = [[0] for i in range(group_num)]

  for i in range(n):
    c,g = map(int,input().split())
    book_map[g - 1].append(c)

  for i in range(group_num):
    book_map[i].sort(reverse=True)
    acc = 0
    bmi = book_map[i]
    append = acc_map[i].append
    for j in range(len(bmi)):
      acc += (bmi[j] + j * 2)
      append(acc)
  
  dp = [[0] * (k + 1) for i in range(group_num + 1)]
  for y in range(1,k + 1):
    for x in range(1,group_num + 1):
      mp = acc_map[x - 1]
      be = dp[x - 1]
      for z in range(min(y + 1, len(mp))):
        v1 = be[y - z]
        v2 = mp[z]
        if dp[x][y] < v1 + v2:
          dp[x][y] = v1 + v2
  
  print(dp[group_num][k])
solve()

Submission status: Accepted

Solution code:
from heapq import heappush as push
from heapq import heappop as pop
def solve():
  n, k = map(int,input().split())
  group_num = 10
  
  book_map = [[] for i in range(group_num)]
  acc_map = [[0] for i in range(group_num)]

  for i in range(n):
    c,g = map(int,input().split())
    push(book_map[g - 1], -c)

  for i in range(group_num):
    bmi = book_map[i]
    append = acc_map[i].append
    acc = 0
    for j in range(len(bmi)):
      acc += (pop(bmi) - j * 2)
      append(acc)
  
  dp = [[0] * (k + 1) for i in range(group_num + 1)]
  for y in range(1,k + 1):
    for x in range(1,group_num + 1):
      mp = acc_map[x - 1]
      be = dp[x - 1]
      for z in range(min(y + 1, len(mp))):
        v1 = be[y - z]
        v2 = mp[z]
        if dp[x][y] > v1 + v2:
          dp[x][y] = v1 + v2
  
  print(-dp[group_num][k])
solve()

Submission status: Accepted

Solution code:
def solve():
  n, k = map(int,input().split())
  group_num = 10
  
  book_map = [[] for i in range(group_num)]
  acc_map = [[0] for i in range(group_num)]

  for i in range(n):
    c,g = map(int,input().split())
    book_map[g - 1].append(c)

  for i in range(group_num):
    bmi = book_map[i]
    bmi.sort(reverse=True)
    append = acc_map[i].append
    acc = 0
    for j in range(len(bmi)):
      acc += (bmi[j] + j * 2)
      append(acc)
  
  dp = [[0] * (k + 1) for i in range(group_num + 1)]
  for y in range(1,k + 1):
    for x in range(1,group_num + 1):
      mp = acc_map[x - 1]
      be = dp[x - 1]
      dp[x][y] = max([be[y - z] + mp[z] for z in range(min(y + 1, len(mp)))])
  
  print(dp[group_num][k])
solve()

Submission status: Accepted

Solution code:
def solve():
  n, k = map(int,input().split())
  group_num = 10
  
  book_map = [[] for i in range(group_num)]
  acc_map = [[0] for i in range(group_num)]

  for i in range(n):
    c, g = map(int,input().split())
    book_map[g - 1].append(c)

  for i in range(group_num):
    bmi = book_map[i]
    bmi.sort(reverse=True)
    ami = acc_map[i]
    acc = 0
    for j in range(len(bmi)):
      acc += (bmi[j] + j * 2)
      ami.append(acc)
  
  dp = [[0] * (k + 1) for i in range(group_num + 1)]
  for y in range(1, k + 1):
    for x in range(1, group_num + 1):
      accs = acc_map[x - 1]
      dp_pre = dp[x - 1]
      dp[x][y] = max([dp_pre[y - z] + accs[z] for z in range(min(y + 1, len(accs)))])
  
  print(dp[group_num][k])
solve()

Submission status: Accepted

Solution code:
def solve():
  n, k = map(int,input().split())
  group_num = 10
  
  book_map = [[] for i in range(group_num)]
  acc_map = []

  for i in range(n):
    c, g = map(int,input().split())
    book_map[g - 1].append(c)

  for i in range(group_num):
    bmi = book_map[i]
    bmi.sort(reverse=True)
    ami = [0 for i in range(len(bmi) + 1)]
    for j in range(len(bmi)):
      ami[j + 1] = ami[j] + bmi[j] + j * 2
    acc_map.append(ami)
  
  dp = [[0] * (k + 1) for i in range(group_num + 1)]
  for y in range(1, k + 1):
    for x in range(1, group_num + 1):
      accs = acc_map[x - 1]
      dp_pre = dp[x - 1]
      dp[x][y] = max([dp_pre[y - z] + accs[z] for z in range(min(y + 1, len(accs)))])
  
  print(dp[group_num][k])
solve()

Submission status: Accepted

Solution code:
import sys

def solve():
    file_input = sys.stdin
    N, K = map(int, file_input.readline().split())
    
    G = [[] for i in range(10)]
    for line in file_input:
        c, g = map(int, line.split())
        G[g - 1].append(c)
    
    for genre in G:
        genre.sort(reverse=True)
        for p, i in zip(genre, range(1, len(genre))):
            genre[i] += p + 2 * i # Recording cumulative price
    
    C = [0] * (K + 1)
    for genre in G:
        pre_C = C.copy()
        for n, p in enumerate(genre, start=1):
            for i, vals in enumerate(zip(pre_C[n:], C)):
                v1, v2 = vals
                v1 += p
                if v1 > v2:
                    C[i] = v1
    print(C[0])

solve()

Submission status: Accepted

Solution code:
import sys

def solve():
    file_input = sys.stdin
    N, K = map(int, file_input.readline().split())
    
    G = [[] for i in range(10)]
    for line in file_input:
        c, g = map(int, line.split())
        G[g - 1].append(c)
    
    for genre in G:
        genre.sort(reverse=True)
        for i, p in zip(range(1, len(genre)), genre):
            genre[i] += p + 2 * i # Recording cumulative price
    
    C = [0] * (K + 1)
    for genre in G:
        pre_C = C.copy()
        for n, p in zip(range(len(genre), 0, -1), genre[::-1]):
            for i, vals in enumerate(zip(pre_C[n:], C)):
                v1, v2 = vals
                v1 += p
                if v1 > v2:
                    C[i] = v1
    print(C[0])

solve()

Submission status: Accepted

Solution code:
def main():
    import sys
    input=sys.stdin.readline
    n,k=map(int,input().split())
    ab=[list(map(int,input().split())) for _ in [0]*n]
    g=[[] for _ in [0]*10]
    [g[b-1].append(a) for a,b in ab]
    [g[c].sort(reverse=True) for c in range(10)]
    for c in range(10):g[c]=[0]+g[c]
    for c in range(10):
        for i in range(2,len(g[c])):
            g[c][i]+=g[c][i-1]+2*(i-1)
    dp=[0]*(k+1)
    for c in range(10):
        dp2=[0]*(k+1)
        for i in range(len(g[c])):
            for j in range(k+1-i):
                dp2[i+j]=max(dp2[i+j],dp[j]+g[c][i])
        dp=dp2
    print(max(dp))
if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import deque
from itertools import chain, combinations

n, k = map(int, input().split())
books = [[] for _ in range(10)]

while n:
    c, g = map(int, input().split())
    books[g - 1].append(c)
    n -= 1

for q in books:
    q.sort(reverse=True)

done = set()
max_cost = 0

for book_combi in combinations(chain.from_iterable([[i] * min(k, len(q)) for i, q in enumerate(books) if q]), k):
    if book_combi in done:
        continue
    done.add(book_combi)

    cost = 0
    counts = [0] * 10
    for i in book_combi:
        cost += books[i][counts[i]] + counts[i] * 2
        counts[i] += 1

    if cost > max_cost:
        max_cost = cost

print(max_cost)

Submission status: Runtime Error

Solution code:
n,k=map(int,raw_input().split())
g=[[] for _ in xrange(10)]
for i in xrange(n):
    v,j=map(int,raw_input().split())
    g[j-1].append(v)
for i in xrange(10):
    g[i].sort()
    g[i].reverse()

books=[[0]*(k+1) for _ in xrange(10)]
for i in xrange(10):
    for j in xrange(1,len(g[i])+1):
        books[i][j]=books[i][j-1]+g[i][j-1]+2*(j-1)

dp=[[0]*(k+1) for _ in xrange(11)]
for i in xrange(10):
    for j in xrange(1,k+1):
        for l in xrange(j+1):
            dp[i+1][j]=max(dp[i+1][j],dp[i][j-l]+books[i][l])

print(dp[10][k])

Submission status: Runtime Error

Solution code:
def solve():
    N, K = map(int, input().split())
    a = [[] for _ in [0]*10]
    l = []
    for c, g in (tuple(map(int, input().split())) for _ in [0]*N):
        a[g-1].append(c)

    for i, prices in enumerate(a):
        ln = len(prices)
        if ln == 0:
            continue
        dp = [[-1]*(K+1) for _ in [0]*(ln+1)]
        dp[0][0] = 0
        for j, price in enumerate(prices):
            for k, prev in enumerate(dp[j][:j+1]):
                if prev > dp[j+1][k]:
                    dp[j+1][k] = prev
                if prev+price+k*2 > dp[j+1][k+1]:
                    dp[j+1][k+1] = prev+price+k*2
        l.append(dp[-1][1:ln+1])

    dp = [0]*(K+1)
    for prices in l:
        for i, price in enumerate(prices, start=1):
            for j, cur, prev in zip(range(K, -1, -1), dp[::-1], dp[K-i::-1]):
                if prev+price > cur:
                    dp[j] = prev+price

    print(max(dp))
    

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
[N,K]=map(int, raw_input().split())
C=[[] for i in range(11)]
D=[[0 for j in range(K+1)] for i in range(11)]
M=[0 for i in range(11)]

for i in range(N):
    c,g=map(int, raw_input().split())
    C[g].append(c)
    M[g]+=1
for i in range(1,11):
    y=sorted(C[i], reverse=True)
    for j in range(len(y)):
        n = j+1
        D[i][n] =y[j] + D[i][n-1] + 2*(n-1)

DP=[[0 for i in range(K+1)] for j in range(11)]

for g in range(1,11):
    for k in range(K+1):
        DP[g][k] = max( [ DP[g-1][k-x] + D[g][x] for x in range(min( k+1, M[g]+1 ) ) ] + [0] )
print DP[10][K]