#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define pf push_front
#define N 100050
#define M ll(1e9 + 7)
#define inf 1e9 + 1e9
using namespace std;
//using namespace __gnu_pbds;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef short int si;
typedef array <ll, 3> a3;
typedef array <ll, 5> a5;
//typedef tree <ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int ans = 0, t = 7 * 1e8, n, k, mx, it;
ll sum;
si a[2001][2001];
vector <pair <si, pair <si, pair <si, si> > > > vr;
inline void dfs(int v, int kol)
{
if (v == k) {ans = max(ans, kol); return;}
for (register int i = 0; i < min(mx, sz(vr)); i++)
{
it++;
if (it == t) {cout << sum - ll(ans) << endl; exit(0);}
int x = vr[i].S.S.F, y = vr[i].S.S.S;
si l, r;
l = a[x][y];
a[x][y] = 0;
if (vr[i].S.F == 0) {r = a[x - 1][y]; a[x - 1][y] = 0;} else {r = a[x][y - 1]; a[x][y - 1] = 0;}
dfs(v + 1, kol + l + r);
a[x][y] = l;
if (vr[i].S.F == 0) a[x - 1][y] = r; else a[x][y - 1] = r;
}
}
int main()
{
//freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k;
if (k <= 5) mx = 30;
else if (k == 6) mx = 24;
else if (k == 7) mx = 13;
else mx = 10;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {cin >> a[i][j]; sum += ll(a[i][j]);}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
if (i != 0) vr.pb({a[i][j] + a[i - 1][j], {0, {i, j}}});
if (j != 0) vr.pb({a[i][j] + a[i][j - 1], {1, {i, j}}});
}
sort(vr.begin(), vr.end());
reverse(vr.begin(), vr.end());
dfs(0, 0);
cout << sum - ll(ans) << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
6516 KB |
Output is correct |
2 |
Correct |
87 ms |
6516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
1 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1863 ms |
74080 KB |
Output is correct |
2 |
Correct |
1268 ms |
74016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1034 ms |
71984 KB |
Output is correct |
2 |
Correct |
832 ms |
71964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
450 ms |
20836 KB |
Output is correct |
2 |
Correct |
307 ms |
20836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2468 ms |
74012 KB |
Output is correct |
2 |
Correct |
1819 ms |
74012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4083 ms |
640 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4099 ms |
74052 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1653 ms |
1152 KB |
Output is correct |
2 |
Incorrect |
1704 ms |
1152 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1627 ms |
396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3681 ms |
74080 KB |
Output is correct |
2 |
Incorrect |
2920 ms |
74012 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1410 ms |
888 KB |
Output is correct |
2 |
Incorrect |
1415 ms |
872 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1888 ms |
20836 KB |
Output is correct |
2 |
Incorrect |
1611 ms |
20832 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1526 ms |
504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3530 ms |
74016 KB |
Output is correct |
2 |
Incorrect |
2557 ms |
89628 KB |
Output isn't correct |