#include "bits/stdc++.h"
using namespace std;
// #define ORD_SET
// #define ROPE
#ifdef ORD_SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#endif
#ifdef ROPE
#include <ext/rope>
using namespace __gnu_cxx;
#endif
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using ll = long long;
using ld = long double;
#define pb push_back
#define ff first
#define ss second
#define sz(x) (ll)(x).size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
// #define ll int
void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
void NotInteractive() { ios_base::sync_with_stdio(false); cin.tie(NULL); }
void pre(ll a) { cout<<fixed<<setprecision(a); }
ll bit(ll a) { return __builtin_popcountll(a); }
ll binmul(ll a, ll b, ll c) { ll res = 0; while(b) { if(b&1) (res += a) %= c; (a += a) %= c; b >>= 1; } return res; }
ll binpow(ll a, ll b, ll c) { ll res = 1; while(b) { if(b&1) (res *= a) %= c; (a *= a) %= c; b >>= 1; } return res; }
template<typename T> T gcd(T a, T b) { if(b==0) return a; return gcd(b, a%b); }
template<typename T> T lcm(T a, T b) { return a/gcd(a, b)*b; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ld rnd() { return rng()%INT_MAX*1.0/INT_MAX; }
struct sn { ll l, r, x; };
ll B = 316;
const ll mod = 1e9+7;
const ll inf = 1e18+7;
const ll MX = LLONG_MAX;
const ll MN = LLONG_MIN;
const ld P = acos(-1.0);
const ll N = 3e5+5;
ll a[2005][2005], k;
ll f(vector<pair<ll, pair<ll, ll>>> v) {
ll ans = 0, cnt = 0;
set<pair<ll, ll>> st;
for(ll i=0; i<sz(v); i++) {
if(cnt>=k) break;
ll x = v[i].ss.ff, y = v[i].ss.ss;
if(!v[i].ff) y--;
else x--;
if(st.count({x, y}) || st.count({v[i].ss.ff, v[i].ss.ss})) continue;
ans += a[x][y]+a[v[i].ss.ff][v[i].ss.ss], cnt++;
st.insert({x, y}), st.insert({v[i].ss.ff, v[i].ss.ss});
}
return ans;
}
void kigash() {
ll n, sum = 0;
cin>>n>>k;
vector<vector<ll>> tmp;
for(ll i=1; i<=n; i++) {
for(ll j=1; j<=n; j++) {
cin>>a[i][j], sum += a[i][j];
if(j>1) tmp.pb({a[i][j]+a[i][j-1], 0, i, j});
if(i>1) tmp.pb({a[i][j]+a[i-1][j], 1, i, j});
}
}
vector<pair<ll, pair<ll, ll>>> v;
sort(rall(tmp));
for(ll i=0; i<min(sz(tmp), 200ll); i++) v.pb({tmp[i][1], {tmp[i][2], tmp[i][3]}});
shuffle(all(v), rng);
ll ans = f(v); ld t = 1;
for(ll it=1000; it>=1; it--) {
t *= 0.99;
auto u = v;
swap(u[rng()%sz(u)], u[rng()%sz(u)]);
ll res = f(u);
if(res>ans || rnd()<exp((res-ans)/t)) v = u, ans = res;
}
shuffle(all(v), rng);
t = 1;
for(ll it=1000; it>=1; it--) {
t *= 0.99;
auto u = v;
swap(u[rng()%sz(u)], u[rng()%sz(u)]);
ll res = f(u);
if(res>ans || rnd()<exp((res-ans)/t)) v = u, ans = res;
}
shuffle(all(v), rng);
t = 1;
for(ll it=1000; it>=1; it--) {
t *= 0.99;
auto u = v;
swap(u[rng()%sz(u)], u[rng()%sz(u)]);
ll res = f(u);
if(res>ans || rnd()<exp((res-ans)/t)) v = u, ans = res;
}
shuffle(all(v), rng);
t = 1;
for(ll it=1000; it>=1; it--) {
t *= 0.99;
auto u = v;
swap(u[rng()%sz(u)], u[rng()%sz(u)]);
ll res = f(u);
if(res>ans || rnd()<exp((res-ans)/t)) v = u, ans = res;
}
shuffle(all(v), rng);
t = 1;
for(ll it=1000; it>=1; it--) {
t *= 0.99;
auto u = v;
swap(u[rng()%sz(u)], u[rng()%sz(u)]);
ll res = f(u);
if(res>ans || rnd()<exp((res-ans)/t)) v = u, ans = res;
}
shuffle(all(v), rng);
t = 1;
for(ll it=1000; it>=1; it--) {
t *= 0.99;
auto u = v;
swap(u[rng()%sz(u)], u[rng()%sz(u)]);
ll res = f(u);
if(res>ans || rnd()<exp((res-ans)/t)) v = u, ans = res;
}
shuffle(all(v), rng);
t = 1;
for(ll it=1000; it>=1; it--) {
t *= 0.99;
auto u = v;
swap(u[rng()%sz(u)], u[rng()%sz(u)]);
ll res = f(u);
if(res>ans || rnd()<exp((res-ans)/t)) v = u, ans = res;
}
v.clear();
for(ll i=0; i<min(sz(tmp), 500ll); i++) v.pb({tmp[i][1], {tmp[i][2], tmp[i][3]}});
shuffle(all(v), rng);
t = 1;
for(ll it=2500; it>=1; it--) {
t *= 0.99;
auto u = v;
swap(u[rng()%sz(u)], u[rng()%sz(u)]);
ll res = f(u);
if(res>ans || rnd()<exp((res-ans)/t)) v = u, ans = res;
}
shuffle(all(v), rng);
t = 1;
for(ll it=2500; it>=1; it--) {
t *= 0.99;
auto u = v;
swap(u[rng()%sz(u)], u[rng()%sz(u)]);
ll res = f(u);
if(res>ans || rnd()<exp((res-ans)/t)) v = u, ans = res;
}
cout<<sum-ans<<"\n";//aadaadad
return;
}
signed main(/*Kigash Amir*/) {
// freopen("");
NotInteractive();
// precalc();
ll tt = 1;
// cin>>tt;
for(ll i=1; i<=tt; i++) kigash();
exit(0);
}
Compilation message
domino.cpp: In function 'void freopen(std::string)':
domino.cpp:31:33: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
domino.cpp:31:73: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
270 ms |
39440 KB |
Output is correct |
2 |
Incorrect |
184 ms |
39452 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
647 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3121 ms |
416220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1267 ms |
152888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
693 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
635 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
2192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
340 KB |
Output is correct |
2 |
Correct |
22 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
665 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
1368 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1223 ms |
152924 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
340 KB |
Output is correct |
2 |
Correct |
24 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
659 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |