structEdge { int to, next, w, fi; } e[1000000]; int c1, head[maxn]; inlinevoidadd_edge(int u, int v, int w, int fi){ e[c1].to = v; e[c1].w = w; e[c1].fi = fi; e[c1].next = head[u]; head[u] = c1++; }
inlinevoidAdd_edge(int u, int v, int w, int fi){ add_edge(u, v, w, fi); add_edge(v, u, 0, -fi); }
bool vis[maxn]; int s, t; int la[maxn], pre[maxn], dis[maxn]; boolspfa(){ fill(dis, dis + maxn, INF); dis[s] = 0; fill(vis, vis + maxn, 0); vis[s] = 1; deque<int> Q; Q.push_front(s); pre[t] = -1; while (!Q.empty()) { int u = Q.front(); Q.pop_front(); vis[u] = 0; for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].to, w = e[i].w, fi = e[i].fi; if (w > 0 && dis[v] > dis[u] + fi) { dis[v] = dis[u] + fi; pre[v] = u; la[v] = i; if (vis[v]) continue; vis[v] = 1; if (Q.empty() || dis[v] <= dis[Q.front()]) Q.push_front(v); else Q.push_back(v); } } } return ~pre[t]; } intmcmf(){ int mf = 0, mc = 0; while (spfa()) { int fl = INF; for (int now = t; now; now = pre[now]) fl = min(fl, e[la[now]].w); for (int now = t; now; now = pre[now]) e[la[now]].w -= fl, e[la[now] ^ 1].w += fl; mc += fl * dis[t]; mf += fl; } return mc; }
voidwork(){ cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; cin >> k >> l >> r; int m = n - k + 1; s = 0; t = m + 2; fill(head + s, head + t + 1, -1); c1 = 0; Add_edge(s, 1, r, 0); Add_edge(m + 1, t, r, 0); for (int i = 1; i <= n; ++i) Add_edge(max(1, i - k + 1), min(i, n - k + 1) + 1, 1, -a[i]); for (int i = 1; i <= m; ++i) Add_edge(i, i + 1, r - l, 0); cout << -mcmf() << "\n"; }
structEdge { int to, next, w, fi; } e[1000000]; int c1, head[maxn]; inlinevoidadd_edge(int u, int v, int w, int fi){ e[c1].to = v; e[c1].w = w; e[c1].fi = fi; e[c1].next = head[u]; head[u] = c1++; }
inlinevoidAdd_edge(int u, int v, int w, int fi){ add_edge(u, v, w, fi); add_edge(v, u, 0, -fi); }
int la[maxn], pre[maxn], fl[maxn], dis[maxn]; bool vis[maxn]; int s, t; boolspfa(){ fill(vis, vis + maxn, 0); vis[s] = 1; fill(dis, dis + maxn, INF); dis[s] = 0; queue<int> Q; Q.push(s); pre[t] = -1; fl[s] = INF; while (!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = 0; for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].to, w = e[i].w, fi = e[i].fi; if (w > 0 && dis[v] > dis[u] + fi) { dis[v] = dis[u] + fi; pre[v] = u; fl[v] = min(fl[u], w); la[v] = i; if (vis[v]) continue; vis[v] = 1; Q.push(v); } } } return ~pre[t]; }
intmcmf(){ int mc = 0, mf = 0; while (spfa()) { int now = t; mc += dis[t] * fl[t]; mf += fl[t]; while (now != s) { e[la[now]].w -= fl[t]; e[la[now] ^ 1].w += fl[t]; now = pre[now]; } } return mc; }
voidinit(int n){ fill(head, head + n + 1, -1); c1 = 0; }
voidwork(){ cin >> n; s = 0; t = n + 2; init(t); for (int i = 1; i <= n; ++i) cin >> a[i]; cin >> k >> l >> r; for (int i = 1; i <= n; ++i) { if (i < k) Add_edge(i, i + 1, INF, 0); else Add_edge(i, i + 1, r - l, 0); } for (int i = 1; i <= n; ++i) Add_edge(i, min(i + k, n + 1), 1, -a[i]); Add_edge(s, 1, r, 0); Add_edge(n + 1, t, r, 0); cout << -mcmf() << "\n"; }