Codeforces 429B Working out

Source: Internet
Author: User

Working outtime limit:2000msmemory limit:262144kbthis problem would be judged onCodeforces. Original id:429b
64-bit integer IO format: %i64d Java class name: (any) 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 awith n lines and m columns. Let number a[ i] [ J] represents the calories burned by performing workout at the cell of Gy M in the I-th line and the J-th column.

Iahub starts with workout located on line1 and column 1. He needs to the finish with Workout  a [ n ] [ m ]. After finishing Workout  a [ i ] [ J ], he can go to workout < Span class= "Tex-span" > 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& nbsp a [ i ] [ J + 1] or  a [ I -1][ J ].

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

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

Input

The first line of the input contains the 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< /C22>]≤105).

Output

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

Sample InputInput
3 3
100 100 100
100 1 100
100 100 100
Output
800
Hint

Iahub would choose exercises a[1][1]→ a[1][2]→ a[2][2]→ a[3][2]→ a[3][] 3]. Iahubina would choose exercises a[3][1]→ a [2][1]→ A [2][2]→ a] [2][3]→< C13>a[1][3].

SourceCodeforces Round #245 (Div. 1) Problem solving: Pay attention to the meeting when two people do not produce that value we can enumerate the meeting point where the upper left corner of the person can go to the right to come to the point of meeting and continue to the right, the lower left corner of the person can move up to meet the point of continued upstream or The upper-left corner of the person moves down to the meeting point and continues to move down, the lower left corner of the person to the right to meet the point to continue to move to the right as the maximum value simple DP
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN =1005;4 intdp[4][maxn][maxn],a[maxn][maxn],n,m;5 intMain () {6      while(~SCANF ("%d%d",&n,&m)) {7Memset (DP,0,sizeofDP);8          for(inti =1; I <= N; ++i)9              for(intj =1; J <= M; ++j)Tenscanf"%d", A[i] +j); One          for(inti =1; I <= N; ++i) A              for(intj =1; J <= M; ++j) -dp[0][I][J] = max (dp[0][i-1][j],dp[0][i][j-1]) +A[i][j]; -          for(inti = n; i >0; --i) the              for(intj =1; J <= M; ++j) -dp[1][I][J] = max (dp[1][i+1][j],dp[1][i][j-1]) +A[i][j]; -          for(inti =1; I <= N; ++i) -              for(intj = m; J >0; --j) +dp[2][I][J] = max (dp[2][i-1][j],dp[2][i][j+1]) +A[i][j]; -          for(inti = n; i >0; --i) +              for(intj = m; J >0; --j) Adp[3][I][J] = max (dp[3][i+1][j],dp[3][i][j+1]) +A[i][j]; at  -         intRET =0; -          for(inti =2; I < n; ++i) -              for(intj =2; J < M; ++j) { -ret = max (ret,dp[0][i-1][J] + dp[3][i+1][J] + dp[1][i][j-1] + dp[2][i][j+1]); -ret = max (ret,dp[0][i][j-1] + dp[3][i][j+1] + dp[1][i+1][J] + dp[2][i-1][j]); in             } -printf"%d\n", ret); to     } +     return 0; -}
View Code

Codeforces 429B Working out

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.