Working out (Codeforces 430D)
Tags: Others, dp

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. 

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].

Sample Inputs:
3 3
100 100 100
100 1 100
100 100 100

Sample Outputs:
800

Solution (Python):
from sys import stdin, stdout
input = stdin.readline
(n, m) = map(int, input().split())
l = []
for i in range(n):
    l.append(list(map(int, input().split())))
dp1 = [[None for i in range(m)] for i in range(n)]
dp2 = [[None for i in range(m)] for i in range(n)]
dp3 = [[None for i in range(m)] for i in range(n)]
dp4 = [[None for i in range(m)] for i in range(n)]
for i in range(n):
    for j in range(m):
        if i == 0 and j == 0:
            dp1[i][j] = l[i][j]
        elif i == 0:
            dp1[i][j] = dp1[i][j - 1] + l[i][j]
        elif j == 0:
            dp1[i][j] = dp1[i - 1][j] + l[i][j]
        else:
            dp1[i][j] = max(dp1[i - 1][j], dp1[i][j - 1]) + l[i][j]
for i in range(n - 1, -1, -1):
    for j in range(m):
        if i == n - 1 and j == 0:
            dp2[i][j] = l[i][j]
        elif i == n - 1:
            dp2[i][j] = dp2[i][j - 1] + l[i][j]
        elif j == 0:
            dp2[i][j] = dp2[i + 1][j] + l[i][j]
        else:
            dp2[i][j] = max(dp2[i + 1][j], dp2[i][j - 1]) + l[i][j]
for i in range(n - 1, -1, -1):
    for j in range(m - 1, -1, -1):
        if i == n - 1 and j == m - 1:
            dp3[i][j] = l[i][j]
        elif i == n - 1:
            dp3[i][j] = dp3[i][j + 1] + l[i][j]
        elif j == m - 1:
            dp3[i][j] = dp3[i + 1][j] + l[i][j]
        else:
            dp3[i][j] = max(dp3[i + 1][j], dp3[i][j + 1]) + l[i][j]
for i in range(n):
    for j in range(m - 1, -1, -1):
        if i == 0 and j == m - 1:
            dp4[i][j] = l[i][j]
        elif i == 0:
            dp4[i][j] = dp4[i][j + 1] + l[i][j]
        elif j == m - 1:
            dp4[i][j] = dp4[i - 1][j] + l[i][j]
        else:
            dp4[i][j] = max(dp4[i - 1][j], dp4[i][j + 1]) + l[i][j]
ma = 0
for i in range(n):
    for j in range(m):
        if i == 0 or j == 0 or i == n - 1 or (j == m - 1):
            continue
        else:
            ma = max(ma, dp1[i - 1][j] + dp3[i + 1][j] + dp2[i][j - 1] + dp4[i][j + 1], dp4[i - 1][j] + dp2[i + 1][j] + dp1[i][j - 1] + dp3[i][j + 1])
print(ma)

Solution (Python):
(n, m) = map(int, input().split())
a = [[0] * (m + 1) for _ in range(n + 1)]
dprog = [[[0] * 5 for _ in range(m + 2)] for _ in range(n + 2)]
sol = 0
for i in range(1, n + 1):
    row = list(map(int, input().split()))
    for j in range(1, m + 1):
        a[i][j] = row[j - 1]
for i in range(1, n + 1):
    for j in range(1, m + 1):
        dprog[i][j][0] = max(dprog[i - 1][j][0], dprog[i][j - 1][0]) + a[i][j]
for i in range(n, 0, -1):
    for j in range(1, m + 1):
        dprog[i][j][1] = max(dprog[i + 1][j][1], dprog[i][j - 1][1]) + a[i][j]
for i in range(n, 0, -1):
    for j in range(m, 0, -1):
        dprog[i][j][2] = max(dprog[i + 1][j][2], dprog[i][j + 1][2]) + a[i][j]
for i in range(1, n + 1):
    for j in range(m, 0, -1):
        dprog[i][j][3] = max(dprog[i - 1][j][3], dprog[i][j + 1][3]) + a[i][j]
for i in range(2, n):
    for j in range(2, m):
        sol = max(sol, dprog[i - 1][j][0] + dprog[i][j - 1][1] + dprog[i + 1][j][2] + dprog[i][j + 1][3])
        sol = max(sol, dprog[i][j - 1][0] + dprog[i + 1][j][1] + dprog[i][j + 1][2] + dprog[i - 1][j][3])
print(sol)

Solution (Python):
(n, m) = map(int, input().split())
a = [[0] * (m + 1) for _ in range(n + 1)]
dp = [[[0] * 5 for _ in range(m + 2)] for _ in range(n + 2)]
ans = 0
for i in range(1, n + 1):
    row = list(map(int, input().split()))
    for j in range(1, m + 1):
        a[i][j] = row[j - 1]
for i in range(1, n + 1):
    for j in range(1, m + 1):
        dp[i][j][0] = max(dp[i - 1][j][0], dp[i][j - 1][0]) + a[i][j]
for i in range(n, 0, -1):
    for j in range(1, m + 1):
        dp[i][j][1] = max(dp[i + 1][j][1], dp[i][j - 1][1]) + a[i][j]
for i in range(n, 0, -1):
    for j in range(m, 0, -1):
        dp[i][j][2] = max(dp[i + 1][j][2], dp[i][j + 1][2]) + a[i][j]
for i in range(1, n + 1):
    for j in range(m, 0, -1):
        dp[i][j][3] = max(dp[i - 1][j][3], dp[i][j + 1][3]) + a[i][j]
for i in range(2, n):
    for j in range(2, m):
        ans = max(ans, dp[i - 1][j][0] + dp[i][j - 1][1] + dp[i + 1][j][2] + dp[i][j + 1][3])
        ans = max(ans, dp[i][j - 1][0] + dp[i + 1][j][1] + dp[i][j + 1][2] + dp[i - 1][j][3])
print(ans)