// Author : FatihCihan
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) v.begin() , v.end()
#define sz(a) (int)a.size()
const int inf = 1e18 + 7;
void solve(){
int n;
cin >> n;
int m = __lg(n);
int 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<int>>>> 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;
}
}
int 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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 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 |
456 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
336 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 |
504 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 |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 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 |
456 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
336 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 |
504 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 |
336 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 |
592 KB |
Output is correct |
23 |
Correct |
7 ms |
848 KB |
Output is correct |
24 |
Correct |
2 ms |
524 KB |
Output is correct |
25 |
Correct |
2 ms |
336 KB |
Output is correct |
26 |
Correct |
6 ms |
592 KB |
Output is correct |
27 |
Correct |
6 ms |
760 KB |
Output is correct |
28 |
Correct |
6 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 |
828 KB |
Output is correct |
34 |
Correct |
5 ms |
592 KB |
Output is correct |
35 |
Correct |
6 ms |
696 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 |
772 KB |
Output is correct |
39 |
Correct |
6 ms |
592 KB |
Output is correct |
40 |
Correct |
6 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 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 |
456 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
336 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 |
504 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 |
336 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 |
592 KB |
Output is correct |
23 |
Correct |
7 ms |
848 KB |
Output is correct |
24 |
Correct |
2 ms |
524 KB |
Output is correct |
25 |
Correct |
2 ms |
336 KB |
Output is correct |
26 |
Correct |
6 ms |
592 KB |
Output is correct |
27 |
Correct |
6 ms |
760 KB |
Output is correct |
28 |
Correct |
6 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 |
828 KB |
Output is correct |
34 |
Correct |
5 ms |
592 KB |
Output is correct |
35 |
Correct |
6 ms |
696 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 |
772 KB |
Output is correct |
39 |
Correct |
6 ms |
592 KB |
Output is correct |
40 |
Correct |
6 ms |
592 KB |
Output is correct |
41 |
Correct |
824 ms |
6216 KB |
Output is correct |
42 |
Correct |
857 ms |
6152 KB |
Output is correct |
43 |
Correct |
833 ms |
6148 KB |
Output is correct |
44 |
Correct |
856 ms |
6144 KB |
Output is correct |
45 |
Correct |
832 ms |
5960 KB |
Output is correct |
46 |
Correct |
824 ms |
5960 KB |
Output is correct |
47 |
Correct |
839 ms |
6140 KB |
Output is correct |
48 |
Correct |
812 ms |
4944 KB |
Output is correct |
49 |
Correct |
813 ms |
6144 KB |
Output is correct |
50 |
Correct |
864 ms |
5928 KB |
Output is correct |
51 |
Correct |
835 ms |
6224 KB |
Output is correct |
52 |
Correct |
840 ms |
6168 KB |
Output is correct |
53 |
Correct |
835 ms |
6184 KB |
Output is correct |
54 |
Correct |
854 ms |
5968 KB |
Output is correct |
55 |
Correct |
843 ms |
6036 KB |
Output is correct |
56 |
Correct |
829 ms |
6216 KB |
Output is correct |
57 |
Correct |
835 ms |
5940 KB |
Output is correct |
58 |
Correct |
832 ms |
5960 KB |
Output is correct |
59 |
Correct |
845 ms |
6180 KB |
Output is correct |
60 |
Correct |
817 ms |
5968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 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 |
456 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
336 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 |
504 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 |
336 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 |
592 KB |
Output is correct |
23 |
Correct |
7 ms |
848 KB |
Output is correct |
24 |
Correct |
2 ms |
524 KB |
Output is correct |
25 |
Correct |
2 ms |
336 KB |
Output is correct |
26 |
Correct |
6 ms |
592 KB |
Output is correct |
27 |
Correct |
6 ms |
760 KB |
Output is correct |
28 |
Correct |
6 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 |
828 KB |
Output is correct |
34 |
Correct |
5 ms |
592 KB |
Output is correct |
35 |
Correct |
6 ms |
696 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 |
772 KB |
Output is correct |
39 |
Correct |
6 ms |
592 KB |
Output is correct |
40 |
Correct |
6 ms |
592 KB |
Output is correct |
41 |
Correct |
824 ms |
6216 KB |
Output is correct |
42 |
Correct |
857 ms |
6152 KB |
Output is correct |
43 |
Correct |
833 ms |
6148 KB |
Output is correct |
44 |
Correct |
856 ms |
6144 KB |
Output is correct |
45 |
Correct |
832 ms |
5960 KB |
Output is correct |
46 |
Correct |
824 ms |
5960 KB |
Output is correct |
47 |
Correct |
839 ms |
6140 KB |
Output is correct |
48 |
Correct |
812 ms |
4944 KB |
Output is correct |
49 |
Correct |
813 ms |
6144 KB |
Output is correct |
50 |
Correct |
864 ms |
5928 KB |
Output is correct |
51 |
Correct |
835 ms |
6224 KB |
Output is correct |
52 |
Correct |
840 ms |
6168 KB |
Output is correct |
53 |
Correct |
835 ms |
6184 KB |
Output is correct |
54 |
Correct |
854 ms |
5968 KB |
Output is correct |
55 |
Correct |
843 ms |
6036 KB |
Output is correct |
56 |
Correct |
829 ms |
6216 KB |
Output is correct |
57 |
Correct |
835 ms |
5940 KB |
Output is correct |
58 |
Correct |
832 ms |
5960 KB |
Output is correct |
59 |
Correct |
845 ms |
6180 KB |
Output is correct |
60 |
Correct |
817 ms |
5968 KB |
Output is correct |
61 |
Execution timed out |
1541 ms |
90340 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |