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.

Solution (Python):
from copy import *
from sys import *
from math import *
import queue
from collections import defaultdict,Counter,deque

setrecursionlimit(1000000)


def main():
	n = int(input())
	abc = [list(map(int, input().split())) for i in range(n)]
	dp = [[0,0,0]]
	for i in range(1,n+1):
		temp = []
		temp.append(max(dp[i-1][1], dp[i-1][2]) + abc[i-1][0])
		temp.append(max(dp[i-1][0], dp[i-1][2]) + abc[i-1][1])
		temp.append(max(dp[i-1][1], dp[i-1][0]) + abc[i-1][2])
		dp.append(temp)
	print(max(dp[-1]))

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(m,n):
	x = max(m,n)
	y = min(m,n)
	while(x%y!=0):
		z = x%y
		x = y
		y = z
	return y

class Queue():
	
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)

class Stack():
	
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)

main()

Solution (Python):
from copy import *
from sys import *
from math import *
import queue
from collections import defaultdict,Counter,deque

setrecursionlimit(1000000)


def main():
	n = int(input())
	abc = [list(map(int, input().split())) for i in range(n)]
	dp = [[0,0,0]]
	for i in range(1,n+1):
		temp = []
		temp.append(max(dp[i-1][1], dp[i-1][2]) + abc[i-1][0])
		temp.append(max(dp[i-1][0], dp[i-1][2]) + abc[i-1][1])
		temp.append(max(dp[i-1][1], dp[i-1][0]) + abc[i-1][2])
		dp.append(temp)
	print(max(dp[-1]))

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(m,n):
	x = max(m,n)
	y = min(m,n)
	while(x%y!=0):
		z = x%y
		x = y
		y = z
	return y

class Queue():
	
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)

class Stack():
	
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)

main()

Solution (Python):
from copy import *
from sys import *
from math import *
import queue
from collections import defaultdict,Counter,deque

setrecursionlimit(1000000)


def main():
	n = int(input())
	abc = [list(map(int, input().split())) for i in range(n)]
	dp = [[0,0,0]]
	for i in range(1,n+1):
		temp = []
		temp.append(max(dp[i-1][1], dp[i-1][2]) + abc[i-1][0])
		temp.append(max(dp[i-1][0], dp[i-1][2]) + abc[i-1][1])
		temp.append(max(dp[i-1][1], dp[i-1][0]) + abc[i-1][2])
		dp.append(temp)
	print(max(dp[-1]))

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(m,n):
	x = max(m,n)
	y = min(m,n)
	while(x%y!=0):
		z = x%y
		x = y
		y = z
	return y

class Queue():
	
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)

class Stack():
	
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)

main()

Solution (Python):
n = int(input())
grid = [[0 for i in range(3)] for j in range(n)]
dp = [[0 for i in range(3)] for j in range(n)]

for i in range(n):
    grid[i] = list(map(int, input().split(" ")))
    
for i in range(n):
    for j in range(3):
        if i == 0 and j == 0:
            dp[i][j] = grid[0][0]
        elif i == 0:
            dp[i][j] = max(grid[i][j], dp[i][j - 1])
        else:
            if (j == 0):
                dp[i][j] = max(grid[i][j] + dp[i-1][1], grid[i][j] + dp[i-1][2])
            elif (j == 1):
                dp[i][j] = max(grid[i][j] + dp[i-1][0], grid[i][j] + dp[i-1][2])
            else:
                dp[i][j] = max(grid[i][j] + dp[i-1][0], grid[i][j] + dp[i-1][1])
                
        
print(max(dp[n - 1][0], max(dp[n - 1][2], dp[n - 1][1])))

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

DP = [[0 for _ in range(3)] for _ in range(N)]


DP[0] = C[0]

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]+C[i][j])

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

Solution (Python):

N = int(raw_input())
H = []

for _ in xrange(N):
    a, b, c = map(int, raw_input().split())
    H.append([a, b, c])

dp = [[0]*3 for __ in xrange(N)]

for i in xrange(0, N):
    for j in xrange(3):
        if i==0:
            dp[i][j] = H[0][j]
        else:
            dp[i][j] = max(dp[i-1][j-1]+H[i][j], dp[i-1][j-2]+H[i][j])

print max(dp[N-1])

Solution (Python):
n = int(input())
dp=[]
next = list(map(int,input().split()))
dp.append(next)
for i in range(n-1):
    cur=[0,0,0]
    next =list(map(int,input().split()))
    cur[0]=max(next[0]+dp[i][1],next[0]+dp[i][2])
    cur[1] = max(next[1] + dp[i][0], next[1] + dp[i][2])
    cur[2] = max(next[2] + dp[i][0], next[2] + dp[i][1])
    dp.append(cur)
print(max(dp[-1]))

Solution (Python):
n=int(input())
dp=[[0,0,0]]*n
for i in range(n):
  a,b,c=map(int,input().split())
  d=dp[i-1]
  dp[i]=[max(d[1],d[2])+a,max(d[0],d[2])+b,max(d[0],d[1])+c]
print(max(dp[n-1]))

Solution (Python):
import sys
input = sys.stdin.readline
n=int(input())
l=[list(map(int,input().split())) for _ in [0]*n]
dp=[[-float("inf") for _ in [0]*3] for _ in [0]*n]
for i in range(3):
    dp[0][i]=l[0][i]
for i in range(1,n):
    for j in range(3):
        dp[i][j]=max(dp[i-1][(j+1)%3]+l[i][j],dp[i-1][(j+2)%3]+l[i][j])
print(max(dp[n-1][i] for i in range(3)))

Solution (Python):
import sys
input = sys.stdin.readline
n=int(input())
l=[list(map(int,input().split())) for _ in [0]*n]
dp=[[-float("inf") for _ in [0]*3] for _ in [0]*n]
for i in range(3):
    dp[0][i]=l[0][i]
for i in range(1,n):
    for j in range(3):
        dp[i][j]=max(dp[i-1][(j+1)%3]+l[i][j],dp[i-1][(j+2)%3]+l[i][j])
print(max(dp[n-1][i] for i in range(3)))

Solution (Python):
n = int(input())
dp = [[0 for _ in range(3)] for _ in range(n+1)]
abc = []

for i in range(1, n+1):
    abc.append([int(x) for x in input().split()])

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] + abc[i][k])

ans = 0
for i in range(3):
    ans = max(ans, dp[n][i])
    
print(ans)

Solution (Python):
n = int(input())
dp = [[0 for _ in range(3)] for _ in range(n+1)]
abc = []

for i in range(1, n+1):
    abc.append([int(x) for x in input().split()])

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] + abc[i][k])

ans = 0
for i in range(3):
    ans = max(ans, dp[n][i])
    
print(ans)

Solution (Python):


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

happiness = [[0 for i in range(3)] for j in range(N)]

happiness[0] = abc[0]

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

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

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

Solution (Python):
N = int(input())
dp = [[0]*3 for i in range(N+1)]

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

print(max(dp[N]))

Solution (Python):
n = int(input())

fa, fb, fc = 0, 0, 0

for _ in range(n):
    a, b, c = map(int, input().split())
    fa, fb, fc = a + max(fb, fc), b + max(fa, fc), c + max(fa, fb)

print(max(fa, fb, fc))

Solution (Python):
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
dp = [[0 for i in range(3)] for j in range(n)]
dp[0]= a[0]

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

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

Solution (Python):
N = int(input())
dp = [[0]*3 for _ in range(N+1)]
happy = [[0]*3 for _ in range(N)]

for i in range(N):
    a, b, c = [int(s) for s in input().split()]
    happy[i][0] = a
    happy[i][1] = b
    happy[i][2] = c

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

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

Solution (Python):
N = int(input())
for i in range(N):
        a,b,c = map(int,input().split())
        if i == 0:
                preA = a
                preB = b
                preC = c
        else:
                A = max(preB + a, preC + a)
                B = max(preA + b, preC + b)
                C = max(preA + c, preB + c)
                preA = A
                preB = B
                preC = C
print(max(preA, preB, preC))

Solution (Python):
def main():
    N = int(input())
    happiness = []
    for i in range(N):
        happiness.append([int(num) for num in input().split()])

    hap_total = [[0] * 3 for _ in range(N)]
    hap_total[0] = [happiness[0][0], happiness[0][1], happiness[0][2]]


    for i in range(1, N):
        for j in range(3):
            for k in {0, 1, 2} - {j}:
                hap_total[i][j] = max(hap_total[i][j], hap_total[i-1][k] + happiness[i][j])


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


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
ABC = []
for _ in range(N):
        a,b,c = map(int,input().split())
        ABC.append((a,b,c))

dpA = [ABC[0][0]]*N
dpB = [ABC[0][1]]*N
dpC = [ABC[0][2]]*N

for i in range(1, N):
        dpA[i] = max(dpB[i-1] + ABC[i][0], dpC[i-1] + ABC[i][0])
        dpB[i] = max(dpA[i-1] + ABC[i][1], dpC[i-1] + ABC[i][1])
        dpC[i] = max(dpA[i-1] + ABC[i][2], dpB[i-1] + ABC[i][2])
print(max(dpA[N-1], dpB[N-1], dpC[N-1]))

Solution (Python):

import string
import sys
from itertools import chain, dropwhile, takewhile

INF = float("inf")


def read(
    *shape, f=int, it=chain.from_iterable(sys.stdin), whitespaces=set(string.whitespace)
):
    def read_word():
        w = lambda c: c in whitespaces
        nw = lambda c: c not in whitespaces
        return f("".join(takewhile(nw, dropwhile(w, it))))

    if not shape:
        return read_word()
    elif len(shape) == 1:
        return [read_word() for _ in range(shape[0])]
    elif len(shape) == 2:
        return [[read_word() for _ in range(shape[1])] for _ in range(shape[0])]


def readi(*shape):
    return read(*shape)


def readi1(*shape):
    return [i - 1 for i in readi(*shape)]


def readf(*shape):
    return read(*shape, f=float)


def reads(*shape):
    return read(*shape, f=str)


def arr(*shape, fill_value=0):
    if len(shape) == 1:
        return [fill_value] * shape[0]
    elif len(shape) == 2:
        return [[fill_value] * shape[1] for _ in range(shape[0])]


def dbg(**kwargs):
    print(
        ", ".join("{} = {}".format(k, repr(v)) for k, v in kwargs.items()),
        file=sys.stderr,
    )


def main():
    N = readi()
    x = readi(N, 3)
    dp = arr(N + 1, 3)
    for i in range(N):
        for j in range(3):
            dp[i + 1][j] = max(
                dp[i + 1][j], dp[i][(j + 1) % 3] + x[i][j], dp[i][(j + 2) % 3] + x[i][j]
            )
    print(max(dp[N]))


if __name__ == "__main__":
    main()

Solution (Python):

import string
import sys
from itertools import chain, dropwhile, takewhile

INF = float("inf")


def read(
    *shape, f=int, it=chain.from_iterable(sys.stdin), whitespaces=set(string.whitespace)
):
    def read_word():
        w = lambda c: c in whitespaces
        nw = lambda c: c not in whitespaces
        return f("".join(takewhile(nw, dropwhile(w, it))))

    if not shape:
        return read_word()
    elif len(shape) == 1:
        return [read_word() for _ in range(shape[0])]
    elif len(shape) == 2:
        return [[read_word() for _ in range(shape[1])] for _ in range(shape[0])]


def readi(*shape):
    return read(*shape)


def readi1(*shape):
    return [i - 1 for i in readi(*shape)]


def readf(*shape):
    return read(*shape, f=float)


def reads(*shape):
    return read(*shape, f=str)


def arr(*shape, fill_value=0):
    if len(shape) == 1:
        return [fill_value] * shape[0]
    elif len(shape) == 2:
        return [[fill_value] * shape[1] for _ in range(shape[0])]


def dbg(**kwargs):
    print(
        ", ".join("{} = {}".format(k, repr(v)) for k, v in kwargs.items()),
        file=sys.stderr,
    )


def main():
    N = readi()
    x = readi(N, 3)
    dp = arr(N + 1, 3)
    for i in range(N):
        for j in range(3):
            dp[i + 1][j] = max(
                dp[i + 1][j], dp[i][(j + 1) % 3] + x[i][j], dp[i][(j + 2) % 3] + x[i][j]
            )
    print(max(dp[N]))


if __name__ == "__main__":
    main()

Solution (Python):

import string
import sys
from itertools import chain, dropwhile, takewhile

INF = float("inf")


def read(
    *shape, f=int, it=chain.from_iterable(sys.stdin), whitespaces=set(string.whitespace)
):
    def read_word():
        w = lambda c: c in whitespaces
        nw = lambda c: c not in whitespaces
        return f("".join(takewhile(nw, dropwhile(w, it))))

    if not shape:
        return read_word()
    elif len(shape) == 1:
        return [read_word() for _ in range(shape[0])]
    elif len(shape) == 2:
        return [[read_word() for _ in range(shape[1])] for _ in range(shape[0])]


def readi(*shape):
    return read(*shape)


def readi1(*shape):
    return [i - 1 for i in readi(*shape)]


def readf(*shape):
    return read(*shape, f=float)


def reads(*shape):
    return read(*shape, f=str)


def arr(*shape, fill_value=0):
    if len(shape) == 1:
        return [fill_value] * shape[0]
    elif len(shape) == 2:
        return [[fill_value] * shape[1] for _ in range(shape[0])]


def dbg(**kwargs):
    print(
        ", ".join("{} = {}".format(k, repr(v)) for k, v in kwargs.items()),
        file=sys.stderr,
    )


def main():
    N = readi()
    x = readi(N, 3)
    dp = arr(N + 1, 3)
    for i in range(N):
        dp[i + 1][0] = max(dp[i + 1][0], dp[i][1] + x[i][0], dp[i][2] + x[i][0])
        dp[i + 1][1] = max(dp[i + 1][1], dp[i][0] + x[i][1], dp[i][2] + x[i][1])
        dp[i + 1][2] = max(dp[i + 1][2], dp[i][0] + x[i][2], dp[i][1] + x[i][2])
    print(max(dp[N]))


if __name__ == "__main__":
    main()

Solution (Python):
def solve(N, ABCs):
    ans = [ABCs[0]]
    for i in range(1, N):
        ans.append([
            max(ans[i - 1][1], ans[i - 1][2]) + ABCs[i][0],
            max(ans[i - 1][0], ans[i - 1][2]) + ABCs[i][1],
            max(ans[i - 1][0], ans[i - 1][1]) + ABCs[i][2],
        ])
    return max(ans[-1])

if __name__ == "__main__":
    N = int(input())
    ABCs = [tuple(map(int, input().split(" "))) for _ in range(N)]
    print(solve(N, ABCs))

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]
N = int(input())
abcN = [list(map(int, input().split())) for i in range(N)]

dp = [[0]*3 for _ in range(N)]
dp[0][0] = abcN[0][0]
dp[0][1] = abcN[0][1]
dp[0][2] = abcN[0][2]

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

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

Solution (Python):
MAX_VAL = 9999999999
MIN_VAL = 0
n = int(input())
abc = []
for i in range(n):
    abc.append(list(map(int, input().split())))
c0 = [[x, MIN_VAL, MIN_VAL] for x in abc[0]]
for i in range(1, n):
    c1 = [max(x) for x in c0]
    for j in range(3):
        for k in range(3):
            if j != k:
                c0[j][k] = abc[i][j] + c1[k]
            else:
                c0[j][k] = MIN_VAL
print(max([max(x) for x in c0]))

Solution (Python):
import numpy as np

N = int(input())

A = np.zeros([N, 3], dtype='int32')

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

dp = np.zeros([N+1, 3], dtype='int32')

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

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

Solution (Python):
def solve():
  N = int(input())
  
  activity = []
  for i in range(N):
    a, b, c = [int(j) for j in input().split()]
    activity.append((a, b, c))

  maximumHappiness = [[0 for i in range(3)] for j in range(N)]

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

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

solve()

Solution (Python):
n = int(input())
a, b, c = [],[],[]
dp = [[0 for i in range(3)] for j in range(n)]

for i in range(n):
    v1, v2, v3 = map(int, input().split())
    a.append(v1)
    b.append(v2)
    c.append(v3)

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] + a[i], dp[i-1][2] + a[i])
    dp[i][1] = max(dp[i-1][0] + b[i], dp[i-1][2] + b[i])
    dp[i][2] = max(dp[i-1][0] + c[i], dp[i-1][1] + c[i])





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

Solution (Python):
if __name__ == '__main__':
    n = int(input())
    abc = [list(map(int, input().split())) for _ in range(n)]

    dp_a, dp_b, dp_c = 0, 0, 0

    for a, b, c in abc:
        ai = max(dp_b, dp_c) + a
        bi = max(dp_a, dp_c) + b
        ci = max(dp_a, dp_b) + c
        dp_a, dp_b,dp_c = ai, bi, ci

    print(max(dp_a, dp_b, dp_c))

Solution (Python):
n = int(input())

dpa, dpb, dpc = 0, 0, 0

for i in range(n):
    a, b, c = map(int, input().split())
    dpa, dpb, dpc = max(dpb, dpc) + a, max(dpa, dpc) + b, max(dpa, dpb) + c

print(max(dpa, dpb, dpc))

Solution (Python):
from functools import lru_cache
import sys

sys.setrecursionlimit(10**8)

N = int(input())
ABC = [list(map(int, input().split(" "))) for _ in range(N)]

l = [[None for _ in range(3)] for _ in range(N+10)]

def dp(i, use):
    if i == -1:
        return 0
    if l[i][use] != None:
        return l[i][use]
    max_num = 0
    for k in range(3):
        if k == use:
            continue
        max_num = max(max_num, dp(i-1, k)+ABC[i][use])
    l[i][use] = max_num 
    return max_num

print(max(dp(N-1, m) for m in range(3)))

Solution (Python):
import sys

n = int(sys.stdin.readline())
values = []

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


for i in range(1,n+1):
    values = list(map(int, sys.stdin.readline().split()))
    
    if i == 1:
        dp[1] = values
        
    else:
        dp[i][0] = max(dp[i-1][1], dp[i-1][2]) + values[0]
        dp[i][1] = max(dp[i-1][0], dp[i-1][2]) + values[1]
        dp[i][2] = max(dp[i-1][0], dp[i-1][1]) + values[2]
        
print(max(dp[n]))

Solution (Python):
N = int(input())
wa, wb, wc = [0] * 3
for a, b, c in (map(int, input().split()) for _ in range(N)):
  wa, wb, wc = a + max(wb, wc), b + max(wa, wc), c + max(wa, wb)
print(max(wa, wb, wc))

Solution (Python):
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] * (N + 1),[0] * (N + 1),[0] * (N + 1)]

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

Solution (Python):

import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math







def main():
	N = int(input())
	abc = []
	for i in range(N):
		a,b,c = map(int, input().split(" "))
		abc.append((a,b,c))
		
	DP = [[0 for _ in range(3)] for _ in range(N)]
	
	for i, val in enumerate(abc):
		a, b, c = val
		if i == 0:
			DP[i][0] = a
			DP[i][1] = b
			DP[i][2] = c
			continue
		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)
		
	print(max(DP[N-1][0], DP[N-1][1], DP[N-1][2]))
	
	
	
if __name__ == "__main__":
	main()

Solution (Python):

import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math







def main():
	N = int(input())
	abc = []
	for i in range(N):
		a,b,c = map(int, input().split(" "))
		abc.append((a,b,c))
		
	DP = [[0 for _ in range(3)] for _ in range(N)]
	
	for i, val in enumerate(abc):
		a, b, c = val
		if i == 0:
			DP[i][0] = a
			DP[i][1] = b
			DP[i][2] = c
			continue
		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)
		
	print(max(DP[N-1][0], DP[N-1][1], DP[N-1][2]))
	
	
	
if __name__ == "__main__":
	main()

Solution (Python):

def vac(l,n):
    dp=[[0 for i in range(3)]for j in range(n+1)]
    for i in range(1,n+1):
        x=l[i-1]
        for j in range(3):
            for k in range(3):
                if(k==j):
                    continue
                else:
                    dp[i][j]=max(dp[i][j],dp[i-1][k]+x[k])
    m=max(dp[n][0],max(dp[n][1],dp[n][2]))
    
    return m

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


print(vac(l,n))

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

dp = [[0] * N , [0] * N, [0] * N]

for i, (a, b, c) in enumerate(lst):
    if i == 0:
        dp[0][0] = a
        dp[1][0] = b
        dp[2][0] = c
    else:
        a_ob = dp[0][i-1]
        b_ob = dp[1][i-1]
        c_ob = dp[2][i-1]
        dp[0][i] = max(b_ob + a, c_ob + a)
        dp[1][i] = max(a_ob + b, c_ob + b)
        dp[2][i] = max(a_ob + c, b_ob + c)
        
print(max(dp[0][-1], dp[1][-1], dp[2][-1]))

Solution (Python):
N = int(input())
abc = list(list(map(int,input().split()))for i in range(N))
DP = [[0]*3 for i in range(N+1)]
for i in range(N):
    for j in range(3):
        DP[i+1][j] = max(DP[i][j-1],DP[i][j-2])+abc[i][j]
print(max(DP[-1]))

Solution (Python):
n = int(input())
ls = [[int(x) for x in input().split()] for _ in range(n)]

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

for i in range(1,n+1):

    dp[i][0] = ls[i-1][0] + max(dp[i-1][1],dp[i-1][2])
    dp[i][1] = ls[i-1][1] + max(dp[i-1][0],dp[i-1][2])
    dp[i][2] = ls[i-1][2] + max(dp[i-1][0],dp[i-1][1])

print(max(dp[n]))

Solution (Python):


def solve():
    n = int(input())
    dpa = [0] * n
    dpb = [0] * n
    dpc = [0] * n
    
    a, b, c = list(map(int, input().split()))
    dpa[0] = a
    dpb[0] = b
    dpc[0] = c
    for i in range(1, n):
        a, b, c = list(map(int, input().split()))
        dpa[i] = max(dpb[i-1] + a, dpc[i-1] + a)
        dpb[i] = max(dpa[i-1] + b, dpc[i-1] + b)
        dpc[i] = max(dpb[i-1] + c, dpa[i-1] + c)
    return(max(dpa[-1], dpb[-1], dpc[-1]))

if __name__ == "__main__":
    print(solve())

Solution (Python):
n = int(input())
m = [[int(i) for i in input().split()] for j in range(n)]
dp = [[0]*3 for i in range(n)]

dp[0][0] = m[0][0]
dp[0][1] = m[0][1]
dp[0][2] = m[0][2]

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

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

Solution (Python):
N = int(input())
abc = [[0 for i in range(3)] for j in range(N)]
for i in range(N):
    a, b, c = map(int, input().split())
    abc[i] = a, b, c

dp = [[0 for i in range(3)] for j 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] + abc[i][k])

res = 0
for i in range(3):
    res = max(res, dp[N][i])

print(res)

Solution (Python):


import sys
def S(): return list(sys.stdin.readline())[:-1]
def LI(): return list(map(int,sys.stdin.readline().split()))
def c():
    n = int(input())
    dpa = [0]*n
    dpb = [0]*n
    dpc = [0]*n

    a,b,c = LI()
    dpa[0] = a
    dpb[0] = b
    dpc[0] = c
    for i in range(1,n):
        a,b,c = LI()
        dpa[i] = max(dpb[i-1] + a,dpc[i-1] + a)
        dpb[i] = max(dpa[i-1] + b,dpc[i-1] + b)
        dpc[i] = max(dpa[i-1] + c,dpb[i-1] + c)
    print(max(dpa[-1],dpb[-1],dpc[-1]))
if __name__ == '__main__':
    c()

Solution (Python):



def c():
    n = int(input())
    dpa = [0]*n
    dpb = [0]*n
    dpc = [0]*n

    a,b,c = list(map(int,input().split()))
    dpa[0] = a
    dpb[0] = b
    dpc[0] = c
    for i in range(1,n):
        a,b,c = list(map(int,input().split()))
        dpa[i] = max(dpb[i-1] + a,dpc[i-1] + a)
        dpb[i] = max(dpa[i-1] + b,dpc[i-1] + b)
        dpc[i] = max(dpa[i-1] + c,dpb[i-1] + c)
    print(max(dpa[-1],dpb[-1],dpc[-1]))
if __name__ == '__main__':
    c()

Solution (Python):


n = int(input())
dpa = [0]*n
dpb = [0]*n
dpc = [0]*n

a,b,c = list(map(int,input().split()))
dpa[0] = a
dpb[0] = b
dpc[0] = c
for i in range(1,n):
    a,b,c = list(map(int,input().split()))
    dpa[i] = max(dpb[i-1] + a,dpc[i-1] + a)
    dpb[i] = max(dpa[i-1] + b,dpc[i-1] + b)
    dpc[i] = max(dpa[i-1] + c,dpb[i-1] + c)
print(max(dpa[-1],dpb[-1],dpc[-1]))

Solution (Python):
N = int(input())
a = [[0] * 3 for _ in range(N)]
for i in range(N):
    a[i][0], a[i][1], a[i][2] = map(int, input().split())

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:
                continue
            
            dp[i][k] = max(dp[i][k], dp[i-1][j] + a[i-1][k])

print(max(dp[N]))

Solution (Python):



def main():
    n = int(input())
    abc = [[0 for _ in range(3)] for _ in range(n)]
    dp = [[0 for _ in range(3)] for _ in range(n + 1)]

    for i in range(n):
        ai, bi, ci = map(int, input().split())
        abc[i][0] = ai
        abc[i][1] = bi
        abc[i][2] = ci

    
    
    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] + abc[i][k])

    print(max(dp[n]))


if __name__ == '__main__':
    main()

Solution (Python):



def main():
    n = int(input())
    abc = [[0 for _ in range(3)] for _ in range(n)]
    dp = [[0 for _ in range(3)] for _ in range(n + 1)]

    for i in range(n):
        ai, bi, ci = map(int, input().split())
        abc[i][0] = ai
        abc[i][1] = bi
        abc[i][2] = ci

    
    
    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] + abc[i][k])

    print(max(dp[n]))


if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict,deque
import numpy as np
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

N = inp()
abc = [inpl() for _ in range(N)]
dp = [[0]*3 for _ in range(N)]
dp[0] = abc[0]

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

print(max(dp[-1]))

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
from functools import reduce
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
_gcd = lambda x, y: _gcd(y, x%y) if (x%y) else y
_lcm = lambda x, y: x*y // _gcd(x, y)
def gcd(*numbers):
    return reduce(_gcd, numbers)
def lcm(*numbers):
    return reduce(_lcm, numbers)
def factorial(n):
    if (n==0) or (n==1):
        return 1
    ret = 1
    for i in range(2, n+1):
        ret *= i
    return ret
def combination(n, k):
    return factorial(n)//factorial(k)//factorial(n-k)
sys.setrecursionlimit(1000000)
mod = 1000000007
dire4 = [(1,0), (0,1), (-1,0), (0,-1)]
dire8 = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]

N = I()
abc = LIR(N)

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

for i in range(N):
    a, b, c = abc[i]
    dp[A][i+1] = max(dp[B][i], dp[C][i]) + a
    dp[B][i+1] = max(dp[A][i], dp[C][i]) + b
    dp[C][i+1] = max(dp[A][i], dp[B][i]) + c

print(max(dp[A][N], dp[B][N], dp[C][N]))