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. listView onItemClick失效

    1.先检查list是否设置监听onItemClick事件 2.ListView中有按钮时,会使子项的onItemClick事件无效,如果onItemClick不能触发,在ListView子项目布局文件 ...

  2. 对InitialContext的理解

        类InitialContext java.lang.Object   javax.naming.InitialContext 此类是执行命名操作的初始上下文. 所有命名操作都相对于某一上下文. ...

  3. 0x00000124蓝屏问题解决方法

    windows7-32位系统: 0x00000124蓝屏是系统问题,win7才有的, xp系统没有 . 解决办法:下载win7蓝屏补丁包解压安装就ok了. 说明:win7蓝屏补丁KB(25286140 ...

  4. iphone开发设置默认字体

    It seems to be possible in iOS 5 using the UIAppearance proxy. [[UILabel appearance] setFont:[UIFont ...

  5. ABAP Netweaver, SAP Cloud Platform和Kubernetes的用户区分

    ABAP Dialog: Individual, interactive system access. System: Background processing and communication ...

  6. adhoc无法下载应用程序 此时无法安装-解决

    解决方法 点击xcode,进入build setting,选择code signing,provisioning profile选择automatic 或者选择adhoc的provisioning p ...

  7. Educational Codeforces Round 11 _D

    http://codeforces.com/contest/660/problem/D 这个题据说是很老的题了 然而我现在才知道做法 用map跑了1953ms: 题目大意 给你n个点的坐标 求这些点能 ...

  8. 利用java自带的base64实现加密、解密

    package com.stone.util; import java.io.UnsupportedEncodingException; import sun.misc.*; public class ...

  9. Web性能优化系列:10个JavaScript性能提升的技巧

    由 伯乐在线 - Delostik 翻译,黄利民 校稿.未经许可,禁止转载!英文出处:jonraasch.com.欢迎加入翻译小组. Nicholas Zakas是一位 JS 大师,Yahoo! 首页 ...

  10. Oracle数据库同步方案

    Oracle数据库同步方案 1. 利用数据泵导出每表前2000行数据 expdp tvpay2/tvpay directory=dmp dumpfile=20170508.dmp include=ta ...