You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
3.6 KiB
C++
132 lines
3.6 KiB
C++
#define _CRT_SECURE_NO_WARNINGS
|
|
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
|
|
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
#define CRP(t, x) const t &x
|
|
#define OPL(t, x) bool operator<(CRP(t, x)) const
|
|
#define FIL(x, v) memset(x, v, sizeof(x))
|
|
#define CLR(x) FIL(x, 0)
|
|
#define NE1(x) FIL(x, -1)
|
|
#define INF(x) FIL(x, 0x3f)
|
|
typedef long long ll, i64;
|
|
const int inf = 0x3f3f3f3f;
|
|
const int N = 5e3 + 50, M = N * N;
|
|
struct Edge
|
|
{
|
|
int nxt, to, len, cap;
|
|
} E[M];
|
|
int adj[N], dis[N], H[N], ecnt, idx, T, pre[N];
|
|
int len[N];
|
|
inline void _int_addEdge(int f, int t, int l, int c)
|
|
{
|
|
if (len[f] != -1)
|
|
{
|
|
E[len[f]].nxt = ecnt;
|
|
E[ecnt] = {-1, t, l, c};
|
|
len[f] = ecnt++;
|
|
}
|
|
else
|
|
{
|
|
E[ecnt] = {-1, t, l, c};
|
|
adj[f] = ecnt;
|
|
len[f] = ecnt++;
|
|
}
|
|
}
|
|
inline void addEdge(int f, int t, int l, int c)
|
|
{
|
|
_int_addEdge(f, t, l, c);
|
|
_int_addEdge(t, f, -l, 0);
|
|
}
|
|
int countEdges(int u)
|
|
{
|
|
int cnt = 0;
|
|
for (int e = adj[u]; ~e; e = E[e].nxt) cnt++;
|
|
return cnt;
|
|
}
|
|
int NnNn = 0;
|
|
pair<int, int> MCMF(int S)
|
|
{
|
|
fill(H, H + 1 + T, 0);
|
|
struct heap_node
|
|
{
|
|
int u, d;
|
|
OPL(heap_node, rhs) { return d > rhs.d; }
|
|
};
|
|
int flow = 0, cost = 0;
|
|
// map <int,int> mpss;
|
|
// mpss.clear();
|
|
int AA = 0, BB = 0;
|
|
for (;;)
|
|
{
|
|
priority_queue<heap_node> pq;
|
|
fill(dis, dis + 1 + T, inf);
|
|
int cnt = 0;
|
|
for (pq.push({S, dis[S] = 0}); !pq.empty();)
|
|
{
|
|
//AA++;
|
|
cnt++;
|
|
int u = pq.top().u, d = pq.top().d;
|
|
pq.pop();
|
|
if (dis[u] < d) continue;
|
|
for (int e = adj[u]; ~e; e = E[e].nxt)
|
|
if (E[e].cap > 0 && dis[E[e].to] > d + E[e].len + H[u] - H[E[e].to])
|
|
{
|
|
//BB++;
|
|
pq.push({E[e].to, dis[E[e].to] = d + E[e].len + H[u] - H[E[e].to]});
|
|
pre[E[e].to] = e;
|
|
// mpss[E[e].to]++;
|
|
}
|
|
}
|
|
// printf("- AA: %d BB: %d\n",AA,BB);
|
|
// printf("- %d\n", cnt);
|
|
if (dis[T] == inf) break;
|
|
for (int i = 0; i <= T; i++) H[i] += dis[i];
|
|
int f = inf;
|
|
for (int u = T; u != S; u = E[pre[u] ^ 1].to)
|
|
f = min(f, E[pre[u]].cap);
|
|
for (int u = T; u != S; u = E[pre[u] ^ 1].to)
|
|
E[pre[u]].cap -= f, E[pre[u] ^ 1].cap += f;
|
|
flow += f, cost += f * H[T];
|
|
}
|
|
/* for(auto ttt:mpss){
|
|
cout<< ttt.first<<" " << ttt.second<<endl;
|
|
}*/
|
|
return make_pair(flow, cost);
|
|
}
|
|
int a[N];
|
|
inline void readint(int &x)
|
|
{
|
|
int ch = x = 0;
|
|
while (!isdigit(ch)) ch = getchar();
|
|
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
|
|
}
|
|
int main()
|
|
{
|
|
//freopen("multi.in", "r", stdin);
|
|
// freopen("aaa.txt","w",stdout);
|
|
int T, n, k;
|
|
readint(T);
|
|
while (T--)
|
|
{
|
|
memset(len, -1, sizeof len);
|
|
NE1(adj), ecnt = 0;
|
|
ll ans = 0;
|
|
readint(n), readint(k), ::T = (n + 1) << 1 | 1;
|
|
int s2 = (n + 1) << 1;
|
|
addEdge(0, s2, 0, k);
|
|
for (int i = 1; i <= n; i++)
|
|
{
|
|
readint(a[i]);
|
|
addEdge(s2, i, 0, 1);
|
|
addEdge(i << 1, i << 1 | 1, -a[i], 1);
|
|
addEdge(i << 1 | 1, ::T, 0, 1);
|
|
}
|
|
for (int i = 1; i <= n; i++)
|
|
for (int j = 1; j < i; j++)
|
|
if (a[j] <= a[i])
|
|
addEdge(j << 1 | 1, i << 1, 0, 1);
|
|
printf("%d\n", -MCMF(0).second);
|
|
}
|
|
return 0;
|
|
}
|