#include <bits/stdc++.h>
using namespace std;
//@formatter:off
#ifdef lol
const bool dbg = true;
#else
const bool dbg = false;
#endif
#define dout if(dbg) cout
#define fin(i, s, n) for (auto i = s; i < n; ++i)
#define fine(i, s, n) for (auto i = s; i <= n; ++i)
#define int int64_t
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define def(x) int x; cin >> x
#define cases def(t); while (t--)
#define cases1 int t = 1; while(t--)
#define all(x) (x).begin(), (x).end()
#define chkmax(a,b) a = max(a,b)
#define chkmin(a,b) a = min(a,b)
using ii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vii = vector<ii>;
using vvii = vector<vector<ii>>;
using ld = long double;
//using bigint = __int128;
#define tct template<class T>
#define tcab template<class A, class B>
tcab ostream &operator<<(ostream &os, pair<A, B> p) { return os << '{' << p.x << ',' << p.y << '}'; }
tct ostream &operator<<(ostream &os, vector<T> v) { os << '['; if (!v.empty()) { os << v[0]; fin(i, 1, v.size()) os << ',' << v[i]; } return os << ']'; }
tcab istream& operator>>(istream& is, pair<A,B>& p) { return is >> p.x >> p.y; }
tct istream& operator>>(istream& is, vector<T>& v) { for(auto& x : v) is >> x; return is; }
#define popcnt(x) __builtin_popcount(x)
//#define sz(x) int(x.size())
int rnd() { return rand()^(rand()<<15); }
int gcd(int a, int b) { return b?gcd(b,a%b):a;}
//@formatter:on
const int maxn = 50;
int a[maxn][maxn],_sum[maxn][maxn];
void calc(int n, int m) {
_sum[0][0] = a[0][0];
fin(i,1,n) _sum[i][0] = _sum[i-1][0] + a[i][0];
fin(i,1,m) _sum[0][i] = _sum[0][i-1] + a[0][i];
fin(i,1,n) fin(j,1,m) _sum[i][j] = _sum[i-1][j]+_sum[i][j-1]-_sum[i-1][j-1]+a[i][j];
}
int sum(int i1, int j1, int i2, int j2) {
return _sum[i2][j2]-(i1?_sum[i1-1][j2]:0)-(j1?_sum[i2][j1-1]:0)+((i1&&j1)?_sum[i1-1][j1-1]:0);
}
int t[maxn][maxn][maxn][maxn];
int solve(int i1, int j1, int i2, int j2) {
int& ans = t[i1][j1][i2][j2];
if(ans>=0) return ans;
if(i1==i2&&j1==j2) return ans=0;
fin(k,i1,i2) {
int op = solve(i1,j1,k,j2)+solve(k+1,j1,i2,j2);
if(ans==-1||op<ans) ans = op;
}
fin(k,j1,j2) {
int op = solve(i1,j1,i2,k)+solve(i1,k+1,i2,j2);
if(ans==-1||op<ans) ans = op;
}
ans += sum(i1,j1,i2,j2);
return ans;
}
int32_t main() {
// freopen("ina.txt","r",stdin);
cin.tie(0)->sync_with_stdio(0);
memset(t,-1,sizeof t);
int n,m;
cin >> n >> m;
fin(i,0,n) fin(j,0,m) cin >> a[i][j];
calc(n,m);
cout << solve(0,0,n-1,m-1) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
49228 KB |
Output is correct |
2 |
Correct |
21 ms |
49132 KB |
Output is correct |
3 |
Correct |
21 ms |
49244 KB |
Output is correct |
4 |
Correct |
20 ms |
49236 KB |
Output is correct |
5 |
Correct |
20 ms |
49236 KB |
Output is correct |
6 |
Correct |
21 ms |
49244 KB |
Output is correct |
7 |
Correct |
22 ms |
49220 KB |
Output is correct |
8 |
Correct |
29 ms |
49228 KB |
Output is correct |
9 |
Correct |
56 ms |
49176 KB |
Output is correct |
10 |
Correct |
42 ms |
49152 KB |
Output is correct |
11 |
Correct |
39 ms |
49244 KB |
Output is correct |
12 |
Correct |
86 ms |
49228 KB |
Output is correct |
13 |
Correct |
133 ms |
49204 KB |
Output is correct |
14 |
Correct |
49 ms |
49240 KB |
Output is correct |
15 |
Correct |
191 ms |
49308 KB |
Output is correct |
16 |
Correct |
32 ms |
49220 KB |
Output is correct |
17 |
Correct |
74 ms |
49260 KB |
Output is correct |
18 |
Correct |
420 ms |
49348 KB |
Output is correct |
19 |
Correct |
609 ms |
49272 KB |
Output is correct |
20 |
Correct |
735 ms |
49268 KB |
Output is correct |