B. Working out

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

Copy

3 3
100 100 100
100 1 100
100 100 100

output

Copy

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的矩阵,一个从(1,1)出发到(n,m),一个从(n,1)出发到(n,m),他们方向只要两个分别不同,一个向右,向下,一个向上,向右。然后他们只能相遇一次(这个很关键),相遇的格子里的值不计算,求两人经过格子的最大值。

思路:

通过分析可知,一个人从上进入,那他必定从下面出,一个从左边进,必定从右边出。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=1e3+100;
LL a[maxn][maxn];
LL dp1[maxn][maxn],dp2[maxn][maxn],dp3[maxn][maxn],dp4[maxn][maxn];
void init(int n,int m)
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            dp1[i][j]=max(dp1[i-1][j]+a[i][j],dp1[i][j-1]+a[i][j]);
        }
    }
    for(int i=n;i>=1;i--)
    {
        for(int j=m;j>=1;j--)
        {
            dp2[i][j]=max(dp2[i+1][j]+a[i][j],dp2[i][j+1]+a[i][j]);
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=m;j>=1;j--)
        {
            dp3[i][j]=max(dp3[i-1][j]+a[i][j],dp3[i][j+1]+a[i][j]);
        }
    }
    for(int i=n;i>=1;i--)
    {
        for(int j=1;j<=m;j++)
        {
            dp4[i][j]=max(dp4[i+1][j]+a[i][j],dp4[i][j-1]+a[i][j]);
        }
    }
    return ;
}
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>a[i][j];
        }
    }
    init(n,m);
    LL ans=-1;
    for(int i=2;i<n;i++)
    {
        for(int j=2;j<m;j++)
        {
            ans=max(ans,dp1[i-1][j]+dp2[i+1][j]+dp3[i][j+1]+dp4[i][j-1]);
            ans=max(ans,dp1[i][j-1]+dp2[i][j+1]+dp3[i-1][j]+dp4[i+1][j]);
        }
    }
    cout<<ans<<endl;
    return 0;
}

 

<think>好的,我现在要解决用户的问题:确定一个使用弱引用的Java程序的正确输出,特别是当垃圾回收可能回收或未回收被引用对象时,预期输出是什么。我需要结合用户提供的引用内容来思考这个问题。 首先,用户提到了三个引用,其中引用[1]解释了弱引用如何使对象在没有强引用时被垃圾回收,但可以通过get()方法重新获得强引用。引用[2]讨论了弱引用队列的作用,用于高效处理被回收的引用。引用[3]则指出Finalize方法执行时间不可控,可能导致资源延迟释放。 接下来,我需要回忆Java中弱引用的基本概念。WeakReference属于java.lang.ref包,当一个对象仅被弱引用引用时,垃圾收集器会回收该对象,尤其是在内存不足的情况下。但回收时间不确定,取决于JVM的具体实现和内存状态。 用户的问题可能涉及两种情况:垃圾回收发生与否。比如,程序可能在调用get()时,弱引用对象是否已被回收,导致返回null或实际对象。 根据引用[1],如果在获取弱引用对象时,没有其他强引用,且垃圾回收已经执行,那么get()会返回null。反之,如果有强引用存在,或垃圾回收未发生,get()会返回对象。 可能的代码示例如下: 1. 创建对象并赋值给强引用。 2. 创建弱引用指向该对象。 3. 显式或隐式地触发垃圾回收。 4. 检查弱引用的get()结果。 但需要注意,调用System.gc()只是建议JVM执行垃圾回收,并不保证立即执行。此外,不同的JVM实现可能有不同的行为,因此输出可能不稳定。 例如,在以下代码中: Object obj = new Object(); WeakReference<Object> weakRef = new WeakReference<>(obj); System.out.println(weakRef.get() != null); // true,因为强引用存在 obj = null; // 移除强引用 System.gc(); // 建议垃圾回收 System.out.println(weakRef.get() != null); // 可能为false,如果回收了 但实际情况可能因JVM是否在此时回收对象而异。因此,正确的输出可能有两种情况,取决于垃圾回收是否执行。 根据引用[2],如果没有使用引用队列,程序可能需要主动检查弱引用是否被回收。而引用[3]提到Finalize方法执行时间不确定,所以对象的实际回收时间也不确定。 因此,回答时需要指出输出取决于垃圾回收是否执行,并可能有两种结果。同时需要强调在代码中无法保证gc()立即执行,因此输出可能不稳定,建议避免依赖此类不确定行为。 另外,用户提供的引用[1]和[2]提到了弱引用的获取和队列处理,可能需要在回答中引用这些内容来说明回收后的处理机制。 总结:正确输出可能有两种情况,取决于垃圾回收是否在检查前回收了对象。需要结合代码示例和引用来解释。</think>### Java弱引用程序的输出分析 在Java中,`WeakReference`用于创建对对象的弱引用。当垃圾回收器运行时,**若对象仅被弱引用指向且内存不足**,该对象会被回收[^1]。程序的输出取决于垃圾回收是否在特定时机执行,以下是关键分析步骤: --- #### 1. 示例代码与行为分析 假设有以下代码: ```java import java.lang.ref.WeakReference; public class WeakRefExample { public static void main(String[] args) { // 创建对象并建立弱引用 Object obj = new Object(); WeakReference<Object> weakRef = new WeakReference<>(obj); System.out.println("Before GC: " + (weakRef.get() != null)); // 输出 true // 移除强引用,仅保留弱引用 obj = null; // 建议执行垃圾回收(不保证立即执行) System.gc(); // 短暂等待以允许GC完成(实际中不可靠) try { Thread.sleep(100); } catch (InterruptedException e) {} System.out.println("After GC: " + (weakRef.get() != null)); // 可能输出 false 或 true } } ``` --- #### 2. 可能输出的两种情况 - **情况1:对象被回收(输出 `false`)** 当垃圾回收器在`System.gc()`调用后执行,且对象`obj`未被其他强引用指向时,弱引用指向的对象会被回收,`weakRef.get()`返回`null`。 - **情况2:对象未被回收(输出 `true`)** 若垃圾回收未执行(如JVM认为内存充足),或对象被其他隐式强引用保留(如调试工具、JVM优化),`weakRef.get()`仍能返回原对象。 --- #### 3. 关键注意事项 1. **`System.gc()`不保证立即回收** 调用`System.gc()`仅是建议JVM执行垃圾回收,实际行为由JVM决定[^3]。 2. **弱引用的不确定性** 程序输出可能因JVM实现、运行环境(如内存压力)或调试模式而不同。 3. **引用队列(ReferenceQueue)的作用** 若需可靠追踪对象回收事件,可结合`ReferenceQueue`监听回收状态[^2]。 --- #### 4. 正确输出结论 程序的输出**可能是以下两种之一**: ``` Before GC: true After GC: false ``` 或 ``` Before GC: true After GC: true ``` 具体结果取决于垃圾回收的实际执行情况。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值