Problem:


Score : 500 points

Problem Statement

Takahashi is at an all-you-can-eat restaurant.

The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th dish, whose deliciousness is B_i.

The restaurant has the following rules:

You can only order one dish at a time. The dish ordered will be immediately served and ready to eat.

You cannot order the same kind of dish more than once.

Until you finish eating the dish already served, you cannot order a new dish.

After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served.

Let Takahashi's happiness be the sum of the deliciousness of the dishes he eats in this restaurant.

What is the maximum possible happiness achieved by making optimal choices?

Constraints

2 \leq N \leq 3000

1 \leq T \leq 3000

1 \leq A_i \leq 3000

1 \leq B_i \leq 3000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N T
A_1 B_1
:
A_N B_N

Output

Print the maximum possible happiness Takahashi can achieve.

Sample Input 1

2 60
10 10
100 100

Sample Output 1

110

By ordering the first and second dishes in this order, Takahashi's happiness will be 110.

Note that, if we manage to order a dish in time, we can spend any amount of time to eat it.

Sample Input 2

3 60
10 10
10 20
10 30

Sample Output 2

60

Takahashi can eat all the dishes within 60 minutes.

Sample Input 3

3 60
30 10
30 20
30 30

Sample Output 3

50

By ordering the second and third dishes in this order, Takahashi's happiness will be 50.

We cannot order three dishes, in whatever order we place them.

Sample Input 4

10 100
15 23
20 18
13 17
24 12
18 29
19 27
23 21
18 20
27 15
22 25

Sample Output 4

145



Sample input:
2 60
10 10
100 100


Sample output:
110

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


def input():
    return stdin.readline()[:-1]


def intput():
    return int(input())


def sinput():
    return input().split()


def intsput():
    return map(int, sinput())


dishes = []
n, t = intsput()
for _ in range(n):
    dishes.append(list(intsput()))
    dishes[-1].append(dishes[-1][1] / dishes[-1][0])

maxweight = max([x[0] for x in dishes])

dp = [[[0, []]] * (t + 1) for _ in range(n + 1)]

for i in range(1, n + 1):
    for j in range(t):
        if dishes[i - 1][0] > j:
            dp[i][j] = dp[i - 1][j][:]
        else:
            val = max(dp[i - 1][j][0], dp[i - 1][j - dishes[i - 1][0]][0] + dishes[i - 1][1])
            dp[i][j] = [val]
            if val == dp[i - 1][j][0]:
                dp[i][j].append(dp[i - 1][j][1])
            else:
                dp[i][j].append(dp[i - 1][j - dishes[i - 1][0]][1] + [i])

rslt = dp[n][t - 1]

op = []
for i in range(1, n + 1):
    if i not in rslt[1]:
        op.append(i)

if op:
    vv = max([dishes[i - 1][1] for i in op])
else:
    vv = 0

print(rslt[0] + vv)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, t = map(int, input().split())
    f = [list(map(int, input().split())) for _ in range(n)]
    dp = [0 for i in range(t)]
    ls = [[] for i in range(t)]
    for i in range(n):
        for j in range(t - 1, -1, -1):
            if j - f[i][0] >= 0 and dp[j - f[i][0]] + f[i][1] > dp[j]:
                dp[j] = dp[j - f[i][0]] + f[i][1]
                ls[j] = ls[j - f[i][0]][:]
                ls[j].append(i)
    res = dp[t - 1]
    ll = ls[t - 1]
    fff = [0 for _ in range(n)]
    for sll in ll:
        fff[sll] = 1
    ff = [f[i][1] for i in range(n) if fff[i] == 0]
    if ff:
        return res + max(ff)
    else:
        return res


print(main())

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
N, T = list(map(int, input().split()))
ab_list = []
weight_org = []
value_org = []
for _ in range(N):
    a, b = list(map(int, input().split()))
    # ab_list.append((a, b))
    weight_org.append(a)
    value_org.append(b)

#  DPテーブル
dp = [[0] * (T - 1) for _ in range(N + 1)]
dp_3 = []

for i in range(N):
    weight = weight_org[:]
    value = value_org[:]
    w_last = weight.pop(i)
    v_last = value.pop(i)
    for i in range(N - 1):
        for w in range((T - 1)):
            if w >= weight[i]:
                dp[i + 1][w] = max(dp[i][w - weight[i]] + value[i], dp[i][w])
            else:
                dp[i + 1][w] = dp[i][w]
    dp_3.append(dp[N - 1][T - 2] + v_last)
ans = max(dp_3)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def examE():
    N, T = LI()
    A, B = [0] * N, [0] * N
    for i in range(N):
        A[i], B[i] = LI()
    dp = [[[0,0] for _ in range(T+ 1)] for _ in range(N + 1)]
    for i in range(N):
        for j in range(A[i],T+1):
            dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j - A[i]][0] + B[i])
            dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j - A[i]][1] + B[i])
        for j in range(1,T+1):
            dp[i+1][j][1] = max(dp[i+1][j][1], dp[i][j-1][0] + B[i])
            dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][0])
            dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][1])
#    print(dp[N][T])
    ans = max(dp[N][T])
    print(ans)

import sys,copy,bisect,itertools
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examE()

Submission status: Time Limit Exceeded

Solution code:
def knapsack(N,W,weight,value):
    dp=[[0 for i in range(W+1)] for j in range(N+1)]
    F=[[[] for i in range(W+1)] for j in range(N+1)]
    for i in range(N):
        for w in range(W+1):
            if weight[i]<=w:
                if dp[i][w-weight[i]]+value[i] <= dp[i][w]:
                    dp[i+1][w]=dp[i][w]
                    F[i+1][w] =F[i][w]
                else:
                    dp[i+1][w] = dp[i][w-weight[i]]+value[i]
                    F[i+1][w] = F[i][w-weight[i]] + [i]
            else:
                dp[i+1][w]=dp[i][w]
                F[i+1][w] =F[i][w]
    return dp[N][W],F[N][W]

if __name__ == "__main__":
    N,T = map(int,input().split())
    D = []
    E = []
    for i in range(N):
        A,B = map(int,input().split())
        D.append(A)
        E.append(B)
    ans,F = knapsack(N,T-1,D,E)
    max = 0
    for i in range(N):
        if i in F:
            continue
        if max <= E[i]:
            max = E[i]
    print(ans+max)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N, T = map(int, input().split())
    foods = [list(map(int, input().split())) for _ in range(N)]
    ans = 0
    for i, food in enumerate(foods):
        now_foods = foods[:i] + foods[i+1:]
        dp = [[0] * T for _ in range(N)]
        for j, n_food in enumerate(now_foods):
            n_a, n_b = n_food
            for k in range(T):
                if k < n_a:
                    dp[j+1][k] = dp[j][k]
                else:
                    dp[j+1][k] = max(dp[j][k], dp[j][k-n_a]+n_b)
        #print(dp[j+1])
        ans = max(ans, dp[-1][-1]+food[1])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

N, T = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort(key=lambda x: x[1], reverse=True)

def dfs(i, sum, time, remain):
    global answer
    if i == N:
        if time < T and len(remain) >= 1:
            sum += AB[min(remain)][1]
        if sum > answer:
            answer = sum
    else:
        dfs(i+1, sum, time, remain)
        if time < T:
            dfs(i+1, sum + AB[i][1], time + AB[i][0], remain-{i})

answer = 0
dfs(0, 0, 0, set(range(N)))
print(answer)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


def input():
    return stdin.readline()[:-1]


def intput():
    return int(input())


def sinput():
    return input().split()


def intsput():
    return map(int, sinput())


dishes = []
n, t = intsput()
for _ in range(n):
    dishes.append(list(intsput()))
    dishes[-1].append(dishes[-1][1] / dishes[-1][0])

maxweight = max([x[0] for x in dishes])

dp = [[[0, []]] * 6001 for _ in range(6001)]

for i in range(1, n + 1):
    for j in range(t):
        if dishes[i - 1][0] > j:
            dp[i][j] = dp[i - 1][j][:]
        else:
            val = max(dp[i - 1][j][0], dp[i - 1][j - dishes[i - 1][0]][0] + dishes[i - 1][1])
            dp[i][j] = [val]
            if val == dp[i - 1][j][0]:
                dp[i][j].append(dp[i - 1][j][1])
            else:
                dp[i][j].append(dp[i - 1][j - dishes[i - 1][0]][1] + [i])

rslt = dp[n][t - 1]

op = []
for i in range(1, n + 1):
    if i not in rslt[1]:
        op.append(i)

if op:
    vv = max([dishes[i - 1][1] for i in op])
else:
    vv = 0

print(rslt[0] + vv)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, t = map(int, input().split())
    f = [list(map(int, input().split())) for _ in range(n)]
    dp = [0 for i in range(t)]
    ls = [[] for i in range(t)]
    for i in range(n):
        for j in range(t - 1, -1, -1):
            if j - f[i][0] >= 0 and dp[j - f[i][0]] + f[i][1] > dp[j]:
                dp[j] = dp[j - f[i][0]] + f[i][1]
                ls[j] = ls[j - f[i][0]] + [i]
    res = dp[t - 1]
    ll = ls[t - 1]
    fff = [0 for _ in range(n)]
    for sll in ll:
        fff[sll] = 1
    ff = [f[i][1] for i in range(n) if fff[i] == 0]
    if ff:
        return res + max(ff)
    else:
        return res


print(main())

Submission status: Time Limit Exceeded

Solution code:
n,w=map(int,input().split())
ab=[]
w-=1
take=[]
dp=[[0 for _ in range(w+1)] for _ in range(n+1)]
for i in range(n):
	a,b=map(int,input().split())
	ab.append([a,b])
	for j in range(w):
		if j+1-a>=0:
			dp[i+1][j+1]=max(dp[i][j+1],dp[i][j+1-a]+b)
		else:
			dp[i+1][j+1]=dp[i][j+1]
k=w
take=[]
for i in range(n):
	if dp[n-i-1][w]!=dp[n-i][w]:
		w-=ab[n-i-1][0]
		take.append(n-i-1)
take=set(take)
ans=0
for i in range(n):
	if i not in take:
		ans=max(ans,ab[i][1])
print(dp[-1][-1]+ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

N, T = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort(key=lambda x: x[1])

def dfs(i, sum, time, remain):
    global answer
    if i == N:
        if time < T and len(remain) >= 1:
            sum += AB[max(remain)][1]
        if sum > answer:
            answer = sum
    else:
        dfs(i+1, sum, time, remain)
        if time < T:
            dfs(i+1, sum + AB[i][1], time + AB[i][0], remain-{i})

answer = 0
dfs(0, 0, 0, set(range(N)))
print(answer)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


def input():
    return stdin.readline()[:-1]


def intput():
    return int(input())


def sinput():
    return input().split()


def intsput():
    return map(int, sinput())


dishes = []
n, t = intsput()
for _ in range(n):
    dishes.append(list(intsput()))
    dishes[-1].append(dishes[-1][1] / dishes[-1][0])

maxweight = max([x[0] for x in dishes])

dp = [[[0, []]] * 6001 for _ in range(6001)]

for i in range(1, n + 1):
    for j in range(t + maxweight):
        if dishes[i - 1][0] > j:
            dp[i][j] = dp[i - 1][j][:]
        else:
            val = max(dp[i - 1][j][0], dp[i - 1][j - dishes[i - 1][0]][0] + dishes[i - 1][1])
            dp[i][j] = [val]
            if val == dp[i - 1][j][0]:
                dp[i][j].append(dp[i - 1][j][1])
            else:
                dp[i][j].append(dp[i - 1][j - dishes[i - 1][0]][1] + [i])

rslt = dp[n][t - 1]

op = []
for i in range(1, n + 1):
    if i not in rslt[1]:
        op.append(i)

if op:
    vv = max([dishes[i - 1][1] for i in op])
else:
    vv = 0

print(rslt[0] + vv)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, t = map(int, input().split())
    f = [list(map(int, input().split())) for _ in range(n)]
    dp = [0 for i in range(t)]
    ls = [[] for i in range(t)]
    for i in range(n):
        for j in range(t - 1, -1, -1):
            if j - f[i][0] >= 0 and dp[j - f[i][0]] + f[i][1] > dp[j]:
                dp[j] = dp[j - f[i][0]] + f[i][1]
                ls[j] = ls[j - f[i][0]] + [i]
    res = dp[t - 1]
    ff = [f[i][1] for i in range(n) if not i in ls[t - 1]]
    if ff:
        return res + max(ff)
    else:
        return res


print(main())

Submission status: Time Limit Exceeded

Solution code:
def knapsack(N,W,weight,value):
    inf=float("inf")
    dp=[[-inf for i in range(W+1)] for j in range(N+1)]
    F=[[[] for i in range(W+1)] for j in range(N+1)]
    for i in range(W+1): dp[0][i]=0
    for i in range(N):
        for w in range(W+1):
            if weight[i]<=w:
                if dp[i][w-weight[i]]+value[i] <= dp[i][w]:
                    dp[i+1][w]=dp[i][w]
                    F[i+1][w] =F[i][w]
                else:
                    dp[i+1][w] = dp[i][w-weight[i]]+value[i]
                    F[i+1][w] = F[i][w-weight[i]] + [i]
            else:
                dp[i+1][w]=dp[i][w]
                F[i+1][w] =F[i][w]
    return dp[N][W],F[N][W]

if __name__ == "__main__":
    N,T = map(int,input().split())
    D = []
    E = []
    for i in range(N):
        A,B = map(int,input().split())
        D.append(A)
        E.append(B)
    ans,F = knapsack(N,T-1,D,E)
    max = 0
    for i in range(N):
        if i in F:
            continue
        if max <= E[i]:
            max = E[i]
    print(ans+max)

Submission status: Time Limit Exceeded

Solution code:
def examE():
    N, T = LI()
    A, B = [0] * N, [0] * N
    for i in range(N):
        A[i], B[i] = map(int, input().split())
    maxT = max(A)
    dp = [[[0 for _ in range(2)] for _ in range(T+ 1)] for _ in range(N + 1)]
    for i in range(N):
        for j in range(A[i],T+1):
            dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j - A[i]][0] + B[i])
            dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j - A[i]][1] + B[i])
        for j in range(1,T+1):
            dp[i+1][j][1] = max(dp[i+1][j][1], dp[i][j-1][0] + B[i])
            dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][0])
            dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][1])
#    print(dp[N][T])
    ans = max(dp[N][T])
    print(ans)

import sys,copy,bisect,itertools
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examE()

Submission status: Time Limit Exceeded

Solution code:
# ABC145e


def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)

    n, t = map(int, input().split())
    food = [tuple(map(int, input().split())) for _ in range(n)]

    # dp[i][j][k]=j番目まで見てiを頼まずにk分以下の注文の組み合わせで最大の美味しさ
    dp = [[[0] * (t) for i in range(n + 1)] for j in range(n + 1)]

    for i in range(n+1):
        for j in range(n):
            for k in range(t):
                if i != j and k-food[j][0] >= 0:
                    dp[i][j][k] = max(dp[i][j][k], dp[i][j-1]
                                      [k - food[j][0]] + food[j][1])
                dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k])
    ans = 0
    for i in range(n):
        ans = max(ans, max(dp[i][n-1]) + food[i][1])
    # print(dp)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, t = map(int, input().split())
    f = [list(map(int, input().split())) for _ in range(n)]
    dp = [[0 for i in range(t)] for j in range(n + 1)]
    ls = [[[] for i in range(t)] for j in range(n + 1)]
    for i in range(n):
        for j in range(t):
            if j - f[i][0] >= 0 and dp[i][j - f[i][0]] + f[i][1] > dp[i][j]:
                dp[i + 1][j] = dp[i][j - f[i][0]] + f[i][1]
                ls[i + 1][j] = ls[i][j - f[i][0]] + [i]
            else:
                dp[i + 1][j] = dp[i][j]
                ls[i + 1][j] = ls[i][j][:]
    res = dp[n][t - 1]
    ff = [f[i][1] for i in range(n) if not i in ls[n][t - 1]]
    if ff:
        return res + max(ff)
    else:
        return res


print(main())

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, t = map(int, raw_input().split())
    wv = []
    for _ in xrange(n):
        a, b = map(int, raw_input().split())
        wv.append([a, b])
    wv.sort(key=lambda x: -x[1])

    dp = [[0] * (t + 1) for _ in xrange(n + 1)]
    used = [[0] * (t + 1) for _ in xrange(n + 1)]
    bits = [[0] * (t + 1) for _ in xrange(n + 1)]
    for i in xrange(n - 1, -1, -1):
        w, v = wv[i]
        dp_i = dp[i]
        used_i = used[i]
        bits_i = bits[i]
        dp_ip = dp[i + 1]
        used_ip = used[i + 1]
        bits_ip = bits[i + 1]
        for j in xrange(t + 1):
            if j < w:
                dp_i[j] = dp_ip[j]
                used_i[j] = used_ip[j]
                bits_i[j] = bits_ip[j]
            else:
                a = dp_ip[j - w] + v
                if a < dp_ip[j]:
                    dp_i[j] = dp_ip[j]
                    used_i[j] = used_ip[j]
                    bits_i[j] = bits_ip[j]
                else:
                    dp_i[j] = a
                    used_i[j] = used_ip[j - w] + w
                    bits_i[j] = bits_ip[j - w] + (1 << i)
    max_value = max(v for _, v in wv)
    value = dp[0]
    weight = used[0]
    bits = bits[0]
    best = 0
    for j in xrange(t, -1, -1):
        if weight[j] < t:
            b = bits[j]
            for i in xrange(n):
                if b & (1 << i) == 0:
                    best = max(best, value[j] + wv[i][1])
                    break
        else:
            best = max(best, value[j])
        if best >= value[j] + max_value:
            break
    print best

main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
import copy
def knapsack(N,W,weight,value):
  dp=[[999999999999 for i in range(W+1)] for j in range(N+1)]
  dp2=[[[] for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1): dp[0][i]=0

  for i in range(N):
    for w in range(W+1):
      if weight[i]<=w:
        if dp[i][w-weight[i]]+value[i]>dp[i][w]:
          dp[i+1][w]=dp[i][w-weight[i]]+value[i]
          x=copy.copy(dp2[i][w-weight[i]])
          x.append(i)
          dp2[i+1][w]=x
        else:
          dp[i+1][w]=dp[i][w]
          dp2[i+1][w]=dp2[i][w]
        
      else:
        dp[i+1][w]=dp[i][w]
        dp2[i+1][w]=dp2[i][w]
  return [dp[N][W],dp2[N][W]]

n,t=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
a=[];b=[]
for i in range(n):
  a.append(l[i][0])
  b.append(l[i][1])
z=knapsack(n,t-1,a,b)
x=z[0];y=z[1]
L=[0 for i in range(n)]
m=0
for i in range(len(y)):
  L[y[i]]=1
M=[]
for i in range(n):
  if L[i]==0:
    M.append(b[i])
if len(M)==0:
  print(x)
else:
  print(x+max(M))

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, t = map(int, raw_input().split())
    wv = []
    for _ in xrange(n):
        a, b = map(int, raw_input().split())
        wv.append([a, b])
    wv.sort(key=lambda x: -x[1])

    dp = [[0] * (t + 1) for _ in xrange(n + 1)]
    used = [[0] * (t + 1) for _ in xrange(n + 1)]
    bits = [[0] * (t + 1) for _ in xrange(n + 1)]
    for i in xrange(n - 1, -1, -1):
        w, v = wv[i]
        dp_i = dp[i]
        used_i = used[i]
        bits_i = bits[i]
        dp_ip = dp[i + 1]
        used_ip = used[i + 1]
        bits_ip = bits[i + 1]
        for j in xrange(t + 1):
            if j < w:
                dp_i[j] = dp_ip[j]
                used_i[j] = used_ip[j]
                bits_i[j] = bits_ip[j]
            else:
                a = dp_ip[j - w] + v
                if a < dp_ip[j]:
                    dp_i[j] = dp_ip[j]
                    used_i[j] = used_ip[j]
                    bits_i[j] = bits_ip[j]
                else:
                    dp_i[j] = a
                    used_i[j] = used_ip[j - w] + w
                    bits_i[j] = bits_ip[j - w] + (1 << i)
    max_value = max(v for _, v in wv)
    value = dp[0]
    weight = used[0]
    bits = bits[0]
    best = 0
    for j in xrange(t, -1, -1):
        if weight[j] < t:
            b = bits[j]
            for i in xrange(n):
                if b & (1 << i) == 0:
                    best = max(best, value[j] + wv[i][1])
                    break
        else:
            best = max(best, value[j])
    print best

main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, t = map(int, input().split())
    f = [list(map(int, input().split())) for _ in range(n)]
    dp = [[[0, []] for i in range(t)] for j in range(n + 1)]
    for i in range(n):
        for j in range(t):
            if j - f[i][0] >= 0 and dp[i][j - f[i][0]][0] + f[i][1] > dp[i][j][0]:
                dp[i + 1][j][0] = dp[i][j - f[i][0]][0] + f[i][1]
                dp[i + 1][j][1] = dp[i][j - f[i][0]][1] + [i]
            else:
                dp[i + 1][j] = dp[i][j]
                dp[i + 1][j][1] = dp[i][j][1][:]
    res = dp[n][t - 1][0]
    ff = [f[i][1] for i in range(n) if not i in dp[n][t - 1][1]]
    if ff:
        return res + max(ff)
    else:
        return res


print(main())

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy
import copy as cp

memory = {}

def dp(A,B,N,T):
    if (tuple(A),tuple(B),N,T) in memory:
        return memory[(tuple(A),tuple(B),N,T)]
    else:
        if T <= 0:
            memory[(tuple(A),tuple(B),N,T)] = 0
            return 0
        if N==1:
            memory[(tuple(A),tuple(B),N,T)] = B[0]
            return B[0]
        else:
            totalA=sum(A)-max(A)
            if totalA < T:
                memory[(tuple(A),tuple(B),N,T)] = sum(B)
                return sum(B)
            else:
                maxans=0
                for i in range(N):
                    A_n = A[:]
                    B_n = B[:]
                    A_n = A_n[:i]+A_n[i+1:]
                    B_n = B_n[:i]+B_n[i+1:]
                    t = T - A[i]
                    ans = B[i] + dp(A_n,B_n,N-1,t)
                    if ans>maxans:
                        maxans=ans
                memory[(tuple(A),tuple(B),N,T)] = maxans
                return maxans
        

N, T = map(int, input().split())
A =[]
B =[]
for i in range(N):
    a,b=map(int, input().split())
    A.append(a)
    B.append(b)
record = set()
ans = dp(A,B,N,T)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, t = list(map(int, input().split()))
dishes = [list(map(int, input().split())) for _ in range(n)]

dishes.sort(key = lambda x: (x[0], -x[1]))
#print(dishes)

# dp[i][j]はi番目までの品物から時間の和がj以下なるように選んだときの価値の和の最大値を表す。
dp = [[-1 for _ in range(t + 1)] for _ in range(n + 1)]

for j in range(t):
	dp[0][j] = 0

for i in range(n):
	for j in range(t):
		if j - dishes[i][0] >= 0:
			dp[i + 1][j] = max(dp[i][j], dp[i][j - dishes[i][0]] + dishes[i][1])
		else:
			dp[i + 1][j] = dp[i][j]
	dp[i + 1][t] = max(dp[i][t], dp[i][t - 1] + dishes[i][1])

#print(dp)
print(dp[-1][-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
import copy
def knapsack(N,W,weight,value):
  inf=float("inf")
  dp=[[-inf for i in range(W+1)] for j in range(N+1)]
  dp2=[[[] for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1): dp[0][i]=0

  for i in range(N):
    for w in range(W+1):
      if weight[i]<=w:
        if dp[i][w-weight[i]]+value[i]>dp[i][w]:
          dp[i+1][w]=dp[i][w-weight[i]]+value[i]
          x=copy.copy(dp2[i][w-weight[i]])
          x.append(i)
          dp2[i+1][w]=x
        else:
          dp[i+1][w]=dp[i][w]
          dp2[i+1][w]=dp2[i][w]
        
      else:
        dp[i+1][w]=dp[i][w]
        dp2[i+1][w]=dp2[i][w]
  return [dp[N][W],dp2[N][W]]

n,t=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
a=[];b=[]
for i in range(n):
  a.append(l[i][0])
  b.append(l[i][1])
z=knapsack(n,t-1,a,b)
x=z[0];y=z[1]
L=[0 for i in range(n)]
for i in range(len(y)):
  L[y[i]]=1
M=[]
for i in range(n):
  if L[i]==0:
    M.append(b[i])
if len(M)==0:
  print(x)
else:
  print(x+max(M))

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, t = map(int, raw_input().split())
    wv = []
    for _ in xrange(n):
        a, b = map(int, raw_input().split())
        wv.append([a, b])
    wv.sort(key=lambda x: -x[1])

    dp = [[0] * (t + 1) for _ in xrange(n + 1)]
    used = [[0] * (t + 1) for _ in xrange(n + 1)]
    bits = [[0] * (t + 1) for _ in xrange(n + 1)]
    for i in xrange(n - 1, -1, -1):
        w, v = wv[i]
        for j in xrange(t + 1):
            if j < w:
                dp[i][j] = dp[i + 1][j]
                used[i][j] = used[i + 1][j]
                bits[i][j] = bits[i + 1][j]
            else:
                a = dp[i + 1][j - w] + v
                if a < dp[i + 1][j]:
                    dp[i][j] = dp[i + 1][j]
                    used[i][j] = used[i + 1][j]
                    bits[i][j] = bits[i + 1][j]
                else:
                    dp[i][j] = a
                    used[i][j] = used[i + 1][j - w] + w
                    bits[i][j] = bits[i + 1][j - w] + (1 << i)
    max_value = max(v for _, v in wv)
    value = dp[0]
    weight = used[0]
    bits = bits[0]
    best = 0
    for j in xrange(t, -1, -1):
        if weight[j] < t:
            for i in xrange(n):
                if bits[j] & (1 << i) == 0:
                    best = max(best, value[j] + wv[i][1])
                    break
        else:
            best = max(best, value[j])
    print best

main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
def knapsack(N,W,weight,value):
  inf=float("inf")
  dp=[[-inf for i in range(W+1)] for j in range(N+1)]
  dp2=[[[] for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1): dp[0][i]=0

  for i in range(N):
    for w in range(W+1):
      if weight[i]<=w:
        if dp[i][w-weight[i]]+value[i]>dp[i][w]:
          dp[i+1][w]=dp[i][w-weight[i]]+value[i]
          dp2[i+1][w]=dp2[i][w-weight[i]]+[i]
        else:
          dp[i+1][w]=dp[i][w]
          dp2[i+1][w]=dp2[i][w]
        
      else:
        dp[i+1][w]=dp[i][w]
        dp2[i+1][w]=dp2[i][w]
  return [dp[N][W],dp2[N][W]]

n,t=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
a=[];b=[]
for i in range(n):
  a.append(l[i][0])
  b.append(l[i][1])
z=knapsack(n,t-1,a,b)
x=z[0];y=z[1]
L=[0 for i in range(n)]
for i in range(len(y)):
  L[y[i]]=1
M=[]
for i in range(n):
  if L[i]==0:
    M.append(b[i])
if len(M)==0:
  print(x)
else:
  print(x+max(M))

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
n,t=map(int,input().split())
AB=[]
for i in range(n):
  a,b=map(int,input().split())
  AB.append((a,b))

def f(m):
  dp=[[0]*(t) for _ in range(n+1)]
  for i in range(n):
    if i==m:
      a,b=AB[i]
      add=b
      for j in range(t):
        dp[i+1][j]=dp[i][j]
    else:
      a,b=AB[i]
      for j in range(t):
        if j>=a:
          dp[i+1][j]=max(dp[i][j-a]+b,dp[i][j],dp[i+1][j-1])
        else:
          dp[i+1][j]=max(dp[i][j],dp[i+1][j-1])
  return dp[n][t-1]+add
ans=0
for i in range(n):
  ans=max(ans,f(i))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def examE():
    N, T = LI()
    A, B = [0] * N, [0] * N
    for i in range(N):
        A[i], B[i] = map(int, input().split())
    maxT = max(A)
    dp = [[[0 for _ in range(2)] for _ in range(T+ 1)] for _ in range(N + 1)]
    for i in range(N):
        for j in range(T+ 1):
            if j>=1:
                dp[i+1][j][1] = max(dp[i+1][j][1], dp[i][j-1][0] + B[i])
            if A[i] <= j:
                dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j - A[i]][0] + B[i])
                dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j - A[i]][1] + B[i])
            dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][0])
            dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][1])

#    print(dp[N][T])
    ans = max(dp[N][T])
    print(ans)

import sys,copy,bisect,itertools
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examE()

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy
import copy

def dp(A,B,N,T):
    if T <= 0:
        return 0
    if N==1:
        return B[0]
    else:
        totalA=sum(A)-max(A)
        if totalA < T:
            return sum(B)
        else:
            maxans=0
            for i in range(N):
                A_n = A[:]
                B_n = B[:]
                A_n = A_n[:i]+A_n[i+1:]
                B_n = B_n[:i]+B_n[i+1:]
                t = T - A[i]
                ans = B[i] + dp(A_n,B_n,N-1,t)
                if ans>maxans:
                    maxans=ans
            return maxans
        

N, T = map(int, input().split())
A =[]
B =[]
for i in range(N):
    a,b=map(int, input().split())
    A.append(a)
    B.append(b)

ans = dp(A,B,N,T)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import heapq
n, t = (int(x) for x in input().split())
food = []
for _ in range(n):
    food.append(tuple(int(x) for x in input().split()))

visited = set()
stack = []
heapq.heappush(stack,(0, set()))
ans = 0
while stack:
    cur_t, state = heapq.heappop(stack)
    if cur_t >= t or len(state) == n:
        score = sum(food[s][1] for s in state)
        ans = max(ans, score)
    else:
        for i, f in enumerate(food):
            if i not in state:
                new_state = state.union((i,))
                new_state_tup = tuple(sorted(new_state))
                if new_state_tup not in visited:
                    if cur_t + f[0] >=t:
                        score = sum(food[s][1] for s in new_state)
                        ans = max(ans, score)
                    else:
                        heapq.heappush(stack, (cur_t + f[0], new_state))
                    visited.add(new_state_tup)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def knapsack(N,W,weight,value):
  inf=float("inf")
  dp=[[-inf for i in range(W+1)] for j in range(N+1)]
  dp2=[[[] for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1): dp[0][i]=0

  for i in range(N):
    for w in range(W+1):
      if weight[i]<=w:
        if dp[i][w-weight[i]]+value[i]>dp[i][w]:
          dp[i+1][w]=dp[i][w-weight[i]]+value[i]
          dp2[i+1][w]=dp2[i][w-weight[i]]+[i]
        else:
          dp[i+1][w]=dp[i][w]
          dp2[i+1][w]=dp2[i][w]
        
      else:
        dp[i+1][w]=dp[i][w]
        dp2[i+1][w]=dp2[i][w]
  return [dp[N][W],dp2[N][W]]

n,t=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
a=[];b=[]
for i in range(n):
  a.append(l[i][0])
  b.append(l[i][1])
x=knapsack(n,t-1,a,b)[0]
y=knapsack(n,t-1,a,b)[1]
L=[0 for i in range(n)]
for i in range(len(y)):
  L[y[i]]=1
M=[]
for i in range(n):
  if L[i]==0:
    M.append(b[i])
if len(M)==0:
  print(x)
else:
  print(x+max(M))