Submission

Status:

[PPPPPPPPTSSSSSS]

Subtask/Task Score:

{0/100}

Score: 0

User: theem1502

Problemset: อัศวินขี่ม้าขาว

Language: cpp

Time: 1.095 second

Submitted On: 2026-02-18 21:57:21

#include <bits/stdc++.h>
using namespace std;
    long long dix[2] = {0,1};
    long long diy[2] = {1,0};
    long long row, collumn;
    const long long INF = 1e18;

    long long longmin(long long a, long long b) {
        if (a > b) {
            return b;
        }
        return a;
    }

        long long longmax(long long a, long long b) {
        if (a > b) {
            return a;
        }
        return b;
    }

long long dfs(long long currentx, long long currenty, long long val, vector<vector<long long>> &thearray) {
    if (currentx  < 0 || currenty < 0 || currentx >= row || currenty >= collumn) {
        return INF;
    }

    val += thearray[currentx][currenty];
    long long thesum = 0;
    if (val <= 0) {
        thesum += -1*val + 1;
        val = 1;

    }
       if (currentx == row -1 && currenty == collumn - 1) {
        return longmax(thesum, 0);
    }
 //   cout << "debug " << currentx << " " << currenty << " " << val << "\n";
    long long minsum = INF;
    for (long long i = 0; i < 2; i++) {
        minsum = longmin(dfs(currentx + dix[i], currenty + diy[i], val, thearray), minsum);
    }

    return minsum + thesum;


}

int main() {

    cin >> row >> collumn;
    vector<vector<long long>> thearray(row, vector<long long> (collumn));
    for (long long i = 0; i < row ;i++) {
        for (long long j = 0; j < collumn; j++) {
            cin >> thearray[i][j];
        }
    }

    cout << longmax( dfs(0,0,0,thearray), 1);


}