CodeForces 762D. Maximum path

You are given a rectangular table 3 × n. Each cell contains an integer. You can move from one cell to another if they share a side.

Find such path from the upper left cell to the bottom right cell of the table that doesn't visit any of the cells twice, and the sum of numbers written in the cells of this path is maximum possible.

Input

The first line contains an integer n (1 ≤ n ≤ 105)  — the number of columns in the table.

Next three lines contain n integers each  — the description of the table. The j-th number in the i-th line corresponds to the cell aij( - 109 ≤ aij ≤ 109) of the table.

Output

Output the maximum sum of numbers on a path from the upper left cell to the bottom right cell of the table, that doesn't visit any of the cells twice.

Examples
input
3
1 1 1
1 -1 1
1 1 1
output
7
input
5
10 10 10 -1 -1
-1 10 10 10 10
-1 10 10 10 10
output
110
Note

The path for the first example:

The path for the second example:

题意:给一个3*n的网格图,找一条从左上到右下的路径使得点权和最大
插头dp?可以做,不过麻烦了。
路径可以向左走,但最多走一格,如果走两格,那存在一种方案使得不向左走是等价的
例如
123
456
789
如果方案是123654789那么也可以走147852369就等效替代掉了
同时向左走一格一定会把这两列的权值都加起来
所以dp[i][j]表示第i列结束,当前在第j行的最大权值,然后记录一个dp[i][3]表示这一列全部选(可以向左走)。
注意dp[i][3]是可以转移到dp[i][0]和dp[i][2]的,因为不向左走的走法仍然选完了全部权值
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 100010;
const long long INF = 1e16;

int a[3][MAXN], n;

long long dp[MAXN][4];

inline long long sum(int x, int l, int r)
{
	if( l > r ) swap( l, r );
	long long ret = 0;
	for( int i = l ; i <= r ; i++ ) ret += a[ i ][ x ];
	return ret;
}

int main()
{
	scanf( "%d", &n );
	for( int i = 0 ; i < 3 ; i++ ) for( int j = 1 ; j <= n ; j++ ) scanf( "%d", &a[ i ][ j ] );
	for( int i = 0 ; i <= n ; i++ ) for( int j = 0 ; j < 4 ; j++ ) dp[ i ][ j ] = -INF;
	dp[ 0 ][ 0 ] = 0;
	for( int i = 1 ; i <= n ; i++ )
	{
		for( int j = 0 ; j < 3 ; j++ )
			for( int k = 0 ; k < 3 ; k++ )
				dp[ i ][ j ] = max( dp[ i ][ j ], dp[ i - 1 ][ k ] + sum( i, j, k ) );
		dp[ i ][ 0 ] = max( dp[ i ][ 0 ], dp[ i - 1 ][ 3 ] + sum( i, 0, 2 ) );
		dp[ i ][ 2 ] = max( dp[ i ][ 2 ], dp[ i - 1 ][ 3 ] + sum( i, 0, 2 ) );
		dp[ i ][ 3 ] = max( dp[ i ][ 3 ], max( dp[ i - 1 ][ 0 ], dp[ i - 1 ][ 2 ] ) + sum( i, 0, 2 ) );
	}
	cout << dp[ n ][ 2 ] << endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值