#include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file(s) freopen(s".in", "r", stdin);freopen(s".out", "w", stdout);
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define ppb pop_back
#define ft first
#define sd second
#define ll long long
#define pll pair<ll,ll>
const int N = 1e6 + 5;
const int M = 1e7 + 5;
const int B = 316;
const ll msize = 101;
const ll mod = 998244353;
const long long inf = 3e18;
struct Mat { vector<vector<ll>> mt = vector<vector<ll>> (msize, vector<ll>(msize, 0)); Mat operator *(const Mat& other) { Mat c; for (int i = 0; i < msize; ++ i) { for (int j = 0; j < msize; ++ j) { for (int k = 0; k < msize; ++ k) { c.mt[i][k] = max(c.mt[i][k], mt[i][j] + other.mt[j][k]); } } } return c; } };
ll binmul(ll x, ll ti, ll m) { ll res = 0;while (ti){if(ti & 1)res += x;x += x;ti >>= 1; x %= m; res %= m;} return res;}
ll binpow(ll x, ll ti, ll m) { ll res = 1;while (ti){if(ti & 1)res=binmul(res,x,m);x=binmul(x,x,m);ti >>= 1; x %= m; res %= m;} return res;}
ll nok(ll a, ll b) { return (a*b)/__gcd(abs(a),abs(b)) * (a*b > 0 ? 1 : -1); }
bool odd(ll n) { return (n % 2 == 1); }
bool even(ll n) { return (n % 2 == 0); }
ll n, k, a[2001][2001], pr1[2001], pr2[2001];
const void solve() {
cin >> n >> k;
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= n; ++ j) cin >> a[i][j];
}
if (k == 1) {
ll sum = 0, mx = 0;
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= n; ++ j) {
sum += a[i][j];
if (i > 1) mx = max(mx, a[i][j] + a[i - 1][j]);
if (j > 1) mx = max(mx, a[i][j] + a[i][j - 1]);
}
}
cout << sum - mx;
return;
}
if (k == 2) {
ll ans = inf, sum = 0, mx = 0;
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= n; ++ j) {
sum += a[i][j];
if (i > 1) mx = max(mx, a[i][j] + a[i - 1][j]);
if (j > 1) mx = max(mx, a[i][j] + a[i][j - 1]);
}
pr1[i] = sum - mx;
}
sum = 0, mx = 0;
for (int j = 1; j <= n; ++ j) {
for (int i = 1; i <= n; ++ i) {
sum += a[i][j];
if (i > 1) mx = max(mx, a[i][j] + a[i - 1][j]);
if (j > 1) mx = max(mx, a[i][j] + a[i][j - 1]);
}
pr2[j] = sum - mx;
}
sum = 0, mx = 0;
for (int i = n; i >= 2; -- i) {
for (int j = 1; j <= n; ++ j) {
sum += a[i][j];
if (i < n) mx = max(mx, a[i][j] + a[i + 1][j]);
if (j > 1) mx = max(mx, a[i][j] + a[i][j - 1]);
}
ans = min(ans, pr1[i - 1] + sum - mx);
}
for (int j = n; j >= 2; -- j) {
for (int i = 1; i <= n; ++ i) {
sum += a[i][j];
if (i > 1) mx = max(mx, a[i][j] + a[i - 1][j]);
if (j < n) mx = max(mx, a[i][j] + a[i][j + 1]);
}
ans = min(ans, pr2[j - 1] + sum - mx);
}
cout << ans;
return;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
srand(time(NULL));
// file("cowsignal");
int tt = 1;
// cin >> tt;
for (int i = 1; i <= tt; ++ i) {
// cout << "Case " << i << ": ";
solve();
}
return 0;
}
Compilation message
domino.cpp:4: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
4 | #pragma GCC optimization("O3")
|
domino.cpp:5: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
5 | #pragma GCC optimization("unroll-loops")
|
domino.cpp:6: warning: ignoring '#pragma comment ' [-Wunknown-pragmas]
6 | #pragma comment(linker, "/stack:200000000")
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
4172 KB |
Output is correct |
2 |
Correct |
16 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
351 ms |
31672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
137 ms |
23692 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
63 ms |
12100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
233 ms |
31576 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
238 ms |
31532 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
244 ms |
31608 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
64 ms |
12108 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
257 ms |
31508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |