forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.cpp
More file actions
200 lines (171 loc) · 5.68 KB
/
C.cpp
File metadata and controls
200 lines (171 loc) · 5.68 KB
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
#include <sstream>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <string>
#include <queue>
#include <bitset>
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a), _##i = (b); i <= _##i; ++i)
#define FORD(i, a, b) for (int i = (a), _##i = (b); i >= _##i; --i)
#define REP(i, a) for (int i = 0, _##i = (a); i < _##i; ++i)
#define REPD(i,n) for(int i = (n)-1; i >= 0; --i)
#define DEBUG(X) { cerr << #X << " = " << (X) << endl; }
#define PR(A, n) { cerr << #A << " = "; FOR(_, 1, n) cerr << A[_] << ' '; cerr << endl; }
#define PR0(A, n) { cerr << #A << " = "; REP(_, n) cerr << A[_] << ' '; cerr << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define double long double
typedef pair<int, int> II;
#define __builtin_popcount __builtin_popcountll
#define SZ(x) ((int)(x).size())
#define ALL(a) (a).begin(), (a).end()
#define MS(a,x) memset(a, x, sizeof(a))
#define next ackjalscjaowjico
#define prev ajcsoua0wucckjsl
#define y1 alkscj9u20cjeijc
#define left lajcljascjljl
#define right aucouasocjolkjl
#define y0 u9cqu3jioajc
#define TWO(X) (1LL<<(X))
#define CONTAIN(S,X) ((S) & TWO(X))
int rand16() {
return rand() & (TWO(16) - 1);
}
int my_rand() {
return rand16() << 15 | rand16();
}
double safe_sqrt(double x) { return sqrt(max((double)0.0, x)); }
int GI(int& x) { return scanf("%lld", &x); }
template<class Flow=int, class Cost=int>
struct MinCostFlow {
const Flow INF_FLOW = 1000111000;
const Cost INF_COST = 1000111000111000LL;
int n, t, S, T;
Flow totalFlow;
Cost totalCost;
vector<int> last, visited;
vector<Cost> dis;
struct Edge {
int to;
Flow cap;
Cost cost;
int next;
Edge(int to, Flow cap, Cost cost, int next) :
to(to), cap(cap), cost(cost), next(next) {}
};
vector<Edge> edges;
MinCostFlow(int n) : n(n), t(0), totalFlow(0), totalCost(0), last(n, -1), visited(n, 0), dis(n, 0) {
edges.clear();
}
int addEdge(int from, int to, Flow cap, Cost cost) {
edges.push_back(Edge(to, cap, cost, last[from]));
last[from] = t++;
edges.push_back(Edge(from, 0, -cost, last[to]));
last[to] = t++;
return t - 2;
}
pair<Flow, Cost> minCostFlow(int _S, int _T) {
S = _S; T = _T;
SPFA();
while (1) {
while (1) {
REP(i,n) visited[i] = 0;
if (!findFlow(S, INF_FLOW)) break;
}
if (!modifyLabel()) break;
}
return make_pair(totalFlow, totalCost);
}
private:
void SPFA() {
REP(i,n) dis[i] = INF_COST;
priority_queue< pair<Cost,int> > Q;
Q.push(make_pair(dis[S]=0, S));
while (!Q.empty()) {
int x = Q.top().second;
Cost d = -Q.top().first;
Q.pop();
// For double: dis[x] > d + EPS
if (dis[x] != d) continue;
for(int it = last[x]; it >= 0; it = edges[it].next)
if (edges[it].cap > 0 && dis[edges[it].to] > d + edges[it].cost)
Q.push(make_pair(-(dis[edges[it].to] = d + edges[it].cost), edges[it].to));
}
Cost disT = dis[T]; REP(i,n) dis[i] = disT - dis[i];
}
Flow findFlow(int x, Flow flow) {
if (x == T) {
totalCost += dis[S] * flow;
totalFlow += flow;
return flow;
}
visited[x] = 1;
Flow now = flow;
for(int it = last[x]; it >= 0; it = edges[it].next)
// For double: fabs(dis[edges[it].to] + edges[it].cost - dis[x]) < EPS
if (edges[it].cap && !visited[edges[it].to] && dis[edges[it].to] + edges[it].cost == dis[x]) {
Flow tmp = findFlow(edges[it].to, min(now, edges[it].cap));
edges[it].cap -= tmp;
edges[it ^ 1].cap += tmp;
now -= tmp;
if (!now) break;
}
return flow - now;
}
bool modifyLabel() {
Cost d = INF_COST;
REP(i,n) if (visited[i])
for(int it = last[i]; it >= 0; it = edges[it].next)
if (edges[it].cap && !visited[edges[it].to])
d = min(d, dis[edges[it].to] + edges[it].cost - dis[i]);
// For double: if (d > INF_COST / 10) INF_COST = 1e20
if (d == INF_COST) return false;
REP(i,n) if (visited[i])
dis[i] += d;
return true;
}
};
int n, k;
int a[211], c[211];
int cur[211], next[211];
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout << (fixed) << setprecision(9) << boolalpha;
while (cin >> n >> k) {
FOR(i,1,n) cin >> a[i];
FOR(i,1,n) cin >> c[i];
REP(i,211) cur[i] = n + 1;
FORD(i,n,1) {
next[i] = cur[a[i]];
cur[a[i]] = i;
}
MinCostFlow<int,int> flow(3*n + 10);
int source = 2 * n + 3;
int sink = 2*n + 2;
FOR(i,1,n) {
flow.addEdge(source, i - 1, 1, +c[a[i]]);
flow.addEdge(i, i+n, 1, 0);
int cost = (next[i] > n) ? 0 : -c[a[i]];
flow.addEdge(next[i] - 1, i+n, 1, cost);
flow.addEdge(i-1, i, k, 0);
flow.addEdge(i+n, sink, 1, 0);
}
auto t = flow.minCostFlow(source, sink);
assert(t.first == n);
cout << t.second << endl;
}
return 0;
}