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

Solution (Python):

N,T=map(int,input().split())
A=[0 for i in range(N+1)]
B=[0 for i in range(N+1)]
for i in range(1,N+1):
    A[i],B[i]=map(int,input().split())

dp1=[[0 for i in range(6001)] for j in range(N+1)]
dp2=[[0 for i in range(6001)] for j in range(N+2)]

for i in range(1,N+1):
    for j in range(T+1):
        if j>=A[i]:
            dp1[i][j]=max(dp1[i-1][j],dp1[i-1][j-A[i]]+B[i])
        else:
            dp1[i][j]=dp1[i-1][j]

for i in range(N,0,-1):
    for j in range(T+1):
        if j>=A[i]:
            dp2[i][j]=max(dp2[i+1][j],dp2[i+1][j-A[i]]+B[i])
        else:
            dp2[i][j]=dp2[i+1][j]

ans=0
for i in range(1,N+1):
    for j in range(T):
        ans=max(ans,dp1[i-1][j]+dp2[i+1][T-1-j]+B[i])
print(ans)

Solution (Python):

import sys
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10**6)
INF = 10**12
m = 10**9 + 7


def main():
    N, T = map(int, input().split())
    AB = []
    for _ in range(N):
        AB.append(list(map(int, input().split())))
    AB.sort()

    dp = [0] * (T + 1)
    ans = 0
    for i in range(N-1):
        for j in range(T, -1, -1):
            if j + AB[i][0] <= T:
                dp[j + AB[i][0]] = max(dp[j + AB[i][0]], dp[j] + AB[i][1])
        ans = max(ans, dp[T-1]+AB[i+1][1])
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline
I=lambda:int(input())
MI=lambda:map(int,input().split())
LI=lambda:list(map(int,input().split()))

N,T=MI()
dp=[[0]*(T+1) for _ in [0]*(N+1)]
food=[tuple(MI()) for _ in [0]*N]
food.sort()

for n in range(N):
    a,b=food[n]
    for t in range(T+1):
        if t<T:
            
            dp[n+1][min(t+a,T)]=max(dp[n][t]+b,dp[n+1][min(t+a,T)])
        
        dp[n+1][t]=max(dp[n][t],dp[n+1][t])
print(dp[N][T])

Solution (Python):
N,T=map(int,input().split())
L=[]
for i in range(N):
  a,b=map(int,input().split())
  L.append((a,b))
L.sort()

DP=[[0 for i in range(T)] for j in range(N+1)]

for i in range(1,N+1):
  for j in range(T):
    if i>0 and j-L[i-1][0]>=0:
      DP[i][j]=max(DP[i-1][j-L[i-1][0]]+L[i-1][1],DP[i-1][j])
    else:
      DP[i][j]=DP[i-1][j]
ans=0
for i in range(N+1):
  if i!=N:
    ans=max(ans,DP[i][T-1]+L[i][1])
  else:
    ans=max(ans,DP[i][T-1])

print(ans)

Solution (Python):
N,T=map(int,input().split())
L=[]
for i in range(N):
  a,b=map(int,input().split())
  L.append((a,b))
L.sort()

DP=[[0 for i in range(T)] for j in range(N)]

for i in range(N):
  a=L[i][0]
  b=L[i][1]
  for j in range(min(T,a)):
    DP[i][j]=DP[i-1][j]
  for j in range(min(T,a),T):
    DP[i][j]=max(DP[i-1][j-a]+b,DP[i-1][j])
    
ans=0
for i in range(N-1):
  ans=max(ans,DP[i][T-1]+L[i+1][1])
print(ans)

Solution (Python):
N,T=[int(s) for s in input().split()]
food=[[int(s) for s in input().split()] for i in range(N)]
food.sort()


DP=[[0 for t in range(T)] for i in range(N)]
for i in range(N):
  time,point=food[i]
  for t in range(min([T,time])):
    DP[i][t]=DP[i-1][t]
  for t in range(time,T):
    DP[i][t]=max([DP[i-1][t] , DP[i-1][t-time]+point])


ls=[DP[i-1][T-1]+food[i][1] for i in range(1,N)]
ls.append(food[0][1])
print(max(ls))

Solution (Python):

def main():
    N, T = map(int,input().split())
    A = [0]
    B = [0]

    for _ in range(N):
        a, b = map(int,input().split())
        A.append(a)
        B.append(b)

    dp1 = [[0] * (T + 1) for i in range(N + 1)]
    
    dp2 = [[0] * (T + 1) for i in range(N + 2)]
    

    for i in range(1, N+1):
        for t in range(T+1):
            if t >= A[i]:
                dp1[i][t] = max(dp1[i-1][t], dp1[i-1][t-A[i]] + B[i])
            else:
                dp1[i][t] = dp1[i-1][t]

    for i in range(N, 0, -1):
        for t in range(T + 1):
            if t >= A[i]:
                dp2[i][t] = max(dp2[i + 1][t], dp2[i + 1][t - A[i]] + B[i])
            else:
                dp2[i][t] = dp2[i + 1][t]

    ans = dp1[N][T]
    for i in range(1, N+1): 
        for t in range(T):
            ans = max(ans, dp1[i-1][t] + dp2[i+1][T-1-t] + B[i])
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):


N, T = map(int, input().split())
food = [tuple(map(int, input().split())) for i in range(N)]

dp = [[0]*(3010) for i in range(3010)]

for i in range(N):
    time, value = food[i]
    for j in range(T):
        if j >= time:
            dp[i+1][j] = max(dp[i][j], dp[i][j-time] + value)
        else:
            dp[i+1][j] = dp[i][j]
        dp[i][j] = max(dp[i][j], dp[i][j-1])

rev_dp = [[0]*(3010) for i in range(3010)]
food.reverse()

for i in range(N):
    time, value = food[i]
    for j in range(T):
        if j >= time:
            rev_dp[i+1][j] = max(rev_dp[i][j], rev_dp[i][j-time] + value)
        else:
            rev_dp[i+1][j] = rev_dp[i][j]
    dp[i][j] = max(dp[i][j], dp[i][j-1])


ans = 0
for x in range(N):
    ans = max(ans, max(dp[N-1-x][t] + rev_dp[x][T-1-t]
                       for t in range(T))+food[x][1])
print(ans)

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

t -= 1
dp1 = [[0] * (t+1) for _ in range(n+1)]
dp2 = [[0] * (t+1) for _ in range(n+2)]


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


for j in reversed(range(2, n+2)):
    i = j-2
    for time in range(t+1):
        if time - dishes[i][0] >= 0:
            dp2[j-1][time] = max(
                dp2[j][time-dishes[i][0]] + dishes[i][1],
                dp2[j][time]
            )
        dp2[j-1][time] = max(dp2[j][time], dp2[j-1][time])


ans = 0
for i in range(1, n+1):
    for time in range(t+1):
        ans = max(
            ans,
            dp1[i-1][time] + dishes[i-1][1] + dp2[i+1][t - time]
        )
print(ans)

Solution (Python):
N, T = map(int, input().split())
food = [0] * N
for i in range(N):
  food[i] = list(map(int, input().split()))

food = sorted(food)  

dp = [[0] * (T + 1) for i in range(N + 1)]  

for i in range(N):
  for j in range(1, T + 1):
    
    now = max(dp[i + 1][j - 1], dp[i][j])
    
    if (j - food[i][0]) >= 0:
      now2 = dp[i][j - food[i][0]] + food[i][1]
    else:
      now2 = 0
    dp[i + 1][j] = max(now, now2)
    


ans = 0
for i in range(N):
  now3 = dp[i][T - 1] + food[i][1]
  
  ans = max(ans, now3)
  
print(ans)

Solution (Python):
N, T = map(int, input().split())
fs = [tuple(map(int, input().split())) for _ in range(N)]
fs = sorted(fs)
dp = [0] * 6005

for a, b in fs:
    for t in range(T + a - 1, a - 1, -1):
        dp[t] = max(dp[t - a] + b, dp[t])
print(max(dp))

Solution (Python):
from itertools import chain
N, T = map(int, input().split())


AB = [(0, 0)]
for i in range(1, N+1):
    a, b = map(int, input().split())
    
    
    AB.append((a, b))
AB.sort()
dp = [[-1]*(6000+1) for _ in range(N+1)]


dp[0][0] = 0
for i in range(1, N+1):
    a, b = AB[i]
    for t in range(T):
        dp[i][t+a] = max(dp[i][t+a], dp[i-1][t] + b)
        dp[i][t] = max(dp[i][t], dp[i-1][t])
print(max(chain.from_iterable(dp)))

Solution (Python):
"""
時間がかかる料理は、食べるとしたら最後に食べるのがベスト。
逆にいうと、その料理を食べるかどうかを検討する段階ではほかの料理は食べるかどうかの検討は終わっている状態。
なので、食べるのにかかる時間でソートしてナップサックすればよい。
"""

N,T = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N)]
dp = [0]*(T)
AB.sort()

ans = 0
for i in range(N):
    a,b = AB[i]
    ans = max(ans,dp[-1]+b)
    for j in range(T-1,-1,-1):
        if j-a >= 0:
            dp[j] = max(dp[j],dp[j-a]+b)
print(ans)

Solution (Python):
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq

n,t =ma()
AB = []
am = 0
for i in range(n):
    a,b = ma()
    AB.append((a,b))
    am = max(am,a)

dp1 = [[0]*t for i in range(n+1)] 
for i in range(n):
    a,b = AB[i]
    
    for j in range(t-1,-1,-1):
        dp1[i][j] = dp1[i-1][j]
        if j+a<t:
            dp1[i][j+a] = max(dp1[i-1][j]+b,dp1[i][j+a])

dp2 = [[0]*t for i in range(n+1)] 

for i in range(n):
    a,b = AB[n-i-1]
    for j in range(t-1,-1,-1):
        dp2[n-i-1][j] = dp2[n-i][j]
        if j+a<t:
            dp2[n-i-1][j+a] = max(dp2[n-i][j]+b,dp2[n-i-1][j+a])

tmp=0
for i in range(n):
    for j in range(t):
        tmp = max(tmp,AB[i][1] + dp1[i-1][j] +dp2[i+1][t-1-j])
print(tmp)

Solution (Python):
import sys
from collections import deque
from functools import lru_cache
import bisect
import copy
import heapq
import itertools
import math
import random
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    N, T = read_values()
    P = sorted([tuple(read_values()) for _ in range(N)], key=lambda p: (p[0], -p[1]))

    dp = [[0] * (T + 2) for _ in range(N + 1)]

    for i, (a, b) in enumerate(P):
        for t in range(T + 1):
            if t < T:
                s = min(T, t + a)
                dp[i + 1][s] = max(dp[i + 1][s], dp[i][t] + b)
            dp[i + 1][t] = max(dp[i + 1][t], dp[i][t])
    print(max(dp[N]))


if __name__ == "__main__":
    main()

Solution (Python):
N, T = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]


AB.sort(key=lambda x: x[0])


dp = [[0 for _ in range(T)] for _ in range(N + 1)]
for i in range(1, N + 1):
    for j in range(T):
        
        if j - AB[i - 1][0] >= 0:
            
            dp[i][j] = max(dp[i - 1][j - AB[i - 1][0]] + AB[i - 1][1], dp[i - 1][j])
        
        else:
            dp[i][j] = dp[i - 1][j]

ans = 0


for k in range(1, N + 1):
    tmp = dp[k - 1][T - 1] + AB[k - 1][1]
    ans = max(ans, tmp)

print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = 10**6
mod = 10 ** 9 + 7 





N, T = MAP()
AB = [LIST() for _ in range(N)]
AB.sort(key = lambda x:x[0])
t = [0]*T
ans = 0

for A, B in AB:
	for i in range(T-1, 0, -1):
		if t[i]:
			if T <= i+A:
				ans = max(ans, t[i]+B)
			else:
				t[i+A] = max(t[i+A], t[i]+B)
	if T <= A:
		ans = max(ans, B)
	else:
		t[A] = max(t[A], B)

ans = max(ans, max(t))
print(ans)

Solution (Python):
N,T=map(int,input().split())

ablist=[]
bsum=0
for _ in range(N):
  a,b=map(int,input().split())
  ablist.append((a,b))
  bsum+=b
ablist.sort()
  
dp=[[0]*T for _ in range(N+1)]
for i in range(1,N+1):
  a,b=ablist[i-1]
  for j in range(T):
    dp[i][j]=dp[i-1][j]
    if j-a>=0:
      dp[i][j]=max(dp[i][j],dp[i-1][j-a]+b)      


if dp[N][T-1]==bsum:
  print(bsum)
else:
  answer=0
  for i in range(1,N+1):
    answer=max(answer,dp[i-1][T-1]+ablist[i-1][1])
  print(answer)

Solution (Python):
import sys


def main():
    N, T, *AB = map(int, sys.stdin.buffer.read().split())
    D = [(a, b) for a, b in zip(*[iter(AB)] * 2)]

    D.sort()

    dp = [0] * T
    ans = 0

    for i, (a, b) in enumerate(D[:-1]):
        for t in range(T - 1, a - 1, -1):
            if dp[t] < dp[t - a] + b:
                dp[t] = dp[t - a] + b
        if ans < dp[T - 1] + D[i + 1][1]:
            ans = dp[T - 1] + D[i + 1][1]

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, T, *AB = map(int, read().split())
    D = [(a, b) for a, b in zip(*[iter(AB)] * 2)]

    D.sort()

    dp = [0] * T
    ans = 0

    for i, (a, b) in enumerate(D[:-1]):
        for t in range(T - 1, a - 1, -1):
            if dp[t] < dp[t - a] + b:
                dp[t] = dp[t - a] + b
        if ans < dp[T - 1] + D[i + 1][1]:
            ans = dp[T - 1] + D[i + 1][1]

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, T, *AB = map(int, read().split())
    D = [(a, b) for a, b in zip(*[iter(AB)] * 2)]

    D.sort()

    dp = [0] * T
    ans = 0

    for i, (a, b) in enumerate(D[:-1]):
        for t in range(T - 1, a - 1, -1):
            if dp[t] < dp[t - a] + b:
                dp[t] = dp[t - a] + b
        if ans < dp[T - 1] + D[i + 1][1]:
            ans = dp[T - 1] + D[i + 1][1]

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
N, T = map(int, input().split())
A = []
for _ in range(N):
  a, b = map(int,input().split())
  A.append((a, b))
A.sort(key=lambda x: x[0])

newdp = [0 for _ in range(T)]
ans = 0
from collections import deque
A = deque(A)
visited = [0]
while A:
  olddp = tuple(newdp)
  newdp = list(newdp)
  X = A.popleft()
  a = X[0]
  b = X[1]
  for v in visited:
    if v + a > T-1:
      ans = max(ans, olddp[v]+b)
    else:

      if newdp[v+a] == 0:
        visited.append(v+a)
      newdp[v+a] = max(olddp[v] + b, newdp[v+a])


if ans == 0:
  print(max(olddp))
else:
  print(ans)

Solution (Python):
n, t = map(int, input().split())
dp1 = [[-float("inf") for i in range(t)] for i in range(n + 1)]
dp1[0][0] = 0
dp2 = [[-float("inf") for i in range(t)] for i in range(n + 1)]
dp2[n][0] = 0
ab = []
for i in range(n):
    ab.append(list(map(int, input().split())))
for i in range(n):
    a, b = ab[i]
    for j in range(t - 1, -1, -1):
        if j >= a:
            dp1[i + 1][j] = max(dp1[i][j], dp1[i][j - a] + b)
        else:
            dp1[i + 1][j] = dp1[i][j]
for i in range(n + 1):
    for j in range(t - 1):
        dp1[i][j + 1] = max(dp1[i][j], dp1[i][j + 1])
for i in range(n - 1, -1, -1):
    a, b = ab[i]
    for j in range(t - 1, -1, -1):
        if j >= a:
            dp2[i][j] = max(dp2[i + 1][j], dp2[i + 1][j - a] + b)
        else:
            dp2[i][j] = dp2[i + 1][j]
for i in range(n + 1):
    for j in range(t - 1):
        dp2[i][j + 1] = max(dp2[i][j], dp2[i][j + 1])
ans = [max([dp1[i][j] + dp2[i + 1][t - 1 - j] + ab[i][1] for j in range(t)]) for i in range(n)]
print(max(ans))

Solution (Python):
import sys



def input():
    return sys.stdin.readline().rstrip()


def main():
    N, T = map(int, input().split())
    dp = [0] * (T + 1)
    AB = []
    for k in range(N):
        a, b = map(int, input().split())
        AB.append((a, b))

    AB.sort()
    maxdp = 0
    temp = 1
    for i in range(N):
        a, b = AB[i]  
        temp += a
        dp[T] = max(maxdp + b, dp[T])
        for j in range(min(T - 1, temp), a - 1, -1):
            if dp[j] < dp[j-a] +b:
                dp[j] = dp[j - a] + b
                maxdp = max(dp[j], maxdp)
    print(dp[T])


if __name__ == "__main__":
    main()

Solution (Python):
import sys



def input():
    return sys.stdin.readline().rstrip()


def main():
    N, T = map(int, input().split())
    dp = [0] * (T + 1)
    AB = []
    for k in range(N):
        a, b = map(int, input().split())
        AB.append((a, b))

    AB.sort()
    maxdp = 0
    temp = 1
    for i in range(N):
        a, b = AB[i]  
        temp += a
        dp[T] = max(maxdp + b, dp[T])
        for j in range(min(T - 1, temp), a - 1, -1):
            dp[j] = max(dp[j], dp[j - a] + b)
            maxdp = max(dp[j], maxdp)
    print(dp[T])


if __name__ == "__main__":
    main()

Solution (Python):
import sys



def input():
    return sys.stdin.readline().rstrip()


def main():
    N, T = map(int, input().split())
    dp = [0] * (T + 1)
    AB = []
    for k in range(N):
        a, b = map(int, input().split())
        AB.append((a, b))

    AB.sort()
    maxdp = 0
    temp =0
    for i in range(N):
        a, b = AB[i]  
        temp +=a
        dp[T] = max(maxdp + b, dp[T])
        for j in range(min(T - 1,temp+1), a - 1, -1):
            dp[j] = max(dp[j], dp[j - a] + b)
            maxdp = max(dp[j], maxdp)
    print(dp[T])


if __name__ == "__main__":
    main()

Solution (Python):
import sys



def input():
    return sys.stdin.readline().rstrip()


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

    dp = [0] * (T + 1)

    AB = []

    for k in range(N):
        a, b = map(int, input().split())
        AB.append((a, b))

    AB.sort()
    maxdp = 0

    for i in range(N):
        a, b = AB[i]  

        dp[T] =max( maxdp + b,dp[T])
        for j in range(T - 1, a - 1, -1):
            dp[j] = max(dp[j], dp[j - a] + b)
            maxdp= max(dp[j],maxdp)
    print(dp[T])

if __name__ == "__main__":
    main()

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

MAX = 10000
dish.sort()
dp = [[0] * MAX for _ in range(n+1)]

for i in range(n):
    time, value = dish[i]
    for j in range(t):
        dp[i+1][j+time] = max(dp[i+1][j+time], dp[i][j] + value)
    for j in range(MAX):
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])

print(max(dp[n]))

Solution (Python):
n, t = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(n)]
ab.sort()
m = ab[-1][0]
dp = [0] * (m + t)
for a, b in ab:
    for j in range(t - 1, -1, -1):
        dp[a + j] = max(dp[a + j], dp[j] + b)
print(max(dp))

Solution (Python):
N, T = map(int, input().split())
dp = [[0 for j in range(T)] for i in range(N)]
W = []
V = []
for i in range(N):
    w, v = map(int, input().split())
    W += [w]
    V += [v]
    
W, V = zip(*sorted(zip(W, V), key=lambda x: x[0]))
for i in range(N-1):
    for j in range(T):
        if j - W[i] < 0 :
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(dp[i][j], dp[i][j-W[i]] + V[i])
ans = 0
for i in range(N):
    ans = max(ans, dp[i][-1] + V[i])
print(ans)