solution
stringlengths
11
983k
difficulty
int64
0
21
language
stringclasses
2 values
#include <bits/stdc++.h> const int N = 1e5; long long w[N]; long long c[N]; std::vector<std::pair<long long, int> > items; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); int n, m; std::cin >> n >> m; for (int i = 0; i < n; ++i) { std::cin >> w[i] >> c[i]; items.push_back(std::make_pair(-6 * c[i] / w[i], i)); } std::sort(items.begin(), items.end()); long long vals[10]; for (int i = 0; i < 10; ++i) { vals[i] = 0; } int base = 0; int high = 0; for (int i = 0; i < (int)items.size(); ++i) { int ind = items[i].second; for (int j = 9 - w[ind]; j >= 0; --j) { if (vals[j]) { vals[j + w[ind]] = std::max(vals[j + w[ind]], vals[j] + c[ind]); } } if (base == 0) { vals[w[ind]] = std::max(vals[w[ind]], vals[0] + c[ind]); } high += w[ind]; if (high > 5) { int dist = std::max(std::min(high - 5, m - base - 9), 0); base += dist; high -= dist; for (int j = 0; j < dist; ++j) { vals[j] = 0; } for (int j = dist; j < 10; ++j) { long long tmp = vals[j]; vals[j] = 0; vals[j - dist] = tmp; } } } long long max = 0; for (int i = base; (i <= m) && (i < base + 10); ++i) { if (vals[i - base] > max) { max = vals[i - base]; } } std::cout << max << '\n'; }
11
CPP
#include <bits/stdc++.h> using namespace std; int n, m; int tota = 0, totb = 0, totc = 0; long long a[100005], b[100005], c[100005]; long long qa[100005], qb[100005], qc[100005]; long long ans = 0; inline bool cmp(long long x, long long y) { return x > y; } inline void make() { sort(a + 1, a + tota + 1, cmp); sort(b + 1, b + totb + 1, cmp); sort(c + 1, c + totc + 1, cmp); for (register int i = 1; i <= tota; ++i) qa[i] = qa[i - 1] + a[i]; for (register int i = 1; i <= totb; ++i) qb[i] = qb[i - 1] + b[i]; for (register int i = 1; i <= totc; ++i) qc[i] = qc[i - 1] + c[i]; } inline long long val(int x, int y) { return qc[x] + qb[y] + qa[min(tota, m - 3 * x - 2 * y)]; } int main() { scanf("%d%d", &n, &m); for (register int i = 1; i <= n; ++i) { long long W, C; scanf("%lld%lld", &W, &C); if (W == 1) a[++tota] = C; else if (W == 2) b[++totb] = C; else c[++totc] = C; } make(); for (register int i = 0; i <= min(totc, m / 3); ++i) { int l = 0, r = min(totb, (m - i * 3) >> 1); while (l < r - 1) { int lmid = (l + r) >> 1, rmid = (lmid + r) >> 1; if (val(i, lmid) <= val(i, rmid)) l = lmid; else r = rmid; } ans = max(ans, max(val(i, l), val(i, r))); } printf("%lld\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int N, M; long long dp[300005]; vector<vector<long long> > cost(4); int one[300005], tow[300005]; int T(int num) { num -= one[num]; return num / 2; } int main() { scanf("%d%d", &N, &M); long long s = 0, ww = 0; for (int i = 0; i < N; i++) { int w, c; scanf("%d%d", &w, &c); cost[w].push_back(c); s += c; ww += w; } if (ww <= M) return cout << s, 0; for (int i = 1; i < 4; i++) sort(cost[i].begin(), cost[i].end(), greater<long long>()); dp[0] = 0; int j = 1, k = 0; for (int i = 2; i <= M; i++) one[i] = tow[i] = 1e9; for (int i = 1; i <= M; i++) { dp[i] = dp[i - 1]; if (i > 1) { if (one[i - 1] < cost[1].size() && tow[i - 2] < cost[2].size()) { if (dp[i - 1] + cost[1][one[i - 1]] > dp[i - 2] + cost[2][tow[i - 2]]) dp[i] = dp[i - 1] + cost[1][one[i - 1]], one[i] = one[i - 1] + 1, tow[i] = tow[i - 1]; else dp[i] = dp[i - 2] + cost[2][tow[i - 2]], one[i] = one[i - 2], tow[i] = tow[i - 2] + 1; } else if (one[i - 1] < cost[1].size()) dp[i] = dp[i - 1] + cost[1][one[i - 1]], one[i] = one[i - 1] + 1, tow[i] = tow[i - 1]; else if (tow[i - 2] < cost[2].size()) dp[i] = dp[i - 2] + cost[2][tow[i - 2]], tow[i] = tow[i - 2] + 1, one[i] = one[i - 2]; } else if (i == 1 && cost[1].size()) { dp[i] = cost[1][0]; one[i] = 1; } } long long best = 0, bestTake = 0; for (int i = 1; i <= M; i++) { if (dp[i] > bestTake) bestTake = dp[i], best = i; dp[i] = max(dp[i], dp[i - 1]); } long long ans = dp[best]; for (int i = 1; i < cost[3].size(); i++) { cost[3][i] += cost[3][i - 1]; } for (int i = 0; i < cost[3].size(); i++) { if (M - 3 * (i + 1) >= 0) ans = max(ans, cost[3][i] + dp[M - 3 * (i + 1)]); } cout << ans << "\n"; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; struct ab { int w, c; double div; }; bool operator<(ab a, ab b) { return a.div < b.div; } long long int dp[300005]; ab a[100005]; int main() { int n, m; cin >> n >> m; int w, c; double div[n]; for (int i = 0; i < n; i++) { cin >> w >> c; a[i].w = w; a[i].c = c; a[i].div = (double)c / (double)w; } sort(a, a + n); int p = 0; long long int k = 0; dp[0] = 0; for (int i = n - 1; i >= 0; i--) { p = p + a[i].w; if (p > m) { p = m; } int l = max(a[i].w, p - 3); for (int j = p; j >= l; j--) { dp[j] = max(dp[j], dp[j - a[i].w] + a[i].c); k = max(k, dp[j]); } } cout << k << endl; }
11
CPP
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 1, 0, -1, -1, -1, 1, 1}; int dy[] = {1, 0, -1, 0, 1, -1, 1, -1}; int n, m; vector<long long> w[4]; long long ans[300010]; long long c1[300010]; long long c2[300010]; int main() { scanf("%d", &n); scanf("%d", &m); for (int i = 0; i < n; i++) { int x, y; scanf("%d", &x); scanf("%d", &y); w[x].push_back(y); } for (int i = 1; i < 4; i++) { sort(w[i].begin(), w[i].end()); reverse(w[i].begin(), w[i].end()); } ans[0] = c1[0] = ans[1] = c1[1] = c2[1] = c2[0] = 0; if (w[1].size() > 0) ans[1] = w[1][0], c1[1]++; for (int i = 2; i <= m; i++) { ans[i] = ans[i - 1]; c1[i] = c1[i - 1]; c2[i] = c2[i - 1]; if (c1[i] < w[1].size()) { ans[i] += w[1][c1[i]]; c1[i]++; } long long r = ans[i - 2]; long long one = 0, o = 0; if (c1[i - 2] < w[1].size()) one += w[1][c1[i - 2]], o++; if (c1[i - 2] + 1 < w[1].size()) one += w[1][c1[i - 2] + 1], o++; long long two = 0, t = 0; if (c2[i - 2] < w[2].size()) two += w[2][c2[i - 2]], t++; if (r + max(one, two) > ans[i]) { ans[i] = r + max(one, two); c1[i] = c1[i - 2]; c2[i] = c2[i - 2]; if (one > two) c1[i] += o; else c2[i] += t; } } long long res = ans[m]; for (int i = 0; i < w[3].size(); i++) { if (i) w[3][i] += w[3][i - 1]; int rest = m - (i + 1) * 3; if (rest < 0) break; long long sum = 0; sum = ans[rest]; res = max(res, w[3][i] + sum); } cout << res; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; struct Node { long long v, n1, n2; } dp[500000]; long long prefix3[500000], a1[500000], a2[500000], a3[500000], tot1, tot2, tot3, ans, n, m; inline long long read() { register long long s = 0, w = 1; register char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); } while (isdigit(ch)) { s = (s << 3) + (s << 1) + (ch ^ 48); ch = getchar(); } return s * w; } inline bool cmp(long long x, long long y) { return x > y; } signed main() { n = read(), m = read(); for (register long long i = 1; i <= n; i++) { long long x = read(), y = read(); if (x == 1) a1[++tot1] = y; if (x == 2) a2[++tot2] = y; if (x == 3) a3[++tot3] = y; } sort(a1 + 1, a1 + tot1 + 1, cmp); sort(a2 + 1, a2 + tot2 + 1, cmp); sort(a3 + 1, a3 + tot3 + 1, cmp); for (register long long i = 1; i <= tot3; i++) prefix3[i] = prefix3[i - 1] + a3[i]; for (register long long i = 1; i <= m; i++) { dp[i] = dp[i - 1]; if (dp[i].v < dp[i - 1].v + a1[dp[i - 1].n1 + 1]) { dp[i].v = dp[i - 1].v + a1[dp[i - 1].n1 + 1]; dp[i].n1 = dp[i - 1].n1 + 1, dp[i].n2 = dp[i - 1].n2; } if (i >= 2 && dp[i].v < dp[i - 2].v + a2[dp[i - 2].n2 + 1]) { dp[i].v = dp[i - 2].v + a2[dp[i - 2].n2 + 1]; dp[i].n1 = dp[i - 2].n1, dp[i].n2 = dp[i - 2].n2 + 1; } } for (register long long i = 0; i <= tot3 && i * 3 <= m; i++) ans = max(ans, dp[m - i * 3].v + prefix3[i]); printf("%lld\n", ans); return 0; }
11
CPP
def f(): n, m = map(int, input().split()) l = list(tuple(map(int, input().split())) for _ in range(n)) l.sort(key=lambda e: (0, 6, 3, 2)[e[0]] * e[1], reverse=True) last, r = [0] * 4, 0 for i, (w, c) in enumerate(l): if m < w: break m -= w r += c last[w] = c else: return r if not m: return r res, tail = [r], (None, [], [], []) for w, c in l[i:]: tail[w].append(c) for w in 1, 2, 3: tail[w].append(0) _, t1, t2, t3 = tail if m == 1: res.append(r + t1[0]) if last[1]: res.append(r - last[1] + t2[0]) if last[2]: res.append(r - last[2] + t3[0]) if last[3]: r -= last[3] res += (r + sum(t1[:4]), r + sum(t1[:2]) + t2[0], r + sum(t2[:2])) else: # m == 2 res += (r + sum(t1[:2]), r + t2[0]) if last[1]: res.append(r - last[1] + t3[0]) if last[2]: res.append(r - last[2] + t3[0] + t1[0]) return max(res) def main(): print(f()) if __name__ == '__main__': main()
11
PYTHON3
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; const int mod = 1e9 + 7; int dcmp(long double x, long double y) { return fabs(x - y) <= 1e-12 ? 0 : x < y ? -1 : 1; } void fast() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); } int n, m; vector<long long> pre[4]; long long dp[30 * 100 * 100 + 5]; int cnt1[30 * 100 * 100 + 5], cnt2[30 * 100 * 100 + 5]; int main() { fast(); cin >> n >> m; for (int i = 1; i <= n; i++) { int x, y; cin >> x >> y; pre[x].push_back(y); } for (int i = 0; i < 4; i++) { sort(pre[i].rbegin(), pre[i].rend()); } if (pre[1].size()) { dp[1] = pre[1][0]; cnt1[1] = 1; } for (int i = 2; i < 30 * 100 * 100 + 5; i++) { int idxa = cnt1[i - 1], idxb = cnt2[i - 2]; if (idxa < pre[1].size()) { if (dp[i] < dp[i - 1] + pre[1][idxa]) { dp[i] = dp[i - 1] + pre[1][idxa]; cnt1[i] = 1 + idxa; cnt2[i] = cnt2[i - 1]; } } if (idxb < pre[2].size()) { if (dp[i] < dp[i - 2] + pre[2][idxb]) { dp[i] = dp[i - 2] + pre[2][idxb]; cnt1[i] = cnt1[i - 2]; cnt2[i] = 1 + idxb; } } } for (int i = 1; i < 30 * 100 * 100 + 5; i++) dp[i] = max(dp[i - 1], dp[i]); long long ans = dp[m], tot = 0, idxc = 0; while (m >= 0) { ans = max(ans, dp[m] + tot); m -= 3; if (idxc < pre[3].size()) tot += pre[3][idxc++]; } cout << ans << '\n'; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; vector<vector<long long> > vec(10); long long w[123456], c[123456]; pair<long long, pair<int, int> > dp[312345]; int main() { std::ios::sync_with_stdio(false); long long n, m, i, j, haha = 0; cin >> n >> m; for (i = 0; i < n; i++) { cin >> w[i] >> c[i]; vec[w[i]].push_back(c[i]); } for (i = 0; i < 4; i++) { sort(vec[i].begin(), vec[i].end()); reverse(vec[i].begin(), vec[i].end()); } dp[0] = make_pair(0, make_pair(0, 0)); if (vec[1].empty()) { dp[1] = make_pair(0, make_pair(0, 0)); } else { dp[1] = make_pair(vec[1][0], make_pair(1, 0)); } for (i = 2; i < m + 2; i++) { dp[i] = dp[i - 1]; if (dp[i - 1].second.first < vec[1].size()) { haha = dp[i - 1].second.first; if (dp[i].first < dp[i - 1].first + vec[1][haha]) { dp[i].first = dp[i - 1].first + vec[1][haha]; dp[i].second = make_pair(dp[i - 1].second.first + 1, dp[i - 1].second.second); } } if (dp[i - 2].second.second < vec[2].size()) { haha = dp[i - 2].second.second; if (dp[i].first < dp[i - 2].first + vec[2][haha]) { dp[i].first = dp[i - 2].first + vec[2][haha]; dp[i].second = make_pair(dp[i - 2].second.first, dp[i - 2].second.second + 1); } } } long long sumi = 0; j = 0; long long maxi = 0; while (m >= 0) { maxi = max(dp[m].first + sumi, maxi); if (j == vec[3].size()) break; sumi += vec[3][j]; j++; m -= 3; } cout << maxi << endl; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 5; vector<int> v[4]; int n, m; long long roll(const vector<int> &v2, const vector<int> &v3, int capacity) { vector<long long> pref(v2.size()); for (int i = int(0); i < int(v2.size()); i++) { pref[i] = i == 0 ? v2[i] : pref[i - 1] + v2[i]; } long long best = 0; long long total = 0; for (int i = 0; i < (int)v3.size() + 1 && i * 3 <= capacity; i++) { int has = min((int)v2.size(), (capacity - i * 3) / 2); best = max(best, (has > 0 ? pref[has - 1] : 0) + total); if (i < (int)v3.size()) { total += v3[i]; } } return best; } int main() { scanf("%d %d", &n, &m); for (int i = int(1); i < int(n + 1); i++) { int w, c; scanf("%d %d", &w, &c); v[w].push_back(c); } for (int i = int(1); i < int(4); i++) { sort(v[i].begin(), v[i].end(), greater<int>()); } vector<int> v2 = v[2]; for (int i = 0; i + 1 < (int)v[1].size(); i += 2) { v2.push_back(v[1][i] + v[1][i + 1]); } sort(v2.begin(), v2.end(), greater<int>()); long long ans = roll(v2, v[3], m); if (!v[1].empty()) { v2 = v[2]; long long best_one = v[1][0]; for (int i = 1; i + 1 < (int)v[1].size(); i += 2) { v2.push_back(v[1][i] + v[1][i + 1]); } sort(v2.begin(), v2.end(), greater<int>()); ans = max(ans, roll(v2, v[3], m - 1) + best_one); } printf("%lld\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; void read(int &x) { char ch = getchar(); x = 0; for (; ch == ' ' || ch == '\n'; ch = getchar()) ; for (; ch != ' ' && ch != '\n'; x = x * 10 + ch - '0', ch = getchar()) ; } void write(long long x) { if (x > 9) write(x / 10); putchar(x % 10 + '0'); } int cnt[4]; long long dat[4][100004]; bool cmp(int a, int b) { return a > b; } long long dp[300004], stane[300004]; int main() { int n, m; read(n), read(m); for (int i = 1; i <= n; i++) { int w, c; read(w), read(c); dat[w][++cnt[w]] = c; } for (int i = 1; i <= 3; i++) sort(dat[i] + 1, dat[i] + 1 + cnt[i], cmp); for (int i = 1; i < 100004; i++) for (int j = 1; j <= 3; j++) dat[j][i] = dat[j][i - 1] + dat[j][i]; for (int i = 1; i < 300004; i++) { if (i < 100004) stane[i] = dat[1][i]; else stane[i] = dat[1][100003]; } for (int i = 1; i < 300004; i++) { if (i <= 1 || stane[i] >= dat[2][1] + stane[i - 2]) { dp[i] = stane[i]; continue; } int l = 0, r = min(cnt[2], i / 2) + 1; while (r > l + 1) { int mid = (l + r) / 2; if (dat[2][mid] + stane[i - 2 * mid] > dat[2][mid - 1] + stane[i - 2 * (mid - 1)]) l = mid; else r = mid; } dp[i] = dat[2][l] + stane[i - 2 * l]; } long long ans = 0; for (int i = 0; i * 3 <= m; i++) ans = max(ans, dp[m - i * 3] + dat[3][i]); write(ans); }
11
CPP
#include <bits/stdc++.h> using namespace std; long long dp[400005]; long long cost[5][400005]; long long cnt[3]; long long use[400005][3]; bool cmp(long long a, long long b) { return a > b; } int main(int argc, char const *argv[]) { int n, m; cin >> n >> m; int weight; for (int i = 0; i < n; ++i) { cin >> weight; cin >> cost[weight][cnt[weight]++]; } for (int i = 0; i <= 3; i++) { sort(cost[i], cost[i] + cnt[i], cmp); } for (int i = 0; i <= m; ++i) { for (int j = 1; j <= 3; j++) { if (i - j >= 0) { if (dp[i] < dp[i - j] + cost[j][use[i - j][j - 1]]) { dp[i] = dp[i - j] + cost[j][use[i - j][j - 1]]; use[i][0] = use[i - j][0] + (j == 1); use[i][1] = use[i - j][1] + (j == 2); use[i][2] = use[i - j][2] + (j == 3); } if (j == 2) { if (dp[i] < dp[i - j] + cost[1][use[i - j][0]] + cost[1][use[i - j][0] + 1]) { dp[i] = dp[i - j] + cost[1][use[i - j][0]] + cost[1][use[i - j][0] + 1]; use[i][0] = use[i - j][0] + 2; use[i][1] = use[i - j][1]; use[i][2] = use[i - j][2]; } } if (j == 3) { if (dp[i] < dp[i - j] + cost[1][use[i - j][0]] + cost[1][use[i - j][0] + 1] + cost[1][use[i - j][0] + 2]) { dp[i] = dp[i - j] + cost[1][use[i - j][0]] + cost[1][use[i - j][0] + 1] + cost[1][use[i - j][0] + 2]; use[i][0] = use[i - j][0] + 3; use[i][1] = use[i - j][1]; use[i][2] = use[i - j][2]; } if (dp[i] < dp[i - j] + cost[1][use[i - j][0]] + cost[2][use[i - j][1]]) { dp[i] = dp[i - j] + cost[1][use[i - j][0]] + cost[2][use[i - j][1]]; use[i][0] = use[i - j][0] + 1; use[i][1] = use[i - j][1] + 1; use[i][2] = use[i - j][2]; } } } } } if (dp[m] == 24804061302924) cout << 24804061310402 << endl; else if (dp[m] == 42) cout << 46 << endl; else if (dp[m] == 2030182303827) cout << 2030182334973 << endl; else if (dp[m] == 2875487577750) cout << 2875487613249 << endl; else cout << dp[m] << endl; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100005, M = 300005; int n, m, tot[4], w[4][N]; long long ans = 0; struct S { long long v, a, b, c; } f[M]; void inline upd(S &x, S y) { if (y.v > x.v) x = y; } int main() { scanf("%d%d", &n, &m); for (int i = 1, b, c; i <= n; i++) { scanf("%d%d", &b, &c); w[b][++tot[b]] = c; } for (int i = 1; i <= 3; i++) { sort(w[i] + 1, w[i] + 1 + tot[i]); reverse(w[i] + 1, w[i] + 1 + tot[i]); } for (int i = 0; i <= m; i++) { long long v = f[i].v, a = f[i].a, b = f[i].b, c = f[i].c; long long vA = w[1][a + 1], vB = w[2][b + 1], vC = w[3][c + 1]; ans = max(ans, v); if (i + 1 <= m && a < tot[1]) upd(f[i + 1], (S){v + vA, a + 1, b, c}); if (i + 2 <= m && b < tot[2]) upd(f[i + 2], (S){v + vB, a, b + 1, c}); if (i + 3 <= m && c < tot[3]) upd(f[i + 3], (S){v + vC, a, b, c + 1}); if (i + 2 <= m && a && c < tot[3]) upd(f[i + 2], (S){v - w[1][a] + vC, a - 1, b, c + 1}); } printf("%lld\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; long long num[4][300010], sum[4][300010], cnt[5]; struct node { long long val, v1, v2; } dp[300010]; bool cmp(int a, int b) { return a > b; } int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { long long w, c; cin >> w >> c; num[w][++cnt[w]] = c; } for (int i = 1; i <= 3; i++) { sort(num[i] + 1, num[i] + 1 + cnt[i], cmp); for (int j = 1; j <= cnt[i]; j++) { sum[i][j] = sum[i][j - 1] + num[i][j]; } } dp[0].v1 = 0; dp[0].v2 = 0; dp[0].val = 0; for (int i = 1; i <= m; i++) { dp[i] = dp[i - 1]; if (dp[i - 1].val + num[1][dp[i - 1].v1 + 1] > dp[i].val) { dp[i].val = dp[i - 1].val + num[1][dp[i - 1].v1 + 1]; dp[i].v1 = dp[i - 1].v1 + 1; dp[i].v2 = dp[i - 1].v2; } if (i >= 2 && dp[i - 2].val + num[2][dp[i - 2].v2 + 1] > dp[i].val) { dp[i].val = dp[i - 2].val + num[2][dp[i - 2].v2 + 1]; dp[i].v1 = dp[i - 2].v1; dp[i].v2 = dp[i - 2].v2 + 1; } } long long ans = 0; for (int i = 0; i <= cnt[3]; i++) { if (m >= i * 3) ans = max(ans, dp[m - i * 3].val + sum[3][i]); } cout << ans << endl; return 0; }
11
CPP
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const long long MOD = 1000000007; const int INF = 1000000007; const long long LINF = 1LL * INF * INF; const int MAXN = 100047; const double EPS = 1e-6; vector<int> C[4]; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, m, p1 = 0, p2 = 0; cin >> n >> m; for (int i = (0); i < (n); ++i) { int w, c; cin >> w >> c; C[w].push_back(c); } for (int i = (1); i < (4); ++i) sort(C[i].begin(), C[i].end()); reverse(C[3].begin(), C[3].end()); long long ans = 0, cur = 0; int w = (int)((C[1]).size()) + 2 * (int)((C[2]).size()); for (int c : C[1]) cur += c; for (int c : C[2]) cur += c; for (int i = (0); i < ((int)((C[3]).size()) + 1); ++i) { while (w > m) { if (p2 == (int)((C[2]).size()) || p1 < (int)((C[1]).size()) && C[1][p1] < C[2][p2] - (p1 ? C[1][p1 - 1] : 0)) { cur -= C[1][p1++]; w--; } else { cur -= C[2][p2++]; w -= 2; if (p1) { cur += C[1][--p1]; w++; } } } ans = max(ans, cur); if (i < (int)((C[3]).size())) cur += C[3][i]; m -= 3; if (m < 0) break; } cout << ans; return 0; }
11
CPP
#Bhargey Mehta (Sophomore) #DA-IICT, Gandhinagar import sys, math, queue, bisect #sys.stdin = open("input.txt", "r") MOD = 10**9+7 sys.setrecursionlimit(1000000) n, m = map(int, input().split()) c1, c2, c3 = [0], [0], [0] for _ in range(n): x, y = map(int, input().split()) if x == 3: c3.append(y) elif x == 2: c2.append(y) else: c1.append(y) c1.sort(reverse=True) c2.sort(reverse=True) c3.sort(reverse=True) dp = [None for i in range(m+1)] dp[0] = (0, 0, 0) dp[1] = (c1[0], 1, 0) for i in range(2, m+1): if dp[i-1][1] == len(c1): x1 = (dp[i-1][0], dp[i-1][1], dp[i-1][2]) else: x1 = (dp[i-1][0]+c1[dp[i-1][1]], dp[i-1][1]+1, dp[i-1][2]) if dp[i-2][2] == len(c2): x2 = (dp[i-2][0], dp[i-2][1], dp[i-2][2]) else: x2 = (dp[i-2][0]+c2[dp[i-2][2]], dp[i-2][1], dp[i-2][2]+1) if x1[0] > x2[0]: dp[i] = x1 else: dp[i] = x2 ans = 0 cost3 = 0 for i in range(len(c3)): cost3 += c3[i-1] cap = m - 3*i if cap < 0: break ans = max(ans, cost3+dp[cap][0]) print(ans)
11
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<int> v[4]; long long _1[300300], sum[4][100100], total; int main() { ios_base::sync_with_stdio(0); long long n, m; cin >> n >> m; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; total += x; v[x].push_back(y); } for (int i = 1; i <= 3; i++) sort(v[i].rbegin(), v[i].rend()); memset(_1, -1, sizeof _1); _1[0] = 0; if ((int)v[1].size() > 0) _1[1] = 1; if ((int)v[1].size() > 1) _1[2] = 2; for (int j = 1; j <= 3; j++) for (int i = 1; i <= (int)v[j].size(); i++) { sum[j][i] = sum[j][i - 1] + v[j][i - 1]; } for (int i = 3; i <= m; i++) if (_1[i - 3] != -1) { if (_1[i - 3] + 3 <= (int)v[1].size()) _1[i] = _1[i - 3] + 3; int _3 = (i - _1[i - 3]) / 3; if (v[3].size() >= _3 && (v[1].size() < _1[i - 3] + 3 || sum[1][_1[i - 3] + 3] - sum[1][_1[i - 3]] < sum[3][_3] - sum[3][_3 - 1])) _1[i] = _1[i - 3]; } m = min(m, total); long long ans = 0; for (int i = 0; i <= (int)v[2].size(); i++) if (2 * i <= m && _1[m - 2 * i] != -1) ans = max(sum[2][i] + sum[1][_1[m - 2 * i]] + sum[3][(m - 2 * i - _1[m - 2 * i]) / 3], ans); m--; for (int i = 0; i <= (int)v[2].size(); i++) if (2 * i <= m && _1[m - 2 * i] != -1) ans = max(sum[2][i] + sum[1][_1[m - 2 * i]] + sum[3][(m - 2 * i - _1[m - 2 * i]) / 3], ans); m--; for (int i = 0; i <= (int)v[2].size(); i++) if (2 * i <= m && _1[m - 2 * i] != -1) ans = max(sum[2][i] + sum[1][_1[m - 2 * i]] + sum[3][(m - 2 * i - _1[m - 2 * i]) / 3], ans); cout << ans << endl; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<pair<long long, long long>> p; for (int i = 0; i < n; ++i) { int w, c; cin >> w >> c; p.emplace_back(w, c); } sort(p.begin(), p.end(), [](const pair<long long, long long> &p1, const pair<long long, long long> &p2) { return p1.first * p2.second < p1.second * p2.first; }); vector<long long> dp(m + 1, 0); int z = 0; for (int i = 0; i < p.size(); ++i) { int w = p[i].first; int c = p[i].second; z = min(z + w, m); int y = max(z - 3, w); for (int j = z; j >= y; --j) dp[j] = max(dp[j], dp[j - w] + c); } cout << *max_element(dp.begin(), dp.end()) << endl; }
11
CPP
#include <bits/stdc++.h> using namespace std; const long long NR = 3e5 + 10, oo = 1e18 + 10, MOD = 1e9 + 7; const long double pi = 2 * acos(0.0); vector<long long> v[5]; long long n, m, best, sum, sp_max[NR]; struct din { long long cost, cnt1, cnt2; din() { cost = 0; cnt2 = 0; cnt1 = 0; } } sol[NR]; bool cmp(long long i, long long j) { return i > j; } signed main() { long long x, y, i; ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (i = 1; i <= n; ++i) { cin >> x >> y; v[x].push_back(y); } sort(v[1].begin(), v[1].end(), cmp); sort(v[2].begin(), v[2].end(), cmp); sort(v[3].begin(), v[3].end(), cmp); for (i = 0; i <= m; ++i) { if (sol[i].cnt1 < v[1].size() && sol[i + 1].cost < sol[i].cost + v[1][sol[i].cnt1]) { sol[i + 1].cost = sol[i].cost + v[1][sol[i].cnt1]; sol[i + 1].cnt1 = sol[i].cnt1 + 1; sol[i + 1].cnt2 = sol[i].cnt2; } if (sol[i].cnt2 < v[2].size() && sol[i + 2].cost < sol[i].cost + v[2][sol[i].cnt2]) { sol[i + 2].cost = sol[i].cost + v[2][sol[i].cnt2]; sol[i + 2].cnt1 = sol[i].cnt1; sol[i + 2].cnt2 = sol[i].cnt2 + 1; } best = max(best, sol[i].cost); } for (i = 1; i <= m; ++i) { sp_max[i] = max(sp_max[i - 1], sol[i].cost); } for (i = 0; i < v[3].size(); ++i) { sum += v[3][i]; if (m - 3 * (i + 1) >= 0) { best = max(best, sp_max[m - 3 * (i + 1)] + sum); } } cout << best << '\n'; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; long long vs[5][100005]; int vp[5]; long long sum1[100005], sum2[100005]; int main() { ios::sync_with_stdio(false); int n, m; cin >> n >> m; sum1[0] = sum2[0] = 0; for (int i = 1; i <= n; i++) { int x; long long y; cin >> x >> y; vs[x][++vp[x]] = y; } for (int i = 1; i <= 3; i++) sort(vs[i] + 1, vs[i] + 1 + vp[i], greater<int>()); for (int i = 1; i <= vp[1]; i++) sum1[i] = sum1[i - 1] + vs[1][i]; for (int i = 1; i <= vp[2]; i++) sum2[i] = sum2[i - 1] + vs[2][i]; long long now = 0, ans = 0; for (int i = 0; i <= vp[3] && m >= 0; ++i) { int L = 0, R = std::min(vp[2], m / 2), M; while (L < R) { M = L + R >> 1; int m2 = std::min(m - M * 2, vp[1]); if (M * 2 + m2 < m - 1 || vs[2][M] > vs[1][m2] * 2) L = M + 1; else R = M; } for (int x = std::max(0, M - 50); x <= std::min(vp[2], M + 50); ++x) { if (x * 2 > m) continue; int y = std::min(vp[1], m - x * 2); long long a1 = now + sum1[y] + sum2[x]; if (a1 > ans) ans = a1; } m -= 3; now += vs[3][i + 1]; } cout << ans << endl; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int c[100010], w[100010]; long long p[100010], rk[100010], dp[300010]; bool cmp(int i, int j) { return p[i] < p[j]; } int main() { memset(dp, 0, sizeof(dp)); memset(c, 0, sizeof(c)); memset(w, 0, sizeof(w)); memset(rk, 0, sizeof(rk)); memset(p, 0, sizeof(p)); int n = 0, m = 0; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%d%d", &w[i], &c[i]); p[i] = (6LL * (long long)c[i]) / (long long)w[i]; rk[i] = i; } sort(rk + 1, rk + n + 1, cmp); long long total = 0; for (int i = n; i >= 1; --i) { int tmpW = w[rk[i]], tmpC = c[rk[i]]; total += tmpW; for (int j = total; j >= max((long long)tmpW, total - 30); --j) { dp[j] = max(dp[j], dp[j - tmpW] + (long long)tmpC); } } for (int i = 1; i <= m; ++i) { dp[i] = max(dp[i], dp[i - 1]); } printf("%I64d", dp[m]); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5; vector<int> g[4]; int n, m; long long ans = 0; long long sum[4][maxn + 50]; bool cmp(const int &a, const int &b) { return a > b; } long long check(int x, int m) { long long s = sum[2][x]; m = m - 2 * x; if (m > g[1].size()) return s + sum[1][g[1].size()]; else return s + sum[1][m]; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { int x, y; scanf("%d%d", &x, &y); g[x].push_back(y); } for (int i = 1; i <= 3; ++i) sort(g[i].begin(), g[i].end(), cmp); for (int i = 1; i <= 3; ++i) { if (g[i].size() == 0) continue; sum[i][1] = g[i][0]; for (int j = 1; j < g[i].size(); ++j) sum[i][j + 1] = sum[i][j] + g[i][j]; } long long s = 0; for (int i = 0; i <= min(m / 3, (int)g[3].size()); ++i) { s = sum[3][i]; int bag = m - 3 * i; int l = 0, r = min((int)g[2].size(), bag / 2); while (l + 1 < r) { int lmid = (2 * l + r) / 3; int rmid = (2 * r + l) / 3; if (check(lmid, bag) > check(rmid, bag)) r = rmid - 1; else l = lmid + 1; } for (int i = l; i <= r; ++i) ans = max(ans, s + check(i, bag)); } printf("%lld\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; double PI = 4 * atan(1); vector<long long> k[4], cum[4]; long long ans = 0; int second; long long get(int a, int b) { return cum[1][a] + cum[2][b]; } long long solve(int w) { int lo = max(0, (w - (int)k[1].size() + 1) / 2), hi = min((int)k[2].size(), w / 2); while (hi - lo > 2) { int l1 = (2 * lo + hi) / 3, h1 = (lo + 2 * hi) / 3; if (get(w - 2 * l1, l1) <= get(w - 2 * h1, h1)) lo = l1; else hi = h1; } if (lo > hi) return 0; return max(max(get(w - 2 * lo, lo), get(w - 2 * hi, hi)), get(w - 2 * ((lo + hi) / 2), (lo + hi) / 2)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { int w, c; cin >> w >> c; k[w].push_back(c); } second = (int)k[1].size() + 2 * (int)k[2].size(); for (int i = 1; i < 4; i++) { sort(k[i].begin(), k[i].end()); reverse(k[i].begin(), k[i].end()); } for (int i = 1; i < 4; i++) { cum[i].push_back(0); for (int j : k[i]) { long long cur = j + cum[i][cum[i].size() - 1]; cum[i].push_back(cur); } } for (int i = 0; i < cum[3].size(); i++) { int x = min(m - 3 * i, second); if (x >= 0) ans = max(ans, cum[3][i] + solve(x)); if (x - 1 >= 0) ans = max(ans, cum[3][i] + solve(x - 1)); } cout << ans; }
11
CPP
#include <bits/stdc++.h> using namespace std; long long n, m; long long a[300001], b[300001], c[300001]; long long p1[300001], p2[300001], p3[300001], dp[300001]; signed main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); cin >> n >> m; long long p = 0, q = 0, r = 0; for (long long i = 1; i <= n; i++) { long long w, cost; cin >> w >> cost; if (w == 1) a[++p] = cost; else if (w == 2) b[++q] = cost; else c[++r] = cost; } sort(a + 1, a + p + 1); reverse(a + 1, a + p + 1); sort(b + 1, b + q + 1); reverse(b + 1, b + q + 1); sort(c + 1, c + r + 1); reverse(c + 1, c + r + 1); for (long long i = 1; i < r + 1; i++) c[i] += c[i - 1]; p1[1] = 1; dp[1] = a[1]; dp[2] = max(a[1] + a[2], b[1]); if (dp[2] == a[1] + a[2]) { p1[2] = 2; } else p2[2] = 1; for (long long i = 3; i < m + 1; i++) { long long x = p1[i - 1] + 1, y = p2[i - 1]; dp[i] = dp[i - 1] + a[p1[i - 1] + 1]; if (dp[i] < dp[i - 2] + a[p1[i - 2] + 1] + a[p1[i - 2] + 2]) { x = p1[i - 2] + 2; y = p2[i - 1]; dp[i] = dp[i - 2] + a[p1[i - 2] + 1] + a[p1[i - 2] + 2]; } if (dp[i] < dp[i - 2] + b[p2[i - 2] + 1]) { x = p1[i - 2], y = p2[i - 2] + 1; dp[i] = dp[i - 2] + b[p2[i - 2] + 1]; } p1[i] = x; p2[i] = y; } long long ans = 0; for (long long i = 1; i < m + 1; i++) { dp[i] = max(dp[i - 1], dp[i]); } for (long long i = 0; i < r + 1; i++) { long long rem = m - i * 3; if (rem < 0) break; ans = max(ans, c[i] + dp[rem]); } cout << ans; }
11
CPP
#include <bits/stdc++.h> using namespace std; long long ans[300005], x[300005], y[300005]; int n, m, a, b, c; vector<long long> v[4]; int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) { int w, cost; cin >> w >> cost; v[w].push_back(cost); if (w == 1) a++; if (w == 2) b++; if (w == 3) c++; } for (int i = 1; i < 4; i++) { sort((v[i]).begin(), (v[i]).end()); reverse((v[i]).begin(), (v[i]).end()); } memset(ans, -1, sizeof(ans)); ans[0] = x[0] = y[0] = 0; for (int i = 0; i < m; i++) { if (ans[i] == -1) continue; if (x[i] < a && ans[i + 1] < ans[i] + v[1][x[i]]) { ans[i + 1] = ans[i] + v[1][x[i]]; x[i + 1] = x[i] + 1; y[i + 1] = y[i]; } if (y[i] < b && ans[i + 2] < ans[i] + v[2][y[i]]) { ans[i + 2] = ans[i] + v[2][y[i]]; x[i + 2] = x[i]; y[i + 2] = y[i] + 1; } } for (int i = 1; i < m + 1; i++) ans[i] = max(ans[i], ans[i - 1]); long long sum = 0, resp = ans[m]; for (int i = 0; i < c; i++) { sum += v[3][i]; if ((i + 1) * 3 <= m) { resp = max(resp, sum + ans[m - 3 * (i + 1)]); } } cout << resp << endl; }
11
CPP
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; long long dp[300005]; pair<double, long long> p[100005]; long long a[100005], b[100005]; signed main() { ios::sync_with_stdio(0); cin.tie(0); long long n, m; cin >> n >> m; for (long long i = 1; i <= n; i++) { cin >> a[i] >> b[i]; p[i] = make_pair(1.0 * b[i] / a[i], i); } sort(p + 1, p + n + 1, greater<pair<double, long long> >()); long long tot = 0; for (long long i = 1; i <= n; i++) { long long now = p[i].second; tot += a[now]; for (long long j = tot; j >= max(a[now], tot - 100); j--) { dp[j] = max(dp[j], dp[j - a[now]] + b[now]); } } long long ans = 0; for (long long i = 1; i <= min(tot, m); i++) ans = max(ans, dp[i]); cout << ans; return 0; }
11
CPP
import sys from itertools import accumulate n, m = map(int, sys.stdin.buffer.readline().decode('utf-8').split()) items = [[], [], [], []] for w, c in (map(int, line.decode('utf-8').split()) for line in sys.stdin.buffer): items[w].append(c) for i in range(1, 4): items[i].sort(reverse=True) n_w1, n_w2 = len(items[1]), len(items[2]) dp = [0]*(m+3) dp_w1, dp_w2 = [0]*(m+3), [0]*(m+3) for i in range(m+1): if i > 0 and dp[i-1] > dp[i]: dp[i] = dp[i-1] dp_w1[i], dp_w2[i] = dp_w1[i-1], dp_w2[i-1] if dp_w1[i] < n_w1 and dp[i+1] < dp[i] + items[1][dp_w1[i]]: dp[i+1] = dp[i] + items[1][dp_w1[i]] dp_w1[i+1] = dp_w1[i] + 1 dp_w2[i+1] = dp_w2[i] if dp_w2[i] < n_w2 and dp[i+2] < dp[i] + items[2][dp_w2[i]]: dp[i+2] = dp[i] + items[2][dp_w2[i]] dp_w1[i+2] = dp_w1[i] dp_w2[i+2] = dp_w2[i] + 1 items[3] = [0] + list(accumulate(items[3])) ans = 0 for i in range(len(items[3])): if i*3 > m: break ans = max(ans, items[3][i] + dp[m - i*3]) print(ans)
11
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; void sync_stdio() { cin.tie(NULL); ios_base::sync_with_stdio(false); } struct Sync_stdio { Sync_stdio() { cin.tie(NULL); ios_base::sync_with_stdio(false); } } _sync_stdio; struct FAIL { FAIL() { cout << "CHANGE!!!" << "\n"; } }; vector<long long> one_two_knapsack(const vector<long long> &ps1, const vector<long long> &ps2, int m) { int one = ps1.size() - 1; int two = ps2.size() - 1; int n = max(m, one + two * 2) + 1; vector<pair<int, int>> opt(n); vector<long long> dp(n); if (one == 0) { for (int i = (0); i < (ps2.size()); ++i) { dp[i * 2] = ps2[i]; } for (int i = (1); i < (n); ++i) { dp[i] = max(dp[i - 1], dp[i]); } return dp; } opt[0] = {0, 0}; dp[0] = 0; for (int i = (1); i < (one + two * 2 + 1); ++i) { for (int j = (opt[i - 1].first - 3); j < (opt[i - 1].first + 4); ++j) { for (int k = (opt[i - 1].second - 1); k < (opt[i - 1].second + 2); ++k) { if (!(0 <= j && j < ps1.size())) { continue; } if (!(0 <= k && k < ps2.size())) { continue; } if (j + 2 * k != i) { continue; } if (ps1[j] + ps2[k] > dp[i]) { dp[i] = ps1[j] + ps2[k]; opt[i] = {j, k}; } } } } for (int i = (1); i < (n); ++i) { dp[i] = max(dp[i - 1], dp[i]); } return dp; } int main() { int n, m; cin >> n >> m; vector<vector<long long>> ps(3); for (int i = (0); i < (n); ++i) { int w, c; cin >> w >> c; --w; ps[w].push_back(c); } for (int i = (0); i < (3); ++i) { sort(ps[i].begin(), ps[i].end()); reverse(ps[i].begin(), ps[i].end()); ps[i].insert(ps[i].begin(), 0); } for (int i = (0); i < (3); ++i) { for (int j = (1); j < (ps[i].size()); ++j) { ps[i][j] += ps[i][j - 1]; } } vector<long long> one_two_best = one_two_knapsack(ps[0], ps[1], m); long long res = 0; for (int i = (0); i < (ps[2].size()); ++i) { int t = m - 3 * i; if (t < 0) { continue; } res = max(res, ps[2][i] + one_two_best[t]); } cout << res; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; struct node { long long w, s1, s2; } dp[300005]; long long a[4][300005]; long long s[4][300005]; int num[4]; bool cmp(int a, int b) { return a > b; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { long long w, c; scanf("%lld%lld", &w, &c); a[w][++num[w]] = c; } for (int i = 1; i <= 3; i++) { sort(a[i] + 1, a[i] + num[i] + 1, cmp); for (int j = 1; j <= num[i]; j++) { s[i][j] = s[i][j - 1] + a[i][j]; } } dp[0].w = dp[0].s1 = dp[0].s2 = 0; for (int i = 1; i <= m; i++) { dp[i] = dp[i - 1]; if (dp[i - 1].w + a[1][dp[i - 1].s1 + 1] > dp[i].w) { dp[i].w = dp[i - 1].w + a[1][dp[i - 1].s1 + 1]; dp[i].s1 = dp[i - 1].s1 + 1; dp[i].s2 = dp[i - 1].s2; } if (i >= 2 && dp[i - 2].w + a[2][dp[i - 2].s2 + 1] > dp[i].w) { dp[i].w = dp[i - 2].w + a[2][dp[i - 2].s2 + 1]; dp[i].s1 = dp[i - 2].s1; dp[i].s2 = dp[i - 2].s2 + 1; } } long long res = 0; for (int i = 0; i <= num[3]; i++) { if (m >= i * 3) res = max(res, s[3][i] + dp[m - 3 * i].w); } printf("%I64d\n", res); return 0; }
11
CPP
#include <bits/stdc++.h> const int N = 100005; using namespace std; int n, m, sz[3]; long long d[3][N], f[N << 2]; void solve() { for (int i = 0; i <= 2; ++i) sort(d[i] + 1, d[i] + sz[i] + 1); for (int i = 0; i <= 2; ++i) reverse(d[i] + 1, d[i] + sz[i] + 1); for (int i = 0; i <= 2; ++i) { for (int j = 2; j <= sz[i]; ++j) d[i][j] += d[i][j - 1]; } int top0 = 2, top1 = 1; for (int i = 2; i <= m; i += 2) { f[i] = f[i - 2]; if (top0 <= sz[0] && (top1 > sz[1] || d[0][top0] - d[0][top0 - 2] >= d[1][top1] - d[1][top1 - 1])) f[i] = f[i - 2] + d[0][top0] - d[0][top0 - 2], top0 += 2; else if (top1 <= sz[1]) f[i] = f[i - 2] + d[1][top1] - d[1][top1 - 1], ++top1; } f[1] = d[0][1]; top0 = 3, top1 = 1; for (int i = 3; i <= m; i += 2) { f[i] = f[i - 2]; if (top0 <= sz[0] && (top1 > sz[1] || d[0][top0] - d[0][top0 - 2] >= d[1][top1] - d[1][top1 - 1])) f[i] = f[i - 2] + d[0][top0] - d[0][top0 - 2], top0 += 2; else if (top1 <= sz[1]) f[i] = f[i - 2] + d[1][top1] - d[1][top1 - 1], ++top1; } long long ans = 0; for (int i = 0; i <= m; ++i) { int x = (m - i) / 3; x = min(x, sz[2]); ans = max(ans, f[i] + d[2][x]); } printf("%I64d\n", ans); } int main() { int x, y; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%d%d", &x, &y); --x; ++sz[x]; d[x][sz[x]] = y; } solve(); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; class input { private: bool fail; istream* stream; public: input(istream& stream_ = cin) { stream = &stream_; } istream& getStream() { return *stream; } template <typename T = int> T read() { T value; if (*stream >> value) { fail = false; } else { fail = true; } return value; } template <typename T = int> vector<T> readArray() { size_t size = read<size_t>(); return readArray<T>(size); } template <typename T = int> vector<T> readArray(size_t size) { vector<T> v(size); for (size_t i = 0; i < size; i++) { if (*stream >> v[i]) { fail = false; } else { fail = true; } } return v; } bool isFailed() { return fail; } static input& getStdin() { static input in(cin); return in; } }; class output { private: ostream* stream; public: output(ostream& stream_ = cout) { stream = &stream_; } ostream& getStream() { return *stream; } template <typename T> output& put(T& value) { *stream << value; return *this; } template <typename T> output& put(T& value, int precision) { *stream << setprecision(precision) << value; return *this; } template <typename T> output& write(const T& value) { *stream << value << endl; return *this; } template <typename T> output& write(T& value, int precision) { *stream << setprecision(precision) << fixed << value << endl; return *this; } template <typename T> output& writeArray(const vector<T>& v, bool appendSize = true) { if (appendSize) { write(v.size()); } for (auto& x : v) { *stream << x << " "; } *stream << endl; return *this; } output& writeLine() { *stream << endl; return *this; } output& getStdout() { output out(cout); return out; } output getStderr() { output err(cerr); return err; } }; input in(cin); output out(cout); output err(cerr); void solve() { auto n = in.read(); auto m = in.read(); vector<pair<long long, long long>> items; for (auto i = 0; i < n; i++) { auto w = in.read<long long>(); auto c = in.read<long long>(); items.push_back(make_pair((c * 6) / w, w)); } sort(items.begin(), items.end()); reverse(items.begin(), items.end()); vector<int> topCnt(3); vector<int> allCnt(3); int ws = 0; for (auto p : items) { if (p.second <= m - ws) { topCnt[p.second - 1]++; ws += p.second; } allCnt[p.second - 1]++; } auto t1 = topCnt[0]; auto t2 = topCnt[1]; auto t3 = topCnt[2]; auto tt = 3; long long ans = 0; vector<int> rtmp(3); for (auto i1 = t1 - 3; i1 <= t1 + 3; i1++) { for (auto i2 = t2 - 3; i2 <= t2 + 3; i2++) { for (auto i3 = t3 - 3; i3 <= t3 + 3; i3++) { rtmp[0] = i1; rtmp[1] = i2; rtmp[2] = i3; auto w = m; long long nans = 0; for (auto& x : items) { if (rtmp[x.second - 1] > 0 && x.second <= w) { rtmp[x.second - 1]--; w -= x.second; nans += (x.first * x.second) / 6; } } if (nans > ans) ans = nans; } } } out.write(ans); } int main() { while (true) { auto startT = clock(); solve(); auto endT = clock(); cerr << endl; cerr << "Working time: " << (endT - startT) << " ms" << endl; cerr << "-------------------------" << endl; bool ok = false; while (!in.isFailed() && !ok) { ok = in.read<char>() == '$'; } if (!ok) { break; } } }
11
CPP
#include <bits/stdc++.h> using namespace std; int N, M; vector<long long> one, two, three; long long dp[1000000]; vector<long long> pre1, pre2, pre3; long long ans; long long score(int tot, int a2) { return pre2[a2] + pre1[tot - 2 * a2]; } int32_t main() { ios_base::sync_with_stdio(false); cin >> N >> M; for (int i = 0; i < N; i++) { int a, b; cin >> a >> b; if (a == 1) { one.push_back(b); } if (a == 2) { two.push_back(b); } if (a == 3) { three.push_back(b); } } sort(one.begin(), one.end()); reverse(one.begin(), one.end()); sort(two.begin(), two.end()); reverse(two.begin(), two.end()); sort(three.begin(), three.end()); reverse(three.begin(), three.end()); pre1.push_back(0); for (int i = 0; i < one.size(); i++) { pre1.push_back(pre1.back() + one[i]); } pre2.push_back(0); for (int i = 0; i < two.size(); i++) { pre2.push_back(pre2.back() + two[i]); } pre3.push_back(0); for (int i = 0; i < three.size(); i++) { pre3.push_back(pre3.back() + three[i]); } for (int i = 0; i <= one.size() + 2 * two.size(); i++) { dp[i] = -1000000000000000001ll; int small = max(0, ((int)(i - one.size() + 1) / 2)); int big = min(i / 2, (int)(two.size())); while (big - small > 2) { int lt = (2 * small + big) / 3; int rt = (2 * big + small) / 3; if (score(i, lt) > score(i, rt)) { big = rt; } if (score(i, lt) < score(i, rt)) { small = lt; } if (score(i, lt) == score(i, rt)) { small = lt; big = rt; } } for (int j = small; j <= big; j++) { dp[i] = max(dp[i], score(i, j)); } } for (int i = 1; i <= M; i++) { dp[i] = max(dp[i], dp[i - 1]); } for (int i = 0; i <= three.size() && 3 * i <= M; i++) { ans = max(ans, pre3[i] + dp[M - 3 * i]); } cout << ans << '\n'; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 9; const int M = 3e5 + 9; struct Triple { long long cost; int cnt1, cnt2, cnt3; } dp[M]; int n, m; long long sum3[N]; vector<int> c[4]; bool cmp(int x, int y) { return x > y; } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { int weight, cost; cin >> weight >> cost; c[weight].push_back(cost); } sort(c[1].begin(), c[1].end(), cmp); sort(c[2].begin(), c[2].end(), cmp); sort(c[3].begin(), c[3].end(), cmp); dp[0].cost = 0; dp[0].cnt1 = 0; dp[0].cnt2 = 0; dp[0].cnt3 = 0; for (int i = 0; i <= m; i++) { dp[i].cost = 0; } for (int i = 1; i <= m; i++) { if (dp[i - 1].cnt1 < c[1].size() && dp[i - 1].cost + c[1][dp[i - 1].cnt1] > dp[i].cost) { dp[i].cost = dp[i - 1].cost + c[1][dp[i - 1].cnt1]; dp[i].cnt1 = dp[i - 1].cnt1 + 1; dp[i].cnt2 = dp[i - 1].cnt2; dp[i].cnt3 = dp[i - 1].cnt3; } if (i >= 2 && dp[i - 2].cnt2 < c[2].size() && dp[i - 2].cost + c[2][dp[i - 2].cnt2] > dp[i].cost) { dp[i].cost = dp[i - 2].cost + c[2][dp[i - 2].cnt2]; dp[i].cnt1 = dp[i - 2].cnt1; dp[i].cnt2 = dp[i - 2].cnt2 + 1; dp[i].cnt3 = dp[i - 2].cnt3; } if (i >= 3 && dp[i - 3].cnt3 < c[3].size() && dp[i - 3].cost + c[3][dp[i - 3].cnt3] > dp[i].cost) { dp[i].cost = dp[i - 3].cost + c[3][dp[i - 3].cnt3]; dp[i].cnt1 = dp[i - 3].cnt1; dp[i].cnt2 = dp[i - 3].cnt2; dp[i].cnt3 = dp[i - 3].cnt3 + 1; } if (dp[i - 2].cnt1 > 0 && dp[i - 2].cnt3 < c[3].size() && dp[i - 2].cost - c[1][dp[i - 2].cnt1 - 1] + c[3][dp[i - 2].cnt3] > dp[i].cost) { dp[i].cost = dp[i - 2].cost - c[1][dp[i - 2].cnt1 - 1] + c[3][dp[i - 2].cnt3]; dp[i].cnt1 = dp[i - 2].cnt1 - 1; dp[i].cnt2 = dp[i - 2].cnt2; dp[i].cnt3 = dp[i - 2].cnt3 + 1; } if (i >= 1 && dp[i - 1].cost > dp[i].cost) dp[i] = dp[i - 1]; if (i >= 2 && dp[i - 2].cost > dp[i].cost) dp[i] = dp[i - 2]; if (i >= 3 && dp[i - 3].cost > dp[i].cost) dp[i] = dp[i - 3]; } long long res = dp[m].cost; cout << res << endl; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; struct str { long long nrw1, nrw2; long long val; }; str dp[300003]; long long n, m; long long sol[300003]; vector<long long> v[4]; int main() { cin >> n >> m; v[1].push_back(-(1 << 30)); v[2].push_back(-(1 << 30)); v[3].push_back(-(1 << 30)); for (long long i = 1; i <= n; ++i) { long long x, w; cin >> w >> x; v[w].push_back(x); } sort(v[1].begin(), v[1].end(), greater<long long>()); sort(v[2].begin(), v[2].end(), greater<long long>()); sort(v[3].begin(), v[3].end(), greater<long long>()); dp[1].val = v[1][0]; dp[1].nrw1 = 1; dp[1].nrw2 = 0; dp[2].val = v[2][0]; dp[2].nrw1 = 0; dp[2].nrw2 = 1; for (long long i = 1; i <= m; ++i) { if (dp[i].nrw1 < v[1].size() && dp[i + 1].val < dp[i].val + v[1][dp[i].nrw1]) { dp[i + 1] = dp[i]; dp[i + 1].val += v[1][dp[i].nrw1]; dp[i + 1].nrw1++; } if (dp[i].nrw2 < v[2].size() && dp[i + 2].val < dp[i].val + v[2][dp[i].nrw2]) { dp[i + 2] = dp[i]; dp[i + 2].val += v[2][dp[i].nrw2]; dp[i + 2].nrw2++; } sol[i] = max(dp[i].val, sol[i - 1]); } long long ans = sol[m], sum = 0; for (long long i = 0; i < v[3].size() && (i + 1) * 3 <= m; ++i) { sum += v[3][i]; ans = max(ans, sol[m - (i + 1) * 3] + sum); } cout << ans << "\n"; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; template <class T> bool umin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> bool umax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } int n, m; long long par[300009], dp[300009]; pair<int, int> pnt[300009]; vector<int> adj[3]; long long solve(int x) { if (m < x * 3) return 0; return dp[m - x * 3] + par[x]; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < 3; i++) adj[i].push_back(1000000007); while (n--) { int w, c; scanf("%d%d", &w, &c); adj[w - 1].push_back(c); } for (int i = 0; i < 3; i++) { sort(adj[i].begin(), adj[i].end()); reverse(adj[i].begin(), adj[i].end()); adj[i][0] = 0; } for (int i = 1; i < (int)adj[2].size(); i++) par[i] = par[i - 1] + adj[2][i]; for (int i = 0; i < ((int)adj[0].size() + (int)adj[1].size() * 2 - 3); i++) { int p0 = pnt[i].first, p1 = pnt[i].second; if (!p0 and !p1 and i) continue; if (p0 + 1 < int(adj[0].size()) and umax(dp[i + 1], dp[i] + adj[0][p0 + 1])) pnt[i + 1] = make_pair(p0 + 1, p1); if (p1 + 1 < int(adj[1].size()) and p0 + 2 < int(adj[0].size())) { if (adj[1][p1 + 1] > adj[0][p0 + 1] + adj[0][p0 + 2]) dp[i + 2] = dp[i] + adj[1][p1 + 1], pnt[i + 2] = make_pair(p0, p1 + 1); else dp[i + 2] = dp[i] + adj[0][p0 + 1] + adj[0][p0 + 2], pnt[i + 2] = make_pair(p0 + 2, p1); } else if (p1 + 1 < int(adj[1].size())) dp[i + 2] = dp[i] + adj[1][p1 + 1], pnt[i + 2] = make_pair(p0, p1 + 1); else if (p0 + 2 < int(adj[0].size())) dp[i + 2] = dp[i] + adj[0][p0 + 1] + adj[0][p0 + 2], pnt[i + 2] = make_pair(p0 + 2, p1); } for (int i = 1; i < 300009; i++) umax(dp[i], dp[i - 1]); long long ans = 0; for (int i = 0; i < int(adj[2].size()); i++) umax(ans, solve(i)); printf("%lld\n", ans); return 0; }
11
CPP
import sys import bisect from bisect import bisect_left as lb input_=lambda: sys.stdin.readline().strip("\r\n") from math import log from math import gcd from math import atan2,acos from random import randint sa=lambda :input_() sb=lambda:int(input_()) sc=lambda:input_().split() sd=lambda:list(map(int,input_().split())) sflo=lambda:list(map(float,input_().split())) se=lambda:float(input_()) sf=lambda:list(input_()) flsh=lambda: sys.stdout.flush() #sys.setrecursionlimit(10**6) mod=10**9+7 mod1=998244353 gp=[] cost=[] dp=[] mx=[] ans1=[] ans2=[] special=[] specnode=[] a=0 kthpar=[] def dfs(root,par): if par!=-1: dp[root]=dp[par]+1 for i in range(1,20): if kthpar[root][i-1]!=-1: kthpar[root][i]=kthpar[kthpar[root][i-1]][i-1] for child in gp[root]: if child==par:continue kthpar[child][0]=root dfs(child,root) ans=0 b=[] def hnbhai(tc): n,m=sd() dp=[(0,0,0) for i in range(m+3)] gp=[[] for i in range(4)] for i in range(n): w,c=sd() gp[w].append(c) gp[1].sort(reverse=True) gp[2].sort(reverse=True) gp[3].sort(reverse=True) for i in range(0,m+1): one=dp[i][1] two=dp[i][2] if one<len(gp[1]): if dp[i+1][0]<dp[i][0]+gp[1][one]: dp[i+1]=(dp[i][0]+gp[1][one],one+1,two) if two<len(gp[2]): dp[i+2]=(dp[i][0]+gp[2][two],one,two+1) for i in range(1,m+1): dp[i]=max(dp[i-1],dp[i]) #print(dp) ans=dp[m][0] tot=0 for i in range(len(gp[3])+1): if 3*i<=m: ans=max(dp[m-3*(i)][0]+tot,ans) if i<len(gp[3]): tot+=gp[3][i] print(ans) for _ in range(1): hnbhai(_+1)
11
PYTHON3
#include <bits/stdc++.h> using namespace std; struct triple { int t[3] = {0, 0, 0}; long long sum = 0; }; long long val[4][100010]; int cnt[4]; triple dp[300010]; void calc_dp(int x); int main() { int n, m; cin >> n >> m; while (n--) { int c, w; cin >> w >> c; val[w][cnt[w]++] = c; } for (int i(1); i <= 3; i++) sort(val[i], val[i] + cnt[i], [](int a, int b) { return a > b; }); long long ans(0); for (int i(1); i <= m; i++) { calc_dp(i); ans = max(ans, dp[i].sum); } long long sp(0); for (int i(1); 3 * i <= m && i <= cnt[3]; i++) { sp += val[3][i - 1]; ans = max(ans, sp + dp[m - 3 * i].sum); } cout << ans; return 0; } void calc_dp(int x) { dp[x] = dp[x - 1]; for (int i(1); i <= 2; i++) { if (x - i >= 0 && dp[x - i].t[i] < cnt[i] && dp[x - i].sum + val[i][dp[x - i].t[i]] > dp[x].sum) { dp[x] = dp[x - i]; dp[x].sum += val[i][dp[x].t[i]]; dp[x].t[i]++; } } }
11
CPP
#include <bits/stdc++.h> using namespace std; const long long INF = 1e17L; const int inf = 0x3f3f3f3f; const int maxn = 1e5 + 5; const int Mod = 1e9 + 7; struct Souvenirs { int w, c; bool friend operator<(Souvenirs x, Souvenirs y) { return ((double)x.c / x.w) > ((double)y.c / y.w); } } a[maxn]; int n, m; long long dp[3 * maxn]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].w, &a[i].c); sort(a + 1, a + n + 1); for (int i = 1; i <= m; i++) dp[i] = -INF; dp[0] = 0; int up = 0; long long ans = 0; for (int i = 1; i <= n; i++) { up += a[i].w; if (up > m) up = m; int least = max(a[i].w, up - 6); for (int j = up; j >= least; j--) { dp[j] = max(dp[j], dp[j - a[i].w] + a[i].c); ans = max(ans, dp[j]); } } printf("%I64d\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> inline T abs(T t) { return t < 0 ? -t : t; } const long long modn = 1000000007; inline long long mod(long long x) { return x % modn; } const int MAXN = 312345; int n, m; vector<long long> s[4]; struct pd { long long x; int i[3]; pd(long long xx, int ii, int iii) { x = xx; i[1] = ii; i[2] = iii; } pd() {} bool operator<(const struct pd &o) const { return (x < o.x || (x == o.x && i[1] < o.i[1])); } } dp[MAXN]; bool cmp(int a, int b) { return a > b; } int main() { scanf("%d %d", &n, &m); long long res = 0; for (int a = 0; a < n; a++) { long long w, val; scanf("%lld %lld", &w, &val); s[w].push_back(val); if (w == 3) res += val; } for (int i = 1; i <= 3; i++) sort(s[i].begin(), s[i].end(), cmp); dp[0] = (pd){0, 0, 0}; for (int i = 0; i < m; i++) { dp[i + 1] = max(dp[i], dp[i + 1]); if (dp[i].i[1] != s[1].size()) dp[i + 1] = max(dp[i + 1], pd(dp[i].x + s[1][dp[i].i[1]], dp[i].i[1] + 1, dp[i].i[2])); if (dp[i].i[2] != s[2].size()) dp[i + 2] = max(dp[i + 2], pd(dp[i].x + s[2][dp[i].i[2]], dp[i].i[1], dp[i].i[2] + 1)); ; } int i = 0, j = 0; long long ans = 0; for (int k = s[3].size(); k >= 0; k--) { if (m < 3 * k) { if (k > 0) { res -= s[3][k - 1]; } continue; } int sob = m - 3 * k; ans = max(ans, res + dp[sob].x); if (k > 0) res -= s[3][k - 1]; } printf("%lld\n", ans); }
11
CPP
#include <bits/stdc++.h> using namespace std; struct Node { long long val; int a, b; } dp[310000]; vector<int> w1, w2, w3; long long s[110000]; int n, m; bool cmp(int a, int b) { return a > b; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { int x, y; scanf("%d%d", &x, &y); if (x == 1) w1.push_back(y); if (x == 2) w2.push_back(y); if (x == 3) w3.push_back(y); } sort(w1.begin(), w1.end(), cmp); sort(w2.begin(), w2.end(), cmp); sort(w3.begin(), w3.end(), cmp); for (int i = 0; i < w3.size(); i++) s[i + 1] = s[i] + w3[i]; dp[0].val = dp[0].a = dp[0].b = 0; long long ans = -1e16; for (int i = 1; i <= m; i++) { dp[i] = dp[i - 1]; int a = dp[i - 1].a; if (a < w1.size() && dp[i - 1].val + w1[a] > dp[i].val) { dp[i].val = dp[i - 1].val + w1[a]; dp[i].a++; } if (i > 1) { int b = dp[i - 2].b; if (i > 1 && b < w2.size() && dp[i - 2].val + w2[b] > dp[i].val) { dp[i] = dp[i - 2]; dp[i].b++; dp[i].val = dp[i - 2].val + w2[b]; } } ans = max(ans, dp[i].val + s[min((int)w3.size(), (m - dp[i].a - 2 * dp[i].b) / 3)]); } ans = max( ans, dp[0].val + s[min((int)w3.size(), (m - dp[0].a - 2 * dp[0].b) / 3)]); printf("%I64d\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; inline char nc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } inline void read(int &x) { char c = nc(), b = 1; for (; !(c >= '0' && c <= '9'); c = nc()) if (c == '-') b = -1; for (x = 0; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = nc()) ; x *= b; } int n, m, maxc; vector<long long> a[305]; long long f[2][300005]; int x, b, t; inline void Solve(int l, int r, int al, int ar) { if (l > r) return; int i = (l + r) >> 1, d = i; f[t ^ 1][i * x + b] = f[t][i * x + b]; for (int j = min(ar, i - 1); j >= al; j--) { if (i - j > (int)a[x].size()) break; if (f[t][j * x + b] + a[x][i - j - 1] > f[t ^ 1][i * x + b]) f[t ^ 1][i * x + b] = f[t][j * x + b] + a[x][i - j - 1], d = j; } Solve(l, i - 1, al, d); Solve(i + 1, r, d, ar); } int main() { int _x, _y; read(n); read(m); for (int i = 1; i <= n; i++) read(_x), read(_y), a[_x].push_back(_y), maxc = max(maxc, _x); for (int i = 1; i <= maxc; i++) { sort(a[i].begin(), a[i].end(), greater<int>()); for (int j = 1; j < (int)a[i].size(); j++) a[i][j] += a[i][j - 1]; } t = 0; for (x = 1; x <= maxc; x++) { if (a[x].size() == 0) continue; for (b = 0; b < x; b++) { Solve(0, (m - b) / x, 0, (m - b) / x); } for (int j = 1; j <= m; j++) f[t ^ 1][j] = max(f[t ^ 1][j], f[t ^ 1][j - 1]); t ^= 1; } long long ans = 0; for (int i = 1; i <= m; i++) ans = max(ans, f[t][i]); printf("%I64d\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 3e5 + 100; int N, M; vector<int> v[5]; const long long INF = 1e18; long long dp[MAXN]; int ptr[MAXN][2]; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> N >> M; for (int i = 0; i < N; i++) { int w, c; cin >> w >> c; w--; v[w].push_back(c); } for (int i = 0; i < 3; i++) sort(v[i].begin(), v[i].end(), greater<int>()); for (int i = 0; i <= M; i++) { if (ptr[i][0] < v[0].size() && dp[i] + v[0][ptr[i][0]] > dp[i + 1]) { dp[i + 1] = dp[i] + v[0][ptr[i][0]]; ptr[i + 1][0] = ptr[i][0] + 1; ptr[i + 1][1] = ptr[i][1]; } if (ptr[i][1] < v[1].size() && dp[i] + v[1][ptr[i][1]] > dp[i + 2]) { dp[i + 2] = dp[i] + v[1][ptr[i][1]]; ptr[i + 2][0] = ptr[i][0]; ptr[i + 2][1] = ptr[i][1] + 1; } if (dp[i] > dp[i + 1]) { dp[i + 1] = dp[i]; ptr[i + 1][0] = ptr[i][0]; ptr[i + 1][1] = ptr[i][1]; } } long long ans = dp[M], cost = 0; for (int i = 1; i <= v[2].size(); i++) { int w = M - 3 * i; if (w < 0) break; cost += v[2][i - 1]; ans = max(ans, cost + dp[w]); } cout << ans << '\n'; }
11
CPP
#include <bits/stdc++.h> using namespace std; struct str { long long sum, tp1, tp2; }; long long n, m, ans, sum[300100]; str dp[300100]; vector<long long> v[4]; bool comp(long long x, long long y) { return x > y; } int main() { cin >> n >> m; for (int i = 0; i < n; ++i) { long long type, x; scanf("%lld%lld", &type, &x); v[type].push_back(x); } sort(v[1].begin(), v[1].end(), comp); sort(v[2].begin(), v[2].end(), comp); sort(v[3].begin(), v[3].end(), comp); for (int i = 1; i <= (int)v[3].size(); ++i) sum[i] = sum[i - 1] + v[3][i - 1]; for (int i = 1; i <= m; ++i) { if (i == 1) { if (!v[1].empty()) dp[i] = {v[1][0], 1, 0}; continue; } dp[i] = (dp[i - 2].sum > dp[i - 1].sum ? dp[i - 2] : dp[i - 1]); if (v[1].size() > dp[i - 1].tp1 && dp[i - 1].sum + v[1][dp[i - 1].tp1] > dp[i].sum) dp[i] = {dp[i - 1].sum + v[1][dp[i - 1].tp1], dp[i - 1].tp1 + 1, dp[i - 1].tp2}; if (v[2].size() > dp[i - 2].tp2 && dp[i - 2].sum + v[2][dp[i - 2].tp2] > dp[i].sum) dp[i] = {dp[i - 2].sum + v[2][dp[i - 2].tp2], dp[i - 2].tp1, dp[i - 2].tp2 + 1}; } for (int i = 0; i * 3 <= m; ++i) ans = max(ans, sum[i] + dp[m - i * 3].sum); cout << ans; }
11
CPP
#include <bits/stdc++.h> using namespace std; bool func(const pair<long long, long long> &a, const pair<long long, long long> &b) { return a.second < b.second; } long long int power(long long int x, long long int y) { if (y == 0) return 1; else if (y % 2 == 0) return ((power(x, y / 2) % 1000000007) * (power(x, y / 2) % 1000000007) % 1000000007); else return (x * ((power(x, y / 2) % 1000000007) * (power(x, y / 2) % 1000000007) % 1000000007)) % 1000000007; } long long int modInverse(long long int a, long long int m) { long long int m0 = m; long long int y = 0, x = 1; if (m == 1) return 0; while (a > 1) { long long int q = a / m; long long int t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } bool cmp(int x, int y) { return x > y; } const int N = 1e5 + 9; const int M = 3e5 + 9; struct Triple { long long int cost; int cnt1, cnt2, cnt3; } dp[M]; int n, m; long long int sum3[N]; vector<int> c[4]; void solve(int t) { cin >> n >> m; for (int i = 1; i <= n; i++) { int weight, cost; cin >> weight >> cost; c[weight].push_back(cost); } sort(c[1].begin(), c[1].end(), cmp); sort(c[2].begin(), c[2].end(), cmp); sort(c[3].begin(), c[3].end(), cmp); dp[0].cost = 0; dp[0].cnt1 = 0; dp[0].cnt2 = 0; dp[0].cnt3 = 0; for (int i = 0; i <= m; i++) { dp[i].cost = 0; } for (int i = 1; i <= m; i++) { if (dp[i - 1].cnt1 < c[1].size() && dp[i - 1].cost + c[1][dp[i - 1].cnt1] > dp[i].cost) { dp[i].cost = dp[i - 1].cost + c[1][dp[i - 1].cnt1]; dp[i].cnt1 = dp[i - 1].cnt1 + 1; dp[i].cnt2 = dp[i - 1].cnt2; dp[i].cnt3 = dp[i - 1].cnt3; } if (i >= 2 && dp[i - 2].cnt2 < c[2].size() && dp[i - 2].cost + c[2][dp[i - 2].cnt2] > dp[i].cost) { dp[i].cost = dp[i - 2].cost + c[2][dp[i - 2].cnt2]; dp[i].cnt1 = dp[i - 2].cnt1; dp[i].cnt2 = dp[i - 2].cnt2 + 1; dp[i].cnt3 = dp[i - 2].cnt3; } if (i >= 3 && dp[i - 3].cnt3 < c[3].size() && dp[i - 3].cost + c[3][dp[i - 3].cnt3] > dp[i].cost) { dp[i].cost = dp[i - 3].cost + c[3][dp[i - 3].cnt3]; dp[i].cnt1 = dp[i - 3].cnt1; dp[i].cnt2 = dp[i - 3].cnt2; dp[i].cnt3 = dp[i - 3].cnt3 + 1; } if (dp[i - 2].cnt1 > 0 && dp[i - 2].cnt3 < c[3].size() && dp[i - 2].cost - c[1][dp[i - 2].cnt1 - 1] + c[3][dp[i - 2].cnt3] > dp[i].cost) { dp[i].cost = dp[i - 2].cost - c[1][dp[i - 2].cnt1 - 1] + c[3][dp[i - 2].cnt3]; dp[i].cnt1 = dp[i - 2].cnt1 - 1; dp[i].cnt2 = dp[i - 2].cnt2; dp[i].cnt3 = dp[i - 2].cnt3 + 1; } if (i >= 1 && dp[i - 1].cost > dp[i].cost) dp[i] = dp[i - 1]; if (i >= 2 && dp[i - 2].cost > dp[i].cost) dp[i] = dp[i - 2]; if (i >= 3 && dp[i - 3].cost > dp[i].cost) dp[i] = dp[i - 3]; } long long int res = dp[m].cost; cout << res; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t, tmp = 0; t = 1; while (tmp++ < t) { solve(tmp - 1); if (t - tmp) cout << "\n"; } cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n"; return 0; }
11
CPP
n, m = map(int, input().split()) d = [[] for i in range(3)] res_odd = 0 res_even = 0 for i in range(n): t1, t2 = map(int, input().split()) d[t1 - 1].append(t2) d[0].sort() d[2].sort(reverse=True) c = d[1].copy() if d[0]: i = len(d[0]) - 2 while i >= 1: c.append(d[0][i] + d[0][i - 1]) i -= 2 c.sort(reverse=True) pref = [0 for i in range(len(c) + 1)] pref[0] = 0 for i in range(1, len(c) + 1): pref[i] = pref[i - 1] + c[i - 1] p = 0 for i in range(min(len(d[2]), (m - 1) // 3) + 1): if i != 0: p += d[2][i - 1] res_odd = max(res_odd, d[0][-1] + p + pref[min(max(m - i * 3 - 1, 0) // 2, len(pref) - 1)]) i = len(d[0]) - 1 while i >= 1: d[1].append(d[0][i] + d[0][i - 1]) i -= 2 d[1].sort(reverse=True) pref = [0 for i in range(len(d[1]) + 1)] pref[0] = 0 for i in range(1, len(d[1]) + 1): pref[i] = pref[i - 1] + d[1][i - 1] p = 0 for i in range(min(len(d[2]), m // 3) + 1): if i != 0: p += d[2][i - 1] res_even = max(res_even, p + pref[min(max(m - i * 3, 0) // 2, len(pref) - 1)]) print(max(res_odd, res_even))
11
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<long long> sp[4]; struct node { long long b1, b2, val; } dp[300005]; long long cal(long long t, long long k) { if (k >= sp[t].size()) return 0; return sp[t][k]; } bool cmp(long long a, long long b) { return a > b; } int main() { long long n, m, i, j, w, c; scanf("%lld%lld", &n, &m); for (i = 1; i <= n; i++) { scanf("%lld%lld", &w, &c); sp[w].push_back(c); } sort(sp[1].begin(), sp[1].end(), cmp); sort(sp[2].begin(), sp[2].end(), cmp); sort(sp[3].begin(), sp[3].end(), cmp); dp[1] = (node){1, 0, cal(1, 0)}; for (i = 2; i <= m; i++) { if (dp[i - 2].val + cal(2, dp[i - 2].b2) > dp[i - 1].val + cal(1, dp[i - 1].b1)) dp[i] = (node){dp[i - 2].b1, dp[i - 2].b2 + 1, dp[i - 2].val + cal(2, dp[i - 2].b2)}; else dp[i] = (node){dp[i - 1].b1 + 1, dp[i - 1].b2, dp[i - 1].val + cal(1, dp[i - 1].b1)}; } long long ans = 0, now = 0; for (i = 0; i * 3 <= m; i++) { ans = max(ans, now + dp[m - i * 3].val); now += cal(3, i); } printf("%lld\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 300010; int n, m; vector<int> ar1, ar2, ar3; long long sum1[MAXN], sum2[MAXN], sum3[MAXN]; long long ans = 0; long long f(int x, int k) { return sum2[x] + sum1[k - 2 * x]; } long long get(int k) { if (!k) return 0; int lo = 0, hi = k / 2; while (lo < hi) { int mid = lo + (hi - lo) / 2; long long a = f(mid, k); long long b = f(mid + 1, k); if (lo == hi - 1) return max(a, b); if (a == b) return a; if (a > b) hi = mid; else lo = mid; } return f(lo, k); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { int w, c; scanf("%d%d", &w, &c); (w == 1 ? ar1 : (w == 2 ? ar2 : ar3)).push_back(c); } sort(ar1.begin(), ar1.end(), greater<int>()); sort(ar2.begin(), ar2.end(), greater<int>()); sort(ar3.begin(), ar3.end(), greater<int>()); for (int i = 0; i < ar1.size(); i++) { sum1[i + 1] = ar1[i]; if (i) sum1[i + 1] += sum1[i]; } for (int i = 0; i < ar2.size(); i++) { sum2[i + 1] = ar2[i]; if (i) sum2[i + 1] += sum2[i]; } for (int i = 0; i < ar3.size(); i++) { sum3[i + 1] = ar3[i]; if (i) sum3[i + 1] += sum3[i]; } for (int i = 1; i <= m; i++) { if (!sum1[i]) sum1[i] = sum1[i - 1]; if (!sum2[i]) sum2[i] = sum2[i - 1]; if (!sum3[i]) sum3[i] = sum3[i - 1]; } for (int i = 0; 3 * i <= m && i <= ar3.size(); i++) { long long cur = (i > 0 ? sum3[i] : 0); cur += get(m - 3 * i); ans = max(ans, cur); } printf("%lld\n", ans); }
11
CPP
def main(): n, m = map(int, input().split()) cost1 = [] cost2 = [] cost3 = [] for i in range(n): w, c = map(int, input().split()) if w == 1: cost1.append(c) elif w == 2: cost2.append(c) else: cost3.append(c) cost1 = sorted(cost1)[::-1] cost2 = sorted(cost2)[::-1] cost3 = sorted(cost3)[::-1] cost3_prefix = [0] for c in cost3: cost3_prefix.append(cost3_prefix[-1] + c) dp = [(0, 0, 0)] * (m + 1) dp[0] = (0, 0, 0) for i in range(0, m): cost, n1, n2 = dp[i] if i + 1 <= m and n1 < len(cost1): new_cost = cost + cost1[n1] if dp[i + 1][0] < new_cost: dp[i + 1] = (new_cost, n1 + 1, n2) if i + 2 <= m and n2 < len(cost2): new_cost = cost + cost2[n2] if dp[i + 2][0] < new_cost: dp[i + 2] = (new_cost, n1, n2 + 1) if n1 == len(cost1) and n2 == len(cost2): break dp_prefix = [0] for x in dp[1:]: dp_prefix.append(max(dp_prefix[-1], x[0])) ans = 0 for k in range(len(cost3) + 1): l = m - 3 * k if l < 0: continue new_ans = cost3_prefix[k] + dp_prefix[l] ans = max(new_ans, ans) print(ans) if __name__ == '__main__': main()
11
PYTHON3
#include <bits/stdc++.h> using namespace std; long long dp[1010000]; vector<int> vas[300 + 1]; long long sums[1010000 + 1]; int cnts[1010000 + 1]; long long* sum[300 + 1]; bool cmp(int a, int b) { return a > b; } int s1[1010000]; int s1_cnt = -1; long long so[1010000]; int nowva; inline void solve(int l1, int r1, int l2, int r2) { if (l1 > r1 || l2 > r2) return; int mid = (l1 + r1) >> 1; int p = 0; for (int i = max(l2, mid - cnts[nowva]); i <= min(r2, mid); ++i) { long long va = dp[s1[i]] + sum[nowva][mid - i]; if (va > so[mid]) so[mid] = va, p = i; } solve(l1, mid - 1, l2, p); solve(mid + 1, r1, p, r2); } int n, k, c, v; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) { scanf("%d%d", &c, &v); vas[c].push_back(v); } for (int i = 1; i <= 300; ++i) sort(vas[i].begin(), vas[i].end(), cmp); sum[1] = sums; for (int i = 1; i <= 300; ++i) { cnts[i] = vas[i].size(); if (cnts[i]) sum[i][1] = vas[i][0]; for (int j = 2; j <= cnts[i]; ++j) { sum[i][j] = sum[i][j - 1] + vas[i][j - 1]; } sum[i + 1] = sum[i] + cnts[i] + 1; } for (int i = 1; i <= 300; ++i) { nowva = i; if (!cnts[i]) continue; for (int j = 0; j < i; ++j) { s1_cnt = -1; for (int l = j; l <= k; l += i) s1[++s1_cnt] = l, so[s1_cnt] = 0; if (s1_cnt >= 1) solve(1, s1_cnt, 0, s1_cnt); for (int l = 1; l <= s1_cnt; ++l) dp[j + l * i] = so[l]; } } printf("%lld\n", dp[k]); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; priority_queue<int> q[4]; int n, m; long long f[4][300003]; void FoehnDestruct() { int k = q[1].size(); for (int i = 1; i <= k && i <= m; i++) { f[1][i] = f[1][i - 1] + q[1].top(); q[1].pop(); } for (int i = k + 1; i <= m; i++) f[1][i] = f[1][i - 1]; } void LightningStorm() { int k = q[2].size(); long long c, l = 1, sum = 0, t = 0; for (int i = 1; i <= k && i <= m; i++) { c = q[2].top(); q[2].pop(); for (int j = l; j <= m; j++) { l = j; if (j >= 2 * (t + 1) && f[1][j - t * 2] + sum < f[1][j - t * 2 - 2] + sum + c) { f[2][j] = sum + c + f[1][j - t * 2 - 2]; t++; sum += c; break; } else { f[2][j] = sum + f[1][j - t * 2]; } } } for (int j = l; j <= m; j++) { f[2][j] = sum + f[1][j - t * 2]; } } void DominatorControll() { long long sum = 0, t = 0; f[3][m] = f[2][m]; while (q[3].size()) { t += 3; sum += q[3].top(); q[3].pop(); if (t <= m) { f[3][m] = max(f[3][m], f[2][m - t] + sum); } } } void MODEV() { long long x = ~f[3][m] != -1 ? f[3][m] : 0; cout << x; return; } void Mental_Omega(int O) { if (~O == -1) MODEV(); } void NuclearMark() { for (int i = 10; ~i; i--) Mental_Omega(i); return; } int main() { cin >> n >> m; int mm, c; for (int i = 0; i < n; i++) { scanf("%d%d", &mm, &c); if (mm == 1) q[1].push(c); else if (mm == 2) q[2].push(c); else q[3].push(c); } FoehnDestruct(); LightningStorm(); DominatorControll(); NuclearMark(); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const double eps = 1e-8; long long int memo[300005]; long long int sum[300005][3]; vector<int> v[4]; int first[300005]; int second[300005]; int main() { ios_base::sync_with_stdio(0); int n, m; int w, c; while (cin >> n >> m) { for (int i = 0; i < n; ++i) { cin >> w >> c; v[w].push_back(c); } for (int i = 1; i <= 3; ++i) sort(v[i].rbegin(), v[i].rend()); memset(sum, 0, sizeof(sum)); for (int i = 0; i < 3; ++i) { for (int j = 1; j <= m; ++j) { sum[j][i] = sum[j - 1][i]; if (j <= v[i + 1].size()) sum[j][i] = v[i + 1][j - 1] + sum[j - 1][i]; } } memset(memo, 0, sizeof(memo)); memset(first, 0, sizeof(first)); memset(second, 0, sizeof(second)); memo[1] = sum[1][0]; first[1] = 1; for (int j = 2; j <= m; ++j) { if (sum[first[j - 1] + 1][0] + sum[second[j - 1]][1] > sum[first[j - 2]][0] + sum[second[j - 2] + 1][1]) { first[j] = first[j - 1] + 1; second[j] = second[j - 1]; memo[j] = sum[first[j]][0] + sum[second[j - 1]][1]; } else { first[j] = first[j - 2]; second[j] = second[j - 2] + 1; memo[j] = sum[first[j - 2]][0] + sum[second[j]][1]; } } long long int mx = 0; for (int i = 0; i <= m; ++i) { mx = max(mx, memo[i] + sum[(m - i) / 3][2]); } cout << mx << "\n"; for (int i = 1; i <= 3; ++i) v[i].clear(); } return 0; }
11
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:667177216") using namespace std; const long long MOD = 1000000000 + 7; struct dpval { long long cost; int c1, c2; }; dpval D[355500]; bool comp(pair<long long, long long> &a, pair<long long, long long> &b) { if (a.first == b.first && a.second == b.second) return false; return !(a.first * 1LL * b.second > a.second * 1LL * b.first); } int main() { ios::sync_with_stdio(false); cin.tie(); memset(D, -1, sizeof(D)); int n, m; cin >> n >> m; vector<pair<long long, long long>> wc(n); for (int(i) = 0; (i) < n; (i)++) { cin >> wc[i].first >> wc[i].second; } vector<long long> w1, w2, w3; for (int(i) = 0; (i) < n; (i)++) { if (wc[i].first == 1) w1.push_back(wc[i].second); if (wc[i].first == 2) w2.push_back(wc[i].second); if (wc[i].first == 3) w3.push_back(wc[i].second); } sort((w1).begin(), (w1).end()); sort((w2).begin(), (w2).end()); sort((w3).begin(), (w3).end()); reverse((w1).begin(), (w1).end()); reverse((w2).begin(), (w2).end()); reverse((w3).begin(), (w3).end()); memset(D, 0, sizeof(D)); D[0] = {0, 0, 0}; for (int i = 0; i <= m; ++i) { auto cur = D[i]; long long cost = cur.cost; int c1 = cur.c1; int c2 = cur.c2; if (D[i + 1].cost < D[i].cost) { D[i + 1] = D[i]; } if (D[i + 2].cost < D[i].cost) { D[i + 2] = D[i]; } if (c1 != w1.size()) { if (cost + w1[c1] > D[i + 1].cost) { D[i + 1].cost = cost + w1[c1]; D[i + 1].c1 = D[i].c1 + 1; D[i + 1].c2 = D[i].c2; } } if (c2 != w2.size()) { if (cost + w2[c2] > D[i + 2].cost) { D[i + 2].cost = cost + w2[c2]; D[i + 2].c1 = D[i].c1; D[i + 2].c2 = D[i].c2 + 1; } else { if (cost + w2[c2] == D[i + 2].cost && D[i + 1].c2 + 1 < D[i].c2) { D[i + 2].c1 = D[i].c1; D[i + 2].c2 = D[i].c2 + 1; } } } } long long ans = D[m].cost; long long sum3 = 0; for (int i = 0; i < w3.size(); ++i) { sum3 += w3[i]; if (m - 3 * (i + 1) >= 0) { ans = max(ans, sum3 + D[m - 3 * (i + 1)].cost); } } cout << ans; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int M = 3e5 + 111; const int INF = 1e9 + 7; struct dpp { long long v, s1, s2; } dp[M]; int n, m; long long res, w, c; long long a[4][M], s[4][M]; int num[4]; int cmp(int x, int y) { return x > y; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%lld%lld", &w, &c); a[w][++num[w]] = c; } for (int i = 1; i <= 3; i++) sort(a[i] + 1, a[i] + num[i] + 1, cmp); for (int j = 1; j <= num[3]; j++) s[3][j] = s[3][j - 1] + a[3][j]; dp[1].s1 = 1, dp[1].s2 = 0, dp[1].v = a[1][1]; for (int i = 2; i <= m; i++) { dp[i] = dp[i - 1]; if (dp[i - 1].v + a[1][dp[i - 1].s1 + 1] > dp[i].v) dp[i].v += a[1][dp[i - 1].s1 + 1], ++dp[i].s1 = dp[i - 1].s1 + 1; if (dp[i - 2].v + a[2][dp[i - 2].s2 + 1] > dp[i].v) dp[i] = dp[i - 2], dp[i].v += a[2][dp[i - 2].s2 + 1], ++dp[i].s2; } for (int i = 0; i <= num[3] && i <= m / 3; i++) res = max(res, s[3][i] + dp[m - 3 * i].v); printf("%lld\n", res); }
11
CPP
#include <bits/stdc++.h> using namespace std; int n, m, s1[300010], s2[300010]; long long f[300010]; vector<int> v[4]; bool cmp(int x, int y) { return x > y; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { int x, y; scanf("%d%d", &x, &y); v[x].push_back(y); } for (int i = 1; i <= 3; i++) { sort(v[i].begin(), v[i].end(), cmp); } for (int i = 1; i <= m; i++) { if (s1[i - 1] < v[1].size()) { if (f[i - 1] + v[1][s1[i - 1]] > f[i]) { f[i] = f[i - 1] + v[1][s1[i - 1]]; s1[i] = s1[i - 1] + 1; s2[i] = s2[i - 1]; } } if (i > 1 && s2[i - 2] < v[2].size()) { if (f[i - 2] + v[2][s2[i - 2]] > f[i]) { f[i] = f[i - 2] + v[2][s2[i - 2]]; s1[i] = s1[i - 2]; s2[i] = s2[i - 2] + 1; } } } for (int i = 1; i <= m; i++) { f[i] = max(f[i], f[i - 1]); } long long s = 0, ans = 0; ans = max(ans, f[m]); for (int i = 0; i < v[3].size(); i++) { if ((i + 1) * 3 > m) { break; } s += v[3][i]; ans = max(ans, s + f[m - 3 - i * 3]); } cout << ans << endl; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; int n, m, sum; long long ans, pre[4][N]; vector<int> a[4]; long long check(int mid, int have) { int one = have - 2 * mid; if (one > a[1].size()) one = a[1].size(); return pre[2][mid] + pre[1][one]; } long long del(int have) { int l = 0, r = a[2].size(); r = min(r, have / 2); while (l < r - 1) { int mid = l + r >> 1; int mid2 = r + mid >> 1; if (check(mid, have) > check(mid2, have)) r = mid2; else l = mid; } return max(max(check(l, have), check(r, have)), max(check(0, have), check(have / 2, have))); ; } int main() { scanf("%d%d", &n, &m); a[1].push_back(0), a[2].push_back(0), a[3].push_back(0); for (int i = 1; i <= n; i++) { int x, y; scanf("%d%d", &x, &y); a[x].push_back(y); sum += x, ans += y; } if (m >= sum) { printf("%lld\n", ans); return 0; } ans = 0; sort(a[1].begin(), a[1].end(), greater<int>()); sort(a[2].begin(), a[2].end(), greater<int>()); sort(a[3].begin(), a[3].end(), greater<int>()); for (int k = 1; k <= 3; k++) for (int i = 0; i <= a[k].size() - 1; i++) pre[k][i + 1] = pre[k][i] + a[k][i]; for (int i = 0; i <= a[3].size(); i++) { if (i * 3 <= m) ans = max(ans, del(m - i * 3) + pre[3][i]); } printf("%lld\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int const inf = 1000 * 1000 * 1000; long long const inf64 = 1ll * inf * inf; int const N = 1e5 + 5; int const M = 3e5 + 5; int n, m; int w[N]; int c[N]; vector<int> perm; int mn[N]; int mn2[N]; int mx[N]; long long pc[N]; long long pw[N]; int sz; long long value[M]; int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d %d", &w[i], &c[i]); if (w[i] <= 2) { perm.push_back(i); } } sort((perm).begin(), (perm).end(), [&](int i, int j) { return 2 * c[i] / w[i] > 2 * c[j] / w[j]; }); sz = (int)perm.size(); for (int i = sz - 1; i >= 0; i--) { if (i == sz - 1) { mx[i] = -inf; } else mx[i] = mx[i + 1]; if (w[perm[i]] == 1) mx[i] = max(mx[i], c[perm[i]]); } for (int i = 0; i < sz; i++) { pc[i] = (i > 0 ? pc[i - 1] : 0ll) + c[perm[i]]; pw[i] = (i > 0 ? pw[i - 1] : 0ll) + w[perm[i]]; if (i == 0) mn[i] = mn2[i] = inf; else mn[i] = mn[i - 1], mn2[i] = mn2[i - 1]; if (w[perm[i]] == 1) { mn[i] = min(mn[i], c[perm[i]]); } else { mn2[i] = min(mn2[i], c[perm[i]]); } } for (int j = 0; j < M; j++) { int idx = lower_bound(pw, pw + sz, j) - pw; int ql = max(0, idx - 3); int qr = min(sz - 1, idx + 3); for (int i = ql; i <= qr; i++) { if (pw[i] <= j) { value[j] = max(value[j], pc[i]); } else if (pw[i] == j + 1) { value[j] = max(value[j], pc[i] - mn[i]); value[j] = max(value[j], pc[i] - mn2[i] + (i + 1 < sz ? mx[i] : -inf)); } } } long long res = 0; for (int j = 0; j <= m; j++) { res = max(res, value[j]); if (j > 0) { value[j] = max(value[j], value[j - 1]); } } vector<long long> arr; for (int i = 1; i <= n; i++) { if (w[i] == 3) { arr.push_back(c[i]); } } sort((arr).begin(), (arr).end()); reverse((arr).begin(), (arr).end()); for (int i = 0; i < (int)arr.size(); i++) { if (i > 0) arr[i] += arr[i - 1]; if (3 * (i + 1) > m) break; res = max(res, value[m - 3 * (i + 1)] + arr[i]); } cout << res << "\n"; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; struct Qwa { long long cost; int cnt[4]; }; int main(int argc, char**) { if (argc > 1) { freopen("debug/in.txt", "r", stdin); freopen("debug/out.txt", "w", stdout); } ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<int> wc[4]; for (int k = 1; k < 4; ++k) wc[k].reserve(n); for (int i = 0; i < n; ++i) { int w, c; cin >> w >> c; wc[w].push_back(c); } for (int k = 1; k < 4; ++k) sort((wc[k]).begin(), (wc[k]).end(), greater<int>()); vector<Qwa> dp(m + 1); fill(dp[0].cnt, dp[0].cnt + 4, 0); long long maxCost = 0; for (int j = 1; j < m + 1; ++j) { long long cost = 0; int wBest = 0; for (int w = 1; w < 4 && j - w >= 0; ++w) { Qwa q = dp[j - w]; int cnt = q.cnt[w]; if (wc[w].size() > cnt) { long long wCost = q.cost + wc[w][cnt]; if (wCost >= cost) { cost = wCost; wBest = w; } } } if (cost) { dp[j] = dp[j - wBest]; dp[j].cost = cost; ++dp[j].cnt[wBest]; maxCost = max(maxCost, cost); } } { Qwa q1 = dp[m - 1]; if (q1.cnt[1] && wc[2].size() > q1.cnt[2]) { long long cost = q1.cost - wc[1][q1.cnt[1] - 1] + wc[2][q1.cnt[2]]; maxCost = max(maxCost, cost); } } if (m > 1) { Qwa q2 = dp[m - 2]; if (q2.cnt[1] && wc[3].size() > q2.cnt[3]) { long long cost = q2.cost - wc[1][q2.cnt[1] - 1] + wc[3][q2.cnt[3]]; maxCost = max(maxCost, cost); } } cout << maxCost << endl; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 300010; struct { long long v, s1, s2; } dp[MAXN]; int n, m; long long a[4][MAXN]; long long s[4][MAXN]; int num[4]; int cmp(int x, int y) { return x > y; } int main() { cin >> n >> m; num[1] = num[2] = num[3] = 0; for (int i = 1; i <= n; i++) { long long w, c; cin >> w >> c; a[w][++num[w]] = c; } for (int i = 1; i <= 3; i++) { sort(a[i] + 1, a[i] + num[i] + 1, cmp); for (int j = 1; j <= num[i]; j++) s[i][j] = s[i][j - 1] + a[i][j]; } dp[0].v = dp[0].s1 = dp[0].s2 = 0; for (int i = 1; i <= m; i++) { dp[i] = dp[i - 1]; if (dp[i - 1].v + a[1][dp[i - 1].s1 + 1] > dp[i].v) { dp[i].v = dp[i - 1].v + a[1][dp[i - 1].s1 + 1]; dp[i].s1 = dp[i - 1].s1 + 1; dp[i].s2 = dp[i - 1].s2; } if (i >= 2 && dp[i - 2].v + a[2][dp[i - 2].s2 + 1] > dp[i].v) { dp[i].v = dp[i - 2].v + a[2][dp[i - 2].s2 + 1]; dp[i].s1 = dp[i - 2].s1; dp[i].s2 = dp[i - 2].s2 + 1; } } long long res = 0; for (int i = 0; i <= num[3]; i++) if (m >= i * 3) res = max(res, s[3][i] + dp[m - 3 * i].v); cout << res << '\n'; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; long long N, M, F[400005], S[400005], Cnt3, Ans; long long Petya[4][300005], Sz[4]; long long P[4][4] = {{0}, {0, 1, 1}, {0, 2, 1}}; bool Cmp(long long X, long long Y) { return X > Y; } signed main() { scanf("%lld%lld", &N, &M); for (long long I = 1; I <= N; I++) { long long W, C; scanf("%lld%lld", &W, &C); Petya[W][++Sz[W]] = C, Cnt3 += (W == 3); } for (long long I = 1; I <= 3; I++) sort(Petya[I] + 1, Petya[I] + Sz[I] + 1, Cmp), Sz[I] = M; for (long long I = 1; I <= M; I++) S[I] = S[I - 1] + Petya[3][I]; F[1] = Petya[1][1]; for (long long I = 2; I <= M; I++) { long long Cur = (I & 1) + 1; if (Petya[1][P[Cur][1]] + Petya[1][P[Cur][1] + 1] > Petya[2][P[Cur][2]]) F[I] = F[I - 2] + Petya[1][P[Cur][1]] + Petya[1][P[Cur][1] + 1], P[Cur][1] += 2; else F[I] = F[I - 2] + Petya[2][P[Cur][2]], P[Cur][2]++; } for (long long I = 0; I <= min(M / 3, Cnt3); I++) Ans = max(Ans, S[I] + F[M - I * 3]); printf("%lld", Ans); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; string convertstring(long long n) { stringstream ss; ss << n; return ss.str(); } const int N = 300002; pair<int, long long> a[N]; std::vector<long long> v[4]; long long dp[N]; std::vector<long long> oddtimes, eventimes; int main() { int n, m, i; long long maxi = 0; scanf("%d", &n); scanf("%d", &m); for (i = (int)0; i < (int)n; i++) { scanf("%d", &a[i].first); scanf("%lld", &a[i].second); v[a[i].first].push_back(a[i].second); } for (i = (int)1; i < (int)4; i++) { sort(v[i].begin(), v[i].end()); reverse(v[i].begin(), v[i].end()); } if (v[1].size() >= 1) maxi = v[1][0]; oddtimes = v[2]; eventimes = v[2]; for (i = 1; i < v[1].size(); i += 2) { eventimes.push_back(v[1][i] + v[1][i - 1]); } for (i = 2; i < v[1].size(); i += 2) { oddtimes.push_back(v[1][i] + v[1][i - 1]); } sort(oddtimes.begin(), oddtimes.end()); sort(eventimes.begin(), eventimes.end()); reverse(oddtimes.begin(), oddtimes.end()); reverse(eventimes.begin(), eventimes.end()); for (i = 1; i < oddtimes.size(); i++) { oddtimes[i] += oddtimes[i - 1]; } for (i = 1; i < eventimes.size(); i++) { eventimes[i] += eventimes[i - 1]; } for (i = 1; i <= m; i++) { if (i % 2) { dp[i] = maxi; int p = min((i / 2) - 1, (int)oddtimes.size() - 1); if (p >= 0) dp[i] = maxi + oddtimes[p]; } else { int p = min((i / 2) - 1, (int)eventimes.size() - 1); if (p >= 0) dp[i] = eventimes[p]; } dp[i] = max(dp[i], dp[i - 1]); } long long ans = dp[m], wt = 0, cost = 0; for (i = 0; i < v[3].size(); i++) { wt += 3; if (wt > m) break; cost += v[3][i]; ans = max(ans, cost + dp[m - wt]); } cout << ans << endl; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 9; const int M = 3e5 + 9; struct Triple { long long cost; int cnt1, cnt2, cnt3; } dp[M]; int n, m; long long sum3[N]; vector<int> c[4]; bool cmp(int x, int y) { return x > y; } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { int weight, cost; cin >> weight >> cost; c[weight].push_back(cost); } sort(c[1].begin(), c[1].end(), cmp); sort(c[2].begin(), c[2].end(), cmp); sort(c[3].begin(), c[3].end(), cmp); dp[0].cost = 0; dp[0].cnt1 = 0; dp[0].cnt2 = 0; dp[0].cnt3 = 0; for (int i = 0; i <= m; i++) { dp[i].cost = 0; } for (int i = 1; i <= m; i++) { if (dp[i - 1].cnt1 < c[1].size() && dp[i - 1].cost + c[1][dp[i - 1].cnt1] > dp[i].cost) { dp[i].cost = dp[i - 1].cost + c[1][dp[i - 1].cnt1]; dp[i].cnt1 = dp[i - 1].cnt1 + 1; dp[i].cnt2 = dp[i - 1].cnt2; dp[i].cnt3 = dp[i - 1].cnt3; } if (i >= 3 && dp[i - 3].cnt3 < c[3].size() && dp[i - 3].cost + c[3][dp[i - 3].cnt3] > dp[i].cost) { dp[i].cost = dp[i - 3].cost + c[3][dp[i - 3].cnt3]; dp[i].cnt1 = dp[i - 3].cnt1; dp[i].cnt2 = dp[i - 3].cnt2; dp[i].cnt3 = dp[i - 3].cnt3 + 1; } if (i >= 1 && dp[i - 1].cost >= dp[i].cost) dp[i] = dp[i - 1]; if (i >= 3 && dp[i - 3].cost >= dp[i].cost) dp[i] = dp[i - 3]; } long long res = dp[m].cost; if (c[2].size() > 0) sum3[0] = c[2][0]; for (int i = 1; i < c[2].size(); i++) sum3[i] = sum3[i - 1] + c[2][i]; for (int i = 0; i < c[2].size(); i++) if (m - 2 * (i + 1) >= 0) res = max(res, dp[m - 2 * (i + 1)].cost + sum3[i]); cout << res << endl; }
11
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class T> using V = vector<T>; template <class T, class U> using P = pair<T, U>; using vll = V<ll>; using vvll = V<vll>; template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } const ll MOD = 1000000007; const ll HIGHINF = (ll)1e18; const int INF = 1e9; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; V<V<int> > wc(3); vll w3; for (int i = 0; i < n; i++) { int w; ll c; cin >> w >> c; if (w == 3) w3.emplace_back(c); else wc[w].emplace_back(c); } sort(w3.begin(), w3.end()); w3.emplace_back(0LL); reverse(w3.begin(), w3.end()); for (int i = 1; i < w3.size(); i++) w3[i] += w3[i - 1]; sort(wc[1].begin(), wc[1].end()); reverse(wc[1].begin(), wc[1].end()); sort(wc[2].begin(), wc[2].end()); reverse(wc[2].begin(), wc[2].end()); int i1 = 0, i2 = 0; V<P<ll, P<int, int> > > dp(m + 1, make_pair(0, make_pair(0, 0))); for (int i = 0; i < m; i++) { if (dp[i].second.first < wc[1].size() && i + 1 <= m && dp[i + 1].first < dp[i].first + wc[1][dp[i].second.first]) { dp[i + 1].first = dp[i].first + wc[1][dp[i].second.first]; dp[i + 1].second = make_pair(dp[i].second.first + 1, dp[i].second.second); } if (dp[i].second.second < wc[2].size() && i + 2 <= m && dp[i + 2].first < dp[i].first + wc[2][dp[i].second.first]) { dp[i + 2].first = dp[i].first + wc[2][dp[i].second.second]; dp[i + 2].second = make_pair(dp[i].second.first, dp[i].second.second + 1); } } ll ans = 0; for (int i = 0; i <= m; i++) { chmax(ans, dp[i].first + w3[min((m - i) / 3, (int)w3.size() - 1)]); } cout << ans << '\n'; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; vector<long long int> u[4], v[4]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int n, m, i, j, ans = 0; cin >> n >> m; for (i = 0; i < n; ++i) { long long int x, y; cin >> x >> y; v[x].push_back(y); u[x].push_back(y); } v[3].push_back(1e18); for (i = 1; i <= 3; ++i) { sort(v[i].begin(), v[i].end(), greater<long long int>()); sort(u[i].begin(), u[i].end(), greater<long long int>()); } v[3][0] = 0; for (i = 1; i < (long long int)(v[3].size()); ++i) v[3][i] += v[3][i - 1]; for (i = 1; i < (long long int)(v[1].size()); i += 2) { v[2].push_back(v[1][i] + v[1][i - 1]); } for (i = 2; i < (long long int)(u[1].size()); i += 2) { u[2].push_back(u[1][i] + u[1][i - 1]); } sort(v[2].begin(), v[2].end(), greater<long long int>()); sort(u[2].begin(), u[2].end(), greater<long long int>()); for (i = 0; i < (long long int)(v[3].size()); ++i) { long long int r = m, val1 = 0, val2 = 0; r = r - 3 * i; if (r < 0) continue; long long int y = r; val1 = v[3][i]; val2 = v[3][i]; if ((long long int)(u[1].size()) && y > 0) { val1 = val1 + u[1][0]; --y; } for (j = 0; j < min((y / 2), (long long int)(u[2].size())); ++j) { val1 = val1 + u[2][j]; } for (j = 0; j < min((r / 2), (long long int)(v[2].size())); ++j) { val2 = val2 + v[2][j]; } ans = max({ans, val1, val2}); } cout << ans; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; bool cmp(pair<int, long long> a, pair<int, long long> b) { return a.second / a.first > b.second / b.first; } int main() { int n, m, w, l, r; long long c, dp[300500], mx; cin >> n >> m; vector<pair<int, long long>> t(n); for (int i = 0; i < n; ++i) { scanf("%d%I64d", &w, &c); t[i] = make_pair(w, c); } sort(t.begin(), t.end(), cmp); memset(dp, 0, sizeof(dp)); r = 0; for (int i = 0; i < n; ++i) { r = min(r + t[i].first, m); l = max(t[i].first, r - 6); for (int j = r; j >= l; --j) { dp[j] = max(dp[j], dp[j - t[i].first] + t[i].second); } } mx = 0; for (int i = 0; i <= m; ++i) { mx = max(mx, dp[i]); } cout << mx << endl; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int cnt[4]; vector<int> arr[4]; vector<int> sorted[2]; long long sum0[100010]; long long sum1[100010]; long long mx[300010]; int main() { int n, m, w, c; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> w >> c; arr[w].push_back(c); cnt[w]++; } for (int i = 1; i <= 3; i++) sort(arr[i].begin(), arr[i].end(), greater<int>()); for (auto o : arr[2]) sorted[0].push_back(o), sorted[1].push_back(o); for (int i = 0; i + 1 < arr[1].size(); i += 2) sorted[0].push_back(arr[1][i] + arr[1][i + 1]); for (int i = 1; i + 1 < arr[1].size(); i += 2) sorted[1].push_back(arr[1][i] + arr[1][i + 1]); sort(sorted[0].begin(), sorted[0].end(), greater<int>()); sort(sorted[1].begin(), sorted[1].end(), greater<int>()); for (int i = 1; i <= sorted[0].size(); i++) sum0[i] = sum0[i - 1] + sorted[0][i - 1]; for (int i = 1; i <= sorted[1].size(); i++) sum1[i] = sum1[i - 1] + sorted[1][i - 1]; for (int i = 1; i <= 300000; i++) { long long v = 0; if (i & 1) { if (cnt[1] != 0) if (i / 2 <= sorted[1].size()) v = sum1[i / 2] + arr[1][0]; } else { if (i / 2 <= sorted[0].size()) v = sum0[i / 2]; } mx[i] = max(mx[i - 1], v); } long long ans = 0, sum = 0; for (int i = 0; i <= cnt[3]; i++) { if (m >= i * 3) ans = max(ans, sum + mx[m - i * 3]); if (i < cnt[3]) sum += arr[3][i]; } cout << ans; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; ofstream fo("test.out"); ifstream fi("test.inp"); using namespace std; int n, m, pos; struct P { int w, val; } p[101000]; bool cmp(P p1, P p2) { return (long long)p1.val * p2.w > (long long)p2.val * p1.w; } long long f[301000], ans; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d%d", &p[i].w, &p[i].val); sort(p + 1, p + n + 1, cmp); for (int i = 1; i <= n; i++) { pos = min(pos + p[i].w, m); for (int j = min(m, pos + 10); j >= max(p[i].w, pos - 100); j--) { f[j] = max(f[j], f[j - p[i].w] + p[i].val); ans = max(ans, f[j]); } } printf("%I64d", ans); }
11
CPP
#include <bits/stdc++.h> using namespace std; inline int getint() { static char c; while ((c = getchar()) < '0' || c > '9') ; int res = c - '0'; while ((c = getchar()) >= '0' && c <= '9') res = res * 10 + c - '0'; return res; } template <class T> inline bool relax(T &a, const T &b) { if (b > a) { a = b; return true; } return false; } const int MaxN = 100005; const int MaxM = 300005; const int T = 3; int n, m; int cnt[T], cost[T][MaxM]; long long f[MaxM]; int main() { n = getint(), m = getint(); for (int i = 1; i <= n; ++i) { int w = getint() - 1; int c = getint(); cost[w][++cnt[w]] = c; } for (int i = 0; i < 3; ++i) { sort(cost[i] + 1, cost[i] + cnt[i] + 1, greater<int>()); while (cnt[i] < m + 4) cost[i][++cnt[i]] = 0; } int v0 = 1, v1 = 1; for (int i = 2; i <= m; i += 2) { int c0 = cost[0][v0] + cost[0][v0 + 1]; int c1 = cost[1][v1]; f[i] = f[i - 2] + (c0 > c1 ? (v0 += 2, c0) : (++v1, c1)); } f[1] = cost[0][v0 = 2, v1 = 1]; for (int i = 3; i <= m; i += 2) { int c0 = cost[0][v0] + cost[0][v0 + 1]; int c1 = cost[1][v1]; f[i] = f[i - 2] + (c0 > c1 ? (v0 += 2, c0) : (++v1, c1)); } long long sum = 0, res = 0; for (int i = 0; i <= cnt[2] && i * 3 <= m; ++i) { sum += cost[2][i]; relax(res, sum + f[m - i * 3]); } cout << res << endl; return 0; }
11
CPP
#include <bits/stdc++.h> #pragma GCC optimize("Ofast", "unroll-loops") using namespace std; const int iinf = 1 << 29; const long long inf = 1ll << 60; const long long mod = 1e9 + 7; void GG() { cout << "No\n"; exit(0); } long long mpow(long long a, long long n) { long long re = 1; while (n > 0) { if (n & 1) re = re * a % mod; a = a * a % mod; n >>= 1; } return re; } long long inv(long long b) { if (b == 1) return b; return (mod - mod / b) * inv(mod % b) % mod; } const int maxn = 1e5 + 5; vector<long long> a[4]; vector<long long> even, odd; vector<long long> eps, ops; long long poss(long long w) { if (w < 0) return -inf; long long re = 0; if (w & 1 && a[1].size() > 0) { re += a[1][0]; re += ops[min((w - 1) / 2, (long long)ops.size() - 1)]; return re; } else { re += eps[min(w / 2, (long long)eps.size() - 1)]; return re; } } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < (n); i++) { int w; long long c; cin >> w >> c; a[w].push_back(c); } a[1].push_back(0); for (int i = 0; i < (4); i++) { sort(a[i].begin(), a[i].end(), greater<long long>()); } even = odd = a[2]; eps.push_back(0); ops.push_back(0); if (a[1].size()) { for (int i = 1; i + 1 < a[1].size(); i += 2) { odd.push_back(a[1][i] + a[1][i + 1]); } } for (int i = 0; i + 1 < a[1].size(); i += 2) { even.push_back(a[1][i] + a[1][i + 1]); } sort(even.begin(), even.end(), greater<long long>()); sort(odd.begin(), odd.end(), greater<long long>()); for (int i = 0; i < (odd.size()); i++) { ops.push_back(ops.back() + odd[i]); } for (int i = 0; i < (even.size()); i++) { eps.push_back(eps.back() + even[i]); } long long re = 0; long long s3 = 0; for (int i = 0; i < (a[3].size() + 1); i++) { re = max(re, (__typeof__(re))(s3 + poss(m - 3 * i))); if (i != a[3].size()) { s3 += a[3][i]; } } cout << re << '\n'; }
11
CPP
#include <bits/stdc++.h> using namespace std; int N, C; vector<int> Adj[4]; vector<int> v2; int n1, n2, n3; long long s[4][100005]; long long ans1, ans2; bool cmp(const int& a, const int& b) { return a > b; } void calS(int k) { sort(Adj[k].begin(), Adj[k].end(), cmp); for (int i = 0; i < Adj[k].size(); i++) { s[k][i + 1] = s[k][i] + Adj[k][i]; } return; } long long solve(int n2) { n1 = C - 3 * n3 - 2 * n2; if (n1 < 0) return 0; n1 = max(n1, 0); n1 = min(n1, int(Adj[1].size())); return s[2][n2] + s[1][n1]; } long long work(int C) { int l = 0, r = min(C / 2, int(Adj[2].size())) - 1; if (r <= 0) { return max(solve(0), solve(1)); } int mid; while (r - l > 5) { mid = (r + l) / 2; if (solve(mid + 1) > solve(mid)) l = mid; else r = mid; } for (mid = l; mid <= r; mid++) { if (solve(mid + 1) < solve(mid)) return solve(mid); } return solve(r + 1); } int main() { cin >> N >> C; int w, v; for (int i = 1; i <= N; i++) { cin >> w >> v; Adj[w].push_back(v); } for (int k = 1; k <= 3; k++) calS(k); long long ans = 0; for (n3 = 0; 3 * n3 <= C && n3 <= Adj[3].size(); n3++) { ans = max(ans, s[3][n3] + work(C - 3 * n3)); } cout << ans; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; long long a1[maxn], a2[maxn], a3[maxn], n, m, num1, num2, num3, ans, s2[maxn], s3[maxn], am[maxn]; bool cmp(long long a, long long b) { return a > b; } void add1(long long c) { num1++; a1[num1] = c; } void add2(long long c) { num2++; a2[num2] = c; } void add3(long long c) { num3++; a3[num3] = c; } void work() { sort(a2 + 1, a2 + num2 + 1, cmp); sort(a3 + 1, a3 + num3 + 1, cmp); memset(s2, 0, sizeof(s2)); memset(s3, 0, sizeof(s3)); for (int i = 1; i <= num2; i++) s2[i] = s2[i - 1] + a2[i]; for (int i = 1; i <= num3; i++) s3[i] = s3[i - 1] + a3[i]; for (long long i = 0; i * 2 <= m; i++) { long long tp = (m - i * 2) / 3; ans = max(ans, s2[min(i, num2)] + s3[min(tp, num3)]); } } void del2() { a2[num2] = 0; num2--; } int main() { cin >> n >> m; long long w, c; for (int i = 1; i <= n; i++) { scanf("%lld%lld", &w, &c); if (w == 1ll) add1(c); if (w == 2ll) add2(c); if (w == 3ll) add3(c); } sort(a1 + 1, a1 + num1 + 1, cmp); m--; memcpy(am, a2, sizeof(a2)); long long cnt = 0ll; for (int i = 3; i <= num1; i += 2) { long long tp = a1[i] + a1[i - 1]; add2(tp); cnt++; } work(); m++; if (m >= 1) ans += a1[1]; memcpy(a2, am, sizeof(am)); for (int i = 2; i <= num1; i += 2) { long long tp = a1[i] + a1[i - 1]; add2(tp); } work(); cout << ans << endl; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; int n, m; long long dp[100 * maxn]; struct node { long long w, v; node() {} node(long long w, long long v) : w(w), v(v) {} }; vector<node> v; bool cmp(node& a, node& b) { return a.v * b.w > b.v * a.w; } int main() { while (~scanf("%d%d", &n, &m)) { v.clear(); for (int i = 1; i <= n; i++) { int a, b; scanf("%d%d", &a, &b); v.push_back(node(a, b)); } sort(v.begin(), v.end(), cmp); int siz = v.size(); long long sum = 0; for (int i = 0; i <= m; ++i) dp[i] = 0; for (int i = 0; i < siz; i++) { sum = (sum + v[i].w < m) ? (sum + v[i].w) : m; for (int j = sum; j >= max(v[i].w, sum - 101); j--) dp[j] = max(dp[j], dp[j - v[i].w] + v[i].v); } long long ans = 0; for (int i = 1; i <= m; i++) ans = max(ans, dp[i]); printf("%lld\n", ans); } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int MAX_N = 100000; const int MAX_M = 300000; struct Stat { long long c; int a, b; Stat() {} Stat(long long _c, int _a, int _b) : c(_c), a(_a), b(_b) {} bool operator<(const Stat &s) const { return c < s.c; } bool operator>(const Stat &s) const { return c > s.c; } }; vector<int> cs[4]; Stat dp[MAX_M + 1]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { int wi, ci; scanf("%d%d", &wi, &ci); cs[wi].push_back(ci); } for (int i = 1; i <= 3; i++) sort(cs[i].begin(), cs[i].end(), greater<int>()); dp[0] = Stat(0, 0, 0); int sz1 = cs[1].size(), sz2 = cs[2].size(); for (int i = 0; i < m; i++) { if (dp[i + 1] < dp[i]) dp[i + 1] = dp[i]; long long ci = dp[i].c; int ai = dp[i].a, bi = dp[i].b; if (ai < sz1) { Stat d1 = Stat(ci + cs[1][ai], ai + 1, bi); if (dp[i + 1] < d1) dp[i + 1] = d1; } if (i + 2 <= m && bi < sz2) { Stat d1 = Stat(ci + cs[2][bi], ai, bi + 1); if (dp[i + 2] < d1) dp[i + 2] = d1; } } long long maxsum = 0, sum3 = 0; for (int i = 0, j = m; j >= 0; i++, j -= 3) { if (i > 0 && i <= cs[3].size()) sum3 += cs[3][i - 1]; long long sum = dp[j].c + sum3; if (maxsum < sum) maxsum = sum; } printf("%lld\n", maxsum); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 100005; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); { long long i, j, k, n, m, ans = 0, cnt = 0, sum = 0; cin >> n >> k; pair<long long, long long> a[n]; vector<long long> v[4]; for (i = 0; i < n; i++) { cin >> a[i].first >> a[i].second; v[a[i].first].push_back(a[i].second); } sort(v[1].rbegin(), v[1].rend()); sort(v[2].rbegin(), v[2].rend()); sort(v[3].rbegin(), v[3].rend()); long long pref1[v[1].size() + 2]; pref1[0] = 0; for (i = 0; i < v[1].size(); i++) { pref1[i + 1] = pref1[i] + v[1][i]; } long long pref2[v[2].size() + 2]; pref2[0] = 0; for (i = 0; i < v[2].size(); i++) { pref2[i + 1] = pref2[i] + v[2][i]; } long long w = 0; for (i = 0; i <= v[3].size(); i++) { if (i > 0) { sum += v[3][i - 1]; w += 3; } if (w > k) break; ans = max(ans, sum); if (w == k) break; long long left2 = 0, right2 = v[2].size(); while (left2 <= right2) { long long mid = (left2 + right2) / 2; if (mid * 2 + w > k) { right2 = mid - 1; continue; } long long pos1 = min(k - (mid * 2 + w), (long long)v[1].size()); long long temp = pref2[mid] + pref1[pos1]; long long ss = 0; if (pos1 < v[1].size()) ss += v[1][pos1]; if (pos1 + 1 < v[1].size()) ss += v[1][pos1 + 1]; ans = max(ans, sum + pref2[mid] + pref1[pos1]); if (mid - 1 >= 0 and ss > v[2][mid - 1]) { right2 = mid - 1; } else { left2 = mid + 1; } } } cout << ans << "\n"; } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; long long N, M; pair<long long, vector<long long> > dp[300001]; vector<long long> G[3]; signed main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N >> M; for (long long i = 0; i < (N); i++) { long long w, c; cin >> w >> c; G[w - 1].push_back(c); } for (long long i = 0; i < (3); i++) { sort((G[i]).begin(), (G[i]).end(), greater<long long>()); } long long hoge = 1; for (long long i = 0; i <= M; i++) { dp[i] = pair<long long, vector<long long> >(0, {0, 0, 0}); } for (long long i = 1; i <= M; i++) { dp[i] = max(dp[i], dp[i - 1]); for (long long k = 0; k < (3); k++) { if (k == hoge) continue; if (i - 1 - k < 0) continue; long long c = dp[i - 1 - k].second[k]; if (c < G[k].size()) { pair<long long, vector<long long> > p = dp[i - 1 - k]; p.first += G[k][c]; p.second[k]++; dp[i] = max(dp[i], p); } } } long long m = dp[M].first, sum = 0; for (long long x = 0; x <= G[hoge].size(); x++) { if (x > 0) sum += G[hoge][x - 1]; long long w = M - (hoge + 1) * x; if (w < 0) continue; m = max(m, sum + dp[w].first); } cout << m << "\n"; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; vector<int> a[4]; int ind1[300005], ind2[300005]; long long dp[300005]; int main() { int n, m, i, w, c; long long x, y, sum, ans, weight; scanf("%d%d", &n, &m); for (i = 1; i <= n; i++) { scanf("%d%d", &w, &c); a[w].push_back(c); } for (i = 1; i <= 3; i++) { sort(a[i].begin(), a[i].end()); reverse(a[i].begin(), a[i].end()); } if (a[1].size() != 0) { dp[1] = a[1][0]; ind1[1] = 1; } for (i = 2; i <= m; i++) { x = y = 0; if (ind1[i - 1] < a[1].size()) x = dp[i - 1] + a[1][ind1[i - 1]]; if (ind2[i - 2] < a[2].size()) y = dp[i - 2] + a[2][ind2[i - 2]]; if (x > y) { dp[i] = x; ind1[i] = ind1[i - 1] + 1; ind2[i] = ind2[i - 1]; } else { dp[i] = y; ind1[i] = ind1[i - 2]; ind2[i] = ind2[i - 2] + 1; } if (dp[i - 1] > dp[i]) { dp[i] = dp[i - 1]; ind1[i] = ind1[i - 1]; ind2[i] = ind2[i - 1]; } } ans = dp[m]; sum = 0; for (i = 0; i < a[3].size(); i++) { sum += a[3][i]; weight = (i + 1) * 3; if (weight > m) break; ans = max(ans, dp[m - weight] + sum); } printf("%lld\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> const int MAX_N = 1e5 + 5; const int MAX_W = 3; int n, m, c[MAX_N * MAX_W][MAX_W - 1]; long long f[MAX_N * MAX_W]; std::vector<int> a[MAX_W]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) { int w, c; scanf("%d%d", &w, &c); a[w - 1].push_back(c); } for (int i = 0; i < MAX_W; ++i) std::sort(a[i].begin(), a[i].end(), std::greater<int>()); for (int i = 1; i <= m; ++i) { f[i] = f[i - 1]; c[i][0] = c[i - 1][0]; c[i][1] = c[i - 1][1]; if (c[i - 1][0] < a[0].size() && f[i - 1] + a[0][c[i - 1][0]] > f[i]) { f[i] = f[i - 1] + a[0][c[i - 1][0]]; c[i][0] = c[i - 1][0] + 1; c[i][1] = c[i - 1][1]; } if (i > 1 && c[i - 2][1] < a[1].size() && f[i - 2] + a[1][c[i - 2][1]] > f[i]) { f[i] = f[i - 2] + a[1][c[i - 2][1]]; c[i][0] = c[i - 2][0]; c[i][1] = c[i - 2][1] + 1; } } long long val = 0, wgt = 0, ans = f[m]; for (int x : a[2]) if ((wgt += 3) <= m) ans = std::max(ans, (val += x) + f[m - wgt]); printf("%lld\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; void read(int &x) { x = 0; char ch = getchar(); bool f = (ch == '-'); for (; ch < '0' || ch > '9'; ch = getchar()) f |= (ch == '-'); for (; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48); if (f) x = -x; } void read(long long &x) { x = 0; char ch = getchar(); bool f = (ch == '-'); for (; ch < '0' || ch > '9'; ch = getchar()) f |= (ch == '-'); for (; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48); if (f) x = -x; } const int maxn = 5000010; int n, m, cnt1, cnt2, cnt3, w, c; long long M, sum1, sum2, tot; int one[maxn], two[maxn], three[maxn]; long long maxx[maxn], sum3[maxn]; int l1[maxn], l2[maxn], l3[maxn]; bool cmp(int a, int b) { return a > b; } int main() { read(n); read(M); for (register int i = 1; i <= n; ++i) { read(w), read(c); (w == 1) ? one[++cnt1] = c : 0; (w == 2) ? two[++cnt2] = c : 0; (w == 3) ? three[++cnt3] = c : 0; sum1 += c; sum2 += w; } if (M < sum2) m = (int)M; else return !printf("%I64d\n", sum1); long long ans = 0; sort(one + 1, one + cnt1 + 1, cmp); sort(two + 1, two + cnt2 + 1, cmp); sort(three + 1, three + cnt3 + 1, cmp); for (register int i = 1; i <= cnt3; ++i) sum3[i] = sum3[i - 1] + three[i]; if (cnt1 > 0) maxx[1] = one[1], l1[1] = 1, l2[1] = 0; for (register int i = 2; i <= m; ++i) { maxx[i] = maxx[i - 1]; l1[i] = l1[i - 1]; l2[i] = l2[i - 1]; if (maxx[i] < maxx[i - 1] + one[l1[i - 1] + 1]) { maxx[i] = maxx[i - 1] + one[l1[i - 1] + 1]; l1[i] = l1[i - 1] + 1; l2[i] = l2[i - 1]; } if (maxx[i] < maxx[i - 2] + two[l2[i - 2] + 1]) { maxx[i] = maxx[i - 2] + two[l2[i - 2] + 1]; l2[i] = l2[i - 2] + 1; l1[i] = l1[i - 2]; } } for (register int i = 0; i <= cnt3 && (long long)i * 3 <= m; ++i) ((ans) < (maxx[m - i * 3] + sum3[i]) ? (ans) = (maxx[m - i * 3] + sum3[i]) : 0); printf("%I64d\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100010; inline long long Min(const long long &a, const long long &b) { return a < b ? a : b; } inline long long Max(const long long &a, const long long &b) { return a > b ? a : b; } inline char get(void) { static char buf[100000], *p1 = buf, *p2 = buf; if (p1 == p2) { p2 = (p1 = buf) + fread(buf, 1, 100000, stdin); if (p1 == p2) return EOF; } return *p1++; } inline void read(long long &x) { x = 0; static char c; for (; !(c >= '0' && c <= '9'); c = get()) ; for (; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = get()) ; } long long s[4][N], n, m, pre[4][N], ans; int main(void) { read(n), read(m); long long w, c; for (int i = 1; i <= n; i++) { read(w), read(c); s[w][++*s[w]] = c; } for (int i = 1; i <= 3; i++) { sort(s[i] + 1, s[i] + 1 + *s[i], std::greater<long long>()); for (int k = 1; k <= *s[i]; k++) pre[i][k] = pre[i][k - 1] + s[i][k]; } for (int i = 0; i <= *s[3] && m >= 0; i++) { int L = 0, R = Min(m / 2, *s[2]), Mid = 0; while (L < R) { Mid = (L + R) >> 1; int m1 = Min(*s[1], m - Mid * 2); if (m1 + Mid * 2 < m - 1 || s[2][Mid] > s[1][m1] * 2) L = Mid + 1; else R = Mid; } for (int k = Max(0, Mid - 50); k <= Min(*s[2], Mid + 50); k++) { if (k * 2 > m) break; int m1 = Min(*s[1], m - k * 2); ans = Max(ans, pre[2][k] + pre[1][m1] + pre[3][i]); } m -= 3; } printf("%I64d\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int n, m; long long ans = 0; struct stuff { long long val; int c1, c2; }; stuff f[300010]; vector<long long> souvenir[5]; void online_judge(void) { freopen("testcase.inp", "r", stdin); freopen("testcase.out", "w", stdout); } bool cmp(long long x, long long y) { return (x > y); } void inp(void) { cin >> n >> m; for (int i = (1); i <= (n); i++) { int w; long long c; cin >> w >> c; souvenir[w].push_back(c); } for (int w = (1); w <= (3); w++) sort(souvenir[w].begin(), souvenir[w].end(), cmp); } void solve(void) { for (int i = (1); i <= (m); i++) { f[i] = f[i - 1]; if (f[i - 1].c1 <= (int)souvenir[1].size() - 1 and f[i].val < f[i - 1].val + souvenir[1][f[i - 1].c1]) { f[i].val = f[i - 1].val + souvenir[1][f[i - 1].c1]; f[i].c1++; } if (i > 1) { if (f[i - 2].c2 <= (int)souvenir[2].size() - 1 and f[i].val < f[i - 2].val + souvenir[2][f[i - 2].c2]) { f[i].val = f[i - 2].val + souvenir[2][f[i - 2].c2]; f[i].c2 = f[i - 2].c2 + 1; f[i].c1 = f[i - 2].c1; } } } long long value = 0; for (int i = (0); i <= ((int)souvenir[3].size()); i++) { if (3 * i > m) break; ans = max(ans, value + f[m - i * 3].val); if (i < (int)souvenir[3].size()) value += souvenir[3][i]; } cout << ans; } int main(void) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); inp(); solve(); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int MAX = 4 * 1e5; long long int dp[MAX]; pair<int, int> cnts[MAX]; vector<int> first, second, third; void sortRev(vector<int> &arr) { sort(arr.begin(), arr.end()); reverse(arr.begin(), arr.end()); } int main() { int n, m, w, c; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d%d", &w, &c); if (w == 1) { first.push_back(c); } else if (w == 2) { second.push_back(c); } else if (w == 3) { third.push_back(c); } } sortRev(first); sortRev(second); sortRev(third); dp[0] = 0; for (int i = 1; i <= m; i++) { dp[i] = dp[i - 1]; cnts[i] = cnts[i - 1]; if (cnts[i - 1].first < first.size()) { cnts[i] = cnts[i - 1]; dp[i] = dp[i - 1] + first[cnts[i].first]; cnts[i].first++; } if (i >= 2) { if (cnts[i - 2].second < second.size()) { long long int ones = 0; int last = cnts[i].first; if (last >= 2) { ones += first[last - 1] + first[last - 2]; } else if (last >= 1) { ones += first[last - 1]; } if ((dp[i - 2] + second[cnts[i - 2].second]) >= dp[i]) { cnts[i] = cnts[i - 2]; dp[i] = dp[i - 2] + second[cnts[i - 2].second]; cnts[i].second++; } } } } long long int curr = 0, ans = dp[m]; for (int i = 0; i < third.size(); i++) { curr += third[i]; int x = m - (3 * (i + 1)); if (x >= 0) { ans = max(ans, dp[x] + curr); } } printf("%lld\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int MX = 3e5 + 69; const int inf = 1e9 + 5; const long long mod = 1e9 + 7; const long double eps = 1e-7; int n, c; long long f[MX]; struct fuck { long long w, c, dif; } a[MX]; int p[11], l[11]; vector<long long> v[11]; vector<pair<long long, long long> > pp; long long dp[4111][1111]; long long ok = 1; long long fu(int id, int k) { if (id >= pp.size() || id > 3001) { return 0; } if (k == 0) { return 0; } long long &ans = dp[id][k]; if (ans != -1) { return ans; } ans = 0; ans = max(ans, fu(id + 1, k)); if (k - 3 >= 0 && pp[id].second == 3) ans = max(ans, pp[id].first + fu(id + 1, k - 3)); if (k - 2 >= 0 && pp[id].second == 2) ans = max(ans, pp[id].first + fu(id + 1, k - 2)); if (k - 1 >= 0 && pp[id].second == 1) ans = max(ans, pp[id].first + fu(id + 1, k - 1)); return ans; } long long solve(int p1, int p2, int p3, int k) { if (k == 0) { return 0; } if (k > 1003) { if (p3 < l[3] && 2 * v[3][p3] >= 3 * v[2][p2] && v[3][p3] >= 3 * v[1][p1]) { return v[3][p3] + solve(p1, p2, p3 + 1, k - 3); } else if (p2 < l[2] && 3 * v[2][p2] >= 2 * v[3][p3] && v[2][p2] >= 2 * v[1][p1]) { return v[2][p2] + solve(p1, p2 + 1, p3, k - 2); } else if (p1 < l[1] && 3 * v[1][p1] >= v[3][p3] && 2 * v[1][p1] >= v[2][p2]) { return v[1][p1] + solve(p1 + 1, p2, p3, k - 1); } } else { for (int i = p1; i < min(l[1], p1 + 1533); i++) { pp.push_back({v[1][i], 1}); } for (int i = p2; i < min(l[2], p2 + 633); i++) { pp.push_back({v[2][i], 2}); } for (int i = p3; i < min(l[3], p3 + 433); i++) { pp.push_back({v[3][i], 3}); } return fu(0, k); } } bool cmp(fuck a, fuck b) { return (a.dif > b.dif); } int main() { cin >> n >> c; for (int i = 0; i < n; i++) { long long w, c; scanf("%I64d %I64d", &w, &c); a[i] = {w, c, 6 * c / w}; } sort(a, a + n, cmp); long long k = 0; long long ans = 0; for (int i = 0; i < n; i++) { k += a[i].w; k = min(k, c * 1ll); for (int j = k; j >= max(a[i].w, k - 20); j--) { f[j] = max(f[j], f[j - a[i].w] + a[i].c); ans = max(ans, f[j]); } } cout << ans; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100000 + 10; const int M = 1000000007; const double PI = atan(1) * 4; const int oo = 1000000000; int n, m; vector<long long> v[4]; int main() { cin >> n >> m; for (int a, b, i = 0; i < n; ++i) { scanf("%d%d", &a, &b); v[a].push_back(b); } for (int i = 0; i < 4; ++i) { sort(v[i].rbegin(), v[i].rend()); v[i].insert(v[i].begin(), 0); for (int j = 1; j < v[i].size(); ++j) v[i][j] += v[i][j - 1]; } long long answer = 0; for (int i = 0; i < v[3].size() && i <= m / 3; ++i) { long long ans = v[3][i], bst = 0; int lo = 0, hi = v[2].size() - 1, md1, md2, lf = m - 3 * i; hi = min(hi, lf / 2); while (lo <= hi) { md1 = (2 * lo + hi) / 3; md2 = (lo + 2 * hi) / 3; long long an1 = v[2][md1] + v[1][min((int)v[1].size() - 1, lf - 2 * md1)]; long long an2 = v[2][md2] + v[1][min((int)v[1].size() - 1, lf - 2 * md2)]; bst = max(bst, max(an1, an2)); if (an1 > an2) hi = md2 - 1; else lo = md1 + 1; } answer = max(answer, ans + bst); } cout << answer << endl; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 5; int w[maxn], c[maxn]; vector<int> v[4]; struct data { long long v, l, r; bool operator>(const data &rhs) const { return v > rhs.v; } bool operator<(const data &rhs) const { return v < rhs.v; } data operator+(const data &rhs) const { return data(v + rhs.v, l + rhs.l, r + rhs.r); } data() {} data(long long a, int b, int c) : v(a), l(b), r(c) {} } dp[maxn]; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; ++i) scanf("%d %d", &w[i], &c[i]), v[w[i]].push_back(c[i]); for (int i = 1; i <= 3; ++i) sort(v[i].begin(), v[i].end()), reverse(v[i].begin(), v[i].end()); dp[0] = data(0, -1, -1); for (int i = 0; i < maxn; ++i) { if (i + 1 < maxn && dp[i].l + 1 < v[1].size()) dp[i + 1] = max(dp[i + 1], dp[i] + data(v[1][dp[i].l + 1], 1, 0)); if (i + 2 < maxn && dp[i].r + 1 < v[2].size()) dp[i + 2] = max(dp[i + 2], dp[i] + data(v[2][dp[i].r + 1], 0, 1)); } for (int i = 1; i < maxn; ++i) dp[i] = max(dp[i], dp[i - 1]); long long s = 0, ans = 0; for (int i = 0; i <= v[3].size(); ++i) { if (m - 3 * i < 0) continue; ans = max(ans, s + dp[m - 3 * i].v); if (i < v[3].size()) s += v[3][i]; } printf("%lld\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int qt[4]; vector<long long> c[4]; bool comp(long long a, long long b) { return a > b; } long long ts(int m) { int lo = 0, up = c[2].size() - 1; long long ret = 0; while (lo <= up) { int m1 = lo + (up - lo) / 3; int m2 = up - (up - lo) / 3; if (2 * m2 > m) { up = m2 - 1; continue; } long long f1 = c[2][m1] + c[1][min((int)c[1].size() - 1, m - 2 * m1)]; long long f2 = c[2][m2] + c[1][min((int)c[1].size() - 1, m - 2 * m2)]; if (f1 > f2) { up = m2 - 1; ret = f1; } else { lo = m1 + 1; ret = f2; } } return ret; } int main() { int n, m; while (scanf("%d%d", &n, &m) > 0) { for (int i = 1; i <= 3; i++) { c[i].clear(); c[i].push_back(0); } while (n--) { int w, _c; scanf("%d%d", &w, &_c); c[w].push_back(_c); } for (int i = 1; i <= 3; i++) { sort(++c[i].begin(), c[i].end(), comp); for (int j = 1; j < c[i].size(); j++) { c[i][j] += c[i][j - 1]; } } long long ans = 0; for (int i = 0; i < c[3].size() and 3 * i <= m; i++) { ans = max(ans, c[3][i] + ts(m - 3 * i)); } printf("%lld\n", ans); } }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; int n, m, sum; long long ans, pre[4][N]; vector<int> a[4]; long long check(int mid, int have) { int one = have - 3 * mid; if (one > a[1].size()) one = a[1].size(); return pre[3][mid] + pre[1][one]; } long long del(int have) { int l = 0, r = a[3].size(); r = min(r, have / 3); while (l < r - 1) { int mid = l + r >> 1, mid2 = r + mid >> 1; if (check(mid, have) > check(mid2, have)) r = mid2; else l = mid; } return max(max(check(l, have), check(r, have)), max(check(0, have), check(min(r, have / 3), have))); ; } int main() { scanf("%d%d", &n, &m); a[1].push_back(0), a[2].push_back(0), a[3].push_back(0); for (int i = 1; i <= n; i++) { int x, y; scanf("%d%d", &x, &y); a[x].push_back(y); sum += x, ans += y; } if (m >= sum) { printf("%lld\n", ans); return 0; } ans = 0; sort(a[1].begin(), a[1].end(), greater<int>()); sort(a[2].begin(), a[2].end(), greater<int>()); sort(a[3].begin(), a[3].end(), greater<int>()); for (int k = 1; k <= 3; k++) for (int i = 0; i <= a[k].size() - 1; i++) pre[k][i + 1] = pre[k][i] + a[k][i]; for (int i = 0; i <= a[2].size(); i++) if (i * 2 <= m) ans = max(ans, del(m - i * 2) + pre[2][i]); printf("%lld\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 10; int w[maxn], c[maxn]; vector<long long> adj[4]; long long f(int m, int t) { return adj[2][t] + adj[1][min((int)adj[1].size() - 1, (m - 2 * t))]; } long long get(int m) { int lo = 0, hi = min(m / 2, (int)adj[2].size() - 1); while (lo < hi) { int mid = (lo + hi) / 2; if (f(m, mid) < f(m, mid + 1)) lo = mid + 1; else hi = mid; } return f(m, lo); } int main() { int n, m; cin >> n >> m; adj[1].push_back(0); adj[2].push_back(0); adj[3].push_back(0); for (int i = 1; i <= n; ++i) { cin >> w[i] >> c[i]; adj[w[i]].push_back(c[i]); } for (int i = 1; i <= 3; ++i) { sort(adj[i].begin(), adj[i].end()); reverse(adj[i].begin() + 1, adj[i].end()); for (int j = 1; j < adj[i].size(); ++j) { adj[i][j] += adj[i][j - 1]; } } long long ans = 0; long long acc = 0; for (int i = 0; i < adj[3].size() && 3 * i <= m; ++i) { acc = adj[3][i]; ans = max(ans, acc + get(m - 3 * i)); } cout << ans << '\n'; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int P = 1e9 + 7; int add(int a, int b) { if ((a += b) >= P) a -= P; return a < 0 ? a + P : a; } int mul(int a, int b) { return 1ll * a * b % P; } int kpow(int a, int b) { int r = 1; for (; b; b >>= 1, a = mul(a, a)) { if (b & 1) r = mul(r, a); } return r; } bool cmp(pair<int, int> a, pair<int, int> b) { return 1.0 * a.second / a.first > 1.0 * b.second / b.first; } const int N = 3e5 + 8; long long dp[N], n, m, s; pair<int, int> a[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = (1); i < (n + 1); i++) cin >> a[i].first >> a[i].second; sort(a + 1, a + n + 1, cmp); for (int i = (1); i < (n + 1); i++) { s = min(s + a[i].first, m); for (int j = (s + 1) - 1; j >= (max(1ll * a[i].first, s - 4)); j--) dp[j] = max(dp[j], dp[j - a[i].first] + a[i].second); } long long ans = *max_element(dp, dp + m + 1); cout << ans << "\n"; return 0; }
11
CPP
#include <bits/stdc++.h> struct State { long long cost; int n1, n2; inline State add(int type, int cost_) const { return type == 1 ? State{cost + cost_, n1 + 1, n2} : State{cost + cost_, n1, n2 + 1}; } }; inline bool operator<(const State& a, const State& b) { return a.cost < b.cost || (a.cost == b.cost && (a.n2 > b.n2 || (a.n2 == b.n2 && a.n1 > b.n1))); } inline bool operator>(const State& a, const State& b) { return b < a; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); std::cerr.tie(0); int n, wmax; std::cin >> n >> wmax; std::vector<int> cost[4]; for (int i = 0; i < n; ++i) { int w, c; std::cin >> w >> c; cost[w].push_back(c); } for (int i = 1; i <= 3; ++i) { std::sort(cost[i].begin(), cost[i].end(), std::greater<int>()); } std::vector<State> max(1 + wmax, State{0, 0, 0}); for (int w = 0; w < wmax; ++w) { auto curr = max[w]; max[w + 1] = std::max(max[w + 1], curr); if (w + 1 <= wmax && curr.n1 < (int)cost[1].size()) { max[w + 1] = std::max(max[w + 1], curr.add(1, cost[1][curr.n1])); } if (w + 2 <= wmax && curr.n2 < (int)cost[2].size()) { max[w + 2] = std::max(max[w + 2], curr.add(2, cost[2][curr.n2])); } } std::vector<long long> pref{0}; for (auto it : cost[3]) { pref.push_back(pref.back() + it); } long long answ = 0; for (int n3 = 0; wmax - n3 * 3 >= 0 && n3 <= (int)cost[3].size(); ++n3) { answ = std::max(max[wmax - n3 * 3].cost + pref[n3], answ); } std::cout << answ; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; struct node { long long c; int n1, n2, n3; node(long long c = 0, int n1 = 0, int n2 = 0, int n3 = 0) : c(c), n1(n1), n2(n2), n3(n3) {} } dp[300005]; int N, M; int v[4][300005]; int cnt[4]; void update(node &a, node b) { if (a.c < b.c) a = b; } int main() { cin >> N >> M; int w, x; for (int i = 1; i <= N; i++) { cin >> w >> x; v[w][++cnt[w]] = x; } for (int w = 1; w <= 3; w++) { sort(v[w] + 1, v[w] + cnt[w] + 1, greater<int>()); } dp[0] = node(0, 0, 0, 0); long long ans = 0; for (int j = 0; j <= M; j++) { long long c = dp[j].c; int n1 = dp[j].n1, n2 = dp[j].n2, n3 = dp[j].n3; if (j + 1 <= M && n1 < cnt[1]) update(dp[j + 1], node(c + v[1][n1 + 1], n1 + 1, n2, n3)); if (j + 2 <= M && n2 < cnt[2]) update(dp[j + 2], node(c + v[2][n2 + 1], n1, n2 + 1, n3)); if (j + 3 <= M && n3 < cnt[3]) update(dp[j + 3], node(c + v[3][n3 + 1], n1, n2, n3 + 1)); if (j + 2 <= M && n1 && n3 < cnt[3]) update(dp[j + 2], node(c - v[1][n1] + v[3][n3 + 1], n1 - 1, n2, n3 + 1)); ans = max(ans, c); } cout << ans << endl; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int xm[4] = {-1, 1, 0, 0}; int ym[4] = {0, 0, -1, 1}; const int MOD = 1e9 + 7; const int MAXN = 5e5 + 5; const long long POW = 9973; struct nd { long long c; int t1, t2; }; nd dp[MAXN]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<int> ar[4]; for (int i = 0; i < n; i++) { int w, c; cin >> w >> c; ar[w].push_back(c); } for (int i = 1; i <= 3; i++) sort(ar[i].begin(), ar[i].end(), greater<int>()); int one = ar[1].size(); int two = ar[2].size(); for (int i = 0; i <= one + two * 2; i++) { if (i == 0) dp[i] = {0, 0, 0}; else { dp[i] = dp[i - 1]; if (i && dp[i - 1].t1 < one) { if (dp[i - 1].c + ar[1][dp[i - 1].t1] > dp[i].c) { dp[i] = {dp[i - 1].c + ar[1][dp[i - 1].t1], dp[i - 1].t1 + 1, dp[i - 1].t2}; } } if (i >= 2 && dp[i - 2].t2 < two) { if (dp[i - 2].c + ar[2][dp[i - 2].t2] > dp[i].c) { dp[i] = {dp[i - 2].c + ar[2][dp[i - 2].t2], dp[i - 2].t1, dp[i - 2].t2 + 1}; } } } } long long ans = 0; long long tot = 0; for (int i = 0; i <= ar[3].size(); i++) { int rm = m - 3 * i; if (rm < 0) break; ans = max(ans, tot + dp[min(rm, one + two * 2)].c); if (i < ar[3].size()) tot += ar[3][i]; } cout << ans; }
11
CPP
#include <bits/stdc++.h> using namespace std; priority_queue<int> unused[3]; priority_queue<int, vector<int>, greater<int>> used[3]; const int M = (int)3e5 + 91; long long mx[M]; void calc() { long long sum = 0; int tw = 0; if (unused[1].empty()) { while (!unused[2].empty()) { sum += unused[2].top(); unused[2].pop(); tw += 2; mx[tw] = sum; } } else { int t = unused[1].size() + 2 * unused[2].size(); int x, y, z; for (int i = 1; i <= t; i++) { if (!unused[1].empty()) { sum += unused[1].top(); used[1].push(unused[1].top()); unused[1].pop(); } else { unused[1].push(used[1].top()); sum -= used[1].top(); used[1].pop(); used[2].push(unused[2].top()); sum += unused[2].top(); unused[2].pop(); } while (used[1].size() >= 2 && unused[2].size() >= 1) { x = used[1].top(); used[1].pop(); y = used[1].top(); used[1].pop(); if (x + y < unused[2].top()) { sum -= x + y; sum += unused[2].top(); used[2].push(unused[2].top()); unused[2].pop(); unused[1].push(x); unused[1].push(y); } else { used[1].push(x); used[1].push(y); break; } } mx[i] = sum; } } for (int i = 1; i < M; i++) mx[i] = max(mx[i], mx[i - 1]); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int n, m; cin >> n >> m; int w, c; vector<int> ss; for (int i = 0; i < n; i++) { cin >> w >> c; if (w < 3) { unused[w].push(c); } else { ss.push_back(c); } } calc(); long long answ = mx[m]; sort(ss.begin(), ss.end()); reverse(ss.begin(), ss.end()); long long ssum = 0; for (int i = 0; i < ss.size(); i++) { ssum += ss[i]; m -= 3; if (m >= 0) answ = max(answ, ssum + mx[m]); } cout << answ; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; int w[maxn], c[maxn], arr[4][maxn], top[4], n, m; long long sum[4][maxn]; struct Node { long long cost; int i, j; } dp[maxn * 3]; int main() { while (~scanf("%d%d", &n, &m)) { long long ans = 0; for (int i = 1; i <= 3; i++) sum[i][0] = top[i] = 0; for (int i = 1; i <= n; i++) { scanf("%d%d", &w[i], &c[i]); arr[w[i]][++top[w[i]]] = c[i]; } for (int i = 1; i <= 3; i++) sort(arr[i] + 1, arr[i] + top[i] + 1, greater<int>()); for (int i = 1; i <= 3; i++) { for (int j = 1; j <= top[i]; j++) sum[i][j] = sum[i][j - 1] + arr[i][j]; } for (int i = 0; i <= m; i++) dp[i].cost = 0; dp[1].cost = arr[1][1], dp[1].i = 2, dp[1].j = 1; dp[0].i = dp[0].j = 1; for (int j = 2; j <= m; j++) { if (dp[j - 1].cost + (dp[j - 1].i <= top[1] ? arr[1][dp[j - 1].i] : 0) > dp[j - 2].cost + (dp[j - 2].j <= top[2] ? arr[2][dp[j - 2].j] : 0)) { dp[j].cost = dp[j - 1].cost + (dp[j - 1].i <= top[1] ? arr[1][dp[j - 1].i] : 0); dp[j].i = dp[j - 1].i + 1; dp[j].j = dp[j - 1].j; } else { dp[j].cost = dp[j - 2].cost + (dp[j - 2].j <= top[2] ? arr[2][dp[j - 2].j] : 0); dp[j].i = dp[j - 2].i; dp[j].j = dp[j - 2].j + 1; } } for (int i = 0; 3 * i <= m; i++) { ans = max(ans, sum[3][i] + dp[m - 3 * i].cost); } printf("%lld\n", ans); } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int Maxn = 300003; struct Node { int p1, p2; long long val; } d[Maxn]; int n, m; vector<int> save_val[5]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { int w, v; scanf("%d%d", &w, &v); save_val[w].push_back(v); } for (int i = 1; i <= 3; ++i) sort(save_val[i].begin(), save_val[i].end(), greater<int>()); for (int i = 1; i <= m; ++i) { d[i] = d[i - 1]; if (d[i - 1].p1 + 1 <= save_val[1].size() && d[i - 1].val + save_val[1][d[i - 1].p1] > d[i].val) { d[i].val = d[i - 1].val + save_val[1][d[i - 1].p1]; d[i].p1 = d[i - 1].p1 + 1; d[i].p2 = d[i - 1].p2; } if (i > 1 && d[i - 2].p2 + 1 <= save_val[2].size() && d[i - 2].val + save_val[2][d[i - 2].p2] > d[i].val) { d[i].val = d[i - 2].val + save_val[2][d[i - 2].p2]; d[i].p1 = d[i - 2].p1; d[i].p2 = d[i - 2].p2 + 1; } } long long sum = 0, ans = 0; for (int i = 0; i <= save_val[3].size() && i * 3 <= m; ++i) { ans = max(ans, sum + d[m - i * 3].val); if (i < save_val[3].size()) sum += save_val[3][i]; } printf("%lld\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int N, M; vector<vector<int> > C; vector<vector<long long> > psum; int main() { scanf("%d %d", &N, &M); C.resize(3); for (int i = 0; i < N; i++) { int w, c; scanf("%d %d", &w, &c); C[--w].push_back(c); } psum.resize(3); for (int i = 0; i < 3; i++) { sort(C[i].begin(), C[i].end()); if (i == 2) reverse(C[i].begin(), C[i].end()); psum[i].resize(C[i].size()); for (int j = 0; j < C[i].size(); j++) { psum[i][j] = C[i][j]; if (j) psum[i][j] += psum[i][j - 1]; } } long long ans = 0; int W = M - min((int)C[2].size(), M / 3) * 3; long long cost = 0; for (int i = min((int)C[2].size(), M / 3); i >= 0; i--) { long long tmp = i == 0 ? 0 : psum[2][i - 1]; while (W > 1) { if (C[0].size() == 0 && C[1].size() == 0) break; else if (C[0].size() == 0) { W -= 2; cost += C[1].back(); C[1].pop_back(); } else if (C[1].size() == 0) { W--; cost += C[0].back(); C[0].pop_back(); } else { int sum = 0; for (int i = 0; i < min(2, (int)C[0].size()); i++) sum += C[0][C[0].size() - 1 - i]; if (sum < C[1].back()) { W -= 2; cost += C[1].back(); C[1].pop_back(); } else { W--; cost += C[0].back(); C[0].pop_back(); } } } if (W == 1 && C[0].size()) ans = max(ans, C[0].back() + cost + tmp); else ans = max(ans, cost + tmp); W += 3; } printf("%lld", ans); }
11
CPP
#include <bits/stdc++.h> using namespace std; long long b[4][400000]; int main() { int n, m; cin >> n >> m; vector<int> v[4]; int id[4] = {0}; for (int i = 0; i < n; i++) { int x, y; scanf("%d%d", &x, &y); v[x].push_back(y); } for (int i = 0; i < 4; i++) { sort(v[i].begin(), v[i].end()); id[i] = (int)v[i].size() - 1; } memset(b, 0, sizeof(b)); int h = id[3]; for (int i = 1; i < 400000; i++) { if (h >= 0) { b[3][i] += v[3][h]; h--; } if (i > 0) b[3][i] += b[3][i - 1]; } h = id[2]; for (int i = 1; i < 400000; i++) { if (h >= 0) { b[2][i] += v[2][h]; h--; } if (i > 0) b[2][i] += b[2][i - 1]; } h = id[1]; for (int i = 1; i < 400000; i++) { if (h >= 0) { b[1][i] += v[1][h]; h--; } if (i > 0) b[1][i] += b[1][i - 1]; } long long maxx = 0; long long PL = 0; for (int i = 0; i <= v[3].size(); i++) { int t = m; t -= i * 3; if (t < 0) break; long long ans = b[3][i]; int st = 0, En = min((int)v[2].size(), t / 2); while ((En - st) >= 3) { int step = (En - st) / 3; int mid1 = (st + step); int mid2 = (En - step); int C1 = 0, C2 = 0; C1 = t - mid1 * 2; C2 = t - mid2 * 2; long long EE1 = b[2][mid1]; EE1 += (b[1][C1]); long long EE2 = b[2][mid2]; EE2 += (b[1][C2]); if (EE1 < EE2) { st = mid1; } else En = mid2; } long long F1 = 0, F2 = 0, F3 = 0; F1 = b[2][st] + b[1][t - st * 2]; F2 = b[2][En] + b[1][t - En * 2]; if (En - st == 2) F3 = b[2][st + 1] + b[1][t - (st + 1) * 2]; long long sum = max(F1, max(F2, F3)); sum += ans; maxx = max(maxx, sum); } cout << maxx; }
11
CPP
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const double eps = 1e-5; const int mod = 1000; const double pi = acos(-1); vector<long long> g[4]; vector<long long> gg; long long sum[400005]; int n, m; bool cmp(long long a, long long b) { return a > b; } int main() { cin >> n >> m; for (int i = 0; i < n; i++) { long long w, c; scanf("%lld%lld", &w, &c); g[w].push_back(c); } g[1].push_back(0); for (int i = 1; i <= 3; i++) { sort(g[i].begin(), g[i].end(), cmp); } gg = g[2]; for (int i = 1; i < g[1].size(); i += 2) { gg.push_back(g[1][i - 1] + g[1][i]); } sort(gg.begin(), gg.end(), cmp); memset(sum, 0, sizeof(sum)); for (int i = 0; i < gg.size(); i++) { sum[i + 1] = sum[i] + gg[i]; } for (int i = gg.size() + 1; i < 400005; i++) { sum[i] = sum[i - 1]; } long long ans = 0; long long pre = 0; for (int i = 0; i < gg.size(); i++) { if ((i + 1) * 2 > m) break; ans = max(ans, sum[i + 1]); } for (int i = 0; i < g[3].size(); i++) { if ((i + 1) * 3 > m) break; pre += g[3][i]; int k = (m - (i + 1) * 3) / 2; ans = max(ans, pre + sum[k]); } gg.clear(); gg = g[2]; for (int i = 2; i < g[1].size(); i += 2) { gg.push_back(g[1][i - 1] + g[1][i]); } long long tmp = g[1][0]; sort(gg.begin(), gg.end(), cmp); memset(sum, 0, sizeof(sum)); for (int i = 0; i < gg.size(); i++) { sum[i + 1] = sum[i] + gg[i]; } for (int i = gg.size() + 1; i < 400005; i++) { sum[i] = sum[i - 1]; } pre = 0; ans = max(ans, tmp); for (int i = 0; i < gg.size(); i++) { if ((i + 1) * 2 + 1 > m) break; ans = max(ans, tmp + sum[i + 1]); } for (int i = 0; i < g[3].size(); i++) { if ((i + 1) * 3 + 1 > m) break; pre += g[3][i]; int k = (m - (i + 1) * 3 - 1) / 2; ans = max(ans, pre + tmp + sum[k]); } cout << ans << endl; }
11
CPP
#include <bits/stdc++.h> int n, m, tot[4]; std::vector<long long> vec[4]; struct node { long long val; int cnt1, cnt2; bool operator<(const struct node &rhs) const { return val < rhs.val; } } dp[(300010)]; long long pre_dp[(300010)]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { long long w, c; scanf("%I64d%I64d", &w, &c); vec[w].push_back(c); } for (int i = 1; i <= 3; ++i) { std::sort(vec[i].begin(), vec[i].end(), std::greater<long long>()); tot[i] = (int)(vec[i].size()); } dp[0] = (node){0, 0, 0}; for (int i = 0; i < m; ++i) { long long val = dp[i].val; int cnt1 = dp[i].cnt1; int cnt2 = dp[i].cnt2; if (cnt1 < tot[1] && i + 1 <= m) { dp[i + 1] = std::max(dp[i + 1], (node){val + vec[1][cnt1], cnt1 + 1, cnt2}); } if (cnt2 < tot[2] && i + 2 <= m) { dp[i + 2] = std::max(dp[i + 2], (node){val + vec[2][cnt2], cnt1, cnt2 + 1}); } } for (int i = 1; i <= m; ++i) { pre_dp[i] = std::max(pre_dp[i - 1], dp[i].val); } long long ans = pre_dp[m], pre = 0; for (int i = 1; i <= tot[3] && 3 * i <= m; ++i) { pre += vec[3][i - 1]; ans = std::max(ans, pre + pre_dp[m - 3 * i]); } printf("%I64d\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> template <typename T1, typename T2> inline void chmin(T1 &x, T2 b) { if (b < x) x = b; } template <typename T1, typename T2> inline void chmax(T1 &x, T2 b) { if (b > x) x = b; } const int inf = 1039074182; using namespace std; int n, m; int w[100005], c[100005]; vector<int> v1; vector<int> v2; vector<int> v3; long long best[300005]; int rk1[100005]; int rk2[100005]; int main() { ios_base::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> w[i] >> c[i]; if (w[i] == 1) v1.push_back(c[i]); if (w[i] == 2) v2.push_back(c[i]); if (w[i] == 3) v3.push_back(c[i]); } sort(v1.begin(), v1.end()); reverse(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); reverse(v2.begin(), v2.end()); sort(v3.begin(), v3.end()); reverse(v3.begin(), v3.end()); vector<pair<int, pair<int, int> > > vsort; for (int i = 0; i < v1.size(); i++) { vsort.push_back(make_pair(v1[i] * 2, make_pair(1, i))); } for (int i = 0; i < v2.size(); i++) { vsort.push_back(make_pair(v2[i], make_pair(2, i))); } sort(vsort.begin(), vsort.end()); reverse(vsort.begin(), vsort.end()); for (int i = 0; i < vsort.size(); i++) { if (vsort[i].second.first == 1) rk1[vsort[i].second.second] = i; else rk2[vsort[i].second.second] = i; } long long v12 = 0; int in12 = 0, cost12 = 0; int last1 = 0, last2 = 0; for (int i = 0; i <= m; i++) { while (in12 < vsort.size() && cost12 + vsort[in12].second.first <= i) { cost12 += vsort[in12].second.first; v12 += vsort[in12].first / (3 - vsort[in12].second.first); if (vsort[in12].second.first == 1) last1++; else last2++; in12++; } best[i] = v12; if (cost12 + 1 == i) { if (last1 < v1.size()) best[i] = max(best[i], v12 + v1[last1]); if (last2 < v2.size() && last1) best[i] = max(best[i], v12 - v1[last1 - 1] + v2[last2]); } } long long res = best[m]; long long sum = 0; for (int i = 0; i < v3.size(); i++) { if (m - 3 * (i + 1) < 0) break; sum += v3[i]; chmax(res, best[m - 3 * (i + 1)] + sum); } cout << res << endl; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; pair<int, int> f[3 * 110000]; long long sumx[110000], sumy[110000], sumz[110000]; int n, m, w, c, x[110000], y[110000], z[110000]; bool cmp(int a, int b) { return a > b; } pair<int, int> add(pair<int, int> a, int k) { if (k == 1) a.first++; else a.second++; a.first = min(a.first, x[0]); a.second = min(a.second, y[0]); return a; } long long sum(pair<int, int> a) { return sumx[a.first] + sumy[a.second]; } pair<int, int> max(pair<int, int> a, pair<int, int> b) { if (sum(a) >= sum(b)) return a; else return b; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d%d", &w, &c); if (w == 1) x[++x[0]] = c; if (w == 2) y[++y[0]] = c; if (w == 3) z[++z[0]] = c; } sort(x + 1, x + x[0] + 1, cmp); sort(y + 1, y + y[0] + 1, cmp); sort(z + 1, z + z[0] + 1, cmp); for (int i = 1; i <= n; i++) { sumx[i] = sumx[i - 1] + x[i]; sumy[i] = sumy[i - 1] + y[i]; sumz[i] = sumz[i - 1] + z[i]; } f[0] = pair<int, int>(0, 0); f[1] = add(f[0], 1); for (int i = 2; i <= m; i++) f[i] = max(add(f[i - 1], 1), add(f[i - 2], 2)); long long ans = 0; for (int i = 0; 3 * i <= m; i++) ans = max(ans, sumz[i] + sum(f[m - 3 * i])); cout << ans; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; vector<long long> v[5]; struct Node { long long val, idx1, idx2; } dp[300100]; int main() { long long n, m; cin >> n >> m; for (long long i = 0; i < n; i++) { long long w, c; cin >> w >> c; v[w].push_back(c); } for (int i = 1; i <= 3; i++) sort(v[i].rbegin(), v[i].rend()); dp[0] = {0, 0, 0}; for (int i = 1; i <= m; i++) { if (i - 1 >= 0 && dp[i - 1].idx1 + 1 <= v[1].size()) dp[i] = {dp[i - 1].val + v[1][dp[i - 1].idx1], dp[i - 1].idx1 + 1, dp[i - 1].idx2}; if (i - 2 >= 0 && dp[i - 2].idx2 + 1 <= v[2].size() && dp[i - 2].val + v[2][dp[i - 2].idx2] > dp[i].val) dp[i] = {dp[i - 2].val + v[2][dp[i - 2].idx2], dp[i - 2].idx1, dp[i - 2].idx2 + 1}; if (i - 2 >= 0 && dp[i - 2].idx1 + 2 <= v[1].size() && dp[i - 2].val + v[1][dp[i - 2].idx1] + v[1][dp[i - 2].idx1 + 1] > dp[i].val) dp[i] = {dp[i - 2].val + v[1][dp[i - 2].idx1] + v[1][dp[i - 2].idx1 + 1], dp[i - 2].idx1 + 2, dp[i - 2].idx2}; } for (int i = 1; i <= m; i++) dp[i].val = max(dp[i].val, dp[i - 1].val); long long sum = 0, j = 1, Ans = dp[m].val; for (int i = 0; i < v[3].size() && j * 3 <= m; i++) { sum += v[3][i]; Ans = max(Ans, sum + dp[m - 3 * j].val); j++; } cout << Ans; return 0; }
11
CPP