solution
stringlengths 11
983k
| difficulty
int64 0
21
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
using namespace std;
long long n, m, ai, bi;
vector<long long> one, two, three;
int32_t main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (long long i = 0; i < n; i++) {
cin >> ai >> bi;
if (ai == 3)
three.push_back(bi);
else if (ai == 2)
two.push_back(bi);
else
one.push_back(bi);
}
sort(one.begin(), one.end(), greater<long long>());
sort(two.begin(), two.end(), greater<long long>());
sort(three.begin(), three.end(), greater<long long>());
vector<vector<long long> > answer;
for (long long i = 0; i <= m; i++) {
vector<long long> help;
help.push_back(-100000000000000000);
help.push_back(-1);
help.push_back(-1);
answer.push_back(help);
}
answer[0][0] = 0;
answer[0][1] = 0;
answer[0][2] = 0;
for (long long i = 0; i <= m; i++) {
if (answer[i][1] == -1) continue;
if (answer[i][1] < one.size()) {
long long v = answer[i][0] + one[answer[i][1]];
if (i + 1 <= m && answer[i + 1][0] < v) {
answer[i + 1][0] = v;
answer[i + 1][1] = answer[i][1] + 1;
answer[i + 1][2] = answer[i][2];
}
}
if (answer[i][2] < two.size()) {
long long v = answer[i][0] + two[answer[i][2]];
if (i + 2 <= m && answer[i + 2][0] < v) {
answer[i + 2][0] = v;
answer[i + 2][1] = answer[i][1];
answer[i + 2][2] = answer[i][2] + 1;
}
}
}
long long ans = 0;
long long summ = 0;
long long maximum = 0;
for (long long i = 0; i <= m; i++) {
maximum = max(maximum, answer[i][0]);
answer[i][0] = maximum;
}
for (long long i = 0; i <= three.size(); i++) {
if (3 * i > m) break;
if (i > 0) summ += three[i - 1];
if (summ + answer[m - 3 * i][0] > ans) {
ans = summ + answer[m - 3 * i][0];
}
}
cout << ans << endl;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
priority_queue<int> q[4];
int n, w;
long long dp[4][300003];
void fun1() {
int k = q[1].size();
for (int i = 1; i <= k && i <= w; i++) {
dp[1][i] = dp[1][i - 1] + q[1].top();
q[1].pop();
}
for (int i = k + 1; i <= w; i++) dp[1][i] = dp[1][i - 1];
}
void fun2() {
int k = q[2].size();
long long c, l = 1, sum = 0, t = 0;
for (int i = 1; i <= k && i <= w; i++) {
c = q[2].top();
q[2].pop();
for (int j = l; j <= w; j++) {
l = j;
if (j >= 2 * (t + 1) &&
dp[1][j - t * 2] + sum < dp[1][j - t * 2 - 2] + sum + c) {
dp[2][j] = sum + c + dp[1][j - t * 2 - 2];
t++;
sum += c;
break;
} else {
dp[2][j] = sum + dp[1][j - t * 2];
}
}
}
for (int j = l; j <= w; j++) {
dp[2][j] = sum + dp[1][j - t * 2];
}
}
void fun3() {
long long sum = 0, t = 0;
dp[3][w] = dp[2][w];
while (q[3].size()) {
t += 3;
sum += q[3].top();
q[3].pop();
if (t <= w) {
dp[3][w] = max(dp[3][w], dp[2][w - t] + sum);
}
}
}
int main() {
cin >> n >> w;
int ww, c;
for (int i = 0; i < n; i++) {
scanf("%d%d", &ww, &c);
if (ww == 1)
q[1].push(c);
else if (ww == 2)
q[2].push(c);
else
q[3].push(c);
}
fun1();
fun2();
fun3();
cout << dp[3][w];
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 29;
const long long LINF = 1LL << 60;
long long mod = 1e9 + 7;
char READ_DATA;
int SIGNAL_INPUT;
template <typename Type>
inline Type ru(Type &v) {
SIGNAL_INPUT = 1;
while ((READ_DATA = getchar()) < '0' || READ_DATA > '9')
if (READ_DATA == '-')
SIGNAL_INPUT = -1;
else if (READ_DATA == EOF)
return EOF;
v = READ_DATA - '0';
while ((READ_DATA = getchar()) >= '0' && READ_DATA <= '9')
v = v * 10 + READ_DATA - '0';
v *= SIGNAL_INPUT;
return v;
}
inline long long modru(long long &v) {
long long p = 0;
SIGNAL_INPUT = 1;
while ((READ_DATA = getchar()) < '0' || READ_DATA > '9')
if (READ_DATA == '-')
SIGNAL_INPUT = -1;
else if (READ_DATA == EOF)
return EOF;
p = v = READ_DATA - '0';
while ((READ_DATA = getchar()) >= '0' && READ_DATA <= '9') {
v = (v * 10 + READ_DATA - '0') % mod;
p = (p * 10 + READ_DATA - '0') % (mod - 1);
}
v *= SIGNAL_INPUT;
return p;
}
template <typename A, typename B>
inline char ru(A &x, B &y) {
if (ru(x) == EOF) return EOF;
ru(y);
return 2;
}
template <typename A, typename B, typename C>
inline char ru(A &x, B &y, C &z) {
if (ru(x) == EOF) return EOF;
ru(y);
ru(z);
return 3;
}
template <typename A, typename B, typename C, typename D>
inline char ru(A &x, B &y, C &z, D &w) {
if (ru(x) == EOF) return EOF;
ru(y);
ru(z);
ru(w);
return 4;
}
struct Edge {
int u, v, next;
int w, cap, flow;
Edge(int _u = 0, int _v = 0, int nxt = -1, long long _w = 1,
long long _cap = 0) {
u = _u;
v = _v;
w = _w;
cap = _cap;
flow = 0;
next = nxt;
}
int operator<(const Edge &b) const { return w < b.w; }
};
const int maxn = 3e5 + 7;
double eps = 1e-6;
int n, m;
long long cnt1, cnt2, cnt3;
long long a[maxn], b[maxn], c[maxn];
long long dp[maxn], pre[maxn];
pair<int, int> f[maxn];
int cmp(const long long &a, const long long &b) { return a > b; }
int main() {
ru(n, m);
long long w, v;
for (int i = 1; i <= n; ++i) {
ru(w, v);
if (w == 1)
a[++cnt1] = v;
else if (w == 2)
b[++cnt2] = v;
else
c[++cnt3] = v;
}
sort(a + 1, a + cnt1 + 1, cmp);
sort(b + 1, b + cnt2 + 1, cmp);
sort(c + 1, c + cnt3 + 1, cmp);
int x;
if (cnt1) {
dp[1] = a[1];
f[1].first = 1;
}
for (int i = 2; i <= m; ++i) {
x = 0;
dp[i] = dp[i - 1];
f[i] = f[i - 1];
if (f[i - 1].first < cnt1) {
dp[i] = dp[i - 1] + a[f[i - 1].first + 1];
x = 1;
}
if (f[i - 2].second < cnt2 && dp[i] < dp[i - 2] + b[f[i - 2].second + 1]) {
dp[i] = dp[i - 2] + b[f[i - 2].second + 1];
x = 2;
}
if (x == 1) {
f[i] = f[i - 1];
f[i].first++;
} else if (x == 2) {
f[i] = f[i - 2];
f[i].second++;
}
}
for (int i = 1; i <= m / 3; ++i) pre[i] = pre[i - 1] + c[i];
long long ans = 0;
for (int i = 0; i <= m; ++i) {
ans = max(ans, dp[i] + pre[(m - i) / 3]);
}
cout << ans;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5;
int n, m, a, b;
long long c[3][maxn];
long long sum[3][maxn];
int cnt[3];
long long cal(int n3, int n2) {
return sum[2][n3] + sum[1][n2] + sum[0][min(cnt[0], m - n3 * 3 - n2 * 2)];
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; ++i) {
scanf("%d%d", &a, &b);
c[a - 1][cnt[a - 1]++] = b;
}
for (int i = 0; i < 3; ++i) sort(c[i], c[i] + cnt[i], greater<long long>());
for (int i = 0; i < 3; ++i)
for (int j = 1; j <= cnt[i]; ++j) sum[i][j] = sum[i][j - 1] + c[i][j - 1];
long long ans = 0;
for (int i = 0; i <= min(m / 3, cnt[2]); ++i) {
int l = 0, r = min((m - i * 3) / 2, cnt[1]), lm, rm;
ans = max(ans, max(cal(i, l), cal(i, r)));
while (l < r) {
lm = (l + r) >> 1;
rm = (lm + r) >> 1;
if (cal(i, lm) > cal(i, rm))
r = rm;
else if (cal(i, lm) == cal(i, rm))
l = lm, r = rm;
else
l = lm;
}
ans = max(ans, cal(i, l));
}
cout << ans << endl;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
struct node {
int w;
long long c;
bool operator<(const node &r) const {
return c * r.w > r.c * w || c * r.w == r.c * w && w < r.w;
}
} e[maxn];
long long t[maxn];
long long a[maxn];
int f[maxn];
long long mi[maxn];
bool cmp(long long a, long long b) { return a > b; }
int main() {
int n, m;
scanf("%d%d", &n, &m);
int now1 = 0, now3 = 0;
for (int i = 1; i <= n; i++) {
int w;
long long c;
scanf("%d%lld", &w, &c);
if (w == 3)
t[++now3] = c;
else
e[++now1].w = w, e[now1].c = c;
}
sort(t + 1, t + 1 + now3, cmp);
for (int i = 2; i <= now3; i++) t[i] += t[i - 1];
sort(e + 1, e + 1 + now1);
int pre = 0;
mi[0] = 1e18;
for (int i = 1; i <= now1; i++) {
a[pre + e[i].w] = a[pre] + e[i].c;
f[pre + e[i].w] = e[i].w;
if (e[i].w == 1)
mi[pre + e[i].w] = e[i].c;
else
mi[pre + 1] = mi[pre + 2] = mi[pre];
pre += e[i].w;
}
for (int i = pre + 1; i <= m; i++) a[i] = a[i - 1];
long long nex1 = 0, nex2 = 0;
for (int i = pre; i >= 1; i--) {
if (f[i] == 1)
nex1 = a[i] - a[i - 1];
else if (f[i] == 2)
nex2 = a[i] - a[i - 2];
else {
a[i] = max(a[i - 1] + nex1, a[i - 1] + nex2 - mi[i]);
}
}
long long ans = 0;
for (int i = 0; i <= now3; i++) {
long long sum = t[i];
int len = m - i * 3;
if (len < 0) break;
sum += a[len];
ans = max(ans, sum);
}
printf("%lld\n", ans);
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct node {
long long v, s1, s2;
} dp[300005];
long long goods[4][300005];
long long num[4];
long long sum[300005];
int cmp(long long a, long long b) { return a > b; }
int main() {
long long n, m, w, c;
while (~scanf("%lld %lld", &n, &m)) {
memset(goods, 0, sizeof(goods));
memset(num, 0, sizeof(num));
memset(dp, 0, sizeof(dp));
for (long long i = 0; i < n; i++) {
scanf("%lld %lld", &w, &c);
goods[w][++num[w]] = c;
}
for (long long i = 1; i <= 3; i++) {
sort(goods[i] + 1, goods[i] + 1 + num[i], cmp);
}
for (long long i = 1; i <= num[3]; i++) {
sum[i] = sum[i - 1] + goods[3][i];
}
for (long long i = 1; i <= m; i++) {
dp[i] = dp[i - 1];
if (dp[i - 1].v + goods[1][dp[i - 1].s1 + 1] > dp[i].v) {
dp[i].v = dp[i - 1].v + goods[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 + goods[2][dp[i - 2].s2 + 1] > dp[i].v) {
dp[i].v = dp[i - 2].v + goods[2][dp[i - 2].s2 + 1];
dp[i].s1 = dp[i - 2].s1;
dp[i].s2 = dp[i - 2].s2 + 1;
}
}
long long ans = 0;
for (long long i = 0; i <= num[3] && i * 3 <= m; i++) {
ans = max(ans, dp[m - i * 3].v + sum[i]);
}
printf("%lld\n", ans);
}
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
long long ans, dp[N];
int ed, n, m;
struct node {
int w, c;
double val;
node(int a = 0, int b = 0) : w(a), c(b) {}
bool operator<(const node &b) const { return val > b.val; }
} a[N], tmp[N];
long long DP() {
for (int i = 1; i <= ed; i++) {
for (int j = m; j >= tmp[i].w; j--) {
dp[j] = max(dp[j], dp[j - tmp[i].w] + tmp[i].c);
}
}
long long mx = 0;
for (int i = 0; i <= m; i++) mx = max(mx, dp[i]);
return mx;
}
int main() {
scanf("%d%d", &n, &m);
ans = 0;
for (int i = 1; i <= n; i++)
scanf("%d%d", &a[i].w, &a[i].c), a[i].val = 1.0 * a[i].c / a[i].w;
sort(a + 1, a + 1 + n);
int tp[4];
memset(tp, -1, sizeof(tp));
for (int i = 1; i <= n; i++) {
if (m < 30) {
ed = 0;
while (i <= n) tmp[++ed] = a[i], i++;
for (int ii = 1; ii <= 3; ii++)
if (~tp[ii]) tmp[++ed] = node(ii, tp[ii]), m += ii, ans -= tp[ii];
ans += DP();
break;
}
ans += a[i].c;
m -= a[i].w;
tp[a[i].w] = a[i].c;
}
printf("%lld\n", ans);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 100000, M = 300000;
long long dp[M + 1][3];
vector<int> c[3];
int main() {
int n, m;
scanf("%d%d", &n, &m);
int ci, wi;
for (int i = 0; i < n; ++i) {
scanf("%d%d", &wi, &ci);
c[wi - 1].push_back(ci);
}
for (int i = 0; i < 3; ++i) sort(c[i].begin(), c[i].end(), greater<int>());
if (!c[0].empty()) {
dp[1][0] = 1;
dp[1][2] = c[0][0];
}
for (int i = 2; i <= m; ++i) {
if (c[0].size() + c[1].size() * 2 < i) {
dp[i][2] = max(dp[i - 1][2], dp[i - 2][2]);
continue;
}
dp[i][0] = dp[i - 1][0] + 1;
dp[i][1] = dp[i - 1][1];
dp[i][2] =
dp[i - 1][2] + (dp[i - 1][0] < c[0].size() ? c[0][dp[i - 1][0]] : 0);
if (dp[i - 2][2] + (dp[i - 2][1] < c[1].size() ? c[1][dp[i - 2][1]] : 0) >
dp[i][2]) {
dp[i][0] = dp[i - 2][0];
dp[i][1] = dp[i - 2][1] + 1;
dp[i][2] =
dp[i - 2][2] + (dp[i - 2][1] < c[1].size() ? c[1][dp[i - 2][1]] : 0);
}
}
long long mx = 0;
long long sum = 0;
for (int i = 0; i <= c[2].size() && m - 3 * i >= 0; ++i) {
mx = max(mx, dp[m - 3 * i][2] + sum);
if (i != c[2].size()) sum += c[2][i];
}
printf("%lld\n", mx);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
using UI = unsigned int;
using UC = unsigned char;
using VI = std::vector<int>;
using SI = std::set<int>;
using PI = std::pair<int, int>;
using PL = std::pair<LL, LL>;
constexpr LL MOD = 1000000009;
constexpr double EXP = 2.7182818284590452;
constexpr double Pi = 3.1415926535;
constexpr long double EPS = 1e-4;
constexpr int INF = 1000 * 1000 * 1001;
constexpr long long INFL = (LL)INF * (LL)INF;
inline LL gcd(LL a, LL b) {
if (a < b) swap(a, b);
while (b != 0) {
a %= b;
swap(a, b);
}
return a;
}
inline LL lcm(LL a, LL b) { return (a / gcd(a, b)) * b; }
LL extgcd(LL a, LL b, LL& x, LL& y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
LL x11, y11;
LL d = extgcd(b % a, a, x11, y11);
x = y11 - (b / a) * x11;
y = x11;
return d;
}
ULL poww(ULL v, ULL p, ULL mod) {
if (p == 0) return 1;
if (p & 1) {
return (poww(v, p - 1, mod) * v) % mod;
} else {
ULL t = poww(v, p >> 1L, mod);
return (t * t) % mod;
}
}
constexpr ULL L31018 = 3000000000000000000L;
constexpr ULL L1018 = 1000000000000000000L;
constexpr int MAXN = 300005;
int n, m;
int cnt[4];
int cur[4];
int arr[4][MAXN];
LL sum[4][MAXN];
LL res = 0;
int w = 0, c;
LL get_sum(int w, int p) { return sum[w][p] - sum[w][0]; }
LL get_sum12(int p1, int p2) {
return sum[1][p1] - sum[1][0] + sum[2][p2] - sum[2][0];
}
PI dp12[MAXN];
bool was[MAXN];
PI func(int w) {
if (w == 0) {
return std::make_pair(1, 1);
}
if (w == 1) {
return std::make_pair(((2 < cnt[1] + 1) ? (2) : (cnt[1] + 1)), 1);
}
if (was[w]) {
return dp12[w];
}
was[w] = true;
PI r1, r2;
r1 = func(w - 1);
if (r1.first <= cnt[1]) {
++r1.first;
}
r2 = func(w - 2);
LL s1 = 0, s2 = 0;
if (r2.first <= cnt[1]) {
s1 += arr[1][r2.first];
}
if (r2.first + 1 <= cnt[1]) {
s1 += arr[1][r2.first + 1];
}
if (r2.second <= cnt[2]) {
s2 += arr[2][r2.second];
}
if (s2 >= s1) {
++r2.second;
} else {
r2.first += 2;
}
if (get_sum12(r1.first - 1, r1.second - 1) >
get_sum12(r2.first - 1, r2.second - 1)) {
return dp12[w] = r1;
}
return dp12[w] = r2;
}
void accept() {
cin >> n >> m;
for (int i = (0); i < (4); ++i) {
cur[i] = 1;
}
for (int i = (1); i < (n + 1); ++i) {
scanf("%d%d", &w, &c);
arr[w][cur[w]++] = c;
++cnt[w];
}
for (int i = (1); i < (4); ++i) {
sort(arr[i] + 1, arr[i] + 1 + cnt[i], greater<int>());
}
for (int i = (1); i < (4); ++i) {
for (int j = (1); j < (n + 1); ++j) {
sum[i][j] = sum[i][j - 1] + arr[i][j];
}
}
LL res = 0, cur = 0;
for (int i = (0); i < (cnt[3] + 1); ++i) {
if (3 * i > m) {
break;
}
PI res12 = func(m - 3 * i);
cur = get_sum(3, i) + get_sum12(res12.first - 1, res12.second - 1);
res = ((res < cur) ? (cur) : (res));
}
cout << res;
}
int main(void) {
accept();
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long n, m;
vector<long long> v[4];
vector<long long> sum[4];
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n >> m;
for (long long i = 0; i < n; i++) {
long long w, c;
cin >> w >> c;
v[w].push_back(c);
}
for (long long i = 1; i <= 3; i++)
sort(v[i].rbegin(), v[i].rend()), sum[i].push_back(0);
for (long long i = 1; i <= 3; i++) {
for (long long j = 1; j <= v[i].size(); j++) {
long long val = sum[i].back() + v[i][j - 1];
sum[i].push_back(val);
}
}
long long ans = 0;
for (long long i = 0; i <= min(m, (long long)v[3].size()); i++) {
long long wt = m - 3 * i;
if (wt < 0) break;
long long low = 0;
long long high = min((long long)v[2].size(), wt / 2);
while (low < high) {
long long mid1 = low + (high - low) / 3;
long long mid2 = high - (high - low) / 3;
long long left1 = wt - mid1 * 2;
long long left2 = wt - mid2 * 2;
long long take1 = min((long long)v[1].size(), left1);
long long take2 = min((long long)v[1].size(), left2);
long long sum1 = sum[2][mid1] + sum[1][take1];
long long sum2 = sum[2][mid2] + sum[1][take2];
if (sum1 < sum2)
low = mid1 + 1;
else
high = mid2 - 1;
}
for (long long j = max(0ll, low - 2);
j <= min(min((long long)v[2].size(), wt / 2), low + 2); j++) {
long long left = wt - j * 2;
long long take = min((long long)v[1].size(), left);
long long sum1 = sum[2][j] + sum[1][take];
ans = max(ans, sum1 + sum[3][i]);
}
}
cout << ans;
}
| 11 | CPP |
#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;
const int maxn = 1e5 + 5;
vector<int> w[4];
int n, m;
long long GetAns(int t) {
int totw = m;
long long ans = 0;
for (int i = 0; i < min(t, (int)w[3].size()); i++) {
totw -= 3;
ans += w[3][i];
}
if (totw < 0) return 0;
int p1 = 0, p2 = 0;
while (totw >= 2 && p2 < w[2].size()) {
totw -= 2;
ans += w[2][p2++];
}
while (totw >= 1 && p1 < w[1].size()) {
totw--;
ans += w[1][p1++];
}
while (p1 + 1 < w[1].size() && p2 >= 1 &&
w[1][p1] + w[1][p1 + 1] > w[2][p2 - 1]) {
ans += w[1][p1] + w[1][p1 + 1];
p1 += 2;
p2--;
ans -= w[2][p2];
}
if (p1 < w[1].size() && p2 >= 1 && w[1][p1] > w[2][p2 - 1]) {
ans += w[1][p1];
p1++;
p2--;
ans -= w[2][p2];
}
return ans;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
int x, y;
scanf("%d %d", &x, &y);
w[x].push_back(y);
}
for (auto &v : w) sort(v.begin(), v.end(), greater<int>());
int L = 0, R = min((int)w[3].size(), m / 3);
while (R - L > 10) {
int mid = L + R >> 1;
int mmid = (mid + R) >> 1;
if (GetAns(mid) < GetAns(mmid))
L = mid;
else
R = mmid;
}
long long ans = 0;
for (int i = L; i <= R; i++) ans = max(ans, GetAns(i));
printf("%lld\n", ans);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > a;
vector<pair<long long, long long> > b;
vector<pair<long long, long long> > c;
long long n, m;
bool cmp(pair<long long, long long> x, pair<long long, long long> y) {
return x.second * y.first < x.first * y.second;
}
long long left__[5];
long long knapsack() {
vector<vector<long long> > dp;
dp.assign(b.size() + 5, vector<long long>(m + 5, 0));
for (long long i = 1; i <= b.size(); i++) {
long long w = b[i - 1].first;
long long val = b[i - 1].second;
for (long long j = 0; j <= m; j++) {
dp[i][j] = dp[i - 1][j];
if (j >= w) dp[i][j] = max(dp[i][j], dp[i - 1][j - w] + val);
}
}
return dp[b.size()][m];
}
long long solve1() {
long long ret = 0;
sort(a.begin(), a.end(), cmp);
while (!a.empty() && m > 60) {
if (left__[a.back().first] > 10) {
m -= a.back().first;
ret += a.back().second;
a.pop_back();
} else {
c.push_back(a.back());
a.pop_back();
}
}
long long cnt[5];
memset(cnt, 0, sizeof(cnt));
for (long long i = (signed)c.size() - 1; i >= 0; i--) {
long long x = c[i].first;
if (cnt[x] * x > m) continue;
cnt[x]++;
b.push_back(c[i]);
}
for (long long i = (signed)a.size() - 1; i >= 0; i--) {
long long x = a[i].first;
if (cnt[x] * x > m) continue;
cnt[x]++;
b.push_back(a[i]);
}
return ret + knapsack();
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (long long i = 1; i <= n; i++) {
long long x, y;
cin >> x >> y;
left__[x]++;
a.push_back(make_pair(x, y));
}
cout << solve1() << '\n';
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct Suv {
long long w, c;
double rate;
};
bool operator<(Suv a, Suv b) { return a.rate < b.rate; }
Suv a[100001];
long long dp[300010];
long long up, down, ans;
int i, j, n, m, c, w;
int main() {
scanf("%d %d", &n, &m);
for (i = 0; i < n; i++) {
scanf("%d %d", &w, &c);
a[i].w = w;
a[i].c = c;
a[i].rate = (double)w / c;
}
sort(a, a + n);
dp[0] = 1;
ans = 1;
for (i = 0; i < n; i++) {
up += a[i].w;
if (up > m) up = m;
down = max(a[i].w, up - 3);
for (j = up; j >= down; j--) {
dp[j] = max(dp[j], dp[j - a[i].w] + a[i].c);
ans = max(ans, dp[j]);
}
}
cout << ans - 1 << endl;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct node {
long long w, c;
};
bool comp(node i, node j) { return (i.c > j.c); }
long long dp[300020];
long long ans;
node arr[100005];
int main() {
long long i, j, n, m;
cin >> n >> m;
for (i = 0; i < n; i++) {
cin >> arr[i].w >> arr[i].c;
arr[i].c = ((arr[i].c * 6) / arr[i].w);
}
sort(arr, arr + n, comp);
long long tot = 0;
for (i = 0; i < n; i++) {
tot += arr[i].w;
tot = min(tot, m);
for (j = tot; tot - j < 4 && j >= arr[i].w; j--) {
dp[j] = max(dp[j], dp[j - arr[i].w] + ((arr[i].c * arr[i].w) / 6));
ans = max(ans, dp[j]);
}
}
cout << ans << endl;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int mxn = 1e5 + 5;
long long f[mxn], s[mxn], t[mxn];
int n, m, cf, cs, ct;
int nf, ns;
signed main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
int x, y;
cin >> x >> y;
if (x == 1)
f[++cf] = y;
else if (x == 2)
s[++cs] = y;
else
t[++ct] = y;
}
sort(f + 1, f + cf + 1);
reverse(f + 1, f + cf + 1);
sort(s + 1, s + cs + 1);
reverse(s + 1, s + cs + 1);
sort(t + 1, t + ct + 1);
reverse(t + 1, t + ct + 1);
for (int i = 1; i <= cf; ++i) f[i] += f[i - 1];
for (int i = 1; i <= cs; ++i) s[i] += s[i - 1];
for (int i = 1; i <= ct; ++i) t[i] += t[i - 1];
if (m >= cf + cs * 2 + ct * 3) {
cout << f[cf] + s[cs] + t[ct] << '\n';
return 0;
}
long long mx = 0, ans = 0;
for (int i = 0; i <= cs; ++i) {
int tmp = m - i * 2;
tmp = min(tmp, cf);
if (tmp < 0) break;
long long sm = s[i] + f[tmp];
if (sm > mx) {
mx = sm;
nf = tmp;
ns = i;
}
}
ans = mx;
for (int i = 1; i <= ct; ++i) {
mx = 0;
int tps = ns, tpf = nf;
for (int cg = -5; cg <= 5; ++cg) {
int ts = ns + cg, tf = m - i * 3 - ts * 2;
ts = min(ts, cs), tf = min(tf, cf);
if (ts < 0 or tf < 0) continue;
long long sm = t[i] + s[ts] + f[tf];
if (sm > mx) {
mx = sm;
tps = ts;
tpf = tf;
}
}
if (ans < mx) {
ans = mx;
ns = tps;
nf = tpf;
}
}
cout << ans << '\n';
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct node {
long long val;
int cnt1, cnt2;
} dp[300005];
long long v1[100005], v2[100005], v3[100005];
long long s[100005];
inline bool cmp(long long a, long long b) { return a > b; }
int cnt1 = 0, cnt2 = 0, cnt3 = 0;
int main() {
int n, m, i, k;
long long w;
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++) {
scanf("%d%lld", &k, &w);
if (k == 1) {
v1[++cnt1] = w;
}
if (k == 2) {
v2[++cnt2] = w;
}
if (k == 3) {
v3[++cnt3] = w;
}
}
sort(v1 + 1, v1 + cnt1 + 1, cmp);
sort(v2 + 1, v2 + cnt2 + 1, cmp);
sort(v3 + 1, v3 + cnt3 + 1, cmp);
for (i = 1; i <= cnt2; i++) {
s[i] = s[i - 1] + v2[i];
}
dp[0].val = 0;
dp[0].cnt1 = 0;
dp[0].cnt2 = 0;
dp[1].val = v1[1];
dp[1].cnt1 = 1;
dp[1].cnt2 = 0;
dp[2].val = v1[1] + v1[2];
dp[2].cnt1 = 2;
dp[2].cnt2 = 0;
for (i = 3; i <= m; i++) {
dp[i].val = dp[i - 1].val + v1[dp[i - 1].cnt1 + 1];
dp[i].cnt1 = dp[i - 1].cnt1 + 1;
dp[i].cnt2 = dp[i - 1].cnt2;
if (dp[i - 3].val + v3[dp[i - 3].cnt2 + 1] > dp[i].val) {
dp[i].val = dp[i - 3].val + v3[dp[i - 3].cnt2 + 1];
dp[i].cnt1 = dp[i - 3].cnt1;
dp[i].cnt2 = dp[i - 3].cnt2 + 1;
}
if (dp[i].cnt1 > cnt1) {
dp[i].cnt1 = cnt1;
}
if (dp[i].cnt2 > cnt3) {
dp[i].cnt2 = cnt3;
}
}
for (i = 1; i <= m; i++) {
if (dp[i - 1].val > dp[i].val) {
dp[i].val = dp[i - 1].val;
}
}
long long ans = dp[m].val;
for (i = 1; i <= cnt2 && 2 * i <= m; i++) {
long long tp = dp[m - 2 * i].val + s[i];
if (tp > ans) {
ans = tp;
}
}
printf("%lld", ans);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 7;
int n, m;
vector<long long> costs[4];
long long sum[4][MAX];
long long f(int a, int w) {
long long ans = sum[2][a];
w -= 2 * a;
w = min(w, (int)costs[1].size());
ans += sum[1][w];
return ans;
}
long long func(int w) {
int beg = 0;
int last = min(w / 2, (int)costs[2].size());
long long ans = 0;
while ((last - beg) > 3) {
int left = beg + (last - beg) / 3;
int right = last - (last - beg) / 3;
if (f(left, w) < f(right, w))
beg = left;
else
last = right;
}
for (int i = beg; i <= last; i++) ans = max(ans, f(i, w));
return ans;
}
long long func() {
for (int i = 1; i <= 3; i++) {
sort(costs[i].rbegin(), costs[i].rend());
for (int j = 1; j <= costs[i].size(); j++)
sum[i][j] = sum[i][j - 1] + costs[i][j - 1];
}
long long ans = 0;
for (int i = 0; i <= costs[3].size(); i++) {
int w = m - 3 * i;
if (w < 0) break;
ans = max(ans, sum[3][i] + func(w));
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int w, c;
cin >> w >> c;
costs[w].push_back(c);
}
cout << func() << endl;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
int n, m, vs[4][100007], vp[4];
long long s1[100007], s2[100007], ans = 0, now = 0;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
int x, y;
scanf("%d%d", &x, &y);
vs[x][++vp[x]] = y;
}
for (int i = 1; i <= 3; ++i)
std::sort(vs[i] + 1, vs[i] + vp[i] + 1, std::greater<int>());
for (int i = 1; i <= vp[1]; ++i) s1[i] = s1[i - 1] + vs[1][i];
for (int i = 1; i <= vp[2]; ++i) s2[i] = s2[i - 1] + vs[2][i];
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 - 2); x <= std::min(vp[2], M + 2); ++x) {
if (x * 2 > m) continue;
int y = std::min(vp[1], m - x * 2);
long long a1 = now + s1[y] + s2[x];
if (a1 > ans) ans = a1;
}
m -= 3;
now += vs[3][i + 1];
}
printf("%I64d\n", ans);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class C>
void mini(C& a4, C b4) {
a4 = min(a4, b4);
}
template <class C>
void maxi(C& a4, C b4) {
a4 = max(a4, b4);
}
template <class T1, class T2>
ostream& operator<<(ostream& out, pair<T1, T2> pair) {
return out << "(" << pair.first << ", " << pair.second << ")";
}
const int maxn = 300010;
int n, W;
int w[maxn], v[maxn];
vector<int> A, B, C;
int nA, nB, nC;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> W;
for (int i = (0); i < (n); i++) {
cin >> w[i] >> v[i];
if (w[i] == 1) A.push_back(v[i]);
if (w[i] == 2) B.push_back(v[i]);
if (w[i] == 3) C.push_back(v[i]);
}
sort(A.begin(), A.end());
reverse(A.begin(), A.end());
sort(B.begin(), B.end());
reverse(B.begin(), B.end());
sort(C.begin(), C.end());
reverse(C.begin(), C.end());
nA = A.size();
nB = B.size();
nC = C.size();
long long dp[maxn], suma[maxn];
pair<int, int> r[maxn];
memset(dp, 0LL, sizeof dp);
dp[0] = 0;
r[0] = pair<int, int>(0, 0);
for (int i = (1); i < (W + 1); i++) {
pair<int, int> x = r[i - 1];
pair<int, int> y;
if (i >= 2) y = r[i - 2];
if (x.first < nA) {
if (dp[i - 1] + A[x.first] > dp[i]) {
dp[i] = dp[i - 1] + A[x.first];
r[i] = pair<int, int>(x.first + 1, x.second);
}
}
if (i >= 2 and y.second < nB) {
if (dp[i - 2] + B[y.second] > dp[i]) {
dp[i] = dp[i - 2] + (int)B[y.second];
r[i] = pair<int, int>(y.first, y.second + 1);
}
}
if (dp[i - 1] > dp[i]) {
dp[i] = dp[i - 1];
r[i] = r[i - 1];
}
if (i >= 2 and dp[i - 2] > dp[i]) {
dp[i] = dp[i - 2];
r[i] = r[i - 2];
}
}
for (int i = (0); i < (nC); i++) {
suma[i] = C[i];
if (i > 0) suma[i] += suma[i - 1];
}
long long maxi = 0;
for (int i = (0); i < (W); i++) {
if (W - (i + 1) * 3 < 0) break;
maxi = max(maxi, suma[i] + dp[W - (i + 1) * 3]);
}
maxi = max(maxi, dp[W]);
cout << maxi << '\n';
return 0;
}
| 11 | CPP |
n,m = map(int,input().split())
a = [[],[],[]]
for _ in range(n):
x,y = map(int,input().split())
a[x-1].append(y)
for i in range(3):
a[i].sort(reverse=True)
a_one_odd = []
a_one_even = []
a_length = [len(a[i]) for i in range(3)]
for i in range(0,a_length[0]-1,2):
a_one_even.append(a[0][i]+a[0][i+1])
for i in range(1,a_length[0]-1,2):
a_one_odd.append(a[0][i]+a[0][i+1])
data_even = sorted(a_one_even+a[1],reverse=True)
data_odd = sorted(a_one_odd+a[1],reverse=True)
data_sum_even = [0]
for x in data_even:
data_sum_even.append(data_sum_even[-1]+x)
data_sum_odd = [0]
for x in data_odd:
data_sum_odd.append(data_sum_odd[-1]+x)
data_sum_three = [0]
for x in a[2]:
data_sum_three.append(data_sum_three[-1]+x)
ans = 0
#print(data_sum_odd,data_sum_even,data_sum_three)
for k in range(a_length[2]+1):
if m-3*k < 0:break
now1,now2 = data_sum_three[k],data_sum_three[k]
if (m-3*k)%2== 0:
now1 += data_sum_even[min((m-3*k)//2,len(data_sum_even)-1)]
if a_length[0] > 0 and m-3*k > 0:
now2 += a[0][0]
if (m-3*k)//2 >= 1:
now2 += data_sum_odd[min((m-3*k)//2-1,len(data_sum_odd)-1)]
else:
now1 += data_sum_even[min((m-3*k)//2,len(data_sum_even)-1)]
if a_length[0] > 0 and m-3*k > 0:
now2 += a[0][0]
now2 += data_sum_odd[min((m-3*k-1)//2,len(data_sum_odd)-1)]
ans = max(ans,now1,now2)
print(ans) | 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 111111;
long long dp[4][maxn];
int cnt[4];
long long go(int x, int y) { return dp[2][x] + dp[1][min(cnt[1], y - x * 2)]; }
long long get(int x) {
int l = 0, ans = min(x >> 1, cnt[2]), r = ans - 1;
while (l <= r) {
int mid = (l + r) >> 1;
if (go(mid, x) > go(mid + 1, x)) {
ans = mid;
r = mid - 1;
} else
l = mid + 1;
}
return go(ans, x);
}
bool cmp(long long x, long long y) { return x > y; }
int main() {
int n, m, a, b;
cin >> n >> m;
for (int(i) = 1; (i) <= (n); (i)++) {
cin >> a >> b;
dp[a][++cnt[a]] = b;
}
for (int(i) = 1; (i) <= (3); (i)++) {
sort(dp[i] + 1, dp[i] + 1 + cnt[i], cmp);
for (int(j) = 1; (j) <= (cnt[i]); (j)++) {
dp[i][j] += dp[i][j - 1];
}
}
long long ans = 0;
for (int i = 0; i <= cnt[3] && i * 3 <= m; i++) {
ans = max(ans, dp[3][i] + get(m - i * 3));
}
cout << ans << endl;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
namespace OI {
template <class T>
void rd(T &x) {
x = 0;
int f = 1;
char c;
while (!isdigit(c = getchar()))
if (c == '-') f = -1;
do {
x = (x << 3) + (x << 1) + (c ^ 48);
} while (isdigit(c = getchar()));
x *= f;
}
template <class T>
void pt(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) pt(x / 10);
putchar(x % 10 ^ 48);
}
} // namespace OI
using namespace OI;
using namespace std;
const int N = 3e5 + 5;
struct node {
int p1, p2;
long long v;
} dp[N];
int n, m;
vector<int> Vec[4];
long long ans, tmp;
int main() {
rd(n), rd(m);
for (int i = 1, x, y; i <= n; i++) rd(x), rd(y), Vec[x].push_back(y);
for (int i = 1; i <= 3; i++)
sort(Vec[i].begin(), Vec[i].end()), reverse(Vec[i].begin(), Vec[i].end());
for (int i = 1; i <= m; i++) {
dp[i] = dp[i - 1];
if (dp[i - 1].p1 < Vec[1].size() &&
dp[i - 1].v + Vec[1][dp[i - 1].p1] > dp[i].v) {
dp[i].v = dp[i - 1].v + Vec[1][dp[i - 1].p1];
dp[i].p1 = dp[i - 1].p1 + 1;
dp[i].p2 = dp[i - 1].p2;
}
if (i > 1 && dp[i - 2].p2 < Vec[2].size() &&
dp[i - 2].v + Vec[2][dp[i - 2].p2] > dp[i].v) {
dp[i].v = dp[i - 2].v + Vec[2][dp[i - 2].p2];
dp[i].p2 = dp[i - 2].p2 + 1;
dp[i].p1 = dp[i - 2].p1;
}
}
for (int i = 0; i <= (int)Vec[3].size() && i * 3 <= m; i++) {
ans = max(ans, tmp + dp[m - i * 3].v);
if (i < Vec[3].size()) tmp += Vec[3][i];
}
pt(ans);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long calc(vector<vector<long long> > v, int m, long long res) {
for (int i = 1; i <= 3; ++i) {
sort(v[i].begin(), v[i].end());
reverse(v[i].begin(), v[i].end());
if (i == 1) {
for (int i = 0; i + 1 < v[1].size(); i += 2)
v[2].push_back(v[1][i] + v[1][i + 1]);
if (v[1].size() % 2) {
v[1][0] = v[1].back();
v[1].resize(1);
} else
v[1].clear();
}
for (int j = 1; j < v[i].size(); ++j) v[i][j] += v[i][j - 1];
}
long long baseRes = res;
for (int a = 0; a * 2 <= m && a <= v[2].size(); ++a) {
int cur = m - a * 2;
long long curRes = a ? v[2][a - 1] : 0;
curRes += baseRes;
if (cur) {
int at = min(cur / 3, int(v[3].size()));
cur -= at * 3;
if (at) curRes += v[3][at - 1];
}
if (cur && !v[1].empty()) curRes += v[1][0];
res = max(res, curRes);
}
return res;
}
int main() {
int n, m;
vector<vector<long long> > w;
scanf("%d%d", &n, &m);
w.resize(4);
int x, y;
for (int i = 0; i < n; ++i) {
scanf("%d%d", &x, &y);
w[x].push_back(y);
}
sort(w[1].begin(), w[1].end());
long long res = 0;
if (!w[1].empty()) {
long long tmp = w[1].back();
w[1].pop_back();
res = calc(w, m - 1, tmp);
res = max(res, calc(w, m, 0));
w[1].push_back(tmp);
}
res = max(res, calc(w, m, 0));
printf("%lld\n", res);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
clock_t start;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
template <typename T1, typename T2, typename T3>
struct query {
T1 ss;
T2 x;
T3 y;
};
template <typename T1, typename T2, typename T3>
bool operator<(query<T1, T2, T3>& o1, query<T1, T2, T3>& o2) {
return o1.x < o2.x;
}
template <typename T1, typename T2, typename T3>
istream& operator>>(istream& in, query<T1, T2, T3>& q) {
in >> q.ss >> q.x >> q.y;
return in;
}
template <typename T1, typename T2, typename T3>
ostream& operator<<(ostream& out, query<T1, T2, T3>& q) {
out << q.ss << " " << q.x << " " << q.y;
return out;
}
void solve() {
long long n, m, ans = 0;
cin >> n >> m;
vector<long long> v[3];
for (long long i = 0; i < n; i++) {
long long w, c;
cin >> w >> c;
w--;
v[w].push_back(c);
}
v[0].resize(max(n, m + 1));
v[1].resize(max(n, m + 1));
v[2].resize(max(n, m + 1));
sort((v[0]).rbegin(), (v[0]).rend());
sort((v[1]).rbegin(), (v[1]).rend());
sort((v[2]).rbegin(), (v[2]).rend());
for (long long i = 1; i <= m; i++) {
v[2][i] += v[2][i - 1];
}
pair<pair<long long, long long>, pair<long long, long long> > dp[m + 1];
dp[0] = make_pair(make_pair(0, 0), make_pair(0, 0));
for (long long j = 1; j <= m; j++) {
long long x = v[0][dp[j - 1].first.second];
long long ff = dp[j - 1].first.first + x;
long long fs = dp[j - 1].first.second + 1;
long long sf = dp[j - 1].second.first;
if (j >= 2) {
x = v[1][dp[j - 2].second.first];
if (ff < dp[j - 2].first.first + x) {
ff = dp[j - 2].first.first + x;
fs = dp[j - 2].first.second;
sf = dp[j - 2].second.first + 1;
}
}
dp[j].first.first = ff;
dp[j].first.second = fs;
dp[j].second.first = sf;
if ((m - j) / 3 - 1 >= 0)
ans = max(ans, ff + v[2][(m - j) / 3 - 1]);
else
ans = max(ans, ff);
}
if (m / 3 > 0) ans = max(ans, v[2][m / 3 - 1]);
cout << ans << '\n';
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
start = clock();
cout << fixed << setprecision(20);
while (t--) {
solve();
}
double time_taken = double(clock() - start) / double(CLOCKS_PER_SEC);
}
| 11 | CPP |
def souvenier_calc(max_weight, weights, value):
const = len(weights)
ans = [-1061109567 for x in range(max_weight+1)]
node = [(x, y) for x, y in zip(weights, value)]
node = sorted(node, key=lambda x: x[1]/x[0], reverse=True)
weights = [x[0] for x in node]
value = [x[1] for x in node]
sum = 0
sol = 0
ans[0] = 0
for i in range(const):
sum += weights[i]
if sum > max_weight:
sum = max_weight
down = max(weights[i], sum-3)
for weight in range(sum, down-1, -1):
ans[weight] = max(ans[weight], ans[weight-weights[i]]+value[i])
sol = max(sol, ans[weight])
return sol
if __name__ == '__main__':
N, M = map(int, input().split())
assert(N <= 10**5 and M <= 3*(10**5)), 'Out of range'
weights = []
value = []
for i in range(N):
x, y = map(int, input().split())
assert(x >= 1 and x <= 3), 'Out of range'
assert(y >= 1 and y <= 10**9), 'Out of range'
weights.append(x)
value.append(y)
print(souvenier_calc(M, weights, value))
| 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
vector<long long> a[4];
struct node {
int i, j;
long long c;
node() : c(0){};
node(int i, int j, long long c) : i(i), j(j), c(c){};
bool operator<(const node& x) const { return c < x.c; }
} dp[3 * maxn];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
int w, c;
cin >> w >> c;
a[w].push_back(c);
}
int s[4];
for (int i = 1; i <= 3; i++) {
sort(a[i].begin(), a[i].end(), greater<long long>());
s[i] = a[i].size();
}
for (int i = 1; i < s[3]; i++) a[3][i] += a[3][i - 1];
long long ans = 0;
dp[0] = node(0, 0, 0);
for (int w = 0; w <= m; w++) {
if (w) dp[w] = max(dp[w], dp[w - 1]);
if (w + 1 <= m && dp[w].i < s[1])
dp[w + 1] =
max(dp[w + 1], node(dp[w].i + 1, dp[w].j, dp[w].c + a[1][dp[w].i]));
if (w + 2 <= m && dp[w].j < s[2])
dp[w + 2] =
max(dp[w + 2], node(dp[w].i, dp[w].j + 1, dp[w].c + a[2][dp[w].j]));
int k = min(s[3], (m - w) / 3);
long long cur = dp[w].c + (k ? a[3][k - 1] : 0);
ans = max(ans, cur);
}
cout << ans << endl;
return 0;
}
| 11 | CPP |
import sys
from itertools import accumulate
def solve():
n, m = map(int, input().split())
w = [[] for i in range(3)]
for i in range(n):
wi, ci = map(int, sys.stdin.readline().split())
wi -= 1
w[wi].append(ci)
for i in range(3):
w[i].sort(reverse=True)
dp = [0]*(m + 1)
used = [[0]*2 for i in range(m + 1)]
s0 = len(w[0])
s1 = len(w[1])
if s0 > 0:
dp[1] = w[0][0]
used[1] = [1, 0]
for i in range(2, m + 1):
if used[i - 1][0] < s0:
dp[i] = dp[i - 1] + w[0][used[i - 1][0]]
used[i] = used[i - 1][:]
used[i][0] += 1
else:
dp[i] = dp[i - 1]
used[i] = used[i - 1][:]
if used[i - 2][1] < s1 and dp[i] < dp[i - 2] + w[1][used[i - 2][1]]:
dp[i] = dp[i - 2] + w[1][used[i - 2][1]]
used[i] = used[i - 2][:]
used[i][1] += 1
pf = [0] + list(accumulate(w[2]))
ans = max(pf[k] + dp[m - 3*k] for k in range(min(len(pf),m // 3 + 1)))
print(ans)
if __name__ == '__main__':
solve() | 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 10;
int a[MAXN], b[MAXN], c[MAXN], an = 0, bn = 0, cn = 0;
long long sa[MAXN], sb[MAXN], sc[MAXN], dp[MAXN];
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++) {
int w, x;
scanf("%d%d", &w, &x);
if (w == 1)
a[++an] = x;
else if (w == 2)
b[++bn] = x;
else
c[++cn] = x;
}
sort(a + 1, a + 1 + an, cmp);
for (int i = 1; i <= m; i++) sa[i] = a[i] + sa[i - 1];
sort(b + 1, b + 1 + bn, cmp);
for (int i = 1; 2 * i <= m; i++) sb[i] = b[i] + sb[i - 1];
sort(c + 1, c + 1 + cn, cmp);
for (int i = 1; 3 * i <= m; i++) sc[i] = c[i] + sc[i - 1];
dp[1] = a[1];
for (int k = 2; k <= m; k++) {
if (b[1] <= a[k] + a[k - 1]) {
dp[k] = sa[k];
continue;
}
int l = 1, r = k / 2 + 1;
while (l < r) {
int mid = (l + r) >> 1;
if (b[mid] > a[k - 2 * mid + 1] + a[k - 2 * mid + 2])
l = mid + 1;
else
r = mid;
}
if (l == k / 2 + 1)
dp[k] = sb[k / 2] + sa[k - (k / 2) * 2];
else
dp[k] = max(sb[l] + sa[k - 2 * l], sb[l - 1] + sa[k - 2 * (l - 1)]);
}
long long ans = 0;
for (int i = 0; i <= cn && 3 * i <= m; i++)
ans = max(ans, dp[m - 3 * i] + sc[i]);
cout << ans << endl;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long int dp[1000005];
long long int p1[1000005];
long long int p2[1000005];
vector<long long int> v[5];
int main() {
long long int n, m;
scanf("%lld%lld", &n, &m);
for (long long int i = 1; i <= n; i++) {
long long int w, c;
scanf("%lld%lld", &w, &c);
v[w].push_back(c);
}
v[1].resize(m + 2);
v[2].resize(m + 2);
v[3].resize(m + 2);
sort(v[1].begin(), v[1].end(), greater<long long int>());
sort(v[2].begin(), v[2].end(), greater<long long int>());
sort(v[3].begin(), v[3].end(), greater<long long int>());
dp[1] = v[1][0];
p1[1] = 1;
p2[1] = 0;
long long int ans = dp[1];
for (long long int i = 2; i <= m; i++) {
dp[i] = dp[i - 2] + v[1][p1[i - 2]] + v[1][p1[i - 2] + 1];
p1[i] = p1[i - 2] + 2;
p2[i] = p2[i - 2];
if (dp[i] < dp[i - 2] + v[2][p2[i - 2]]) {
dp[i] = dp[i - 2] + v[2][p2[i - 2]];
p2[i] = p2[i - 2] + 1;
p1[i] = p1[i - 2];
}
ans = max(ans, dp[i]);
}
long long int ptr = 0;
long long int sum = 0;
for (long long int i = 3; i <= m; i += 3) {
ans = max(ans, dp[m - i] + sum + v[3][ptr]);
sum += v[3][ptr];
ptr++;
}
cout << ans;
return 0;
}
| 11 | CPP |
n, m = list(map(int, input().split()))
a = [[], [], []]
for i in range(n):
w, c = list(map(int, input().split()))
a[w-1].append(c)
p = [[0], [0], [0]]
for i in range(3):
a[i].sort(reverse=True)
for x in a[i]:
p[i].append(p[i][-1] + x)
ans = 0
for i in range(min(m//3, len(a[2])) + 1):
w = m - i * 3
if len(a[1]) * 2 + len(a[0]) <= w:
ans = max(ans, p[2][i] + p[1][len(a[1])] + p[0][len(a[0])])
continue
if not len(a[0]):
ans = max(ans, p[2][i] + p[1][min(w//2, len(a[1]))])
continue
if 2 + 2 == 4:
x = min(len(a[0]), w)
y = (w - x) // 2
ans = max(ans, p[2][i] + p[1][y] + p[0][x])
lo = max((w - len(a[0]) + 1) // 2 + 1, 1)
hi = min(len(a[1]), w // 2)
while lo <= hi:
mi = (lo + hi) // 2
if a[1][mi - 1] - (p[0][w-mi*2+2] - p[0][w-mi*2]) > 0:
lo = mi + 1
else:
hi = mi - 1
ans = max(ans, p[2][i] + p[1][hi] + p[0][w-hi*2])
print(ans)
| 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 300003;
int n, m;
long long bestOne[N];
long long bestTwo[N];
long long bestThree[N];
pair<long long, pair<int, int> > bestOneTwo[N];
int main() {
int x, y;
scanf("%d%d", &n, &m);
vector<pair<int, int> > vec;
vector<int> currOne, currTwo, currThree, leftOne, leftTwo, leftThree;
for (int i = 0; i < n; i++) {
scanf("%d%d", &x, &y);
if (x == 1) {
currOne.push_back(y);
} else if (x == 2) {
currTwo.push_back(y);
} else if (x == 3) {
currThree.push_back(y);
}
}
sort(currOne.rbegin(), currOne.rend());
for (int i = 1; i <= currOne.size(); i++)
bestOne[i] = bestOne[i - 1] + currOne[i - 1];
sort(currTwo.rbegin(), currTwo.rend());
for (int i = 1; i <= currTwo.size(); i++)
bestTwo[i] = bestTwo[i - 1] + currTwo[i - 1];
sort(currThree.rbegin(), currThree.rend());
for (int i = 1; i <= currThree.size(); i++)
bestThree[i] = bestThree[i - 1] + currThree[i - 1];
for (int i = currOne.size() + 1; i < N; i++) bestOne[i] = bestOne[i - 1];
for (int i = currTwo.size() + 1; i < N; i++) bestTwo[i] = bestTwo[i - 1];
for (int i = currThree.size() + 1; i < N; i++)
bestThree[i] = bestThree[i - 1];
bestOneTwo[0] = make_pair(0, make_pair(0, 0));
bestOneTwo[1] = make_pair(bestOne[1], make_pair(1, 0));
for (int i = 2; i <= m; i++) {
pair<long long, pair<int, int> > last = bestOneTwo[i - 1];
pair<long long, pair<int, int> > bl = bestOneTwo[i - 2];
bestOneTwo[i] =
make_pair(bestOne[last.second.first + 1] + bestTwo[last.second.second],
make_pair(last.second.first + 1, last.second.second));
if (bestOne[bl.second.first + 2] + bestTwo[bl.second.second] >
bestOneTwo[i].first) {
bestOneTwo[i] =
make_pair(bestOne[bl.second.first + 2] + bestTwo[bl.second.second],
make_pair(bl.second.first + 2, bl.second.second));
}
if (bestOne[bl.second.first] + bestTwo[bl.second.second + 1] >
bestOneTwo[i].first) {
bestOneTwo[i] =
make_pair(bestOne[bl.second.first] + bestTwo[bl.second.second + 1],
make_pair(bl.second.first, bl.second.second + 1));
}
}
long long ans = 0;
for (int i = 0; i * 3 <= m; i++) {
int left = m - i * 3;
long long curr = bestThree[i];
ans = max(ans, curr + bestOneTwo[left].first);
}
printf("%I64d", ans);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct NODE {
long long val, x, y;
} dp[100010 * 3];
int cnt[4];
long long pre_sum[4][100010], a[4][100010], ans;
inline int read() {
int f = 1, x = 0;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return f * x;
}
inline bool cmp(int x, int y) { return x > y; }
int main() {
int n = read(), load = read();
for (int i = 1; i <= n; i++) {
int w = read(), v = read();
a[w][++cnt[w]] = v;
}
for (int i = 1; i <= 3; i++) {
sort(a[i] + 1, a[i] + cnt[i] + 1, cmp);
for (int j = 1; j <= cnt[i]; j++)
pre_sum[i][j] = pre_sum[i][j - 1] + a[i][j];
}
dp[0].val = dp[0].x = dp[0].y = 0;
for (int i = 1; i <= load; i++) {
dp[i] = dp[i - 1];
if (dp[i - 1].val + a[1][dp[i - 1].x + 1] > dp[i].val)
dp[i].val = dp[i - 1].val + a[1][dp[i - 1].x + 1],
dp[i].x = dp[i - 1].x + 1, dp[i].y = dp[i - 1].y;
if (i >= 2 && dp[i - 2].val + a[2][dp[i - 2].y + 1] > dp[i].val)
dp[i].val = dp[i - 2].val + a[2][dp[i - 2].y + 1], dp[i].x = dp[i - 2].x,
dp[i].y = dp[i - 2].y + 1;
}
ans = 0;
for (int i = 0; i <= cnt[3]; i++)
if (load >= i * 3) ans = max(ans, dp[load - i * 3].val + pre_sum[3][i]);
printf("%lld\n", ans);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T sqr(T x) {
return x * x;
}
template <class T>
T gcd(T a, T b) {
return (b != 0 ? gcd<T>(b, a % b) : a);
}
template <class T>
T lcm(T a, T b) {
return (a / gcd<T>(a, b) * b);
}
template <class T>
inline T bigmod(T p, T e, T M) {
if (e == 0) return 1;
if (e % 2 == 0) {
long long int t = bigmod(p, e / 2, M);
return (T)((t * t) % M);
}
return (T)((long long int)bigmod(p, e - 1, M) * (long long int)p) % M;
}
template <class T>
inline T bigexp(T p, T e) {
if (e == 0) return 1;
if (e % 2 == 0) {
long long int t = bigexp(p, e / 2);
return (T)((t * t));
}
return (T)((long long int)bigexp(p, e - 1) * (long long int)p);
}
template <class T>
inline T modinverse(T a, T M) {
return bigmod(a, M - 2, M);
}
int dx4[] = {1, 0, -1, 0};
int dy4[] = {0, 1, 0, -1};
int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1};
int nx8[] = {1, 1, -1, -1, 2, 2, -2, -2};
int ny8[] = {2, -2, 2, -2, 1, -1, 1, -1};
int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
struct T {
int w;
long long int cost;
bool operator<(const T& p) const {
if (cost == p.cost) return w > p.w;
return cost > p.cost;
}
} a[100005], b[100005], c[100005];
int n, m, x, c1, c2, c3;
long long int xx, a1[100005][2], b1[100005][2], cc1[100005][2];
long long int ternary3(int rem) {
long long int ret = 0, tmp1, tmp2;
int mid1, mid2;
int lo = 1, hi = c1;
while (hi - lo >= 5) {
mid1 = (2 * lo + hi) / 3;
mid2 = (lo + 2 * hi) / 3;
tmp1 = 0;
tmp2 = 0;
if (a1[mid1][0] <= rem) {
tmp1 = a1[mid1][1];
}
if (a1[mid2][0] <= rem) {
tmp2 = a1[mid2][1];
}
if (tmp1 >= tmp2) {
hi = mid2;
ret = max(tmp1, ret);
} else {
ret = max(tmp2, ret);
lo = mid1;
}
}
for (int i = lo; i <= hi; i++) {
if (a1[i][0] <= rem) {
tmp1 = a1[i][1];
ret = max(ret, tmp1);
}
}
return ret;
}
long long int ternary2(int rem) {
long long int ret = 0, tmp1, tmp2;
int mid1, mid2;
int lo = 1, hi = c2;
while (hi - lo >= 5) {
mid1 = (2 * lo + hi) / 3;
mid2 = (lo + 2 * hi) / 3;
tmp1 = 0;
tmp2 = 0;
if (b1[mid1][0] <= rem) {
tmp1 = b1[mid1][1] + ternary3(rem - b1[mid1][0]);
}
if (b1[mid2][0] <= rem) {
tmp2 = b1[mid2][1] + ternary3(rem - b1[mid2][0]);
}
if (tmp1 >= tmp2) {
hi = mid2;
ret = max(tmp1, ret);
} else {
ret = max(tmp2, ret);
lo = mid1;
}
}
for (int i = lo; i <= hi; i++) {
if (b1[i][0] <= rem) {
tmp1 = b1[i][1] + ternary3(rem - b1[i][0]);
ret = max(ret, tmp1);
}
}
ret = max(ret, ternary3(rem));
return ret;
}
long long int ternary1(int rem) {
long long int ret = 0, tmp1, tmp2;
int cnt = 300, mid1, mid2;
int lo = 1, hi = c3;
while (hi - lo >= 5) {
mid1 = (2 * lo + hi) / 3;
mid2 = (lo + 2 * hi) / 3;
tmp1 = 0;
tmp2 = 0;
if (cc1[mid1][0] <= rem) {
tmp1 = cc1[mid1][1] + ternary2(rem - cc1[mid1][0]);
}
if (cc1[mid2][0] <= rem) {
tmp2 = cc1[mid2][1] + ternary2(rem - cc1[mid2][0]);
}
if (tmp1 >= tmp2) {
hi = mid2;
ret = max(tmp1, ret);
} else {
ret = max(tmp2, ret);
lo = mid1;
}
}
for (int i = lo; i <= hi; i++) {
if (cc1[i][0] <= rem) {
tmp1 = cc1[i][1] + ternary2(rem - cc1[i][0]);
ret = max(tmp1, ret);
}
}
ret = max(ret, ternary2(rem));
return ret;
}
int main() {
scanf("%d %d", &n, &m);
for (__typeof(n) i = (1); i <= (n); i++) {
scanf("%d %lld", &x, &xx);
if (x == 1) {
a[++c1].w = 1;
a[c1].cost = xx;
} else if (x == 2) {
b[++c2].w = 2;
b[c2].cost = xx;
} else {
c[++c3].w = 3;
c[c3].cost = xx;
}
}
sort(a + 1, a + c1 + 1);
sort(b + 1, b + c2 + 1);
sort(c + 1, c + c3 + 1);
for (__typeof(c1) i = (1); i <= (c1); i++) {
a1[i][0] = a1[i - 1][0] + 1;
a1[i][1] = a1[i - 1][1] + a[i].cost;
}
for (__typeof(c2) i = (1); i <= (c2); i++) {
b1[i][0] = b1[i - 1][0] + 2;
b1[i][1] = b1[i - 1][1] + b[i].cost;
}
for (__typeof(c3) i = (1); i <= (c3); i++) {
cc1[i][0] = cc1[i - 1][0] + 3;
cc1[i][1] = cc1[i - 1][1] + c[i].cost;
}
long long int res = ternary1(m);
cout << res;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long cost[4][300100];
vector<long long> val[4];
long long cal(long long x) {
long long sz = val[2].size();
long long l = 1, r = min(x / 2, sz), best = 0;
while (l <= r) {
long long mid = (l + r) / 2;
if (cost[2][mid * 2] + cost[1][x - mid * 2] >=
cost[2][(mid - 1) * 2] + cost[1][x - (mid - 1) * 2]) {
best = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return cost[2][best * 2] + cost[1][x - best * 2];
}
int main() {
int t = 1;
while (t--) {
long long n, m;
cin >> n >> m;
for (long long i = 1; i <= n; i++) {
long long x, y;
cin >> x >> y;
val[x].push_back(y);
}
for (long long i = 1; i <= 3; i++) {
sort(val[i].begin(), val[i].end());
reverse(val[i].begin(), val[i].end());
long long now = 0;
for (long long j = 0; j < val[i].size(); j++) {
now += val[i][j];
cost[i][(j + 1) * i] = now;
}
for (long long j = 1; j <= m; j++) {
cost[i][j] = max(cost[i][j], cost[i][j - 1]);
}
}
long long ans = 0;
for (long long i = 0; i <= m; i++) {
ans = max(ans, cost[3][i] + cal(m - i));
}
cout << ans << endl;
memset(cost, 0, sizeof(cost));
for (long long i = 0; i <= 3; i++) val[i].clear();
}
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 300010;
struct {
ll v;
ll c1, c2;
} dp[N];
int num[4], n, m;
ll a[4][N], s[4][N];
int main() {
scanf("%d%d", &n, &m);
memset(num, 0, sizeof num);
memset(dp, 0, sizeof dp);
for (int i = 1; i <= n; ++i) {
ll w, c;
scanf("%I64d%I64d", &w, &c);
a[w][++num[w]] = c;
}
for (int i = 1; i <= 3; ++i) {
s[i][0] = 0;
sort(a[i] + 1, a[i] + num[i] + 1,
[](const int &a, const int &b) { return a > b; });
for (int j = 1; j <= num[i]; ++j) s[i][j] = s[i][j - 1] + a[i][j];
}
dp[0].v = dp[0].c1 = dp[0].c2 = 0;
for (int i = 1; i <= m; ++i) {
if (dp[i - 1].v + a[1][dp[i - 1].c1 + 1] > dp[i].v) {
dp[i].v = dp[i - 1].v + a[1][dp[i - 1].c1 + 1];
dp[i].c1 = dp[i - 1].c1 + 1;
dp[i].c2 = dp[i - 1].c2;
}
if (i > 1 && dp[i - 2].v + a[2][dp[i - 2].c2 + 1] > dp[i].v) {
dp[i].v = dp[i - 2].v + a[2][dp[i - 2].c2 + 1];
dp[i].c1 = dp[i - 2].c1;
dp[i].c2 = dp[i - 2].c2 + 1;
}
}
ll ans = 0;
for (int i = 0; i <= num[3]; ++i) {
if (m >= i * 3) ans = max(ans, s[3][i] + dp[m - i * 3].v);
}
printf("%I64d\n", ans);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
struct data {
ll cost;
int cnt1, cnt2, cnt3;
} d[300010];
ll m, c1[100010], c2[100010], c3[100010], t1 = 0, t2 = 0, t3 = 0;
bool cmp(ll a, ll b) { return a > b; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
int a;
ll b;
cin >> a >> b;
if (a == 1) {
++t1;
c1[t1] = b;
}
if (a == 2) {
++t2;
c2[t2] = b;
}
if (a == 3) {
++t3;
c3[t3] = b;
}
}
d[0] = {0, 0, 0};
sort(c1 + 1, c1 + 1 + t1, cmp);
sort(c2 + 1, c2 + 1 + t2, cmp);
sort(c3 + 1, c3 + 1 + t3, cmp);
for (int i = 1; i <= m; ++i) {
if (i >= 1) {
if (d[i - 1].cnt1 < t1 &&
d[i - 1].cost + c1[d[i - 1].cnt1 + 1] > d[i].cost) {
d[i].cost = d[i - 1].cost + c1[d[i - 1].cnt1 + 1];
d[i].cnt1 = d[i - 1].cnt1 + 1;
d[i].cnt2 = d[i - 1].cnt2;
d[i].cnt3 = d[i - 1].cnt3;
}
}
if (i >= 2) {
if (d[i - 2].cnt2 < t2 &&
d[i - 2].cost + c2[d[i - 2].cnt2 + 1] > d[i].cost) {
d[i].cost = d[i - 2].cost + c2[d[i - 2].cnt2 + 1];
d[i].cnt1 = d[i - 2].cnt1;
d[i].cnt2 = d[i - 2].cnt2 + 1;
d[i].cnt3 = d[i - 2].cnt3;
}
if (d[i - 2].cnt1 > 0 && d[i - 2].cnt3 < t3) {
if (d[i - 2].cost - c1[d[i - 2].cnt1] + c3[d[i - 2].cnt3 + 1] >
d[i].cost) {
d[i].cost = d[i - 2].cost - c1[d[i - 2].cnt1] + c3[d[i - 2].cnt3 + 1];
d[i].cnt1 = d[i - 2].cnt1 - 1;
d[i].cnt2 = d[i - 2].cnt2;
d[i].cnt3 = d[i - 2].cnt3 + 1;
}
}
}
if (i >= 3) {
if (d[i - 3].cnt3 < t3 &&
d[i - 3].cost + c3[d[i - 3].cnt3 + 1] > d[i].cost) {
d[i].cost = d[i - 3].cost + c3[d[i - 3].cnt3 + 1];
d[i].cnt1 = d[i - 3].cnt1;
d[i].cnt2 = d[i - 3].cnt2;
d[i].cnt3 = d[i - 3].cnt3 + 1;
}
}
}
ll ans = 0;
for (int i = 1; i <= m; ++i) ans = max(ans, d[i].cost);
cout << ans;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, M, m, k;
long long now, ans, f_min[25], f_max[25];
struct ff {
long long w, p;
bool operator<(const ff b) const {
return ((long long)b.p * w < (long long)p * b.w) ||
((long long)b.p * w == (long long)p * b.w && w < b.w);
}
} a[100005];
int read() {
int ret = 0;
bool f = 0;
char ch = getchar();
while (ch < '0' || ch > '9') f |= (ch == '-'), ch = getchar();
while (ch >= '0' && ch <= '9') ret = ret * 10 + ch - '0', ch = getchar();
return f ? -ret : ret;
}
int main() {
n = read(), M = m = read();
for (int i = 1; i <= n; i++) a[i].w = read(), a[i].p = read();
sort(a + 1, a + 1 + n);
for (k = 1; k <= n && m; k++)
if (m >= a[k].w)
m -= a[k].w, ans += a[k].p;
else
break;
k--;
if (k == n) {
printf("%I64d\n", ans);
return 0;
}
memset(f_min, 63, sizeof f_min), f_min[0] = 0;
for (int i = 1; i <= k; i++)
for (int j = 10; j >= a[i].w; j--)
if (f_min[j - a[i].w] + a[i].p < f_min[j])
f_min[j] = f_min[j - a[i].w] + a[i].p;
for (int i = k + 1; i <= n; i++)
for (int j = 20; j >= a[i].w; j--)
if (f_max[j - a[i].w] + a[i].p > f_max[j])
f_max[j] = f_max[j - a[i].w] + a[i].p;
now = ans;
for (int i = 0; i <= 10; i++)
if (now - f_min[i] + f_max[m + i] > ans)
ans = now - f_min[i] + f_max[m + i];
printf("%I64d\n", ans);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long int n, m;
vector<long long int> a[4];
vector<double> b[4];
pair<long long int, pair<int, int> > a1[300010];
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
long long int k, l;
cin >> k >> l;
a[k].push_back(l);
}
for (int i = 1; i < 4; i++) {
sort(a[i].begin(), a[i].end(), greater<long long int>());
}
a1[0].first = 0;
a1[0].second.first = 0;
a1[0].second.second = 0;
a1[1].first = 0;
a1[1].second.first = 0;
a1[1].second.second = 0;
if (a[1].size() > 0) {
a1[1].first = a[1][0];
a1[1].second.first = 1;
}
long long int ans = 0;
for (int i = 2; i <= m; i++) {
if ((a[1].size() > a1[i - 1].second.first) and
(a[2].size() > a1[i - 2].second.second)) {
if (a1[i - 1].first + a[1][a1[i - 1].second.first] >=
a1[i - 2].first + a[2][a1[i - 2].second.second]) {
a1[i].first = a1[i - 1].first + a[1][a1[i - 1].second.first];
a1[i].second.first = a1[i - 1].second.first + 1;
a1[i].second.second = a1[i - 1].second.second;
} else {
a1[i].first = a1[i - 2].first + a[2][a1[i - 2].second.second];
a1[i].second.first = a1[i - 2].second.first;
a1[i].second.second = a1[i - 2].second.second + 1;
}
} else if (a[1].size() > a1[i - 1].second.first) {
if (a1[i - 1].first + a[1][a1[i - 1].second.first] >= a1[i - 2].first) {
a1[i].first = a1[i - 1].first + a[1][a1[i - 1].second.first];
a1[i].second.first = a1[i - 1].second.first + 1;
a1[i].second.second = a1[i - 1].second.second;
} else {
a1[i].first = a1[i - 2].first;
a1[i].second.first = a1[i - 2].second.first;
a1[i].second.second = a1[i - 2].second.second;
}
} else if (a[2].size() > a1[i - 2].second.second) {
if (a1[i - 1].first >= a1[i - 2].first + a[2][a1[i - 2].second.second]) {
a1[i].first = a1[i - 1].first;
a1[i].second.first = a1[i - 1].second.first;
a1[i].second.second = a1[i - 1].second.second;
} else {
a1[i].first = a1[i - 2].first + a[2][a1[i - 2].second.second];
a1[i].second.first = a1[i - 2].second.first;
a1[i].second.second = a1[i - 2].second.second + 1;
}
} else {
if (a1[i - 1].first >= a1[i - 2].first) {
a1[i].first = a1[i - 1].first;
a1[i].second.first = a1[i - 1].second.first;
a1[i].second.second = a1[i - 1].second.second;
} else {
a1[i].first = a1[i - 2].first;
a1[i].second.first = a1[i - 2].second.first;
a1[i].second.second = a1[i - 2].second.second;
}
}
}
long long int k = 0;
for (int i = 0; i <= m; i += 3) {
if (a1[m - i].first + k > ans) ans = a1[m - i].first + k;
if (i / 3 < a[3].size()) k += a[3][i / 3];
}
cout << ans;
}
| 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 - 3);
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;
long n, m;
vector<long long> w[3];
long long f(long mid, long tot) {
if (2 * mid > tot) return -(1 >> 30);
if (mid < w[1].size() && (tot - 2 * mid) < w[0].size())
return w[1][mid] + w[0][tot - 2 * mid];
else if (mid < w[1].size())
return w[1][mid] + w[0][w[0].size() - 1];
else if ((tot - 2 * mid) < w[0].size())
return w[1][w[1].size() - 1] + w[0][tot - 2 * mid];
else
return w[1][w[1].size() - 1] + w[0][w[0].size() - 1];
}
long long ternary(long y) {
long l = 0, r = y / 2;
while (r - l >= 3) {
long m1 = l + (r - l) / 3;
long m2 = r - (r - l) / 3;
long long f1 = f(m1, y);
long long f2 = f(m2, y);
if (f1 < f2)
l = m1;
else
r = m2;
}
long long ans = 0;
for (long i = l; i <= r; i++) {
ans = max(ans, f(i, y));
}
return ans;
}
int main() {
cin >> n >> m;
long i, j, we, c;
for (i = 0; i < n; i++) {
cin >> we >> c;
w[we - 1].push_back(c);
}
for (i = 0; i < 3; i++) {
sort(w[i].begin(), w[i].end(), greater<long>());
}
for (i = 0; i < 3; i++) w[i].insert(w[i].begin(), 0);
for (i = 0; i < 3; i++) {
for (j = 1; j < w[i].size(); j++) {
w[i][j] += w[i][j - 1];
}
}
long long ans = 0;
for (i = 0; i < w[2].size() && i * 3 <= m; i++) {
ans = max(ans, w[2][i] + ternary(m - 3 * i));
}
cout << ans << endl;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m;
long long w[200009], c[200009], pre[10][200009];
vector<long long> v[10];
inline bool check(int mid, int curr1, int curr2) {
if (v[1][curr1 - 2 * (mid - 1) - 1] + v[1][curr1 - 2 * (mid - 1) - 2] <=
v[2][curr2 + (mid - 1)]) {
return true;
}
return false;
}
inline long long calc_max(int i) {
long long ans1 = 0, ans2 = 0;
int req = m - (i * 3), curr1 = 0, curr2 = 0;
if (v[1].size() == 0) {
curr1 = 0;
} else if (v[1].size() <= req) {
curr1 = v[1].size();
ans1 = pre[1][curr1 - 1];
} else {
curr1 = req;
ans1 = pre[1][curr1 - 1];
}
req = req - curr1;
if (v[2].size() == 0) {
curr2 = 0;
} else if (2 * (int)v[2].size() <= req) {
curr2 = v[2].size();
ans2 = pre[2][curr2 - 1];
} else {
curr2 = req / 2;
ans2 = pre[2][curr2 - 1];
}
req = req - (curr2 * 2);
if (curr1 == v[1].size() && curr2 == v[2].size()) {
return ans1 + ans2;
}
if (curr1 == 0 && curr2 == 0) {
return 0;
}
if (curr1 == 0 || curr2 == v[2].size()) {
return ans1 + ans2;
}
if (req) {
if (v[1][curr1 - 1] <= v[2][curr2]) {
ans1 -= v[1][curr1 - 1];
ans2 += v[2][curr2];
curr2++;
curr1--;
req--;
}
}
if (curr1 < 2 || curr2 == v[2].size()) {
return ans1 + ans2;
}
if (v[1][curr1 - 1] + v[1][curr1 - 2] > v[2][curr2]) {
return ans1 + ans2;
}
int low = 1, high = min((curr1 / 2), (int)v[2].size() - curr2);
while (low < high) {
int mid = low + ((high - low + 1) / 2);
if (check(mid, curr1, curr2)) {
low = mid;
} else {
high = mid - 1;
}
}
if (check(low, curr1, curr2)) {
} else {
}
ans1 = ans1 - (pre[1][curr1 - 1] - pre[1][curr1 - 2 * low - 1]);
ans2 = ans2 + (pre[2][curr2 + (low - 1)] - pre[2][curr2 - 1]);
return ans1 + ans2;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> 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(), greater<long long>());
if (v[i].size() >= 1) {
pre[i][0] = v[i][0];
}
for (int j = 1; j < v[i].size(); j++) {
pre[i][j] = pre[i][j - 1] + v[i][j];
}
}
long long ans = 0, s = 0;
for (int i = 0; i <= v[3].size(); i++) {
if (m - (i * 3) >= 0) {
if (i) {
s = s + v[3][i - 1];
}
ans = max(ans, s + calc_max(i));
}
}
cout << ans << "\n";
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 3e5 + 7;
struct node {
int w, c;
double v;
node() {}
node(int w, int c) : w(w), c(c) { v = (double)c / w; }
bool operator<(const node &rhs) const { return v > rhs.v; }
} a[N];
int n, m;
long long dp[N];
void Init() {
for (int i = (1); i < (n + 1); ++i) {
int w, c;
scanf("%d%d", &w, &c);
a[i] = node(w, c);
}
sort(a + 1, a + n + 1);
}
int Solve() {
int V = 0;
long long ans = 0;
for (int i = (1); i < (n + 1); ++i) {
V = min(V + a[i].w, m);
for (int j = (V + 1) - 1; j >= (max(V - 3, a[i].w)); --j) {
dp[j] = max(dp[j], dp[j - a[i].w] + a[i].c);
ans = max(ans, dp[j]);
}
}
return printf("%I64d\n", ans);
}
int main() {
while (~scanf("%d%d", &n, &m)) {
Init();
Solve();
}
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct nr {
int w, val;
} v[100005];
int cmp(nr a, nr b) { return 1LL * a.val * b.w > 1ll * b.val * a.w; }
long long dp[300005];
int main() {
int n, m, curw = 0, i, j;
long long rasp = 0;
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++) scanf("%d%d", &v[i].w, &v[i].val);
sort(v + 1, v + n + 1, cmp);
for (i = 1; i <= n; i++) {
curw = min(curw + v[i].w, m);
for (j = min(m, curw); j >= max(v[i].w, curw - 3); j--) {
dp[j] = max(dp[j], dp[j - v[i].w] + v[i].val);
rasp = max(rasp, dp[j]);
}
}
printf("%lld\n", rasp);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 10;
long long a[MAXN], b[MAXN], c[MAXN];
long long va[MAXN], vb[MAXN], vc[MAXN];
int n, m, indxa = 1, indxb = 1, indxc = 1;
bool cmp(long long a, long long b) { return a > b; }
void get_v(void) {
for (int i = 1; i <= m; i++) {
va[i] = va[i - 1] + a[i];
}
for (int i = 1; i * 2 <= m; i++) {
vb[i << 1] = vb[(i - 1) << 1] + b[i];
vb[(i << 1) - 1] = vb[(i - 1) << 1];
}
for (int i = 1; i * 3 <= m; i++) {
vc[i * 3] = vc[(i - 1) * 3] + c[i];
vc[i * 3 - 1] = vc[(i - 1) * 3];
vc[i * 3 - 2] = vc[(i - 1) * 3];
}
}
long long f(int x, int w) { return vb[x * 2] + va[w - x * 2]; }
long long find(int w) {
if (w <= 0) return 0;
int l = 0, r = w / 2;
while (l < r - 1) {
int lmid = (l + r) >> 1;
int rmid = (lmid + r) >> 1;
if (f(lmid, w) > f(rmid, w))
r = rmid;
else
l = lmid;
}
return max(max(max(f(l, w), f(r, w)), f(0, w)), f(w / 2, w));
}
int main(void) {
long long ans = 0;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
if (x == 1)
a[indxa++] = y;
else if (x == 2)
b[indxb++] = y;
else
c[indxc++] = y;
}
sort(a + 1, a + indxa, cmp);
sort(b + 1, b + indxb, cmp);
sort(c + 1, c + indxc, cmp);
get_v();
for (int i = 0; i * 3 <= m; i++) {
long long cnt = vc[i * 3];
cnt += find(m - i * 3);
ans = ans > cnt ? ans : cnt;
}
printf("%lld\n", ans);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300200;
struct Node {
int a, b;
long long v;
} dp[maxn];
long long n, w, a[4][maxn], sum[maxn];
int a1, b1, c1;
int main() {
int x;
long long y;
scanf("%lld%lld", &n, &w);
a1 = b1 = c1 = 0;
for (int i = 0; i < n; i++) {
scanf("%d%lld", &x, &y);
if (x == 1) a[1][++a1] = y;
if (x == 2) a[2][++b1] = y;
if (x == 3) a[3][++c1] = y;
}
sort(a[1] + 1, a[1] + a1 + 1, greater<long long>());
sort(a[2] + 1, a[2] + b1 + 1, greater<long long>());
sort(a[3] + 1, a[3] + c1 + 1, greater<long long>());
for (int i = 1; i <= c1; i++) sum[i] = sum[i - 1] + a[3][i];
dp[0].a = 0;
dp[0].b = 0;
dp[0].v = 0;
for (int i = 1; i <= w; i++) {
dp[i] = dp[i - 1];
if (dp[i - 1].a < a1 && dp[i - 1].v + a[1][dp[i - 1].a + 1] > dp[i].v) {
dp[i] = dp[i - 1];
dp[i].v += a[1][dp[i - 1].a + 1];
dp[i].a++;
}
if (i >= 2 && dp[i - 2].b < b1 &&
dp[i - 2].v + a[2][dp[i - 2].b + 1] > dp[i].v) {
dp[i] = dp[i - 2];
dp[i].v += a[2][dp[i - 2].b + 1];
dp[i].b++;
}
}
long long cnt = dp[w].v;
for (int i = 1; i <= c1; i++) {
if (w >= i * 3) cnt = max(cnt, sum[i] + dp[w - 3 * i].v);
}
printf("%lld\n", cnt);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long N = (2e5 + 5);
const long long MOD = 10243;
const long long INF = 1e16;
const long long LOG = 29;
long long binpow(long long a, long long b) {
a %= MOD;
long long res = 1;
while (b > 0) {
if (b & 1) res = (res * a) % MOD;
a = (a * a) % MOD;
b >>= 1;
}
res %= MOD;
return res;
}
struct gg {
long long ans;
long long one;
long long two;
};
gg dp[300005];
vector<long long> w[4];
void solve() {
long long n, m;
cin >> n >> m;
long long lol[n + 1], c[n + 1];
for (int i = 1; i <= n; i++) {
cin >> lol[i] >> c[i];
w[lol[i]].push_back(c[i]);
}
for (int i = 1; i <= 3; i++) {
sort(w[i].begin(), w[i].end(), greater<long long>());
}
dp[0].ans = 0;
dp[0].one = 0;
dp[0].two = 0;
for (int i = 1; i <= m; i++) {
dp[i] = {0, 0, 0};
}
if (m == 1) {
long long i = 0;
long long wp = dp[i].one;
if (wp >= w[1].size())
wp = 0;
else
wp = w[1][wp];
long long x = 1;
if (wp == 0) x = 0;
if (dp[i].ans + wp > dp[i + 1].ans) {
dp[i + 1].ans = dp[i].ans + wp;
dp[i + 1].one = dp[i].one + x;
dp[i + 1].two = dp[i].two;
}
}
for (int i = 0; i <= m - 2; i++) {
long long wp = dp[i].one;
if (wp >= w[1].size())
wp = 0;
else
wp = w[1][wp];
long long x = 1;
if (wp == 0) x = 0;
if (dp[i].ans + wp > dp[i + 1].ans) {
dp[i + 1].ans = dp[i].ans + wp;
dp[i + 1].one = dp[i].one + x;
dp[i + 1].two = dp[i].two;
}
long long o = dp[i].one;
long long t = dp[i].two;
if ((o + 1) >= w[1].size() && t >= w[2].size()) {
if (dp[i + 2].ans < dp[i].ans) {
dp[i + 2] = dp[i];
}
continue;
}
if ((o + 1) >= w[1].size()) {
if (dp[i].ans + w[2][t] > dp[i + 2].ans) {
dp[i + 2].ans = dp[i].ans + w[2][t];
dp[i + 2].two = dp[i].two + 1;
dp[i + 2].one = dp[i].one;
}
} else if (t >= w[2].size()) {
if (dp[i].ans + w[1][o] + w[1][o + 1] > dp[i + 2].ans) {
dp[i + 2].ans = dp[i].ans + w[1][o] + w[1][o + 1];
dp[i + 2].two = dp[i].two;
dp[i + 2].one = dp[i].one + 2;
}
} else {
if (w[1][o] + w[1][o + 1] > w[2][t]) {
if (dp[i].ans + w[1][o] + w[1][o + 1] > dp[i + 2].ans) {
dp[i + 2].ans = dp[i].ans + w[1][o] + w[1][o + 1];
dp[i + 2].two = dp[i].two;
dp[i + 2].one = dp[i].one + 2;
}
} else {
if (dp[i].ans + w[2][t] > dp[i + 2].ans) {
dp[i + 2].ans = dp[i].ans + w[2][t];
dp[i + 2].two = dp[i].two + 1;
dp[i + 2].one = dp[i].one;
}
}
}
}
long long ans = 0;
long long sum = 0;
for (long long x = 0; x < w[3].size(); x++) {
if (3 * (x + 1) > m) break;
sum += w[3][x];
ans = max(ans, sum + dp[m - 3 * (x + 1)].ans);
}
for (int i = 1; i <= m; i++) {
ans = max(ans, dp[i].ans);
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
long long t = 1;
while (t--) {
solve();
}
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long inff = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-9;
const int maxV = 3e5 + 5;
int n, V, mV, a[maxV], tot;
long long dp[5][maxV];
vector<long long> w[5];
void solve(int l, int r, int L, int R, int id) {
if (l > r) return;
int pos = L, mid = l + r >> 1;
for (int i = max(mid - (int)w[id].size(), L); i <= min(mid, R); i++) {
if (dp[id][a[mid]] <
dp[id - 1][a[i]] + (i == mid ? 0 : w[id][mid - i - 1])) {
dp[id][a[mid]] = dp[id - 1][a[i]] + (i == mid ? 0 : w[id][mid - i - 1]);
pos = i;
}
}
solve(l, mid - 1, L, pos, id);
solve(mid + 1, r, pos, R, id);
}
namespace fastIO {
static char buf[100000], *h = buf, *d = buf;
template <typename T>
inline void read(T& x) {
int f = 1;
x = 0;
register char c(
h == d && (d = (h = buf) + fread(buf, 1, 100000, stdin), h == d) ? EOF
: *h++);
while (c > '9' || c < '0') {
if (c == '-') f = -1;
c = h == d && (d = (h = buf) + fread(buf, 1, 100000, stdin), h == d) ? EOF
: *h++;
}
while (c <= '9' && c >= '0')
x = (x << 1) + (x << 3) + (c ^ 48),
c = h == d && (d = (h = buf) + fread(buf, 1, 100000, stdin), h == d) ? EOF
: *h++;
x *= f;
}
template <typename T>
void output(T x) {
if (x < 0) {
putchar('-');
x = ~(x - 1);
}
static int s[20], top = 0;
while (x) {
s[++top] = x % 10;
x /= 10;
}
if (!top) s[++top] = 0;
while (top) putchar(s[top--] + '0');
}
} // namespace fastIO
using fastIO::output;
using fastIO::read;
int main() {
read(n), read(V);
for (int i = (1); i <= (int)(n); ++i) {
int c, w0;
read(c), read(w0);
w[c].push_back(w0);
mV = max(mV, c);
}
for (int i = (1); i <= (int)(mV); ++i) {
sort(w[i].begin(), w[i].end(), greater<int>());
for (int j = 1; j < w[i].size(); j++) {
w[i][j] += w[i][j - 1];
}
}
for (int i = (1); i <= (int)(mV); ++i) {
for (int j = (0); j <= (int)(i - 1); ++j) {
tot = 0;
for (int k = j; k <= V; k += i) {
a[++tot] = k;
}
solve(1, tot, 1, tot, i);
}
}
int flag = 0;
printf("%lld\n", dp[mV][V]);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 100000;
const int MAXCAP = 300000;
typedef struct R {
int w, c;
} R;
bool operator<(const R &a, const R &b) {
return (long long)a.c * b.w > (long long)b.c * a.w;
}
int n, cap;
R r[MAXN];
multiset<int> have[4];
multiset<int> avail[4];
long long getlargest(multiset<int> &a, int cnt) {
if (((int)(a).size()) < cnt) return INT_MIN;
auto it = a.end();
long long ret = 0;
for (int i = (0); i < (cnt); ++i) {
--it;
ret += *it;
}
return ret;
}
long long getsmallest(multiset<int> &a, int cnt) {
if (((int)(a).size()) < cnt) return INT_MAX;
auto it = a.begin();
long long ret = 0;
for (int i = (0); i < (cnt); ++i) {
ret += *it;
++it;
}
return ret;
}
void removeone(multiset<int> &a, int val) {
auto it = a.find(val);
if (it == a.end())
printf("err (%d)\n", val);
else
a.erase(it);
}
void take(int cnt, int type) {
for (int i = (0); i < (cnt); ++i) {
long long cur = getlargest(avail[type], 1);
removeone(avail[type], cur);
have[type].insert(cur);
}
}
void remove(int cnt, int type) {
for (int i = (0); i < (cnt); ++i) {
long long cur = getsmallest(have[type], 1);
removeone(have[type], cur);
avail[type].insert(cur);
}
}
void run() {
scanf("%d%d", &n, &cap);
for (int i = (0); i < (n); ++i) scanf("%d%d", &r[i].w, &r[i].c);
sort(r, r + n);
int left = cap;
for (int i = (0); i < (4); ++i) have[i].clear(), avail[i].clear();
for (int i = (0); i < (n); ++i) avail[r[i].w].insert(r[i].c);
long long ret = 0;
for (int i = (0); i < (n); ++i) {
if (r[i].w > left) break;
left -= r[i].w;
ret += r[i].c;
have[r[i].w].insert(r[i].c);
removeone(avail[r[i].w], r[i].c);
}
while (left >= 1) {
long long best = 0;
int besttype = -1;
{
long long cur = getlargest(avail[1], 1);
if (cur > best) best = cur, besttype = 0;
}
{
long long cur = getlargest(avail[2], 1) - getsmallest(have[1], 1);
if (cur > best) best = cur, besttype = 1;
}
{
long long cur = getlargest(avail[2], 2) - getsmallest(have[3], 1);
if (cur > best) best = cur, besttype = 2;
}
{
long long cur = getlargest(avail[3], 1) - getsmallest(have[2], 1);
if (cur > best) best = cur, besttype = 3;
}
{
long long cur = getlargest(avail[3], 1) - getsmallest(have[1], 2);
if (cur > best) best = cur, besttype = 4;
}
if (besttype == -1) break;
if (besttype == 0)
take(1, 1);
else if (besttype == 1)
take(1, 2), remove(1, 1);
else if (besttype == 2)
take(2, 2), remove(1, 3);
else if (besttype == 3)
take(1, 3), remove(1, 2);
else if (besttype == 4)
take(1, 3), remove(2, 1);
ret += best;
--left;
}
while (left >= 2) {
long long best = 0;
int besttype = -1;
{
long long cur = getlargest(avail[2], 1);
if (cur > best) best = cur, besttype = 0;
}
{
long long cur = getlargest(avail[3], 1) - getsmallest(have[1], 1);
if (cur > best) best = cur, besttype = 1;
}
{
long long cur = getlargest(avail[3], 2) - getsmallest(have[2], 2);
if (cur > best) best = cur, besttype = 2;
}
if (besttype == -1) break;
if (besttype == 0)
take(1, 2);
else if (besttype == 1)
take(1, 3), remove(1, 1);
else if (besttype == 2)
take(2, 3), remove(2, 2);
ret += best;
--left;
}
printf("%lld\n", ret);
}
int main() {
run();
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m;
struct Souvenir {
int w, c;
bool operator<(const Souvenir &ot) const {
if (ot.w == w) return c > ot.c;
return w < ot.w;
}
} s[100002];
int cnt[3];
long long sum[3][100002];
long long ans;
long long cost(int i, int w) {
return sum[1][i] + sum[0][min(cnt[0], w - 2 * i)];
}
int bs(int w) {
int i = 0, p = 1 << 16;
while (p) {
if (i + p <= cnt[1] && ((i + p) * 2 <= w) &&
(cost(i + p, w) >= cost(i + p - 1, w)))
i += p;
p >>= 1;
}
return i;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d %d", &s[i].w, &s[i].c);
++cnt[--s[i].w];
}
sort(s + 1, s + n + 1);
int j = 0, crt = 0;
for (int i = 1; i <= n; ++i) {
if (s[i].w != s[i - 1].w) j = 0;
crt = s[i].w;
++j;
sum[crt][j] = sum[crt][j - 1] + 1LL * s[i].c;
}
for (int i = 0; i <= cnt[2] && m >= 3 * i; ++i) {
int nr2 = bs(m - 3 * i), nr1 = min(cnt[0], m - 2 * nr2 - 3 * i);
ans = max(ans, sum[0][nr1] + sum[1][nr2] + sum[2][i]);
}
printf("%lld\n", ans);
return 0;
}
| 11 | CPP |
import sys
n, m = map(int, input().split())
w1 = []
w2 = []
w3 = [10**10]
for w, c in (map(int, l.split()) for l in sys.stdin):
if w == 1:
w1.append(c)
elif w == 2:
w2.append(c)
else:
w3.append(c)
w1.sort(reverse=True)
w2.sort(reverse=True)
w3.sort(reverse=True)
w3[0] = 0
w1_size, w2_size = len(w1), len(w2)
dp = [(0, 0, 0) for _ in range(m+3)]
for i in range(m):
dp[i+1] = max(dp[i+1], dp[i])
if dp[i][1] < w1_size:
dp[i+1] = max(dp[i+1], (dp[i][0]+w1[dp[i][1]], dp[i][1]+1, dp[i][2]))
if dp[i][2] < w2_size:
dp[i+2] = max(dp[i+2], (dp[i][0]+w2[dp[i][2]], dp[i][1], dp[i][2]+1))
ans = 0
w3_c = 0
for i in range(len(w3)):
if i*3 > m:
continue
w3_c += w3[i]
ans = max(ans, w3_c + dp[m-i*3][0])
print(ans)
| 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
typedef struct status {
long long val;
long long cnt1;
long long cnt2;
} status;
vector<long long> v[4];
status s[300001];
int main() {
int n, m, w;
long long c, ans, add;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d%I64d", &w, &c);
v[w].push_back(c);
}
for (int i = 1; i <= 3; i++) {
sort(v[i].begin(), v[i].end());
reverse(v[i].begin(), v[i].end());
}
for (int i = 1; i <= m; i++) {
if (s[i - 1].cnt1 < v[1].size()) {
s[i].val = s[i - 1].val + v[1][s[i - 1].cnt1];
s[i].cnt1 = s[i - 1].cnt1 + 1;
s[i].cnt2 = s[i - 1].cnt2;
}
if (i >= 2 && s[i - 2].cnt2 < v[2].size() &&
s[i].val < s[i - 2].val + v[2][s[i - 2].cnt2]) {
s[i].val = s[i - 2].val + v[2][s[i - 2].cnt2];
s[i].cnt1 = s[i - 2].cnt1;
s[i].cnt2 = s[i - 2].cnt2 + 1;
}
}
for (int i = 1; i <= m; i++)
if (s[i].val < s[i - 1].val) s[i].val = s[i - 1].val;
ans = 0;
add = 0;
for (int i = 0; i <= v[3].size() && m - 3 * i >= 0; i++) {
if (s[m - 3 * i].val + add > ans) ans = s[m - 3 * i].val + add;
if (i < v[3].size()) add += v[3][i];
}
printf("%I64d\n", ans);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m;
cin >> n >> m;
vector<long long> a3, a2, a1;
for (int i = 0; i < n; i++) {
long long w, c;
cin >> w >> c;
if (w == 3)
a3.push_back(c);
else if (w == 2)
a2.push_back(c);
else
a1.push_back(c);
}
sort(a2.begin(), a2.end());
sort(a1.begin(), a1.end());
reverse(a1.begin(), a1.end());
reverse(a2.begin(), a2.end());
vector<pair<pair<long long, long long>, pair<long long, long long> > > comb;
long long second = 0, sw = 0;
int i = 0, j = 0;
int s1 = (int)a1.size(), s2 = (int)a2.size();
while (i < s1 && j < s2) {
long long k1 = 2ll * a1[i];
if (k1 > a2[j]) {
second += a1[i];
sw += 1;
comb.push_back({{second, sw}, {i + 1, j}});
i++;
} else {
second += a2[j];
sw += 2;
comb.push_back({{second, sw}, {i, j + 1}});
j++;
}
}
while (i < s1) {
second += a1[i];
sw += 1;
comb.push_back({{second, sw}, {i + 1, j}});
i++;
}
while (j < s2) {
second += a2[j];
sw += 2;
comb.push_back({{second, sw}, {i, j + 1}});
j++;
}
a1.push_back(0ll);
a2.push_back(0ll);
sort(a3.begin(), a3.end());
reverse(a3.begin(), a3.end());
s2 = (int)comb.size();
long long mx = 0;
long long tempscore = 0;
long long tl = 0;
for (int i = 0; i <= (int)a3.size(); i++) {
long long ma = m - 3ll * i;
tempscore = 0;
if (i >= 1) tl += a3[i - 1];
tempscore = tl;
if (ma < 0) break;
if (ma >= comb[s2 - 1].first.second) {
tempscore += comb[s2 - 1].first.first;
mx = max(mx, tempscore);
continue;
}
int lo = 0, hi = s2 - 1;
int ans = -1;
while (lo <= hi) {
int mid = (lo + hi) >> 1;
long long cw = comb[mid].first.second;
if (cw <= ma) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
if (ans == -1) {
if (s1 && ma) tempscore += a1[0];
} else {
long long j = 0;
if (comb[ans].first.second != ma) {
assert(comb[ans].first.second == (ma - 1));
j = a1[comb[ans].second.first];
if (comb[ans].second.first != 0) {
j = max(j,
a2[comb[ans].second.second] - a1[comb[ans].second.first - 1]);
}
tempscore += j;
}
tempscore += comb[ans].first.first;
}
mx = max(mx, tempscore);
}
cout << mx << endl;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
long long ans, dp[N];
int ed, n, m;
struct node {
int w, c;
double val;
node(int a = 0, int b = 0) : w(a), c(b) {}
bool operator<(const node &b) const { return val > b.val; }
} a[N], tmp[N];
long long DP() {
for (int i = 1; i <= ed; i++) {
for (int j = m; j >= tmp[i].w; j--) {
dp[j] = max(dp[j], dp[j - tmp[i].w] + tmp[i].c);
}
}
return dp[m];
}
int main() {
scanf("%d%d", &n, &m);
ans = 0;
for (int i = 1; i <= n; i++)
scanf("%d%d", &a[i].w, &a[i].c), a[i].val = 1.0 * a[i].c / a[i].w;
sort(a + 1, a + 1 + n);
int tp[4];
memset(tp, -1, sizeof(tp));
for (int i = 1; i <= n; i++) {
if (m < 30) {
ed = 0;
while (i <= n) tmp[++ed] = a[i], i++;
for (int ii = 1; ii <= 3; ii++)
if (~tp[ii]) tmp[++ed] = node(ii, tp[ii]), m += ii, ans -= tp[ii];
ans += DP();
break;
}
ans += a[i].c;
m -= a[i].w;
tp[a[i].w] = a[i].c;
}
printf("%lld\n", ans);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
void debug() {}
const int N = 1e5 + 5;
long long n, m, dp[N * 5];
struct Node {
long long weight, cost, ratio;
bool operator<(const Node &rhs) const { return ratio > rhs.ratio; }
} A[N];
int main() {
scanf("%lld %lld", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%lld %lld", &A[i].weight, &A[i].cost);
A[i].ratio = A[i].cost * 6 / A[i].weight;
}
std::sort(A + 1, A + 1 + n);
long long sum = 0;
for (int i = 1; i <= n; ++i) {
long long w = A[i].weight, c = A[i].cost;
sum += w;
for (int j = sum; j >= std::max(w, sum - 50); --j) {
dp[j] = std::max(dp[j], dp[j - w] + c);
}
}
long long ans = 0;
for (int i = m; i >= 0; --i) ans = std::max(ans, dp[i]);
printf("%lld\n", ans);
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
long long take[4][N];
long long best[4][N];
int cnt[N];
int n, m;
long long solve(int weight, int take_dua) {
int one = min(cnt[1], weight - 2 * take_dua);
return take[2][take_dua] + take[1][one];
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
int a, b;
scanf("%d %d", &a, &b);
cnt[a]++;
best[a][cnt[a]] = b;
}
for (int j = 1; j <= 3; j++) {
sort(best[j] + 1, best[j] + 1 + cnt[j]);
reverse(best[j] + 1, best[j] + 1 + cnt[j]);
for (int i = 1; i <= cnt[j]; i++) {
take[j][i] = take[j][i - 1] + best[j][i];
}
}
long long ans = 0;
for (int i = 0; i <= cnt[3] && 3 * i <= m; i++) {
int l = 0, r = min(cnt[2], (m - 3 * i) / 2);
int sisa = m - 3 * i;
while (r - l > 3) {
int lf = l + (r - l) / 3;
int rf = r - (r - l) / 3;
if (solve(sisa, lf) > solve(sisa, rf)) {
r = rf;
} else {
l = lf;
}
}
for (int j = l; j <= r; j++)
ans = max(ans, take[3][i] + solve(m - 3 * i, j));
}
cout << ans << endl;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<long long> A, B, C;
vector<long long> b, c, bb, cc;
long long solve(int mm) {
b.push_back(0);
bb.resize(b.size());
bb[0] = b[0];
for (int i = 1; i < (int)b.size(); i++) bb[i] = b[i] + bb[i - 1];
cc.resize(c.size());
cc[0] = c[0];
for (int i = 1; i < (int)c.size(); i++) cc[i] = c[i] + cc[i - 1];
long long ans = 0;
for (int i = 0; i * 3 <= mm && i < (int)c.size(); i++) {
int r = mm - i * 3;
r = min(r / 2, -1 + (int)b.size());
ans = max(ans, cc[i] - c[i] + bb[r] - b[r]);
}
return ans;
}
int main() {
scanf("%d", &n), scanf("%d", &m);
for (int i = 0; i < (int)n; i++) {
int w, c;
scanf("%d", &w), scanf("%d", &c);
if (w == 1) A.push_back(c);
if (w == 2) B.push_back(c);
if (w == 3) C.push_back(c);
}
sort(A.begin(), A.end(), greater<long long>());
sort(B.begin(), B.end(), greater<long long>());
sort(C.begin(), C.end(), greater<long long>());
for (int i = 0; i + 1 < (int)A.size(); i += 2) {
b.push_back(A[i] + A[i + 1]);
}
for (int i = 0; i < (int)B.size(); i++) b.push_back(B[i]);
sort(b.begin(), b.end(), greater<long long>());
for (int i = 0; i < (int)C.size(); i++) c.push_back(C[i]);
c.push_back(0);
long long ans = solve(m);
if (A.size() > 0) {
b.clear();
for (int i = 1; i + 1 < (int)A.size(); i += 2) {
b.push_back(A[i] + A[i + 1]);
}
for (int i = 0; i < (int)B.size(); i++) b.push_back(B[i]);
sort(b.begin(), b.end(), greater<long long>());
ans = max(ans, solve(m - 1) + A[0]);
}
printf("%lld\n", ans);
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
long long mode(long long a) {
a %= mod;
if (a < 0) a += mod;
return a;
}
long long subt(long long a, long long b) { return mode(mode(a) - mode(b)); }
long long add(long long a, long long b) { return mode(mode(a) + mode(b)); }
long long mult(long long a, long long b) { return mode(mode(a) * mode(b)); }
long long binpow(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = mult(res, a);
a = mult(a, a);
b >>= 1;
}
return res;
}
const int M = 3e5 + 7;
struct node {
long long cost;
long long cnt1, cnt2, cnt3;
} dp[M];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<int> soos[4];
for (int i = 1; i <= n; i++) {
int w, c;
cin >> w >> c;
soos[w].push_back(c);
}
sort(soos[1].rbegin(), soos[1].rend());
sort(soos[2].rbegin(), soos[2].rend());
sort(soos[3].rbegin(), soos[3].rend());
dp[0] = {0, 0, 0, 0};
for (int i = 1; i <= m; i++) {
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];
if (dp[i - 1].cnt1 < int((soos[1]).size()) &&
dp[i - 1].cost + soos[1][dp[i - 1].cnt1] > dp[i].cost) {
dp[i] = dp[i - 1];
dp[i].cost += soos[1][dp[i - 1].cnt1];
dp[i].cnt1++;
}
if (i >= 2 && dp[i - 2].cnt2 < int((soos[2]).size()) &&
dp[i - 2].cost + soos[2][dp[i - 2].cnt2] > dp[i].cost) {
dp[i] = dp[i - 2];
dp[i].cost += soos[2][dp[i - 2].cnt2];
dp[i].cnt2++;
}
if (i >= 3 && dp[i - 3].cnt3 < int((soos[3]).size()) &&
dp[i - 3].cost + soos[3][dp[i - 3].cnt3] > dp[i].cost) {
dp[i] = dp[i - 3];
dp[i].cost += soos[3][dp[i - 3].cnt3];
dp[i].cnt3++;
}
if (i >= 2 && dp[i - 2].cnt1 && dp[i - 2].cnt3 < int((soos[3]).size()) &&
dp[i - 2].cost - soos[1][dp[i - 2].cnt1 - 1] + soos[3][dp[i - 2].cnt3] >
dp[i].cost) {
dp[i].cost = dp[i - 2].cost - soos[1][dp[i - 2].cnt1 - 1] +
soos[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;
}
}
cout << dp[m].cost;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
typedef struct {
long long int cost, ones, twos;
} s;
int main() {
long long int n, m;
cin >> n >> m;
vector<long long int> ones;
vector<long long int> twos;
vector<long long int> threes;
for (long long int i = 0; i < n; i++) {
long long int a, b;
cin >> a >> b;
if (a == 1) ones.push_back(b);
if (a == 2) twos.push_back(b);
if (a == 3) threes.push_back(b);
}
sort(ones.begin(), ones.end());
sort(twos.begin(), twos.end());
sort(threes.begin(), threes.end());
vector<s> ss(m + 1);
ss[0] = {0, 0, 0};
if (ones.size() > 0) {
ss[1] = {ones.back(), 1, 0};
} else {
ss[1] = {0, 0, 0};
}
for (long long int i = 2; i <= m; i++) {
s a = {-1, 0, 0}, b = {-1, 0, 0}, c = ss[i - 1];
if (ss[i - 1].ones < ones.size()) {
a = {ss[i - 1].cost + ones[ones.size() - 1 - ss[i - 1].ones],
ss[i - 1].ones + 1, ss[i - 1].twos};
}
if (ss[i - 2].twos < twos.size()) {
b = {ss[i - 2].cost + twos[twos.size() - 1 - ss[i - 2].twos],
ss[i - 2].ones, ss[i - 2].twos + 1};
}
s d;
if (a.cost > b.cost)
d = a;
else
d = b;
if (d.cost > c.cost)
ss[i] = d;
else
ss[i] = c;
}
vector<long long int> part_threes(threes.size() + 1, 0);
for (long long int i = 1; i <= threes.size(); i++) {
part_threes[i] = part_threes[i - 1] + threes[threes.size() - i];
}
long long int best = 0;
for (long long int i = 0; i <= threes.size(); i++) {
long long int a;
if (3 * i > m)
a = -1;
else
a = part_threes[i] + ss[m - 3 * i].cost;
best = max(best, a);
}
cout << best << endl;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int qq = 3e5 + 10;
struct Backpack {
long long val;
int m1, m2;
} dp[qq];
int num[4];
long long Val[4][qq];
long long pre[4][qq];
int n, m;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
int w;
long long val;
scanf("%d%lld", &w, &val);
Val[w][++num[w]] = val;
}
for (int i = 1; i < 4; ++i) {
sort(Val[i] + 1, Val[i] + 1 + num[i], greater<long long>());
for (int j = 1; j <= num[i]; ++j) pre[i][j] = pre[i][j - 1] + Val[i][j];
}
dp[0].val = dp[0].m1 = dp[0].m2 = 0;
for (int i = 1; i <= m; ++i) {
dp[i] = dp[i - 1];
if (dp[i].val < dp[i - 1].val + Val[1][dp[i - 1].m1 + 1]) {
dp[i].val = dp[i - 1].val + Val[1][dp[i - 1].m1 + 1];
dp[i].m1 = dp[i - 1].m1 + 1;
dp[i].m2 = dp[i - 1].m2;
}
if (i >= 2 && dp[i].val < dp[i - 2].val + Val[2][dp[i - 2].m2 + 1]) {
dp[i].val = dp[i - 2].val + Val[2][dp[i - 2].m2 + 1];
dp[i].m1 = dp[i - 2].m1;
dp[i].m2 = dp[i - 2].m2 + 1;
}
}
long long ans = 0;
for (int i = 0; i <= num[3]; ++i) {
if (m >= i * 3) {
ans = max(ans, pre[3][i] + dp[m - 3 * i].val);
}
}
printf("%lld\n", ans);
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long dp[300024];
int k[300024][3];
vector<int> v[3];
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
int w, c;
cin >> w >> c;
v[w - 1].push_back(c);
}
for (int i = 0; i < 3; i++) {
sort(v[i].begin(), v[i].end());
reverse(v[i].begin(), v[i].end());
}
if (v[0].size() > 0) {
dp[1] = v[0][0];
k[1][0]++;
}
long long t1 = 0, t2 = 0, t3 = 0;
dp[2] = dp[1];
k[2][0] = k[1][0];
k[2][1] = k[1][1];
k[2][2] = k[1][2];
if (v[0].size() > 1) t1 = v[0][0] + v[0][1];
if (v[1].size() > 0) t2 = v[1][0];
if (t1 > t2 && t1 > dp[2]) {
dp[2] = t1;
k[2][0] = 2;
k[2][1] = 0;
k[2][2] = 0;
} else if (t2 > dp[2]) {
dp[2] = t2;
k[2][0] = 0;
k[2][1] = 1;
k[2][2] = 0;
}
t1 = 0, t2 = 0, t3 = 0;
dp[3] = dp[2];
k[3][0] = k[2][0];
k[3][1] = k[2][1];
k[3][2] = k[2][2];
if (v[0].size() > 2) t1 = v[0][0] + v[0][1] + v[0][2];
if (v[1].size() > 0 && v[0].size() > 0) t2 = v[0][0] + v[1][0];
if (v[2].size() > 0) t3 = v[2][0];
if (t1 > t2 && t1 > t3 && t1 > dp[3]) {
dp[3] = t1;
k[3][0] = 3;
k[3][1] = 0;
k[3][2] = 0;
} else if (t2 > t3 && t2 > dp[3]) {
dp[3] = t2;
k[3][0] = 1;
k[3][1] = 1;
k[3][2] = 0;
} else if (t3 > dp[3]) {
dp[3] = t3;
k[3][0] = 0;
k[3][1] = 0;
k[3][2] = 1;
}
for (int i = 4; i <= m; i++) {
t1 = 0, t2 = 0, t3 = 0;
dp[i] = dp[i - 1];
k[i][0] = k[i - 1][0];
k[i][1] = k[i - 1][1];
k[i][2] = k[i - 1][2];
if (k[i - 3][2] < v[2].size()) t1 = dp[i - 3] + v[2][k[i - 3][2]];
if (k[i - 2][1] < v[1].size()) t2 = dp[i - 2] + v[1][k[i - 2][1]];
if (k[i - 1][0] < v[0].size()) t3 = dp[i - 1] + v[0][k[i - 1][0]];
long long t4 = 0, t5 = 0, t6 = 0;
if (k[i - 1][0] > 0 && k[i - 1][1] < v[1].size())
t4 = dp[i - 1] - v[0][k[i - 1][0] - 1] + v[1][k[i - 1][1]];
if (k[i - 1][1] > 0 && k[i - 1][2] < v[2].size())
t5 = dp[i - 1] - v[1][k[i - 1][1] - 1] + v[2][k[i - 1][2]];
if (k[i - 2][0] > 0 && k[i - 2][2] < v[2].size())
t6 = dp[i - 2] - v[0][k[i - 2][0] - 1] + v[2][k[i - 2][2]];
if (t1 > t2 && t1 > t3 && t1 > t4 && t1 > t5 && t1 > t6 && t1 > dp[i]) {
dp[i] = t1;
k[i][0] = k[i - 3][0];
k[i][1] = k[i - 3][1];
k[i][2] = k[i - 3][2] + 1;
} else if (t2 > t3 && t2 > t4 && t2 > t5 && t2 > t6 && t2 > dp[i]) {
dp[i] = t2;
k[i][0] = k[i - 2][0];
k[i][1] = k[i - 2][1] + 1;
k[i][2] = k[i - 2][2];
} else if (t3 > t4 && t3 > t5 && t3 > t6 && t3 > dp[i]) {
dp[i] = t3;
k[i][0] = k[i - 1][0] + 1;
k[i][1] = k[i - 1][1];
k[i][2] = k[i - 1][2];
} else if (t4 > t5 && t4 > t6 && t4 > dp[i]) {
dp[i] = t4;
k[i][0] = k[i - 1][0] - 1;
k[i][1] = k[i - 1][1] + 1;
k[i][2] = k[i - 1][2];
} else if (t5 > t6 && t5 > dp[i]) {
dp[i] = t5;
k[i][0] = k[i - 1][0];
k[i][1] = k[i - 1][1] - 1;
k[i][2] = k[i - 1][2] + 1;
} else if (t6 > dp[i]) {
dp[i] = t6;
k[i][0] = k[i - 2][0] - 1;
k[i][1] = k[i - 2][1];
k[i][2] = k[i - 2][2] + 1;
}
}
cout << dp[m] << endl;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr int N = 1e5 + 5;
vector<LL> sum[4];
LL check(int two, int three, int rem) {
LL rv = 0;
if (two > 0) rv += sum[2][two - 1];
if (three > 0) rv += sum[3][three - 1];
int one = rem - two * 2 - three * 3;
assert(one >= 0);
one = min(one, (int)sum[1].size());
if (one > 0) rv += sum[1][one - 1];
return rv;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
int w, c;
cin >> w >> c;
sum[w].push_back(c);
}
for (int i = 1; i <= 3; i++) sort(sum[i].rbegin(), sum[i].rend());
for (int i = 1; i <= 3; i++) {
for (int j = 1; j < sum[i].size(); j++) {
sum[i][j] += sum[i][j - 1];
}
}
LL best = 0;
for (int three = 0; three <= sum[3].size(); three++) {
if (three * 3 > m) break;
int cnt1 = min((int)sum[2].size(), (m - three * 3) / 2);
int two = -1;
for (int b = cnt1; b >= 1; b /= 2) {
while (two + b + 1 <= cnt1 &&
check(two + b, three, m) < check(two + b + 1, three, m)) {
two += b;
}
}
LL cur = check(two + 1, three, m);
best = max(best, cur);
}
cout << best << "\n";
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300000 + 10;
const int INF = 0x7fffffff;
struct {
long long v;
long long s1, s2;
} dp[MAXN];
long long a[4][MAXN];
long long s[4][MAXN];
int num[4];
int cmp(int x, int y) { return x > y; }
int main() {
int n, m;
scanf("%d%d", &n, &m);
num[1] = num[2] = num[3] = 0;
for (int i = 1; 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].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);
}
printf("%I64d\n", res);
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long n, m;
struct nod {
long long val, pt1, pt2;
} dp[300005];
ifstream in("selling.in");
vector<long long> unu, doi, trei;
nod Max(nod a, nod b) { return a.val < b.val ? b : a; }
int main() {
long long i, j, w, v, last1, last2, var1, var2;
cin >> n >> m;
for (i = 1; i <= n; i++) {
cin >> w >> v;
if (w == 1) unu.push_back(v);
if (w == 2) doi.push_back(v);
if (w == 3) trei.push_back(v);
}
sort(unu.begin(), unu.end());
sort(doi.begin(), doi.end());
sort(trei.begin(), trei.end());
for (i = 1; i <= m; i++) {
if (int(unu.size()) - dp[i - 1].pt1 - 1 >= 0)
var1 = dp[i - 1].val + unu[int(unu.size()) - 1 - dp[i - 1].pt1];
else
var1 = 0;
if (i >= 2 && int(doi.size()) - dp[i - 2].pt2 - 1 >= 0)
var2 = dp[i - 2].val + doi[int(doi.size()) - 1 - dp[i - 2].pt2];
else
var2 = 0;
if (var1 > var2) {
if (var1 > dp[i].val) {
dp[i].val = var1;
dp[i].pt1 = dp[i - 1].pt1 + 1;
dp[i].pt2 = dp[i - 1].pt2;
}
} else {
if (var2 > dp[i].val) {
dp[i].val = var2;
dp[i].pt1 = dp[i - 2].pt1;
dp[i].pt2 = dp[i - 2].pt2 + 1;
}
}
}
for (i = 1; i <= m; i++) dp[i] = Max(dp[i - 1], dp[i]);
long long sol = 0, sum = 0, l = trei.size();
for (i = 0; 3 * i <= m; i++) {
sol = max(sol, dp[m - 3 * i].val + sum);
if (trei.empty()) break;
sum += trei.back(), trei.pop_back();
}
cout << sol << "\n";
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > v(3);
long long s[3][100005];
int m;
long long f1(int y, int z) {
if (m - 3 * z - 2 * y < 0) {
return 0;
}
long long val = 0;
if (z > 0) val += s[2][z - 1];
if (y > 0) val = val + s[1][y - 1];
if (m - 3 * z - 2 * y > 0) {
val += s[0][min(m - 3 * z - 2 * y - 1, (int)v[0].size() - 1)];
}
return val;
}
long long compute(int z) {
int m1 = m;
long long ans = -100;
m1 -= z * 3;
if (m1 < 0) {
return 0;
}
int l = 0;
int r = min((int)v[1].size(), m1 / 2);
while (r - l >= 3) {
int mid1 = (2 * l + r) / 3;
int mid2 = (2 * r + l) / 3;
if (f1(mid1, z) > f1(mid2, z)) {
r = mid2;
} else {
l = mid1;
}
}
while (l <= r) {
ans = max(ans, f1(l, z));
l++;
}
return ans;
}
int main() {
std::ios::sync_with_stdio(false);
int n, w, c, i, j;
long long res = -100;
cin >> n >> m;
for (i = 0; i < n; i++) {
cin >> w >> c;
w--;
v[w].push_back(c);
}
for (i = 0; i < 3; i++) sort(v[i].rbegin(), v[i].rend());
for (i = 0; i < 3; i++) {
for (j = 0; j < v[i].size(); j++) {
s[i][j] = j > 0 ? s[i][j - 1] + v[i][j] : v[i][j];
}
}
for (i = 0; i <= v[2].size(); i++) {
res = max(res, compute(i));
}
cout << res << endl;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const int mod = 1e9 + 7;
long long kek[100005], w[100005], dp[1337228], c[100005], nm[100005];
bool kekekekekekkek(int i, int j) { return kek[i] > kek[j]; }
int main() {
ios_base::sync_with_stdio(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> w[i] >> c[i];
kek[i] = 1337 * c[i] / w[i];
nm[i] = i;
}
sort(nm + 1, nm + n + 1, kekekekekekkek);
int cur = 0;
for (int i = 1; i <= n; i++) {
int curw = w[nm[i]], curc = c[nm[i]];
cur += curw;
for (int j = cur; j >= max(curw, cur - 228); j--) {
dp[j] = max(dp[j], dp[j - curw] + curc);
}
}
for (int i = 1; i <= m; i++) {
dp[i] = max(dp[i], dp[i - 1]);
}
cout << dp[m] << endl;
}
| 11 | CPP |
#!/usr/bin/env python3
[n, m] = map(int, input().strip().split())
wc = [[] for _ in range(4)] # w[0] unused
for _ in range(n):
w, c = map(int, input().strip().split())
wc[w].append(c)
for i in range(1, 4):
wc[i].sort(reverse=True)
iwc = [[0 for _ in range(len(wc[i]) + 1)] for i in range(4)]
for i in range(4):
for j in range(len(wc[i])):
iwc[i][j + 1] = iwc[i][j] + wc[i][j]
n1 = len(wc[1])
n2 = len(wc[2])
n3 = len(wc[3])
c12 = [(0, 0, 0) for _ in range(m + 1)]
for w in range(len(c12) - 1):
c, q1, q2 = c12[w]
c12[w + 1] = max(c12[w + 1], c12[w])
if q1 < n1:
c12[w + 1] = max(c12[w + 1], (iwc[1][q1 + 1] + iwc[2][q2], q1 + 1, q2))
if q2 < n2 and w + 2 < len(c12):
c12[w + 2] = max(c12[w + 2], (iwc[1][q1] + iwc[2][q2 + 1], q1, q2 + 1))
cmax = 0
for i in range(n3 + 1):
if 3 * i > m:
break
cmax = max(cmax, iwc[3][i] + c12[m - 3 * i][0])
print(cmax)
| 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
struct Data {
long long cost, ones, twos;
};
const long long N = 100100;
const long long K = 300300;
long long n, m;
Data dp[K];
vector<long long> adj[4];
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (long long i = 1; i <= n; i++) {
long long w, c;
cin >> w >> c;
adj[w].push_back(c);
}
for (long long i = 1; i <= 3; i++) {
sort(adj[i].begin(), adj[i].end());
reverse(adj[i].begin(), adj[i].end());
}
dp[0] = {0, 0, 0};
for (long long i = 1; i <= m; i++) {
dp[i] = dp[i - 1];
if (i - 2 >= 0 && dp[i - 2].cost > dp[i].cost) {
dp[i] = dp[i - 2];
}
long long ones = dp[i].ones, twos = dp[i].twos;
if (dp[i - 1].ones < adj[1].size()) {
if (dp[i - 1].cost + adj[1][dp[i - 1].ones] > dp[i].cost) {
dp[i].cost = dp[i - 1].cost + adj[1][dp[i - 1].ones];
ones = 1 + dp[i - 1].ones;
twos = dp[i - 1].twos;
}
}
if (i - 2 >= 0 && dp[i - 2].twos < adj[2].size()) {
if (dp[i - 2].cost + adj[2][dp[i - 2].twos] > dp[i].cost) {
dp[i].cost = dp[i - 2].cost + adj[2][dp[i - 2].twos];
ones = dp[i - 2].ones;
twos = 1 + dp[i - 2].twos;
}
}
dp[i].ones = ones;
dp[i].twos = twos;
}
long long ans = dp[m].cost;
long long sm = 0;
for (long long i = 0; i < adj[3].size(); i++) {
sm += adj[3][i];
if (m - 3 * (i + 1) < 0) continue;
ans = max(ans, sm + dp[m - 3 * (i + 1)].cost);
}
cout << ans << "\n";
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 300010;
int n, m;
int arr[4][N];
int idx[4];
long long best[N], ans = 0;
pair<int, int> best2[N];
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
int w, c;
scanf("%d%d", &w, &c);
arr[w][idx[w]++] = c;
}
for (int i = 0; i < 4; i++) {
sort(arr[i], arr[i] + idx[i]);
reverse(arr[i], arr[i] + idx[i]);
}
best[1] = arr[1][0];
ans = max(ans, best[1]);
best2[1] = make_pair(0, -1);
best2[0] = make_pair(-1, -1);
for (int i = 2; i <= m; i++) {
best[i] = best[i - 1] + arr[1][best2[i - 1].first + 1];
best2[i] = best2[i - 1];
best2[i].first++;
if (best[i - 2] + arr[2][best2[i - 2].second + 1] > best[i]) {
best[i] = best[i - 2] + arr[2][best2[i - 2].second + 1];
best2[i] = best2[i - 2];
best2[i].second++;
}
ans = max(ans, best[i]);
}
long long sum = 0;
int i = 0;
int cur = 0;
while (cur + 3 <= m) {
cur += 3;
sum += arr[3][i++];
ans = max(ans, sum + best[m - cur]);
}
cout << ans << endl;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m, w;
long long c, r, vop;
vector<long long> s[3], op[2], tot[3];
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d %lld", &w, &c);
s[w - 1].push_back(c);
}
sort(s[2].begin(), s[2].end());
tot[0].push_back(0);
for (int i = s[2].size() - 1; i >= 0; i--)
tot[0].push_back(tot[0].back() + s[2][i]);
if (!s[0].empty()) {
sort(s[0].begin(), s[0].end());
vop = s[0].back();
}
op[0] = s[1];
op[1] = s[1];
for (int i = s[0].size() - 1; i >= 0; i -= 2) {
if (i)
op[0].push_back(s[0][i] + s[0][i - 1]);
else
op[0].push_back(s[0][0]);
}
for (int i = s[0].size() - 2; i >= 0; i -= 2) {
if (i)
op[1].push_back(s[0][i] + s[0][i - 1]);
else
op[1].push_back(s[0][0]);
}
sort(op[0].begin(), op[0].end());
sort(op[1].begin(), op[1].end());
tot[1].push_back(0);
for (int i = op[0].size() - 1; i >= 0; i--)
tot[1].push_back(tot[1].back() + op[0][i]);
tot[2].push_back(0);
for (int i = op[1].size() - 1; i >= 0; i--)
tot[2].push_back(tot[2].back() + op[1][i]);
for (int i = 0; i < tot[0].size() && 3 * i <= m; i++) {
int h = m - 3 * i, s1 = tot[1].size(), s2 = tot[2].size();
if (h & 1)
r = max(r, tot[0][i] + vop + tot[2][min(s2 - 1, h / 2)]);
else
r = max(r, tot[0][i] + tot[1][min(s1 - 1, h / 2)]);
}
printf("%lld\n", r);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m;
long long dp[300005];
struct S {
int w, c;
bool friend operator<(S x, S y) {
return ((double)x.c / x.w > (double)y.c / y.w);
}
} a[100005];
int main() {
while (scanf("%d%d", &n, &m) != -1) {
for (int i = 1; i <= n; i += 1) {
scanf("%d%d", &a[i].w, &a[i].c);
}
sort(a + 1, a + n + 1);
dp[0] = 0;
for (int i = 1; i < m; i += 1) {
dp[i] = -1e17L;
}
int up = 0;
long long ans = 0;
for (int i = 1; i <= n; i += 1) {
up += a[i].w;
if (up > m) {
up = m;
}
int least = max(a[i].w, up - 3);
for (int j = up; j >= least; j -= 1) {
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;
const int MAXN = 100010;
const int MAXW = 300010;
int n, m;
int a[MAXW] = {0}, b[MAXW] = {0}, c[MAXW] = {0};
int aidx = 0, bidx = 0, cidx = 0;
long long f[MAXW] = {0};
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i) {
int w, cost;
scanf("%d%d", &w, &cost);
if (w == 1)
a[aidx++] = cost;
else if (w == 2)
b[bidx++] = cost;
else
c[cidx++] = cost;
}
sort(a, a + aidx, greater<int>());
sort(b, b + bidx, greater<int>());
sort(c, c + cidx, greater<int>());
int ai = 0, bi = 0, noww = 0;
long long nowc = 0;
for (int i = 1; i <= m; ++i) {
if (i - noww >= 2) {
if (a[ai] * 2 > b[bi])
nowc += a[ai++], ++noww;
else
nowc += b[bi++], noww += 2;
}
if (i == noww)
f[i] = nowc;
else {
f[i] = nowc + a[ai];
if (ai) f[i] = max(f[i], nowc - a[ai - 1] + b[bi]);
}
}
long long ans = f[m];
noww = m, nowc = 0;
for (int i = 0; i < cidx; ++i) {
noww -= 3, nowc += c[i];
if (noww < 0) break;
ans = max(ans, f[noww] + nowc);
}
printf("%lld\n", ans);
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 - 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)), check(0, 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;
unsigned sed = std::chrono::system_clock::now().time_since_epoch().count();
mt19937 rand_num(sed);
uniform_int_distribution<long long> dis(0, 1000000007);
template <typename T>
void read(T &x) {
x = 0;
char ch = getchar();
long long fh = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') fh = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
x *= fh;
}
template <typename T>
void write(T x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
template <typename T>
void writeln(T x) {
write(x);
puts("");
}
long long n, m;
long long l1, l2, l3;
long long c1[100005], c2[100005], c3[100005];
long long sum1[100005], sum2[100005], sum3[100005];
signed main() {
read(n);
read(m);
for (long long i = 1; i <= n; ++i) {
long long w, c;
read(w);
read(c);
if (w == 1) c1[++l1] = c;
if (w == 2) c2[++l2] = c;
if (w == 3) c3[++l3] = c;
}
sort(c1 + 1, c1 + l1 + 1);
sort(c2 + 1, c2 + l2 + 1);
sort(c3 + 1, c3 + l3 + 1);
reverse(c1 + 1, c1 + l1 + 1);
reverse(c2 + 1, c2 + l2 + 1);
reverse(c3 + 1, c3 + l3 + 1);
for (long long i = 1; i <= l1; ++i) sum1[i] = sum1[i - 1] + c1[i];
for (long long i = 1; i <= l2; ++i) sum2[i] = sum2[i - 1] + c2[i];
for (long long i = 1; i <= l3; ++i) sum3[i] = sum3[i - 1] + c3[i];
long long ans = 0;
for (long long i = 0; i <= min(m / 3, l3); ++i) {
long long left = m - i * 3;
long long l = 1, r = min(left / 2, l2);
while (l < r) {
long long mid = l + r + 1 >> 1;
long long left1 = left - mid * 2;
long long val =
sum1[min(l1, left1)] + sum2[min(l2, mid)] + sum3[min(l3, i)];
long long mid1 = mid - 1;
long long left2 = left - mid1 * 2;
long long val2 =
sum1[min(l1, left2)] + sum2[min(l2, mid1)] + sum3[min(l3, i)];
if (val >= val2)
l = mid;
else
r = mid - 1;
}
for (long long j = max(0LL, l - 2); j <= min(l2, l + 2); ++j) {
long long left1 = m - i * 3 - j * 2;
if (left1 < 0) continue;
ans =
max(ans, sum1[min(l1, left1)] + sum2[min(l2, j)] + sum3[min(l3, i)]);
}
}
writeln(ans);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
pair<long long int, pair<int, int> > dp[300005];
vector<int> cost1;
vector<int> cost2;
vector<int> cost3;
int main() {
int n, m;
cin >> n;
cin >> m;
for (int i = 0; i < n; i++) {
int w, c;
cin >> w;
cin >> c;
if (w == 1) {
cost1.push_back(c);
} else if (w == 2) {
cost2.push_back(c);
} else {
cost3.push_back(c);
}
}
sort(cost1.begin(), cost1.end());
reverse(cost1.begin(), cost1.end());
sort(cost2.begin(), cost2.end());
reverse(cost2.begin(), cost2.end());
sort(cost3.begin(), cost3.end());
reverse(cost3.begin(), cost3.end());
dp[0].first = 0;
dp[0].second.first = 0;
dp[0].second.second = 0;
for (int i = 1; i <= m; i++) {
if (i == 1) {
if (cost1.size() > 0) {
dp[i].first = cost1[0];
dp[i].second.first = 1;
dp[i].second.second = 0;
} else {
dp[i].first = 0;
dp[i].second.first = 0;
dp[i].second.second = 0;
}
} else {
if ((cost1.size() > dp[i - 1].second.first) &&
(cost2.size() > dp[i - 2].second.second) &&
(dp[i - 1].first + cost1[dp[i - 1].second.first] >
dp[i - 2].first + cost2[dp[i - 2].second.second])) {
dp[i].first = dp[i - 1].first + cost1[dp[i - 1].second.first];
dp[i].second.first = dp[i - 1].second.first + 1;
dp[i].second.second = dp[i - 1].second.second;
} else if ((cost1.size() > dp[i - 1].second.first) &&
(cost2.size() > dp[i - 2].second.second) &&
(dp[i - 1].first + cost1[dp[i - 1].second.first] <=
dp[i - 2].first + cost2[dp[i - 2].second.second])) {
dp[i].first = dp[i - 2].first + cost2[dp[i - 2].second.second];
dp[i].second.first = dp[i - 2].second.first;
dp[i].second.second = dp[i - 2].second.second + 1;
} else if ((cost1.size() <= dp[i - 1].second.first) &&
(cost2.size() > dp[i - 2].second.second)) {
if (dp[i - 1].first >=
dp[i - 2].first + cost2[dp[i - 2].second.second]) {
dp[i].first = dp[i - 1].first;
dp[i].second.first = dp[i - 1].second.first;
dp[i].second.second = dp[i - 1].second.second;
} else {
dp[i].first = dp[i - 2].first + cost2[dp[i - 2].second.second];
dp[i].second.first = dp[i - 2].second.first;
dp[i].second.second = dp[i - 2].second.second + 1;
}
} else if ((cost1.size() > dp[i - 1].second.first) &&
(cost2.size() <= dp[i - 2].second.second)) {
if (dp[i - 2].first >=
dp[i - 1].first + cost1[dp[i - 1].second.first]) {
dp[i].first = dp[i - 2].first;
dp[i].second.first = dp[i - 2].second.first;
dp[i].second.second = dp[i - 2].second.second;
} else {
dp[i].first = dp[i - 1].first + cost1[dp[i - 1].second.first];
dp[i].second.first = dp[i - 1].second.first + 1;
dp[i].second.second = dp[i - 1].second.second;
}
} else {
dp[i].first = dp[i - 1].first;
dp[i].second.first = dp[i - 1].second.first;
dp[i].second.second = dp[i - 1].second.second;
}
}
}
long long int ans = dp[m].first;
long long int temp = 0;
for (int i = 0; i < cost3.size(); i++) {
temp += cost3[i];
if (m - 3 * (i + 1) >= 0) ans = max(ans, temp + dp[m - 3 * (i + 1)].first);
}
cout << ans;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, i, l1, l2, l3;
long long pre[311111], ans;
long long dp[311111][3];
vector<long long> v[3];
bool cmp(long long x, long long y) { return x > y; }
long long min(long long x, long long y) { return x < y ? x : y; }
long long max(long long x, long long y) { return x > y ? x : y; }
int main() {
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++) {
scanf("%d%d", &x, &y);
v[x].push_back(y);
}
l1 = (int)v[1].size();
l2 = (int)v[2].size();
l3 = (int)v[3].size();
sort(v[1].begin(), v[1].end(), cmp);
sort(v[2].begin(), v[2].end(), cmp);
sort(v[3].begin(), v[3].end(), cmp);
if (l3) pre[0] = v[3][0];
for (i = 1; i < l3; i++) pre[i] = pre[i - 1] + v[3][i];
for (i = 0; i < m; i++) {
if (dp[i + 1][0] < dp[i][0]) {
dp[i + 1][0] = dp[i][0];
dp[i + 1][1] = dp[i][1];
dp[i + 1][2] = dp[i][2];
}
if (dp[i][1] < l1) {
if (dp[i + 1][0] < dp[i][0] + v[1][dp[i][1]]) {
dp[i + 1][0] = dp[i][0] + v[1][dp[i][1]];
dp[i + 1][1] = dp[i][1] + 1;
dp[i + 1][2] = dp[i][2];
}
}
if (dp[i][2] < l2) {
dp[i + 2][0] = dp[i][0] + v[2][dp[i][2]];
dp[i + 2][1] = dp[i][1];
dp[i + 2][2] = dp[i][2] + 1;
}
if (l3 && (m - i) / 3)
ans = max(ans, dp[i][0] + pre[min((m - i) / 3 - 1, l3 - 1)]);
}
ans = max(ans, dp[m][0]);
printf("%lld", ans);
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300005;
inline long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int n, m, c, C, yu, sz, pos[maxn];
vector<long long> vec[4];
long long sum[1000005], f[maxn] = {0}, v, ww[maxn], dp[maxn], g[maxn];
void solve(int l, int r, int L, int R) {
if (l > r) return;
int mid = (l + r) >> 1;
pos[mid] = mid;
long long ma = -1;
for (int i = L; i <= mid && i <= R; i++) {
if (i + sz >= mid & dp[i] + sum[mid - i] > ma)
pos[mid] = i, ma = dp[i] + sum[mid - i];
}
solve(mid + 1, r, pos[mid], R);
solve(l, mid - 1, L, pos[mid]);
}
int main() {
n = read(), m = read();
for (int i = 1; i <= n; i++) {
c = read(), v = read();
vec[c].push_back(v);
}
for (C = 1; C < 4; C++) {
if (!vec[C].size()) continue;
sort(vec[C].begin(), vec[C].end());
sz = vec[C].size();
sum[0] = 0;
for (int i = 1; i <= sz; i++)
sum[i] = sum[i - 1] + vec[C][vec[C].size() - i];
for (yu = 0; yu < C; yu++) {
int tot = -1;
for (int j = yu; j <= m; j += C) {
tot++;
dp[tot] = f[j];
ww[tot] = 0;
}
solve(0, tot, 0, tot);
for (int j = 0; j <= tot; j++) ww[j] = dp[pos[j]] + sum[j - pos[j]];
int cur = 0;
for (int j = yu; j <= m; j += C) g[j] = ww[cur++];
}
for (int i = 0; i <= m; i++) f[i] = g[i];
}
printf("%lld\n", f[m]);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
void solve(std::istream& in, std::ostream& out) {
long long n, m;
in >> n >> m;
std::array<std::vector<long long>, 3> c, c_sum;
for (long long i = 0; i < n; ++i) {
long long w, cur_c;
in >> w >> cur_c;
c[w - 1].push_back(cur_c);
}
for (auto& p : c) std::sort(p.begin(), p.end(), greater<long long>());
for (long long i = 0; i < 3; ++i) {
c_sum[i].resize(c[i].size() + 1);
std::partial_sum(c[i].begin(), c[i].end(), c_sum[i].begin() + 1);
}
auto get_sum = [&](long long weight, long long num) {
return c_sum[weight - 1][std::min((long long)c[weight - 1].size(), num)];
};
auto get = [&](long long weight, long long num) {
if (num < c[weight - 1].size()) return c[weight - 1][num];
return 0LL;
};
long long best = 0LL;
for (long long n3 = 0; n3 <= std::min(m / 3, (long long)c[2].size());
++n3) {
long long n1 = m - n3 * 3;
long long n2 = 0;
long long r = n1 / 2 + 1;
long long l = std::min(1LL, r);
auto price_of = [&](long long n1_arg, long long n2_arg,
long long n3_arg) {
return get_sum(1, n1_arg) + get_sum(2, n2_arg) + get_sum(3, n3_arg);
};
while (l < r) {
long long cur = l + (r - l) / 2;
auto val = get(1, n1 - cur * 2 + 1) + get(1, n1 - cur * 2) -
get(2, n2 + cur - 1);
if (val < 0)
l = cur + 1;
else
r = cur;
}
l = std::max(0LL, l - 1);
n1 -= l * 2;
n2 += l;
best = std::max(best, price_of(n1, n2, n3));
}
out << best << '\n';
}
};
void solve(std::istream& in, std::ostream& out) {
out << std::setprecision(12);
Solution solution;
solution.solve(in, out);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
istream& in = cin;
ostream& out = cout;
solve(in, out);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long r;
bool is_perfect_cube(long long n) {
r = (long long)round(cbrt(n));
if (r * r * r == n) return true;
return false;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
int a, b;
scanf("%d %d", &a, &b);
if (is_perfect_cube((long long)a * b)) {
if (((long long)a + b) % r == 0)
printf("Yes\n");
else
printf("No\n");
} else
printf("No\n");
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
while (n--) {
long long x, y, z, w;
scanf("%I64d%I64d", &x, &y);
z = x * y;
w = pow(z, 1.0 / 3) + 0.5;
if (w * w * w == z && x % w == 0 && y % w == 0)
puts("Yes");
else
puts("No");
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int vec[1000005];
map<long long, int> M;
int main() {
int n;
cin >> n;
for (int i = 2; i <= 1000000; i++) {
if (vec[i]) continue;
for (int j = i; j <= 1000000; j += i) vec[j] = i;
}
for (int i = 1; i <= 1000000; i++) {
M[1LL * i * i * i] = i;
}
for (int i = 0; i < n; i++) {
long long a, b;
scanf("%lld%lld", &a, &b);
long long c = a * b;
bool bad = 0;
if (M.find(c) == M.end()) {
bad = 1;
} else {
int x = M[c];
while (x != 1) {
int y = vec[x];
{
int A = 0, B = 0;
while (a % y == 0) {
a /= y;
A++;
}
while (b % y == 0) {
b /= y;
B++;
}
while (x % y == 0) x /= y;
if (A + B > 0) {
if ((A + B) % 3 == 0 && min(A, B) * 2 >= max(A, B))
;
else
bad = 1;
}
}
}
}
puts(bad ? "No" : "Yes");
}
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const long long M = (long long)1e9 + 7;
int Int() {
int x;
scanf("%d", &x);
return x;
}
long long Long() {
long long x;
scanf("%lld", &x);
return x;
}
void Char(char *ch) { scanf("%s", ch); }
template <typename t>
inline t abs(t a) {
if (a >= 0) return a;
return -a;
}
template <typename T>
inline T modpow(T b, T p, T Mod) {
T res = 1;
while (p) {
if (p & 1) {
res *= b;
res %= Mod;
}
b *= b;
b %= Mod;
p >>= 1;
}
return res;
}
template <typename T>
inline T ModInv(T b, T Mod) {
return modpow(b, Mod - 2, Mod);
}
template <typename T>
inline T pwr(T b, T p) {
T res = 1;
while (p) {
if (p & 1) {
res *= b;
}
b *= b;
p >>= 1;
}
return res;
}
template <typename T>
inline T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
template <typename T>
inline void Int(T &n) {
n = 0;
int f = 1;
register int ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') f = -1;
for (; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch - '0';
n = n * f;
}
template <typename T, typename U>
inline void Int(T &a, U &b) {
Int(a);
Int(b);
}
template <typename T, typename U, typename W>
inline void Int(T &a, U &b, W &c) {
Int(a, b);
Int(c);
}
template <typename T, typename U, typename W, typename X>
inline void Int(T &a, U &b, W &c, X &d) {
Int(a, b, c);
Int(d);
}
vector<int> Unique(vector<int> v) {
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
return v;
}
vector<string> split(const string &s, char c) {
vector<string> v;
stringstream ss(s);
string x;
while (getline(ss, x, c)) v.emplace_back(x);
return move(v);
}
void err(vector<string>::iterator it) {}
template <typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
cout << it->substr((*it)[0] == ' ', it->length()) << " = " << a << " ";
err(++it, args...);
}
int main() {
int t = Int();
while (t--) {
long long n = Long(), m = Long();
if ((n == 1 and m != 1) or (n != 1 and m == 1)) {
puts("No");
continue;
}
long long nm = n * m;
long long l = 1, r = 1000000LL;
int f = 0;
while (l <= r) {
long long mid = (l + r) >> 1;
long long d = mid * mid * mid;
if (d < nm) {
l = mid + 1;
} else if (d > nm) {
r = mid - 1;
} else {
f = 1;
break;
}
}
if (f) l = (l + r) >> 1;
if (l * l * l == nm and (n % l == 0) and (m % l == 0)) {
puts("Yes");
} else {
puts("No");
}
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
void testCase() {
int64_t a, b;
cin >> a >> b;
int64_t crt = cbrtl(a * b);
int64_t have = crt * crt * crt;
if ((have == (a * b)) && a % crt == 0 && b % crt == 0) {
cout << "YES" << '\n';
} else {
cout << "NO" << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int64_t t;
cin >> t;
while (t--) testCase();
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long cubeRoot(long long n) {
long long l = 0, r = 1001010, m;
while (l != r) {
m = (l + r + 1) / 2;
if (m * m * m > n) {
r = m - 1;
} else {
l = m;
}
}
return l;
}
int main() {
long long c, a, b, n, i, j, k, flag = 0, x, sum = 0, sum2 = 0, sr;
cin >> n;
for (i = 0; i < n; i++) {
scanf("%I64d %I64d", &a, &b);
c = cubeRoot(a * b);
if ((c * c * c) == (a * b) && a % c == 0 && b % c == 0) {
printf("Yes\n");
} else {
printf("No\n");
}
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
map<long long, int> mp;
for (long long i = 1; i <= 1e6; ++i) mp[i * i * i] = i;
int t;
cin >> t;
while (t--) {
long long a, b;
scanf("%lld %lld", &a, &b);
if (mp.count(a * b) and a % mp[a * b] == 0 and b % mp[a * b] == 0)
printf("YeS\n");
else
printf("nO\n");
}
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
long long a, b;
scanf("%lld%lld", &a, &b);
long long l = 1, r = 1000000, x = -1;
while (l <= r) {
long long mid = (l + r) / 2;
if (mid * mid * mid > a * b)
r = mid - 1;
else if (mid * mid * mid < a * b)
l = mid + 1;
else {
x = mid;
break;
}
}
if (x == -1 || a * a % b != 0 || b * b % a != 0) {
printf("No\n");
continue;
}
printf("Yes\n");
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int e = 2 * 1e6 + 7;
long long a[e];
long long bs(long long x) {
long long l = 0, h = 1000005;
while (l != h) {
long long m = (l + h + 1) / 2;
if (m * m * m > x) {
h = m - 1;
} else {
l = m;
}
}
return l;
}
int main() {
int t;
cin >> t;
while (t--) {
long long a, b;
scanf("%I64d %I64d", &a, &b);
long long x = bs(a * b);
if (x * x * x != a * b)
puts("NO");
else if (a % x == 0 && b % x == 0)
puts("YES");
else
puts("NO");
}
}
| 7 | CPP |
import sys,os,io
from sys import stdin
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_left , bisect_right
import math
def ii():
return int(input())
def li():
return list(map(int,input().split()))
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r") ; sys.stdout = open("output.txt","w")
else:
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
no = "No"
yes = "Yes"
def solve():
a,b = li()
x = (pow(a*b,1/3))
x=round(x)
if x*x*x==a*b and a%x==b%x==0:
print(yes)
else:
print(no)
t = 1
t = int(input())
for _ in range(t):
solve()
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int N;
int gcd(int x, int y) {
while (y) {
x %= y;
swap(x, y);
}
return x;
}
int cube(int x) { return x * x * x; }
int iscube(int x) {
int y = 0;
for (int l = 10; l >= 0; l--) {
if (cube(y + (1 << l)) <= x) {
y += 1 << l;
}
}
return cube(y) == x;
}
int main() {
scanf("%d", &N);
for (int i = 0, x, y; i < N; i++) {
scanf("%d%d", &x, &y);
int g = gcd(x, y);
int a = x / g, b = y / g;
if (g % (1ll * a * b) == 0 && iscube(g / a / b)) {
puts("Yes");
} else {
puts("No");
}
}
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long check(long long p) { return p * p * p; }
long long res;
bool binary_search(long long l, long long r, long long q) {
long long mid = (l + r) / 2;
while (l <= r) {
mid = (l + r) / 2;
long long c = check(mid);
if (c > q) {
r = mid - 1;
} else if (c < q) {
l = mid + 1;
} else {
res = mid;
return true;
}
}
return false;
}
long long gcd(long long a, long long b) {
long long c = a % b;
while (c) {
a = b;
b = c;
c = a % b;
}
return b;
}
int main() {
long long n;
scanf("%lld", &n);
while (n--) {
long long a, b;
scanf("%lld %lld", &a, &b);
long long mn = min(a, b);
long long mx = max(a, b);
if (mn * mn < mx) {
puts("No");
continue;
}
if (binary_search(1, 1e6, a * b)) {
if (a % res == 0 && b % res == 0) {
puts("Yes");
} else {
puts("No");
}
} else {
puts("No");
}
}
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
long long cube(long long c) {
long long l = 1, r = 1e6, mid;
while (l < r) {
mid = (l + r) / 2;
if ((mid * mid * mid) < c)
l = mid + 1;
else
r = mid;
}
return l;
}
int main() {
int n;
scanf("%d", &n);
for (int t = 0; t < n; t++) {
long long a, b;
scanf("%lld%lld", &a, &b);
long long c = cube(a * b);
((void)0);
if (c * c * c != a * b) {
printf("No\n");
continue;
}
((void)0);
if (a % c == 0 and b % c == 0)
printf("Yes\n");
else
printf("No\n");
}
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
ll a, b;
cin >> a >> b;
ll x = round(pow(a * b, 1.0 / 3.0));
if (pow(x, 3) == a * b && a % x == 0 && b % x == 0)
cout << "YES\n";
else
cout << "NO\n";
}
}
| 7 | CPP |
#include <bits/stdc++.h>
#pragma 03
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimization("unroll-loops")
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long t;
cin >> t;
while (t--) {
long long a, b;
cin >> a >> b;
long long k = floor(cbrt(a * b));
if (k * k * k != a * b)
cout << "No\n";
else if (a % k == 0 && b % k == 0)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int T;
long long a, b, x;
int Calc(int x, int a) {
int cnt = 0;
for (; a % x == 0; a /= x, cnt++)
;
return cnt;
}
bool judge(int x, int a, int b) {
int p = Calc(x, a), q = Calc(x, b);
return p + p >= q && q + q >= p;
}
int main() {
for (cin >> T; T--;) {
scanf("%lld%lld", &a, &b), x = a * b;
long long p = pow(x, 1. / 3);
if ((p + 1) * (p + 1) * (p + 1) == x) p = p + 1;
if (p * p * p != x) {
puts("No");
continue;
}
bool res = 1;
for (long long i = 2; i * i <= p; i++)
if (p % i == 0) {
for (; p % i == 0; p /= i)
;
if (!judge(i, a, b)) {
res = 0;
break;
}
}
if (res && p > 1 && !judge(p, a, b)) res = 0;
puts(res ? "Yes" : "No");
}
}
| 7 | CPP |
import sys
input = sys.stdin.readline
print = sys.stdout.write
cbrt = {i**3:i for i in range(1001)}
n = int(input())
all_res = []
for _ in range(n):
a, b = map(int, input().split())
if a == b:
all_res.append('Yes' if a in cbrt else 'No')
continue
if a > b:
a, b = b, a
r = cbrt.get(a * a // b, 0)
if r == 0 or a % (r * r) > 0:
all_res.append('No')
continue
y = a //(r * r)
if r * r * y == a and r * y * y == b:
all_res.append('Yes')
else:
all_res.append('No')
print('\n'.join(all_res))
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long int inf = 1e9 + 7;
const long long int modc = 998244353;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
while (n--) {
long long int a, b;
cin >> a >> b;
long long int prod = a * b, cb = round(cbrt(prod));
if (cb * cb * cb == prod && a % cb == 0 && b % cb == 0)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
const int maxh = 1e5 + 10, maxn = 350010;
using namespace std;
long long n, m, k, ps, pr[maxh];
vector<long long> p;
map<pair<int, int>, int> mp;
void sieve() {
for (long long i = 2; i < maxh; i++) {
if (pr[i]) continue;
p.push_back(i);
for (long long j = i * i; j < maxh; j += i) {
pr[j] = 1;
}
}
ps = p.size();
}
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
bool solve(long long a, long long b) {
if (mp[make_pair(a, b)]) return mp[make_pair(a, b)] - 1;
int c = gcd(a, b);
for (long long i = 0; p[i] * p[i] <= c; i++) {
if (c % p[i] == 0) {
int x = 0, y = 0;
while (c % p[i] == 0) c /= p[i];
while (a % p[i] == 0) a /= p[i], x++;
while (b % p[i] == 0) b /= p[i], y++;
if (2 * x - y < 0 || 2 * y - x < 0) return 0;
if ((2 * x - y) % 3 || (2 * y - x) % 3) return 0;
if (a > b) swap(a, b);
if (a * a == b) return 1;
} else if (a % p[i] == 0 || b % p[i] == 0)
return 0;
}
if (c > 1) {
int i = c;
int x = 0, y = 0;
while (a % i == 0) a /= i, x++;
while (b % i == 0) b /= i, y++;
if (2 * x - y < 0 || 2 * y - x < 0) return 0;
if ((2 * x - y) % 3 || (2 * y - x) % 3) return 0;
}
if (a > 1 || b > 1) return 0;
return 1;
}
int main() {
sieve();
scanf("%I64d", &n);
for (int i = 0; i < n; i++) {
long long a, b;
scanf("%I64d %I64d", &a, &b);
int as = solve(a, b);
mp[make_pair(a, b)] = as + 1;
if (as)
printf("Yes\n");
else
printf("NO\n");
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
map<long long, int> mp;
for (long long i = 1; i <= 1e6; ++i) mp[i * i * i] = i;
int t;
cin >> t;
while (t--) {
long long a, b;
cin >> a >> b;
if (mp.count(a * b) and a % mp[a * b] == 0 and b % mp[a * b] == 0)
cout << "YeS\n";
else
cout << "nO\n";
}
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long int Cube_Root(long long int x) {
long long int l = 1, r = 1000005;
while (l < r) {
long long int mid = (l + r) / 2;
if (mid * mid * mid >= x) {
r = mid;
} else {
l = mid + 1;
}
}
if (l * l * l > x) {
return l - 1;
} else {
return l;
}
}
int main() {
int n;
scanf("%d", &n);
while (n--) {
long long int a, b, temp;
scanf("%lld%lld", &a, &b);
temp = Cube_Root(a * b);
if ((temp * temp * temp == a * b) && (a % temp == 0) && (b % temp == 0)) {
printf("Yes\n");
} else {
printf("No\n");
}
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
int main() {
int T;
scanf("%d", &T);
while (T--) {
long long a, b;
scanf("%I64d%I64d", &a, &b);
long long tmp = ceil(cbrt(a * b));
puts(tmp * tmp * tmp == a * b && a % tmp == 0 && b % tmp == 0 ? "Yes"
: "No");
}
return 0;
}
| 7 | CPP |