Problem:


Score : 100 points

Problem Statement

There are N items, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.

Taro has decided to choose some of the N items and carry them home in a knapsack.
The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.

Find the maximum possible sum of the values of items that Taro takes home.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq W \leq 10^5

1 \leq w_i \leq W

1 \leq v_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible sum of the values of items that Taro takes home.

Sample Input 1

3 8
3 30
4 50
5 60

Sample Output 1

90

Items 1 and 3 should be taken.
Then, the sum of the weights is 3 + 5 = 8, and the sum of the values is 30 + 60 = 90.

Sample Input 2

5 5
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

Sample Output 2

5000000000

The answer may not fit into a 32-bit integer type.

Sample Input 3

6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3

17

Items 2, 4 and 5 should be taken.
Then, the sum of the weights is 5 + 6 + 3 = 14, and the sum of the values is 6 + 6 + 5 = 17.



Sample input:
3 8
3 30
4 50
5 60


Sample output:
90

Submission status: Time Limit Exceeded

Solution code:


n,w = list(map(int,input().split()))
arr = []
for i in range(n):
    lista = list(map(int,input().split()))
    arr.append(lista)

dp = [0 for i in range(w+1)]

'''for i in range(1,w):
    for j in range(n):
        weight,value = arr[j]
        if(i-weight >=0):
            dp[i] = max(dp[i],dp[i-weight]+value)

ans = 0
for i in range(w+1):
    ans = max(ans,dp[i])

print(ans)'''

for i in range(n):
    weight ,value = arr[i]
    for j in range(w,weight-1,-1):
        dp[j] = max(dp[j] ,dp[j-weight]+ value)

ans = 0
for i in range(w+1):
    ans = max(ans,dp[i])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n, ww = map(int, input().split())

# dp = [0]*(ww+1)
dp = np.zeros(ww+1)

for i in range(n):
    w,v = map(int, input().split())
    for j in range(ww-w+1)[::-1]:
        if dp[j] or j==0:
            dp[j+w] = max(dp[j+w], dp[j]+v)
print(int(max(dp)))

Submission status: Time Limit Exceeded

Solution code:
n, ww = map(int, input().split())

dp = [0]*(ww+1)

for i in range(n):
    w,v = map(int, input().split())
    for j in range(ww-w+1)[::-1]:
        if dp[j] or j==0:
            dp[j+w] = max(dp[j+w], dp[j]+v)
print(max(dp))

Submission status: Time Limit Exceeded

Solution code:
N, W = map(int, input().split())
d = []
for i in range(N):
    w, v = map(int, input().split())
    d.append((w, v))

dp = [[0] * (W + 1) for i in range(N + 1)]
for i in range(N):
    for w in range(W + 1):
        w1, v1 = d[i]
        dp[i + 1][w] = max(dp[i + 1][w], dp[i][w])
        if w + w1 <= W:
            dp[i + 1][w + w1] = max(dp[i + 1][w + w1], dp[i][w] + v1)

num = 0
for i in range(W + 1):
    num = max(num, dp[N][i])

print(num)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3

def main():
    N, W = map(int, input().split())

    dp = [[0 for _ in range(W + 1)] for _ in range(N + 1)]

    for i in range(N):
        weight, value = map(int, input().split())

        for w in range(W + 1):
            if w < weight:
                dp[i+1][w] = dp[i][w]

            else:
                dp[i+1][w] = max(dp[i][w], dp[i][w-weight] + value)

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8


def main():
    #raw = ["3 8",
    #       "3 30",
    #       "4 50",
    #       "5 60"]
    #raw = ["5 5",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000"]
    #raw = ["6 15",
    #       "6 5",
    #       "5 6",
    #       "6 4",
    #       "6 6",
    #       "3 5",
    #       "7 2"]
    #N, W = map(int, raw[0].split())
    #data = [list(map(int, x.split())) for x in raw[1:]]
    import sys
    N, W = map(int, sys.stdin.readline().split())
    data = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
    #dp = [[0] * (W + 1) for _ in range(N+1)]
    #for i in range(N-1, -1, -1):
    #    for j in range(W+1):
    #        weight, value = data[i]
    #        if j < weight:
    #            dp[i][j] = dp[i+1][j]
    #        else:
    #            dp[i][j] = max(dp[i+1][j], dp[i+1][j - weight] + value)
    #print(dp[0][W])
    dp = [0] * (W + 1)
    mymax = max
    for i in range(N):
        weight, value = data[i]
        for j in range(W, weight-1, -1):
            dp[j] = mymax(dp[j], dp[j - weight] + value)
    print(dp[W])
    pass

def main2():
    #raw = ["3 8",
    #       "3 30",
    #       "4 50",
    #       "5 60"]
    #raw = ["5 5",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000"]
    #raw = ["6 15",
    #       "6 5",
    #       "5 6",
    #       "6 4",
    #       "6 6",
    #       "3 5",
    #       "7 2"]
    #N, W = map(int, raw[0].split())
    #data = [list(map(int, x.split())) for x in raw[1:]]
    import sys
    N, W = map(int, sys.stdin.readline().split())
    data = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
    from functools import cmp_to_key, lru_cache
    def cmp(x, y):
        return -1 if (x[1]+0.0)/x[0] > (y[1]+0.0)/y[0] else 1
    data.sort(key=cmp_to_key(cmp))
    @lru_cache(maxsize=None)
    def bt(i, c, aux, res):
        if i == N:
            return res
        aux2 = 0
        weight, value = data[i]
        if c != 0:
            c2 = c
            for j in range(i, N):
                if weight <= c2:
                    c2 -= weight
                    aux2 += value
                else:
                    aux2 += (value * c2) / weight
                    c2 = 0
        if aux2 + aux <= res:
            return res
        if weight <= c:
            aux3 = aux + value
            if aux3 > res:
                res = aux3
            res = bt(i+1, c - weight, aux3, res)
        return bt(i+1, c, aux, res)
    print(bt(0, W, 0, 0))

main2()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8


def main():
    #raw = ["3 8",
    #       "3 30",
    #       "4 50",
    #       "5 60"]
    #raw = ["5 5",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000"]
    #raw = ["6 15",
    #       "6 5",
    #       "5 6",
    #       "6 4",
    #       "6 6",
    #       "3 5",
    #       "7 2"]
    #N, W = map(int, raw[0].split())
    #data = [list(map(int, x.split())) for x in raw[1:]]
    import sys
    N, W = map(int, sys.stdin.readline().split())
    data = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
    #dp = [[0] * (W + 1) for _ in range(N+1)]
    #for i in range(N-1, -1, -1):
    #    for j in range(W+1):
    #        weight, value = data[i]
    #        if j < weight:
    #            dp[i][j] = dp[i+1][j]
    #        else:
    #            dp[i][j] = max(dp[i+1][j], dp[i+1][j - weight] + value)
    #print(dp[0][W])
    dp = [0] * (W + 1)
    mymax = max
    for i in range(N):
        weight, value = data[i]
        for j in range(W, weight-1, -1):
            dp[j] = mymax(dp[j], dp[j - weight] + value)
    print(dp[W])
    pass

def main2():
    #raw = ["3 8",
    #       "3 30",
    #       "4 50",
    #       "5 60"]
    #raw = ["5 5",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000"]
    #raw = ["6 15",
    #       "6 5",
    #       "5 6",
    #       "6 4",
    #       "6 6",
    #       "3 5",
    #       "7 2"]
    #N, W = map(int, raw[0].split())
    #data = [list(map(int, x.split())) for x in raw[1:]]
    import sys
    N, W = map(int, sys.stdin.readline().split())
    data = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
    from functools import cmp_to_key, lru_cache
    def cmp(x, y):
        return -1 if (x[1]+0.0)/x[0] > (y[1]+0.0)/y[0] else 1
    data.sort(key=cmp_to_key(cmp))
    from array import array
    @lru_cache(maxsize=100000)
    def bt(i, c, aux, res):
        if i == N:
            return res
        aux2 = 0
        weight, value = data[i]
        if c != 0:
            c2 = c
            for j in range(i, N):
                if weight <= c2:
                    c2 -= weight
                    aux2 += value
                else:
                    aux2 += (value * c2) / weight
                    c2 = 0
        if aux2 + aux <= res:
            return res
        if weight <= c:
            aux3 = aux + value
            if aux3 > res:
                res = aux3
            res = bt(i+1, c - weight, aux3, res)
            #aux -= value
        return bt(i+1, c, aux, res)
    res = bt(0, W, 0, 0)
    print(res)

main2()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8


def main():
    #raw = ["3 8",
    #       "3 30",
    #       "4 50",
    #       "5 60"]
    #raw = ["5 5",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000"]
    #raw = ["6 15",
    #       "6 5",
    #       "5 6",
    #       "6 4",
    #       "6 6",
    #       "3 5",
    #       "7 2"]
    #N, W = map(int, raw[0].split())
    #data = [list(map(int, x.split())) for x in raw[1:]]
    import sys
    N, W = map(int, sys.stdin.readline().split())
    data = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
    #dp = [[0] * (W + 1) for _ in range(N+1)]
    #for i in range(N-1, -1, -1):
    #    for j in range(W+1):
    #        weight, value = data[i]
    #        if j < weight:
    #            dp[i][j] = dp[i+1][j]
    #        else:
    #            dp[i][j] = max(dp[i+1][j], dp[i+1][j - weight] + value)
    #print(dp[0][W])
    dp = [0] * (W + 1)
    mymax = max
    for i in range(N):
        weight, value = data[i]
        for j in range(W, weight-1, -1):
            dp[j] = mymax(dp[j], dp[j - weight] + value)
    print(dp[W])
    pass

def main2():
    #raw = ["3 8",
    #       "3 30",
    #       "4 50",
    #       "5 60"]
    #raw = ["5 5",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000"]
    #raw = ["6 15",
    #       "6 5",
    #       "5 6",
    #       "6 4",
    #       "6 6",
    #       "3 5",
    #       "7 2"]
    #N, W = map(int, raw[0].split())
    #data = [list(map(int, x.split())) for x in raw[1:]]
    import sys
    N, W = map(int, sys.stdin.readline().split())
    data = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
    from functools import cmp_to_key, lru_cache
    def cmp(x, y):
        return -1 if (x[1]+0.0)/x[0] > (y[1]+0.0)/y[0] else 1
    data.sort(key=cmp_to_key(cmp))
    from array import array
    @lru_cache(maxsize=1000)
    def bt(i, c, aux, res):
        if i == N:
            return res
        aux2 = 0
        weight, value = data[i]
        if c != 0:
            c2 = c
            for j in range(i, N):
                if weight <= c2:
                    c2 -= weight
                    aux2 += value
                else:
                    aux2 += (value * c2) / weight
                    c2 = 0
        if aux2 + aux <= res:
            return res
        if weight <= c:
            aux3 = aux + value
            if aux3 > res:
                res = aux3
            res = bt(i+1, c - weight, aux3, res)
            #aux -= value
        return bt(i+1, c, aux, res)
    res = bt(0, W, 0, 0)
    print(res)

main2()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8


def main():
    #raw = ["3 8",
    #       "3 30",
    #       "4 50",
    #       "5 60"]
    #raw = ["5 5",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000"]
    #raw = ["6 15",
    #       "6 5",
    #       "5 6",
    #       "6 4",
    #       "6 6",
    #       "3 5",
    #       "7 2"]
    #N, W = map(int, raw[0].split())
    #data = [list(map(int, x.split())) for x in raw[1:]]
    import sys
    N, W = map(int, sys.stdin.readline().split())
    data = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
    #dp = [[0] * (W + 1) for _ in range(N+1)]
    #for i in range(N-1, -1, -1):
    #    for j in range(W+1):
    #        weight, value = data[i]
    #        if j < weight:
    #            dp[i][j] = dp[i+1][j]
    #        else:
    #            dp[i][j] = max(dp[i+1][j], dp[i+1][j - weight] + value)
    #print(dp[0][W])
    dp = [0] * (W + 1)
    mymax = max
    for i in range(N):
        weight, value = data[i]
        for j in range(W, weight-1, -1):
            dp[j] = mymax(dp[j], dp[j - weight] + value)
    print(dp[W])
    pass

def main2():
    #raw = ["3 8",
    #       "3 30",
    #       "4 50",
    #       "5 60"]
    #raw = ["5 5",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000"]
    #raw = ["6 15",
    #       "6 5",
    #       "5 6",
    #       "6 4",
    #       "6 6",
    #       "3 5",
    #       "7 2"]
    #N, W = map(int, raw[0].split())
    #data = [list(map(int, x.split())) for x in raw[1:]]
    import sys
    N, W = map(int, sys.stdin.readline().split())
    data = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
    from functools import cmp_to_key
    def cmp(x, y):
        return -1 if (x[1]+0.0)/x[0] > (y[1]+0.0)/y[0] else 1
    data.sort(key=cmp_to_key(cmp))
    from array import array
    def bt(i, c, aux, res):
        if i == N:
            return res
        aux2 = 0
        weight, value = data[i]
        if c != 0:
            c2 = c
            for j in range(i, N):
                if weight <= c2:
                    c2 -= weight
                    aux2 += value
                else:
                    aux2 += (value * c2) / weight
                    c2 = 0
        if aux2 + aux <= res:
            return res
        if weight <= c:
            aux3 = aux + value
            if aux3 > res:
                res = aux3
            res = bt(i+1, c - weight, aux3, res)
            #aux -= value
        return bt(i+1, c, aux, res)
    res = bt(0, W, 0, 0)
    print(res)

main2()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8


def main():
    #raw = ["3 8",
    #       "3 30",
    #       "4 50",
    #       "5 60"]
    #raw = ["5 5",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000"]
    #raw = ["6 15",
    #       "6 5",
    #       "5 6",
    #       "6 4",
    #       "6 6",
    #       "3 5",
    #       "7 2"]
    #N, W = map(int, raw[0].split())
    #data = [list(map(int, x.split())) for x in raw[1:]]
    import sys
    N, W = map(int, sys.stdin.readline().split())
    data = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
    #dp = [[0] * (W + 1) for _ in range(N+1)]
    #for i in range(N-1, -1, -1):
    #    for j in range(W+1):
    #        weight, value = data[i]
    #        if j < weight:
    #            dp[i][j] = dp[i+1][j]
    #        else:
    #            dp[i][j] = max(dp[i+1][j], dp[i+1][j - weight] + value)
    #print(dp[0][W])
    dp = [0] * (W + 1)
    mymax = max
    for i in range(N):
        weight, value = data[i]
        for j in range(W, weight-1, -1):
            dp[j] = mymax(dp[j], dp[j - weight] + value)
    print(dp[W])
    pass

def main2():
    #raw = ["3 8",
    #       "3 30",
    #       "4 50",
    #       "5 60"]
    #raw = ["5 5",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000"]
    #raw = ["6 15",
    #       "6 5",
    #       "5 6",
    #       "6 4",
    #       "6 6",
    #       "3 5",
    #       "7 2"]
    #N, W = map(int, raw[0].split())
    #data = [list(map(int, x.split())) for x in raw[1:]]
    import sys
    N, W = map(int, sys.stdin.readline().split())
    data = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
    from functools import cmp_to_key
    def cmp(x, y):
        return -1 if (x[1]+0.0)/x[0] > (y[1]+0.0)/y[0] else 1
    data.sort(key=cmp_to_key(cmp))
    def bt(i, c, aux, res):
        if i == N:
            return res
        aux2 = 0
        weight, value = data[i]
        if c != 0:
            c2 = c
            for j in range(i, N):
                if weight <= c2:
                    c2 -= weight
                    aux2 += value
                else:
                    aux2 += (value * c2) / weight
                    c2 = 0
        if aux2 + aux <= res:
            return res
        if weight <= c:
            aux += value
            if aux > res:
                res = aux
            res = bt(i+1, c - weight, aux, res)
            aux -= value
        return bt(i+1, c, aux, res)
    res = bt(0, W, 0, 0)
    print(res)

main2()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8


def main():
    #raw = ["3 8",
    #       "3 30",
    #       "4 50",
    #       "5 60"]
    #raw = ["5 5",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000"]
    #raw = ["6 15",
    #       "6 5",
    #       "5 6",
    #       "6 4",
    #       "6 6",
    #       "3 5",
    #       "7 2"]
    #N, W = map(int, raw[0].split())
    #data = [list(map(int, x.split())) for x in raw[1:]]
    import sys
    N, W = map(int, sys.stdin.readline().split())
    data = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
    #dp = [[0] * (W + 1) for _ in range(N+1)]
    #for i in range(N-1, -1, -1):
    #    for j in range(W+1):
    #        weight, value = data[i]
    #        if j < weight:
    #            dp[i][j] = dp[i+1][j]
    #        else:
    #            dp[i][j] = max(dp[i+1][j], dp[i+1][j - weight] + value)
    #print(dp[0][W])
    dp = [0] * (W + 1)
    mymax = max
    for i in range(N):
        weight, value = data[i]
        for j in range(W, weight-1, -1):
            dp[j] = mymax(dp[j], dp[j - weight] + value)
    print(dp[W])
    pass

def main2():
    #raw = ["3 8",
    #       "3 30",
    #       "4 50",
    #       "5 60"]
    #raw = ["5 5",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000"]
    #raw = ["6 15",
    #       "6 5",
    #       "5 6",
    #       "6 4",
    #       "6 6",
    #       "3 5",
    #       "7 2"]
    #N, W = map(int, raw[0].split())
    #data = [list(map(int, x.split())) for x in raw[1:]]
    import sys
    N, W = map(int, sys.stdin.readline().split())
    data = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
    from functools import cmp_to_key
    def cmp(x, y):
        return -1 if (x[1]+0.0)/x[0] > (y[1]+0.0)/y[0] else 1
    data.sort(key=cmp_to_key(cmp))
    def bt(i, c, aux, res):
        if i == N:
            return res
        aux2 = 0
        if c != 0:
            c2 = c
            for j in range(i, N):
                weight, value = data[i]
                if weight <= c2:
                    c2 -= weight
                    aux2 += value
                else:
                    aux2 += (value * c2) / weight
                    c2 = 0
        if aux2 + aux <= res:
            return res
        if weight <= c:
            #c -= weight
            aux += value
            if aux > res:
                res = aux
            res = bt(i+1, c - weight, aux, res)
            aux -= value
            #c += weight
        return bt(i+1, c, aux, res)
    res = bt(0, W, 0, 0)
    print(res)

main2()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8


def main():
    #raw = ["3 8",
    #       "3 30",
    #       "4 50",
    #       "5 60"]
    #raw = ["5 5",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000"]
    #raw = ["6 15",
    #       "6 5",
    #       "5 6",
    #       "6 4",
    #       "6 6",
    #       "3 5",
    #       "7 2"]
    #N, W = map(int, raw[0].split())
    #data = [list(map(int, x.split())) for x in raw[1:]]
    import sys
    N, W = map(int, sys.stdin.readline().split())
    data = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
    #dp = [[0] * (W + 1) for _ in range(N+1)]
    #for i in range(N-1, -1, -1):
    #    for j in range(W+1):
    #        weight, value = data[i]
    #        if j < weight:
    #            dp[i][j] = dp[i+1][j]
    #        else:
    #            dp[i][j] = max(dp[i+1][j], dp[i+1][j - weight] + value)
    #print(dp[0][W])
    dp = [0] * (W + 1)
    mymax = max
    for i in range(N):
        weight, value = data[i]
        for j in range(W, weight-1, -1):
            dp[j] = mymax(dp[j], dp[j - weight] + value)
    print(dp[W])
    pass

def main2():
    #raw = ["3 8",
    #       "3 30",
    #       "4 50",
    #       "5 60"]
    #raw = ["5 5",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000"]
    #raw = ["6 15",
    #       "6 5",
    #       "5 6",
    #       "6 4",
    #       "6 6",
    #       "3 5",
    #       "7 2"]
    #N, W = map(int, raw[0].split())
    #data = [list(map(int, x.split())) for x in raw[1:]]
    import sys
    N, W = map(int, sys.stdin.readline().split())
    data = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
    from functools import cmp_to_key
    def cmp(x, y):
        return -1 if (x[1]+0.0)/x[0] > (y[1]+0.0)/y[0] else 1
    data.sort(key=cmp_to_key(cmp))
    def bt(i, c, aux, res):
        if i == N:
            return res
        aux2 = 0
        c2 = c
        if c2 != 0:
            for j in range(i, N):
                weight, value = data[i]
                if weight <= c2:
                    c2 -= weight
                    aux2 += value
                else:
                    aux2 += (value * c2) / weight
                    c2 = 0
        if aux2 + aux <= res:
            return res
        if weight <= c:
            c -= weight
            aux += value
            if aux > res:
                res = aux
            res = bt(i+1, c, aux, res)
            aux -= value
            c += weight
        return bt(i+1, c, aux, res)
    res = bt(0, W, 0, 0)
    print(res)

main2()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8


def main():
    #raw = ["3 8",
    #       "3 30",
    #       "4 50",
    #       "5 60"]
    #raw = ["5 5",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000"]
    #raw = ["6 15",
    #       "6 5",
    #       "5 6",
    #       "6 4",
    #       "6 6",
    #       "3 5",
    #       "7 2"]
    #N, W = map(int, raw[0].split())
    #data = [list(map(int, x.split())) for x in raw[1:]]
    import sys
    N, W = map(int, sys.stdin.readline().split())
    data = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
    #dp = [[0] * (W + 1) for _ in range(N+1)]
    #for i in range(N-1, -1, -1):
    #    for j in range(W+1):
    #        weight, value = data[i]
    #        if j < weight:
    #            dp[i][j] = dp[i+1][j]
    #        else:
    #            dp[i][j] = max(dp[i+1][j], dp[i+1][j - weight] + value)
    #print(dp[0][W])
    dp = [0] * (W + 1)
    mymax = max
    for i in range(N):
        weight, value = data[i]
        for j in range(W, weight-1, -1):
            dp[j] = mymax(dp[j], dp[j - weight] + value)
    print(dp[W])
    pass

def main2():
    #raw = ["3 8",
    #       "3 30",
    #       "4 50",
    #       "5 60"]
    #raw = ["5 5",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000"]
    #raw = ["6 15",
    #       "6 5",
    #       "5 6",
    #       "6 4",
    #       "6 6",
    #       "3 5",
    #       "7 2"]
    #N, W = map(int, raw[0].split())
    #data = [list(map(int, x.split())) for x in raw[1:]]
    import sys
    N, W = map(int, sys.stdin.readline().split())
    data = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
    from functools import cmp_to_key
    def cmp(x, y):
        return -1 if x[1]/x[0] > y[1]/y[0] else 1
    data.sort(key=cmp_to_key(cmp))
    def bt(i, c, aux, res):
        if i == N:
            return res
        aux2 = 0
        c2 = c
        if c2 != 0:
            for j in range(i, N):
                weight, value = data[i]
                if weight <= c2:
                    c2 -= weight
                    aux2 += value
                else:
                    aux2 += (value * c2) / weight
                    c2 = 0
        if aux2 + aux <= res:
            return res
        if weight <= c:
            c -= weight
            aux += value
            if aux > res:
                res = aux
            res = bt(i+1, c, aux, res)
            aux -= value
            c += weight
        return bt(i+1, c, aux, res)
    res = bt(0, W, 0, 0)
    print(res)

main2()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8


def main():
    #raw = ["3 8",
    #       "3 30",
    #       "4 50",
    #       "5 60"]
    #raw = ["5 5",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000"]
    #raw = ["6 15",
    #       "6 5",
    #       "5 6",
    #       "6 4",
    #       "6 6",
    #       "3 5",
    #       "7 2"]
    #N, W = map(int, raw[0].split())
    #data = [list(map(int, x.split())) for x in raw[1:]]
    import sys
    N, W = map(int, sys.stdin.readline().split())
    data = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
    #dp = [[0] * (W + 1) for _ in range(N+1)]
    #for i in range(N-1, -1, -1):
    #    for j in range(W+1):
    #        weight, value = data[i]
    #        if j < weight:
    #            dp[i][j] = dp[i+1][j]
    #        else:
    #            dp[i][j] = max(dp[i+1][j], dp[i+1][j - weight] + value)
    #print(dp[0][W])
    dp = [0] * (W + 1)
    mymax = max
    for i in range(N):
        weight, value = data[i]
        for j in range(W, weight-1, -1):
            dp[j] = mymax(dp[j], dp[j - weight] + value)
    print(dp[W])
    pass

main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8


def main():
    #raw = ["3 8",
    #       "3 30",
    #       "4 50",
    #       "5 60"]
    #raw = ["5 5",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000",
    #       "1 1000000000"]
    #raw = ["6 15",
    #       "6 5",
    #       "5 6",
    #       "6 4",
    #       "6 6",
    #       "3 5",
    #       "7 2"]
    #N, W = map(int, raw[0].split())
    #data = [list(map(int, x.split())) for x in raw[1:]]
    import sys
    N, W = map(int, sys.stdin.readline().split())
    data = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
    ###
    #dp = [[0] * (W + 1) for _ in range(N+1)]
    #for i in range(N-1, -1, -1):
    #    for j in range(W+1):
    #        weight, value = data[i]
    #        if j < weight:
    #            dp[i][j] = dp[i+1][j]
    #        else:
    #            dp[i][j] = max(dp[i+1][j], dp[i+1][j - weight] + value)
    #print(dp[0][W])
    ###
    dp = [0] * (W + 1)
    for i in range(N):
        for j in range(W, data[i][0]-1, -1):
            dp[j] = max(dp[j], dp[j - data[i][0]] + data[i][1])
    print(dp[W])
    pass

main()

Submission status: Time Limit Exceeded

Solution code:
import sys
# inputを高速化する。
input = sys.stdin.readline

# 入力
N, W = map(int, input().split())

dp = [[0 for i in range(W+1)] for j in range(N+1)]


def chmax(a, b):
    if a >= b:
        return a
    else:
        return b

def knapsack(n, w):
    lst_weight = [0 for _ in range(N)]
    lst_value = [0 for _ in range(N)]
    for i in range(N):
        lst_weight[i], lst_value[i] = map(int, input().split())


    for i in range(N):
        for sum_w in range(W+1):
            # i番目の品物を選ぶ
            if sum_w - lst_weight[i] >= 0:
                dp[i+1][sum_w] = chmax(dp[i+1][sum_w], dp[i][sum_w - lst_weight[i]] + lst_value[i])
            # i番目の品物を選ばない
            dp[i+1][sum_w] = chmax(dp[i+1][sum_w], dp[i][sum_w])
    
    return dp[n][w]


print(knapsack(N, W))

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

N, W = map(int, input().split())

dp = [0] * (W + 1)

for _ in range(N):
    w, v = map(int, input().split())
    for wk in range(W, w-1, -1):
        tv = dp[wk - w] + v
        if tv > dp[wk]:
            dp[wk] = tv

print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:

import sys

input = sys.stdin.readline

N, W = map(int, input().split())

dp = [0] * (W + 1)

for _ in range(N):
    w, v = map(int, input().split())
    for wk in range(W, w-1, -1):
        tv = dp[wk - w] + v
        if tv > dp[wk]:
            dp[wk] = tv

print(max(dp))

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

N, W = map(int, input().split())

dp = [0] * (W + 1)

for _ in range(N):
    w, v = map(int, input().split())
    for wk in range(W, w-1, -1):
        tv = dp[wk - w] + v
        if tv > dp[wk]:
            dp[wk] = tv

print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys

# inputを高速化する。
input = sys.stdin.readline

# 入力
N, W = map(int, input().split())

dp = [0] * (W + 1)

for _ in range(N):
    weight, value = map(int, input().split())
    for wk in range(W, weight-1, -1):
        tv = dp[wk - weight] + value
        if tv > dp[wk]:
            dp[wk] = tv

print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys

# inputを高速化する。
input = sys.stdin.readline

# 入力
N, W = map(int, input().split())
lst_weight_value = [list(map(int, input().split())) for _ in range(N)]

def chmax(a, b):
    if a >= b:
        return a
    else:
        return b

dp = [0] * (W + 1)

for weight, value in lst_weight_value:
    for wk in range(W, weight-1, -1):
        tv = dp[wk - weight] + value
        if tv > dp[wk]:
            dp[wk] = tv

print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys

# inputを高速化する。
input = sys.stdin.readline

# 入力
N, W = map(int, input().split())
# lst_weight = [0 for _ in range(N)]
# lst_value = [0 for _ in range(N)]
# for i in range(N):
#     lst_weight[i], lst_value[i] = map(int, input().split())

def chmax(a, b):
    if a >= b:
        return a
    else:
        return b

dp = [[0 for i in range(W+1)] for j in range(N+1)]

for i in range(N):
    weight, value = map(int, input().split())
    for sum_w in range(W+1):
        # i番目の品物を選ぶ
        if sum_w - weight >= 0:
            dp[i+1][sum_w] = chmax(dp[i+1][sum_w], dp[i][sum_w - weight] + value)
        # i番目の品物を選ばない
        dp[i+1][sum_w] = chmax(dp[i+1][sum_w], dp[i][sum_w])

print(dp[N][W])