1016C - Vasya And The Mushrooms - CodeForces Solution


dp implementation *1800

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
} 
const int N=3e5+5;
int a[2][N],dis[2][N],dis2[2][N],dis3[2][N];
signed main(){
	int n=read();
	for(int i=0;i<2;++i){
		for(int j=1;j<=n;++j){
			a[i][j]=read(); 
		}
	}
	for(int i=1;i<=n;++i){
		for(int j=0;j<2;++j){
			dis[j][i]=dis[j][i-1]+((i%2)==j)*(a[1-j][i-1]*((i-1)*2-((i-1)%2!=1-j)-1)+a[1-j][i]*(i*2-(i%2!=1-j)-1))+a[j][i]*(2*i-(i%2!=j)-1);  
		}
	}
	int mx=max(dis[0][n],dis[1][n]);
	int k=0;
	for(int i=0;i<2;++i){
		for(int j=n;j>0;--j){
			dis2[i][j]=dis2[i][j+1]+a[i][j]*(j-1);
			dis3[i][j]=dis3[i][j+1]+a[i][j]; 
		}
	}
	for(int i=1;i<n;++i){
		int sum=dis[1-i%2][i-1];
		int sum2=dis2[1-i%2][i]+(i-1)*dis3[1-i%2][i];
		int sum3=(2*n-2+i)*dis3[i%2][i]-dis2[i%2][i];
		mx=max(mx,sum+sum2+sum3);
	}
	printf("%lld\n",mx);
	return 0;
}


Comments

Submit
0 Comments
More Questions

468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions