#include<bits/stdc++.h>
using namespace std;
#define int long long
#define il inline
#define rg register
#define vi std::vector<int>
#define L(i, a, b) for (rg int i = (a); i <= (b); i++)
#define R(i, a, b) for (rg int i = (a); i >= (b); i--)
#define rep(i, a) for (rg auto i : (a))
const int N = 3e5 + 5, M = 21, p = 1e9 + 7, inf = 8e18;
namespace zrh{
int n, L, pos[10], f[N];
struct node{
int c; vi P;
}; vector<node> a;
il int get(int L, const vi &p1, const vi &p2, int lim){
L(i, 0, 9) pos[i] = 0; L(i, 0, L - 1) pos[p1[i]] = i;
int inv = 0;
L(i, 0, L - 1){
int pi = pos[p2[i]];
L(j, i + 1, L - 1)
if (pi > pos[p2[j]]){ inv++; if (inv > lim) return inv; }
}
return inv;
}
il void main(){
std::cin >> n >> L; a.resize(n); memset(f, -1, sizeof(f));
L(i, 0, n - 1){
std::cin >> a[i].c, a[i].P.resize(L);
L(j, 0, L - 1) std::cin >> a[i].P[j];
}
int mx = -1; vi p1(L); L(i, 0, L - 1) p1[i] = i + 1;
L(i, 0, n - 1){
int stp = i + 1, cur = -1;
int d0 = get(L, p1, a[i].P, stp); if (d0 <= stp) cur = a[i].c;
if (i >= 36){
int v = f[i - 36];
if (v != -1 && (mx == -1 || v > mx)) mx = v;
}
if (mx != -1){
int v = mx + a[i].c;
if (cur == -1 || v > cur) cur = v;
}
int lim = max(0LL, i - 36 + 1);
R(j, i - 1, lim){
if (f[j] == -1) continue;
int dif = stp - (j + 1), d = get(L, a[j].P, a[i].P, dif);
if (d <= dif){
int v = f[j] + a[i].c;
if (cur == -1 || v > cur) cur = v;
}
}
f[i] = cur;
}
int ans = 0; L(i, 0, n - 1) ans = max(ans, f[i]);
std::cout << ans;
}
} signed main(){
ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
return zrh::main(), 0;
}
./Main.cpp: In function 'long long int zrh::get(long long int, const std::vector<long long int>&, const std::vector<long long int>&, long long int)':
./Main.cpp:17:19: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
17 | L(i, 0, 9) pos[i] = 0; L(i, 0, L - 1) pos[p1[i]] = i;
| ^
./Main.cpp:7:32: note: in definition of macro 'L'
7 | #define L(i, a, b) for (rg int i = (a); i <= (b); i++)
| ^
./Main.cpp:7:20: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
7 | #define L(i, a, b) for (rg int i = (a); i <= (b); i++)
| ^~~
./Main.cpp:17:17: note: in expansion of macro 'L'
17 | L(i, 0, 9) pos[i] = 0; L(i, 0, L - 1) pos[p1[i]] = i;
| ^
./Main.cpp:7:20: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
7 | #define L(i, a, b) for (rg int i = (a); i <= (b); i++)
| ^~~
./Main.cpp:17:40: note: in expansion of macro 'L'
17 | L(i, 0, 9) pos[i] = 0; L(i, 0, L - 1) pos[p1[i]] = i;
| ^
./Main.cpp:17:42: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
17 | L(i, 0, 9) pos[i] = 0; L(i, 0, L - 1) pos[p1[i]] = i;
| ^
./Main.cpp:7:32: note: in definition of macro 'L'
7 | #define L(i, a, b) for (rg int i = (a); i <= (b); i++)
| ^
./Main.cpp:19:19: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
19 | L(i, 0, L - 1){
| ^
./Main.cpp:7:32: note: in definition of macro 'L'
7 | #define L(i, a, b) for (rg int i = (a); i <= (b); i++)
| ^
./Main.cpp:21:27: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
21 | L(j, i + 1, L - 1)
| ^
./Main.cpp:7:32: note: in definition of macro 'L'
7 | #define L(i, a, b) for (rg int i = (a); i <= (b); i++)
| ^
./Main.cpp: In function 'void zrh::main()':
./Main.cpp:28:19: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
28 | L(i, 0, n - 1){
| ^
./Main.cpp:7:32: note: in definition of macro 'L'
7 | #define L(i, a, b) for (rg int i = (a); i <= (b); i++)
| ^
./Main.cpp:30:27: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
30 | L(j, 0, L - 1) std::cin >> a[i].P[j];
| ^
./Main.cpp:7:32: note: in definition of macro 'L'
7 | #define L(i, a, b) for (rg int i = (a); i <= (b); i++)
| ^
./Main.cpp:32:42: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
32 | int mx = -1; vi p1(L); L(i, 0, L - 1) p1[i] = i + 1;
| ^
./Main.cpp:7:32: note: in definition of macro 'L'
7 | #define L(i, a, b) for (rg int i = (a); i <= (b); i++)
| ^
./Main.cpp:33:19: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
33 | L(i, 0, n - 1){
| ^
./Main.cpp:7:32: note: in definition of macro 'L'
7 | #define L(i, a, b) for (rg int i = (a); i <= (b); i++)
| ^
./Main.cpp:45:27: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
45 | R(j, i - 1, lim){
| ^
./Main.cpp:8:32: note: in definition of macro 'R'
8 | #define R(i, a, b) for (rg int i = (a); i >= (b); i--)
| ^
./Main.cpp:55:32: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
55 | int ans = 0; L(i, 0, n - 1) ans = max(ans, f[i]);
| ^
./Main.cpp:7:32: note: in definition of macro 'L'
7 | #define L(i, a, b) for (rg int i = (a); i <= (b); i++)
| ^