Submission
Status:
[PPPPPPPPTSSSSSS]
Subtask/Task Score:
{0/100}
Score: 0
User: mocngaijakraila
Problemset: อัศวินขี่ม้าขาว
Language: cpp
Time: 1.027 second
Submitted On: 2026-07-18 15:27:01
#include<bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
const int mx = 1e9 + 7;
int arr[1001][1001];
int main() {
cin.tie(0)->sync_with_stdio(0);
int min = 0, hp = mx;
// int y, x; cin >> y >> x;
int N,M; cin >> N >> M;
vector<int> move;
for(int i = 1; i <= N; i++) {
// if(i != 1) move.push_back(1);
for(int j = 1; j <= M; j++) {
// if(j != 1 && j != M) move.push_back(0);
cin >> arr[i][j];
// if(arr[i][j] < min) min = arr[i][j];
}
}
for(int i = 0; i < M-1; i++) move.push_back(0);
for(int i = 0; i < N-1; i++) move.push_back(1);
// sort(all(move));
do{
// for(auto e : move) cout << e << ' ';
// cout << '\n';
int b = 1, a = 1, k = arr[b][a];
if(k < min) min = k;
// cout << arr[b][a] << ' ';
for(int i = 0; i < N + M - 2; i++) {
if(move[i] == 0) {
a++;
}
else b++;
// cout << arr[b][a] << ' ';
k += arr[b][a];
if(k < min) min = k;
// if(abs(k) + 1 < min) min = abs(k) + 1;
}
if(abs(min) < hp) hp = abs(min);
// cout << min << '\n';
min = 0;
// cout << '\n';
}while(next_permutation(all(move)));
cout << hp + 1;
// vector<int> A = {0, 0, 1, 1};
// sort(all(A));
// do{
// for(auto &e : A) cout << e << ' ';
// cout << '\n';
// }while(next_permutation(all(A)));
return 0;
}