CodeForces 214E - Relay Race(二取方格数)

E. Relay Race
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 n integers 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.

Sample test(s)
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.


==========================


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
const int INF=0x3f3f3f3f;
int f[605][301][301],n;
int a[301][301];

int main()
{
    memset(f,-INF,sizeof(f));
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            scanf("%d",&a[i][j]);
    f[1][1][1]=a[1][1];
    for(int k=2;k<=n+n-1;k++)//第几个对角线
    {
        for(int x1=1;x1<=n;x1++)
        {
            for(int x2=1;x2<=n;x2++)
            {
                int p=max(f[k-1][x1-1][x2-1],f[k-1][x1-1][x2]);
                p=max(f[k-1][x1][x2-1],p);
                p=max(f[k-1][x1][x2],p);
                if(x1!=x2) f[k][x1][x2]=p+a[x1][k+1-x1]+a[x2][k+1-x2];
                else f[k][x1][x2]=p+a[x1][k+1-x1];//相等的时候说明两人在同一位置所以只加一遍
            }
        }
    }
    printf("%d\n",f[n+n-1][n][n]);

    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值