description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
import sys n = int(sys.stdin.readline()) h1 = list(map(int, sys.stdin.readline().split())) h2 = list(map(int, sys.stdin.readline().split())) best_last_up = [0] * (n + 2) best_last_down = [0] * (n + 2) for i in range(n): best_last_up[i + 1] = max(best_last_up[i + 1], best_last_down[i] + h1[i]) best_last_up[i + 2] = max(best_last_up[i + 2], best_last_down[i] + h1[i]) best_last_down[i + 1] = max(best_last_down[i + 1], best_last_up[i] + h2[i]) best_last_down[i + 2] = max(best_last_down[i + 2], best_last_up[i] + h2[i]) print(max(best_last_up[n], best_last_down[n]))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) l1 = list(map(int, input().split())) l2 = list(map(int, input().split())) dp1 = [0] * len(l1) dp2 = [0] * len(l2) dp1[0] = l1[0] dp2[0] = l2[0] if n >= 2: dp1[1] = l2[0] + l1[1] dp2[1] = l1[0] + l2[1] for i in range(2, n): dp1[i] = max(dp2[i - 1], max(dp1[i - 2], dp2[i - 2])) + l1[i] dp2[i] = max(dp1[i - 1], max(dp1[i - 2], dp2[i - 2])) + l2[i] print(max(dp1[-1], dp2[-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
from sys import stdin def solution2(): input = stdin.readline n = int(input()) arr1 = [int(x) for x in input().split()] arr2 = [int(x) for x in input().split()] a = a1 = arr1[0] b = b1 = arr2[0] c = c1 = 0 for i in range(1, n): a1 = max(b + arr1[i], c + arr1[i]) b1 = max(a + arr2[i], c + arr2[i]) c1 = max(a, b) a = a1 b = b1 c = c1 print(max(a1, b1, c1)) solution2()
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) A = [int(i) for i in input().split()] B = [int(i) for i in input().split()] DP = [([0] * n) for _ in range(2)] DP[0][0] = A[0] DP[1][0] = B[0] for i in range(1, n): DP[0][i] = max(A[i] + DP[1][i - 1], DP[0][i - 1]) DP[1][i] = max(B[i] + DP[0][i - 1], DP[1][i - 1]) print(max(DP[0][n - 1], DP[1][n - 1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) row_one = [int(height) for height in input().split(" ")] row_two = [int(height) for height in input().split(" ")] if n == 1: print(max(row_one[0], row_two[0])) elif n == 2: print(max(row_one[0] + row_two[1], row_one[1] + row_two[0])) else: row_one[1] = row_one[1] + row_two[0] row_two[1] = row_two[1] + row_one[0] for col_idx in range(2, n): row_one[col_idx] = row_one[col_idx] + max( row_two[col_idx - 1], row_two[col_idx - 2] ) row_two[col_idx] = row_two[col_idx] + max( row_one[col_idx - 1], row_one[col_idx - 2] ) print(max(row_one[n - 1], row_two[n - 1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) if n == 1: print(max(a[0], b[0])) else: dpa = [0] * n dpb = [0] * n dpa[0] = a[0] dpb[0] = b[0] dpa[1] = dpb[0] + a[1] dpb[1] = dpa[0] + b[1] i = 2 while i < n: dpa[i] = max(dpb[i - 1], dpb[i - 2]) + a[i] dpb[i] = max(dpa[i - 1], dpa[i - 2]) + b[i] i += 1 m1 = max(dpb) m2 = max(dpa) print(max(m1, m2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
import sys num = int(input()) row1 = [int(i) for i in input().split()] row2 = [int(i) for i in input().split()] if num == 1: print(max(row1[0], row2[0])) sys.exit() row1.reverse() row2.reverse() dp = [[row1[0], row2[0]], [row2[0] + row1[1], row1[0] + row2[1]]] for i in range(2, num): top = max(dp[-1][1] + row1[i], dp[-2][1] + row1[i], dp[-1][0]) bot = max(dp[-1][0] + row2[i], dp[-2][0] + row2[i], dp[-1][1]) dp.append([top, bot]) print(max(dp[-1]))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST LIST VAR NUMBER VAR NUMBER LIST BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) h1 = list(map(int, input().split())) h2 = list(map(int, input().split())) x = [0] * (n + 1) y = [0] * (n + 1) x[1] = h1[0] y[1] = h2[0] for i in range(1, n): x[i + 1] = max(y[i], y[i - 1]) + h1[i] y[i + 1] = max(x[i], x[i - 1]) + h2[i] ans = max(x[n], y[n]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) l1 = [int(s) for s in input().split()] l2 = [int(s) for s in input().split()] if n == 1: print(max(l1[0], l2[0])) elif n == 2: print(max(l1[0] + l2[1], l2[0] + l1[0])) else: re1 = [0] * n re2 = [0] * n re1[0] = l1[0] re2[0] = l2[0] re1[1] = re2[0] + l1[1] re2[1] = re1[0] + l2[1] for i in range(2, n): re1[i] = max(re2[i - 1], re2[i - 2]) + l1[i] re2[i] = max(re1[i - 1], re1[i - 2]) + l2[i] print(max(re1[n - 1], re2[n - 1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) li1 = list(map(int, input().split())) li2 = list(map(int, input().split())) max1 = li1[0] max2 = li2[0] for i in range(1, n): max1e = max(max2 + li1[i], max1) max2e = max(max1 + li2[i], max2) max1 = max1e max2 = max2e print(max(max1, max2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) x = list(map(int, input().split())) y = list(map(int, input().split())) if n == 1: print(max(x[0], y[0])) else: x[-2] += y[-1] y[-2] += x[-1] for i in range(3, n + 1): x[-i] += max(y[-i + 1], y[-i + 2]) y[-i] += max(x[-i + 1], x[-i + 2]) print(max(x[0], y[0]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
R = lambda: map(int, input().split()) n = int(input()) a = b = c = d = 0 for x, y in zip(R(), R()): a, b, c, d = c, d, max(x + b, x + d), max(y + a, y + c) print(max(c, d))
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
from sys import stdin def solution(): input = stdin.readline n = int(input()) arr1 = [int(x) for x in input().split()] arr2 = [int(x) for x in input().split()] dp1 = [0] * n dp2 = [0] * n dp3 = [0] * n dp1[0] = arr1[0] dp2[0] = arr2[0] for i in range(1, n): dp1[i] = max(dp2[i - 1] + arr1[i], dp3[i - 1] + arr1[i]) dp2[i] = max(dp1[i - 1] + arr2[i], dp3[i - 1] + arr2[i]) dp3[i] = max(dp1[i - 1], dp2[i - 1]) print(max(dp3[-1], dp2[-1], dp1[-1])) solution()
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) a = input() A = list(map(int, list(a.split()))) b = input() B = list(map(int, list(b.split()))) if n > 2: dp = [[(0) for i in range(n)] for j in range(4)] dp[1][0] = A[0] dp[2][0] = B[0] dp[1][1] = A[1] + dp[2][0] dp[2][1] = B[1] + dp[1][0] dp[3][1] = max(dp[1][0], dp[2][0]) for i in range(2, n): dp[1][i] = max(dp[2][i - 1] + A[i], dp[3][i - 1] + A[i]) dp[2][i] = max(dp[1][i - 1] + B[i], dp[3][i - 1] + B[i]) dp[3][i] = max(dp[1][i - 1], dp[2][i - 1]) print(max(dp[1][n - 1], dp[2][n - 1])) elif n == 1: print(max(A[0], B[0])) else: print(max(A[0] + B[1], A[1] + B[0]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) above = [int(x) for x in input().split()] below = [int(x) for x in input().split()] taken_above = [above[0]] taken_below = [below[0]] taken_none = [0] for i in range(1, n): taken_above.append(max(taken_below[i - 1] + above[i], taken_none[i - 1] + above[i])) taken_below.append(max(taken_above[i - 1] + below[i], taken_none[i - 1] + below[i])) taken_none.append(max(taken_none[i - 1], taken_below[i - 1], taken_above[i - 1])) print(max(taken_above[n - 1], taken_below[n - 1], taken_none[n - 1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) p = [[0] * (n + 1)] + [([0] + list(map(int, input().split()))) for _ in range(2)] t = [([0] * (n + 1)) for _ in range(3)] t[1][1], t[2][1] = p[1][1], p[2][1] for i in range(2, n + 1): t[0][i] = max(t[1][i - 1], t[2][i - 1]) t[1][i] = p[1][i] + max(t[0][i - 1], t[2][i - 1]) t[2][i] = p[2][i] + max(t[0][i - 1], t[1][i - 1]) print(max(t[0][n], t[1][n], t[2][n]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) h = [list(map(int, input().split())) for i in range(2)] for i in range(n - 1): for t in range(2): h[t][i + 1] = max(h[t][i + 1] + h[t ^ 1][i], h[t][i]) print(max(h[0][-1], h[1][-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) heights1 = [int(ele) for ele in input().split(" ")] heights2 = [int(ele) for ele in input().split(" ")] bestdp1 = [0] * n bestdp2 = [0] * n bestdp1[0] = heights1[0] bestdp2[0] = heights2[0] for index in range(1, n): bestdp1[index] = max(bestdp2[max(index - 3, 0) : index]) + heights1[index] bestdp2[index] = max(bestdp1[max(index - 3, 0) : index]) + heights2[index] print(max(max(bestdp1), max(bestdp2)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
def f(l1, l2, index=0, currsum=0, prev=0, memo=dict()): key = index, currsum, prev if key in memo: return memo[key] if index == len(l1): return currsum if prev == 0: to_return = max( f(l1, l2, index + 1, currsum + l1[index], 1, memo), f(l1, l2, index + 1, currsum + l2[index], 2, memo), f(l1, l2, index + 1, currsum, prev, memo), ) elif prev == 2: to_return = max( f(l1, l2, index + 1, currsum + l1[index], 1, memo), f(l1, l2, index + 1, currsum, prev, memo), ) elif prev == 1: to_return = max( f(l1, l2, index + 1, currsum + l2[index], 2, memo), f(l1, l2, index + 1, currsum, prev, memo), ) memo[key] = to_return return to_return def iterative(l1, l2): n = len(l1) dp1 = [0] * n dp2 = [0] * n dp1[0] = l1[0] dp2[0] = l2[0] for i in range(1, n): dp1[i] = max(dp2[i - 1] + l1[i], dp1[i - 1]) dp2[i] = max(dp1[i - 1] + l2[i], dp2[i - 1]) return max(dp1[n - 1], dp2[n - 1]) input() l1 = list(map(int, input().split())) l2 = list(map(int, input().split())) print(iterative(l1, l2))
FUNC_DEF NUMBER NUMBER NUMBER FUNC_CALL VAR ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) h1 = list(map(int, input().split())) h2 = list(map(int, input().split())) if n == 1: print(max(h1[0], h2[0])) exit(0) x1 = [0] * n x2 = [0] * n x1[0] = h1[0] x2[0] = h2[0] x1[1] = x2[0] + h1[1] x2[1] = x1[0] + h2[1] for i in range(2, n): if i % 2: x1[i] = h1[i] + max(x2[i - 1], x2[i - 2]) x2[i] = h2[i] + max(x1[i - 1], x1[i - 2]) else: x1[i] = h1[i] + max(x2[i - 1], x2[i - 2]) x2[i] = h2[i] + max(x1[i - 1], x1[i - 2]) print(max(x1[-1], x2[-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
from sys import stdin, stdout cin = stdin.readline cout = stdout.write n = int(cin()) h1 = list(map(int, cin().split())) h2 = list(map(int, cin().split())) x, y, temp = 0, 0, 0 for i in range(n): x2 = max(y + h1[i], h1[i], temp + h1[i]) y2 = max(x + h2[i], h2[i], temp + h2[i]) temp = max(x, y) x = x2 y = y2 cout(str(max(x, y)))
ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) a = list(map(int, input().split(" "))) b = list(map(int, input().split(" "))) A = a[n - 1] B = b[n - 1] for i in range(n - 2, -1, -1): lA = A lB = B A = max(a[i] + lB, lA) B = max(b[i] + lA, lB) print(max(A, B))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) line_1 = list(map(int, input().split(" "))) line_2 = list(map(int, input().split(" "))) dp_1 = [0] * n dp_1[0] = line_1[0] dp_2 = [0] * n dp_2[0] = line_2[0] dp_3 = [0] * n for i in range(1, n): dp_1[i] = max(dp_2[i - 1] + line_1[i], dp_3[i - 1] + line_1[i], line_1[i]) dp_2[i] = max(dp_1[i - 1] + line_2[i], dp_3[i - 1] + line_2[i], line_2[i]) dp_3[i] = max(dp_1[i - 1], dp_2[i - 1]) print(max(dp_1[n - 1], dp_2[n - 1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) h1 = list(map(int, input().split())) h2 = list(map(int, input().split())) if n == 1: print(max(h1[0], h2[0])) elif n == 2: print(max(h1[0] + h2[1], h1[1] + h2[0])) else: j = 2 c1 = h1[0] + h2[1] c2 = h1[1] + h2[0] c3 = h1[0] c4 = h2[0] for i in range(j, n): c1, c2, c3, c4 = max(c2, c3) + h2[i], max(c1, c4) + h1[i], c2, c1 print(max(c1, c2, c3, c4))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) dp = [0, 0] for i in range(n): v1 = max(dp[0], dp[1] + a[i]) v2 = max(dp[1], dp[0] + b[i]) dp[0], dp[1] = v1, v2 print(max(dp[0], dp[1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) l = [list(map(int, input().split())), list(map(int, input().split()))] m = [0, 0] for i in range(n - 1, -1, -1): mm = [0, 0] for j in range(2): l[j][i] += m[j ^ 1] mm[j] = max(m[j], l[j][i]) m = mm print(max(m))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
N = int(input()) a = [] for i in range(2): a.append(list(map(int, input().split()))) dp = [([0] * 2) for _ in range(N)] for i in range(2): dp[0][i] = a[i][0] for i in range(N): for j in range(2): if i % 2 == 0: if i >= 1: dp[i][j] = max(dp[i][j], dp[i - 1][j] + a[j][i]) if i >= 2: dp[i][j] = max(dp[i][j], dp[i - 2][1 - j] + a[j][i]) else: if i >= 1: dp[i][1 - j] = max(dp[i][1 - j], dp[i - 1][1 - j] + a[j][i]) if i >= 2: dp[i][1 - j] = max(dp[i][1 - j], dp[i - 2][j] + a[j][i]) print(max(dp[N - 1][0], dp[N - 1][1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) team1 = list(map(int, input().split())) team2 = list(map(int, input().split())) team1 = [0, 0] + team1 team2 = [0, 0] + team2 ans1 = [0, 0] ans2 = [0, 0] for i in range(2, n + 2): ans1.append( max(ans1[i - 2] + team1[i], ans2[i - 2] + team1[i], ans2[i - 1] + team1[i]) ) ans2.append( max(ans2[i - 2] + team2[i], ans1[i - 1] + team2[i], ans1[i - 2] + team2[i]) ) print(max(ans1[-1], ans2[-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = 0 row = list() value = list() n = int(input()) row.append(list(map(int, input().split()))) row.append(list(map(int, input().split()))) value.append([0] * n) value.append([0] * n) for i in range(n - 1, -1, -1): if i == n - 1: value[0][i] = row[0][i] value[1][i] = row[1][i] else: value[0][i] = row[0][i] + max(value[1][i + 1 : i + 3]) value[1][i] = row[1][i] + max(value[0][i + 1 : i + 3]) print(max(value[0][0], value[1][0]))
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) str1 = input().split() str2 = input().split() str1 = [int(i) for i in str1] str2 = [int(i) for i in str2] arr1 = str1.copy() arr2 = str2.copy() if n > 1: arr1[1] += arr2[0] arr2[1] += arr1[0] for i in range(2, n): add1 = max(arr2[i - 2], arr2[i - 1]) add2 = max(arr1[i - 2], arr1[i - 1]) arr1[i] += add1 arr2[i] += add2 print(max(arr1[-1], arr2[-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
import sys from itertools import repeat dp = None n = None arr = None def func(ind, row): if ind == n: return 0 if dp[row][ind] != -1: return dp[row][ind] dp[row][ind] = max(func(ind + 1, row), arr[row][ind] + func(ind + 1, row ^ 1)) return dp[row][ind] def solve(): global n, dp, arr n = int(input()) up = [int(x) for x in input().split()] down = [int(x) for x in input().split()] arr = [up, down] dp = [list(repeat(-1, n)), list(repeat(-1, n))] for i in reversed(range(n)): func(i, 0) func(i, 1) print(max(func(0, 0), func(0, 1))) solve()
IMPORT ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) h1 = [int(i) for i in input().split()] h2 = [int(i) for i in input().split()] for i in range(1, n): if i == 1: h1[i] += h2[i - 1] h2[i] += h1[i - 1] else: h1[i] += max(h2[i - 1], h1[i - 2], h2[i - 2]) h2[i] += max(h1[i - 1], h2[i - 2], h1[i - 2]) print(max(h1[-1], h2[-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) arr_0 = [int(i) for i in filter(None, input().split(" "))] arr_1 = [int(i) for i in filter(None, input().split(" "))] arr = [arr_0, arr_1] end = [[0] * n, [0] * n] if n == 1: print(max([arr[0][0], arr[1][0]])) elif n == 2: print(max(arr[0][0] + arr[1][1], arr[1][0] + arr[0][1])) else: for i in range(2 * n): num = i % 2 el_num = 1 - num ind = i // 2 end[num][ind] = max(end[el_num][ind - 1], end[el_num][ind - 2]) + arr[num][ind] print(max(end[0][-1], end[1][-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NONE FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NONE FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) l1 = list(map(int, input().split())) l2 = list(map(int, input().split())) l = [] a = b = 0 for i in range(n): l.append((l1[i], l2[i])) su0, su1 = l[0] for i in range(1, n): a, b, su0, su1 = su0, su1, max(b, su1) + l[i][0], max(a, su0) + l[i][1] print(max(su0, su1))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
R = lambda: map(int, input().split()) n = int(input()) arr = [[], []] arr[0] = [0] + list(R()) arr[1] = [0] + list(R()) dp = [[(0) for i in range(n + 2)] for _ in range(2)] dp[0][1] = arr[0][1] dp[1][1] = arr[1][1] for i in range(2, n + 1): dp[0][i] = max(dp[0][i - 2], dp[1][i - 2], dp[1][i - 1]) + arr[0][i] dp[1][i] = max(dp[1][i - 2], dp[0][i - 2], dp[0][i - 1]) + arr[1][i] print(max(dp[0][n], dp[1][n]))
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST LIST LIST ASSIGN VAR NUMBER BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) a = [list(map(int, input().split())), list(map(int, input().split()))] res = [[a[0][0]], [a[1][0]]] if n > 1: res[0].append(a[1][0] + a[0][1]) res[1].append(a[0][0] + a[1][1]) for i in range(2, n): res[0].append(max(res[1][-1], res[1][-2]) + a[0][i]) res[1].append(max(res[0][-2], res[0][-3]) + a[1][i]) print(max(res[0][-1], res[1][-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST VAR NUMBER NUMBER LIST VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) l = [list(map(int, input().split())) for _ in range(2)] dp = [([0] * 2) for _ in range(n + 1)] for i in range(n): dp[i + 1][0] = max(dp[i][0], dp[i][1] + l[0][i]) dp[i + 1][1] = max(dp[i][1], dp[i][0] + l[1][i]) print(max(dp[n]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
a = int(input()) b = list(map(int, input().split())) c = list(map(int, input().split())) dp = [[] for x in range(a)] for x in range(a): dp[x].append(b[x]) dp[x].append(c[x]) for x in range(a - 1): dp[x + 1][0] = max(dp[x][0], dp[x][1] + dp[x + 1][0]) dp[x + 1][1] = max(dp[x][1], dp[x][0] + dp[x + 1][1]) print(max(dp[a - 1][0], dp[a - 1][1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) f1 = [0] * (n + 3) f2 = [0] * (n + 3) f1[1] = a[0] f2[1] = b[0] for i in range(1, n): f2[i + 1] = max(f2[i], max(f1[i - 1] + b[i], f1[i] + b[i])) f1[i + 1] = max(f1[i], max(f2[i - 1] + a[i], f2[i] + a[i])) print(max(f1[n], f2[n]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) memo = {} def f(a, b, i, is_last_el_a, memo): if (i, is_last_el_a) in memo: return memo[i, is_last_el_a] if i == 0: if is_last_el_a: memo[i, is_last_el_a] = a[0] else: memo[i, is_last_el_a] = b[0] return memo[i, is_last_el_a] if i == 1: if is_last_el_a: memo[i, is_last_el_a] = a[1] + b[0] else: memo[i, is_last_el_a] = b[1] + a[0] return memo[i, is_last_el_a] cur_value = b[i] if is_last_el_a: cur_value = a[i] memo[i, is_last_el_a] = max( f(a, b, i - 1, not is_last_el_a, memo) + cur_value, f(a, b, i - 2, not is_last_el_a, memo) + cur_value, ) return memo[i, is_last_el_a] max_height = -1 for i in range(n): max_height = max(f(a, b, i, True, memo), f(a, b, i, False, memo)) print(max_height)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER IF VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR NUMBER IF VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
import sys def input(): return sys.stdin.readline().strip() def dinput(): return int(input()) def tinput(): return input().split() def rinput(): return map(int, tinput()) def rt(a, s): t = "" for i in range(len(s)): t = t + a[i] + s[i] return int(t) def main(): n = dinput() a = list(rinput()) s = list(rinput()) q1 = [0] * n q2 = [0] * n q1[0] = a[0] q2[0] = s[0] m1 = a[0] m2 = s[0] for i in range(1, n): q1[i] = m2 + a[i] q2[i] = s[i] + m1 if q1[i] > m1: m1 = q1[i] if q2[i] > m2: m2 = q2[i] print(max(q1[-1], q2[-1])) main()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) a1 = [int(x) for x in input().split()] a2 = [int(x) for x in input().split()] dp = [[(0) for i in range(n)] for j in range(2)] dp[0][n - 1] = a1[n - 1] dp[1][n - 1] = a2[n - 1] if n > 1: dp[0][n - 2] = a1[n - 2] + a2[n - 1] dp[1][n - 2] = a2[n - 2] + a1[n - 1] for i in range(n - 3, -1, -1): dp[0][i] = a1[i] + max(dp[1][i + 1], dp[1][i + 2]) dp[1][i] = a2[i] + max(dp[0][i + 1], dp[0][i + 2]) print(max(dp[0][0], dp[1][0]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
from sys import stdin n = int(stdin.readline()) heights = [] heights.append([int(x) for x in stdin.readline().split()]) heights.append([int(x) for x in stdin.readline().split()]) maxes = [([0] * n) for i in range(2)] maxes[0][n - 1] = heights[0][n - 1] maxes[1][n - 1] = heights[1][n - 1] old_top_max = maxes[0][n - 1] top_max = maxes[0][n - 1] bot_max = maxes[1][n - 1] for x in range(n - 2, -1, -1): for y in range(2): if y == 0: maxes[y][x] = max(heights[y][x] + bot_max, top_max) top_max = maxes[y][x] if y == 1: maxes[y][x] = max(heights[y][x] + old_top_max, bot_max) bot_max = maxes[y][x] old_top_max = top_max print(max(maxes[0][0], maxes[1][0]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) h1 = list(map(int, input().split())) h2 = list(map(int, input().split())) l = [h1, h2] dp = [[(0) for i in range(3)] for j in range(n + 1)] for i in range(1, n + 1): dp[i][0] = max(dp[i - 1]) dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + l[0][i - 1] dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + l[1][i - 1] print(max(dp[n]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) l = [[0], [0]] topmax = 0 botmax = 0 l[0].extend(list(map(int, input().split()))) l[1].extend(list(map(int, input().split()))) for i in range(1, n + 1): l[0][i] = l[0][i] + botmax l[1][i] = l[1][i] + topmax topmax = max(topmax, l[0][i]) botmax = max(botmax, l[1][i]) print(max(topmax, botmax))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
def basketball(n, r1, r2): dpi1 = 0 dpi2 = 0 dpi3 = 0 for i in range(n): dpi_1 = dpi1 dpi_2 = dpi2 dpi1 = max(dpi2 + r1[i], dpi3 + r1[i]) dpi2 = max(dpi_1 + r2[i], dpi3 + r2[i]) dpi3 = max(dpi_1, dpi_2) return max(dpi1, dpi2) t = int(input()) a1 = list(map(int, input().split(" "))) a2 = list(map(int, input().split(" "))) print(basketball(t, a1, a2))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) s = [int(i) for i in input().split()] ss = [int(i) for i in input().split()] dp = [0] * n dpp = [0] * n if n > 1: for i in range(0, n): dp[i] = max(dpp[i - 1], dpp[i - 2]) + s[i] dpp[i] = max(dp[i - 1], dp[i - 2]) + ss[i] print(max(dp[-1], dpp[-1])) else: print(max(s[0], ss[0]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) r1 = list(map(int, input().split(" "))) r2 = list(map(int, input().split(" "))) given = [[0, 0] for x in range(n)] array = [[0, 0, 0] for x in range(n)] for i in range(n): given[i][0] = r1[i] given[i][1] = r2[i] if n == 1: print(max(given[0])) if n == 2: print(max(given[0][0] + given[1][1], given[0][1] + given[1][0])) if n > 2: j = given[0].index(max(given[0])) array[0][0] = j array[0][1] = given[0][j] array[0][2] = given[0][(j + 1) % 2] temp = [given[0][1] + given[1][0], given[0][0] + given[1][1]] j = temp.index(max(temp)) array[1][0] = j array[1][1] = temp[j] array[1][2] = temp[(j + 1) % 2] for i in range(2, n): k = 2 if array[i - 1][0] == 0 else 1 l = 2 if array[i - 2][0] == 0 else 1 temp0 = max(given[i][0] + array[i - 1][k], given[i][0] + array[i - 2][l]) k = 2 if array[i - 1][0] == 1 else 1 l = 2 if array[i - 2][0] == 1 else 1 temp1 = max(given[i][1] + array[i - 1][k], given[i][1] + array[i - 2][l]) temp2 = [temp0, temp1] j = temp2.index(max(temp2)) array[i][0] = j array[i][1] = temp2[j] array[i][2] = temp2[(j + 1) % 2] print(array[-1][1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) dp = [a[0], b[0], 0] for i in range(n - 1): dp_1 = max(dp[1], dp[2]) + a[i + 1] dp_2 = max(dp[0], dp[2]) + b[i + 1] dp_3 = max(dp[0], dp[1]) dp = [dp_1, dp_2, dp_3] print(max(dp))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) row1 = list(map(int, input().split())) + [0] row2 = list(map(int, input().split())) + [0] row1[1] += row2[0] row2[1] += row1[0] for i in range(2, n): row1[i] += max(row2[i - 1], row2[i - 2]) row2[i] += max(row1[i - 1], row1[i - 2]) print(max(row1[n - 1], row2[n - 1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
def calc_max_height_team(n, t1, t2): mht = [[(0) for i in range(n)] for j in range(3)] if n == 1: return max(t1[0], t2[0]) mht[0][0] = t1[0] mht[1][0] = t2[0] mht[2][0] = 0 for i in range(1, n): mht[0][i] = max(mht[1][i - 1] + t1[i], mht[2][i - 1] + t1[i]) mht[1][i] = max(mht[0][i - 1] + t2[i], mht[2][i - 1] + t2[i]) mht[2][i] = max(mht[0][i - 1], mht[1][i - 1]) return max(mht[0][n - 1], mht[1][n - 1]) def solve(): n = int(input()) f_t = list(map(lambda x: int(x), input().split())) s_t = list(map(lambda x: int(x), input().split())) print(int(calc_max_height_team(n, f_t, s_t))) solve()
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
CHOSE_A = 0 CHOSE_B = 1 CHOSE_NEITHER = 2 n = int(input()) a = list(map(int, input().split(" "))) b = list(map(int, input().split(" "))) memo = [] for i in range(n): memo.append([-1, -1, -1]) memo[n - 1][CHOSE_B] = a[n - 1] memo[n - 1][CHOSE_A] = b[n - 1] memo[n - 1][CHOSE_NEITHER] = max(a[n - 1], b[n - 1]) for i in range(n - 2, -1, -1): memo[i][CHOSE_A] = max(b[i] + memo[i + 1][CHOSE_B], memo[i + 1][CHOSE_NEITHER]) memo[i][CHOSE_B] = max(a[i] + memo[i + 1][CHOSE_A], memo[i + 1][CHOSE_NEITHER]) memo[i][CHOSE_NEITHER] = max( a[i] + memo[i + 1][CHOSE_A], b[i] + memo[i + 1][CHOSE_B], memo[i + 1][CHOSE_NEITHER], ) print(memo[0][CHOSE_NEITHER])
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) a1 = list(map(int, input().split())) a2 = list(map(int, input().split())) dp = [[0, 0] for j in range(n + 1)] dp[0][0] = a1[0] dp[0][1] = a2[0] if n > 2: dp[1][0] = a1[1] + a2[0] dp[1][1] = a1[0] + a2[1] for i in range(2, n): dp[i][0] = max( dp[i - 1][1] + a1[i], max(dp[i - 2][0], dp[i - 2][1]) + a1[i], dp[i - 1][0] ) dp[i][1] = max( dp[i - 1][0] + a2[i], max(dp[i - 2][1], dp[i - 2][0]) + a2[i], dp[i - 1][1] ) print(max(dp[n - 1][1], dp[n - 1][0]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
import sys n = int(sys.stdin.readline().strip()) h1 = list(map(int, sys.stdin.readline().strip().split())) h2 = list(map(int, sys.stdin.readline().strip().split())) m1 = 0 n1 = h1[0] m2 = 0 n2 = h2[0] for i in range(1, n): m1, n1, m2, n2 = n1, h1[i] + max([m2, n2]), n2, h2[i] + max([m1, n1]) print(max([n1, n2]))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
I = lambda: int(input()) IL = lambda: list(map(int, input().split())) IF = lambda: list(map(float, input().split())) n = I() A = IL() B = IL() S = [A[0], B[0], 0] for i in range(1, n): S = [max(S[1], S[2]) + A[i], max(S[0], S[2]) + B[i], max(S[0], S[1])] print(max(S))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] mat = [[(0) for j in range(2)] for i in range(n)] for i in range(n): mat[i][0] = a[i] mat[i][1] = b[i] dp = [[(0) for j in range(2)] for i in range(n)] dp[0][0] = a[0] dp[0][1] = b[0] for i in range(1, n): dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + a[i]) dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + b[i]) print(max(dp[n - 1][0], dp[n - 1][1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) list1 = list(map(int, input().split())) list2 = list(map(int, input().split())) ans = [[], []] for i in range(n): ans[0].append(0) ans[1].append(0) ans[0][0] = list1[0] ans[1][0] = list2[0] for i in range(1, n): ans[0][i] = max(ans[0][i - 1], list1[i] + ans[1][i - 1]) ans[1][i] = max(ans[1][i - 1], list2[i] + ans[0][i - 1]) print(max(ans[0][-1], ans[1][-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) first = [int(i) for i in input().split()] second = [int(i) for i in input().split()] dp = [[0, 0] for i in range(n)] dp[0][0] = first[0] dp[0][1] = second[0] for i in range(0, n - 1): dp[i + 1][0] = max(dp[i][0], dp[i][1] + first[i + 1]) dp[i + 1][1] = max(dp[i][1], dp[i][0] + second[i + 1]) print(max(dp[n - 1][0], dp[n - 1][1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
def safeaccess(arr, i): if i >= len(arr) or i < 0: return 0 return arr[i] n = int(input()) array1 = [int(x) for x in input().split(" ")] array2 = [int(x) for x in input().split(" ")] dpa1 = [0] * n dpa2 = [0] * n maxnum = 0 for i in range(n): dpa1[i] = max( safeaccess(dpa2, i - 1) + array1[i], safeaccess(dpa2, i - 2) + array1[i] ) dpa2[i] = max( safeaccess(dpa1, i - 1) + array2[i], safeaccess(dpa1, i - 2) + array2[i] ) maxnum = max(maxnum, dpa1[i], dpa2[i]) print(maxnum)
FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) l1 = [int(i) for i in input().split()] l2 = [int(i) for i in input().split()] maxi_haut = [0] maxi_bas = [0] indice = len(l1) - 1 while indice >= 0: tmp = l1[indice] + maxi_bas[-1] maxi_haut.append(max(maxi_haut[-1], tmp)) tmp = l2[indice] + maxi_haut[-2] maxi_bas.append(max(maxi_bas[-1], tmp)) indice -= 1 print(max(maxi_bas[-1], maxi_haut[-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
import sys input = sys.stdin.readline n = int(input()) a1 = list(map(int, input().split())) a2 = list(map(int, input().split())) put1 = [a1[0]] put2 = [a2[0]] if n > 1: put1.append(put2[0] + a1[1]) put2.append(put1[0] + a2[1]) for i in range(2, n): p1 = max(put2[i - 1], put2[i - 2], put2[i - 3]) + a1[i] p2 = max(put1[i - 1], put1[i - 2], put1[i - 3]) + a2[i] put1.append(p1) put2.append(p2) print(max(max(put1), max(put2)))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
mod = 1000000007 ii = lambda: int(input()) si = lambda: input() dgl = lambda: list(map(int, input())) f = lambda: map(int, input().split()) il = lambda: list(map(int, input().split())) ls = lambda: list(input()) n = ii() mx1r, mx2r = 0, 0 mx1, mx2 = 0, 0 for i, j in zip(il(), il()): mx1 = max(mx2r + i, mx1r) mx2 = max(mx1r + j, mx2r) mx1r, mx2r = mx1, mx2 print(max(mx1, mx2))
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
N = int(input()) A = input().split() A = list(map(int, A)) B = input().split() B = list(map(int, B)) dym = [[0, 0] for i in range(N)] for i in range(N): if i == 0: dym[i][0] = A[0] dym[i][1] = B[0] elif i == 1: dym[i][0] = A[1] + B[0] dym[i][1] = A[0] + B[1] else: dym[i][0] = A[i] + max(dym[i - 1][1], dym[i - 2][1]) dym[i][1] = B[i] + max(dym[i - 1][0], dym[i - 2][0]) print(max(dym[-1][0], dym[-1][1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
def solution(x, y): d0 = [0] * len(x) dx = [x[0]] * len(x) dy = [y[0]] * len(x) for k in range(1, len(x)): d0[k] = max(dx[k - 1], dy[k - 1]) dx[k] = max(d0[k - 1], dy[k - 1]) + x[k] dy[k] = max(d0[k - 1], dx[k - 1]) + y[k] return max(d0[-1], dx[-1], dy[-1]) n = int(input()) x = list(map(int, input().split())) y = list(map(int, input().split())) print(solution(x, y))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) s1 = list(map(int, input().split())) s2 = list(map(int, input().split())) if n > 1: s1[1] = s2[0] + s1[1] s2[1] = s1[0] + s2[1] m = max(s1[0], s1[1], s2[0], s2[1]) for i in range(2, n): s1[i] = max(s2[i - 1] + s1[i], s2[i - 2] + s1[i]) s2[i] = max(s1[i - 1] + s2[i], s1[i - 2] + s2[i]) if max(s1[i], s2[i]) > m: m = max(s1[i], s2[i]) else: m = max(s1[0], s2[0]) print(m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) a1 = list(map(int, input().split(" "))) a2 = list(map(int, input().split(" "))) dp1 = [[0, 0] for _ in range(n)] dp2 = [[0, 0] for _ in range(n)] dp1[0][0] = a1[0] dp2[0][0] = a2[0] for i in range(1, n): dp1[i][0] = max([dp1[i - 1][1], dp2[i - 1][0], dp2[i - 1][1]]) + a1[i] dp1[i][1] = max([dp1[i - 1][0], dp1[i - 1][1], dp2[i - 1][0], dp2[i - 1][1]]) dp2[i][0] = max([dp2[i - 1][1], dp1[i - 1][0], dp1[i - 1][1]]) + a2[i] dp2[i][1] = max([dp2[i - 1][1], dp2[i - 1][0], dp1[i - 1][0], dp1[i - 1][1]]) e = n - 1 print(max([dp1[e][0], dp1[e][1], dp2[e][0], dp2[e][1]]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
def solve(n, ar, ar2): dp = [([-1] * 2) for i in range(n)] dp[0][0] = ar[0] dp[0][1] = ar2[0] for i in range(1, n): for j in range(2): if j == 0: dp[i][j] = max(dp[i][j], dp[i - 1][1] + ar[i]) if i > 1: dp[i][j] = max(dp[i][j], dp[i - 2][1] + ar[i]) if j == 1: dp[i][j] = max(dp[i][j], dp[i - 1][0] + ar2[i]) if i > 1: dp[i][j] = max(dp[i][j], dp[i - 2][0] + ar2[i]) print(max(dp[n - 1][0], dp[n - 1][1])) n = int(input()) ar = list(map(int, input().split())) ar2 = list(map(int, input().split())) solve(n, ar, ar2)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) a = input().split() b = input().split() table = [[(0) for i in range(3)] for j in range(n + 1)] for i in range(n - 1, -1, -1): table[i][0] = max(table[i][0], int(a[i]) + max(table[i + 1][1], table[i + 1][2])) table[i][1] = max(table[i][1], int(b[i]) + max(table[i + 1][0], table[i + 1][2])) table[i][2] = max(table[i][2], table[i + 1][0], table[i + 1][1], table[i + 1][2]) print(max(table[0]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) mat = [] mat.append(list(map(int, input().split()))) mat.append(list(map(int, input().split()))) dp = [[(0) for j in range(n)] for i in range(3)] dp[1][0] = mat[0][0] dp[2][0] = mat[1][0] for i in range(1, n): dp[0][i] = max(dp[0][i - 1], dp[1][i - 1], dp[2][i - 1]) dp[1][i] = max(dp[0][i - 1], dp[2][i - 1]) + mat[0][i] dp[2][i] = max(dp[0][i - 1], dp[1][i - 1]) + mat[1][i] res = max(max(dp[0]), max(dp[1]), max(dp[2])) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
x = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) dp1 = [(0) for n in range(x)] dp2 = [(0) for n in range(x)] dp3 = [(0) for n in range(x)] dp1[0] = a[0] dp2[0] = b[0] for n in range(1, x): dp1[n] = max(dp2[n - 1] + a[n], dp3[n - 1] + a[n]) dp2[n] = max(dp1[n - 1] + b[n], dp3[n - 1] + b[n]) dp3[n] = max(dp1[n - 1], dp2[n - 1]) print(max(dp1[-1], dp2[-1], dp3[-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) ar1 = list(map(int, input().split())) ar2 = list(map(int, input().split())) dp1 = [0] * n dp2 = [0] * n dp1[0] = ar1[0] dp2[0] = ar2[0] for i in range(n - 1): if i == n - 2: dp2[i + 1] = max(dp1[i] + ar2[i + 1], dp2[i + 1]) dp1[i + 1] = max(dp2[i] + ar1[i + 1], dp1[i + 1]) else: dp2[i + 1] = max(dp1[i] + ar2[i + 1], dp2[i + 1]) dp1[i + 1] = max(dp2[i] + ar1[i + 1], dp1[i + 1]) dp2[i + 2] = max(dp1[i] + ar2[i + 2], dp2[i + 2]) dp1[i + 2] = max(dp2[i] + ar1[i + 2], dp1[i + 2]) print(max(dp1[-1], dp2[-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
def f(a, b, n): dpa = [0] * n dpb = [0] * n dpa[n - 1] = a[n - 1] dpb[n - 1] = b[n - 1] for i in range(n - 2, -1, -1): dpa[i] = max(a[i] + dpb[i + 1], dpa[i + 1]) dpb[i] = max(b[i] + dpa[i + 1], dpb[i + 1]) return max(dpa[0], dpb[0]) n = int(input().strip()) a = list(map(int, input().strip().split(" "))) b = list(map(int, input().strip().split(" "))) print(f(a, b, n))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
import sys sys.setrecursionlimit(2 * 10**5 + 1) n = int(input()) a = [[int(x) for x in input().split()] for t in range(2)] ans = [0, 0] for i in range(n): ans[0], ans[1] = max(ans[0], ans[1] + a[0][i]), max(ans[1], ans[0] + a[1][i]) print(max(ans))
IMPORT EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
for t in range(1): n = int(input()) row1 = list(map(int, input().split())) row2 = list(map(int, input().split())) dp1 = [0] * n dp1[0] = row1[0] dp2 = [0] * n dp2[0] = row2[0] for i in range(1, n): dp1[i] = max(dp2[i - 1] + row1[i], dp1[i - 1]) dp2[i] = max(dp1[i - 1] + row2[i], dp2[i - 1]) print(max(dp2[-1], dp1[-1]))
FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) h1 = [int(x) for x in input().split()] h2 = [int(x) for x in input().split()] h = [h1, h2] x, y, z = h[0][0], h[1][0], 0 for i in range(1, n): temp1 = x temp2 = y temp3 = z x = max(temp2 + h[0][i], temp3 + h[0][i], h[0][i]) y = max(temp1 + h[1][i], temp3 + h[1][i], h[1][i]) z = max(temp1, temp2) print(max(x, y, z))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) a = input().split(" ") b = input().split(" ") for i in range(n): a[i] = int(a[i]) b[i] = int(b[i]) F = [[0, 0]] * n G = [[0, 0]] * n F[0][0], F[0][1] = G[0][0], G[0][1] = a[0], b[0] for i in range(1, n): F[i][0] = G[i - 1][1] + a[i] F[i][1] = G[i - 1][0] + b[i] G[i][0] = max(G[i - 1][0], F[i][0]) G[i][1] = max(G[i - 1][1], F[i][1]) print(max(G[n - 1][0], G[n - 1][1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST LIST NUMBER NUMBER VAR ASSIGN VAR BIN_OP LIST LIST NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
import sys input = sys.stdin.readline n = int(input()) h0 = list(map(int, input().split())) h1 = list(map(int, input().split())) dp0, dp1 = [0] * (n + 5), [0] * (n + 5) dp0[0], dp1[0] = h0[0], h1[0] for i in range(1, n): dp0[i] = max(dp1[i - 1], dp1[i - 2], dp0[i - 2]) + h0[i] dp1[i] = max(dp0[i - 1], dp1[i - 2], dp0[i - 2]) + h1[i] print(max(max(dp0), max(dp1)))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) arr1 = list(map(int, input().split())) arr2 = list(map(int, input().split())) dp = [[([0] * 2) for _ in range(3)] for _ in range(n)] dp[0][2][0] = arr1[0] dp[0][2][1] = arr2[0] for i in range(1, n): dp[i][0][0] = dp[i - 1][1][0] dp[i][0][1] = dp[i - 1][1][1] dp[i][1][0] = dp[i - 1][2][0] dp[i][1][1] = dp[i - 1][2][1] dp[i][2][0] = max( arr1[i] + dp[i][0][0], arr1[i] + dp[i][0][1], arr1[i] + dp[i][1][1] ) dp[i][2][1] = max( arr2[i] + dp[i][0][0], arr2[i] + dp[i][0][1], arr2[i] + dp[i][1][0] ) print(max(dp[n - 1][2][0], dp[n - 1][2][1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
from sys import stdin, stdout def INI(): return int(stdin.readline()) def INL(): return [int(_) for _ in stdin.readline().split()] def INS(): return stdin.readline() def MOD(): return pow(10, 9) + 7 def OPS(ans): stdout.write(str(ans) + "\n") def OPL(ans): [stdout.write(str(_) + " ") for _ in ans] stdout.write("\n") n = INI() A = INL() B = INL() if n == 1: OPS(max(A[0], B[0])) elif n == 2: OPS(max(A[0] + B[1], B[0] + A[1])) else: X = [A[0], B[0] + A[1]] Y = [B[0], A[0] + B[1]] for _ in range(2, n): X.append(A[_] + max(Y[_ - 1], Y[_ - 2])) Y.append(B[_] + max(X[_ - 1], X[_ - 2])) OPS(max(X[-1], Y[-1]))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) h = [[] for _ in range(2)] h[0] = list(map(int, input().split())) h[1] = list(map(int, input().split())) h[0].insert(0, 0) h[1].insert(0, 0) dp = [[(0) for __ in range(2)] for _ in range(n + 1)] best_edge = [-1] * (n + 1) dp[0][0] = h[0][0] dp[0][1] = h[1][0] dp[1][0] = dp[0][0] + h[0][1] dp[1][1] = dp[0][1] + h[1][1] for i in range(2, n + 1): dp[i][0] = max(dp[i - 1][1] + h[0][i], dp[i - 2][1] + h[0][i]) dp[i][1] = max(dp[i - 1][0] + h[1][i], dp[i - 2][0] + h[1][i]) print(max(dp[n]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = input() row1 = [x for x in map(int, input().split())] row2 = [x for x in map(int, input().split())] def largest_height(row1, row2): largest_r1 = row1[0] largest_r2 = row2[0] for ind in range(1, len(row1)): new_largest_r1 = max(largest_r1, largest_r2 + row1[ind]) new_largest_r2 = max(largest_r2, largest_r1 + row2[ind]) largest_r1, largest_r2 = new_largest_r1, new_largest_r2 return max(largest_r1, largest_r2) print(largest_height(row1, row2))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) h_t = list(map(int, input().split())) h_b = list(map(int, input().split())) dp_t = [0, h_t[0]] dp_b = [0, h_b[0]] if n > 1: for i in range(1, n): dp_t.append(max(dp_b[-1], dp_b[-2]) + h_t[i]) dp_b.append(max(dp_t[-2], dp_t[-3]) + h_b[i]) print(max(dp_t[-1], dp_b[-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) l1 = [int(i) for i in input().split(" ")] l2 = [int(i) for i in input().split(" ")] l = [l1, l2] def find_max_dp(): dp = [[0] * n, [0] * n] dp[0][0] = l[0][0] dp[1][0] = l[1][0] if n > 1: dp[0][1] = l[0][1] + dp[1][0] dp[1][1] = l[1][1] + dp[0][0] for i in range(1, n): dp[1][i] = max(dp[0][i - 1], dp[0][i - 2]) + l[1][i] dp[0][i] = max(dp[1][i - 1], dp[1][i - 2]) + l[0][i] return max(dp[1][n - 1], dp[0][n - 1]) print(find_max_dp())
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST VAR VAR FUNC_DEF ASSIGN VAR LIST BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) h = [None] * 2 h[0] = list(map(int, input().split())) h[1] = list(map(int, input().split())) top_max = h[0][0] bot_max = h[1][0] for col in range(1, n): new_top_max = max(top_max, bot_max + h[0][col]) new_bot_max = max(bot_max, top_max + h[1][col]) top_max = new_top_max bot_max = new_bot_max total_max = max(top_max, bot_max) print(total_max)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
def selectTeam(H1, H2): i = 0 n = len(H1) amax = H1[i] bmax = H2[i] max1 = H1[i] max2 = H2[i] i = 1 while i < n: amax = H1[i] + max2 bmax = H2[i] + max1 max1 = max(max1, amax) max2 = max(max2, bmax) i = i + 1 return max(amax, bmax) n = int(input()) H1 = [int(ele) for ele in input().split()] H2 = [int(ele) for ele in input().split()] print(selectTeam(H1, H2))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) l1 = list(map(int, input().split())) l2 = list(map(int, input().split())) r1 = [[l1[0], l1[0]]] + [[0, 1] for _ in range(n - 1)] r2 = [[l2[0], l2[0]]] + [[0, 1] for _ in range(n - 1)] result1, result2 = 0, 0 for i in range(n): a = result1 result1 = max(result1, result2 + l1[i]) result2 = max(result2, a + l2[i]) print(max(result1, result2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST LIST VAR NUMBER VAR NUMBER LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST LIST VAR NUMBER VAR NUMBER LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
x = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) lol = [] for i in range(2): lol.append([0] * x) lol[0][0] = a[0] lol[1][0] = b[0] if x > 1: lol[0][1] = lol[1][0] + a[1] lol[1][1] = lol[0][0] + b[1] for i in range(2, x): lol[0][i] = max(lol[1][i - 1] + a[i], a[i] + lol[1][i - 2]) lol[1][i] = max(b[i] + lol[0][i - 1], b[i] + lol[0][i - 2]) print(max(lol[0][x - 1], lol[1][x - 1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
def dp_solve(h): n = len(h[0]) memo = {} def solve(r, c): if (r, c) in memo.keys(): return memo[r, c] if c == n - 1: return h[r][c] elif c == n - 2: return h[r][c] + h[1 - r][c + 1] else: m = h[r][c] + max(solve(1 - r, c + 1), solve(1 - r, c + 2)) memo[r, c] = m return m for i in range(n): solve(0, n - 1 - i) solve(1, n - 1 - i) return max(solve(0, 0), solve(1, 0)) n = int(input()) h = [] for i in range(2): h.append([int(x) for x in input().split(" ")]) print(dp_solve(h))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR VAR FUNC_CALL VAR RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
CHOSE_A = 0 CHOSE_B = 1 CHOSE_NEITHER = 2 n = int(input()) a = list(map(int, input().split(" "))) b = list(map(int, input().split(" "))) memo = [] for i in range(n): memo.append([-1, -1, -1]) chose_b = a[n - 1] chose_a = b[n - 1] chose_neither = max(a[n - 1], b[n - 1]) for i in range(n - 2, -1, -1): choose_a = max(b[i] + chose_b, chose_neither) choose_b = max(a[i] + chose_a, chose_neither) choose_neither = max(a[i] + chose_a, b[i] + chose_b, chose_neither) chose_a = choose_a chose_b = choose_b chose_neither = choose_neither print(chose_neither)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) h1 = list(map(int, input().split())) h2 = list(map(int, input().split())) if n > 1: h12 = [0] * n h22 = [0] * n h12[0] = h1[0] h22[0] = h2[0] h12[1] = max(h12[0], h22[0] + h1[1]) h22[1] = max(h22[0], h12[0] + h2[1]) for i in range(2, n): h12[i] = max(h12[i - 1], h22[i - 1] + h1[i]) h22[i] = max(h22[i - 1], h12[i - 1] + h2[i]) h12[i] = max(h12[i], h22[i - 2] + h1[i]) h22[i] = max(h22[i], h12[i - 2] + h2[i]) print(max(h12[-1], h22[-1])) else: print(max(h1[0], h2[0]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) matrix = [] arr = [int(i) for i in input().split()] arr1 = [int(i) for i in input().split()] matrix.append(arr) matrix.append(arr1) dp = [[(0) for i in range(n)] for j in range(2)] dp[0][0] = matrix[0][0] dp[1][0] = matrix[1][0] for i in range(1, n): if i >= 2: dp[0][i] = max(matrix[0][i] + dp[1][i - 1], matrix[0][i] + dp[1][i - 2]) dp[1][i] = max(matrix[1][i] + dp[0][i - 1], matrix[1][i] + dp[0][i - 2]) else: dp[0][i] = matrix[0][i] + dp[1][i - 1] dp[1][i] = matrix[1][i] + dp[0][i - 1] print(max(dp[0][-1], dp[1][-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
def solve(n, queue1, queue2): ans1 = [0] * n ans2 = [0] * n ans1[n - 1] = queue1[n - 1] ans2[n - 1] = queue2[n - 1] for index in range(n - 2, -1, -1): temp = queue1[index] + ans2[index + 1] ans1[index] = max(temp, ans1[index + 1]) temp = queue2[index] + ans1[index + 1] ans2[index] = max(temp, ans2[index + 1]) return max(ans1[0], ans2[0]) n = int(input()) l1 = list(map(int, input().split())) l2 = list(map(int, input().split())) print(solve(n, l1, l2))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) if n == 1: print(max(a[0], b[0])) elif n == 2: print(max(a[0] + b[1], b[0] + a[1])) else: dp1 = [0] * (n + 1) dp2 = [0] * (n + 1) dp0 = [0] * (n + 1) dp1[1] = a[0] dp2[1] = b[0] dp0[1] = 0 dp1[2] = dp2[1] + a[1] dp2[2] = dp1[1] + b[1] dp0[2] = max(a[0], b[0]) for i in range(3, n + 1): m, n = a[i - 1], b[i - 1] dp1[i] = max(dp2[i - 1], dp0[i - 1]) + m dp2[i] = max(dp1[i - 1], dp0[i - 1]) + n dp0[i] = max(dp1[i - 1], dp2[i - 1]) print(max(dp1[-1], dp2[-1], dp0[-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
def is_safe(i, j, n): if i >= 0 and i < n and j >= 0 and j < n: return True return False n = int(input()) mat = [list(map(int, input().split())), list(map(int, input().split()))] dp = [[0] * (n + 1), [0] * (n + 1)] for i in range(n): dp[0][i + 1] = max(dp[1][i] + mat[0][i], dp[0][i]) dp[1][i + 1] = max(dp[0][i] + mat[1][i], dp[1][i]) print(max(dp[0][-1], dp[1][-1]))
FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) num = [] num.append(list(map(int, input().split()))) num.append(list(map(int, input().split()))) value = [] dp = [[(0) for _ in range(n)] for _ in range(2)] dp[0][0] = num[0][0] dp[1][0] = num[1][0] for i in range(1, n): dp[0][i] = max(dp[0][i - 1], num[0][i] + dp[1][i - 1]) dp[1][i] = max(dp[1][i - 1], num[1][i] + dp[0][i - 1]) print(max(dp[0][n - 1], dp[1][n - 1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
maxe = 0 def recur(arr, m, n, flag): global maxe if m >= len(arr): if n > maxe: maxe = n return 0 return recur(arr, m + 1, n, flag) + recur(arr, m + 1, n + arr[m][flag], 1 - flag) n = int(input()) arr1 = list(map(int, input().split())) arr2 = list(map(int, input().split())) arr = [] d = [[(0) for i in range(2)] for j in range(n + 1)] for i in range(n): arr.append([arr1[i], arr2[i]]) for i in range(1, n + 1): for j in range(2): d[i][j] = max(d[i - 1][1 - j] + arr[i - 1][j], d[i - 1][j]) print(max(d[n][0], d[n][1]))
ASSIGN VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
from sys import stdin input = stdin.readline n = int(input()) ar1 = list(map(int, input().split())) ar2 = list(map(int, input().split())) dp = [[0, 0] for i in range(n)] dp[0][0] = ar1[0] dp[0][1] = ar2[0] for i in range(1, n): dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + ar1[i]) dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + ar2[i]) print(max(dp[n - 1]))
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) arr1 = [int(p) for p in input().split()] arr2 = [int(p) for p in input().split()] ans1 = [0] * n ans2 = [0] * n if n == 1: print(max(arr1[0], arr2[0])) exit() ans1[0] = arr1[0] ans2[0] = arr2[0] ans1[1] = arr1[1] + ans2[0] ans2[1] = arr2[1] + ans1[0] for i in range(2, n): ans1[i] = max(ans2[i - 1] + arr1[i], ans2[i - 2] + arr1[i]) ans2[i] = max(ans1[i - 1] + arr2[i], ans1[i - 2] + arr2[i]) print(max(ans1[-1], ans2[-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) left = list(map(int, input().split())) right = list(map(int, input().split())) sequences = [(0, 0)] for i in range(n): new = [] m = {(0): 0, (1): 0, (2): 0} for s in sequences: new.append((0, s[1])) m[0] = max(m[0], s[1]) if not s[0] == 1: new.append((1, s[1] + left[i])) m[1] = max(m[1], s[1] + left[i]) if not s[0] == 2: new.append((2, s[1] + right[i])) m[2] = max(m[2], s[1] + right[i]) sequences = [(0, m[0]), (1, m[1]), (2, m[2])] m = 0 for s in sequences: m = max(m, s[1]) print(m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer β€” the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) a = [list(map(int, input().split())), list(map(int, input().split()))] m = [a[0][-1], a[1][-1]] for i in list(range(n - 1))[::-1]: m[0], m[1] = max(m[0], m[1] + a[0][i]), max(m[1], m[0] + a[1][i]) print(max(m[0], m[1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER