# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
758939 | raysh07 | Raisins (IOI09_raisins) | C++17 | 146 ms | 26460 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
int n, m;
const int N = 51;
int dp[N][N][N][N];
int a[N][N];
int val(int l1, int r1, int l2, int r2){
return a[r1][r2] + a[l1 - 1][l2 - 1] - a[l1 - 1][r2] - a[r1][l2 - 1];
}
void Solve()
{
cin >> n >> m;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
cin >> a[i][j];
a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
}
}
for (int l1 = n; l1 >= 1; l1--){
for (int r1 = l1; r1 <= n; r1++){
for (int l2 = m; l2 >= 1; l2--){
for (int r2 = l2; r2 <= m; r2++){
if (l1 == r1 && l2 == r2) dp[l1][r1][l2][r2] = 0;
else {
int ok = val(l1, r1, l2, r2);
int add = INF;
for (int x = l1; x < r1; x++){
add = min(add, dp[l1][x][l2][r2] + dp[x + 1][r1][l2][r2]);
}
for (int x = l2; x < r2; x++){
add = min(add, dp[l1][r1][l2][x] + dp[l1][r1][x + 1][r2]);
}
dp[l1][r1][l2][r2] = add + ok;
// if (l1 == 1 && r1 == n && l2 == )
}
}
}
}
}
cout << dp[1][n][1][m] << "\n";
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |