Submission #1034943

#TimeUsernameProblemLanguageResultExecution timeMemory
1034943ArthuroWichRaisins (IOI09_raisins)C++17
100 / 100
135 ms59740 KiB
#include <bits/stdc++.h> using namespace std; #define int long long int int n, m, r[55][55], pref[55][55], dp[55][55][55][55]; void find(int i1, int j1, int i2, int j2) { if (dp[i1][j1][i2][j2] != -1) { return; } dp[i1][j1][i2][j2] = INT_MAX; int v = pref[i2][j2]-pref[i1-1][j2]-pref[i2][j1-1]+pref[i1-1][j1-1]; for (int i = i1; i < i2; i++) { find(i1, j1, i, j2); find(i+1, j1, i2, j2); dp[i1][j1][i2][j2] = min(dp[i1][j1][i2][j2], dp[i1][j1][i][j2]+dp[i+1][j1][i2][j2]+v); } for (int j = j1; j < j2; j++) { find(i1, j1, i2, j); find(i1, j+1, i2, j2); dp[i1][j1][i2][j2] = min(dp[i1][j1][i2][j2], dp[i1][j1][i2][j]+dp[i1][j+1][i2][j2]+v); } } void solve() { cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { for (int x = 1; x <= m; x++) { for (int y = 1; y <= m; y++) { dp[i][x][j][y] = -1; } } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> r[i][j]; dp[i][j][i][j] = 0; } } 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]+r[i][j]; } } find(1, 1, n, m); cout << dp[1][1][n][m] << endl; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int t; t = 1; while(t--) { solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...