B. Working out
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix a with n lines and m columns. Let number a[i][j] represents the calories burned by performing workout at the cell of gym in the i-th line and the j-th column.

Iahub starts with workout located at line 1 and column 1. He needs to finish with workout a[n][m]. After finishing workout a[i][j], he can go to workout a[i + 1][j] or a[i][j + 1]. Similarly, Iahubina starts with workout a[n][1] and she needs to finish with workout a[1][m]. After finishing workout from cell a[i][j], she goes to either a[i][j + 1] or a[i - 1][j].

There is one additional condition for their training. They have to meet in exactly one cell of gym. At that cell, none of them will work out. They will talk about fast exponentiation (pretty odd small talk) and then both of them will move to the next workout.

If a workout was done by either Iahub or Iahubina, it counts as total gain. Please plan a workout for Iahub and Iahubina such as total gain to be as big as possible. Note, that Iahub and Iahubina can perform workouts with different speed, so the number of cells that they use to reach meet cell may differs.

Input

The first line of the input contains two integers n and m (3 ≤ n, m ≤ 1000). Each of the next n lines contains m integers: j-th number from i-th line denotes element a[i][j] (0 ≤ a[i][j] ≤ 105).

Output

The output contains a single number — the maximum total gain possible.

Examples
input
3 3
100 100 100
100 1 100
100 100 100
output
800
Note

Iahub will choose exercises a[1][1] → a[1][2] → a[2][2] → a[3][2] → a[3][3]. Iahubina will choose exercises a[3][1] → a[2][1] → a[2][2] → a[2][3] → a[1][3].

给一个n*m的方格,A从左上角走到右下角,B从左下角走到右上角,路线交叉处的权值不算,问两条路线权值之和最大值。要求:两条路线只在一点交叉。

可以枚举交叉点,求该点到四个角落的权值之和。到每个角的权值都可以dp得到最大值。从左上角顺时针得到0,1,2,3四个方向。

 
两幅图的权值之和分别是: 
dp[i][j-1][0] + dp[i-1][j][1] + dp[i][j+1][2] + dp[i+1][j][3] 
dp[i][j-1][3] + dp[i-1][j][0] + dp[i][j+1][1] + dp[i+1][j][2]

还有需注意的是,交叉点只在(n-2)*(m-2)里面这个矩形里变化(否则一个角上的矩形会不存在)

 #include <bits/stdc++.h>
using namespace std; const int MAXN = ;
__int64 a[MAXN][MAXN];
__int64 dp[MAXN][MAXN][];//0, 1, 2, 3分别代表左上,右上,右下,左下 int main()
{ int n, m;
int i, j; while (~scanf("%d%d", &n, &m)) {
for (i = ; i <= n; ++i) {
for (j = ; j <= m; ++j) {
scanf("%I64d", &a[i][j]);
}
}
memset(dp, , sizeof(dp)); for (i = ; i <= n; ++i) {
for (j = ; j <= m; ++j) {
dp[i][j][] = max(dp[i - ][j][], dp[i][j - ][]) + a[i][j];
}
}
for (i = ; i <= n; ++i) {
for (j = m; j >= ; --j) {
dp[i][j][] = max(dp[i - ][j][], dp[i][j + ][]) + a[i][j];
}
}
for (i = n; i >= ; --i) {
for (j = m; j >= ; --j) {
dp[i][j][] = max(dp[i + ][j][], dp[i][j + ][]) + a[i][j];
}
}
for (i = n; i >= ; --i) {
for (j = ; j <= m; ++j) {
dp[i][j][] = max(dp[i + ][j][], dp[i][j - ][]) + a[i][j];
}
} __int64 ans = ;
for (i = ; i < n; ++i) {
for (j = ; j < m; ++j) {
ans = max(ans, dp[i][j - ][] + dp[i - ][j][] + dp[i][j + ][] + dp[i + ][j][]);
ans = max(ans, dp[i][j - ][] + dp[i - ][j][] + dp[i][j + ][] + dp[i + ][j][]);
}
}
printf("%I64d\n", ans);
} return ;
}

cf 429B Working out(简单dp)的更多相关文章

  1. HDU 1087 简单dp,求递增子序列使和最大

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

  3. codeforces Gym 100500H A. Potion of Immortality 简单DP

    Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...

  4. 简单dp --- HDU1248寒冰王座

    题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...

  5. poj2385 简单DP

    J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit ...

  6. hdu1087 简单DP

    I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     ...

  7. poj 1157 LITTLE SHOP_简单dp

    题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...

  8. hdu 2471 简单DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2571 简单dp, dp[n][m] +=(  dp[n-1][m],dp[n][m-1],d[i][k ...

  9. Codeforces 41D Pawn 简单dp

    题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...

  10. poj1189 简单dp

    http://poj.org/problem?id=1189 Description 有一个三角形木板,竖直立放.上面钉着n(n+1)/2颗钉子,还有(n+1)个格子(当n=5时如图1).每颗钉子和周 ...

随机推荐

  1. 机器学习2—K近邻算法学习笔记

    Python3.6.3下修改代码中def classify0(inX,dataSet,labels,k)函数的classCount.iteritems()为classCount.items(),另外p ...

  2. linux kernel学习笔记-5内存管理_转

    void * kmalloc(size_t size, gfp_t gfp_mask); kmalloc()第一个参数是要分配的块的大小,第一个参数为分配标志,用于控制kmalloc()的行为. km ...

  3. python3读取BJDA药品经营企业数据

    #-*- coding:utf-8 -*- #读取北京FDA的药品经营企业数据 # 20161125 zhangshaohua import re import urllib.request impo ...

  4. Matlab典型论坛

    Matlab典型论坛 http://www.ilovematlab.cn/forum.php?tid=271705&goto=lastpost

  5. 【cocos2dx 3.3】口袋空战5 总结与公布

    打包好的APK:点击下载

  6. asp.net core使用中间件美化开发环境异常页面

    asp.net core系统自带的异常页面色彩给人感觉模糊.朦胧,晕眩! 原版: 美化版 实现思路:(在系统自带异常中间件“DeveloperExceptionPageMiddleware”执行后,调 ...

  7. python并发编程之多进程、多线程、异步和协程

    一.多线程 多线程就是允许一个进程内存在多个控制权,以便让多个函数同时处于激活状态,从而让多个函数的操作同时运行.即使是单CPU的计算机,也可以通过不停地在不同线程的指令间切换,从而造成多线程同时运行 ...

  8. vue路由vue-route

    首先先引入插件 <script src="Vue.js"></script> //vue.js在前面 <script src="vue-ro ...

  9. 九度OJ 1341:艾薇儿的演唱会 (最短路)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:528 解决:241 题目描述: 艾薇儿今天来到了中国,她计划两天后在哈尔滨举行一场个人的演唱会.由于出现了紧急情况,演唱会的举办方要求艾薇儿 ...

  10. 自定义弹窗 VS AlertDialog分享弹窗

    一.摘要 弹窗通常用于提示用户进行某种操作,比如:点击分享按钮,弹窗分享对话框:双击返回按钮,弹窗退出对话框:下载文件,提示下载对话框等等,分享对话框/退出对话框/下载对话框,都可以直接使用Alert ...