1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
| #include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f #define N 4010
namespace IO { const int S = (1 << 20) + 5; char buf[S], *H, *T;
inline char Get() { if (H == T) T = (H = buf) + fread(buf, 1, S, stdin); if (H == T) return -1; return *H++; } inline int read() { int x = 0, fg = 1; char c = Get(); while (!isdigit(c) && c != '-') c = Get(); if (c == '-') fg = -1, c = Get(); while (isdigit(c)) x = x * 10 + c - '0', c = Get(); return x * fg; } inline void reads(char *s) { char c = Get(); int tot = 0; while (c < 'a' || c > 'z') c = Get(); while (c >= 'a' && c <= 'z') s[++tot] = c, c = Get(); s[++tot] = '\0'; } char obuf[S], *oS = obuf, *oT = oS + S - 1, c, qu[55]; int qr; inline void flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; } inline void putc(char x) { *oS++ = x; if (oS == oT) flush(); } template<class I> inline void print(I x) { if (!x) putc('0'); if (x < 0) putc('-'), x = -x; while (x) qu[++qr] = x % 10 + '0', x /= 10; while (qr) putc(qu[qr--]); } inline void prints(const char *s) { int len = strlen(s); for (int i = 0; i < len; i++) putc(s[i]); putc('\n'); } inline void printd(int d, double x) { long long t = (long long) floor(x); print(t); putc('.'); x -= t; while (d--) { double y = x * 10; x *= 10; int c = (int) floor(y); putc(c + '0'); x -= floor(y); } } } using namespace IO;
struct edge { int to, capacity, cost, rev;
edge() {} edge(int to, int _capacity, int _cost, int _rev) : to(to), capacity(_capacity), cost(_cost), rev(_rev) {} };
struct Min_Cost_Max_Flow { int V, H[N + 5], dis[N + 5], PreV[N + 5], PreE[N + 5]; vector<edge> G[N + 5];
void Init(int n) { V = n; for (int i = 0; i <= V; ++i)G[i].clear(); } void Add_Edge(int from, int to, int cap, int cost) { G[from].push_back(edge(to, cap, cost, G[to].size())); G[to].push_back(edge(from, 0, -cost, G[from].size() - 1)); } int Min_cost_max_flow(int s, int t, int f, int &flow) { int res = 0; fill(H, H + 1 + V, 0); while (f) { priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q; fill(dis, dis + 1 + V, INF); dis[s] = 0; q.push(pair<int, int>(0, s)); while (!q.empty()) { pair<int, int> now = q.top(); q.pop(); int v = now.second; if (dis[v] < now.first)continue; for (int i = 0; i < G[v].size(); ++i) { edge &e = G[v][i]; if (e.capacity > 0 && dis[e.to] > dis[v] + e.cost + H[v] - H[e.to]) { dis[e.to] = dis[v] + e.cost + H[v] - H[e.to]; PreV[e.to] = v; PreE[e.to] = i; q.push(pair<int, int>(dis[e.to], e.to)); } } } if (dis[t] == INF)break; for (int i = 0; i <= V; ++i)H[i] += dis[i]; int d = f; for (int v = t; v != s; v = PreV[v])d = min(d, G[PreV[v]][PreE[v]].capacity); f -= d; flow += d; res += d * H[t]; for (int v = t; v != s; v = PreV[v]) { edge &e = G[PreV[v]][PreE[v]]; e.capacity -= d; G[v][e.rev].capacity += d; } } return res; } int Max_cost_max_flow(int s, int t, int f, int &flow) { int res = 0; fill(H, H + 1 + V, 0); while (f) { priority_queue<pair<int, int>> q; fill(dis, dis + 1 + V, -INF); dis[s] = 0; q.push(pair<int, int>(0, s)); while (!q.empty()) { pair<int, int> now = q.top(); q.pop(); int v = now.second; if (dis[v] > now.first)continue; for (int i = 0; i < G[v].size(); ++i) { edge &e = G[v][i]; if (e.capacity > 0 && dis[e.to] < dis[v] + e.cost + H[v] - H[e.to]) { dis[e.to] = dis[v] + e.cost + H[v] - H[e.to]; PreV[e.to] = v; PreE[e.to] = i; q.push(pair<int, int>(dis[e.to], e.to)); } } } if (dis[t] == -INF)break; for (int i = 0; i <= V; ++i)H[i] += dis[i]; int d = f; for (int v = t; v != s; v = PreV[v])d = min(d, G[PreV[v]][PreE[v]].capacity); f -= d; flow += d; res += d * H[t]; for (int v = t; v != s; v = PreV[v]) { edge &e = G[PreV[v]][PreE[v]]; e.capacity -= d; G[v][e.rev].capacity += d; } } return res; } } MCMF;
int n, k, s1, s2, t, a[N], flow;
int main() {
int cas = read(); while (cas--) { n = read(), k = read(); s1 = 0, s2 = 1, t = 2 * n + 2; MCMF.Init(t); MCMF.Add_Edge(s1, s2, k, 0); for (int i = 1; i <= n; ++i) { a[i] = read(); MCMF.Add_Edge(s2, i << 1, 1, 0); MCMF.Add_Edge(i << 1 | 1, t, 1, 0); MCMF.Add_Edge(i << 1, i << 1 | 1, 1, -a[i]); } for (int i = 1; i <= n; ++i) { for (int j = 1; j < i; ++j) { if (a[i] >= a[j]) { MCMF.Add_Edge(j << 1 | 1, i << 1, 1, 0); } } } print(-MCMF.Min_cost_max_flow(s1, t, INF, flow)); putc('\n'); } flush(); return 0; }
|