Educational Codeforces Round 17-D. Maximum path

原题链接

D. Maximum path
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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
向回走两格以上的路径可以转化为不向回走的路径,所以只需要考虑向回走一格的路径即可

#include <iostream>
#include <cstdio> 
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#define maxn 100005
#define INF 1e18
#define MOD 1000000007
typedef long long ll;
using namespace std;

ll dp[3][maxn], num[3][maxn];
ll sum(int i, int j, int k){
	if(j > k)
	 swap(j, k);
	ll p = 0;
	for(int h = j; h <= k; h++)
	 p += num[h][i];
	return p;
}
int main(){
//	freopen("in.txt", "r", stdin);
	int n;
	scanf("%d", &n);
	for(int i = 0; i < 3; i++)
	 for(int j = 1; j <= n; j++){
	  scanf("%I64d", &num[i][j]);
	  dp[i][j] = -INF;
    }
    dp[0][0] = 0;
    dp[0][1] = num[0][1];
    dp[1][1] = (ll)num[0][1] + num[1][1];
    dp[2][1] = (ll)num[0][1] + num[1][1] + num[2][1];
    for(int i = 2; i <= n; i++){
     for(int j = 0; j < 3; j++)
     	for(int k = 0; k < 3; k++){
     	    dp[j][i] = max(dp[j][i], dp[k][i-1] + sum(i, j, k));
     	}
     	ll p = sum(i-1, 0, 2) + sum(i, 0, 2);
     	dp[0][i] = max(dp[0][i], dp[2][i-2] + p);
     	dp[2][i] = max(dp[2][i], dp[0][i-2] + p);
   }
   printf("%I64d\n", dp[2][n]);
   return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值