Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.

At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.

To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.

Print the maximum number of points Furik and Rubik can earn on the relay race.

Input

The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain nintegers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).

Output

On a single line print a single number — the answer to the problem.

Examples

Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136

Note

Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).

Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:

Furik's path is marked with yellow, and Rubik's path is marked with pink.

题目大意:

输入一个n,然后输入一个n*n的矩阵,求两条从左上角到右下角的路所经过的所有的格子里的权值和,求最大和。

#include <bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int dp[][][];///总步数,第一个人向右的步数,第二个人向右的步数
int a[][],n;
int main()
{
cin>>n;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
cin>>a[i][j];
int st=*(n-);
for(int i=;i<=st;i++)
for(int j=;j<=n;j++)
for(int k=;k<=n;k++)
dp[i][j][k]=-INF;
dp[][][]=;
for(int i=;i<st;i++)
for(int j=;j<=i&&j<n;j++)
for(int k=;k<=i&&k<n;k++)
{
int x1=i-j+,y1=j+,x2=i-k+,y2=k+;///x1,y1,x2,y2代表第一个人的位置和第二个人的位置
if(x1+<=n&&x2+<=n)///下下
{
int ans;
if(x1+==x2+&&y1==y2)///相同格子
ans=a[x1+][y1];
else
ans=a[x1+][y1]+a[x2+][y2];
dp[i+][j][k]=max(dp[i+][j][k],dp[i][j][k]+ans);
}
if(x1+<=n&&y2+<=n)///下右
{
int ans;
if(x1+==x2&&y1==y2+)
ans=a[x1+][y1];
else
ans=a[x1+][y1]+a[x2][y2+];
dp[i+][j][k+]=max(dp[i+][j][k+],dp[i][j][k]+ans);
}
if(y1+<=n&&x2+<=n)///右下
{
int ans;
if(x1==x2+&&y1+==y2)
ans=a[x1][y1+];
else
ans=a[x1][y1+]+a[x2+][y2];
dp[i+][j+][k]=max(dp[i+][j+][k],dp[i][j][k]+ans);
}
if(y1+<=n&&y2+<=n)///右右
{
int ans;
if(x1==x2&&y1+==y2+)
ans=a[x1][y1+];
else
ans=a[x1][y1+]+a[x2][y2+];
dp[i+][j+][k+]=max(dp[i+][j+][k+],dp[i][j][k]+ans);
}
}
cout<<dp[st][n-][n-]+a[][]<<'\n';
return ;
}

Relay Race (DP)的更多相关文章

  1. LightOJ1326 Race(DP)

    题目问N匹马比赛有多少种结果.一开始想用排列组合搞搞,然后发现想错了.艰难地把思路转向DP,最后AC了. dp[i][j]表示前i匹马确定出j个名次的方案数 dp[1][1]=1 对于第i匹马,它要确 ...

  2. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  3. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  4. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  5. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  6. 初探动态规划(DP)

    学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...

  7. Tour(dp)

    Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...

  8. 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)

    .navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...

  9. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

随机推荐

  1. Bootstrap基础知识学习

    Bootstrap中文网 http://www.bootcss.com/ Bootstrap菜鸟教程 http://www.runoob.com/bootstrap/bootstrap-tutoria ...

  2. Java虚拟机(JVM),JDK,JRE和JVM的区别——通过示例学习Java编程(2)

    Java虚拟机(JVM),JDK,JRE和JVM的区别 作者:CHAITANYA SINGH 来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid ...

  3. mongodb备份、还原

    1.mongodb  备份 mongodump 命令可以使用可用的选项的列表. 此命令将仅备份指定的数据库,在指定的路径 语法 描述 示例 mongodump --host HOST_NAME --p ...

  4. Sql 行转换为列 以及列转换为行的心得

    这是 创建数据库的脚本文件 CREATE TABLE [dbo].[stu]( [学号] [nvarchar](255) NOT NULL, [姓名] [nvarchar](255) NULL, [性 ...

  5. windows命令行快速启动软件

    windows桌面上太多的应用程序快捷方式很影响美观,于是寻思使用类似Linux系统中命令行的方式来启动软件. 只需要3步: 1.建立一个目录A,用来存放快捷方式.比如,建立D:\path.并复制快捷 ...

  6. nmon各配置项含义介绍

    1)nmon各配置项含义介绍

  7. Android学习总结(十一)———— Adapter的使用

    一.Adapter的基本概念 UI控件都是跟Adapter(适配器)打交道的,了解并学会使用这个Adapter很重要, Adapter是用来帮助填充数据的中间桥梁,简单点说就是:将各种数据以合适的形式 ...

  8. 为什么字符串String是不可变字符串&&"".equals(str)与str.equals("")的区别

    为什么字符串String是不可变字符串 实际上String类的实现是char类型的数组 虽然说源码中设置的是private final char[] value; final关键词表示不可变动 但是只 ...

  9. QT+常用控件_Line Edit

    #include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> #incl ...

  10. c++中的结构体:声明 定义 初始化

    什么是结构体? 之前的学习中我们知道了数组是一个容器,而且是存放固定大小数据的容器,而且存放的元素的数据类型必须要一致. 比如数据库中有这样的一条记录学号 性别 年龄 成绩 地址应该怎样存放 结构体: ...