// put it before the include
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Author : FatihCihan
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin() , v.end()
#define sz(a) (int)a.size()
const long long inf = 1e18 + 7;
void solve(){
int n;
cin >> n;
int m = __lg(n);
long long cost[n][n];
for(int i = 0;i<n;i++)for(int j = 0;j<n;j++)cin >> cost[i][j];
vector<vector<vector<vector<long long>>>> dp;
dp.resize(m+1);
for(int i = 1;i<=m;i++){
dp[i].resize(n / (1ll << i));
for(int j = 0;j<sz(dp[i]);j++){
dp[i][j].resize(1ll << (i-1));
for(int k = 0;k<sz(dp[i][j]);k++){
dp[i][j][k].assign(1ll << (i-1) , inf);
}
}
}
for(int i = 0;i<n/2;i++){
// 2 lik ler uzerinden tanimla
dp[1][i][0][0] = cost[2 * i][2 * i + 1];
}
// cout << "start" << endl;
for(int i = 1;i<m;i++){
for(int j = 0;j<sz(dp[i]);j+=2){
// cout << "lul : " << i << " , " << j << endl;
for(int t1 = 0;t1<sz(dp[i][j]);t1++)for(int t2 = 0;t2<sz(dp[i][j]);t2++)for(int t3 = 0;t3<sz(dp[i][j]);t3++)for(int t4 = 0;t4<sz(dp[i][j]);t4++){
// cout << "flag0" << endl;
int arr[4];
arr[0] = (1 << i) * j + t1;
arr[1] = (1 << i) * j + (1 << (i-1)) + t2;
arr[2] = (1 << i) * (j + 1) + t3;
arr[3] = (1 << i) * (j + 1) + (1 << (i-1)) + t4;
// cout << "t : " << t1 << " , " << t2 << " , " << t3 << " , " << t4 << endl;
// cout << "arr : " << arr[0] << " , " << arr[1] << " , " << arr[2] << " , " << arr[3] << endl;
// cout << "dp : " << dp[i][j][t1][t2] << " , " << dp[i][j+1][t3][t4] << endl;
// cout << "huh ? " << ((1<<(i-1)) + t4) << " - " << sz(dp[i+1][j/2]) << endl;
// cout << "flag1" << endl;
// 0 2
dp[i+1][j/2][t1][t3] = min(dp[i+1][j/2][t1][t3] , dp[i][j][t1][t2] + dp[i][j+1][t3][t4] + cost[arr[1]][arr[3]]);
// 0 3
dp[i+1][j/2][t1][(1<<(i-1)) + t4] = min(dp[i+1][j/2][t1][(1<<(i-1)) + t4] , dp[i][j][t1][t2] + dp[i][j+1][t3][t4] + cost[arr[1]][arr[2]]);
// 1 2
dp[i+1][j/2][(1<<(i-1)) + t2][t3] = min(dp[i+1][j/2][(1<<(i-1)) + t2][t3] , dp[i][j][t1][t2] + dp[i][j+1][t3][t4] + cost[arr[0]][arr[3]]);
// 1 3
dp[i+1][j/2][(1<<(i-1)) + t2][(1<<(i-1)) + t4] = min(dp[i+1][j/2][(1<<(i-1)) + t2][(1<<(i-1)) + t4] , dp[i][j][t1][t2] + dp[i][j+1][t3][t4] + cost[arr[0]][arr[2]]);
// cout << "flag2" << endl;
}
// cout << "done" << endl;
}
}
long long ans = inf;
for(int i = 0;i<sz(dp[m][0]);i++){
for(int j = 0;j<sz(dp[m][0]);j++){
ans = min(ans , dp[m][0][i][j]);
}
}
cout << ans << endl;
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int testcase = 1;//cin >> testcase;
while(testcase--)solve();
cerr << 1000.0 * clock() / CLOCKS_PER_SEC << " ms" << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
504 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
504 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
6 ms |
592 KB |
Output is correct |
22 |
Correct |
6 ms |
668 KB |
Output is correct |
23 |
Correct |
6 ms |
592 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
6 ms |
592 KB |
Output is correct |
27 |
Correct |
5 ms |
592 KB |
Output is correct |
28 |
Correct |
7 ms |
592 KB |
Output is correct |
29 |
Correct |
6 ms |
592 KB |
Output is correct |
30 |
Correct |
6 ms |
592 KB |
Output is correct |
31 |
Correct |
6 ms |
592 KB |
Output is correct |
32 |
Correct |
6 ms |
592 KB |
Output is correct |
33 |
Correct |
6 ms |
592 KB |
Output is correct |
34 |
Correct |
6 ms |
592 KB |
Output is correct |
35 |
Correct |
6 ms |
592 KB |
Output is correct |
36 |
Correct |
6 ms |
592 KB |
Output is correct |
37 |
Correct |
6 ms |
592 KB |
Output is correct |
38 |
Correct |
6 ms |
592 KB |
Output is correct |
39 |
Correct |
6 ms |
580 KB |
Output is correct |
40 |
Correct |
6 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
504 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
6 ms |
592 KB |
Output is correct |
22 |
Correct |
6 ms |
668 KB |
Output is correct |
23 |
Correct |
6 ms |
592 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
6 ms |
592 KB |
Output is correct |
27 |
Correct |
5 ms |
592 KB |
Output is correct |
28 |
Correct |
7 ms |
592 KB |
Output is correct |
29 |
Correct |
6 ms |
592 KB |
Output is correct |
30 |
Correct |
6 ms |
592 KB |
Output is correct |
31 |
Correct |
6 ms |
592 KB |
Output is correct |
32 |
Correct |
6 ms |
592 KB |
Output is correct |
33 |
Correct |
6 ms |
592 KB |
Output is correct |
34 |
Correct |
6 ms |
592 KB |
Output is correct |
35 |
Correct |
6 ms |
592 KB |
Output is correct |
36 |
Correct |
6 ms |
592 KB |
Output is correct |
37 |
Correct |
6 ms |
592 KB |
Output is correct |
38 |
Correct |
6 ms |
592 KB |
Output is correct |
39 |
Correct |
6 ms |
580 KB |
Output is correct |
40 |
Correct |
6 ms |
592 KB |
Output is correct |
41 |
Correct |
775 ms |
3408 KB |
Output is correct |
42 |
Correct |
765 ms |
3620 KB |
Output is correct |
43 |
Correct |
764 ms |
3408 KB |
Output is correct |
44 |
Correct |
793 ms |
3408 KB |
Output is correct |
45 |
Correct |
797 ms |
3408 KB |
Output is correct |
46 |
Correct |
782 ms |
3624 KB |
Output is correct |
47 |
Correct |
765 ms |
3628 KB |
Output is correct |
48 |
Correct |
779 ms |
3624 KB |
Output is correct |
49 |
Correct |
811 ms |
3620 KB |
Output is correct |
50 |
Correct |
763 ms |
3408 KB |
Output is correct |
51 |
Correct |
770 ms |
3408 KB |
Output is correct |
52 |
Correct |
772 ms |
3624 KB |
Output is correct |
53 |
Correct |
764 ms |
3624 KB |
Output is correct |
54 |
Correct |
756 ms |
3620 KB |
Output is correct |
55 |
Correct |
788 ms |
3620 KB |
Output is correct |
56 |
Correct |
764 ms |
3628 KB |
Output is correct |
57 |
Correct |
790 ms |
3408 KB |
Output is correct |
58 |
Correct |
803 ms |
3624 KB |
Output is correct |
59 |
Correct |
786 ms |
3628 KB |
Output is correct |
60 |
Correct |
780 ms |
3620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
504 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
6 ms |
592 KB |
Output is correct |
22 |
Correct |
6 ms |
668 KB |
Output is correct |
23 |
Correct |
6 ms |
592 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
6 ms |
592 KB |
Output is correct |
27 |
Correct |
5 ms |
592 KB |
Output is correct |
28 |
Correct |
7 ms |
592 KB |
Output is correct |
29 |
Correct |
6 ms |
592 KB |
Output is correct |
30 |
Correct |
6 ms |
592 KB |
Output is correct |
31 |
Correct |
6 ms |
592 KB |
Output is correct |
32 |
Correct |
6 ms |
592 KB |
Output is correct |
33 |
Correct |
6 ms |
592 KB |
Output is correct |
34 |
Correct |
6 ms |
592 KB |
Output is correct |
35 |
Correct |
6 ms |
592 KB |
Output is correct |
36 |
Correct |
6 ms |
592 KB |
Output is correct |
37 |
Correct |
6 ms |
592 KB |
Output is correct |
38 |
Correct |
6 ms |
592 KB |
Output is correct |
39 |
Correct |
6 ms |
580 KB |
Output is correct |
40 |
Correct |
6 ms |
592 KB |
Output is correct |
41 |
Correct |
775 ms |
3408 KB |
Output is correct |
42 |
Correct |
765 ms |
3620 KB |
Output is correct |
43 |
Correct |
764 ms |
3408 KB |
Output is correct |
44 |
Correct |
793 ms |
3408 KB |
Output is correct |
45 |
Correct |
797 ms |
3408 KB |
Output is correct |
46 |
Correct |
782 ms |
3624 KB |
Output is correct |
47 |
Correct |
765 ms |
3628 KB |
Output is correct |
48 |
Correct |
779 ms |
3624 KB |
Output is correct |
49 |
Correct |
811 ms |
3620 KB |
Output is correct |
50 |
Correct |
763 ms |
3408 KB |
Output is correct |
51 |
Correct |
770 ms |
3408 KB |
Output is correct |
52 |
Correct |
772 ms |
3624 KB |
Output is correct |
53 |
Correct |
764 ms |
3624 KB |
Output is correct |
54 |
Correct |
756 ms |
3620 KB |
Output is correct |
55 |
Correct |
788 ms |
3620 KB |
Output is correct |
56 |
Correct |
764 ms |
3628 KB |
Output is correct |
57 |
Correct |
790 ms |
3408 KB |
Output is correct |
58 |
Correct |
803 ms |
3624 KB |
Output is correct |
59 |
Correct |
786 ms |
3628 KB |
Output is correct |
60 |
Correct |
780 ms |
3620 KB |
Output is correct |
61 |
Execution timed out |
1555 ms |
50168 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |