#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 |
102 ms |
6508 KB |
Output is correct |
2 |
Correct |
84 ms |
6636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
640 KB |
Output is correct |
2 |
Correct |
1 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1911 ms |
74032 KB |
Output is correct |
2 |
Correct |
1459 ms |
74012 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 |
1030 ms |
71968 KB |
Output is correct |
2 |
Correct |
828 ms |
71964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
434 ms |
20828 KB |
Output is correct |
2 |
Correct |
299 ms |
20836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2420 ms |
74016 KB |
Output is correct |
2 |
Correct |
1834 ms |
74012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4091 ms |
640 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4094 ms |
73920 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1642 ms |
1152 KB |
Output is correct |
2 |
Incorrect |
1709 ms |
1152 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1665 ms |
400 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3562 ms |
74072 KB |
Output is correct |
2 |
Incorrect |
2953 ms |
74004 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1424 ms |
872 KB |
Output is correct |
2 |
Incorrect |
1387 ms |
876 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1938 ms |
20832 KB |
Output is correct |
2 |
Incorrect |
1633 ms |
20840 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1548 ms |
408 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3307 ms |
74016 KB |
Output is correct |
2 |
Incorrect |
2572 ms |
74180 KB |
Output isn't correct |