#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define ios ios_base::sync_with_stdio(0);cin.tie(0);
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int gen(int a, int b) {return (ll)rng() % (b - a + 1) + a;}
const int INF = 1e18L;
const int nax = 51;
int memo[nax][nax][nax][nax];
int pref[nax][nax];
int dp(int i, int j, int ii, int jj) {
if(make_pair(i, j) == make_pair(ii, jj)) return 0LL;
int& p = memo[i][j][ii][jj];
if(p != INF) return p;
int sm = pref[ii + 1][jj + 1] - pref[i][jj + 1] - pref[ii + 1][j] + pref[i][j];
int mn = INF;
for(int f = i; f < ii; ++f) {
mn = min(mn, dp(i, j, f, jj) + dp(f + 1, j, ii, jj));
}
for(int f = j; f < jj; ++f) {
mn = min(mn, dp(i, j, ii, f) + dp(i, f + 1, ii, jj));
}
p = mn + sm;
return p;
}
signed main()
{
ios
int n, m; cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m));
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
cin >> a[i][j];
pref[i + 1][j + 1] += a[i][j];
}
}
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
pref[i][j] += pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
}
}
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
for(int ii = i; ii < n; ii++) {
for(int jj = j; jj < m; ++jj) {
memo[i][j][ii][jj] = INF;
}
}
}
}
cout << dp(0, 0, n - 1, m - 1) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
3 ms |
3412 KB |
Output is correct |
9 |
Correct |
7 ms |
4948 KB |
Output is correct |
10 |
Correct |
6 ms |
5844 KB |
Output is correct |
11 |
Correct |
8 ms |
4820 KB |
Output is correct |
12 |
Correct |
16 ms |
10644 KB |
Output is correct |
13 |
Correct |
25 ms |
13652 KB |
Output is correct |
14 |
Correct |
8 ms |
6184 KB |
Output is correct |
15 |
Correct |
30 ms |
15444 KB |
Output is correct |
16 |
Correct |
4 ms |
5332 KB |
Output is correct |
17 |
Correct |
13 ms |
11220 KB |
Output is correct |
18 |
Correct |
92 ms |
25832 KB |
Output is correct |
19 |
Correct |
157 ms |
31828 KB |
Output is correct |
20 |
Correct |
180 ms |
34532 KB |
Output is correct |