Codeforces 762D. Maximum path 题目详解+错点记录

这篇博客详细解析了Codeforces上的762D问题,这是一个关于找到从矩形表格左上角到右下角的最大路径和的DP题目。博客内容包括问题描述、输入输出格式、示例、思路、代码实现以及错误记录。作者提到,由于题目要求路径不重复,且表格大小为3×n,因此采用了类似插头DP的解决方法,并提醒注意负数权值的情况。代码部分展示了解决问题的算法,同时指出在定义常量时需要注意类型转换的问题。
摘要由CSDN通过智能技术生成

Codeforces 762D. Maximum path


首先,这是一道DP题,虽然说有简便的做法,但我还是采用了插头DP的方法(不知道是不是插头DP,可能是轮廓线DP)

Problem

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:

这里写图片描述

好了,那么这道题其实就是要求不重复经过某个方格,求从左上角到右下角的路径上权值和最大值。注意本题方格中的权值可以为负

思路

我曾经参加过这次比赛,然而时间太短写不完。我知道本题应该有简便的DP方法,但我觉得正常的思路还是先使用插头DP(还是轮廓线DP)的方法。因为本题是一个窄棋盘(3*n)所以我们可以枚举一个竖列的所有情况,作为参数,进行状态转移

代码

不多说,贴代码(使用这种方法写的代码十分丑。。。)

#include<cstdio>
#include<cstdlib>
#define maxn 100005
#define maxn2 18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值