1195C - Basketball Exercise - CodeForces Solution


dp *1400

Please click on ads to support us..

Python Code:

def main():
    import sys
    def get_ints(): return list(map(int, sys.stdin.readline().strip().split()))
    [n]=get_ints()
    g=[0]+get_ints()
    d=[0]+get_ints()
    zlicz=[[0 for _ in range(n+2)]for _ in range(3)]
    zlicz[1][1]=g[1]
    zlicz[2][1]=d[1]
    zlicz[0][2]=max(g[1],d[1])
    for i in range(2,n+1):
        zlicz[1][i]=g[i]+max(zlicz[2][i-1],zlicz[0][i-1])
        zlicz[2][i]=d[i]+max(zlicz[1][i-1],zlicz[0][i-1])
        zlicz[0][i]=max(zlicz[1][i-1],zlicz[2][i-1])
    print(max(zlicz[0][-2],zlicz[1][-2],zlicz[2][-2]))
main()

C++ Code:

#include <bits/stdc++.h> 
 
#define int long long 
#define pii pair <int,int> 
#define st first 
#define nd second 
#define vi vector <int> 
#define vpii vector <pii> 
#define pb push_back 
#define pf push_front 
#define FOR(a, b, c) for (int i = a; i <= b; i += c) 
#define ROS(a, b, c) for (int i = a; i >= b; i -= c) 
#define lb lower_bound 
#define ub upper_bound 
#define FILE "" 
#define trii pair <int, pii> 
using namespace std;
 
const int oo = 1e18;
const int N = 3e5 + 1;
const int mod = 1e9 + 7;
const int blocks = 448;
 
int n, a[N], b[N];
int dp[N][3];
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	//freopen(FILE".inp","r", stdin);
	//freopen(FILE".out","w", stdout);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	
	for (int i = 1; i <= n; i++) 
		cin >> b[i];
	dp[0][0] = 0;
	dp[0][1] = 0;
	dp[0][2] = 0;
	for (int i = 1; i <= n; i++) 
	{
		dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][1], dp[i - 1][2]));
		dp[i][1] = max(dp[i - 1][0] + a[i], dp[i - 1][2] + a[i]);
		dp[i][2] = max(dp[i - 1][0] + b[i], dp[i - 1][1] + b[i]);
	}
	cout << max(dp[n][0], max(dp[n][1], dp[n][2]));
	return 0;
}


Comments

Submit
0 Comments
More Questions

208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold
1268. Search Suggestions System
841. Keys and Rooms
152. Maximum Product Subarray
337. House Robber III
869. Reordered Power of 2
1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer