# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1096044 | HiepVu217 | Gondola (IOI14_gondola) | C++17 | 0 ms | 0 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;
const int N = 1e3 + 17;
int n, k, a[N][N], z;
long long f[N][N], ans, s[N][N], ss[N][N];
long long calc (int l, int r)
{
return ss[r][r] - ss[r][l - 1];
}
void dac (int l, int r, int from, int to)
{
if (l > r)
{
return;
}
int mid = l + r >> 1, x;
for (int i = from; i <= min (mid, to); ++i)
{
long long s = f[z - 1][i] + calc (i + 1, mid);
if (s < f[z][mid])
{
f[z][mid] = s;
x = i;
}
}
dac (l, mid - 1, from, x);
dac (mid + 1, r, x, to);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
cin >> a[i][j];
}
}
for (int i = 1; i <= n; ++i)
{
long long zz = 0;
for (int j = 1; j <= i; ++j)
{
zz += a[i][j];
s[i][j] = zz;
}
}
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= i; ++j)
{
ss[i][j] = max (ss[i - 1][j - 1], ss[i - 1][j]) + s[i][j];
}
}
for (int i = 0; i <= k; ++i)
{
for (int j = 0; j <= n; ++j)
{
f[i][j] = 1e18;
}
}
f[0][0] = 0;
for (int i = 1; i <= k; ++i)
{
++z;
dac (i, n, i - 1, n - 1);
}
cout << f[k][n];
}