Problem:


Score : 100 points

Problem Statement

Taro's summer vacation starts tomorrow, and he has decided to make plans for it now.

The vacation consists of N days.
For each i (1 \leq i \leq N), Taro will choose one of the following activities and do it on the i-th day:

A: Swim in the sea. Gain a_i points of happiness.

B: Catch bugs in the mountains. Gain b_i points of happiness.

C: Do homework at home. Gain c_i points of happiness.

As Taro gets bored easily, he cannot do the same activities for two or more consecutive days.

Find the maximum possible total points of happiness that Taro gains.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq a_i, b_i, c_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N
a_1 b_1 c_1
a_2 b_2 c_2
:
a_N b_N c_N

Output

Print the maximum possible total points of happiness that Taro gains.

Sample Input 1

3
10 40 70
20 50 80
30 60 90

Sample Output 1

210

If Taro does activities in the order C, B, C, he will gain 70 + 50 + 90 = 210 points of happiness.

Sample Input 2

1
100 10 1

Sample Output 2

100

Sample Input 3

7
6 7 8
8 8 3
2 5 2
7 8 6
4 6 8
2 3 4
7 5 1

Sample Output 3

46

Taro should do activities in the order C, A, B, A, C, B, A.



Sample input:
3
10 40 70
20 50 80
30 60 90


Sample output:
210

Submission status: Accepted

Solution code:
def edpcC(n, abcs):
    coidx = ((1, 2), (2, 0), (0, 1))
    dp = [abcs[0]]
    for i in range(1, n):
        prev_dp = dp[i-1]
        cur_vals = abcs[i]
        new_d = []
        for j in range(3):
            tmp = [cur_vals[j][1] + p[1] for p in (prev_dp[c] for c in coidx[j])]
            new_d.append((j, max(tmp)))
        dp.append(new_d)
    print(max(dp[-1], key=lambda x:x[1])[1])
    
n = int(input())
abcs = [list(enumerate(map(int, input().split(' ')))) for _ in range(n)]
edpcC(n, abcs)

Submission status: Accepted

Solution code:
import sys
 
stdin = sys.stdin

mod = 1000000007
inf = 1 << 60
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
nas = lambda: stdin.readline().split()

n = ni()

abc = [na() for i in range(n)]
dp = [3 * [0] for i in range(n)]
dp[0][0] = abc[0][0]
dp[0][1] = abc[0][1]
dp[0][2] = abc[0][2]

for i in range(n - 1):
    for j in range(3):
        for k in range(3):
            if k != j:
                dp[i + 1][j] = max(dp[i + 1][j], dp[i][k] + abc[i + 1][j])

ans = max(dp[n - 1][0], dp[n - 1][1], dp[n - 1][2])
print(ans)

Submission status: Accepted

Solution code:
N=int(input())

a=[0]*N
b=[0]*N
c=[0]*N

flag = -1
for i in range(N):
    a[i],b[i],c[i] = map(int,input().split())

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

for i in range(N):
    if i==0:
        dp[i][0]=a[i]
        dp[i][1]=b[i]
        dp[i][2]=c[i]
    else:
        dp[i][0]=a[i] + max(dp[i-1][1],dp[i-1][2])
        dp[i][1]=b[i] + max(dp[i-1][2],dp[i-1][0])
        dp[i][2]=c[i] + max(dp[i-1][0],dp[i-1][1])
print(max(dp[N-1]))

Submission status: Accepted

Solution code:
n = int(input())
v = [ list(map(int, input().split())) for _ in range(n) ]
dp = [ [0]*3 for _ in range(n) ]
for i in range(n):
  for j in range(3):
    if i == 0:
      dp[i][j] = v[i][j]
    else:
      for k in range(3):
        if j != k:
          dp[i][j] = max(dp[i][j], dp[i-1][k]+v[i][j])
print(max(dp[n-1]))

Submission status: Accepted

Solution code:
n=int(input())
a=[0]*n
b=[0]*n
c=[0]*n
for i in range(n):
    a[i],b[i],c[i]=map(int,input().split())

dp=[[0,0,0] for _ in range(n+1)]
for i in range(1,n+1):
    dp[i][0] = max(dp[i-1][1],dp[i-1][2])+a[i-1]
    dp[i][1] = max(dp[i-1][0],dp[i-1][2])+b[i-1]
    dp[i][2] = max(dp[i-1][0],dp[i-1][1])+c[i-1]

print(max(dp[n]))

Submission status: Accepted

Solution code:
N = int(input())
H = []
for i in range(N):
    H.append(list(map(int, input().split())))

dp = [[0]*3 for _ in range(N)]
dp[0] = H[0]
for i in range(1,N):
    for j in range(3):
        dp[i][j] = H[i][j] + max(dp[i-1][k] for k in range(3) if k != j)
print(max(dp[N-1]))

Submission status: Accepted

Solution code:
N = int(input())
P = []

for _ in range(N):
    P.append(list(map(int, input().split())))

dp = [[0 for _ in range(3)] for _ in range(10**5)]

for i in range(N):
    for j in range(3):
        for k in range(3):
            if k == j:
                continue
            dp[i][k] = max(dp[i][k], dp[i-1][j] + P[i][k])
            
print(max(dp[N-1]))

Submission status: Accepted

Solution code:
N = int(input())

happiness = list(map(int,input().split()))
dp = happiness # dp: i日目にa~cそれぞれの活動をしたときの幸福度の最大値

for i in range(1,N):
    happiness = list(map(int,input().split()))
    dp_pre = dp.copy()

    dp[0] = max(dp_pre[1],dp_pre[2])+happiness[0] # i日目にaをする場合
    dp[1] = max(dp_pre[0],dp_pre[2])+happiness[1] # i日目にbをする場合
    dp[2] = max(dp_pre[1],dp_pre[0])+happiness[2] # i日目にcをする場合
     
print(max(dp))

Submission status: Accepted

Solution code:
n = int(input())
x,y,z = 0,0,0
for _ in range(n):
    a,b,c = map(int,input().split())
    x,y,z = a + max(y,z), b + max(x,z), c + max(x,y)
print(max(x,y,z))

Submission status: Accepted

Solution code:
n = int(input())
a = [0]*n
b = [0]*n
c = [0]*n
for i in range(n):
    a[i],b[i],c[i] = map(int,input().split())

dp = [[0,0,0] for i in range(n+1)]
for i in range(1,n+1):
    dp[i][0] = max(dp[i-1][1],dp[i-1][2])+a[i-1]
    dp[i][1] = max(dp[i-1][0],dp[i-1][2])+b[i-1]
    dp[i][2] = max(dp[i-1][0],dp[i-1][1])+c[i-1]

print(max(dp[n]))

Submission status: Accepted

Solution code:
N = int(input())

dp = [[None]*3 for n in range(N+1)]
dp[0] = [0, 0, 0]

for n in range(1, N+1):
    act = list(map(int, input().split()))
    dp[n][0] = max(dp[n-1][1], dp[n-1][2]) + act[0]
    dp[n][1] = max(dp[n-1][2], dp[n-1][0]) + act[1]
    dp[n][2] = max(dp[n-1][0], dp[n-1][1]) + act[2]

print(max(dp[N]))

Submission status: Accepted

Solution code:
N = int(input())
a = [[0 for _ in range(3)] for _ in range(N)]
for i in range(N):
    t = list(map(int, input().split()))
    for j in range(3):
        a[i][j] = t[j]

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

for i in range(N):
    for j in range(3):
        for k in range(3):
            if j == k:
                continue
            dp[i+1][k] = max(dp[i+1][k], dp[i][j] + a[i][k])
#print(dp)


answer = 0
for j in range(3):
    answer = max(answer, dp[N][j])

print(answer)

Submission status: Accepted

Solution code:
N = int(input())
H = []
for _ in range(N):
    H.append(tuple(map(int, input().split())))

table = [[0] * 3 for _ in range(N)]
st = set(range(3))
for n in range(N):
    table[n] = [H[n][i] + max(table[n - 1][j] for j in (st - {i})) for i in st]

print(max(table[N - 1]))

Submission status: Accepted

Solution code:
n=int(input())
old=[0,0,0]
new=[0,0,0]
for i in range(n):
    a,b,c=map(int,input().split())
    new[0]=a+max(old[1],old[2])
    new[1]=b+max(old[2],old[0])
    new[2]=c+max(old[0],old[1])
    old=new[:]

print(max(new))

Submission status: Accepted

Solution code:
n=int(input())
a=[[0]*3]*n
for i in range(n):
    a[i]=list(map(int,input().split()))
b=[0]*3
for i in range(n-2,-1,-1):
    for j in range(3):
        t=0
        for k in range(3):
            if j!=k:
                t=max(t,a[i][j]+a[i+1][k])
        a[i][j]=t
print(max(a[0]))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    abc = [list(map(int,input().split())) for _ in range(N)]
    abc.insert(0,[0,0,0])
    dp = [[0]*3 for _ in range(N+1)]
    for i in range(1,N+1):
        for j in range(3):
            for k in range(3):
                if j != k:
                    dp[i][j] = max(dp[i][j],dp[i-1][k]+abc[i][j])
    print(max(dp[N]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

def main():
    N=int(input())
    l=[]
    for i in range(N):
        l.append(list(map(int,input().split())))
    dp=[[0]*3 for _ in range(N+1)]
    for i in range(N):
        for k in range(3):
            dp[i+1][k]=max(dp[i][(k+1)%3]+l[i][k],dp[i][(k+2)%3]+l[i][k])
    print(max(dp[N]))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())

what_to_do = []
for i in range(N):
    input2 = input().split()
    tmp1,tmp2,tmp3 = list(map(int,input2))
    what_to_do.append([tmp1,tmp2,tmp3])

dp = []
for _ in range(N+1):
    dp.append([0,0,0])

for i in range(N):
    for k in range(3):
        for j in range(3):
            if k != j:
                dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + what_to_do[i][k])
                
            
print(max(dp[-1]))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)


MOD = int(1e09) + 7
INF = int(1e15)


def main():
    # sys.stdin = open("sample.txt")
    N = Scanner.int()
    A = [Scanner.map_int() for _ in range(N)]
    dp = [[0 for _ in range(3)] for _ in range(N)]
    dp[0][0], dp[0][1], dp[0][2] = A[0]
    for i in range(1, N):
        for c in range(3):
            dp[i][c] = max(dp[i][c], dp[i-1][(c+1) % 3] + A[i][c])
            dp[i][c] = max(dp[i][c], dp[i-1][(c+2) % 3] + A[i][c])
    print(max(dp[N-1]))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
lis = [list(map(int,input().split())) for _ in range(n)]
# print(lis)
# [[10, 40, 70], [20, 50, 80], [30, 60, 90]]
#dp = [lis[0]+[0,0,0]*(n-1)]
#dp[0]=lis[0]
#dp = [lis[0][0]+[0]*(n-2),lis[0][1]+[0]*(n-2),lis[0][2+[0]*(n-2)]
dp = [[lis[0][0]]+[0]*(n-1),[lis[0][1]]+[0]*(n-1),[lis[0][2]]+[0]*(n-1)]
#print(dp)

for j in range(n):
    if j == 0:
        continue
    for i in range(3):
        if i == 0:
            dp[i][j] = max(dp[1][j-1],dp[2][j-1])+lis[j][i]
        if i == 1:
            dp[i][j] = max(dp[0][j-1],dp[2][j-1])+lis[j][i]
        if i == 2:
            dp[i][j] = max(dp[0][j-1],dp[1][j-1])+lis[j][i]

#print(dp)

print(max(dp[0][n-1],dp[1][n-1],dp[2][n-1]))

Submission status: Accepted

Solution code:
def get_vacation(vacation_data):
    A, B, C = range(3)
    dp = [[-1 for i in range(3)] for j in range(len(vacation_data))]
    dp[0] = vacation_data[0]
    for i in range(1, len(vacation_data)):
        a, b, c = vacation_data[i]
        dp[i][A] = max(dp[i - 1][B], dp[i - 1][C]) + a
        dp[i][B] = max(dp[i - 1][A], dp[i - 1][C]) + b
        dp[i][C] = max(dp[i - 1][A], dp[i - 1][B]) + c
    return max(dp[-1])


def get_input():
    n = int(input())
    data = []
    for i in range(n):
        el = list(map(int, input().split()))
        data.append(el)
    return data
 
data = get_input()
print(get_vacation(data))

Submission status: Accepted

Solution code:


days = int(input())
mat = [[0 for x in range(3)] for y in range(days)] 
a = [[0 for x in range(3)] for y in range(days)] 


for day in range(days):
    ln = input()
    ln = ln.split()
    for act in range(0,3):
        a[day][act] = int(ln[act])
        #print(mat[day][act])
        

for i in range(0,days):
    for act in range(0,3):
        if(i==0):
           mat[i][act] = a[i][act]
        else:
            if(act == 0):
                mat[i][0] = max(mat[i-1][1] + a[i][act],  mat[i-1][2] + a[i][act])
            elif(act == 1):
                mat[i][1] = max(mat[i-1][0] + a[i][act],  mat[i-1][2] + a[i][act])
            elif(act == 2):        
                mat[i][2] = max(mat[i-1][1] + a[i][act],  mat[i-1][0] + a[i][act])
        
        
pts = max(mat[days-1][0], mat[days-1][1])  
pts = max(pts, mat[days-1][2])
print(pts)

'''    
a= [int(act) for act in input().split()]
'''

Submission status: Accepted

Solution code:
# coding: utf-8
#便利そうなので置いとく
import fractions 
import functools 
#再帰関数を使うときは、再帰上限を上げておこうね！
import sys
sys.setrecursionlimit(200000000)
# my functions here!
def pin(type=int):
    return map(type,input().split())
"""
------------------------------------------------------------------
|OBJECTIVE:                                                      |
|Educatinal DP contest("https://atcoder.jp/contests/dp")を解こう!|
------------------------------------------------------------------
"""
#solution:
INF = float("inf")
"""
|#遷移式を考える
|   つまり、漸化式みたいに i回目までの結果を保存して、i+1回目の評価を行う
"""
def Flog_1(N,H):
    dp=[INF for i in range(N)]#{DP table}:最小化問題なので初期値はINF, h1=H[0]）
    dp[0]=0  #h1=0
    dp[1]=abs(H[1]-H[0])#ほら、"2段前"がないから…
    for i in range(2,N):
        hop1=dp[i-1]+abs(H[i-1]-H[i]) 
        hop2=dp[i-2]+abs(H[i-2]-H[i]) 
        #print(hop1,hop2)
        dp[i]=hop1 if hop1<hop2 else hop2
    return dp
def Flog_1_Another(N,H):#"配る"DP
    dp=[INF for i in range(N)]#{DP table}:最小化問題なので初期値はINF, h1=H[0]）
    dp[0]=0  #h1=0
    for i in range(0,N):
        for k in [1,2]:
            if i+k <N:
                dp[i+k]=min(dp[i+k],dp[i]+abs(H[i+k]-H[i]))
    return dp
"""
|# Flog1との違い:飛ぶ段数に（1個か2個という）制限がない
|    
"""    
def Flog_2(N,K,H):#pypy回答でなんとかおｋ
    dp=[INF for i in range(N)]#{DP table}:最小化問題なので初期値はINF, h1=H[0]）
    dp[0]=0  #h1=0,default position
    for i in range(1,N):#k段前から飛んでくることを考えよ
        temp = INF
        for k in range(1,K+1):
            if i-k < 0:break
            else:
                #print("*",i-k)
                temp = min(temp,(dp[i-k]+abs(H[i-k]-H[i])))
                
        dp[i]=temp
        #print("*",dp)
    return dp
def Flog_2_Another(N,K,H):#"配る"DP
    dp=[INF for i in range(N)]#{DP table}:最小化問題なので初期値はINF, h1=H[0]）
    dp[0]=0  #h1=0
    for i in range(0,N):
        for k in range(1,K+1):
            if i+k <N:
                dp[i+k]=min(dp[i+k],dp[i]+abs(H[i+k]-H[i]))
    return dp
def Flog_2_YETAnother(N,K,H):#python提出やりたい再帰関数やりたいよお
    #つまり、つまりf2Y(N,K,H)->f2Y((0 or enough value to be default),K,H)に持っていく関数を書くぞ
    if N == 1:
        return 0
    else:
        map(min,[Flog_2_YETAnother(N-i,K,H) for i in range(1,N+1)])
    #詰んだ

def vacation(N):
    #これは二次元で書けばいいですね？
    dp=[[0]*3 for i in range(N)]#[i]+1日目にある行動をした時にできる幸福度の最大値 
    dp[0]=list(pin())
    #print(dp[0])
    for i in range(1,N):
        #めんどいのでfor分かかない、ごめんね
        temp=list(pin())
       # print("*",temp)
        dp[i][0]= max(dp[i-1][1],dp[i-1][2])+temp[0]
        dp[i][1]= max(dp[i-1][2],dp[i-1][0])+temp[1]
        dp[i][2]= max(dp[i-1][0],dp[i-1][1])+temp[2]
        #print(dp[i])
    return max(dp[-1])
#input
N,=pin()

#output
print(vacation(N))

Submission status: Accepted

Solution code:
#DP-C

n=int(input())
l=[list(map(int,input().split())) for i in range(n)]

dp=[[0]*3 for i in range(n)]
dp[0]=l[0]
#print(dp)

for i in range(1,n):
    for j in range(3):
        for k in range(3):
            #同じ活動の場合を除く
            if j!=k:
                dp[i][j]=max(dp[i-1][k]+l[i][j],dp[i][j])
print(max(dp[-1]))

Submission status: Accepted

Solution code:
# C
N = int(input())
abc = [list(map(int, input().split())) for i in range(N)]

da, db, dc = 0, 0, 0

for a, b, c in abc:
    na = max(db, dc) + a
    nb = max(da, dc) + b
    nc = max(da, db) + c
    da = na
    db = nb
    dc = nc
    
print(max([na, nb, nc]))

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

n = int(input())

abc = [[int(i) for i in input().split()] for j in range(n)]

def happy(n, abc):
    da, db, dc = 0, 0, 0
    
    for a, b, c in abc:
        na = max(db, dc) + a
        nb = max(da, dc) + b
        nc = max(da, db) + c
        
        da, db, dc = na, nb, nc
    
    return max(da, db, dc)
    
print(happy(n, abc))

Submission status: Accepted

Solution code:
N = int(input())
arr = [[0]*3 for _ in range(N)]
"""
a = [0]*N
b = [0]*N
c = [0]*N
"""
for i in range(N):
    arr[i]=list(map(int,input().split()))
    #a[i],b[i],c[i] = map(int,input().split())

dp = [[-1]*3 for _ in range(N)]

dp[0] = arr[0]

for i in range(1,N):
    for j in range(3):
        k = (j+1)%3
        l = (j+2)%3
        dp[i][j] = max(dp[i-1][k]+arr[i][j],dp[i-1][l]+arr[i][j])

ans = max(dp[-1])
print(ans)

Submission status: Accepted

Solution code:
import sys
n = int(sys.stdin.readline().strip('\n'))
a = [list(map(int, sys.stdin.readline().strip('\n').split())) for _ in range(n)]
# dp[i+1][j]: i日目までの活動に加え、i+1日目に活動jを選んだときの幸福度の最大値
dp = [[0 for _ in range(3)] for _ in range(n+1)]
for i in range(n):
  for j in range(3):
    for k in range(3):
      if j==k: continue
      dp[i+1][k] = max(dp[i+1][k], dp[i][j] + a[i][k])
#for i in range(n+1): print(dp[i])
print(max(dp[n]))

Submission status: Accepted

Solution code:
n=int(input())
l=[list(map(int,input().split())) for i in range(n)]
dp=[[0]*3 for i in range(n)]
#print(dp)
dp[0]=l[0]
#print(dp)
for i in range(1,n):
    for j in range(3):
        for k in range(3):
            if j!=k:
                dp[i][j]=max(dp[i][j],dp[i-1][k]+l[i][j])
print(max(dp[-1]))

Submission status: Accepted

Solution code:
N = int(input())

abc = []
for i in range(N):
    tmp = list(map(int, input().split()))
    abc.append(tmp)
    
dp = [[0]*3 for i in range(N+1)]
for i in range(N):
    a, b, c = abc[i]
    dp[i+1][0] += max(dp[i][1] + a, dp[i][2] + a)
    dp[i+1][1] += max(dp[i][0] + b, dp[i][2] + b)
    dp[i+1][2] += max(dp[i][0] + c, dp[i][1] + c)
                
print(max(dp[N]))

Submission status: Accepted

Solution code:
N = int(input())
dp = [[0] * 3 for _ in range(N)]
action = list(map(int, input().split()))
dp[0][0] = action[0]
dp[0][1] = action[1]
dp[0][2] = action[2]
for i in range(N-1):
    action = list(map(int, input().split()))
    dp[i+1][0] = max(dp[i][1]+action[0],dp[i][2]+action[0])
    dp[i+1][1] = max(dp[i][0]+action[1],dp[i][2]+action[1])
    dp[i+1][2] = max(dp[i][0]+action[2],dp[i][1]+action[2])
print(max(dp[N-1]))

Submission status: Accepted

Solution code:
N = int(input())
act = [[int(item) for item in input().split()] for _ in range(N)]

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

for i in range(N):
    for j in range(3):
        temp_hp = []
        for k in range(3):
            if k != j:
                temp_hp.append(dp[i-1][k] + act[i][j])
        dp[i][j] = max(temp_hp)
# print(dp)
print(max(dp[N-1]))

Submission status: Accepted

Solution code:
n, *t = map( int, open( 0 ).read().split() )
 
# manemane
A, B, C = 0, 0, 0
for i in  range( n ):
  A, B, C = t[ 3 * i  ] + max( B, C ), t[ 3 * i + 1 ] + max( A, C ), t[ 3 * i + 2 ] + max( A, B )
 
print( max( A, B, C ) )

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())

abc = np.array([list(map(int, input().split())) for i in range(N)])

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

for i in range(1, N+1):
    dp[i][0] = max(dp[i - 1][1] + abc[i - 1][1], dp[i - 1][2] + abc[i - 1][2])
    dp[i][1] = max(dp[i - 1][0] + abc[i - 1][0], dp[i - 1][2] + abc[i - 1][2])
    dp[i][2] = max(dp[i - 1][1] + abc[i - 1][1], dp[i - 1][0] + abc[i - 1][0])

print(max(dp[-1]))

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())

abc = np.array([list(map(int, input().split())) for i in range(N)])

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

for i in range(1, N+1):
    dp[i][0] = max(dp[i - 1][1] + abc[i - 1][1], dp[i - 1][2] + abc[i - 1][2])
    dp[i][1] = max(dp[i - 1][0] + abc[i - 1][0], dp[i - 1][2] + abc[i - 1][2])
    dp[i][2] = max(dp[i - 1][1] + abc[i - 1][1], dp[i - 1][0] + abc[i - 1][0])

print(max(max(dp)))

Submission status: Accepted

Solution code:
#指定数分の３つの整数を順番に受けてリストに入力
n = int(input())
a = [0]*n
b = [0]*n
c = [0]*n
for i in range(n):
    a[i],b[i],c[i] = map(int, input().split())

'''
前日までの選択による最大値が分かれば、当日の最適選択ができる
dpテーブルを３枠用意する形にして、下から順に更新していけば良い
'''

# 初日の効用
dp = []
dp.append([a[0],b[0],c[0]])

# ２日目以降のループ
for i in range(1,n):
    dp.append( [ \
        a[i] + max(dp[i-1][1],dp[i-1][2]), \
        b[i] + max(dp[i-1][0],dp[i-1][2]), \
        c[i] + max(dp[i-1][0],dp[i-1][1])  \
        ] )

print(max(dp[n-1][0],dp[n-1][1],dp[n-1][2]))

Submission status: Accepted

Solution code:
n = int(input())
s = [list(map(int, input().split())) for i in range(n)]
dp = [[0, 0, 0] for _ in range(n)]
dp[0][:] = s[0][:]
for i in range(1, n):
    for j in range(3):  # dasu te
        for k in range(3):  # maeno te
            if j != k:
                try:
                    dp[i][j] = max(dp[i][j], dp[i-1][k] + s[i][j])
                except:
                    pass

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

Submission status: Accepted

Solution code:
n = int(input())
h = [list(map(int, input().split())) for _ in range(n)]
dp = [[0, 0, 0] for _ in range(2)]
for i, (a, b, c) in enumerate(h):
    dp[i % 2][0] = a + max(dp[1 - i % 2][1], dp[1 - i % 2][2])
    dp[i % 2][1] = b + max(dp[1 - i % 2][0], dp[1 - i % 2][2])
    dp[i % 2][2] = c + max(dp[1 - i % 2][0], dp[1 - i % 2][1])
print(max(dp[i % 2]))

Submission status: Accepted

Solution code:
N=int(input())
List=[list(map(int,input().split())) for i in range(N)]
A,B,C=zip(*List)
dp=[[0]*3 for i in range(N)]
dp[0][0]=A[0]
dp[0][1]=B[0]
dp[0][2]=C[0]
for i in range(1,N):
  dp[i][0]=max(dp[i-1][1],dp[i-1][2])+A[i]
  dp[i][1]=max(dp[i-1][0],dp[i-1][2])+B[i]
  dp[i][2]=max(dp[i-1][0],dp[i-1][1])+C[i]
print(max(dp[-1]))

Submission status: Accepted

Solution code:
N = int(input())
x = [[0,0,0]]*N
for i in range(N):
	x[i] = list(map(int,input().split()))
#print(x)

#dp = [[0,0,0]]*N this caused error!!! dp[i] and dp[j] have the same memory!!!!
dp = [[0,0,0] for i in range(N)]
#dp[i][j] means the happiness of day i, when Taro do activity j.
dp[0] = x[0]

for i in range(1,N):
	for j in range(3):
		for k in range(3):
			#print(i,j,k,dp)
			if j != k:
				dp[i][j] = max(dp[i][j],dp[i-1][k]+x[i][j])
#print(dp)
print(max(dp[N-1]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline  #文字列入力では注意！

N = int(input())
a,b,c = map(int,input().split())
dp = [[a,b,c]]
for i in range(N-1):
    a,b,c = map(int,input().split())
    A = max(dp[i][1]+a,dp[i][2]+a)
    B = max(dp[i][2]+b,dp[i][0]+b)
    C = max(dp[i][0]+c,dp[i][1]+c)
    dp.append([A,B,C])
print(max(dp[-1]))

Submission status: Accepted

Solution code:
n = int(input())
A,B,C = [],[],[]
for i in range(n):
    a,b,c = map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)

dp = [[0]*3 for i in range(n+1)]
for i in range(n):
    dp[i+1][0] = max(max( dp[i][1], dp[i][2] ) + A[i] ,dp[i+1][0])
    dp[i+1][1] = max(max( dp[i][0], dp[i][2] ) + B[i] ,dp[i+1][1])
    dp[i+1][2] = max(max( dp[i][0], dp[i][1] ) + C[i] ,dp[i+1][2])
    
print(max(dp[-1]))

Submission status: Accepted

Solution code:
N = int(input())
X=[list(map(int,input().split())) for i in range(N)]
#print(X)
dp = [[0] * 3 for _ in range(N+1)]
for i in range(N):
  for j in range(3):
    for k in range(3):
      if j==k:
        continue
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][k] + X[i][j])	
ans=0
for j in range(3):
  ans=max(ans,dp[N][j])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X=[list(map(int,input().split())) for i in range(N)]
#print(X)
dp = [[0] * 3 for _ in range(N+1)]
for i in range(N):
  for j in range(3):
    for k in range(3):
      if j==k:
        continue
      dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + X[i][k])	
ans=0
for j in range(3):
  ans=max(ans,dp[N][j])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
abcl = []
for _ in range(n):
   a,b,c = map(int,input().split())
   abcl.append((a,b,c))
dp = [[float("inf") for _ in range(3)] for _ in range(n)]

dp[0][0] = abcl[0][0]
dp[0][1] = abcl[0][1]
dp[0][2] = abcl[0][2]

for i in range(n-1):
    a,b,c = abcl[i+1]
    dp[i+1][0] = max(dp[i][1]+a, dp[i][2]+a)
    dp[i+1][1] =  max(dp[i][0]+b, dp[i][2]+b)
    dp[i+1][2] =  max(dp[i][0]+c, dp[i][1]+c)

print(max(dp[-1][0], dp[-1][1], dp[-1][2]))

Submission status: Accepted

Solution code:
# その日に行なった活動も保持しておく
N = int(input())
vacation = [list(map(int, input().split())) for _ in range(N)]
dp = [[0]*3 for _ in range(N+1)]
# テーブル上で真下には遷移できないだけ
i = 1
dp[0][0] = vacation[0][0]
dp[0][1] = vacation[0][1]
dp[0][2] = vacation[0][2]
for a, b, c in vacation[1:]:
    dp[i][0] = max(dp[i-1][1]+a, dp[i-1][2]+a)
    dp[i][1] = max(dp[i-1][0]+b, dp[i-1][2]+b)
    dp[i][2] = max(dp[i-1][0]+c, dp[i-1][1]+c)
    i += 1

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

Submission status: Accepted

Solution code:
def examA():
    N = I()
    H = LI()
    dp = [inf]*(N)
    dp[0] = 0
    for i in range(N-1):
        dp[i+1] = min(dp[i+1],dp[i]+abs(H[i+1]-H[i]))
        if i<N-2:
            dp[i + 2] = min(dp[i + 2], dp[i]+abs(H[i + 2] - H[i]))
    ans = dp[-1]
    print(ans)
    return

def examB():
    N, K = LI()
    H = LI()
    dp = [inf]*N
    dp[0] = 0
    for i in range(N):
        for k in range(1,K+1):
            if i+k>=N:
                break
            if dp[i+k]>dp[i]+abs(H[i+k]-H[i]):
                dp[i+k] = dp[i]+abs(H[i+k]-H[i])
    ans = dp[-1]
    print(ans)
    return

def examC():
    N = I()
    A = [0]*N; B = [0]*N; C = [0]*N
    for i in range(N):
        A[i],B[i],C[i] = LI()
    dp = [[0]*3 for _ in range(N+1)]
    for i in range(N):
        dp[i+1][0] = max(dp[i][1],dp[i][2]) + A[i]
        dp[i + 1][1] = max(dp[i][0], dp[i][2]) + B[i]
        dp[i + 1][2] = max(dp[i][1], dp[i][0]) + C[i]
    ans = max(dp[N])
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
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 LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examC()

"""

"""

Submission status: Accepted

Solution code:
N=int(input())
X=[list(map(int,input().split())) for i in range(N)]
A=[0]
B=[0]
C=[0]
for i in range(N):
  a=X[i][0]+max(B[-1],C[-1])
  b=X[i][1]+max(A[-1],C[-1])
  c=X[i][2]+max(B[-1],A[-1])
  A+=[a]
  B+=[b]
  C+=[c]
print(max(A[-1],B[-1],C[-1]))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    hpy = []
    for _ in range(n):
        tmp = list(map(int,input().split()))
        hpy.append(tmp)
    # a:0, b:1, c:2
    dp = [[0] * 3 for i in range(n) ]
    dp[0][0] = hpy[0][0]
    dp[0][1] = hpy[0][1]
    dp[0][2] = hpy[0][2]
    for i, abc in enumerate(dp):
        if i ==n-1:
            break
        a = max(abc[1], abc[2]) + hpy[i+1][0]
        b = max(abc[2], abc[0]) + hpy[i+1][1]
        c = max(abc[0], abc[1]) + hpy[i+1][2]
        dp[i+1][0] = a
        dp[i+1][1] = b
        dp[i+1][2] = c
    print(max(dp[n-1]))


if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
l=[list(map(int,input().split())) for i in range(n)]

dp=[[-1,-1,-1] for i in range(n)]
dp[0]=[l[0][0],l[0][1],l[0][2]]

for i in range(1,n):
 for j in range(3):
  for k in range(3):
   if j==k:
    continue
   dp[i][j]=max(dp[i][j],dp[i-1][k]+l[i][j])

print(max(dp[-1]))

Submission status: Accepted

Solution code:
N = int(input())
S = []
for _ in range(N):
    D = list(map(int, input().split()))
    S.append(D)

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

for i in range(1,N+1):
    for j in range(3):
        for k in range(3):
            if j != k:
                happy =  dp[i-1][k] + S[i-1][j]
                if dp[i][j] < happy:
                    dp[i][j] = happy

print(int(max(dp[N])))

Submission status: Accepted

Solution code:
N = int(input())
S = []
for _ in range(N):
    D = list(map(int, input().split()))
    S.append(D)

import numpy as np
#dp=np.zeros((N+1,3))

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

for i in range(1,N+1):
    for j in range(3):
        for k in range(3):
            if j != k:
                happy =  dp[i-1][k] + S[i-1][j]
                if dp[i][j] < happy:
                    dp[i][j] = happy

print(int(max(dp[N])))

Submission status: Accepted

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

dp = [[0 for i in range(N)] for j in range(3)]
dp[0][0] = A[0]
dp[1][0] = B[0]
dp[2][0] = C[0]

# for i in range(N):
#     print(dp[i])

for i in range(1,N):
#     for j in range(N):
#         print(dp[j])
#     print()    i+1 = 今日
    dp[0][i] = max(dp[1][i-1]+A[i],dp[2][i-1]+A[i])
    dp[1][i] = max(dp[0][i-1]+B[i],dp[2][i-1]+B[i])
    dp[2][i] = max(dp[0][i-1]+C[i],dp[1][i-1]+C[i])
    
print(max(dp[0][-1],dp[1][-1],dp[2][-1]))

Submission status: Accepted

Solution code:
#python3

def main():
    n = int(input())
    a = []
    for _ in range(n):
        x, y, z = map(int, input().split())
        a.append((x, y, z))

    dp = [[0 for _ in range(3)] for _ in range(10*n)]

    for i in range(n):
        for j in range(3):
            for k in range(3):
                if j == k:
                    continue
                dp[i+1][j] = max(dp[i+1][j], dp[i][k] + a[i][j])

    ans = max(dp[n])
    print(ans)
main()

Submission status: Accepted

Solution code:
N = int(input())
H = [[int(x) for x in input().split()] for _ in range(N)]
dp = [[0]*3 for _ in range(N)]
dp[0][0] = H[0][0]
dp[0][1] = H[0][1]
dp[0][2] = H[0][2]
for i in range(N-1):
    dp[i+1][0] = max(dp[i][1] + H[i+1][0], dp[i][2] + H[i+1][0])
    dp[i+1][1] = max(dp[i][0] + H[i+1][1], dp[i][2] + H[i+1][1])
    dp[i+1][2] = max(dp[i][0] + H[i+1][2], dp[i][1] + H[i+1][2])

print(max(dp[-1]))