problem_id
stringlengths 6
6
| language
stringclasses 2
values | original_status
stringclasses 3
values | original_src
stringlengths 19
243k
| changed_src
stringlengths 19
243k
| change
stringclasses 3
values | i1
int64 0
8.44k
| i2
int64 0
8.44k
| j1
int64 0
8.44k
| j2
int64 0
8.44k
| error
stringclasses 270
values | stderr
stringlengths 0
226k
|
---|---|---|---|---|---|---|---|---|---|---|---|
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int n, W;
int w[100], v[100];
ll dp[101][100001];
int main() {
cin >> n >> W;
for (int i = 0; i < n; i++)
cin >> w[i] >> v[i];
memset(dp, 0, sizeof(dp));
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= W; j++) {
if (j < w[i])
dp[i + 1][j] = dp[i][j];
else
dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
}
}
cout << dp[n][W] << "\n";
return 0;
}
|
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int n, W;
int w[100], v[100];
ll dp[101][100001];
int main() {
cin >> n >> W;
for (int i = 0; i < n; i++)
cin >> w[i] >> v[i];
memset(dp, 0, sizeof(dp));
for (int i = 0; i < n; i++) {
for (int j = 0; j <= W; j++) {
if (j < w[i])
dp[i + 1][j] = dp[i][j];
else
dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
}
}
cout << dp[n][W] << "\n";
return 0;
}
|
replace
| 13 | 14 | 13 | 14 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define rep(i, m) for (long long i = 0; i < m; i++)
#define per(i, m) for (long long i = m - 1; i >= 0; i--)
#define FOR(i, n, m) for (long long i = n; i < m; i++)
#define ROF(i, n, m) for (long long i = m - 1; i >= n; i--)
#define SORT(v, n) \
do { \
sort(v, v + n); \
reverse(v, v + n); \
} while (0)
#define all(x) (x).begin(), (x).end()
#define EPS (1e-7)
#define INF (1e18)
#define PI (acos(-1))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl;
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
typedef pair<ll, ll> LP;
ll POW(ll x, ll n) {
if (n == 0)
return 1;
if (n % 2 == 0)
return POW(x * x, n / 2) % MOD;
return x % MOD * POW(x, n - 1) % MOD;
}
ll POW2(ll x, ll n) {
if (n == 0)
return 1;
if (n % 2 == 0)
return POW2(x * x, n / 2);
return x * POW2(x, n - 1);
}
ll POW3(ll x, ll n, ll m) {
x %= m;
if (n == 0)
return 1;
if (n % 2 == 0)
return POW3(x * x, n / 2, m) % m;
return x * POW3(x, n - 1, m) % m;
}
ll gcd(ll u, ll v) {
ll r;
while (0 != v) {
r = u % v;
u = v;
v = r;
}
return u;
}
ll lcm(ll u, ll v) { return u * v / gcd(u, v); }
ll KAI(ll m) {
if (m < 0)
return 0;
if (m == 0)
return 1;
return m * KAI(m - 1) % MOD;
}
ll KAI2(ll m) {
if (m < 0)
return 0;
if (m == 0)
return 1;
return m * KAI2(m - 1);
}
ll extGCD(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll d = extGCD(b, a % b, y, x);
y -= a / b * x;
return d;
}
inline ll mod(ll a, ll m) { return (a % m + m) % m; }
ll modinv(ll a) {
ll x, y;
extGCD(a, MOD, x, y);
return mod(x, MOD);
}
ll COM(ll m, ll n) {
if (m < n)
return 0;
if (n < 0)
return 0;
if (n == 0)
return 1;
if (m == n)
return 1;
return KAI(m) % MOD * modinv(KAI(n) % MOD * KAI(m - n) % MOD) % MOD;
}
ll COM2(ll m, ll n) {
if (m < n)
return 0;
if (n < 0)
return 0;
if (n == 0)
return 1;
if (m == n)
return 1;
return KAI2(m) / KAI2(n) / KAI2(m - n);
}
ll DEC(ll x, ll m, ll n) // xのm進数でのx^nの位の値
{
return x % POW2(m, n + 1) / POW2(m, n);
}
ll keta(ll x, ll n) // xのn進数での桁数
{
if (x == 0)
return 0;
return keta(x / n, n) + 1;
}
ll DIV(ll x, ll n) // x!のnで割り切れる回数
{
if (x == 0)
return 0;
return x / n + DIV(x / n, n);
}
ll ORD(ll x, ll n) // xのnで割り切れる回数
{
if (x == 0)
return INF;
if (x % n != 0)
return 0;
return 1 + ORD(x / n, n);
}
ll SGS(ll x, ll y, ll m) // 1+x+…+x^(y-1)をmで割った余り
{
if (y == 0)
return 0;
if (y % 2 == 0) {
return (1 + POW3(x, y / 2, m)) * SGS(x, y / 2, m) % m;
}
return (1 + x * SGS(x, y - 1, m)) % m;
}
ll SSGS(ll x, ll y, ll m) // Σ[k=1→y](1+x+…+x^(k-1))をmで割った余り
{
if (y == 0)
return 0;
if (y == 1)
return 1;
if (y % 2 == 0) {
return (SSGS(x, y / 2, m) * (POW3(x, y / 2, m) + 1) % m +
SGS(x, y / 2, m) * y / 2 % m) %
m;
}
return (SSGS(x, y - 1, m) * x % m + y) % m;
}
struct UnionFind {
vector<int> par;
vector<int> sizes;
UnionFind(int n) : par(n), sizes(n, 1) { rep(i, n) par[i] = i; }
int find(int x) {
if (x == par[x])
return x;
return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (sizes[x] < sizes[y])
swap(x, y);
par[y] = x;
sizes[x] += sizes[y];
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return sizes[find(x)]; }
};
int main() {
ll n, w, u[110], v[110], dp[110][2100], ans = 0;
cin >> n >> w;
rep(i, n) cin >> u[i] >> v[i];
rep(i, n) rep(j, w + 1) dp[i][j] = 0;
rep(i, n) {
rep(j, w + 1) {
dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]);
dp[i + 1][j + u[i]] = max(dp[i][j] + v[i], dp[i + 1][j + u[i]]);
}
}
rep(i, w + 1) ans = max(ans, dp[n][i]);
printf("%lld", ans);
}
|
#include <bits/stdc++.h>
#define rep(i, m) for (long long i = 0; i < m; i++)
#define per(i, m) for (long long i = m - 1; i >= 0; i--)
#define FOR(i, n, m) for (long long i = n; i < m; i++)
#define ROF(i, n, m) for (long long i = m - 1; i >= n; i--)
#define SORT(v, n) \
do { \
sort(v, v + n); \
reverse(v, v + n); \
} while (0)
#define all(x) (x).begin(), (x).end()
#define EPS (1e-7)
#define INF (1e18)
#define PI (acos(-1))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl;
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
typedef pair<ll, ll> LP;
ll POW(ll x, ll n) {
if (n == 0)
return 1;
if (n % 2 == 0)
return POW(x * x, n / 2) % MOD;
return x % MOD * POW(x, n - 1) % MOD;
}
ll POW2(ll x, ll n) {
if (n == 0)
return 1;
if (n % 2 == 0)
return POW2(x * x, n / 2);
return x * POW2(x, n - 1);
}
ll POW3(ll x, ll n, ll m) {
x %= m;
if (n == 0)
return 1;
if (n % 2 == 0)
return POW3(x * x, n / 2, m) % m;
return x * POW3(x, n - 1, m) % m;
}
ll gcd(ll u, ll v) {
ll r;
while (0 != v) {
r = u % v;
u = v;
v = r;
}
return u;
}
ll lcm(ll u, ll v) { return u * v / gcd(u, v); }
ll KAI(ll m) {
if (m < 0)
return 0;
if (m == 0)
return 1;
return m * KAI(m - 1) % MOD;
}
ll KAI2(ll m) {
if (m < 0)
return 0;
if (m == 0)
return 1;
return m * KAI2(m - 1);
}
ll extGCD(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll d = extGCD(b, a % b, y, x);
y -= a / b * x;
return d;
}
inline ll mod(ll a, ll m) { return (a % m + m) % m; }
ll modinv(ll a) {
ll x, y;
extGCD(a, MOD, x, y);
return mod(x, MOD);
}
ll COM(ll m, ll n) {
if (m < n)
return 0;
if (n < 0)
return 0;
if (n == 0)
return 1;
if (m == n)
return 1;
return KAI(m) % MOD * modinv(KAI(n) % MOD * KAI(m - n) % MOD) % MOD;
}
ll COM2(ll m, ll n) {
if (m < n)
return 0;
if (n < 0)
return 0;
if (n == 0)
return 1;
if (m == n)
return 1;
return KAI2(m) / KAI2(n) / KAI2(m - n);
}
ll DEC(ll x, ll m, ll n) // xのm進数でのx^nの位の値
{
return x % POW2(m, n + 1) / POW2(m, n);
}
ll keta(ll x, ll n) // xのn進数での桁数
{
if (x == 0)
return 0;
return keta(x / n, n) + 1;
}
ll DIV(ll x, ll n) // x!のnで割り切れる回数
{
if (x == 0)
return 0;
return x / n + DIV(x / n, n);
}
ll ORD(ll x, ll n) // xのnで割り切れる回数
{
if (x == 0)
return INF;
if (x % n != 0)
return 0;
return 1 + ORD(x / n, n);
}
ll SGS(ll x, ll y, ll m) // 1+x+…+x^(y-1)をmで割った余り
{
if (y == 0)
return 0;
if (y % 2 == 0) {
return (1 + POW3(x, y / 2, m)) * SGS(x, y / 2, m) % m;
}
return (1 + x * SGS(x, y - 1, m)) % m;
}
ll SSGS(ll x, ll y, ll m) // Σ[k=1→y](1+x+…+x^(k-1))をmで割った余り
{
if (y == 0)
return 0;
if (y == 1)
return 1;
if (y % 2 == 0) {
return (SSGS(x, y / 2, m) * (POW3(x, y / 2, m) + 1) % m +
SGS(x, y / 2, m) * y / 2 % m) %
m;
}
return (SSGS(x, y - 1, m) * x % m + y) % m;
}
struct UnionFind {
vector<int> par;
vector<int> sizes;
UnionFind(int n) : par(n), sizes(n, 1) { rep(i, n) par[i] = i; }
int find(int x) {
if (x == par[x])
return x;
return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (sizes[x] < sizes[y])
swap(x, y);
par[y] = x;
sizes[x] += sizes[y];
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return sizes[find(x)]; }
};
int main() {
ll n, w, u[110], v[110], dp[110][210000], ans = 0;
cin >> n >> w;
rep(i, n) cin >> u[i] >> v[i];
rep(i, n) rep(j, w + 1) dp[i][j] = 0;
rep(i, n) {
rep(j, w + 1) {
dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]);
dp[i + 1][j + u[i]] = max(dp[i][j] + v[i], dp[i + 1][j + u[i]]);
}
}
rep(i, w + 1) ans = max(ans, dp[n][i]);
printf("%lld", ans);
}
|
replace
| 195 | 196 | 195 | 196 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mkp make_pair
#define int long long
#define fori(j, n) for (int i = j; i < n; i++)
#define forb(n, j) for (int i = n - 1; i >= 0; i--)
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define endl "\n"
#define PI 3.141592653589793
#define M 1000000007
using namespace std;
int fast(int a, int b) {
int ans = 1;
a = a % M;
if (a == 0)
return 0;
while (b > 0) {
if (b & 1) {
ans = (ans * a) % M;
}
b >>= 1;
a = (a * a) % M;
}
}
int N;
int v[105];
int w[105];
int dp[110][110];
int cal(int i, int W) {
if (W < 0)
return INT_MIN;
if (i < 0 || w == 0)
return 0;
if (dp[i][W] != -1)
return dp[i][W];
int notinclude = cal(i - 1, W);
int include = v[i] + cal(i - 1, W - w[i]);
return dp[i][W] = max(include, notinclude);
}
int32_t main() {
IOS;
int W;
memset(dp, -1, sizeof(dp));
cin >> N >> W;
fori(0, N) { cin >> w[i] >> v[i]; }
cout << cal(N - 1, W);
return 0;
}
|
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mkp make_pair
#define int long long
#define fori(j, n) for (int i = j; i < n; i++)
#define forb(n, j) for (int i = n - 1; i >= 0; i--)
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define endl "\n"
#define PI 3.141592653589793
#define M 1000000007
using namespace std;
int fast(int a, int b) {
int ans = 1;
a = a % M;
if (a == 0)
return 0;
while (b > 0) {
if (b & 1) {
ans = (ans * a) % M;
}
b >>= 1;
a = (a * a) % M;
}
}
int N;
int v[105];
int w[105];
int dp[110][100001];
int cal(int i, int W) {
if (W < 0)
return INT_MIN;
if (i < 0 || w == 0)
return 0;
if (dp[i][W] != -1)
return dp[i][W];
int notinclude = cal(i - 1, W);
int include = v[i] + cal(i - 1, W - w[i]);
return dp[i][W] = max(include, notinclude);
}
int32_t main() {
IOS;
int W;
memset(dp, -1, sizeof(dp));
cin >> N >> W;
fori(0, N) { cin >> w[i] >> v[i]; }
cout << cal(N - 1, W);
return 0;
}
|
replace
| 32 | 33 | 32 | 33 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct Fast {
Fast() {
std::cin.tie(0);
ios::sync_with_stdio(false);
}
} fast;
/* short */
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define ALL(v) begin(v), end(v)
/* REPmacro */
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define RFOR(i, s, n) for (int i = (s); i >= (n); i--)
#define rep(i, n) FOR(i, 0, n)
#define repi(i, n) FOR(i, 1, n + 1)
#define rrep(i, n) RFOR(i, n - 1, 0)
#define rrepi(i, n) RFOR(i, n, 1)
/* debug */
#define pp(v) cerr << #v "=" << (v) << endl;
/* alias */
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using vs = vector<string>;
using Graph = vvi;
template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;
/* iostream */
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (auto &v : vec)
is >> v;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "[";
for (auto v : vec)
os << v << ",";
os << "]";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) {
os << "deq[";
for (auto v : vec)
os << v << ",";
os << "]";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) {
os << "{";
for (auto v : vec)
os << v << ",";
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) {
os << "{";
for (auto v : vec)
os << v << ",";
os << "}";
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &pa) {
os << "(" << pa.first << "," << pa.second << ")";
return os;
}
template <typename TK, typename TV>
ostream &operator<<(ostream &os, const map<TK, TV> &mp) {
os << "{";
for (auto v : mp)
os << v.first << "=>" << v.second << ",";
os << "}";
return os;
}
/* const */
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1},
dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
/* func */
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define TIME system("date +%M:%S.%N")
inline bool inside(int y, int x, int H, int W) {
return y >= 0 && x >= 0 && y < H && x < W;
}
template <typename T> inline bool chmin(T &a, const T &b) {
if (a > b)
a = b;
return a > b;
}
template <typename T> inline bool chmax(T &a, const T &b) {
if (a < b)
a = b;
return a < b;
}
template <class T> T gcd(const T &a, const T &b) {
return a < b ? gcd(b, a) : b ? gcd(b, a % b) : a;
}
template <class T> T lcm(const T &a, const T &b) { return a / gcd(a, b) * b; }
template <class T> T div_ceil(const T &a, const T &b) {
return (a + b - 1) / b;
}
signed main() {
int n, W;
cin >> n >> W;
vvi dp(110, vi(10010));
repi(i, n) {
int w, v;
cin >> w >> v;
rep(j, W + 1) {
if (w <= j)
chmax(dp[i][j], dp[i - 1][j - w] + v);
chmax(dp[i][j], dp[i - 1][j]);
}
}
int ans = dp[n][W];
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct Fast {
Fast() {
std::cin.tie(0);
ios::sync_with_stdio(false);
}
} fast;
/* short */
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define ALL(v) begin(v), end(v)
/* REPmacro */
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define RFOR(i, s, n) for (int i = (s); i >= (n); i--)
#define rep(i, n) FOR(i, 0, n)
#define repi(i, n) FOR(i, 1, n + 1)
#define rrep(i, n) RFOR(i, n - 1, 0)
#define rrepi(i, n) RFOR(i, n, 1)
/* debug */
#define pp(v) cerr << #v "=" << (v) << endl;
/* alias */
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using vs = vector<string>;
using Graph = vvi;
template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;
/* iostream */
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (auto &v : vec)
is >> v;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "[";
for (auto v : vec)
os << v << ",";
os << "]";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) {
os << "deq[";
for (auto v : vec)
os << v << ",";
os << "]";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) {
os << "{";
for (auto v : vec)
os << v << ",";
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) {
os << "{";
for (auto v : vec)
os << v << ",";
os << "}";
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &pa) {
os << "(" << pa.first << "," << pa.second << ")";
return os;
}
template <typename TK, typename TV>
ostream &operator<<(ostream &os, const map<TK, TV> &mp) {
os << "{";
for (auto v : mp)
os << v.first << "=>" << v.second << ",";
os << "}";
return os;
}
/* const */
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1},
dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
/* func */
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define TIME system("date +%M:%S.%N")
inline bool inside(int y, int x, int H, int W) {
return y >= 0 && x >= 0 && y < H && x < W;
}
template <typename T> inline bool chmin(T &a, const T &b) {
if (a > b)
a = b;
return a > b;
}
template <typename T> inline bool chmax(T &a, const T &b) {
if (a < b)
a = b;
return a < b;
}
template <class T> T gcd(const T &a, const T &b) {
return a < b ? gcd(b, a) : b ? gcd(b, a % b) : a;
}
template <class T> T lcm(const T &a, const T &b) { return a / gcd(a, b) * b; }
template <class T> T div_ceil(const T &a, const T &b) {
return (a + b - 1) / b;
}
signed main() {
int n, W;
cin >> n >> W;
vvi dp(110, vi(100100));
repi(i, n) {
int w, v;
cin >> w >> v;
rep(j, W + 1) {
if (w <= j)
chmax(dp[i][j], dp[i - 1][j - w] + v);
chmax(dp[i][j], dp[i - 1][j]);
}
}
int ans = dp[n][W];
cout << ans << endl;
}
|
replace
| 123 | 124 | 123 | 124 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
const int MAX = 100005;
const long long mod = 1.0e9 + 7;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ll n, w, i, j, k;
cin >> n >> w;
vector<pair<ll, ll>> v;
for (i = 0; i < n; i++) {
cin >> j >> k;
v.push_back(make_pair(j, k));
}
sort(v.begin(), v.end());
ll dp[n + 1][w + 1];
memset(dp, 0, sizeof(dp));
for (i = 1; i <= n; i++) {
for (j = 1; j <= w; j++) {
if (j >= v[i - 1].first)
dp[i][j] =
max(dp[i - 1][j], v[i - 1].second + dp[i - 1][j - v[i - 1].first]);
else
dp[i][j] = dp[i - 1][j];
}
}
// cout<<dp[w]<<endl;
cout << dp[n][w] << endl;
// cout<<ans<<endl;
return 0;
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
const int MAX = 100005;
const long long mod = 1.0e9 + 7;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll n, w, i, j, k;
cin >> n >> w;
vector<pair<ll, ll>> v;
for (i = 0; i < n; i++) {
cin >> j >> k;
v.push_back(make_pair(j, k));
}
sort(v.begin(), v.end());
ll dp[n + 1][w + 1];
memset(dp, 0, sizeof(dp));
for (i = 1; i <= n; i++) {
for (j = 1; j <= w; j++) {
if (j >= v[i - 1].first)
dp[i][j] =
max(dp[i - 1][j], v[i - 1].second + dp[i - 1][j - v[i - 1].first]);
else
dp[i][j] = dp[i - 1][j];
}
}
// cout<<dp[w]<<endl;
cout << dp[n][w] << endl;
// cout<<ans<<endl;
return 0;
}
|
replace
| 15 | 19 | 15 | 16 |
-6
|
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef double db;
#define mp make_pair
#define pb push_back
#define pt(i) printf("%lld\n", (ll)i)
#define mp make_pair
#define ff first
#define ss second
#define pi acos(-1.0)
ll in() {
ll a;
scanf("%lld", &a);
return a;
}
db din() {
db a;
scanf("%lf", &a);
return a;
}
ll bigmod(ll b, ll p, ll md) {
if (p == 0)
return 1;
if (p % 2 == 1) {
return ((b % md) * bigmod(b, p - 1, md)) % md;
} else {
ll y = bigmod(b, p / 2, md);
return (y * y) % md;
}
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
long long getRandom(long long a, long long b) {
long long ret = (long long)rand() * (long long)rand();
ret %= (b - a + 1);
ret += a;
return ret;
}
ll dp[105][100005], n, w, wi[105], vvi[105];
ll solve(ll pos, ll wei) {
if (wei > w)
return -1 * INT_MAX;
if (dp[pos][wei] != -1)
return dp[pos][wei];
return dp[pos][wei] =
max(vvi[pos] + solve(pos + 1, wei + wi[pos]), solve(pos + 1, wei));
}
int main() {
// freopen("in.c","r",stdin);
// freopen("out.c","w",stdout);
n = in(), w = in();
for (int i = 0; i < n; i++)
wi[i] = in(), vvi[i] = in();
memset(dp, -1, sizeof dp);
pt(solve(0, 0));
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef double db;
#define mp make_pair
#define pb push_back
#define pt(i) printf("%lld\n", (ll)i)
#define mp make_pair
#define ff first
#define ss second
#define pi acos(-1.0)
ll in() {
ll a;
scanf("%lld", &a);
return a;
}
db din() {
db a;
scanf("%lf", &a);
return a;
}
ll bigmod(ll b, ll p, ll md) {
if (p == 0)
return 1;
if (p % 2 == 1) {
return ((b % md) * bigmod(b, p - 1, md)) % md;
} else {
ll y = bigmod(b, p / 2, md);
return (y * y) % md;
}
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
long long getRandom(long long a, long long b) {
long long ret = (long long)rand() * (long long)rand();
ret %= (b - a + 1);
ret += a;
return ret;
}
ll dp[105][100005], n, w, wi[105], vvi[105];
ll solve(ll pos, ll wei) {
if (wei > w)
return -1 * INT_MAX;
if (pos == n)
return 0;
if (dp[pos][wei] != -1)
return dp[pos][wei];
return dp[pos][wei] =
max(vvi[pos] + solve(pos + 1, wei + wi[pos]), solve(pos + 1, wei));
}
int main() {
// freopen("in.c","r",stdin);
// freopen("out.c","w",stdout);
n = in(), w = in();
for (int i = 0; i < n; i++)
wi[i] = in(), vvi[i] = in();
memset(dp, -1, sizeof dp);
pt(solve(0, 0));
}
|
insert
| 57 | 57 | 57 | 59 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
#define ll long long
#define endl '\n'
const ll mod = 1000000007;
ll dp[101][101];
pair<int, int> p[101];
int x, w;
ll f(int i, int w) {
if (w == 0 || i == x)
return 0;
else if (p[i].first > w)
return dp[i][w] = f(i + 1, w);
else if (dp[i][w] != -1)
return dp[i][w];
return dp[i][w] = max(p[i].second + f(i + 1, w - p[i].first), f(i + 1, w));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> x >> w;
for (int i = 0; i < x; i++)
cin >> p[i].first >> p[i].second;
memset(dp, -1, sizeof(dp));
cout << f(0, w) << endl;
/*for (int i = 0; i < x; i++) {
for (int j = 0; j <= w; j++) {
cout << dp[i][j] << " ";
}
cout << endl;
}*/
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
#define ll long long
#define endl '\n'
const ll mod = 1000000007;
ll dp[101][(int)1e5 + 1];
pair<int, int> p[101];
int x, w;
ll f(int i, int w) {
if (w == 0 || i == x)
return 0;
else if (p[i].first > w)
return dp[i][w] = f(i + 1, w);
else if (dp[i][w] != -1)
return dp[i][w];
return dp[i][w] = max(p[i].second + f(i + 1, w - p[i].first), f(i + 1, w));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> x >> w;
for (int i = 0; i < x; i++)
cin >> p[i].first >> p[i].second;
memset(dp, -1, sizeof(dp));
cout << f(0, w) << endl;
/*for (int i = 0; i < x; i++) {
for (int j = 0; j <= w; j++) {
cout << dp[i][j] << " ";
}
cout << endl;
}*/
return 0;
}
|
replace
| 24 | 25 | 24 | 25 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <climits>
#include <iostream>
using namespace std;
long long int dp[1000][1000] = {};
long long int knapsack(long long int wt[], long long int val[], long long int w,
long long int i) {
if (w < 0) {
return INT_MIN;
} else if (i == -1) {
return 0;
} else if (dp[w][i] != 0) {
return dp[w][i];
} else {
dp[w][i] = max(knapsack(wt, val, w - wt[i], i - 1) + val[i],
knapsack(wt, val, w, i - 1));
return dp[w][i];
}
}
int main() {
long long int n, i, w;
cin >> n >> w;
long long int wt[n], val[n];
for (i = 0; i < n; ++i) {
cin >> wt[i] >> val[i];
}
cout << knapsack(wt, val, w, n - 1) << endl;
return 0;
}
|
#include <climits>
#include <iostream>
using namespace std;
long long int dp[100001][101] = {};
long long int knapsack(long long int wt[], long long int val[], long long int w,
long long int i) {
if (w < 0) {
return INT_MIN;
} else if (i == -1) {
return 0;
} else if (dp[w][i] != 0) {
return dp[w][i];
} else {
dp[w][i] = max(knapsack(wt, val, w - wt[i], i - 1) + val[i],
knapsack(wt, val, w, i - 1));
return dp[w][i];
}
}
int main() {
long long int n, i, w;
cin >> n >> w;
long long int wt[n], val[n];
for (i = 0; i < n; ++i) {
cin >> wt[i] >> val[i];
}
cout << knapsack(wt, val, w, n - 1) << endl;
return 0;
}
|
replace
| 5 | 6 | 5 | 6 |
0
| |
p03163
|
C++
|
Runtime Error
|
// Problem : C. Chocolate Bunny
// Contest : Codeforces - Codeforces Round #669 (Div. 2)
// URL : https://codeforces.com/contest/1407/problem/C
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define vi vector<int>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define vs vector<string>
#define vpii vector<pair<int, int>>
#define pii pair<int, int>
#define mii map<int, int>
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define mkp make_pair
#define all(v) v.begin(), v.end()
#define input(a, b, n) \
for (i = b; i < n; i++) { \
cin >> a[i]; \
}
#define print(a) \
for (i = 0; i < a.size(); i++) { \
cout << a[i] << " "; \
} \
cout << endl;
#define trace1(a) \
; \
cout << "a=" << a << endl;
#define trace2(a, b) \
; \
cout << "a=" << a << " b=" << b << endl;
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
const int inf = 0x3f3f3f3f3f3f3f3f;
int power(int a, int b) {
int res = 1;
while (b != 0) {
if (b & 1) {
res *= a;
}
a = (a * a);
b = (b / 2);
}
return res;
}
vector<vector<int>> dp(1001, vector<int>(1001, -1));
int knapSack(int W, int wt[], int val[], int n) {
// Your code here
if (dp[W][n] != -1) {
return dp[W][n];
}
if (W == 0) {
dp[W][n] = 0;
return 0;
}
if (n == 0) {
dp[W][n] = 0;
return 0;
}
int x = -1, y = -1;
if (wt[n - 1] <= W) {
x = val[n - 1] + knapSack(W - wt[n - 1], wt, val, n - 1);
}
y = knapSack(W, wt, val, n - 1);
if (x != -1 && y != -1) {
dp[W][n] = max(x, y);
return max(x, y);
} else if (x != -1) {
dp[W][n] = x;
return x;
} else {
dp[W][n] = y;
return y;
}
}
int32_t main() {
// fast;
int t;
t = 1;
// cin>>t;
while (t--) {
int w, n;
cin >> n >> w;
int wt[n];
int val[n];
int i;
for (i = 0; i < n; i++) {
cin >> wt[i] >> val[i];
}
cout << knapSack(w, wt, val, n) << endl;
}
return 0;
}
|
// Problem : C. Chocolate Bunny
// Contest : Codeforces - Codeforces Round #669 (Div. 2)
// URL : https://codeforces.com/contest/1407/problem/C
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define vi vector<int>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define vs vector<string>
#define vpii vector<pair<int, int>>
#define pii pair<int, int>
#define mii map<int, int>
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define mkp make_pair
#define all(v) v.begin(), v.end()
#define input(a, b, n) \
for (i = b; i < n; i++) { \
cin >> a[i]; \
}
#define print(a) \
for (i = 0; i < a.size(); i++) { \
cout << a[i] << " "; \
} \
cout << endl;
#define trace1(a) \
; \
cout << "a=" << a << endl;
#define trace2(a, b) \
; \
cout << "a=" << a << " b=" << b << endl;
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
const int inf = 0x3f3f3f3f3f3f3f3f;
int power(int a, int b) {
int res = 1;
while (b != 0) {
if (b & 1) {
res *= a;
}
a = (a * a);
b = (b / 2);
}
return res;
}
vector<vector<int>> dp(100001, vector<int>(101, -1));
int knapSack(int W, int wt[], int val[], int n) {
// Your code here
if (dp[W][n] != -1) {
return dp[W][n];
}
if (W == 0) {
dp[W][n] = 0;
return 0;
}
if (n == 0) {
dp[W][n] = 0;
return 0;
}
int x = -1, y = -1;
if (wt[n - 1] <= W) {
x = val[n - 1] + knapSack(W - wt[n - 1], wt, val, n - 1);
}
y = knapSack(W, wt, val, n - 1);
if (x != -1 && y != -1) {
dp[W][n] = max(x, y);
return max(x, y);
} else if (x != -1) {
dp[W][n] = x;
return x;
} else {
dp[W][n] = y;
return y;
}
}
int32_t main() {
// fast;
int t;
t = 1;
// cin>>t;
while (t--) {
int w, n;
cin >> n >> w;
int wt[n];
int val[n];
int i;
for (i = 0; i < n; i++) {
cin >> wt[i] >> val[i];
}
cout << knapSack(w, wt, val, n) << endl;
}
return 0;
}
|
replace
| 56 | 57 | 56 | 57 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm> // sort
#include <iostream>
#include <map> // pair
#include <numeric> // accumulate(begin,end,初期値)
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
#define FALSE printf("false\n");
#define TRUE printf("true\n");
#define all(x) (x).begin(), (x).end()
#define print(x) cout << x << endl
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SUM(vec) accumulate(all(vec), 0)
const ll INF = 1e9 + 7;
// 各桁の和
template <typename T> T digit(T num) {
T sum = 0;
while (num) {
sum += num % 10;
num /= 10;
}
return sum;
}
template <typename T> T gcd(T a, T b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
template <typename T> T lcm(T a, T b) {
T g = gcd(a, b);
return a / g * b;
}
template <typename T> T power(T a, T b) {
T tmp = 1;
rep(i, b) { tmp *= a; }
return tmp;
}
int main() {
ll n, W;
cin >> n >> W;
vector<ll> w(n), v(n);
rep(i, n) cin >> w[i] >> v[i];
ll dp[100][100100];
rep(i, n) {
rep(j, W + 1) {
if (j >= w[i]) {
dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
} else {
dp[i + 1][j] = dp[i][j];
}
}
}
print(dp[n][W]);
return 0;
}
|
#include <algorithm> // sort
#include <iostream>
#include <map> // pair
#include <numeric> // accumulate(begin,end,初期値)
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
#define FALSE printf("false\n");
#define TRUE printf("true\n");
#define all(x) (x).begin(), (x).end()
#define print(x) cout << x << endl
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SUM(vec) accumulate(all(vec), 0)
const ll INF = 1e9 + 7;
// 各桁の和
template <typename T> T digit(T num) {
T sum = 0;
while (num) {
sum += num % 10;
num /= 10;
}
return sum;
}
template <typename T> T gcd(T a, T b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
template <typename T> T lcm(T a, T b) {
T g = gcd(a, b);
return a / g * b;
}
template <typename T> T power(T a, T b) {
T tmp = 1;
rep(i, b) { tmp *= a; }
return tmp;
}
int main() {
ll n, W;
cin >> n >> W;
vector<ll> w(n), v(n);
rep(i, n) cin >> w[i] >> v[i];
ll dp[110][100100];
rep(i, n) {
rep(j, W + 1) {
if (j >= w[i]) {
dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
} else {
dp[i + 1][j] = dp[i][j];
}
}
}
print(dp[n][W]);
return 0;
}
|
replace
| 53 | 54 | 53 | 54 |
-11
| |
p03163
|
C++
|
Runtime Error
|
#include <iostream>
#include <string>
#include <vector>
#define rep(i, x, n) for (int i = x; i < n; i++)
using namespace std;
const long long INF = 1LL << 60;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int N;
long long wv[110][2];
long long dp[110][100010];
int main() {
int N, W;
cin >> N >> W;
rep(i, 0, N) {
rep(r, 0, 2) { cin >> wv[i][r]; }
}
rep(i, 0, N) {
rep(sum, 0, W + 1) {
if (sum - wv[sum][0] >= 0) {
chmax(dp[i + 1][sum], dp[i][sum - wv[i][0]] + wv[i][1]);
}
chmax(dp[i + 1][sum], dp[i][sum]);
}
}
cout << dp[N][W] << endl;
}
|
#include <iostream>
#include <string>
#include <vector>
#define rep(i, x, n) for (int i = x; i < n; i++)
using namespace std;
const long long INF = 1LL << 60;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int N;
long long wv[110][2];
long long dp[110][100010];
int main() {
int N, W;
cin >> N >> W;
rep(i, 0, N) {
rep(r, 0, 2) { cin >> wv[i][r]; }
}
rep(i, 0, N) {
rep(sum, 0, W + 1) {
if (sum - wv[i][0] >= 0) {
chmax(dp[i + 1][sum], dp[i][sum - wv[i][0]] + wv[i][1]);
}
chmax(dp[i + 1][sum], dp[i][sum]);
}
}
cout << dp[N][W] << endl;
}
|
replace
| 33 | 34 | 33 | 34 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
long long int dp[100][100001] = {0};
int main() {
int N, W;
cin >> N >> W;
long long int w[2][100];
for (int i = 0; i < N; i++)
cin >> w[0][i] >> w[1][i];
for (int i = 0; i < N; i++) {
for (int sum = 0; sum <= W; sum++) {
if (sum - w[0][i] >= 0)
dp[i + 1][sum] = max(dp[i + 1][sum], dp[i][sum - w[0][i]] + w[1][i]);
dp[i + 1][sum] = max(dp[i + 1][sum], dp[i][sum]);
// cout << "dp[" << i+1 << "][" << sum << "]:" << dp[i+1][sum] << endl;
}
}
cout << dp[N][W] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int dp[101][100001] = {0};
int main() {
int N, W;
cin >> N >> W;
long long int w[2][100];
for (int i = 0; i < N; i++)
cin >> w[0][i] >> w[1][i];
for (int i = 0; i < N; i++) {
for (int sum = 0; sum <= W; sum++) {
if (sum - w[0][i] >= 0)
dp[i + 1][sum] = max(dp[i + 1][sum], dp[i][sum - w[0][i]] + w[1][i]);
dp[i + 1][sum] = max(dp[i + 1][sum], dp[i][sum]);
// cout << "dp[" << i+1 << "][" << sum << "]:" << dp[i+1][sum] << endl;
}
}
cout << dp[N][W] << endl;
return 0;
}
|
replace
| 2 | 3 | 2 | 3 |
0
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 100000000000
int n;
ll we, k;
ll dp[102][100003], w[100003], v[100003];
ll solve(int i, ll j) {
if (i < 0) {
return 0;
}
if (j - w[i] < 0)
return dp[i][j] = max(dp[i][j], solve(i - 1, j));
return dp[i][j] = max(solve(i - 1, j - w[i]) + v[i], solve(i - 1, j));
}
int main() {
int t = 1, tn = 1;
// scanf("%d",&t);
while (t--) {
scanf("%d%lld", &n, &we);
for (int i = 0; i < n; i++)
for (int j = 0; j <= we; j++)
dp[i][j] = -1;
for (int i = 0; i < n; i++)
scanf("%lld %lld", &w[i], &v[i]);
printf("%lld", solve(n - 1, we));
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 100000000000
int n;
ll we, k;
ll dp[102][100003], w[100003], v[100003];
ll solve(int i, ll j) {
if (i < 0) {
return 0;
}
if (dp[i][j] != -1)
return dp[i][j];
if (j - w[i] < 0)
return dp[i][j] = max(dp[i][j], solve(i - 1, j));
return dp[i][j] = max(solve(i - 1, j - w[i]) + v[i], solve(i - 1, j));
}
int main() {
int t = 1, tn = 1;
// scanf("%d",&t);
while (t--) {
scanf("%d%lld", &n, &we);
for (int i = 0; i < n; i++)
for (int j = 0; j <= we; j++)
dp[i][j] = -1;
for (int i = 0; i < n; i++)
scanf("%lld %lld", &w[i], &v[i]);
printf("%lld", solve(n - 1, we));
}
return 0;
}
|
insert
| 14 | 14 | 14 | 16 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
int N, W;
cin >> N >> W;
vector<vector<int>> dp(100000 + 10, vector<int>(100 + 10, 0));
int w[N], v[N];
for (int i = 0; i < N; i++)
cin >> w[i] >> v[i];
for (int i = 0; i < N; i++) {
for (int j = 0; j <= W; j++) {
if (j + w[i] <= W) {
dp[i + 1][j + w[i]] = max(dp[i][j] + v[i], dp[i + 1][j + w[i]]);
}
dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]);
}
}
cout << dp[N][W] << endl;
}
|
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
int N, W;
cin >> N >> W;
vector<vector<int>> dp(100 + 10, vector<int>(100000 + 100, 0));
int w[N], v[N];
for (int i = 0; i < N; i++)
cin >> w[i] >> v[i];
for (int i = 0; i < N; i++) {
for (int j = 0; j <= W; j++) {
if (j + w[i] <= W) {
dp[i + 1][j + w[i]] = max(dp[i][j] + v[i], dp[i + 1][j + w[i]]);
}
dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]);
}
}
cout << dp[N][W] << endl;
}
|
replace
| 7 | 8 | 7 | 8 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
long long int n, x, dp[1001][101], c, d, d1;
int main() {
cin >> n >> x;
int val[n + 1], wei[n + 1];
for (int i = 0; i < n; i++)
cin >> wei[i] >> val[i];
for (int i = 1; i <= n; i++)
dp[i][0] = 0;
for (int i = 0; i <= x; i++)
dp[0][i] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= x; j++) {
if (wei[i - 1] <= j) {
d1 = val[i - 1], c = dp[i - 1][j], d = dp[i - 1][j - wei[i - 1]];
dp[i][j] = max(c, d + d1);
} else
dp[i][j] = dp[i - 1][j];
// cout<<dp[i][j]<<" ";
}
// cout<<"\n";
}
cout << dp[n][x];
}
|
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
long long int n, x, dp[101][100001], c, d, d1;
int main() {
cin >> n >> x;
int val[n + 1], wei[n + 1];
for (int i = 0; i < n; i++)
cin >> wei[i] >> val[i];
for (int i = 1; i <= n; i++)
dp[i][0] = 0;
for (int i = 0; i <= x; i++)
dp[0][i] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= x; j++) {
if (wei[i - 1] <= j) {
d1 = val[i - 1], c = dp[i - 1][j], d = dp[i - 1][j - wei[i - 1]];
dp[i][j] = max(c, d + d1);
} else
dp[i][j] = dp[i - 1][j];
// cout<<dp[i][j]<<" ";
}
// cout<<"\n";
}
cout << dp[n][x];
}
|
replace
| 5 | 6 | 5 | 6 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
const int x = 101, y = 10001;
long long dp[x][y];
long long c[x], w[x], n, mx_w;
long long solve(int idx, int acc_w) {
if (idx == n)
return 0;
if (dp[idx][acc_w] != -1)
return dp[idx][acc_w];
if (w[idx] + acc_w <= mx_w)
return dp[idx][acc_w] = max(solve(idx + 1, acc_w),
c[idx] + solve(idx + 1, acc_w + w[idx]));
else
return dp[idx][acc_w] = solve(idx + 1, acc_w);
}
int main() {
memset(dp, -1, sizeof dp);
cin >> n >> mx_w;
for (int i = 0; i < n; i++)
cin >> w[i] >> c[i];
cout << solve(0, 0) << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int x = 101, y = 100001;
long long dp[x][y];
long long c[x], w[x], n, mx_w;
long long solve(int idx, int acc_w) {
if (idx == n)
return 0;
if (dp[idx][acc_w] != -1)
return dp[idx][acc_w];
if (w[idx] + acc_w <= mx_w)
return dp[idx][acc_w] = max(solve(idx + 1, acc_w),
c[idx] + solve(idx + 1, acc_w + w[idx]));
else
return dp[idx][acc_w] = solve(idx + 1, acc_w);
}
int main() {
memset(dp, -1, sizeof dp);
cin >> n >> mx_w;
for (int i = 0; i < n; i++)
cin >> w[i] >> c[i];
cout << solve(0, 0) << '\n';
return 0;
}
|
replace
| 3 | 4 | 3 | 4 |
0
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
#define x first
#define y second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define szz(x) (int)(x).size()
#define FOR(i, a, b) for (int i = (a), _for = (b); i < _for; i++)
#define REP(i, n) FOR(i, 0, (n))
#define _ << " " <<
const ll MOD = 1000000007ll;
const ll INF = 0x3f3f3f3f3f3f3f3f;
inline ll SUB(ll a, ll b) { return a - b >= 0ll ? a - b : a - b + MOD; }
inline ll ADD(ll a, ll b) { return a + b >= MOD ? a + b - MOD : a + b; }
inline ll MUL(ll a, ll b) { return a * b % MOD; }
int n, W;
ll dp[103][100003];
ll w[103], v[103];
ll f(int i, ll j) {
if (i == 0)
return 0;
if (dp[i][j] != -1)
return dp[i][j];
ll ret = 0;
REP(k, i) {
if (j - w[k] >= 0) {
ret = max(ret, v[k] + f(k, j - w[k]));
}
}
dp[i][j] = ret;
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> W;
REP(i, n) cin >> w[i] >> v[i];
memset(dp, -1, sizeof(ll) * 103 * 100003);
cout << f(n, W) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
#define x first
#define y second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define szz(x) (int)(x).size()
#define FOR(i, a, b) for (int i = (a), _for = (b); i < _for; i++)
#define REP(i, n) FOR(i, 0, (n))
#define _ << " " <<
const ll MOD = 1000000007ll;
const ll INF = 0x3f3f3f3f3f3f3f3f;
inline ll SUB(ll a, ll b) { return a - b >= 0ll ? a - b : a - b + MOD; }
inline ll ADD(ll a, ll b) { return a + b >= MOD ? a + b - MOD : a + b; }
inline ll MUL(ll a, ll b) { return a * b % MOD; }
int n, W;
ll dp[103][100003];
ll w[103], v[103];
ll f(int i, ll j) {
if (i == 0)
return 0;
if (dp[i][j] != -1)
return dp[i][j];
ll ret = 0;
if (j - w[i - 1] >= 0)
ret = max(ret, v[i - 1] + f(i - 1, j - w[i - 1]));
ret = max(ret, f(i - 1, j));
dp[i][j] = ret;
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> W;
REP(i, n) cin >> w[i] >> v[i];
memset(dp, -1, sizeof(ll) * 103 * 100003);
cout << f(n, W) << endl;
}
|
replace
| 32 | 37 | 32 | 35 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int abs(int a, int b) {
if (a >= b)
return a - b;
else
return b - a;
}
ll dp[2][10005];
int main() {
int n, w;
cin >> n >> w;
rep(i, w + 1) dp[0][i] == 0;
int wi;
ll vi;
rep(i, n) {
cin >> wi >> vi;
rep(j, w + 1) {
if (j < wi)
dp[1][j] = dp[0][j];
else
dp[1][j] = max(dp[0][j - wi] + vi, dp[0][j]);
}
rep(j, w + 1) dp[0][j] = dp[1][j];
}
ll max = 0;
rep(i, w) if (dp[1][i + 1] > max) max = dp[1][i + 1];
cout << max << endl;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int abs(int a, int b) {
if (a >= b)
return a - b;
else
return b - a;
}
ll dp[2][100005];
int main() {
int n, w;
cin >> n >> w;
rep(i, w + 1) dp[0][i] == 0;
int wi;
ll vi;
rep(i, n) {
cin >> wi >> vi;
rep(j, w + 1) {
if (j < wi)
dp[1][j] = dp[0][j];
else
dp[1][j] = max(dp[0][j - wi] + vi, dp[0][j]);
}
rep(j, w + 1) dp[0][j] = dp[1][j];
}
ll max = 0;
rep(i, w) if (dp[1][i + 1] > max) max = dp[1][i + 1];
cout << max << endl;
}
|
replace
| 13 | 14 | 13 | 14 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int n, w;
int arr[100][2];
long long dp[100][100000];
long long inf;
long long go(int i, int sum) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= w; j++) {
if (arr[i - 1][0] <= j)
dp[i][j] =
max(arr[i - 1][1] + dp[i - 1][j - arr[i - 1][0]], dp[i - 1][j]);
else
dp[i][j] = dp[i - 1][j];
}
}
return dp[n][w];
}
int main() {
cin >> n >> w;
inf = 5 * 100000000000000;
for (int i = 0; i < n; i++)
for (int j = 0; j < 2; j++)
cin >> arr[i][j];
long long res = go(0, 0);
if (res > 0)
cout << res;
else
cout << 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, w;
int arr[100][2];
long long dp[105][100010];
long long inf;
long long go(int i, int sum) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= w; j++) {
if (arr[i - 1][0] <= j)
dp[i][j] =
max(arr[i - 1][1] + dp[i - 1][j - arr[i - 1][0]], dp[i - 1][j]);
else
dp[i][j] = dp[i - 1][j];
}
}
return dp[n][w];
}
int main() {
cin >> n >> w;
inf = 5 * 100000000000000;
for (int i = 0; i < n; i++)
for (int j = 0; j < 2; j++)
cin >> arr[i][j];
long long res = go(0, 0);
if (res > 0)
cout << res;
else
cout << 0;
}
|
replace
| 4 | 5 | 4 | 5 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define INF LLONG_MAX
#define ll long long
#define ull unsigned long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define MOD 1000000007
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#define N_MAX 100
#define W_MAX 100000
ll dp[N_MAX][W_MAX];
int main() {
ll N, W;
cin >> N >> W;
vector<ll> w(N + 1), v(N + 1);
for (int i = 1; i <= N; ++i)
cin >> w[i] >> v[i];
for (int i = 1; i <= N; ++i) {
for (int j = 0; j < w[i]; ++j) {
chmax(dp[i][j], dp[i - 1][j]);
}
for (int j = w[i]; j <= W; ++j) {
chmax(dp[i][j], dp[i - 1][j - w[i]] + v[i]);
chmax(dp[i][j], dp[i - 1][j]);
}
}
ll ans = 0;
// Output DP table
// rep(i,N+1) {
// rep(j,W+1) {
// cout << dp[i][j] << " ";
// }
// cout << endl;
// }
rep(i, N + 1) chmax(ans, dp[i][W]);
cout << ans << endl;
}
|
#include <bits/stdc++.h>
#define INF LLONG_MAX
#define ll long long
#define ull unsigned long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define MOD 1000000007
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#define N_MAX 200
#define W_MAX 200000
ll dp[N_MAX][W_MAX];
int main() {
ll N, W;
cin >> N >> W;
vector<ll> w(N + 1), v(N + 1);
for (int i = 1; i <= N; ++i)
cin >> w[i] >> v[i];
for (int i = 1; i <= N; ++i) {
for (int j = 0; j < w[i]; ++j) {
chmax(dp[i][j], dp[i - 1][j]);
}
for (int j = w[i]; j <= W; ++j) {
chmax(dp[i][j], dp[i - 1][j - w[i]] + v[i]);
chmax(dp[i][j], dp[i - 1][j]);
}
}
ll ans = 0;
// Output DP table
// rep(i,N+1) {
// rep(j,W+1) {
// cout << dp[i][j] << " ";
// }
// cout << endl;
// }
rep(i, N + 1) chmax(ans, dp[i][W]);
cout << ans << endl;
}
|
replace
| 21 | 23 | 21 | 23 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T> void _dump(const char *s, T &&head) {
cerr << s << "=" << head << endl;
}
template <typename T, typename... Args>
void _dump(const char *s, T &&head, Args &&...tail) {
while (*s != ',')
cerr << *s++;
cerr << "=" << head << ",";
_dump(s + 1, tail...);
}
template <typename T> void _dumpv(const char *s, T &&v) {
cerr << *s << "=";
for (auto it = v.begin(); it != v.end(); it++) {
cerr << *it << " ";
}
cerr << endl;
}
#ifdef LOCAL
#define dump(...) _dump(#__VA_ARGS__, __VA_ARGS__)
#define dumpv(...) _dumpv(#__VA_ARGS__, __VA_ARGS__)
#else
#define dump(...)
#define dumpv(...)
#endif
const ll MOD = 1000000007;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
freopen("d.3", "r", stdin);
int N, W;
cin >> N >> W;
vector<vector<ll>> dp(N + 1, vector<ll>(W + 1, 0));
vector<ll> weight(N + 1);
vector<ll> value(N + 1);
for (int i = 1; i <= N; i++) {
cin >> weight[i] >> value[i];
}
// for (int i = 1; i <= N; i++) {
// for (int w = 0; w <= W; w++) {
// if (weight[i] <= w) {
// dp[i][w] = max(dp[i - 1][w - weight[i]] + value[i], dp[i - 1][w]);
// } else {
// dp[i][w] = dp[i - 1][w];
// }
// }
// }
for (int i = 1; i <= N; i++) {
for (int w = W; 0 <= w; w--) {
if (weight[i] <= w) {
dp[0][w] = max(dp[0][w - weight[i]] + value[i], dp[0][w]);
} else {
// dp[0][w] = dp[i - 1][w];
}
}
}
dumpv(dp[0]);
cout << dp[0][W] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T> void _dump(const char *s, T &&head) {
cerr << s << "=" << head << endl;
}
template <typename T, typename... Args>
void _dump(const char *s, T &&head, Args &&...tail) {
while (*s != ',')
cerr << *s++;
cerr << "=" << head << ",";
_dump(s + 1, tail...);
}
template <typename T> void _dumpv(const char *s, T &&v) {
cerr << *s << "=";
for (auto it = v.begin(); it != v.end(); it++) {
cerr << *it << " ";
}
cerr << endl;
}
#ifdef LOCAL
#define dump(...) _dump(#__VA_ARGS__, __VA_ARGS__)
#define dumpv(...) _dumpv(#__VA_ARGS__, __VA_ARGS__)
#else
#define dump(...)
#define dumpv(...)
#endif
const ll MOD = 1000000007;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
// freopen("d.3", "r", stdin);
int N, W;
cin >> N >> W;
vector<vector<ll>> dp(N + 1, vector<ll>(W + 1, 0));
vector<ll> weight(N + 1);
vector<ll> value(N + 1);
for (int i = 1; i <= N; i++) {
cin >> weight[i] >> value[i];
}
// for (int i = 1; i <= N; i++) {
// for (int w = 0; w <= W; w++) {
// if (weight[i] <= w) {
// dp[i][w] = max(dp[i - 1][w - weight[i]] + value[i], dp[i - 1][w]);
// } else {
// dp[i][w] = dp[i - 1][w];
// }
// }
// }
for (int i = 1; i <= N; i++) {
for (int w = W; 0 <= w; w--) {
if (weight[i] <= w) {
dp[0][w] = max(dp[0][w - weight[i]] + value[i], dp[0][w]);
} else {
// dp[0][w] = dp[i - 1][w];
}
}
}
dumpv(dp[0]);
cout << dp[0][W] << endl;
return 0;
}
|
replace
| 39 | 40 | 39 | 40 |
-6
|
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;
// hamko utils
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define repi(i, a, b) \
for (long long i = (long long)(a); i < (long long)(b); i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mt make_tuple
#define mp make_pair
template <class T1, class T2> bool chmin(T1 &a, T2 b) {
return b < a && (a = b, true);
}
template <class T1, class T2> bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<ll, ll>;
using ld = long double;
using vld = vector<ld>;
using vi = vector<int>;
using vvi = vector<vi>;
vll conv(vi &v) {
vll r(v.size());
rep(i, v.size()) r[i] = v[i];
return r;
}
inline void input(int &v) {
v = 0;
char c = 0;
int p = 1;
while (c < '0' || c > '9') {
if (c == '-')
p = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
v = (v << 3) + (v << 1) + c - '0';
c = getchar();
}
v *= p;
} // これを使うならば、tieとかを消して!!
template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &v) {
o << "(" << v.first << ", " << v.second << ")";
return o;
}
template <size_t...> struct seq {};
template <size_t N, size_t... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> {};
template <size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {};
template <class Ch, class Tr, class Tuple, size_t... Is>
void print_tuple(basic_ostream<Ch, Tr> &os, Tuple const &t, seq<Is...>) {
using s = int[];
(void)s{0, (void(os << (Is == 0 ? "" : ", ") << get<Is>(t)), 0)...};
}
template <class Ch, class Tr, class... Args>
auto operator<<(basic_ostream<Ch, Tr> &os, tuple<Args...> const &t)
-> basic_ostream<Ch, Tr> & {
os << "(";
print_tuple(os, t, gen_seq<sizeof...(Args)>());
return os << ")";
}
ostream &operator<<(ostream &o, const vvll &v) {
rep(i, v.size()) {
rep(j, v[i].size()) o << v[i][j] << " ";
o << endl;
}
return o;
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) {
o << '[';
rep(i, v.size()) o << v[i] << (i != v.size() - 1 ? ", " : "");
o << "]";
return o;
}
template <typename T> ostream &operator<<(ostream &o, const deque<T> &v) {
o << '[';
rep(i, v.size()) o << v[i] << (i != v.size() - 1 ? ", " : "");
o << "]";
return o;
}
template <typename T> ostream &operator<<(ostream &o, const set<T> &m) {
o << '[';
for (auto it = m.begin(); it != m.end(); it++)
o << *it << (next(it) != m.end() ? ", " : "");
o << "]";
return o;
}
template <typename T>
ostream &operator<<(ostream &o, const unordered_set<T> &m) {
o << '[';
for (auto it = m.begin(); it != m.end(); it++)
o << *it << (next(it) != m.end() ? ", " : "");
o << "]";
return o;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const map<T, U> &m) {
o << '[';
for (auto it = m.begin(); it != m.end(); it++)
o << *it << (next(it) != m.end() ? ", " : "");
o << "]";
return o;
}
template <typename T, typename U, typename V>
ostream &operator<<(ostream &o, const unordered_map<T, U, V> &m) {
o << '[';
for (auto it = m.begin(); it != m.end(); it++)
o << *it;
o << "]";
return o;
}
vector<int> range(const int x, const int y) {
vector<int> v(y - x + 1);
iota(v.begin(), v.end(), x);
return v;
}
template <typename T> istream &operator>>(istream &i, vector<T> &o) {
rep(j, o.size()) i >> o[j];
return i;
}
template <typename T, typename S, typename U>
ostream &operator<<(ostream &o, const priority_queue<T, S, U> &v) {
auto tmp = v;
while (tmp.size()) {
auto x = tmp.top();
tmp.pop();
o << x << " ";
}
return o;
}
template <typename T> ostream &operator<<(ostream &o, const queue<T> &v) {
auto tmp = v;
while (tmp.size()) {
auto x = tmp.front();
tmp.pop();
o << x << " ";
}
return o;
}
template <typename T> ostream &operator<<(ostream &o, const stack<T> &v) {
auto tmp = v;
while (tmp.size()) {
auto x = tmp.top();
tmp.pop();
o << x << " ";
}
return o;
}
template <typename T> unordered_map<T, ll> counter(vector<T> vec) {
unordered_map<T, ll> ret;
for (auto &&x : vec)
ret[x]++;
return ret;
};
string substr(string s, P x) { return s.substr(x.fi, x.se - x.fi); }
void vizGraph(vvll &g, int mode = 0, string filename = "out.png") {
ofstream ofs("./out.dot");
ofs << "digraph graph_name {" << endl;
set<P> memo;
rep(i, g.size()) rep(j, g[i].size()) {
if (mode && (memo.count(P(i, g[i][j])) || memo.count(P(g[i][j], i))))
continue;
memo.insert(P(i, g[i][j]));
ofs << " " << i << " -> " << g[i][j]
<< (mode ? " [arrowhead = none]" : "") << endl;
}
ofs << "}" << endl;
ofs.close();
system(((string) "dot -T png out.dot >" + filename).c_str());
}
size_t random_seed;
namespace std {
using argument_type = P;
template <> struct hash<argument_type> {
size_t operator()(argument_type const &x) const {
size_t seed = random_seed;
seed ^= hash<ll>{}(x.fi);
seed ^= (hash<ll>{}(x.se) << 1);
return seed;
}
};
}; // namespace std
#define ldout fixed << setprecision(40)
#define EPS (double)1e-14
#define INF (ll)1e18
#define mo (ll)(1e9 + 7)
// end of hamko utils
vector<vector<ll>> dp(110, vector<ll>(11000, 0));
int main() {
ll N, W;
cin >> N >> W;
vector<ll> w(N), v(N);
for (ll i = 0; i < N; ++i)
cin >> w[i] >> v[i];
// N個の品物の重さの総和がWになるような持ち方の価値の最大値
for (ll i = 0; i < N; ++i) {
for (ll j = 0; j <= W; ++j) {
if (j - w[i] >= 0)
dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
else
dp[i + 1][j] = dp[i][j];
}
}
cout << dp[N][W] << endl;
return 0;
}
|
#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;
// hamko utils
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define repi(i, a, b) \
for (long long i = (long long)(a); i < (long long)(b); i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mt make_tuple
#define mp make_pair
template <class T1, class T2> bool chmin(T1 &a, T2 b) {
return b < a && (a = b, true);
}
template <class T1, class T2> bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<ll, ll>;
using ld = long double;
using vld = vector<ld>;
using vi = vector<int>;
using vvi = vector<vi>;
vll conv(vi &v) {
vll r(v.size());
rep(i, v.size()) r[i] = v[i];
return r;
}
inline void input(int &v) {
v = 0;
char c = 0;
int p = 1;
while (c < '0' || c > '9') {
if (c == '-')
p = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
v = (v << 3) + (v << 1) + c - '0';
c = getchar();
}
v *= p;
} // これを使うならば、tieとかを消して!!
template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &v) {
o << "(" << v.first << ", " << v.second << ")";
return o;
}
template <size_t...> struct seq {};
template <size_t N, size_t... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> {};
template <size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {};
template <class Ch, class Tr, class Tuple, size_t... Is>
void print_tuple(basic_ostream<Ch, Tr> &os, Tuple const &t, seq<Is...>) {
using s = int[];
(void)s{0, (void(os << (Is == 0 ? "" : ", ") << get<Is>(t)), 0)...};
}
template <class Ch, class Tr, class... Args>
auto operator<<(basic_ostream<Ch, Tr> &os, tuple<Args...> const &t)
-> basic_ostream<Ch, Tr> & {
os << "(";
print_tuple(os, t, gen_seq<sizeof...(Args)>());
return os << ")";
}
ostream &operator<<(ostream &o, const vvll &v) {
rep(i, v.size()) {
rep(j, v[i].size()) o << v[i][j] << " ";
o << endl;
}
return o;
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) {
o << '[';
rep(i, v.size()) o << v[i] << (i != v.size() - 1 ? ", " : "");
o << "]";
return o;
}
template <typename T> ostream &operator<<(ostream &o, const deque<T> &v) {
o << '[';
rep(i, v.size()) o << v[i] << (i != v.size() - 1 ? ", " : "");
o << "]";
return o;
}
template <typename T> ostream &operator<<(ostream &o, const set<T> &m) {
o << '[';
for (auto it = m.begin(); it != m.end(); it++)
o << *it << (next(it) != m.end() ? ", " : "");
o << "]";
return o;
}
template <typename T>
ostream &operator<<(ostream &o, const unordered_set<T> &m) {
o << '[';
for (auto it = m.begin(); it != m.end(); it++)
o << *it << (next(it) != m.end() ? ", " : "");
o << "]";
return o;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const map<T, U> &m) {
o << '[';
for (auto it = m.begin(); it != m.end(); it++)
o << *it << (next(it) != m.end() ? ", " : "");
o << "]";
return o;
}
template <typename T, typename U, typename V>
ostream &operator<<(ostream &o, const unordered_map<T, U, V> &m) {
o << '[';
for (auto it = m.begin(); it != m.end(); it++)
o << *it;
o << "]";
return o;
}
vector<int> range(const int x, const int y) {
vector<int> v(y - x + 1);
iota(v.begin(), v.end(), x);
return v;
}
template <typename T> istream &operator>>(istream &i, vector<T> &o) {
rep(j, o.size()) i >> o[j];
return i;
}
template <typename T, typename S, typename U>
ostream &operator<<(ostream &o, const priority_queue<T, S, U> &v) {
auto tmp = v;
while (tmp.size()) {
auto x = tmp.top();
tmp.pop();
o << x << " ";
}
return o;
}
template <typename T> ostream &operator<<(ostream &o, const queue<T> &v) {
auto tmp = v;
while (tmp.size()) {
auto x = tmp.front();
tmp.pop();
o << x << " ";
}
return o;
}
template <typename T> ostream &operator<<(ostream &o, const stack<T> &v) {
auto tmp = v;
while (tmp.size()) {
auto x = tmp.top();
tmp.pop();
o << x << " ";
}
return o;
}
template <typename T> unordered_map<T, ll> counter(vector<T> vec) {
unordered_map<T, ll> ret;
for (auto &&x : vec)
ret[x]++;
return ret;
};
string substr(string s, P x) { return s.substr(x.fi, x.se - x.fi); }
void vizGraph(vvll &g, int mode = 0, string filename = "out.png") {
ofstream ofs("./out.dot");
ofs << "digraph graph_name {" << endl;
set<P> memo;
rep(i, g.size()) rep(j, g[i].size()) {
if (mode && (memo.count(P(i, g[i][j])) || memo.count(P(g[i][j], i))))
continue;
memo.insert(P(i, g[i][j]));
ofs << " " << i << " -> " << g[i][j]
<< (mode ? " [arrowhead = none]" : "") << endl;
}
ofs << "}" << endl;
ofs.close();
system(((string) "dot -T png out.dot >" + filename).c_str());
}
size_t random_seed;
namespace std {
using argument_type = P;
template <> struct hash<argument_type> {
size_t operator()(argument_type const &x) const {
size_t seed = random_seed;
seed ^= hash<ll>{}(x.fi);
seed ^= (hash<ll>{}(x.se) << 1);
return seed;
}
};
}; // namespace std
#define ldout fixed << setprecision(40)
#define EPS (double)1e-14
#define INF (ll)1e18
#define mo (ll)(1e9 + 7)
// end of hamko utils
vector<vector<ll>> dp(110, vector<ll>(110000, 0));
int main() {
ll N, W;
cin >> N >> W;
vector<ll> w(N), v(N);
for (ll i = 0; i < N; ++i)
cin >> w[i] >> v[i];
// N個の品物の重さの総和がWになるような持ち方の価値の最大値
for (ll i = 0; i < N; ++i) {
for (ll j = 0; j <= W; ++j) {
if (j - w[i] >= 0)
dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
else
dp[i + 1][j] = dp[i][j];
}
}
cout << dp[N][W] << endl;
return 0;
}
|
replace
| 199 | 200 | 199 | 200 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define vi vector<ll>
typedef long long ll;
void read() { freopen("in.txt", "r", stdin); }
ll ks(ll num_items, ll cap, const vector<ll> &w, const vector<ll> &v) {
ll dp[num_items + 1][cap + 1];
for (ll i = 0; i <= num_items; i++) {
dp[i][0] = 0;
}
for (ll i = 0; i <= cap; i++) {
dp[0][i] = 0;
}
for (ll i = 1; i <= num_items; i++) {
for (ll j = 1; j <= cap; j++) {
ll curr_value = v[i - 1];
ll curr_wt = w[i - 1];
if (curr_wt > j) {
dp[i][j] = dp[i - 1][j];
} else {
dp[i][j] = max(curr_value + dp[i - 1][j - curr_wt], dp[i - 1][j]);
}
}
}
return dp[num_items][cap];
}
int main() {
read();
ll num_items, cap;
cin >> num_items >> cap;
vector<ll> w(num_items), v(num_items);
for (ll i = 0; i < num_items; i++) {
cin >> w[i] >> v[i];
}
cout << ks(num_items, cap, w, v);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define vi vector<ll>
typedef long long ll;
void read() { freopen("in.txt", "r", stdin); }
ll ks(ll num_items, ll cap, const vector<ll> &w, const vector<ll> &v) {
ll dp[num_items + 1][cap + 1];
for (ll i = 0; i <= num_items; i++) {
dp[i][0] = 0;
}
for (ll i = 0; i <= cap; i++) {
dp[0][i] = 0;
}
for (ll i = 1; i <= num_items; i++) {
for (ll j = 1; j <= cap; j++) {
ll curr_value = v[i - 1];
ll curr_wt = w[i - 1];
if (curr_wt > j) {
dp[i][j] = dp[i - 1][j];
} else {
dp[i][j] = max(curr_value + dp[i - 1][j - curr_wt], dp[i - 1][j]);
}
}
}
return dp[num_items][cap];
}
int main() {
// read();
ll num_items, cap;
cin >> num_items >> cap;
vector<ll> w(num_items), v(num_items);
for (ll i = 0; i < num_items; i++) {
cin >> w[i] >> v[i];
}
cout << ks(num_items, cap, w, v);
return 0;
}
|
replace
| 33 | 34 | 33 | 34 |
-6
|
terminate called after throwing an instance of 'std::length_error'
what(): cannot create std::vector larger than max_size()
|
p03163
|
C++
|
Runtime Error
|
// author:satwik_bhv1
#include <bits/stdc++.h>
// datatypes
#define ll long long
#define ld long double
// loops
#define fr(i, z, n) for (ll i = z; i < n; i++)
#define br(i, z, n) for (ll i = z; i > n; i--)
// operations
#define mp make_pair
#define ff first
#define ss second
#define pub push_back
#define all(v) v.begin(), v.end()
// map
#define mi map<ll, ll>
// vectors
#define vi vector<ll>
#define vpi vector<pair<ll, ll>>
// constants
#define pi 3.1415926535897932384626
#define mod 1000000007
#define MAXN 100001
/*notes
Don't use inbuilt fun for power
Range of longlong=(-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 )
INT_MAX = 2147483647(aprox 2*10^10)
INT_MIN = -2147483648
LLONG_MAX = 9223372036854775807
LLONG_MIN = -9223372036854775808
__gcd is the function in built for hcf
priority queue is max heap by default
topological sort -> khan's algo use bfs and indegree of node
sssp(positive weighted edges) -> Dijkstras
apsp -> Floydwarshall
MST -> kruskal(DSU)
*/
using namespace std;
// functions
ll power(ll x, ll y) {
ll res = 1;
while (y) {
if (y % 2) {
res *= x;
}
y /= 2;
x *= x;
}
return res;
}
ll power(ll x, ll y, ll z) {
ll res = 1;
while (y) {
if (y % 2) {
res = (res * x) % z;
}
y /= 2;
x = (x * x) % z;
}
return res;
}
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
bool sortbysec(const pair<ll, ll> &a, const pair<ll, ll> &b) {
return (a.second > b.second);
}
ll ceil(ll x, ll y) { return (x / y) + (x % y != 0); }
ll n, w;
ll v[100][2];
ll dp[100][100001];
ll fun(ll pos, ll limit) {
if (pos == n) {
return 0;
}
ll ans = 0;
if (dp[pos + 1][limit] == -1) {
ans = max(ans, fun(pos + 1, limit));
} else {
ans = max(ans, dp[pos + 1][limit]);
}
if (limit >= v[pos][0]) {
if (dp[pos + 1][limit - v[pos][0]] == -1) {
ans = max(ans, fun(pos + 1, limit - v[pos][0]) + v[pos][1]);
} else {
ans = max(ans, dp[pos + 1][limit - v[pos][0]] + v[pos][1]);
}
}
return dp[pos][limit] = ans;
}
void solve() {
cin >> n >> w;
fr(i, 0, n) {
fr(j, 0, 2) { cin >> v[i][j]; }
}
fr(i, 0, n) {
fr(j, 0, w + 1) { dp[i][j] = -1; }
}
cout << fun(0, w);
}
int main() {
fast();
ll t;
t = 1;
// cin>>t;
fr(i, 0, t) { solve(); }
}
/*
*/
|
// author:satwik_bhv1
#include <bits/stdc++.h>
// datatypes
#define ll long long
#define ld long double
// loops
#define fr(i, z, n) for (ll i = z; i < n; i++)
#define br(i, z, n) for (ll i = z; i > n; i--)
// operations
#define mp make_pair
#define ff first
#define ss second
#define pub push_back
#define all(v) v.begin(), v.end()
// map
#define mi map<ll, ll>
// vectors
#define vi vector<ll>
#define vpi vector<pair<ll, ll>>
// constants
#define pi 3.1415926535897932384626
#define mod 1000000007
#define MAXN 100001
/*notes
Don't use inbuilt fun for power
Range of longlong=(-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 )
INT_MAX = 2147483647(aprox 2*10^10)
INT_MIN = -2147483648
LLONG_MAX = 9223372036854775807
LLONG_MIN = -9223372036854775808
__gcd is the function in built for hcf
priority queue is max heap by default
topological sort -> khan's algo use bfs and indegree of node
sssp(positive weighted edges) -> Dijkstras
apsp -> Floydwarshall
MST -> kruskal(DSU)
*/
using namespace std;
// functions
ll power(ll x, ll y) {
ll res = 1;
while (y) {
if (y % 2) {
res *= x;
}
y /= 2;
x *= x;
}
return res;
}
ll power(ll x, ll y, ll z) {
ll res = 1;
while (y) {
if (y % 2) {
res = (res * x) % z;
}
y /= 2;
x = (x * x) % z;
}
return res;
}
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
bool sortbysec(const pair<ll, ll> &a, const pair<ll, ll> &b) {
return (a.second > b.second);
}
ll ceil(ll x, ll y) { return (x / y) + (x % y != 0); }
ll n, w;
ll v[101][2];
ll dp[101][100001];
ll fun(ll pos, ll limit) {
if (pos == n) {
return 0;
}
ll ans = 0;
if (dp[pos + 1][limit] == -1) {
ans = max(ans, fun(pos + 1, limit));
} else {
ans = max(ans, dp[pos + 1][limit]);
}
if (limit >= v[pos][0]) {
if (dp[pos + 1][limit - v[pos][0]] == -1) {
ans = max(ans, fun(pos + 1, limit - v[pos][0]) + v[pos][1]);
} else {
ans = max(ans, dp[pos + 1][limit - v[pos][0]] + v[pos][1]);
}
}
return dp[pos][limit] = ans;
}
void solve() {
cin >> n >> w;
fr(i, 0, n) {
fr(j, 0, 2) { cin >> v[i][j]; }
}
fr(i, 0, n) {
fr(j, 0, w + 1) { dp[i][j] = -1; }
}
cout << fun(0, w);
}
int main() {
fast();
ll t;
t = 1;
// cin>>t;
fr(i, 0, t) { solve(); }
}
/*
*/
|
replace
| 71 | 73 | 71 | 73 |
0
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<pair<int, int>> A;
ll relaxed(int i, ll cap) {
ll result = 0;
while (i < A.size()) {
if (A[i].first <= cap) {
cap -= A[i].first;
result += A[i].second;
i++;
} else {
// result += (A[i].second * cap + A[i].first - 1) / A[i].first;
result += A[i].second;
break;
}
}
return result;
}
ll backtrack(int i, ll cap) {
if (i >= A.size())
return 0;
if (A[i].first > cap)
return backtrack(i + 1, cap);
ll a = A[i].second + backtrack(i + 1, cap - A[i].first);
ll b = relaxed(i + 1, cap);
if (a >= b)
return a;
return max(a, backtrack(i + 1, cap));
}
int main() {
std::ios::sync_with_stdio(false);
ll n, w, a, b;
cin >> n >> w;
A.resize(n);
for (int i = 0; i < n; i++)
cin >> A[i].first >> A[i].second;
sort(A.begin(), A.end(), [](const pair<ll, ll> &x, const pair<ll, ll> &y) {
if (x.second * y.first == y.second * x.first)
return x.first < y.first;
return x.second * y.first > y.second * x.first;
});
cout << backtrack(0, w) << "\n";
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<pair<int, int>> A;
ll relaxed(int i, ll cap) {
ll result = 0;
while (i < A.size()) {
if (A[i].first <= cap) {
cap -= A[i].first;
result += A[i].second;
i++;
} else {
result += (A[i].second * cap + A[i].first - 1) / A[i].first;
// result += A[i].second;
break;
}
}
return result;
}
ll backtrack(int i, ll cap) {
if (i >= A.size())
return 0;
if (A[i].first > cap)
return backtrack(i + 1, cap);
ll a = A[i].second + backtrack(i + 1, cap - A[i].first);
ll b = relaxed(i + 1, cap);
if (a >= b)
return a;
return max(a, backtrack(i + 1, cap));
}
int main() {
std::ios::sync_with_stdio(false);
ll n, w, a, b;
cin >> n >> w;
A.resize(n);
for (int i = 0; i < n; i++)
cin >> A[i].first >> A[i].second;
sort(A.begin(), A.end(), [](const pair<ll, ll> &x, const pair<ll, ll> &y) {
if (x.second * y.first == y.second * x.first)
return x.first < y.first;
return x.second * y.first > y.second * x.first;
});
cout << backtrack(0, w) << "\n";
return 0;
}
|
replace
| 16 | 18 | 16 | 18 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define REP(i, num, n) for (ll i = num, i##_len = (n); i < i##_len; ++i)
#define repprev(i, a, b) for (ll i = b - 1; i >= a; i--)
#define reprev(i, n) repprev(i, 0, n)
using namespace std;
#define sz(x) ((int)(x).size())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define MEMSET(v, h) memset((v), h, sizeof(v))
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <class T> int former(const vector<T> &v, T x) {
return upper_bound(v.begin(), v.end(), x) - v.begin() - 1;
}
template <class T> int latter(const vector<T> &v, T x) {
return lower_bound(v.begin(), v.end(), x) - v.begin();
}
#define pb push_back
#define mp make_pair
#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define BIT_FLAG_0 (1 << 0) // 0000 0000 0000 0001
#define BIT_FLAG_1 (1 << 1) // 0000 0000 0000 0010
#define BIT_FLAG_2 (1 << 2) // 0000 0000 0000 0100
#define BIT_FLAG_3 (1 << 3) // 0000 0000 0000 1000
#define BIT_FLAG_4 (1 << 4) // 0000 0000 0001 0000
#define BIT_FLAG_5 (1 << 5) // 0000 0000 0010 0000
#define BIT_FLAG_6 (1 << 6) // 0000 0000 0100 0000
#define BIT_FLAG_7 (1 << 7) // 0000 0000 1000 0000
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const ll LLINF = 1LL << 60;
const int INTINF = 1 << 30;
const int MAX = 510000;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
struct UnionFind {
vector<ll> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
UnionFind(ll n) : par(n, -1) {}
void init(ll n) { par.assign(n, -1); }
ll root(ll x) {
if (par[x] < 0)
return x;
else
return par[x] = root(par[x]);
}
bool issame(ll x, ll y) { return root(x) == root(y); }
bool merge(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (par[x] > par[y])
swap(x, y); // merge technique
par[x] += par[y];
par[y] = x;
return true;
}
ll size(ll x) { return -par[root(x)]; }
};
template <typename T>
vector<T> dijkstra(int s, vector<vector<pair<int, T>>> &G) {
const T INF = numeric_limits<T>::max();
using P = pair<T, int>;
int n = G.size();
vector<T> d(n, INF);
vector<int> b(n, -1);
priority_queue<P, vector<P>, greater<P>> q;
d[s] = 0;
q.emplace(d[s], s);
while (!q.empty()) {
P p = q.top();
q.pop();
int v = p.second;
if (d[v] < p.first)
continue;
for (auto &e : G[v]) {
int u = e.first;
T c = e.second;
if (d[u] > d[v] + c) {
d[u] = d[v] + c;
b[u] = v;
q.emplace(d[u], u);
}
}
}
return d;
}
vector<vector<int>> bfs(vector<string> &s, int sy, int sx, char wall, int dir) {
int h = s.size(), w = s.front().size();
vector<vector<int>> dp(h, vector<int>(w, -1));
using P = pair<int, int>;
queue<P> q;
dp[sy][sx] = 0;
q.emplace(sy, sx);
int dy[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
auto in = [&](int y, int x) { return 0 <= y && y < h && 0 <= x && x < w; };
while (!q.empty()) {
int y, x;
tie(y, x) = q.front();
q.pop();
for (int k = 0; k < dir; k++) {
int ny = y + dy[k], nx = x + dx[k];
if (!in(ny, nx) || s[ny][nx] == wall)
continue;
if (~dp[ny][nx])
continue;
dp[ny][nx] = dp[y][x] + 1;
q.emplace(ny, nx);
}
}
return dp;
}
int64_t power(int64_t x, int64_t n, int64_t mod) {
int64_t ret = 1;
while (n > 0) {
if (n & 1)
(ret *= x) %= mod;
(x *= x) %= mod;
n >>= 1;
}
return ret;
}
vector<int> sieve_of_eratosthenes(int n) {
vector<int> primes(n);
for (int i = 2; i < n; ++i)
primes[i] = i;
for (int i = 2; i * i < n; ++i)
if (primes[i])
for (int j = i * i; j < n; j += i)
primes[j] = 0;
return primes;
}
struct Dice {
int s[6];
int &top() { return s[0]; }
int &south() { return s[1]; }
int &east() { return s[2]; }
int &west() { return s[3]; }
int &north() { return s[4]; }
int &bottom() { return s[5]; }
void roll(char c) {
// the view from above
// N
// W E
// S
string b("EWNSRL");
int v[6][4] = {{0, 3, 5, 2}, {0, 2, 5, 3}, {0, 1, 5, 4},
{0, 4, 5, 1}, {1, 2, 4, 3}, {1, 3, 4, 2}};
for (int k = 0; k < 6; k++) {
if (b[k] != c)
continue;
int t = s[v[k][0]];
s[v[k][0]] = s[v[k][1]];
s[v[k][1]] = s[v[k][2]];
s[v[k][2]] = s[v[k][3]];
s[v[k][3]] = t;
}
}
using ll = long long;
ll hash() {
ll res = 0;
for (int i = 0; i < 6; i++)
res = res * 256 + s[i];
return res;
}
bool operator==(const Dice &d) const {
for (int i = 0; i < 6; i++)
if (s[i] != d.s[i])
return 0;
return 1;
}
};
vector<Dice> makeDices(Dice d) {
vector<Dice> res;
for (int i = 0; i < 6; i++) {
Dice t(d);
if (i == 1)
t.roll('N');
if (i == 2)
t.roll('S');
if (i == 3)
t.roll('S'), t.roll('S');
if (i == 4)
t.roll('L');
if (i == 5)
t.roll('R');
for (int k = 0; k < 4; k++) {
res.push_back(t);
t.roll('E');
}
}
return res;
}
std::vector<ll> divisor(ll n) // nの約数を列挙
{
std::vector<ll> ret;
for (ll i = 1; i * i <= n; ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i != 1 && i * i != n) {
ret.push_back(n / i);
}
}
}
return ret;
}
// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
using Graph = vector<vector<int>>;
// 深さ優先探索
vector<bool> seen;
void gdfs(const Graph &G, int v) {
seen[v] = true; // v を訪問済にする
// v から行ける各頂点 next_v について
for (auto next_v : G[v]) {
if (seen[next_v])
continue; // next_v が探索済だったらスルー
gdfs(G, next_v); // 再帰的に探索
}
}
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const string MINUSINF = "-";
void Cmax(string &a, string b) {
if (a == MINUSINF)
a = b;
else if (a.size() < b.size())
a = b;
else if (a.size() == b.size()) {
if (a < b)
a = b;
}
}
int N;
ll dp[110][100100];
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
ll N, W;
cin >> N >> W;
vector<ll> w(N);
vector<ll> v(N);
rep(i, N) { cin >> w[i] >> v[i]; }
ll ans = 0;
rep(i, N) {
rep(j, W + 1) {
if (j < w[i - 1])
continue;
dp[i + 1][j] = chmax(dp[i][j], dp[i][j - w[i]] + v[i]);
}
}
cout << dp[N][W] << endl;
}
|
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define REP(i, num, n) for (ll i = num, i##_len = (n); i < i##_len; ++i)
#define repprev(i, a, b) for (ll i = b - 1; i >= a; i--)
#define reprev(i, n) repprev(i, 0, n)
using namespace std;
#define sz(x) ((int)(x).size())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define MEMSET(v, h) memset((v), h, sizeof(v))
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <class T> int former(const vector<T> &v, T x) {
return upper_bound(v.begin(), v.end(), x) - v.begin() - 1;
}
template <class T> int latter(const vector<T> &v, T x) {
return lower_bound(v.begin(), v.end(), x) - v.begin();
}
#define pb push_back
#define mp make_pair
#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define BIT_FLAG_0 (1 << 0) // 0000 0000 0000 0001
#define BIT_FLAG_1 (1 << 1) // 0000 0000 0000 0010
#define BIT_FLAG_2 (1 << 2) // 0000 0000 0000 0100
#define BIT_FLAG_3 (1 << 3) // 0000 0000 0000 1000
#define BIT_FLAG_4 (1 << 4) // 0000 0000 0001 0000
#define BIT_FLAG_5 (1 << 5) // 0000 0000 0010 0000
#define BIT_FLAG_6 (1 << 6) // 0000 0000 0100 0000
#define BIT_FLAG_7 (1 << 7) // 0000 0000 1000 0000
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const ll LLINF = 1LL << 60;
const int INTINF = 1 << 30;
const int MAX = 510000;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
struct UnionFind {
vector<ll> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
UnionFind(ll n) : par(n, -1) {}
void init(ll n) { par.assign(n, -1); }
ll root(ll x) {
if (par[x] < 0)
return x;
else
return par[x] = root(par[x]);
}
bool issame(ll x, ll y) { return root(x) == root(y); }
bool merge(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (par[x] > par[y])
swap(x, y); // merge technique
par[x] += par[y];
par[y] = x;
return true;
}
ll size(ll x) { return -par[root(x)]; }
};
template <typename T>
vector<T> dijkstra(int s, vector<vector<pair<int, T>>> &G) {
const T INF = numeric_limits<T>::max();
using P = pair<T, int>;
int n = G.size();
vector<T> d(n, INF);
vector<int> b(n, -1);
priority_queue<P, vector<P>, greater<P>> q;
d[s] = 0;
q.emplace(d[s], s);
while (!q.empty()) {
P p = q.top();
q.pop();
int v = p.second;
if (d[v] < p.first)
continue;
for (auto &e : G[v]) {
int u = e.first;
T c = e.second;
if (d[u] > d[v] + c) {
d[u] = d[v] + c;
b[u] = v;
q.emplace(d[u], u);
}
}
}
return d;
}
vector<vector<int>> bfs(vector<string> &s, int sy, int sx, char wall, int dir) {
int h = s.size(), w = s.front().size();
vector<vector<int>> dp(h, vector<int>(w, -1));
using P = pair<int, int>;
queue<P> q;
dp[sy][sx] = 0;
q.emplace(sy, sx);
int dy[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
auto in = [&](int y, int x) { return 0 <= y && y < h && 0 <= x && x < w; };
while (!q.empty()) {
int y, x;
tie(y, x) = q.front();
q.pop();
for (int k = 0; k < dir; k++) {
int ny = y + dy[k], nx = x + dx[k];
if (!in(ny, nx) || s[ny][nx] == wall)
continue;
if (~dp[ny][nx])
continue;
dp[ny][nx] = dp[y][x] + 1;
q.emplace(ny, nx);
}
}
return dp;
}
int64_t power(int64_t x, int64_t n, int64_t mod) {
int64_t ret = 1;
while (n > 0) {
if (n & 1)
(ret *= x) %= mod;
(x *= x) %= mod;
n >>= 1;
}
return ret;
}
vector<int> sieve_of_eratosthenes(int n) {
vector<int> primes(n);
for (int i = 2; i < n; ++i)
primes[i] = i;
for (int i = 2; i * i < n; ++i)
if (primes[i])
for (int j = i * i; j < n; j += i)
primes[j] = 0;
return primes;
}
struct Dice {
int s[6];
int &top() { return s[0]; }
int &south() { return s[1]; }
int &east() { return s[2]; }
int &west() { return s[3]; }
int &north() { return s[4]; }
int &bottom() { return s[5]; }
void roll(char c) {
// the view from above
// N
// W E
// S
string b("EWNSRL");
int v[6][4] = {{0, 3, 5, 2}, {0, 2, 5, 3}, {0, 1, 5, 4},
{0, 4, 5, 1}, {1, 2, 4, 3}, {1, 3, 4, 2}};
for (int k = 0; k < 6; k++) {
if (b[k] != c)
continue;
int t = s[v[k][0]];
s[v[k][0]] = s[v[k][1]];
s[v[k][1]] = s[v[k][2]];
s[v[k][2]] = s[v[k][3]];
s[v[k][3]] = t;
}
}
using ll = long long;
ll hash() {
ll res = 0;
for (int i = 0; i < 6; i++)
res = res * 256 + s[i];
return res;
}
bool operator==(const Dice &d) const {
for (int i = 0; i < 6; i++)
if (s[i] != d.s[i])
return 0;
return 1;
}
};
vector<Dice> makeDices(Dice d) {
vector<Dice> res;
for (int i = 0; i < 6; i++) {
Dice t(d);
if (i == 1)
t.roll('N');
if (i == 2)
t.roll('S');
if (i == 3)
t.roll('S'), t.roll('S');
if (i == 4)
t.roll('L');
if (i == 5)
t.roll('R');
for (int k = 0; k < 4; k++) {
res.push_back(t);
t.roll('E');
}
}
return res;
}
std::vector<ll> divisor(ll n) // nの約数を列挙
{
std::vector<ll> ret;
for (ll i = 1; i * i <= n; ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i != 1 && i * i != n) {
ret.push_back(n / i);
}
}
}
return ret;
}
// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
using Graph = vector<vector<int>>;
// 深さ優先探索
vector<bool> seen;
void gdfs(const Graph &G, int v) {
seen[v] = true; // v を訪問済にする
// v から行ける各頂点 next_v について
for (auto next_v : G[v]) {
if (seen[next_v])
continue; // next_v が探索済だったらスルー
gdfs(G, next_v); // 再帰的に探索
}
}
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const string MINUSINF = "-";
void Cmax(string &a, string b) {
if (a == MINUSINF)
a = b;
else if (a.size() < b.size())
a = b;
else if (a.size() == b.size()) {
if (a < b)
a = b;
}
}
int N;
ll dp[110][100100];
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
ll N, W;
cin >> N >> W;
vector<ll> w(N);
vector<ll> v(N);
rep(i, N) { cin >> w[i] >> v[i]; }
ll ans = 0;
rep(i, N) {
rep(j, W + 1) {
if (j >= w[i])
chmax(dp[i + 1][j], dp[i][j - w[i]] + v[i]);
chmax(dp[i + 1][j], dp[i][j]);
}
}
cout << dp[N][W] << endl;
}
|
replace
| 315 | 318 | 315 | 318 |
127
|
/tmp/8188185e-41f8-44a3-86cc-3c237dc478cb.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object
|
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using lli = long long int;
vector<lli> input(int N) {
vector<lli> r(N);
for (int i = 0; i < N; i++)
cin >> r[i];
return r;
}
int main() {
lli N, W;
cin >> N >> W;
lli dp[100][100000];
for (int i = 0; i < N; i++) {
lli w, v;
cin >> w >> v;
for (lli x = 0; x <= W; x++) {
if (x < w)
dp[i + 1][x] = dp[i][x];
else
dp[i + 1][x] = max(dp[i][x], dp[i][x - w] + v);
}
}
cout << dp[N][W] << endl;
}
|
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using lli = long long int;
vector<lli> input(int N) {
vector<lli> r(N);
for (int i = 0; i < N; i++)
cin >> r[i];
return r;
}
int main() {
lli N, W;
cin >> N >> W;
lli dp[110][100010];
for (int i = 0; i < N; i++) {
lli w, v;
cin >> w >> v;
for (lli x = 0; x <= W; x++) {
if (x < w)
dp[i + 1][x] = dp[i][x];
else
dp[i + 1][x] = max(dp[i][x], dp[i][x - w] + v);
}
}
cout << dp[N][W] << endl;
}
|
replace
| 18 | 19 | 18 | 19 |
-11
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ull unsigned long long
#define endl "\n"
#define pb push_back
#define mp make_pair
#define fast_io ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ini(a, i) memset(a, i, sizeof(a))
#define forf(i, a, b) for (int i = a; i < b; i++)
#define forb(i, a, b) for (int i = b - 1; i >= a; i--)
#define debug(ar, n) \
for (int i = 0; i < n; ++i) \
cout << ar[i] << ' '; \
cout << endl;
const int MAX_VALUE = LONG_MAX;
const int MIN_VALUE = LONG_MIN;
const ull U_MAX_VALUE = ULLONG_MAX;
typedef pair<int, int> pi;
int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
int max(int a, int b) {
if (a > b)
return a;
else
return b;
}
int min(int a, int b) {
if (a < b)
return a;
else
return b;
}
ll mod_power(ll x, ull y, ll p) {
ll res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
ll find_no_of_bits(ll n) {
ll bits = 0;
while (n) {
n /= 2;
bits++;
}
return bits;
}
void solve() {
ll n, w;
cin >> n >> w;
ll arr[n + 1][2];
for (ll i = 1; i <= n; i++)
cin >> arr[i][0] >> arr[i][1];
ll dp[n + 1][w + 1];
ini(dp, 0);
bool picked[n];
ini(picked, 0);
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= w; j++) {
dp[i][j] = dp[i - 1][j];
if (j - arr[i][0] >= 0)
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - arr[i][0]] + arr[i][1]);
// cout << dp[i][j] << " ";
}
// cout << "\n";
}
cout << dp[n][w];
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fast_io;
// ll t = 1;
// cin >> t;
// while (t--)
// {
solve();
cout << "\n";
// }
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ull unsigned long long
#define endl "\n"
#define pb push_back
#define mp make_pair
#define fast_io ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ini(a, i) memset(a, i, sizeof(a))
#define forf(i, a, b) for (int i = a; i < b; i++)
#define forb(i, a, b) for (int i = b - 1; i >= a; i--)
#define debug(ar, n) \
for (int i = 0; i < n; ++i) \
cout << ar[i] << ' '; \
cout << endl;
const int MAX_VALUE = LONG_MAX;
const int MIN_VALUE = LONG_MIN;
const ull U_MAX_VALUE = ULLONG_MAX;
typedef pair<int, int> pi;
int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
int max(int a, int b) {
if (a > b)
return a;
else
return b;
}
int min(int a, int b) {
if (a < b)
return a;
else
return b;
}
ll mod_power(ll x, ull y, ll p) {
ll res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
ll find_no_of_bits(ll n) {
ll bits = 0;
while (n) {
n /= 2;
bits++;
}
return bits;
}
void solve() {
ll n, w;
cin >> n >> w;
ll arr[n + 1][2];
for (ll i = 1; i <= n; i++)
cin >> arr[i][0] >> arr[i][1];
ll dp[n + 1][w + 1];
ini(dp, 0);
bool picked[n];
ini(picked, 0);
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= w; j++) {
dp[i][j] = dp[i - 1][j];
if (j - arr[i][0] >= 0)
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - arr[i][0]] + arr[i][1]);
// cout << dp[i][j] << " ";
}
// cout << "\n";
}
cout << dp[n][w];
}
signed main() {
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
fast_io;
// ll t = 1;
// cin >> t;
// while (t--)
// {
solve();
cout << "\n";
// }
}
|
replace
| 83 | 87 | 83 | 87 |
-11
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define endl "\n"
#define int long long
bool compare(pair<int, int> p1, pair<int, int> p2) {
if (p1.first == p2.first)
return p1.second < p2.second;
return p1.first < p2.first;
}
int32_t main() {
IOS
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, W, w, v;
cin >> n >> W;
vector<pair<int, int>> item;
item.push_back({0, 0});
for (int i = 0; i < n; i++) {
cin >> w >> v;
item.push_back(make_pair(w, v));
}
sort(item.begin(), item.end(), compare);
// for(int i=1;i<=n;i++) cout<<item[i].first<<" "<<item[i].second<<endl;
int dp[n + 1][W + 1];
for (int i = 0; i <= n; i++)
dp[i][0] = 0;
for (int i = 0; i <= W; i++)
dp[0][i] = 0;
for (int i = 1; i <= W; i++) {
for (int j = 1; j <= n; j++) {
if (i - item[j].first >= 0)
dp[j][i] =
max(dp[j - 1][i], item[j].second + dp[j - 1][i - item[j].first]);
else
dp[j][i] = dp[j - 1][i];
}
}
// for(int i=0;i<=n;i++) {for(int j=0;j<=W;j++) cout<<dp[i][j]<<" ";
// cout<<endl;}
cout << dp[n][W];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define endl "\n"
#define int long long
bool compare(pair<int, int> p1, pair<int, int> p2) {
if (p1.first == p2.first)
return p1.second < p2.second;
return p1.first < p2.first;
}
int32_t main() {
IOS
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
int n,
W, w, v;
cin >> n >> W;
vector<pair<int, int>> item;
item.push_back({0, 0});
for (int i = 0; i < n; i++) {
cin >> w >> v;
item.push_back(make_pair(w, v));
}
sort(item.begin(), item.end(), compare);
// for(int i=1;i<=n;i++) cout<<item[i].first<<" "<<item[i].second<<endl;
int dp[n + 1][W + 1];
for (int i = 0; i <= n; i++)
dp[i][0] = 0;
for (int i = 0; i <= W; i++)
dp[0][i] = 0;
for (int i = 1; i <= W; i++) {
for (int j = 1; j <= n; j++) {
if (i - item[j].first >= 0)
dp[j][i] =
max(dp[j - 1][i], item[j].second + dp[j - 1][i - item[j].first]);
else
dp[j][i] = dp[j - 1][i];
}
}
// for(int i=0;i<=n;i++) {for(int j=0;j<=W;j++) cout<<dp[i][j]<<" ";
// cout<<endl;}
cout << dp[n][W];
return 0;
}
|
replace
| 15 | 20 | 15 | 21 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<ll> vel;
typedef vector<int> vei;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<ll>> ve_vel;
typedef vector<vector<int>> ve_vei;
typedef vector<vector<char>> ve_vec;
typedef vector<vector<bool>> ve_veb;
typedef vector<vector<string>> ve_ves;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define rep2(i, n) for (int i = 2; i < (int)(n); i++)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define fs first
#define sc second
#define pub push_back
#define puf push_front
#define pob pop_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(a) *max_element(all(a))
#define minel(a) *min_element(all(a))
#define acc accumulate
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
#define MAX_W 100000
#define MAX_N 100
ll dp[MAX_N][MAX_W];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, W;
cin >> N >> W;
vei w(N);
vel v(N);
rep(i, N) cin >> w[i] >> v[i];
for (int i = N - 1; i >= 0; i--) {
rep(j, W + 1) {
if (j < w[i])
dp[i][j] = dp[i + 1][j];
else
dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
}
}
cout << dp[0][W] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<ll> vel;
typedef vector<int> vei;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<ll>> ve_vel;
typedef vector<vector<int>> ve_vei;
typedef vector<vector<char>> ve_vec;
typedef vector<vector<bool>> ve_veb;
typedef vector<vector<string>> ve_ves;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define rep2(i, n) for (int i = 2; i < (int)(n); i++)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define fs first
#define sc second
#define pub push_back
#define puf push_front
#define pob pop_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(a) *max_element(all(a))
#define minel(a) *min_element(all(a))
#define acc accumulate
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
#define MAX_W 100000
#define MAX_N 100
ll dp[MAX_N + 5][MAX_W + 5];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, W;
cin >> N >> W;
vei w(N);
vel v(N);
rep(i, N) cin >> w[i] >> v[i];
for (int i = N - 1; i >= 0; i--) {
rep(j, W + 1) {
if (j < w[i])
dp[i][j] = dp[i + 1][j];
else
dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
}
}
cout << dp[0][W] << endl;
return 0;
}
|
replace
| 51 | 52 | 51 | 52 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
int main() {
int n, w;
cin >> n >> w;
pair<long long, long long> arr[n];
for (int i = 0; i < n; ++i) {
int a, b;
cin >> a >> b;
arr[i] = make_pair(a, b);
}
long long dp[n + 1][w + 1];
for (int i = 0; i <= n; ++i) {
dp[i][0] = 0;
}
for (int i = 0; i <= w; ++i) {
dp[0][i] = 0;
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= w; ++j) {
if (j - arr[i - 1].first >= 0) {
dp[i][j] = max(arr[i - 1].second + dp[i - 1][j - arr[i - 1].first],
dp[i - 1][j]);
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
// long long val = 0;
// for (int i = 0; i < n + 1; ++i) {
// for (int j = 0; j <= w; ++j)
// {
// val = max(val, dp[i][j]);
// cout << dp[i][j] << "\t";
// }
// cout << endl;
// }
cout << dp[n][w] << endl;
return 1;
}
|
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
int main() {
int n, w;
cin >> n >> w;
pair<long long, long long> arr[n];
for (int i = 0; i < n; ++i) {
int a, b;
cin >> a >> b;
arr[i] = make_pair(a, b);
}
long long dp[n + 1][w + 1];
for (int i = 0; i <= n; ++i) {
dp[i][0] = 0;
}
for (int i = 0; i <= w; ++i) {
dp[0][i] = 0;
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= w; ++j) {
if (j - arr[i - 1].first >= 0) {
dp[i][j] = max(arr[i - 1].second + dp[i - 1][j - arr[i - 1].first],
dp[i - 1][j]);
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
// long long val = 0;
// for (int i = 0; i < n + 1; ++i) {
// for (int j = 0; j <= w; ++j)
// {
// val = max(val, dp[i][j]);
// cout << dp[i][j] << "\t";
// }
// cout << endl;
// }
cout << dp[n][w] << endl;
return 0;
}
|
replace
| 42 | 43 | 42 | 43 |
1
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
long long dp[2][(unsigned int)(1e5 + 1)];
int main() {
int n, w;
cin >> n >> w;
pair<long long, long long> arr[n];
for (int i = 0; i < n; ++i) {
long long a, b;
cin >> a >> b;
arr[i] = make_pair(a, b);
}
for (int i = 0; i <= 1; ++i) {
dp[i][0] = 0;
}
for (int i = 0; i <= w; ++i) {
dp[0][i] = 0;
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= w; ++j) {
if (j - arr[i - 1].first >= 0) {
dp[i % 2][j] =
max(arr[i - 1].second + dp[(i + 1) % 2][j - arr[i - 1].first],
dp[(i + 1) % 2][j]);
} else {
dp[i % 2][j] = dp[(i + 1) % 2][j];
}
// cout << dp[i % 2 ][j] << "\t";
}
// cout << endl;
}
// long long val = 0;
// for (int i = 0; i < n + 1; ++i) {
// for (int j = 0; j <= w; ++j)
// {
// val = max(val, dp[i][j]);
// cout << dp[i][j] << "\t";
// }
// cout << endl;
// }
cout << dp[n % 2][w] << endl;
return 1;
}
|
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
long long dp[2][(unsigned int)(1e5 + 1)];
int main() {
int n, w;
cin >> n >> w;
pair<long long, long long> arr[n];
for (int i = 0; i < n; ++i) {
long long a, b;
cin >> a >> b;
arr[i] = make_pair(a, b);
}
for (int i = 0; i <= 1; ++i) {
dp[i][0] = 0;
}
for (int i = 0; i <= w; ++i) {
dp[0][i] = 0;
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= w; ++j) {
if (j - arr[i - 1].first >= 0) {
dp[i % 2][j] =
max(arr[i - 1].second + dp[(i + 1) % 2][j - arr[i - 1].first],
dp[(i + 1) % 2][j]);
} else {
dp[i % 2][j] = dp[(i + 1) % 2][j];
}
// cout << dp[i % 2 ][j] << "\t";
}
// cout << endl;
}
// long long val = 0;
// for (int i = 0; i < n + 1; ++i) {
// for (int j = 0; j <= w; ++j)
// {
// val = max(val, dp[i][j]);
// cout << dp[i][j] << "\t";
// }
// cout << endl;
// }
cout << dp[n % 2][w] << endl;
return 0;
}
|
replace
| 44 | 45 | 44 | 45 |
1
| |
p03163
|
Python
|
Time Limit Exceeded
|
#!/usr/bin/env python3
def main():
import numpy as np
N, W = map(int, input().split())
products = [list(map(int, input().split())) for _ in range(N)]
dp = np.zeros((N * 2, (W + 1)), dtype=np.int64)
for n in range(N):
for w in range(W + 1):
can_w, can_v = products[n]
if w >= can_w:
dp[n + 1][w] = max(dp[n + 1][w], dp[n][w - can_w] + can_v)
dp[n + 1][w] = max(dp[n + 1][w], dp[n][w])
print(np.max(dp))
if __name__ == "__main__":
main()
|
#!/usr/bin/env python3
def main():
import numpy as np
N, W = map(int, input().split())
products = [list(map(int, input().split())) for _ in range(N)]
dp = np.zeros(W + 1, dtype=np.int64)
for can_w, can_v in products:
dp[can_w:] = np.maximum(dp[can_w:], dp[:-can_w] + can_v)
print(np.max(dp))
if __name__ == "__main__":
main()
|
replace
| 7 | 14 | 7 | 11 |
TLE
| |
p03163
|
Python
|
Runtime Error
|
N, W = map(int, input().split())
wv = []
for _ in range(N):
wv.append(list(map(int, input().split())))
# print(wv)
dp = [[0] * 110 for _ in range(100100)]
for i in range(N):
for sum_w in range(W + 1):
if wv[i][0] <= sum_w:
dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w - wv[i][0]] + wv[i][1])
dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w])
print(dp[N][W])
|
N, W = map(int, input().split())
wv = []
for _ in range(N):
wv.append(list(map(int, input().split())))
# print(wv)
dp = [[0] * 100100 for _ in range(110)]
for i in range(N):
for sum_w in range(W + 1):
if wv[i][0] <= sum_w:
dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w - wv[i][0]] + wv[i][1])
dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w])
print(dp[N][W])
|
replace
| 5 | 6 | 5 | 6 |
1
|
Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03163/Python/s607479372.py", line 6, in <module>
dp = [[0] * 110 for _ in range(100100)]
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03163/Python/s607479372.py", line 6, in <listcomp>
dp = [[0] * 110 for _ in range(100100)]
MemoryError
|
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
const long long INF = 1LL << 60;
using pll = pair<long long, long long>;
typedef long long ll;
#define min(a, b) ((a) < (b) ? (a) : (b))
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
int main() {
int N;
long W;
long long weight[110];
long long value[110];
long long dp[110][110] = {0};
cin >> N >> W;
for (int i = 0; i < N; i++)
cin >> weight[i] >> value[i];
for (int i = 0; i < N; ++i) {
for (int sum_w = 0; sum_w <= W; ++sum_w) {
if (sum_w >= weight[i]) {
chmax(dp[i + 1][sum_w], value[i] + dp[i][sum_w - weight[i]]);
}
chmax(dp[i + 1][sum_w], dp[i][sum_w]);
}
}
cout << dp[N][W] << endl;
}
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
const long long INF = 1LL << 60;
using pll = pair<long long, long long>;
typedef long long ll;
#define min(a, b) ((a) < (b) ? (a) : (b))
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
int main() {
int N;
long W;
long long weight[110];
long long value[110];
long long dp[110][100010] = {0};
cin >> N >> W;
for (int i = 0; i < N; i++)
cin >> weight[i] >> value[i];
for (int i = 0; i < N; ++i) {
for (int sum_w = 0; sum_w <= W; ++sum_w) {
if (sum_w >= weight[i]) {
chmax(dp[i + 1][sum_w], value[i] + dp[i][sum_w - weight[i]]);
}
chmax(dp[i + 1][sum_w], dp[i][sum_w]);
}
}
cout << dp[N][W] << endl;
}
|
replace
| 42 | 43 | 42 | 43 |
0
| |
p03163
|
C++
|
Runtime Error
|
/*
Written by Nitrogens
Desire for getting accepted!!
*/
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;
const int maxn = 1e2 + 5;
const int Mod = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e = exp(1);
const db PI = acos(-1);
const db ERR = 1e-10;
#define Se second
#define Fi first
#define pb push_back
#define dbg(x) cout << #x << " = " << (x) << endl
#define dbg2(x1, x2) \
cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << endl
#define dbg3(x1, x2, x3) \
cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << " " << #x3 \
<< " = " << x3 << endl
ll w[maxn], v[maxn];
ll dp[100][100005];
int main() {
// ios::sync_with_stdio(false);
// freopen("a.txt","r",stdin);
// freopen("b.txt","w",stdout);
int n, W;
scanf("%d%d", &n, &W);
for (int i = 1; i <= n; i++)
scanf("%lld%lld", &w[i], &v[i]);
memset(dp, 0, sizeof(dp));
ll answer = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= W; j++) {
if (j >= w[i])
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
else
dp[i][j] = dp[i - 1][j];
answer = max(answer, dp[i][j]);
}
}
printf("%lld\n", answer);
// cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" <<
// endl;
return 0;
}
|
/*
Written by Nitrogens
Desire for getting accepted!!
*/
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;
const int maxn = 1e2 + 5;
const int Mod = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e = exp(1);
const db PI = acos(-1);
const db ERR = 1e-10;
#define Se second
#define Fi first
#define pb push_back
#define dbg(x) cout << #x << " = " << (x) << endl
#define dbg2(x1, x2) \
cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << endl
#define dbg3(x1, x2, x3) \
cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << " " << #x3 \
<< " = " << x3 << endl
ll w[maxn], v[maxn];
ll dp[105][100005];
int main() {
// ios::sync_with_stdio(false);
// freopen("a.txt","r",stdin);
// freopen("b.txt","w",stdout);
int n, W;
scanf("%d%d", &n, &W);
for (int i = 1; i <= n; i++)
scanf("%lld%lld", &w[i], &v[i]);
memset(dp, 0, sizeof(dp));
ll answer = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= W; j++) {
if (j >= w[i])
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
else
dp[i][j] = dp[i - 1][j];
answer = max(answer, dp[i][j]);
}
}
printf("%lld\n", answer);
// cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" <<
// endl;
return 0;
}
|
replace
| 46 | 47 | 46 | 47 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using Vl = vector<ll>;
using pll = pair<ll, ll>;
using vb = vector<bool>;
using vs = vector<string>;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
ll N, W;
Vl w(N), v(N);
ll dp[102][100002]; // 0
int main() {
cin >> N >> W;
w.resize(N);
v.resize(N);
REP(i, N) cin >> w[i] >> v[i];
REP(i, N) REP(wi, W + 1) {
if (wi >= w[i]) {
dp[i + 1][wi] = max(dp[i][wi - w[i]] + v[i], dp[i][wi]);
} else {
dp[i + i][wi] = dp[i][wi];
}
}
cout << dp[N][W];
return 0;
}
|
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using Vl = vector<ll>;
using pll = pair<ll, ll>;
using vb = vector<bool>;
using vs = vector<string>;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
ll N, W;
Vl w(N), v(N);
ll dp[102][100002]; // 0
int main() {
cin >> N >> W;
w.resize(N);
v.resize(N);
REP(i, N) cin >> w[i] >> v[i];
REP(i, N) REP(wi, W + 1) {
if (wi >= w[i]) {
dp[i + 1][wi] = max(dp[i][wi - w[i]] + v[i], dp[i][wi]);
} else {
dp[i + 1][wi] = dp[i][wi];
}
}
cout << dp[N][W];
return 0;
}
|
replace
| 42 | 43 | 42 | 43 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define ll long long
#define faltu \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define binary(x) \
std::string binary = std::bitset<25>(x).to_string(); // to binary
#define inf 1000000007
#define mod 1000000007
#define pi pair<int, int>
#define lpi pair<int, int>
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define lvi vector<long long int>
#define pie 3.14159265358979323846
ll int MMM = inf;
int pw(int x, int y) {
int z = 1;
for (; y; y >>= 1, x = 1ll * x * x % MMM)
if (y & 1)
z = 1ll * z * x % MMM;
return z;
}
using namespace std;
vi pm;
void prm(int n) {
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
for (int p = 2; p <= n; p++)
if (prime[p])
pm.pb(p);
}
bool sortbysecdesc(const pair<ll int, ll int> &a,
const pair<ll int, ll int> &b) {
return a.first > b.first;
}
bool cmp(int x, int y) { return x > y; }
ll int dp[100][100002];
int main() {
faltu;
ll int n, ww;
cin >> n >> ww;
ll int val[n + 1], we[n + 1];
for (ll int i = 0; i < n; i++) {
cin >> we[i] >> val[i];
}
for (ll int i = 0; i <= n; i++) {
for (ll int w = 0; w <= ww; w++) {
if (i == 0 || w == 0)
dp[i][w] = 0;
else if (we[i - 1] <= w) {
dp[i][w] = max(val[i - 1] + dp[i - 1][w - we[i - 1]], dp[i - 1][w]);
} else
dp[i][w] = dp[i - 1][w];
}
}
cout << dp[n][ww];
}
|
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define ll long long
#define faltu \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define binary(x) \
std::string binary = std::bitset<25>(x).to_string(); // to binary
#define inf 1000000007
#define mod 1000000007
#define pi pair<int, int>
#define lpi pair<int, int>
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define lvi vector<long long int>
#define pie 3.14159265358979323846
ll int MMM = inf;
int pw(int x, int y) {
int z = 1;
for (; y; y >>= 1, x = 1ll * x * x % MMM)
if (y & 1)
z = 1ll * z * x % MMM;
return z;
}
using namespace std;
vi pm;
void prm(int n) {
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
for (int p = 2; p <= n; p++)
if (prime[p])
pm.pb(p);
}
bool sortbysecdesc(const pair<ll int, ll int> &a,
const pair<ll int, ll int> &b) {
return a.first > b.first;
}
bool cmp(int x, int y) { return x > y; }
ll int dp[205][200002];
int main() {
faltu;
ll int n, ww;
cin >> n >> ww;
ll int val[n + 1], we[n + 1];
for (ll int i = 0; i < n; i++) {
cin >> we[i] >> val[i];
}
for (ll int i = 0; i <= n; i++) {
for (ll int w = 0; w <= ww; w++) {
if (i == 0 || w == 0)
dp[i][w] = 0;
else if (we[i - 1] <= w) {
dp[i][w] = max(val[i - 1] + dp[i - 1][w - we[i - 1]], dp[i - 1][w]);
} else
dp[i][w] = dp[i - 1][w];
}
}
cout << dp[n][ww];
}
|
replace
| 50 | 51 | 50 | 51 |
0
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, k;
int a[N];
int b[N];
long long dp[105][N];
long long f(int idx, int weight) {
if (weight > k)
return -1e9;
if (idx >= n)
return 0;
long long &ret = dp[idx][weight];
ret = max(f(idx + 1, weight + a[idx]) + b[idx], f(idx + 1, weight));
return ret;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
}
memset(dp, -1, sizeof dp);
cout << f(0, 0) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, k;
int a[N];
int b[N];
long long dp[105][N];
long long f(int idx, int weight) {
if (weight > k)
return -1e9;
if (idx >= n)
return 0;
long long &ret = dp[idx][weight];
if (ret != -1)
return ret;
ret = max(f(idx + 1, weight + a[idx]) + b[idx], f(idx + 1, weight));
return ret;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
}
memset(dp, -1, sizeof dp);
cout << f(0, 0) << endl;
}
|
insert
| 14 | 14 | 14 | 16 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define endl '\n'
#define N 100005
using namespace std;
ll w[N], v[N], n, have;
ll dp[105][N];
ll rec(int can, int i) {
if (i == n)
return 0;
if (dp[can][i] != -1)
return dp[can][i];
ll ans = 0;
if (can >= w[i])
ans = max(v[i] + rec(can - w[i], i + 1), rec(can, i + 1));
else
ans = rec(can, i + 1);
dp[can][i] = ans;
return dp[can][i];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> have;
for (int i = 0; i <= have; i++) {
for (int j = 0; j <= n; j++)
dp[i][j] = -1;
}
for (int i = 0; i < n; i++)
cin >> w[i] >> v[i];
cout << rec(have, 0) << endl;
}
|
#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define endl '\n'
#define N 100005
using namespace std;
ll w[N], v[N], n, have;
ll dp[N][105];
ll rec(int can, int i) {
if (i == n)
return 0;
if (dp[can][i] != -1)
return dp[can][i];
ll ans = 0;
if (can >= w[i])
ans = max(v[i] + rec(can - w[i], i + 1), rec(can, i + 1));
else
ans = rec(can, i + 1);
dp[can][i] = ans;
return dp[can][i];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> have;
for (int i = 0; i <= have; i++) {
for (int j = 0; j <= n; j++)
dp[i][j] = -1;
}
for (int i = 0; i < n; i++)
cin >> w[i] >> v[i];
cout << rec(have, 0) << endl;
}
|
replace
| 9 | 10 | 9 | 10 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll mod = 1e9;
int main() {
int N, W, w[100], v[100];
cin >> N >> W;
for (int i(0); i < N; i++)
cin >> w[i] >> v[i];
ll dp[101][10001] = {
0}; // i番目までを使って重さw以下になるように選んだ時の価値の最大値
for (int i(0); i < N; i++) {
for (int j(0); j <= W; j++) {
if (w[i] > j)
dp[i + 1][j] = dp[i][j];
else
dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
}
}
cout << dp[N][W] << endl;
}
|
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll mod = 1e9;
int main() {
int N, W, w[100], v[100];
cin >> N >> W;
for (int i(0); i < N; i++)
cin >> w[i] >> v[i];
ll dp[101][100001] = {
0}; // i番目までを使って重さw以下になるように選んだ時の価値の最大値
for (int i(0); i < N; i++) {
for (int j(0); j <= W; j++) {
if (w[i] > j)
dp[i + 1][j] = dp[i][j];
else
dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
}
}
cout << dp[N][W] << endl;
}
|
replace
| 10 | 11 | 10 | 11 |
0
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll mod = 1e9 + 7;
int w[101], v[101];
ll dp[101][100001] = {0};
int main() {
int N, W;
cin >> N >> W;
for (int i(1); i <= N; i++)
cin >> w[i] >> v[i];
for (int ni(1); ni <= N; ni++) {
for (int wi(0); wi <= W; wi++) {
if (wi - w[ni] >= 0) {
dp[ni][wi] = max(dp[ni - 1][wi], dp[ni - 1][wi - w[ni]] + v[ni]);
} else {
dp[ni][wi] = dp[ni - 1][wi];
}
}
}
for (int wi(0); wi <= W; wi++) {
for (int ni(0); ni <= N; ni++) {
cerr << dp[ni][wi] << " ";
}
cerr << endl;
}
cout << dp[N][W] << endl;
}
|
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll mod = 1e9 + 7;
int w[101], v[101];
ll dp[101][100001] = {0};
int main() {
int N, W;
cin >> N >> W;
for (int i(1); i <= N; i++)
cin >> w[i] >> v[i];
for (int ni(1); ni <= N; ni++) {
for (int wi(0); wi <= W; wi++) {
if (wi - w[ni] >= 0) {
dp[ni][wi] = max(dp[ni - 1][wi], dp[ni - 1][wi - w[ni]] + v[ni]);
} else {
dp[ni][wi] = dp[ni - 1][wi];
}
}
}
cout << dp[N][W] << endl;
}
|
delete
| 21 | 27 | 21 | 21 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = (a); i <= (b); i++)
#define PER(i, a, b) for (int i = (a); i >= (b); i--)
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(S) (S).begin(), (S).end()
#define pf push_front
#define pb push_back
#define mk make_pair
#define S second
#define F first
typedef long long ll;
typedef long double lf;
typedef pair<int, int> ii;
const int MAXN = 1e2 + 2;
const int MAX = 1e5 + 5;
int N, W, w[MAXN], b[MAXN];
ll pd[MAXN];
ll solve() {
REP(i, 1, N)
PER(j, W, 1) if (w[i] <= j) pd[j] = max(pd[j], pd[j - w[i]] + b[i]);
return pd[W];
}
int main(int argc, char **argv) {
scanf("%d%d", &N, &W);
REP(i, 1, N) scanf("%d%d", &w[i], &b[i]);
printf("%lld\n", solve());
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = (a); i <= (b); i++)
#define PER(i, a, b) for (int i = (a); i >= (b); i--)
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(S) (S).begin(), (S).end()
#define pf push_front
#define pb push_back
#define mk make_pair
#define S second
#define F first
typedef long long ll;
typedef long double lf;
typedef pair<int, int> ii;
const int MAXN = 1e2 + 2;
const int MAX = 1e5 + 5;
int N, W, w[MAXN], b[MAXN];
ll pd[MAX];
ll solve() {
REP(i, 1, N)
PER(j, W, 1) if (w[i] <= j) pd[j] = max(pd[j], pd[j - w[i]] + b[i]);
return pd[W];
}
int main(int argc, char **argv) {
scanf("%d%d", &N, &W);
REP(i, 1, N) scanf("%d%d", &w[i], &b[i]);
printf("%lld\n", solve());
return 0;
}
|
replace
| 18 | 19 | 18 | 19 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define mod 1000000007
#define vll vector<ll>
ll val[105], wt[105], ti[105][105];
ll func(ll len, ll w) {
if (len <= 0 || w <= 0)
return 0;
if (ti[len][w] != -1)
return ti[len][w];
if (wt[len] <= w) {
return ti[len][w] =
max(val[len] + func(len - 1, w - wt[len]), func(len - 1, w));
} else
return ti[len][w] = func(len - 1, w);
}
int main() {
// your code goes here
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n, w;
cin >> n >> w;
memset(ti, -1, sizeof ti);
for (ll i = 1; i <= n; i++)
cin >> wt[i] >> val[i];
ll ans = func(n, w);
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define mod 1000000007
#define vll vector<ll>
ll val[105], wt[105], ti[105][100005];
ll func(ll len, ll w) {
if (len <= 0 || w <= 0)
return 0;
if (ti[len][w] != -1)
return ti[len][w];
if (wt[len] <= w) {
return ti[len][w] =
max(val[len] + func(len - 1, w - wt[len]), func(len - 1, w));
} else
return ti[len][w] = func(len - 1, w);
}
int main() {
// your code goes here
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n, w;
cin >> n >> w;
memset(ti, -1, sizeof ti);
for (ll i = 1; i <= n; i++)
cin >> wt[i] >> val[i];
ll ans = func(n, w);
cout << ans << endl;
return 0;
}
|
replace
| 9 | 10 | 9 | 10 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sc scanf
#define scin(x) sc("%d", &(x))
#define scln(x) sc("%lld", &(x))
#define pf printf
#define NL pf("\n")
#define ms(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define mp make_pair
#define db double
#define EPS 10E-10
#define ff first
#define ss second
#define sqr(x) (x) * (x)
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define DBG pf("HI\n")
#define MOD 1000000007
#define CIN \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define RUN_CASE(t, T) for (__typeof(t) t = 1; t <= T; t++)
#define CASE(t) printf("Case %d: ", t)
#define CASEL(t) printf("Case %d:\n", t)
#define intlimit 2147483648
#define longlimit 9223372036854775808
#define infinity (1 << 28)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a) * (b) / gcd(a, b))
#define mx 1234567899
#define TC \
int t; \
scin(t); \
while (t--)
#define PI 2 * acos(0.0)
#define rep(i, a, b) for (i = a; i <= b; i++)
#define rev(i, a, b) for (i = a; i >= b; i--)
#define file \
freopen("E:\\file.txt", "r", stdin); \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define all(x) x.begin(), x.end()
#define DEBUG(args...) \
do { \
cerr << #args << ' '; \
print(args); \
} while (0); \
cerr << endl;
using namespace std;
int get(int n, int p) {
int res = 0;
while (n >= p)
res += n / p, n /= p;
return res;
}
template <class T> T power(T N, T P) {
return (P == 0) ? 1 : N * power(N, P - 1);
}
void VC(vector<ll> &v) {
for (int i = 0; i < v.size(); i++)
printf("%lld ", v[i]);
printf("\n");
}
template <typename T> void print(const T &v) { cerr << v << ' '; }
template <typename T1, typename... T2>
void print(const T1 &first, const T2 &...rest) {
print(first);
print(rest...);
}
///--------------Graph Moves--------------------------------------
/// const int fx[] = {+1,-1,+0,+0};
/// const int fy[] = {+0,+0,+1,-1};
/// const int fx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's move
/// const int fy[] = {-1,+1,+0,+0,+1,+1,-1,-1}; ///king's Move
/// const int fx[] = {-2,-2,-1,-1,+1,+1,+2,+2}; ///knight's move
/// const int fy[] = {-1,+1,-2,+2,-2,+2,-1,+1}; ///knight's move
///---------------------------------------------------------------
long long BigMod(long long int a, long long int b, int M) {
long long remainder, answer = 1;
remainder = a % M;
while (b != 0) {
if (b % 2 == 1)
answer = (answer * remainder) % M;
remainder = (remainder * remainder) % M;
b /= 2;
}
return answer;
}
bool cmp(int a, int b) { return a > b; }
ll capacity, n;
vector<pair<ll, ll>> W_V;
ll dp[105][105]; /// state are position and remaining weight
ll knapsack(ll position, ll weight) {
ll profit1 = 0, profit2 = 0;
if (position == n)
return 0;
if (dp[position][weight] != -1)
return dp[position][weight];
else {
if (W_V[position].ff + weight <= capacity)
profit1 =
W_V[position].ss + knapsack(position + 1, W_V[position].ff + weight);
profit2 = knapsack(position + 1, weight);
return dp[position][weight] = max(profit1, profit2);
}
}
int main() {
ll x, y;
cin >> n >> capacity;
ms(dp, -1);
for (int i = 0; i < n; i++) {
cin >> x >> y;
W_V.pb(mp(x, y));
}
ll ans = knapsack(0, 0);
pf("%lld\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sc scanf
#define scin(x) sc("%d", &(x))
#define scln(x) sc("%lld", &(x))
#define pf printf
#define NL pf("\n")
#define ms(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define mp make_pair
#define db double
#define EPS 10E-10
#define ff first
#define ss second
#define sqr(x) (x) * (x)
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define DBG pf("HI\n")
#define MOD 1000000007
#define CIN \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define RUN_CASE(t, T) for (__typeof(t) t = 1; t <= T; t++)
#define CASE(t) printf("Case %d: ", t)
#define CASEL(t) printf("Case %d:\n", t)
#define intlimit 2147483648
#define longlimit 9223372036854775808
#define infinity (1 << 28)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a) * (b) / gcd(a, b))
#define mx 1234567899
#define TC \
int t; \
scin(t); \
while (t--)
#define PI 2 * acos(0.0)
#define rep(i, a, b) for (i = a; i <= b; i++)
#define rev(i, a, b) for (i = a; i >= b; i--)
#define file \
freopen("E:\\file.txt", "r", stdin); \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define all(x) x.begin(), x.end()
#define DEBUG(args...) \
do { \
cerr << #args << ' '; \
print(args); \
} while (0); \
cerr << endl;
using namespace std;
int get(int n, int p) {
int res = 0;
while (n >= p)
res += n / p, n /= p;
return res;
}
template <class T> T power(T N, T P) {
return (P == 0) ? 1 : N * power(N, P - 1);
}
void VC(vector<ll> &v) {
for (int i = 0; i < v.size(); i++)
printf("%lld ", v[i]);
printf("\n");
}
template <typename T> void print(const T &v) { cerr << v << ' '; }
template <typename T1, typename... T2>
void print(const T1 &first, const T2 &...rest) {
print(first);
print(rest...);
}
///--------------Graph Moves--------------------------------------
/// const int fx[] = {+1,-1,+0,+0};
/// const int fy[] = {+0,+0,+1,-1};
/// const int fx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's move
/// const int fy[] = {-1,+1,+0,+0,+1,+1,-1,-1}; ///king's Move
/// const int fx[] = {-2,-2,-1,-1,+1,+1,+2,+2}; ///knight's move
/// const int fy[] = {-1,+1,-2,+2,-2,+2,-1,+1}; ///knight's move
///---------------------------------------------------------------
long long BigMod(long long int a, long long int b, int M) {
long long remainder, answer = 1;
remainder = a % M;
while (b != 0) {
if (b % 2 == 1)
answer = (answer * remainder) % M;
remainder = (remainder * remainder) % M;
b /= 2;
}
return answer;
}
bool cmp(int a, int b) { return a > b; }
ll capacity, n;
vector<pair<ll, ll>> W_V;
ll dp[105][100005]; /// state are position and remaining weight
ll knapsack(ll position, ll weight) {
ll profit1 = 0, profit2 = 0;
if (position == n)
return 0;
if (dp[position][weight] != -1)
return dp[position][weight];
else {
if (W_V[position].ff + weight <= capacity)
profit1 =
W_V[position].ss + knapsack(position + 1, W_V[position].ff + weight);
profit2 = knapsack(position + 1, weight);
return dp[position][weight] = max(profit1, profit2);
}
}
int main() {
ll x, y;
cin >> n >> capacity;
ms(dp, -1);
for (int i = 0; i < n; i++) {
cin >> x >> y;
W_V.pb(mp(x, y));
}
ll ans = knapsack(0, 0);
pf("%lld\n", ans);
return 0;
}
|
replace
| 101 | 102 | 101 | 102 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
ll w[100], v[100], dp[101][100001];
int main() {
int n, W;
cin >> n >> W;
for (int i = 0; i < n; ++i)
cin >> w[i] >> v[i];
for (int i = 0; i < n; ++i) {
for (int wi = 0; wi <= W; ++wi) {
dp[i + 1][wi] = max(dp[i + 1][wi], dp[i][wi]);
dp[i + 1][wi + w[i]] = max(dp[i + 1][wi + w[i]], dp[i][wi] + v[i]);
}
}
ll ans = 0LL;
for (int wi = 0; wi <= W; ++wi) {
ans = max(ans, dp[n][wi]);
}
cout << ans << "\n";
}
|
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
ll w[100], v[100], dp[101][100001];
int main() {
int n, W;
cin >> n >> W;
for (int i = 0; i < n; ++i)
cin >> w[i] >> v[i];
for (int i = 0; i < n; ++i) {
for (int wi = 0; wi <= W; ++wi) {
dp[i + 1][wi] = max(dp[i + 1][wi], dp[i][wi]);
if (wi + w[i] <= W)
dp[i + 1][wi + w[i]] = max(dp[i + 1][wi + w[i]], dp[i][wi] + v[i]);
}
}
ll ans = 0LL;
for (int wi = 0; wi <= W; ++wi) {
ans = max(ans, dp[n][wi]);
}
cout << ans << "\n";
}
|
replace
| 18 | 19 | 18 | 20 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <iostream>
using namespace std;
typedef long long ll;
ll dp[100][100005];
int main(int argc, char *argv[]) {
int N, W, w[100005], v[100005];
cin >> N >> W;
for (int i = 0; i < N; i++) {
cin >> w[i] >> v[i];
}
for (int i = 0; i <= W; i++) {
dp[0][i] = 0;
}
for (int i = 0; i < N; i++) {
for (int j = 0; j <= W; j++) {
if (j >= w[i]) {
dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j]);
} else {
dp[i + 1][j] = dp[i][j];
}
}
}
cout << dp[N][W] << endl;
return 0;
}
|
#include <iostream>
using namespace std;
typedef long long ll;
ll dp[105][100005];
int main(int argc, char *argv[]) {
int N, W, w[100005], v[100005];
cin >> N >> W;
for (int i = 0; i < N; i++) {
cin >> w[i] >> v[i];
}
for (int i = 0; i <= W; i++) {
dp[0][i] = 0;
}
for (int i = 0; i < N; i++) {
for (int j = 0; j <= W; j++) {
if (j >= w[i]) {
dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j]);
} else {
dp[i + 1][j] = dp[i][j];
}
}
}
cout << dp[N][W] << endl;
return 0;
}
|
replace
| 6 | 7 | 6 | 7 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n, w;
cin >> n >> w;
ll dp[n + 1][w + 1];
ll a[n + 1], b[n + 1];
for (ll i = 1; i <= n; i++)
cin >> a[i] >> b[i];
for (ll i = 0; i <= w; i++)
dp[0][i] = 0;
for (ll i = 1; i <= n; i++) {
for (ll j = 0; j <= w; j++) {
// if(j>=a[i])
dp[i][j] = max((b[i] + dp[i - 1][j - a[i]]), dp[i - 1][j]);
}
}
cout << dp[n][w];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n, w;
cin >> n >> w;
ll dp[n + 1][w + 1];
ll a[n + 1], b[n + 1];
for (ll i = 1; i <= n; i++)
cin >> a[i] >> b[i];
for (ll i = 0; i <= w; i++)
dp[0][i] = 0;
for (ll i = 1; i <= n; i++) {
for (ll j = 0; j <= w; j++) {
if (j >= a[i])
dp[i][j] = max((b[i] + dp[i - 1][j - a[i]]), dp[i - 1][j]);
else
dp[i][j] = dp[i - 1][j];
}
}
cout << dp[n][w];
return 0;
}
|
replace
| 17 | 19 | 17 | 21 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9 + 5;
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int n;
cin >> n;
int W;
cin >> W;
vector<int> w(n + 1);
vector<int> v(n + 1);
for (int i = 1; i <= n; i++) {
cin >> w[i];
cin >> v[i];
}
ll dp[n + 1][W + 1];
dp[0][0] = 0;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= W; j++) {
dp[i][j] = 0;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= W; j++) {
if (w[i] <= j)
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
else
dp[i][j] = dp[i - 1][j];
}
}
cout << dp[n][W] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9 + 5;
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int n;
cin >> n;
int W;
cin >> W;
vector<int> w(n + 1);
vector<int> v(n + 1);
for (int i = 1; i <= n; i++) {
cin >> w[i];
cin >> v[i];
}
ll dp[n + 1][W + 1];
dp[0][0] = 0;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= W; j++) {
dp[i][j] = 0;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= W; j++) {
if (w[i] <= j)
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
else
dp[i][j] = dp[i - 1][j];
}
}
cout << dp[n][W] << endl;
return 0;
}
|
replace
| 5 | 7 | 5 | 7 |
-11
| |
p03163
|
C++
|
Runtime Error
|
#include "bits/stdc++.h"
using namespace std;
// int h[100001];
unsigned long long dp[100001] = {};
/*
int solve(int i){
if(i == 0) return 0;
if(i == 1) return 0;
if(memo[i] != -1) return memo[i];
int m = INT_MAX;
for(int d = 1; d <= k; d++){
if(i - d < 0) break;
m = min(solve(i-d, k)+abs(h[i]-h[i-d]), m);
}
return memo[i] = m;
}
*/
int main() {
int n;
cin >> n;
int w;
cin >> w;
pair<int, int> wv[10];
for (int i = 0; i < n; i++) {
cin >> wv[i].first >> wv[i].second;
}
dp[0] = 0;
unsigned long long ans = 0;
for (int j = 0; j < n; j++) {
for (int i = w - wv[j].first; i >= 0; i--) {
dp[i + wv[j].first] = max(dp[i] + wv[j].second, dp[i + wv[j].first]);
ans = max(ans, dp[i + wv[j].first]);
}
}
cout << ans << endl;
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
// int h[100001];
unsigned long long dp[100001] = {};
/*
int solve(int i){
if(i == 0) return 0;
if(i == 1) return 0;
if(memo[i] != -1) return memo[i];
int m = INT_MAX;
for(int d = 1; d <= k; d++){
if(i - d < 0) break;
m = min(solve(i-d, k)+abs(h[i]-h[i-d]), m);
}
return memo[i] = m;
}
*/
int main() {
int n;
cin >> n;
int w;
cin >> w;
pair<int, int> wv[100];
for (int i = 0; i < n; i++) {
cin >> wv[i].first >> wv[i].second;
}
dp[0] = 0;
unsigned long long ans = 0;
for (int j = 0; j < n; j++) {
for (int i = w - wv[j].first; i >= 0; i--) {
dp[i + wv[j].first] = max(dp[i] + wv[j].second, dp[i + wv[j].first]);
ans = max(ans, dp[i + wv[j].first]);
}
}
cout << ans << endl;
return 0;
}
|
replace
| 28 | 29 | 28 | 29 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N, W;
cin >> N >> W;
vector<long> w(N + 1), v(N + 1);
for (int i = 1; i <= N; ++i)
cin >> w[i] >> v[i];
// dp[i][j]: i 番目までの品物から重さの和が j 以下になるように
// 選んだときの価値の最大値
long dp[101][10001];
for (int j = 0; j <= W; ++j)
dp[0][j] = 0;
for (int i = 1; i <= N; ++i) {
for (int j = 0; j <= W; ++j) {
if (j >= w[i])
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
else
dp[i][j] = dp[i - 1][j];
}
}
cout << dp[N][W] << endl;
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N, W;
cin >> N >> W;
vector<long> w(N + 1), v(N + 1);
for (int i = 1; i <= N; ++i)
cin >> w[i] >> v[i];
// dp[i][j]: i 番目までの品物から重さの和が j 以下になるように
// 選んだときの価値の最大値
long dp[101][100001];
for (int j = 0; j <= W; ++j)
dp[0][j] = 0;
for (int i = 1; i <= N; ++i) {
for (int j = 0; j <= W; ++j) {
if (j >= w[i])
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
else
dp[i][j] = dp[i - 1][j];
}
}
cout << dp[N][W] << endl;
return 0;
}
|
replace
| 14 | 15 | 14 | 15 |
0
| |
p03163
|
Python
|
Time Limit Exceeded
|
N, M = map(int, input().split())
W, V = [], []
for n in range(N):
w, v = map(int, input().split())
W.append(w)
V.append(v)
def f(n, m):
if n == 0 or m <= 0:
return 0
elif m < W[n - 1]:
return f(n - 1, m)
else:
return max(f(n - 1, m), f(n - 1, m - W[n - 1]) + V[n - 1])
print(f(N, M))
|
N, M = map(int, input().split())
W, V = [], []
for n in range(N):
w, v = map(int, input().split())
W.append(w)
V.append(v)
f = [[0 for m in range(M + 1)] for n in range(N + 1)]
for n in range(N):
for m in range(M + 1):
if m < W[n]:
f[n + 1][m] = f[n][m]
else:
f[n + 1][m] = max(f[n][m], f[n][m - W[n]] + V[n])
print(f[N][M])
|
replace
| 6 | 18 | 6 | 14 |
TLE
| |
p03163
|
Python
|
Time Limit Exceeded
|
import numpy as np
n, W = map(int, input().split())
wv = np.array([list(map(int, input().split())) for i in range(n)])
dp = np.zeros((n + 1, W + 1), dtype=np.int)
def chmax(a, b):
if a > b:
return a
else:
return b
for i in range(n):
for sum_w in range(W + 1):
if sum_w >= wv[i, 0]:
dp[i + 1][sum_w] = chmax(
dp[i + 1][sum_w], dp[i][sum_w - wv[i, 0]] + wv[i, 1]
)
dp[i + 1][sum_w] = chmax(dp[i + 1][sum_w], dp[i][sum_w])
print(int(dp[n, W]))
|
import numpy as np
n, W = map(int, input().split())
wv = np.array([list(map(int, input().split())) for i in range(n)])
dp = np.zeros((n + 1, W + 1), dtype=np.int)
def chmax(a, b):
if a > b:
return a
else:
return b
# # 遅い
# for i in range(n):
# for sum_w in range(W + 1):
# if sum_w >= wv[i, 0]:
# dp[i + 1][sum_w] = chmax(dp[i + 1][sum_w], dp[i][sum_w - wv[i, 0]] + wv[i, 1])
# dp[i + 1][sum_w] = chmax(dp[i + 1][sum_w], dp[i][sum_w])
for i in range(0, n):
w, v = wv[i, :]
dp[i + 1, w:] = np.maximum(dp[i, w:], dp[i, :-w] + v)
dp[i + 1, :] = np.maximum(dp[i + 1, :], dp[i, :])
print(int(dp[n, W]))
|
replace
| 15 | 22 | 15 | 26 |
TLE
| |
p03163
|
Python
|
Time Limit Exceeded
|
def main():
N, W = map(int, input().split())
wi, vi = map(int, input().split())
dp = [0] * wi + [vi] * (W + 1 - wi)
for _ in range(1, N):
wi, vi = map(int, input().split())
dp[wi:] = [max(i + vi, j) for i, j in zip(dp, dp[wi:])]
print(dp[-1])
if __name__ == "__main__":
main()
|
def main():
N, W = map(int, input().split())
wi, vi = map(int, input().split())
dp = [0] * wi + [vi] * (W + 1 - wi)
for _ in range(1, N):
wi, vi = map(int, input().split())
for i in range(W, wi - 1, -1):
t = dp[i - wi] + vi
if t > dp[i]:
dp[i] = t
print(dp[-1])
if __name__ == "__main__":
main()
|
replace
| 6 | 7 | 6 | 10 |
TLE
| |
p03163
|
Python
|
Time Limit Exceeded
|
N, W = [int(_) for _ in input().split()]
WV = sorted([[int(_) for _ in input().split()] for _ in range(N)], reverse=True)
dp = {}
dp[0] = 0
for w, v in WV:
dp_o = dp.copy()
for k in dp_o:
if k + w <= W:
dp[k + w] = max(dp_o.get(k + w, 0), dp_o[k] + v)
print(max([v for k, v in dp.items()]))
|
N, W = [int(_) for _ in input().split()]
WV = [[int(_) for _ in input().split()] for _ in range(N)]
dp = {}
dp[0] = 0
for w, v in WV:
dp_o = dp.copy()
for k in dp_o:
if k + w <= W:
dp[k + w] = max(dp_o.get(k + w, 0), dp_o[k] + v)
print(max([v for k, v in dp.items()]))
|
replace
| 1 | 2 | 1 | 2 |
TLE
| |
p03163
|
Python
|
Runtime Error
|
# using dp with memoization, TLE
from sys import stdin
N, W = map(int, input().split())
m = stdin.read().splitlines()
s = [[int(i) for i in line.split()] for line in m]
dp = [0] * (W + 1)
next_dp = [0] * (W + 1)
for n in range(1, N + 1):
for w in range(0, W + 1):
if w >= s[n - 1][0]:
next_dp[w] = max(next_dp[w], dp[w - s[n - 1][0]] + s[n - 1][1])
dp = next_dp.copy()
print(dp[W])
|
# using dp with memoization, TLE
from sys import stdin
N, W = map(int, input().split())
m = stdin.read().splitlines()
s = [[int(i) for i in line.split()] for line in m]
dp = [0] * (W + 1)
next_dp = [0] * (W + 1)
for n in range(1, N + 1):
for w in range(0, W + 1):
if w >= s[n - 1][0]:
next_dp[w] = max(next_dp[w], dp[w], dp[w - s[n - 1][0]] + s[n - 1][1])
dp = next_dp[:]
print(dp[W])
|
replace
| 12 | 14 | 12 | 14 |
0
| |
p03163
|
Python
|
Time Limit Exceeded
|
def knapsack1(weight_value, w):
cur = [0] * (w + 1)
nxt = cur.copy()
for weight, value in weight_value:
for i in range(1, weight):
nxt[i] = cur[i]
for i in range(weight, w + 1):
nxt[i] = max(cur[i], cur[i - weight] + value)
cur, nxt = nxt, cur
return cur[w]
def main():
n, w = [int(x) for x in input().split()]
weight_value = [None] * n
for i in range(n):
weight_value[i] = [int(x) for x in input().split()]
return knapsack1(weight_value, w)
print(main())
|
def knapsack1(weight_value, w):
cur = [0] * (w + 1)
nxt = [0] * (w + 1)
for weight, value in weight_value:
for i in range(1, weight):
nxt[i] = cur[i]
for i in range(weight, w + 1):
nxt[i] = max(cur[i], cur[i - weight] + value)
cur, nxt = nxt, cur
return cur[w]
def main():
n, w = [int(x) for x in input().split()]
weight_value = [None] * n
for i in range(n):
weight_value[i] = [int(x) for x in input().split()]
return knapsack1(weight_value, w)
print(main())
|
replace
| 2 | 3 | 2 | 3 |
TLE
| |
p03163
|
Python
|
Time Limit Exceeded
|
def main():
item_count, capacity = [int(x) for x in input().split()]
weights, values = [], []
for _ in range(item_count):
w, v = [int(x) for x in input().split()]
weights.append(w)
values.append(v)
# dp[i][j]
# = (i番目までの品物を、重さがj以下になるように選んだときの価値の最大値)
dp = [[0] * (capacity + 1) for _ in range(item_count)]
for item_i in range(item_count):
for weight_i in range(1, capacity + 1):
if (w := weights[item_i]) <= weight_i:
dp[item_i][weight_i] = max(
dp[item_i - 1][weight_i - w] + values[item_i],
dp[item_i - 1][weight_i],
)
else:
dp[item_i][weight_i] = dp[item_i - 1][weight_i]
print(dp[item_count - 1][capacity])
if __name__ == "__main__":
main()
|
def main():
item_count, capacity = [int(x) for x in input().split()]
weights, values = [], []
for _ in range(item_count):
w, v = [int(x) for x in input().split()]
weights.append(w)
values.append(v)
# dp[i][j]
# = (i番目までの品物を、重さがj以下になるように選んだときの価値の最大値)
dp = [[0] * (capacity + 1) for _ in range(item_count)]
for item_i in range(item_count):
for weight_i in range(1, capacity + 1):
w = weights[item_i]
if w <= weight_i:
dp[item_i][weight_i] = max(
dp[item_i - 1][weight_i - w] + values[item_i],
dp[item_i - 1][weight_i],
)
else:
dp[item_i][weight_i] = dp[item_i - 1][weight_i]
print(dp[item_count - 1][capacity])
if __name__ == "__main__":
main()
|
replace
| 12 | 13 | 12 | 14 |
TLE
| |
p03163
|
Python
|
Time Limit Exceeded
|
import sys
sys.setrecursionlimit(10**6)
INF = float("inf")
MOD = 10**9 + 7
def input():
return sys.stdin.readline().strip()
def main():
N, W = map(int, input().split())
item = []
for _ in range(N):
w, v = map(int, input().split())
item.append((w, v))
dp = [0] * (W + 1)
for w, v in item:
for i in range(W + 1)[::-1]:
if i - w >= 0:
tmp = dp[i - w] + v
if tmp > dp[i]:
dp[i] = tmp
print(max(dp))
if __name__ == "__main__":
main()
|
import sys
sys.setrecursionlimit(10**6)
INF = float("inf")
MOD = 10**9 + 7
def input():
return sys.stdin.readline().strip()
def main():
N, W = map(int, input().split())
item = []
for _ in range(N):
w, v = map(int, input().split())
item.append((w, v))
dp = [0] * (W + 1)
for w, v in item:
for i in range(w, W + 1)[::-1]:
tmp = dp[i - w] + v
if tmp > dp[i]:
dp[i] = tmp
print(max(dp))
if __name__ == "__main__":
main()
|
replace
| 20 | 25 | 20 | 24 |
TLE
| |
p03163
|
Python
|
Time Limit Exceeded
|
import sys
def main():
input = sys.stdin.readline
N, W = map(int, input().split())
dp = [0] * (W + 1)
WV = (map(int, input().split()) for _ in range(N))
for w, v in WV:
for j in range(W, w - 1, -1):
dp[j] = max(dp[j], dp[j - w] + v)
print(dp[-1])
main()
|
import sys
def main():
input = sys.stdin.readline
N, W = map(int, input().split())
dp = [0] * (W + 1)
WV = (map(int, input().split()) for _ in range(N))
for w, v in WV:
for j in range(W, w - 1, -1):
tmp = dp[j - w] + v
if tmp > dp[j]:
dp[j] = tmp
print(dp[-1])
main()
|
replace
| 13 | 14 | 13 | 16 |
TLE
| |
p03163
|
Python
|
Runtime Error
|
import sys
sys.setrecursionlimit(10**8)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, W = map(int, readline().split())
things = []
for i in range(N):
weight, value = map(int, readline().split())
things.append((weight, value))
def solve():
dp = [0] * (W + 1)
w0, v0 = things[0]
for i in range(w0, W + 1):
dp[i] = v0
for i in range(1, N):
dn = dp.copy()
weight, value = things[i]
for j in range(weight, W + 1):
dn[j] = max(dn[j], dp[j - weight] + value)
dp = dn
return dp[W]
if __name__ == "__main__":
print(solve())
|
import sys
sys.setrecursionlimit(10**8)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, W = map(int, readline().split())
things = []
for i in range(N):
weight, value = map(int, readline().split())
things.append((weight, value))
def solve():
dp = [0] * (W + 1)
w0, v0 = things[0]
for i in range(w0, W + 1):
dp[i] = v0
for i in range(1, N):
dn = list(dp)
weight, value = things[i]
for j in range(weight, W + 1):
dn[j] = max(dn[j], dp[j - weight] + value)
dp = dn
return dp[W]
if __name__ == "__main__":
print(solve())
|
replace
| 20 | 21 | 20 | 21 |
0
| |
p03163
|
Python
|
Time Limit Exceeded
|
#!/usr/bin/env python3
# vim: set fileencoding=utf-8
# pylint: disable=unused-import, invalid-name, missing-docstring, bad-continuation
"""Module docstring
"""
import sys
EMPTY = (0, 0, frozenset())
def solve(values, _nb, max_capacity):
dp = [0] * (max_capacity + 1)
for w, v in values:
for j in range(max_capacity, w - 1, -1):
if dp[j] < dp[j - w] + v:
dp[j] = dp[j - w] + v
return dp[max_capacity]
def solve_better(values, _nb, max_capacity):
dp = [0] * (max_capacity + 1)
for w, v in values:
# dp = dp[:w] + [max(dp[j], dp[j - w] + v) for j in range(w, max_capacity + 1)]
dp[w:] = [max(dpj, dpjw + v) for dpj, dpjw in zip(dp[w:], dp[:-w])]
return dp[max_capacity]
def solve_slow(values, nb, max_capacity):
dp = [[0 for _ in range(max_capacity + 1)] for _ in range(nb + 1)]
for i in range(1, nb + 1):
w, v = values[i - 1]
for j in range(1, max_capacity + 1):
if w <= j:
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w] + v)
else:
dp[i][j] = dp[i - 1][j]
# print("\n".join(map(str, dp)))
return dp[nb][max_capacity]
def do_job():
"Do the work"
# first line is number of test cases
N, W = map(int, input().split())
values = []
for _ in range(N):
values.append(list(map(int, input().split())))
result = solve(values, N, W)
print(result)
def print_output(testcase, result) -> None:
"Formats and print result"
if result is None:
result = "IMPOSSIBLE"
print("Case #{}: {}".format(testcase + 1, result))
def main(argv=None):
"Program wrapper."
if argv is None:
argv = sys.argv[1:]
do_job()
return 0
if __name__ == "__main__":
import doctest
doctest.testmod()
sys.exit(main())
|
#!/usr/bin/env python3
# vim: set fileencoding=utf-8
# pylint: disable=unused-import, invalid-name, missing-docstring, bad-continuation
"""Module docstring
"""
import sys
EMPTY = (0, 0, frozenset())
def solve(values, _nb, max_capacity):
dp = [0] * (max_capacity + 1)
for w, v in values:
for j in range(max_capacity, w - 1, -1):
tmp = dp[j - w] + v
if dp[j] < tmp:
dp[j] = tmp
return dp[max_capacity]
def solve_better(values, _nb, max_capacity):
dp = [0] * (max_capacity + 1)
for w, v in values:
# dp = dp[:w] + [max(dp[j], dp[j - w] + v) for j in range(w, max_capacity + 1)]
dp[w:] = [max(dpj, dpjw + v) for dpj, dpjw in zip(dp[w:], dp[:-w])]
return dp[max_capacity]
def solve_slow(values, nb, max_capacity):
dp = [[0 for _ in range(max_capacity + 1)] for _ in range(nb + 1)]
for i in range(1, nb + 1):
w, v = values[i - 1]
for j in range(1, max_capacity + 1):
if w <= j:
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w] + v)
else:
dp[i][j] = dp[i - 1][j]
# print("\n".join(map(str, dp)))
return dp[nb][max_capacity]
def do_job():
"Do the work"
# first line is number of test cases
N, W = map(int, input().split())
values = []
for _ in range(N):
values.append(list(map(int, input().split())))
result = solve(values, N, W)
print(result)
def print_output(testcase, result) -> None:
"Formats and print result"
if result is None:
result = "IMPOSSIBLE"
print("Case #{}: {}".format(testcase + 1, result))
def main(argv=None):
"Program wrapper."
if argv is None:
argv = sys.argv[1:]
do_job()
return 0
if __name__ == "__main__":
import doctest
doctest.testmod()
sys.exit(main())
|
replace
| 18 | 20 | 18 | 21 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
//
#include <algorithm>
#include <array>
#include <bitset>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <set>
#include <vector>
//
using ll = long long;
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
inline void yes(bool cond) { cond ? puts("Yes") : puts("No"); }
template <typename Type> inline void chmin(Type &a, Type b) {
if (a > b)
a = b;
}
template <typename Type> inline void chmax(Type &a, Type b) {
if (a < b)
a = b;
}
template <typename Type> inline void sort(Type &arr) {
std::sort(arr.begin(), arr.end());
}
template <typename Type> inline Type nth(vector<Type> &arr, size_t pos) {
std::nth_element(arr.begin(), arr.begin() + pos, arr.end());
return arr[pos];
}
#define BIGP 1000000007
#define INF_I std::numeric_limits<ll>::max()
#define INF_F std::numeric_limits<float>::infinity()
//
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
//
ll N, W;
cin >> N >> W;
vector<pair<ll, ll>> ps;
rep(i, N) {
ll w, v;
cin >> w >> v;
ps.push_back({w, v});
}
vector<vector<ll>> dp;
dp.resize(N + 1);
rep(i, N + 1) { dp[i].resize(10001000, 0); }
for (ll item = 0; item < N; ++item) {
auto p = ps[item];
ll w = std::get<0>(p);
ll v = std::get<1>(p);
for (ll wi = 0; wi <= W; ++wi) {
chmax(dp[item + 1][wi], dp[item][wi]);
if (wi + w <= W) {
chmax(dp[item + 1][wi + w], dp[item][wi] + v);
}
}
}
ll h = dp[N][W];
cout << h;
//
return 0;
}
|
//
#include <algorithm>
#include <array>
#include <bitset>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <set>
#include <vector>
//
using ll = long long;
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
inline void yes(bool cond) { cond ? puts("Yes") : puts("No"); }
template <typename Type> inline void chmin(Type &a, Type b) {
if (a > b)
a = b;
}
template <typename Type> inline void chmax(Type &a, Type b) {
if (a < b)
a = b;
}
template <typename Type> inline void sort(Type &arr) {
std::sort(arr.begin(), arr.end());
}
template <typename Type> inline Type nth(vector<Type> &arr, size_t pos) {
std::nth_element(arr.begin(), arr.begin() + pos, arr.end());
return arr[pos];
}
#define BIGP 1000000007
#define INF_I std::numeric_limits<ll>::max()
#define INF_F std::numeric_limits<float>::infinity()
//
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
//
ll N, W;
cin >> N >> W;
vector<pair<ll, ll>> ps;
rep(i, N) {
ll w, v;
cin >> w >> v;
ps.push_back({w, v});
}
vector<vector<ll>> dp;
dp.resize(N + 1);
rep(i, N + 1) { dp[i].resize(100100, 0); }
for (ll item = 0; item < N; ++item) {
auto p = ps[item];
ll w = std::get<0>(p);
ll v = std::get<1>(p);
for (ll wi = 0; wi <= W; ++wi) {
chmax(dp[item + 1][wi], dp[item][wi]);
if (wi + w <= W) {
chmax(dp[item + 1][wi + w], dp[item][wi] + v);
}
}
}
ll h = dp[N][W];
cout << h;
//
return 0;
}
|
replace
| 48 | 49 | 48 | 49 |
-6
|
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, w;
cin >> n >> w;
int v[n], wt[n];
for (int i = 0; i < n; i++) {
cin >> wt[i];
cin >> v[i];
}
vector<long long> dp(w + 1, 0);
for (int i = 0; i < n; i++) {
for (int j = w - i; j >= 0; j--) {
dp[j + wt[i]] = max(dp[j + wt[i]], v[i] + dp[j]);
}
// for (int i = 0; i < w + 1; i++)
// {
// cout << dp[i] << " ";
// }
// cout << endl;
}
long long ans = 0;
for (int i = 0; i < w + 1; i++) {
ans = max(ans, dp[i]);
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, w;
cin >> n >> w;
int v[n], wt[n];
for (int i = 0; i < n; i++) {
cin >> wt[i];
cin >> v[i];
}
vector<long long> dp(w + 1, 0);
for (int i = 0; i < n; i++) {
for (long long j = w - wt[i]; j >= 0; j--) {
dp[j + wt[i]] = max(dp[j + wt[i]], v[i] + dp[j]);
}
// for (int i = 0; i < w + 1; i++)
// {
// cout << dp[i] << " ";
// }
// cout << endl;
}
long long ans = 0;
for (int i = 0; i < w + 1; i++) {
ans = max(ans, dp[i]);
}
cout << ans << endl;
}
|
replace
| 13 | 14 | 13 | 14 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define forr(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define ford(i, n) for (int i = n; i > 0; i--)
#define forin(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define debug(x) cerr << #x << " " << x << "\n";
#define yn \
{ puts("YES"); } \
else { \
puts("NO"); \
}
#define maxs(x, y) (x = max(x, y))
#define sz(x) (int)(x).size()
#define mins(x, y) (x = min(x, y))
#define newline cout << "\n"
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
ll dp[20005] = {};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n, W, w, v;
cin >> n >> W;
forn(i, n) {
cin >> w >> v;
forr(j, W - w + 1) { maxs(dp[j + w], dp[j] + v); }
}
cout << dp[W];
return 0;
}
|
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define forr(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define ford(i, n) for (int i = n; i > 0; i--)
#define forin(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define debug(x) cerr << #x << " " << x << "\n";
#define yn \
{ puts("YES"); } \
else { \
puts("NO"); \
}
#define maxs(x, y) (x = max(x, y))
#define sz(x) (int)(x).size()
#define mins(x, y) (x = min(x, y))
#define newline cout << "\n"
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
ll dp[200005] = {};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n, W, w, v;
cin >> n >> W;
forn(i, n) {
cin >> w >> v;
forr(j, W - w + 1) { maxs(dp[j + w], dp[j] + v); }
}
cout << dp[W];
return 0;
}
|
replace
| 30 | 31 | 30 | 31 |
0
| |
p03163
|
C++
|
Runtime Error
|
/*
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
| |
| author : thanos1399 Language : C ++ |
| college : MSIT |
| _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |
*/
#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define TEST \
int t; \
cin >> t; \
while (t--)
#define line cout << endl;
#define loop(i, a, b) for (long long int i = a; i < b; i++)
#define loopIn(i, a, b) for (long long int i = a; i <= b; i++)
#define MAX_SIZE 1000000
#define ff first
#define ss second
#define pb push_back
#define ll long long int
#define mp make_pair
#define vt vector<tup>
#define vtt vector<vt>
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
void Sieve(int *prime, int n) {
for (int i = 0; i < n; i++) {
prime[i] = 0;
}
for (int i = 2; i < n; i++) {
prime[i] = 1;
}
for (int i = 4; i < n; i += 2) {
prime[i] = 0;
}
for (int i = 3; i < n; i++) {
if (prime[i] == 1) {
for (int j = i * i; j < n; j += i) {
prime[j] = 0;
}
}
}
return;
}
void input() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("/home/yash/Desktop/CPP/input.txt", "r", stdin);
freopen("/home/yash/Desktop/CPP/output.txt", "w", stdout);
#endif
}
ll inp;
int parent[101];
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
return (a.second < b.second);
}
/*int find(int a)
{
while(parent[a] > 0)
a = parent[a];
return a;
} */
class item {
public:
ll val, wt;
};
void Union(int a, int b) {
parent[a] += parent[b]; // adding size of set b to set a
parent[b] = a; // making a , parent of new set
}
int find(int node) {
if (parent[node] < 0)
return node;
parent[node] = find(parent[node]);
return parent[node];
}
// Solution 1
ll solve(vector<item> arr, ll n, ll W) {
ll dp[n + 1][W + 1];
for (ll w = 0; w <= W; w++) {
dp[1][w] = 0;
}
dp[1][arr[1].wt] = arr[1].val;
for (ll i = 2; i <= n; i++) {
for (ll w = 0; w <= W; w++) {
dp[i][w] = dp[i - 1][w];
if (arr[i].wt > w)
continue;
dp[i][w] = max(dp[i][w], arr[i].val + dp[i - 1][w - arr[i].wt]);
}
}
// return dp[n][W];
return *max_element(dp[n], dp[n] + W + 1);
}
int solve1(vector<item> arr, ll n, ll W) {
ll i, w;
ll K[n + 1][W + 1];
// Build table K[][] in bottom up manner
for (i = 0; i <= n; i++) {
for (w = 0; w <= W; w++) {
if (i == 0 || w == 0)
K[i][w] = 0;
else if (arr[i - 1].wt <= w)
K[i][w] =
max(arr[i - 1].val + K[i - 1][w - arr[i - 1].wt], K[i - 1][w]);
else
K[i][w] = K[i - 1][w];
}
}
return K[n][W];
}
int main() {
input();
ll n, W;
cin >> n >> W;
vector<item> arr(n + 1);
for (ll i = 1; i <= n; i++) {
cin >> arr[i].wt >> arr[i].val;
}
ll ans = solve(arr, n, W);
// ll ans2 = solve1( arr , n , W) ;
cout << ans << endl;
return 0;
}
|
/*
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
| |
| author : thanos1399 Language : C ++ |
| college : MSIT |
| _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |
*/
#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define TEST \
int t; \
cin >> t; \
while (t--)
#define line cout << endl;
#define loop(i, a, b) for (long long int i = a; i < b; i++)
#define loopIn(i, a, b) for (long long int i = a; i <= b; i++)
#define MAX_SIZE 1000000
#define ff first
#define ss second
#define pb push_back
#define ll long long int
#define mp make_pair
#define vt vector<tup>
#define vtt vector<vt>
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
void Sieve(int *prime, int n) {
for (int i = 0; i < n; i++) {
prime[i] = 0;
}
for (int i = 2; i < n; i++) {
prime[i] = 1;
}
for (int i = 4; i < n; i += 2) {
prime[i] = 0;
}
for (int i = 3; i < n; i++) {
if (prime[i] == 1) {
for (int j = i * i; j < n; j += i) {
prime[j] = 0;
}
}
}
return;
}
void input() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("/home/yash/Desktop/CPP/input.txt", "r", stdin);
freopen("/home/yash/Desktop/CPP/output.txt", "w", stdout);
#endif
}
ll inp;
int parent[101];
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
return (a.second < b.second);
}
/*int find(int a)
{
while(parent[a] > 0)
a = parent[a];
return a;
} */
class item {
public:
ll val, wt;
};
void Union(int a, int b) {
parent[a] += parent[b]; // adding size of set b to set a
parent[b] = a; // making a , parent of new set
}
int find(int node) {
if (parent[node] < 0)
return node;
parent[node] = find(parent[node]);
return parent[node];
}
// Solution 1
ll solve(vector<item> arr, ll n, ll W) {
ll dp[n + 1][W + 1];
for (ll w = 0; w <= W; w++) {
dp[1][w] = 0;
}
dp[1][arr[1].wt] = arr[1].val;
for (ll i = 2; i <= n; i++) {
for (ll w = 0; w <= W; w++) {
dp[i][w] = dp[i - 1][w];
if (arr[i].wt > w)
continue;
dp[i][w] = max(dp[i][w], arr[i].val + dp[i - 1][w - arr[i].wt]);
}
}
// return dp[n][W];
return *max_element(dp[n], dp[n] + W + 1);
}
int solve1(vector<item> arr, ll n, ll W) {
ll i, w;
ll K[n + 1][W + 1];
// Build table K[][] in bottom up manner
for (i = 0; i <= n; i++) {
for (w = 0; w <= W; w++) {
if (i == 0 || w == 0)
K[i][w] = 0;
else if (arr[i - 1].wt <= w)
K[i][w] =
max(arr[i - 1].val + K[i - 1][w - arr[i - 1].wt], K[i - 1][w]);
else
K[i][w] = K[i - 1][w];
}
}
return K[n][W];
}
int main() {
// input();
ll n, W;
cin >> n >> W;
vector<item> arr(n + 1);
for (ll i = 1; i <= n; i++) {
cin >> arr[i].wt >> arr[i].val;
}
ll ans = solve(arr, n, W);
// ll ans2 = solve1( arr , n , W) ;
cout << ans << endl;
return 0;
}
|
replace
| 146 | 147 | 146 | 147 |
-6
|
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define fastIO \
ios::sync_with_stdio(NULL); \
cin.tie(NULL);
using namespace std;
const int N = 1E5 + 7;
long long f[100][N];
int n, W;
long long w[N], c[N];
int main() {
fastIO cin >> n >> W;
for (int i = 1; i <= n; ++i)
cin >> w[i] >> c[i];
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= W; ++j)
if (j - w[i] >= 0)
f[i][j] = max(f[i - 1][j], f[i - 1][j - w[i]] + c[i]);
else
f[i][j] = f[i - 1][j];
cout << f[n][W];
}
|
#include <bits/stdc++.h>
#define fastIO \
ios::sync_with_stdio(NULL); \
cin.tie(NULL);
using namespace std;
const int N = 1E5 + 7;
long long f[107][N];
int n, W;
long long w[N], c[N];
int main() {
fastIO cin >> n >> W;
for (int i = 1; i <= n; ++i)
cin >> w[i] >> c[i];
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= W; ++j)
if (j - w[i] >= 0)
f[i][j] = max(f[i - 1][j], f[i - 1][j - w[i]] + c[i]);
else
f[i][j] = f[i - 1][j];
cout << f[n][W];
}
|
replace
| 8 | 9 | 8 | 9 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")
/*
||||||||||||||||||||||||||||||||||||||||||||||||
| ||| | |||| |||| | |
| | | | | | | | | | |
| | | | | | | | | | |
||||||| | | ||||||| ||| ||| |||||
| | | | | | | | | | |
| | ||| | | | || |||| | |
||||||||||||||||||||||||||||||||||||||||||||||||
*/
using namespace std;
// #include "testlib.h"
#define ff first
#define ss second
#define mp make_pair
#define all(v) v.begin(), v.end()
#define int long long
#define ll long long
#define M 1000000007
#define inputarr(a, n) \
for (int i = 0; i < n; ++i) \
cin >> a[i]
#define GCD(m, n) __gcd(m, n)
#define LCM(m, n) m *(n / GCD(m, n))
#define mii map<ll, ll>
#define msi map<string, ll>
#define mis map<ll int, string>
#define rep(a, b) for (ll i = a; i < b; i++)
#define rep0(n) for (ll i = 0; i < n; i++)
#define repi(i, a, b) for (ll i = a; i < b; i++)
#define pb push_back
#define vi vector<ll>
#define mp make_pair
#define vs vector<string>
#define ppb pop_back
#define endl '\n'
#define asdf \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define r0 return 0;
#define FORD(i, a, b) for (int i = (int)(a); i >= (int)(b); --i)
#define FORE(it, c) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define inputoutput \
freopen("input.txt", "r", stdin); \
freopen("output.txt", "w", stdout);
#define input freopen("input.txt", "r", stdin);
#define Set(a, s) 4(a, s, sizeof(a))
#define FOR repi
#define pii pair<int, int>
#define REVERSE(v) reverse(ALL(v))
#define display(x) \
trav(a, x) cout << a << " "; \
cout << endl
#define debug cerr << "bhau" << endl
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
std::cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
// #define float long double
ll max(ll a, ll b) { return (a > b) ? a : b; }
int min(int a, int b) { return (a < b) ? a : b; }
int knapSack(int W, vi &wt, vi &val, int n) {
int i, w;
int K[n + 1][W + 1];
// Build table K[][] in bottom up manner
for (i = 0; i <= n; i++) {
for (w = 0; w <= W; w++) {
if (i == 0 || w == 0)
K[i][w] = 0;
else if (wt[i - 1] <= w)
K[i][w] = max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);
else
K[i][w] = K[i - 1][w];
}
}
return K[n][W];
}
int solve() {
int n, W;
cin >> n >> W;
vi w(n), v(n);
rep0(n) cin >> w[i] >> v[i];
cout << knapSack(W, w, v, n);
r0
}
signed main() {
asdf
#ifndef ONLINE_JUDGE
inputoutput
#endif
int t = 1;
// cin>>t;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")
/*
||||||||||||||||||||||||||||||||||||||||||||||||
| ||| | |||| |||| | |
| | | | | | | | | | |
| | | | | | | | | | |
||||||| | | ||||||| ||| ||| |||||
| | | | | | | | | | |
| | ||| | | | || |||| | |
||||||||||||||||||||||||||||||||||||||||||||||||
*/
using namespace std;
// #include "testlib.h"
#define ff first
#define ss second
#define mp make_pair
#define all(v) v.begin(), v.end()
#define int long long
#define ll long long
#define M 1000000007
#define inputarr(a, n) \
for (int i = 0; i < n; ++i) \
cin >> a[i]
#define GCD(m, n) __gcd(m, n)
#define LCM(m, n) m *(n / GCD(m, n))
#define mii map<ll, ll>
#define msi map<string, ll>
#define mis map<ll int, string>
#define rep(a, b) for (ll i = a; i < b; i++)
#define rep0(n) for (ll i = 0; i < n; i++)
#define repi(i, a, b) for (ll i = a; i < b; i++)
#define pb push_back
#define vi vector<ll>
#define mp make_pair
#define vs vector<string>
#define ppb pop_back
#define endl '\n'
#define asdf \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define r0 return 0;
#define FORD(i, a, b) for (int i = (int)(a); i >= (int)(b); --i)
#define FORE(it, c) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define inputoutput \
freopen("input.txt", "r", stdin); \
freopen("output.txt", "w", stdout);
#define input freopen("input.txt", "r", stdin);
#define Set(a, s) 4(a, s, sizeof(a))
#define FOR repi
#define pii pair<int, int>
#define REVERSE(v) reverse(ALL(v))
#define display(x) \
trav(a, x) cout << a << " "; \
cout << endl
#define debug cerr << "bhau" << endl
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
std::cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
// #define float long double
ll max(ll a, ll b) { return (a > b) ? a : b; }
int min(int a, int b) { return (a < b) ? a : b; }
int knapSack(int W, vi &wt, vi &val, int n) {
int i, w;
int K[n + 1][W + 1];
// Build table K[][] in bottom up manner
for (i = 0; i <= n; i++) {
for (w = 0; w <= W; w++) {
if (i == 0 || w == 0)
K[i][w] = 0;
else if (wt[i - 1] <= w)
K[i][w] = max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);
else
K[i][w] = K[i - 1][w];
}
}
return K[n][W];
}
int solve() {
int n, W;
cin >> n >> W;
vi w(n), v(n);
rep0(n) cin >> w[i] >> v[i];
cout << knapSack(W, w, v, n);
r0
}
signed main() {
asdf
int t = 1;
// cin>>t;
while (t--) {
solve();
}
}
|
replace
| 108 | 111 | 108 | 109 |
-11
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define vi vector<float>
#define vl vector<long long>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define loop(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define mp make_pair
#define ll long long
#define umap unordered_map
const int mod = 1e9 + 7;
using namespace std;
#define xx first
#define yy second
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("nnout.txt", "w", stdout);
#endif
int n, weight;
cin >> n >> weight;
int w[n], v[n];
for (int i = 0; i < n; i++) {
cin >> w[i] >> v[i];
}
ll dp[n + 1][weight + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= weight; j++) {
if (i == 0 || j == 0) {
dp[i][j] = 0;
} else {
if (w[i - 1] <= j)
dp[i][j] = max(v[i - 1] + dp[i - 1][j - w[i - 1]], dp[i - 1][j]);
else
dp[i][j] = dp[i - 1][j];
}
}
}
cout << dp[n][weight] << endl;
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define vi vector<float>
#define vl vector<long long>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define loop(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define mp make_pair
#define ll long long
#define umap unordered_map
const int mod = 1e9 + 7;
using namespace std;
#define xx first
#define yy second
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("nnout.txt", "w", stdout);
// #endif
int n, weight;
cin >> n >> weight;
int w[n], v[n];
for (int i = 0; i < n; i++) {
cin >> w[i] >> v[i];
}
ll dp[n + 1][weight + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= weight; j++) {
if (i == 0 || j == 0) {
dp[i][j] = 0;
} else {
if (w[i - 1] <= j)
dp[i][j] = max(v[i - 1] + dp[i - 1][j - w[i - 1]], dp[i - 1][j]);
else
dp[i][j] = dp[i - 1][j];
}
}
}
cout << dp[n][weight] << endl;
return 0;
}
|
replace
| 34 | 38 | 34 | 38 |
-11
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(i, n) repi(i, 0, n)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
using ll = long long;
const int MAX_N = 100;
const int MAX_M = 1e5;
ll N, W;
ll WV[MAX_N][2] = {{0}};
ll DP[MAX_N][MAX_M] = {{0}};
inline void input() {
cin >> N >> W;
rep(i, N) cin >> WV[i][0] >> WV[i][1];
}
inline void solve() {
rep(i, N) {
rep(j, W + 1) {
if (j - WV[i][0] >= 0)
chmax(DP[i + 1][j], DP[i][j - WV[i][0]] + WV[i][1]);
chmax(DP[i + 1][j], DP[i][j]);
}
}
}
inline void Main() {
// code
input();
solve();
cout << DP[N][W] << endl;
}
int main() {
std::cin.tie(0);
std::ios::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(20);
Main();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(i, n) repi(i, 0, n)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
using ll = long long;
const int MAX_N = 100 + 10;
const int MAX_M = 1e5 + 10;
ll N, W;
ll WV[MAX_N][2] = {{0}};
ll DP[MAX_N][MAX_M] = {{0}};
inline void input() {
cin >> N >> W;
rep(i, N) cin >> WV[i][0] >> WV[i][1];
}
inline void solve() {
rep(i, N) {
rep(j, W + 1) {
if (j - WV[i][0] >= 0)
chmax(DP[i + 1][j], DP[i][j - WV[i][0]] + WV[i][1]);
chmax(DP[i + 1][j], DP[i][j]);
}
}
}
inline void Main() {
// code
input();
solve();
cout << DP[N][W] << endl;
}
int main() {
std::cin.tie(0);
std::ios::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(20);
Main();
return 0;
}
|
replace
| 24 | 26 | 24 | 26 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <iostream>
using namespace std;
const int NMAX = 100;
const int WMAX = 100001;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int N, W;
int w[NMAX], v[NMAX];
long long DP[NMAX][WMAX] = {};
int main() {
cin >> N >> W;
for (int i = 0; i < N; i++)
cin >> w[i] >> v[i];
for (int i = 0; i < N; i++) {
for (int j = 0; j < W + 1; j++) {
chmax(DP[i + 1][j], DP[i][j]);
if (j + w[i] > W)
continue;
chmax(DP[i + 1][j + w[i]], DP[i][j] + v[i]);
}
}
/*
for(int i=0; i<N+1; i++){
for(int j=0; j<W+1; j++) cout << DP[i][j] << " ";
cout << endl;
}*/
cout << DP[N][W] << endl;
}
|
#include <iostream>
using namespace std;
const int NMAX = 101;
const int WMAX = 100001;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int N, W;
int w[NMAX], v[NMAX];
long long DP[NMAX][WMAX] = {};
int main() {
cin >> N >> W;
for (int i = 0; i < N; i++)
cin >> w[i] >> v[i];
for (int i = 0; i < N; i++) {
for (int j = 0; j < W + 1; j++) {
chmax(DP[i + 1][j], DP[i][j]);
if (j + w[i] > W)
continue;
chmax(DP[i + 1][j + w[i]], DP[i][j] + v[i]);
}
}
/*
for(int i=0; i<N+1; i++){
for(int j=0; j<W+1; j++) cout << DP[i][j] << " ";
cout << endl;
}*/
cout << DP[N][W] << endl;
}
|
replace
| 2 | 3 | 2 | 3 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
long long int N, v[50], w[50], W;
long long m[100002][102];
long long int resi(long long int W, long long int N) {
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= W; j++) {
if (i == 0 || j == 0)
m[j][i] = 0;
else {
if (j < w[i - 1]) {
m[j][i] = m[j][i - 1];
// cout << j << " " << i << endl;
} else {
m[j][i] = max(m[j - w[i - 1]][i - 1] + v[i - 1], m[j][i - 1]);
}
}
}
}
// for(int i = 0; i <= N; i++)
// {
// for(int j = 0; j <= W; j++)
// {
// cout << m[j][i] << "(" << j << ") " ;
// }
// cout << endl;
// }
// cout << "REZ: "<< m[][];
return m[W][N];
}
int main() {
cin >> N >> W;
for (int i = 0; i < N; i++) {
cin >> w[i] >> v[i];
}
long long int x = resi(W, N);
cout << x;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int N, v[102], w[102], W;
long long m[100002][102];
long long int resi(long long int W, long long int N) {
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= W; j++) {
if (i == 0 || j == 0)
m[j][i] = 0;
else {
if (j < w[i - 1]) {
m[j][i] = m[j][i - 1];
// cout << j << " " << i << endl;
} else {
m[j][i] = max(m[j - w[i - 1]][i - 1] + v[i - 1], m[j][i - 1]);
}
}
}
}
// for(int i = 0; i <= N; i++)
// {
// for(int j = 0; j <= W; j++)
// {
// cout << m[j][i] << "(" << j << ") " ;
// }
// cout << endl;
// }
// cout << "REZ: "<< m[][];
return m[W][N];
}
int main() {
cin >> N >> W;
for (int i = 0; i < N; i++) {
cin >> w[i] >> v[i];
}
long long int x = resi(W, N);
cout << x;
return 0;
}
|
replace
| 4 | 5 | 4 | 5 |
0
| |
p03163
|
C++
|
Runtime Error
|
// It doesn't matter how slow you go, Unless you don't stop.
#include <algorithm>
#include <bitset>
#include <iostream>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <chrono>
#include <fstream>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> p64;
typedef vector<LL> v64;
typedef map<LL, LL> mp64;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define rep(i, s, e) for (long long i = s; i <= e; i++)
#define brep(i, s, e) for (long long i = s; i >= e; i--)
#define all(x) x.begin(), x.end()
#define mem(x, y) memset(x, y, sizeof(x))
#define DANGER \
std::ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
LL knapsack(LL W[], LL v[], int wght, int n) {
LL **dp = new LL *[n];
for (int i = 0; i <= n; i++) {
dp[i] = new LL[wght + 1];
for (int j = 0; j <= wght; j++) {
dp[i][j] = 0;
}
}
for (int i = 0; i <= wght; i++) {
dp[0][i] = 0;
}
for (int i = 0; i < n; i++) {
dp[i][0] = 0;
}
// solve
for (int i = 1; i <= n; i++) {
for (int w = 0; w <= wght; w++) {
dp[i][w] = dp[i - 1][w];
if (W[i - 1] <= w) {
// cout<<W[i-1]<<" "<<w<<endl;
dp[i][w] = max(dp[i][w], dp[i - 1][w - W[i - 1]] + v[i - 1]);
}
}
}
// for(int i=0;i<=n;i++)
// {
// for(int j=0;j<=wght;j++)
// {
// cout<<dp[i][j]<<" ";
// }
// cout<<endl;
// }
// cout<<endl;
return dp[n][wght];
}
void solve() {
int n, wght;
cin >> n >> wght;
LL w[n], v[n];
for (int i = 0; i < n; i++) {
LL x, y;
cin >> x >> y;
w[i] = x;
v[i] = y;
}
LL ans = knapsack(w, v, wght, n);
cout << ans << endl;
}
int main() {
DANGER;
ofstream out("output.txt");
auto start = chrono::high_resolution_clock::now();
LL t = 1;
// cin>>t;
rep(test, 1, t) { solve(); }
auto stop = chrono::high_resolution_clock::now();
// auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start);
// cout << "Time taken by function: " << duration.count() << " milliseconds"
// << endl;
}
|
// It doesn't matter how slow you go, Unless you don't stop.
#include <algorithm>
#include <bitset>
#include <iostream>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <chrono>
#include <fstream>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> p64;
typedef vector<LL> v64;
typedef map<LL, LL> mp64;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define rep(i, s, e) for (long long i = s; i <= e; i++)
#define brep(i, s, e) for (long long i = s; i >= e; i--)
#define all(x) x.begin(), x.end()
#define mem(x, y) memset(x, y, sizeof(x))
#define DANGER \
std::ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
LL knapsack(LL W[], LL v[], int wght, int n) {
LL **dp = new LL *[n + 1];
for (int i = 0; i <= n; i++) {
dp[i] = new LL[wght + 1];
for (int j = 0; j <= wght; j++) {
dp[i][j] = 0;
}
}
for (int i = 0; i <= wght; i++) {
dp[0][i] = 0;
}
for (int i = 0; i < n; i++) {
dp[i][0] = 0;
}
// solve
for (int i = 1; i <= n; i++) {
for (int w = 0; w <= wght; w++) {
dp[i][w] = dp[i - 1][w];
if (W[i - 1] <= w) {
// cout<<W[i-1]<<" "<<w<<endl;
dp[i][w] = max(dp[i][w], dp[i - 1][w - W[i - 1]] + v[i - 1]);
}
}
}
// for(int i=0;i<=n;i++)
// {
// for(int j=0;j<=wght;j++)
// {
// cout<<dp[i][j]<<" ";
// }
// cout<<endl;
// }
// cout<<endl;
return dp[n][wght];
}
void solve() {
int n, wght;
cin >> n >> wght;
LL w[n], v[n];
for (int i = 0; i < n; i++) {
LL x, y;
cin >> x >> y;
w[i] = x;
v[i] = y;
}
LL ans = knapsack(w, v, wght, n);
cout << ans << endl;
}
int main() {
DANGER;
ofstream out("output.txt");
auto start = chrono::high_resolution_clock::now();
LL t = 1;
// cin>>t;
rep(test, 1, t) { solve(); }
auto stop = chrono::high_resolution_clock::now();
// auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start);
// cout << "Time taken by function: " << duration.count() << " milliseconds"
// << endl;
}
|
replace
| 39 | 40 | 39 | 40 |
-6
|
corrupted size vs. prev_size
|
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
// #define mp make_pair
#define eb emplace_back
#define pb push_back
#define ss second
#define ff first
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define mod 998244353
#define MOD (1000 * 1000 * 1000 + 7)
#define MN LLONG_MIN
#define MX LLONG_MAX
#define v1d vector<int>
#define v2d vector<vector<int>>
#define vip vector<pair<int, int>>
#define v1s vector<string>
#define pa pair<int, int>
#define mxpq(T) priority_queue<T>
#define mnpq(T) priority_queue<T, vector<T>, greater<T>>
#define print(v) \
for (auto i : v) \
cout << i << " "; \
cout << "\n";
#define p2d(v) \
for (auto a : v) { \
for (auto b : a) \
cout << b << " "; \
cout << endl; \
}
#define p1d(v) \
for (auto a : v) \
cout << a << " "; \
cout << endl;
#define ppd(v) \
for (auto a : v) \
cout << a.ff << " " << a.ss << endl;
#define endl "\n"
#define input(b, n) \
for (int i = 0; i < n; i++) \
cin >> b[i];
#define Sort(v) sort(v.begin(), v.end())
#define RSort(v) sort(v.rbegin(), v.rend())
#define all(v) v.begin(), v.end()
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
// string s = bitset<64>(a).to_string();
void azmuth(int n = 12) {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << setprecision(n) << fixed;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int lb = 1000000000;
//____________________________________________________________________________________
void solve() {
int n, W;
cin >> n >> W;
v1d dp(W + 5, 0);
for (int i = 0; i < n; i++) {
int w, v;
cin >> w >> v;
for (int j = W; j >= w; j--) {
dp[j] = max(dp[j], dp[j - w] + v);
}
// print(dp);
}
int ans = 0;
for (auto i : dp)
ans = max(ans, i);
cout << ans << "\n";
}
int32_t main() {
azmuth();
int t = 1;
// cin>>t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
// #define mp make_pair
#define eb emplace_back
#define pb push_back
#define ss second
#define ff first
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define mod 998244353
#define MOD (1000 * 1000 * 1000 + 7)
#define MN LLONG_MIN
#define MX LLONG_MAX
#define v1d vector<int>
#define v2d vector<vector<int>>
#define vip vector<pair<int, int>>
#define v1s vector<string>
#define pa pair<int, int>
#define mxpq(T) priority_queue<T>
#define mnpq(T) priority_queue<T, vector<T>, greater<T>>
#define print(v) \
for (auto i : v) \
cout << i << " "; \
cout << "\n";
#define p2d(v) \
for (auto a : v) { \
for (auto b : a) \
cout << b << " "; \
cout << endl; \
}
#define p1d(v) \
for (auto a : v) \
cout << a << " "; \
cout << endl;
#define ppd(v) \
for (auto a : v) \
cout << a.ff << " " << a.ss << endl;
#define endl "\n"
#define input(b, n) \
for (int i = 0; i < n; i++) \
cin >> b[i];
#define Sort(v) sort(v.begin(), v.end())
#define RSort(v) sort(v.rbegin(), v.rend())
#define all(v) v.begin(), v.end()
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
// string s = bitset<64>(a).to_string();
void azmuth(int n = 12) {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << setprecision(n) << fixed;
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
}
int lb = 1000000000;
//____________________________________________________________________________________
void solve() {
int n, W;
cin >> n >> W;
v1d dp(W + 5, 0);
for (int i = 0; i < n; i++) {
int w, v;
cin >> w >> v;
for (int j = W; j >= w; j--) {
dp[j] = max(dp[j], dp[j - w] + v);
}
// print(dp);
}
int ans = 0;
for (auto i : dp)
ans = max(ans, i);
cout << ans << "\n";
}
int32_t main() {
azmuth();
int t = 1;
// cin>>t;
while (t--) {
solve();
}
return 0;
}
|
replace
| 56 | 60 | 56 | 60 |
-6
|
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll aw[105], av[105], dp[105][105] = {0}, n;
ll fun(ll i, ll w) {
if (dp[i][w] != -1)
return dp[i][w];
if (i == n + 1)
return 0;
ll ans1, ans2;
ans1 = fun(i + 1, w);
if (w - aw[i] >= 0)
ans2 = av[i] + fun(i + 1, w - aw[i]);
else
ans2 = 0;
dp[i][w] = max(ans1, ans2);
return dp[i][w];
}
int main() {
ll w, i;
cin >> n >> w;
for (i = 0; i < n; i++)
cin >> aw[i] >> av[i];
for (i = 0; i <= n; i++)
for (int j = 0; j <= w; j++)
dp[i][j] = -1;
cout << fun(0, w);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll aw[105], av[105], dp[105][100007] = {0}, n;
ll fun(ll i, ll w) {
if (dp[i][w] != -1)
return dp[i][w];
if (i == n + 1)
return 0;
ll ans1, ans2;
ans1 = fun(i + 1, w);
if (w - aw[i] >= 0)
ans2 = av[i] + fun(i + 1, w - aw[i]);
else
ans2 = 0;
dp[i][w] = max(ans1, ans2);
return dp[i][w];
}
int main() {
ll w, i;
cin >> n >> w;
for (i = 0; i < n; i++)
cin >> aw[i] >> av[i];
for (i = 0; i <= n; i++)
for (int j = 0; j <= w; j++)
dp[i][j] = -1;
cout << fun(0, w);
return 0;
}
|
replace
| 3 | 4 | 3 | 4 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define int long long
#define All(v) (v).begin(), (v).end()
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int mod = 1000000007;
const int inf = mod * mod;
const int d5 = 100100;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, W;
cin >> N >> W;
// dp[i]:重さiで達成できる最大の価値
int dp[d5];
for (int i = 0; i < d5; i++)
dp[i] = -1;
dp[0] = 0;
for (int i = 0; i < N; i++) {
int w, v;
cin >> w >> v;
for (int i = W; i >= 0; i--) {
if (dp[i] == -1)
continue;
else {
dp[i + w] = max(dp[i + w], dp[i] + v);
}
}
}
int res = 0;
for (int i = 0; i <= W; i++)
res = max(res, dp[i]);
cout << res << endl;
}
|
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define int long long
#define All(v) (v).begin(), (v).end()
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int mod = 1000000007;
const int inf = mod * mod;
const int d5 = 200100;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, W;
cin >> N >> W;
// dp[i]:重さiで達成できる最大の価値
int dp[d5];
for (int i = 0; i < d5; i++)
dp[i] = -1;
dp[0] = 0;
for (int i = 0; i < N; i++) {
int w, v;
cin >> w >> v;
for (int i = W; i >= 0; i--) {
if (dp[i] == -1)
continue;
else {
dp[i + w] = max(dp[i + w], dp[i] + v);
}
}
}
int res = 0;
for (int i = 0; i <= W; i++)
res = max(res, dp[i]);
cout << res << endl;
}
|
replace
| 24 | 25 | 24 | 25 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int N, W;
vector<int> v, w;
ll dp[100][100100];
void input() {
cin >> N >> W;
v.resize(N);
w.resize(N);
rep(i, N) cin >> w[i] >> v[i];
}
ll solve() {
memset(dp, 0, sizeof(dp));
for (int i = 0; i < N; i++) {
for (int j = 1; j <= W; j++) {
if (j - w[i] >= 0) {
dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
} else {
dp[i + 1][j] = dp[i][j];
}
}
}
return dp[N][W];
}
int main() {
input();
cout << solve() << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int N, W;
vector<int> v, w;
ll dp[110][100100];
void input() {
cin >> N >> W;
v.resize(N);
w.resize(N);
rep(i, N) cin >> w[i] >> v[i];
}
ll solve() {
memset(dp, 0, sizeof(dp));
for (int i = 0; i < N; i++) {
for (int j = 1; j <= W; j++) {
if (j - w[i] >= 0) {
dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
} else {
dp[i + 1][j] = dp[i][j];
}
}
}
return dp[N][W];
}
int main() {
input();
cout << solve() << endl;
}
|
replace
| 15 | 16 | 15 | 16 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define r1 101
#define c1 10001
ll int knapsack(ll int *w, ll int *val, ll int sum, ll int n, ll int dp[][c1]) {
if (sum <= 0 || n <= 0)
return 0;
if (dp[n][sum] != -1)
return dp[n][sum];
if (w[n - 1] <= sum)
return dp[n][sum] =
max(val[n - 1] + knapsack(w, val, sum - w[n - 1], n - 1, dp),
knapsack(w, val, sum, n - 1, dp));
else
return dp[n][sum] = knapsack(w, val, sum, n - 1, dp);
}
int main() {
ll int n, W;
cin >> n >> W;
ll int w[n], val[n];
for (int i = 0; i < n; i++)
cin >> w[i] >> val[i];
ll int dp[r1][c1];
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++)
dp[i][j] = -1;
}
cout << knapsack(w, val, W, n, dp);
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define r1 101
#define c1 100001
ll int knapsack(ll int *w, ll int *val, ll int sum, ll int n, ll int dp[][c1]) {
if (sum <= 0 || n <= 0)
return 0;
if (dp[n][sum] != -1)
return dp[n][sum];
if (w[n - 1] <= sum)
return dp[n][sum] =
max(val[n - 1] + knapsack(w, val, sum - w[n - 1], n - 1, dp),
knapsack(w, val, sum, n - 1, dp));
else
return dp[n][sum] = knapsack(w, val, sum, n - 1, dp);
}
int main() {
ll int n, W;
cin >> n >> W;
ll int w[n], val[n];
for (int i = 0; i < n; i++)
cin >> w[i] >> val[i];
ll int dp[r1][c1];
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++)
dp[i][j] = -1;
}
cout << knapsack(w, val, W, n, dp);
}
|
replace
| 4 | 5 | 4 | 5 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 100;
const int MAXW = 100000;
unsigned long long dp[MAXN + 1][MAXW + 1];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, W;
cin >> n >> W;
int v[n], w[n];
for (int i = 0; i < n; i++) {
cin >> w[i] >> v[i];
}
for (int i = 0; i <= n; i++) {
dp[i][0] = 0;
}
for (int i = 1; i <= W; i++) {
dp[0][i] = 0;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= W; j++) {
if (w[i] <= j) {
dp[i][j] = max(dp[i - 1][j], v[i] + dp[i - 1][j - w[i]]);
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
cout << dp[n][W];
return 0;
}
|
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 100;
const int MAXW = 100000;
unsigned long long dp[MAXN + 1][MAXW + 1];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, W;
cin >> n >> W;
int v[n + 1], w[n + 1];
for (int i = 1; i <= n; i++) {
cin >> w[i] >> v[i];
}
for (int i = 0; i <= n; i++) {
dp[i][0] = 0;
}
for (int i = 1; i <= W; i++) {
dp[0][i] = 0;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= W; j++) {
if (w[i] <= j) {
dp[i][j] = max(dp[i - 1][j], v[i] + dp[i - 1][j - w[i]]);
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
cout << dp[n][W];
return 0;
}
|
replace
| 15 | 17 | 15 | 17 |
0
| |
p03163
|
C++
|
Runtime Error
|
#define _CRT_SECURE_NO_WARNINGS
#define ll long long
#define vi vector<int>
#define LLMAX 4000000000000000000LL
#include <bits/stdc++.h>
#include <unordered_set>
using namespace std;
struct edge {
int t, f, w;
bool operator<(const edge &e) const { return w > e.w; }
edge(int from, int to, int weight) {
t = to;
f = from;
w = weight;
}
};
#define ii pair<int, int>
#define MOD 1000000007
const double EPS = (1e-5);
long long binpow(long long a, long long b) {
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
int findXOR(int n) {
switch (n % 4) {
case 0:
return n;
case 1:
return 1;
case 2:
return n + 1;
case 3:
return 0;
}
}
int rangeXOR(int l, int r) { return (findXOR(l - 1) ^ findXOR(r)); }
int getbit(int mask, int bit) { return (mask & (1 << bit)); }
void setbit(int &mask, int bit, int val) {
if (val)
mask |= (1 << bit);
else
mask &= ~(1 << bit);
}
// ll gcd(ll a, ll b){return b == 0 ? a : gcd(b, a % b);}
// ll lcm(ll a, ll b){return a * (b / gcd(a, b));}
const int N = 1e5 + 10;
const int M = 101;
const ll INF = 1e9;
int w[M], v[M];
ll dp[2][M];
void solve() {
int n, W;
cin >> n >> W;
for (int i = 0; i < n; ++i)
cin >> w[i] >> v[i];
int x = 0;
dp[x][w[0]] = v[0];
for (int i = 1; i < n; ++i) {
x ^= 1;
for (int j = 0; j <= W; ++j) {
if (j + w[i] <= W) {
dp[x][j + w[i]] = max(dp[x][j + w[i]], v[i] + dp[x ^ 1][j]);
}
dp[x][j] = max(dp[x][j], dp[x ^ 1][j]);
}
}
ll ans = 0;
for (int i = 0; i <= W; ++i)
ans = max(ans, dp[x][i]);
cout << ans << '\n';
}
int main(void) {
// freopen("commandos.in", "r", stdin);
// freopen("output.txt", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tc = 1;
// cin >> tc;
// scanf("%d", &tc);
while (tc--) {
solve();
}
}
|
#define _CRT_SECURE_NO_WARNINGS
#define ll long long
#define vi vector<int>
#define LLMAX 4000000000000000000LL
#include <bits/stdc++.h>
#include <unordered_set>
using namespace std;
struct edge {
int t, f, w;
bool operator<(const edge &e) const { return w > e.w; }
edge(int from, int to, int weight) {
t = to;
f = from;
w = weight;
}
};
#define ii pair<int, int>
#define MOD 1000000007
const double EPS = (1e-5);
long long binpow(long long a, long long b) {
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
int findXOR(int n) {
switch (n % 4) {
case 0:
return n;
case 1:
return 1;
case 2:
return n + 1;
case 3:
return 0;
}
}
int rangeXOR(int l, int r) { return (findXOR(l - 1) ^ findXOR(r)); }
int getbit(int mask, int bit) { return (mask & (1 << bit)); }
void setbit(int &mask, int bit, int val) {
if (val)
mask |= (1 << bit);
else
mask &= ~(1 << bit);
}
// ll gcd(ll a, ll b){return b == 0 ? a : gcd(b, a % b);}
// ll lcm(ll a, ll b){return a * (b / gcd(a, b));}
const int N = 1e5 + 10;
const int M = 101;
const ll INF = 1e9;
int w[M], v[M];
ll dp[2][N];
void solve() {
int n, W;
cin >> n >> W;
for (int i = 0; i < n; ++i)
cin >> w[i] >> v[i];
int x = 0;
dp[x][w[0]] = v[0];
for (int i = 1; i < n; ++i) {
x ^= 1;
for (int j = 0; j <= W; ++j) {
if (j + w[i] <= W) {
dp[x][j + w[i]] = max(dp[x][j + w[i]], v[i] + dp[x ^ 1][j]);
}
dp[x][j] = max(dp[x][j], dp[x ^ 1][j]);
}
}
ll ans = 0;
for (int i = 0; i <= W; ++i)
ans = max(ans, dp[x][i]);
cout << ans << '\n';
}
int main(void) {
// freopen("commandos.in", "r", stdin);
// freopen("output.txt", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tc = 1;
// cin >> tc;
// scanf("%d", &tc);
while (tc--) {
solve();
}
}
|
replace
| 55 | 56 | 55 | 56 |
0
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define Int int32_t
#define all(c) c.begin(), c.end()
#define FAST ios_base::sync_with_stdio(false), cin.tie(NULL);
#define pii pair<int, int>
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define bitcnt(n) __builtin_popcountll(n)
#define setpre(n) cout << fixed << setprecision(n)
#define tr(c) \
for (const auto &x : c) \
cout << x << " "; \
cout << "\n";
#define ol(c, s, e) \
for (int pos = s; pos < e; pos++) \
cout << c[pos] << " "; \
cout << "\n";
#define PI acos(-1LL)
const int M = 1000000007;
const int N = 2e5 + 5;
const long long INF = 1e9 + 12;
vector<vector<int>> v;
int n, weight;
int dp[105][N];
int fun(int i = n - 1, int w = weight) {
if (w < 0)
return -INF;
if (i < 0)
return 0;
if (dp[i][w] != -1)
return dp[i][w];
int ans = max(fun(i - 1, w - v[i][0]) + v[i][1], fun(i - 1, w));
return ans;
}
void solve() {
cin >> n >> weight;
v.resize(n, vector<int>(2));
memset(dp, -1, sizeof dp);
for (int i = 0; i < n; i++) {
cin >> v[i][0] >> v[i][1];
}
cout << fun();
}
signed main() {
FAST int tc = 1;
// TODO: check for tc
// cin >> tc;
for (int t = 1; t <= tc; t++) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define Int int32_t
#define all(c) c.begin(), c.end()
#define FAST ios_base::sync_with_stdio(false), cin.tie(NULL);
#define pii pair<int, int>
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define bitcnt(n) __builtin_popcountll(n)
#define setpre(n) cout << fixed << setprecision(n)
#define tr(c) \
for (const auto &x : c) \
cout << x << " "; \
cout << "\n";
#define ol(c, s, e) \
for (int pos = s; pos < e; pos++) \
cout << c[pos] << " "; \
cout << "\n";
#define PI acos(-1LL)
const int M = 1000000007;
const int N = 2e5 + 5;
const long long INF = 1e9 + 12;
vector<vector<int>> v;
int n, weight;
int dp[105][N];
int fun(int i = n - 1, int w = weight) {
if (w < 0)
return -INF;
if (i < 0)
return 0;
if (dp[i][w] != -1)
return dp[i][w];
int ans = max(fun(i - 1, w - v[i][0]) + v[i][1], fun(i - 1, w));
return dp[i][w] = ans;
}
void solve() {
cin >> n >> weight;
v.resize(n, vector<int>(2));
memset(dp, -1, sizeof dp);
for (int i = 0; i < n; i++) {
cin >> v[i][0] >> v[i][1];
}
cout << fun();
}
signed main() {
FAST int tc = 1;
// TODO: check for tc
// cin >> tc;
for (int t = 1; t <= tc; t++) {
solve();
}
}
|
replace
| 38 | 39 | 38 | 39 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define watch(x) cout << (#x) << " = " << x << endl;
#define mkp make_pair
#define mkt make_tuple
#define pb push_back
#define ll long long
#define ld long double
using namespace std;
int n, W;
ll dp[110][10010];
int w[110], v[110];
int main() {
scanf("%d%d", &n, &W);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &w[i], &v[i]);
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= W; j++) {
dp[i][j] = -1e12;
}
}
dp[1][W] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= W; j++) {
if (j >= w[i])
dp[i + 1][j - w[i]] = max(dp[i + 1][j - w[i]], dp[i][j] + v[i]);
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
}
}
ll ans = -1;
for (int i = 0; i <= W; i++)
ans = max(ans, dp[n + 1][i]);
printf("%lld\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
#define watch(x) cout << (#x) << " = " << x << endl;
#define mkp make_pair
#define mkt make_tuple
#define pb push_back
#define ll long long
#define ld long double
using namespace std;
int n, W;
ll dp[110][100010];
int w[110], v[110];
int main() {
scanf("%d%d", &n, &W);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &w[i], &v[i]);
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= W; j++) {
dp[i][j] = -1e12;
}
}
dp[1][W] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= W; j++) {
if (j >= w[i])
dp[i + 1][j - w[i]] = max(dp[i + 1][j - w[i]], dp[i][j] + v[i]);
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
}
}
ll ans = -1;
for (int i = 0; i <= W; i++)
ans = max(ans, dp[n + 1][i]);
printf("%lld\n", ans);
return 0;
}
|
replace
| 12 | 13 | 12 | 13 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define int long long
using namespace std;
int wt[105];
int val[105];
int ans[105][10005];
int N, w;
int ks(int n, int c) {
if (ans[n][c] != -1)
return ans[n][c];
if (n == 0 or c == 0)
return 0;
if (wt[n - 1] > c)
return ks(n - 1, c);
int a = ks(n - 1, c);
int b = val[n - 1] + ks(n - 1, c - wt[n - 1]);
ans[n][c] = max(a, b);
return ans[n][c];
}
void solve() {
cin >> N >> w;
for (int i = 0; i < N; ++i) {
cin >> wt[i] >> val[i];
}
memset(ans, -1, sizeof(ans));
cout << ks(N, w);
}
int32_t main() { solve(); }
|
#include <bits/stdc++.h>
#define int long long
using namespace std;
int wt[105];
int val[105];
int ans[105][100005];
int N, w;
int ks(int n, int c) {
if (ans[n][c] != -1)
return ans[n][c];
if (n == 0 or c == 0)
return 0;
if (wt[n - 1] > c)
return ks(n - 1, c);
int a = ks(n - 1, c);
int b = val[n - 1] + ks(n - 1, c - wt[n - 1]);
ans[n][c] = max(a, b);
return ans[n][c];
}
void solve() {
cin >> N >> w;
for (int i = 0; i < N; ++i) {
cin >> wt[i] >> val[i];
}
memset(ans, -1, sizeof(ans));
cout << ks(N, w);
}
int32_t main() { solve(); }
|
replace
| 5 | 6 | 5 | 6 |
0
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
long long recurse(int n, int w, vector<int> weights, vector<long long> values,
vector<vector<long long>> dp) {
if (w <= 0 || n <= 0)
return 0;
else {
if (dp[w][n] != -1)
return dp[w][n];
else {
if (w - weights[n - 1] >= 0)
dp[w][n] = max(recurse(n - 1, w - weights[n - 1], weights, values, dp) +
values[n - 1],
recurse(n - 1, w, weights, values, dp));
else
dp[w][n] = recurse(n - 1, w, weights, values, dp);
return dp[w][n];
}
}
}
int main(int argc, char const *argv[]) {
int n, w;
long long temp;
cin >> n >> w;
vector<int> weights(n, 0);
vector<long long> values(n, 0);
for (int i = 0; i < n; i++) {
cin >> temp;
weights[i] = temp;
cin >> temp;
values[i] = temp;
}
vector<vector<long long>> dp(w + 1, vector<long long>(n + 1, -1));
cout << recurse(n, w, weights, values, dp);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long recurse(int n, int w, vector<int> &weights, vector<long long> &values,
vector<vector<long long>> &dp) {
if (w <= 0 || n <= 0)
return 0;
else {
if (dp[w][n] != -1)
return dp[w][n];
else {
if (w - weights[n - 1] >= 0)
dp[w][n] = max(recurse(n - 1, w - weights[n - 1], weights, values, dp) +
values[n - 1],
recurse(n - 1, w, weights, values, dp));
else
dp[w][n] = recurse(n - 1, w, weights, values, dp);
return dp[w][n];
}
}
}
int main(int argc, char const *argv[]) {
int n, w;
long long temp;
cin >> n >> w;
vector<int> weights(n, 0);
vector<long long> values(n, 0);
for (int i = 0; i < n; i++) {
cin >> temp;
weights[i] = temp;
cin >> temp;
values[i] = temp;
}
vector<vector<long long>> dp(w + 1, vector<long long>(n + 1, -1));
cout << recurse(n, w, weights, values, dp);
return 0;
}
|
replace
| 4 | 6 | 4 | 6 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define INF (1 << 30)
#define MOD 1000000007
#define l_ength size
using ll = long long;
using namespace std;
ll dp[110][100100];
int main() {
ll n, wc;
cin >> n >> wc;
vector<ll> w(n, 0);
vector<ll> v(n, 0);
for (int i = 0; i < n; ++i) {
cin >> w[i + 1] >> v[i + 1];
}
for (ll i = 1; i <= n; ++i) {
for (ll j = 1; j <= wc; ++j) {
dp[i][j] = dp[i - 1][j];
if (j >= w[i])
dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i]] + v[i]);
}
}
cout << dp[n][wc] << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define INF (1 << 30)
#define MOD 1000000007
#define l_ength size
using ll = long long;
using namespace std;
ll dp[110][100100];
int main() {
ll n, wc;
cin >> n >> wc;
vector<ll> w(n + 10, 0);
vector<ll> v(n + 10, 0);
for (int i = 0; i < n; ++i) {
cin >> w[i + 1] >> v[i + 1];
}
for (ll i = 1; i <= n; ++i) {
for (ll j = 1; j <= wc; ++j) {
dp[i][j] = dp[i - 1][j];
if (j >= w[i])
dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i]] + v[i]);
}
}
cout << dp[n][wc] << endl;
return 0;
}
|
replace
| 12 | 14 | 12 | 14 |
-6
|
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
|
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>
#define fir first
#define sec second
#define sz(s) (s).size();
#define pb push_back
#define get(n) scanf("%d", &n);
#define gets(s) \
string s; \
cin >> (s);
#define prfi(n) printf("%d", &n);
#define prfd(n) printf("%lf", &n);
#define All(s) (s).begin(), (s).end()
#define rep(i, j, k) for (int(i) = (j); (i) <= (k); (i)++)
#define repdown(i, j, k) for (int(i) = (j); (i) >= (k); (i)--)
#define dump(x) std::cout << #x << " = " << (x) << std::endl;
#define debug(x) \
cout << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl;
using ll = long long;
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vd = std::vector<double>;
using vvd = std::vector<vd>;
using qi = std::queue<int>;
using vpii = std::vector<std::pair<int, int>>;
using namespace std;
const int Mod = (1e9) + 7;
const int max_n = 3 * (1e5) + 1;
const int max_m = 83 * (1e5) + 1;
const int INF = 10241024;
long double INFD = 100100100;
//_____________________________________Templates_________________________________________//
template <class T1, class T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <class T1, class T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
// mainly use for dynamic prog
template <class T1, class T2> void update(T1 &a, T2 b) {
a += b;
if (a > Mod)
a %= Mod;
}
inline void IN(void) { return; }
template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
cin >> first;
IN(rest...);
return;
}
inline void OUT(void) {
cout << "\n";
return;
}
template <typename First, typename... Rest>
void OUT(First first, Rest... rest) {
cout << first << " ";
OUT(rest...);
return;
}
//_____________________array calc____________________________________//
/*
vvi mul(vvi &A, vvi &B){
vvi C(A.size(), vi(B.size()))
rep(i,A.size()){
rep(j,B.size()){
rep(k,B[0].size()){
C[i][j] = (C[i][j] A[i][k] + B[k][j]) % Mod;
}
}
}
return C;
}
vvi pow(vvi A, ll n){
vvi B(A.size(), vi(A.size()));
rep(i=0;i<A.size();i++){
B[i][i] = 1;
}
while (n >0){
if (n & 1) B = mul(B, A);
A = mul(A, A);
n = n >> 1;
}
return B;
}
*/
//_____________________Bynary Indexed Tree __________________________//
/*
const max_st = (1 << 15) - 1;
int bit[max_st];
int sum (int i){
int s = 0;
while(i > 0){
s += bit[i];
i -= i & -i;
}
return s;
}
void add(int i, int x){
while(i <= n){
bit[i] += x;
i += i & -i;
}
}
*/
//_____________________ following sorce code_________________________//
vll dp(10010);
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N, W;
IN(N, W);
rep(i, 0, N - 1) {
int a, b;
IN(a, b);
repdown(j, W, a) { chmax(dp[j], dp[j - a] + b); }
}
OUT(dp[W]);
return 0;
}
|
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>
#define fir first
#define sec second
#define sz(s) (s).size();
#define pb push_back
#define get(n) scanf("%d", &n);
#define gets(s) \
string s; \
cin >> (s);
#define prfi(n) printf("%d", &n);
#define prfd(n) printf("%lf", &n);
#define All(s) (s).begin(), (s).end()
#define rep(i, j, k) for (int(i) = (j); (i) <= (k); (i)++)
#define repdown(i, j, k) for (int(i) = (j); (i) >= (k); (i)--)
#define dump(x) std::cout << #x << " = " << (x) << std::endl;
#define debug(x) \
cout << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl;
using ll = long long;
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vd = std::vector<double>;
using vvd = std::vector<vd>;
using qi = std::queue<int>;
using vpii = std::vector<std::pair<int, int>>;
using namespace std;
const int Mod = (1e9) + 7;
const int max_n = 3 * (1e5) + 1;
const int max_m = 83 * (1e5) + 1;
const int INF = 10241024;
long double INFD = 100100100;
//_____________________________________Templates_________________________________________//
template <class T1, class T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <class T1, class T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
// mainly use for dynamic prog
template <class T1, class T2> void update(T1 &a, T2 b) {
a += b;
if (a > Mod)
a %= Mod;
}
inline void IN(void) { return; }
template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
cin >> first;
IN(rest...);
return;
}
inline void OUT(void) {
cout << "\n";
return;
}
template <typename First, typename... Rest>
void OUT(First first, Rest... rest) {
cout << first << " ";
OUT(rest...);
return;
}
//_____________________array calc____________________________________//
/*
vvi mul(vvi &A, vvi &B){
vvi C(A.size(), vi(B.size()))
rep(i,A.size()){
rep(j,B.size()){
rep(k,B[0].size()){
C[i][j] = (C[i][j] A[i][k] + B[k][j]) % Mod;
}
}
}
return C;
}
vvi pow(vvi A, ll n){
vvi B(A.size(), vi(A.size()));
rep(i=0;i<A.size();i++){
B[i][i] = 1;
}
while (n >0){
if (n & 1) B = mul(B, A);
A = mul(A, A);
n = n >> 1;
}
return B;
}
*/
//_____________________Bynary Indexed Tree __________________________//
/*
const max_st = (1 << 15) - 1;
int bit[max_st];
int sum (int i){
int s = 0;
while(i > 0){
s += bit[i];
i -= i & -i;
}
return s;
}
void add(int i, int x){
while(i <= n){
bit[i] += x;
i += i & -i;
}
}
*/
//_____________________ following sorce code_________________________//
vll dp(500000);
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N, W;
IN(N, W);
rep(i, 0, N - 1) {
int a, b;
IN(a, b);
repdown(j, W, a) { chmax(dp[j], dp[j - a] + b); }
}
OUT(dp[W]);
return 0;
}
|
replace
| 135 | 136 | 135 | 136 |
0
| |
p03163
|
C++
|
Runtime Error
|
/*Function Template*/
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pint;
const int MAX = 510000;
const int MOD = 1000000007;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define Rep(i, n) for (ll i = 1; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
ll fac[MAX], finv[MAX], inv[MAX];
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll Len(ll n) {
ll s = 0;
while (n != 0)
s++, n /= 10;
return s;
}
ll Sint(ll n) {
ll m = 0, s = 0, a = n;
while (a != 0)
s++, a /= 10;
for (ll i = s - 1; i >= 0; i--)
m += n / ((ll)pow(10, i)) - (n / ((ll)pow(10, i + 1))) * 10;
return m;
}
ll Svec(vector<ll> v) {
ll n = 0;
for (ll i = 0; i < v.size(); i++)
n += v[i];
return n;
}
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
ll Factorial(ll n) {
ll m = 1;
while (n >= 1)
m *= n, n--;
return m;
}
void runlength(string s, vector<pair<char, ll>> &p) {
ll x = 1;
if (s.size() == 1) {
p.push_back(pair<char, ll>(s[0], 1));
}
for (ll i = 0; i < s.size() - 1; i++) {
if (s[i] == s[i + 1]) {
x++;
if (i == s.size() - 2) {
p.push_back(pair<char, ll>(s[i], x));
}
} else {
p.push_back(pair<char, ll>(s[i], x));
x = 1;
if (i == s.size() - 2) {
p.push_back(pair<char, ll>(s[s.size() - 1], x));
}
}
}
}
ll COM(ll n, ll k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
const int MAX_N = 100010;
vector<bool> sieve_of_eratosthenes() {
vector<bool> isPrime(MAX_N + 1, true);
for (int i = 2; i <= MAX_N; i++) {
if (isPrime[i]) {
for (int j = 2 * i; j <= MAX_N; j += i) {
isPrime[j] = false;
}
}
}
return isPrime;
}
vector<pint> prime_factorize(ll n) {
vector<pint> ans;
for (ll p = 2; p <= sqrt(n); p++) {
if (n % p != 0)
continue;
ll cnt = 0;
while (n % p == 0) {
n /= p;
cnt++;
}
ans.push_back(make_pair(p, cnt));
}
if (n != 1)
ans.push_back(make_pair(n, 1));
return ans;
}
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
int main() {
IOS;
ll n, w;
cin >> n >> w;
vector<ll> weight(n), value(n);
rep(i, n) cin >> weight[i] >> value[i];
ll dp[n + 1][w + 1];
rep(i, w + 1) dp[0][i] = 0;
rep(i, n) {
rep(j, w + 1) {
if (j - weight[i])
dp[i + 1][j] = max(dp[i][j], dp[i][j - weight[i]] + value[i]);
else
dp[i + 1][j] = dp[i][j];
}
}
cout << dp[n][w] << endl;
}
|
/*Function Template*/
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pint;
const int MAX = 510000;
const int MOD = 1000000007;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define Rep(i, n) for (ll i = 1; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
ll fac[MAX], finv[MAX], inv[MAX];
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll Len(ll n) {
ll s = 0;
while (n != 0)
s++, n /= 10;
return s;
}
ll Sint(ll n) {
ll m = 0, s = 0, a = n;
while (a != 0)
s++, a /= 10;
for (ll i = s - 1; i >= 0; i--)
m += n / ((ll)pow(10, i)) - (n / ((ll)pow(10, i + 1))) * 10;
return m;
}
ll Svec(vector<ll> v) {
ll n = 0;
for (ll i = 0; i < v.size(); i++)
n += v[i];
return n;
}
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
ll Factorial(ll n) {
ll m = 1;
while (n >= 1)
m *= n, n--;
return m;
}
void runlength(string s, vector<pair<char, ll>> &p) {
ll x = 1;
if (s.size() == 1) {
p.push_back(pair<char, ll>(s[0], 1));
}
for (ll i = 0; i < s.size() - 1; i++) {
if (s[i] == s[i + 1]) {
x++;
if (i == s.size() - 2) {
p.push_back(pair<char, ll>(s[i], x));
}
} else {
p.push_back(pair<char, ll>(s[i], x));
x = 1;
if (i == s.size() - 2) {
p.push_back(pair<char, ll>(s[s.size() - 1], x));
}
}
}
}
ll COM(ll n, ll k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
const int MAX_N = 100010;
vector<bool> sieve_of_eratosthenes() {
vector<bool> isPrime(MAX_N + 1, true);
for (int i = 2; i <= MAX_N; i++) {
if (isPrime[i]) {
for (int j = 2 * i; j <= MAX_N; j += i) {
isPrime[j] = false;
}
}
}
return isPrime;
}
vector<pint> prime_factorize(ll n) {
vector<pint> ans;
for (ll p = 2; p <= sqrt(n); p++) {
if (n % p != 0)
continue;
ll cnt = 0;
while (n % p == 0) {
n /= p;
cnt++;
}
ans.push_back(make_pair(p, cnt));
}
if (n != 1)
ans.push_back(make_pair(n, 1));
return ans;
}
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
int main() {
IOS;
ll n, w;
cin >> n >> w;
vector<ll> weight(n), value(n);
rep(i, n) cin >> weight[i] >> value[i];
ll dp[n + 1][w + 1];
rep(i, w + 1) dp[0][i] = 0;
rep(i, n) {
rep(j, w + 1) {
if (j - weight[i] >= 0)
dp[i + 1][j] = max(dp[i][j], dp[i][j - weight[i]] + value[i]);
else
dp[i + 1][j] = dp[i][j];
}
}
cout << dp[n][w] << endl;
}
|
replace
| 145 | 146 | 145 | 146 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
int main() {
ll N, W;
cin >> N >> W;
vector<ll> weight(N), value(N);
rep(i, N) cin >> weight[i] >> value[i];
ll dp[110][100010];
rep(i, N + 1) dp[0][i] = 0;
rep(i, N) {
rep(j, W + 1) {
if (W >= weight[i])
dp[i + 1][j] = max(dp[i][j - weight[i]] + value[i], dp[i][j]);
else
dp[i + 1][j] = dp[i][j];
}
}
cout << dp[N][W] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
int main() {
ll N, W;
cin >> N >> W;
vector<ll> weight(N), value(N);
rep(i, N) cin >> weight[i] >> value[i];
ll dp[110][100010];
rep(i, N + 1) dp[0][i] = 0;
rep(i, N) {
rep(j, W + 1) {
if (j >= weight[i])
dp[i + 1][j] = max(dp[i][j - weight[i]] + value[i], dp[i][j]);
else
dp[i + 1][j] = dp[i][j];
}
}
cout << dp[N][W] << endl;
}
|
replace
| 13 | 14 | 13 | 14 |
-11
| |
p03163
|
C++
|
Runtime Error
|
/*
@author : srinathbalaji_99
*/
#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define endl '\n'
#define MAX LLONG_MAX
#define MIN LLONG_MIN
#define rep(i, a, b, inc) for (long long i = a; i < b; i += inc)
#define REP(i, n) rep(i, 0, n, 1)
#define MEM(a, b) memset(a, (b), sizeof(a))
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define PLL pair<long long, long long>
#define VL vector<long long>
#define VS vector<string>
#define VLL vector<PLL>
#define VVL vector<VL>
#define MPLL map<long long, long long>
#define UMPLL unordered_map<long long, long long>
#define SETL set<long long>
#define MSETL multiset<long long>
#define GCD(a, b) __gcd(a, b)
#define LCM(a, b) (a * b) / GCD(a, b)
#define ff first
#define ss second
#define pall(a) \
REP(i, sizeof(a) / sizeof(a[0])) cout << a[i] << " "; \
cout << endl;
#define pvall(v) \
REP(i, v.size()) cout << v[i] << " "; \
cout << endl;
#define gall(a, n) REP(i, n) cin >> a[i];
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
typedef unsigned long long ULL;
typedef long long LL;
typedef long double LD;
using namespace std;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
LL weight[101], value[101], dp[101][100001];
int main() {
fast();
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
LL t, n, i, j, k, x, y, c, W;
cin >> n >> W;
REP(i, n) { cin >> weight[i] >> value[i]; }
REP(i, n) {
for (LL w = 0; w <= W; ++w) {
if (w - weight[i] >= 0) {
dp[i + 1][w] = max(dp[i][w], dp[i][w - weight[i]] + value[i]);
} else
dp[i + 1][w] = dp[i][w];
}
}
cout << dp[n][W];
}
|
/*
@author : srinathbalaji_99
*/
#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define endl '\n'
#define MAX LLONG_MAX
#define MIN LLONG_MIN
#define rep(i, a, b, inc) for (long long i = a; i < b; i += inc)
#define REP(i, n) rep(i, 0, n, 1)
#define MEM(a, b) memset(a, (b), sizeof(a))
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define PLL pair<long long, long long>
#define VL vector<long long>
#define VS vector<string>
#define VLL vector<PLL>
#define VVL vector<VL>
#define MPLL map<long long, long long>
#define UMPLL unordered_map<long long, long long>
#define SETL set<long long>
#define MSETL multiset<long long>
#define GCD(a, b) __gcd(a, b)
#define LCM(a, b) (a * b) / GCD(a, b)
#define ff first
#define ss second
#define pall(a) \
REP(i, sizeof(a) / sizeof(a[0])) cout << a[i] << " "; \
cout << endl;
#define pvall(v) \
REP(i, v.size()) cout << v[i] << " "; \
cout << endl;
#define gall(a, n) REP(i, n) cin >> a[i];
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
typedef unsigned long long ULL;
typedef long long LL;
typedef long double LD;
using namespace std;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
LL weight[101], value[101], dp[101][100001];
int main() {
fast();
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
LL t, n, i, j, k, x, y, c, W;
cin >> n >> W;
REP(i, n) { cin >> weight[i] >> value[i]; }
REP(i, n) {
for (LL w = 0; w <= W; ++w) {
if (w - weight[i] >= 0) {
dp[i + 1][w] = max(dp[i][w], dp[i][w - weight[i]] + value[i]);
} else
dp[i + 1][w] = dp[i][w];
}
}
cout << dp[n][W];
}
|
replace
| 55 | 59 | 55 | 59 |
TLE
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
long long w[1010];
long long v[1010];
int N;
long long dp[110][100010];
long long mo(int pos, long long W) {
if (W < 0)
return -1e9;
if (pos == N) {
return 0;
}
long long re = dp[pos][W];
if (re != -1)
return re;
re = max(mo(pos + 1, W), mo(pos + 1, W - w[pos]) + v[pos]);
return re;
}
int main() {
int W;
while (cin >> N >> W) {
memset(dp, -1, sizeof dp);
for (int i = 0; i < N; i++) {
cin >> w[i] >> v[i];
}
cout << mo(0, W) << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long w[1010];
long long v[1010];
int N;
long long dp[110][100010];
long long mo(int pos, long long W) {
if (W < 0)
return -1e9;
if (pos == N) {
return 0;
}
long long &re = dp[pos][W];
if (re != -1)
return re;
re = max(mo(pos + 1, W), mo(pos + 1, W - w[pos]) + v[pos]);
return re;
}
int main() {
int W;
while (cin >> N >> W) {
memset(dp, -1, sizeof dp);
for (int i = 0; i < N; i++) {
cin >> w[i] >> v[i];
}
cout << mo(0, W) << "\n";
}
return 0;
}
|
replace
| 12 | 13 | 12 | 13 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repk(i, k, n) for (int i = k; i < n; i++)
#define all(vec) vec.begin(), vec.end()
#define ll long long
#define debug(x) cout << "debug: " << x << endl
#define MOD 1e9 + 7
#define INF 1e9
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
int n, w;
cin >> n >> w;
vector<vector<int>> v(n, vector<int>(2));
rep(i, n) rep(j, 2) cin >> v[i][j];
vector<vector<ll>> dp(110, vector<ll>(10010, 0));
rep(i, n) {
rep(j, w + 1) {
if (j - v[i][0] >= 0)
chmax(dp[i + 1][j], dp[i][j - v[i][0]] + v[i][1]);
chmax(dp[i + 1][j], dp[i][j]);
}
}
cout << dp[n][w] << endl;
}
|
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repk(i, k, n) for (int i = k; i < n; i++)
#define all(vec) vec.begin(), vec.end()
#define ll long long
#define debug(x) cout << "debug: " << x << endl
#define MOD 1e9 + 7
#define INF 1e9
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
int n, w;
cin >> n >> w;
vector<vector<int>> v(n, vector<int>(2));
rep(i, n) rep(j, 2) cin >> v[i][j];
vector<vector<ll>> dp(110, vector<ll>(100100, 0));
rep(i, n) {
rep(j, w + 1) {
if (j - v[i][0] >= 0)
chmax(dp[i + 1][j], dp[i][j - v[i][0]] + v[i][1]);
chmax(dp[i + 1][j], dp[i][j]);
}
}
cout << dp[n][w] << endl;
}
|
replace
| 41 | 42 | 41 | 42 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define endl "\n"
#define sd(val) scanf("%d", &val)
#define ss(val) scanf("%s", &val)
#define sl(val) scanf("%lld", &val)
#define debug(val) printf("check%d\n", val)
#define all(v) v.begin(), v.end()
#define PB push_back
#define MP make_pair
#define FF first
#define SS second
#define ll long long
#define ull unsigned long long
#define MOD 1000000007
#define clr(val) memset(val, 0, sizeof(val))
#define what_is(x) cerr << #x << " is " << x << endl;
#define OJ \
freopen("input.txt", "r", stdin); \
freopen("output.txt", "w", stdout);
#define FIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
ll n, w;
ll we[105];
ll va[105];
ull mem[105][105];
ll dp(ll n, ll w) {
if (w == 0 || n == 0)
return 0;
if (mem[n][w] != -1)
return mem[n][w];
if (we[n] <= w)
return mem[n][w] = max(va[n] + dp(n - 1, w - we[n]), dp(n - 1, w));
else {
return mem[n][w] = dp(n - 1, w);
}
}
int main() {
cin >> n >> w;
for (ll i = 1; i <= n; i++) {
cin >> we[i] >> va[i];
}
memset(mem, -1, sizeof(mem));
cout << dp(n, w) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define endl "\n"
#define sd(val) scanf("%d", &val)
#define ss(val) scanf("%s", &val)
#define sl(val) scanf("%lld", &val)
#define debug(val) printf("check%d\n", val)
#define all(v) v.begin(), v.end()
#define PB push_back
#define MP make_pair
#define FF first
#define SS second
#define ll long long
#define ull unsigned long long
#define MOD 1000000007
#define clr(val) memset(val, 0, sizeof(val))
#define what_is(x) cerr << #x << " is " << x << endl;
#define OJ \
freopen("input.txt", "r", stdin); \
freopen("output.txt", "w", stdout);
#define FIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
ll n, w;
ll we[105];
ll va[105];
ll mem[105][100005];
ll dp(ll n, ll w) {
if (w == 0 || n == 0)
return 0;
if (mem[n][w] != -1)
return mem[n][w];
if (we[n] <= w)
return mem[n][w] = max(va[n] + dp(n - 1, w - we[n]), dp(n - 1, w));
else {
return mem[n][w] = dp(n - 1, w);
}
}
int main() {
cin >> n >> w;
for (ll i = 1; i <= n; i++) {
cin >> we[i] >> va[i];
}
memset(mem, -1, sizeof(mem));
cout << dp(n, w) << endl;
return 0;
}
|
replace
| 31 | 32 | 31 | 32 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vl>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vb = vector<bool>;
using vs = vector<string>;
const ll oo = 0x3f3f3f3f3f3f3f3fLL;
const ld eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = ll(b) - 1; i >= (a); i--)
#define TR(X) \
({ \
if (1) \
cerr << "TR: " << (#X) << " = " << (X) << endl; \
})
ll weight[101] = {0};
ll value[101] = {0};
ll maximum[100][100000] = {};
ll n, w;
ll knapsack(ll item, ll weightLeft) {
if (weightLeft == 0) {
return 0;
}
if (weightLeft < 0) {
return -oo;
}
if (item > n) {
return 0;
}
if (maximum[item][weightLeft] != -1) {
return maximum[item][weightLeft];
}
ll res;
res = max(knapsack(item + 1, weightLeft - weight[item]) + value[item],
knapsack(item + 1, weightLeft));
maximum[item][weightLeft] = res;
return res;
}
int main() {
cin.sync_with_stdio(0);
fill(&maximum[0][0], &maximum[0][0] + sizeof(maximum) / sizeof(maximum[0][0]),
-1);
cin >> n >> w;
FOR(i, 1, n + 1) {
ll tw, tv;
cin >> weight[i] >> value[i];
}
cout << knapsack(1, w);
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vl>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vb = vector<bool>;
using vs = vector<string>;
const ll oo = 0x3f3f3f3f3f3f3f3fLL;
const ld eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = ll(b) - 1; i >= (a); i--)
#define TR(X) \
({ \
if (1) \
cerr << "TR: " << (#X) << " = " << (X) << endl; \
})
ll weight[101] = {0};
ll value[101] = {0};
ll maximum[105][100005] = {};
ll n, w;
ll knapsack(ll item, ll weightLeft) {
if (weightLeft == 0) {
return 0;
}
if (weightLeft < 0) {
return -oo;
}
if (item > n) {
return 0;
}
if (maximum[item][weightLeft] != -1) {
return maximum[item][weightLeft];
}
ll res;
res = max(knapsack(item + 1, weightLeft - weight[item]) + value[item],
knapsack(item + 1, weightLeft));
maximum[item][weightLeft] = res;
return res;
}
int main() {
cin.sync_with_stdio(0);
fill(&maximum[0][0], &maximum[0][0] + sizeof(maximum) / sizeof(maximum[0][0]),
-1);
cin >> n >> w;
FOR(i, 1, n + 1) {
ll tw, tv;
cin >> weight[i] >> value[i];
}
cout << knapsack(1, w);
}
|
replace
| 30 | 31 | 30 | 31 |
0
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
long long int n, w;
cin >> n >> w;
long long int weight[n + 1], value[n + 1];
for (ll int i = 1; i <= n; i++) {
cin >> weight[i] >> value[i];
}
ll int dp[n + 1][w + 1];
for (ll int i = 0; i <= n; i++) {
dp[i][0] = 0;
}
for (ll int i = 0; i <= w; i++) {
dp[0][i] = 0;
}
for (ll int i = 1; i <= n; i++) {
for (ll int j = 1; j <= w; j++) {
if (j >= weight[i]) {
ll int cap = j - weight[i], add = 0;
for (ll int k = 0; k < i; k++) {
if (dp[k][cap] > add) {
add = dp[k][cap];
}
}
dp[i][j] = value[i] + add;
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
/*for(int i=0;i<=n;i++){
for(int j=0;j<=w;j++){
cout<<dp[i][j]<<" ";
}
cout<<endl;
}*/
ll int ans = 0;
for (ll int i = 0; i <= n; i++) {
if (dp[i][w] > ans) {
ans = dp[i][w];
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
long long int n, w;
cin >> n >> w;
long long int weight[n + 1], value[n + 1];
for (ll int i = 1; i <= n; i++) {
cin >> weight[i] >> value[i];
}
ll int dp[n + 1][w + 1];
for (ll int i = 0; i <= n; i++) {
dp[i][0] = 0;
}
for (ll int i = 0; i <= w; i++) {
dp[0][i] = 0;
}
for (ll int i = 1; i <= n; i++) {
for (ll int j = 1; j <= w; j++) {
if (j >= weight[i]) {
ll int cap = j - weight[i], add = 0;
add = dp[i - 1][cap];
dp[i][j] = max(value[i] + add, dp[i - 1][j]);
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
/*for(int i=0;i<=n;i++){
for(int j=0;j<=w;j++){
cout<<dp[i][j]<<" ";
}
cout<<endl;
}*/
ll int ans = 0;
for (ll int i = 0; i <= n; i++) {
if (dp[i][w] > ans) {
ans = dp[i][w];
}
}
cout << ans << endl;
}
|
replace
| 21 | 27 | 21 | 23 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define pb push_back
#define rep(a, b, c) for (int a = (int)b; a < (int)c; a++)
#define repk(a, b, c, k) for (int a = (int)b; a < (int)c; a += (int)k)
#define comeback \
std::ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define mod 1000000007
#define int long long int
#define pii pair<int, int>
#define ful(a) a.begin(), a.end()
#define ub upper_bound
#define lb lower_bound
#define ff first
#define ss second
using namespace std;
int em(int x, int n) {
if (n == 0)
return 1;
else if (n % 2 == 0)
return em((x % mod * x % mod) % mod, n / 2);
else
return (x % mod * em((x % mod * x % mod) % mod, (n - 1) / 2)) % mod;
}
signed main() {
comeback
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output1.txt", "w", stdout);
#endif
int n, W, i, j, k, l;
cin >> n >> W;
int w[n + 1], va[n + 1];
for (i = 1; i <= n; i++) {
cin >> w[i] >> va[i];
}
int dp[n + 1][W + 1];
memset(dp, -1, sizeof(dp));
for (i = 0; i <= W; i++)
dp[0][i] = 0;
for (i = 1; i <= n; i++) {
for (j = 0; j <= W; j++) {
if (w[i] <= j)
dp[i][j] = max(va[i] + dp[i - 1][j - w[i]], dp[i - 1][j]);
else
dp[i][j] = dp[i - 1][j];
}
}
cout << dp[n][W];
}
|
#include <bits/stdc++.h>
#define pb push_back
#define rep(a, b, c) for (int a = (int)b; a < (int)c; a++)
#define repk(a, b, c, k) for (int a = (int)b; a < (int)c; a += (int)k)
#define comeback \
std::ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define mod 1000000007
#define int long long int
#define pii pair<int, int>
#define ful(a) a.begin(), a.end()
#define ub upper_bound
#define lb lower_bound
#define ff first
#define ss second
using namespace std;
int em(int x, int n) {
if (n == 0)
return 1;
else if (n % 2 == 0)
return em((x % mod * x % mod) % mod, n / 2);
else
return (x % mod * em((x % mod * x % mod) % mod, (n - 1) / 2)) % mod;
}
signed main() {
comeback
/*#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output1.txt","w",stdout);
#endif*/
int n,
W, i, j, k, l;
cin >> n >> W;
int w[n + 1], va[n + 1];
for (i = 1; i <= n; i++) {
cin >> w[i] >> va[i];
}
int dp[n + 1][W + 1];
memset(dp, -1, sizeof(dp));
for (i = 0; i <= W; i++)
dp[0][i] = 0;
for (i = 1; i <= n; i++) {
for (j = 0; j <= W; j++) {
if (w[i] <= j)
dp[i][j] = max(va[i] + dp[i - 1][j - w[i]], dp[i - 1][j]);
else
dp[i][j] = dp[i - 1][j];
}
}
cout << dp[n][W];
}
|
replace
| 27 | 32 | 27 | 33 |
-11
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int n, w;
int value[100], weight[100];
long int bag[100][10005] = {0};
cin >> n;
cin >> w;
for (int i = 0; i < n; i++) {
cin >> weight[i] >> value[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= w; j++) {
if (weight[i] <= j) {
bag[i + 1][j] = max(bag[i][j - weight[i]] + value[i], bag[i][j]);
} else {
bag[i + 1][j] = bag[i][j];
}
}
}
cout << bag[n][w] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int n, w;
int value[100], weight[100];
long int bag[101][100005] = {0};
cin >> n;
cin >> w;
for (int i = 0; i < n; i++) {
cin >> weight[i] >> value[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= w; j++) {
if (weight[i] <= j) {
bag[i + 1][j] = max(bag[i][j - weight[i]] + value[i], bag[i][j]);
} else {
bag[i + 1][j] = bag[i][j];
}
}
}
cout << bag[n][w] << endl;
return 0;
}
|
replace
| 5 | 6 | 5 | 6 |
0
| |
p03163
|
C++
|
Memory Limit Exceeded
|
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, w;
int dp[100001][1001];
int v[100001][1001];
int w1[100001];
int v1[100001];
int s1(int wr, int i) {
if (wr < 0) {
return INT_MIN;
}
if (i == n + 1) {
return 0;
}
if (v[wr][i]) {
return dp[wr][i];
}
v[wr][i] = 1;
dp[wr][i] = max(v1[i] + s1(wr - w1[i], i + 1), s1(wr, i + 1));
return dp[wr][i];
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin>>t;
// int k=1;
while (t--) {
cin >> n >> w;
for (int i = 1; i <= n; i++) {
cin >> w1[i] >> v1[i];
}
cout << s1(w, 1) << '\n';
}
}
|
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, w;
int dp[100001][101];
int v[100001][101];
int w1[101];
int v1[101];
int s1(int wr, int i) {
if (wr < 0) {
return INT_MIN;
}
if (i == n + 1) {
return 0;
}
if (v[wr][i]) {
return dp[wr][i];
}
v[wr][i] = 1;
dp[wr][i] = max(v1[i] + s1(wr - w1[i], i + 1), s1(wr, i + 1));
return dp[wr][i];
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin>>t;
// int k=1;
while (t--) {
cin >> n >> w;
for (int i = 1; i <= n; i++) {
cin >> w1[i] >> v1[i];
}
cout << s1(w, 1) << '\n';
}
}
|
replace
| 5 | 9 | 5 | 9 |
MLE
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define pb push_back
#define int long long int
#define mod 1000000007
#define inf (1LL << 60)
#define f(i, a, b) for (int i = a; i < b; ++i)
using namespace std;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, w;
cin >> n >> w;
pair<int, int> p[n];
for (int i = 0; i < n; i++)
cin >> p[i].first >> p[i].second;
int dp[n + 1][w + 1];
memset(dp, 0, sizeof dp);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= w; j++) {
dp[i][j] = dp[i - 1][j];
if (p[i].first >= j)
dp[i][j] = max(dp[i][j], p[i].second + dp[i - 1][j - p[i].first]);
}
}
cout << dp[n][w];
}
|
#include <bits/stdc++.h>
#define pb push_back
#define int long long int
#define mod 1000000007
#define inf (1LL << 60)
#define f(i, a, b) for (int i = a; i < b; ++i)
using namespace std;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, w;
cin >> n >> w;
pair<int, int> p[n];
for (int i = 0; i < n; i++)
cin >> p[i].first >> p[i].second;
int dp[n + 1][w + 1];
memset(dp, 0, sizeof dp);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= w; j++) {
dp[i][j] = dp[i - 1][j];
if (p[i - 1].first <= j)
dp[i][j] =
max(dp[i][j], p[i - 1].second + dp[i - 1][j - p[i - 1].first]);
}
}
cout << dp[n][w];
}
|
replace
| 22 | 24 | 22 | 25 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define fastii \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define endl "\n"
#define f0(i, n) for (int i = 0; i < n; i++)
#define f1(i, n) for (int i = 1; i <= n; i++)
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define ll long long int
#define pi pair<int, int>
#define ai(v) v.begin(), v.end()
#define air(v) v.rbegin(), v.rend()
#define pii 3.14159265358979323
#define inf LLONG_MAX
#define fill(a, b) memset(a, b, sizeof(a))
#define mod 1000000007
ll static t[101][101];
ll mod_pow(ll a, ll b, ll m) {
ll res = 1;
while (b) {
if (b & 1) {
res = (res * a) % m;
}
a = (a * a) % m;
b >>= 1;
}
return res;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return (a % b, b);
}
void sieveofEratosthenes(ll n) {
bool prime[n + 1];
fill(prime, true);
for (int i = 2; i * i <= n; i++) {
if (prime[i] == true) {
for (int j = i * i; j <= n; j += i)
prime[j] = false;
}
}
for (int i = 2; i <= n; i++) {
if (prime[i])
cout << i << " ";
}
}
ll mod_inverse(ll a) { return mod_pow(a, mod - 2, mod); }
ll knapsack(ll wt[], ll val[], ll W, ll n) {
if (n == 0 || W == 0)
return 0;
if (t[n][W] != -1)
return t[n][W];
if (wt[n - 1] <= W)
return t[n][W] = max(val[n - 1] + knapsack(wt, val, W - wt[n - 1], n - 1),
knapsack(wt, val, W, n - 1));
else if (wt[n - 1] > W)
return (t[n][W] = knapsack(wt, val, W, n - 1));
}
void solve() {
ll n, W;
cin >> n >> W;
ll wt[n], val[n];
f0(i, n) {
fill(t, -1);
cin >> wt[i];
cin >> val[i];
}
cout << knapsack(wt, val, W, n) << endl;
;
}
signed main() {
fastii;
int T = 1;
while (T--) {
solve();
}
return 0;
}
/*Author :- Ankit Raj
*Site:-
*Topic:-
* Competative Programming
*email :-
*Technocrats Institute Of Technology
*5th Sem
*/
|
#include <bits/stdc++.h>
using namespace std;
#define fastii \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define endl "\n"
#define f0(i, n) for (int i = 0; i < n; i++)
#define f1(i, n) for (int i = 1; i <= n; i++)
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define ll long long int
#define pi pair<int, int>
#define ai(v) v.begin(), v.end()
#define air(v) v.rbegin(), v.rend()
#define pii 3.14159265358979323
#define inf LLONG_MAX
#define fill(a, b) memset(a, b, sizeof(a))
#define mod 1000000007
ll static t[101][100001];
ll mod_pow(ll a, ll b, ll m) {
ll res = 1;
while (b) {
if (b & 1) {
res = (res * a) % m;
}
a = (a * a) % m;
b >>= 1;
}
return res;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return (a % b, b);
}
void sieveofEratosthenes(ll n) {
bool prime[n + 1];
fill(prime, true);
for (int i = 2; i * i <= n; i++) {
if (prime[i] == true) {
for (int j = i * i; j <= n; j += i)
prime[j] = false;
}
}
for (int i = 2; i <= n; i++) {
if (prime[i])
cout << i << " ";
}
}
ll mod_inverse(ll a) { return mod_pow(a, mod - 2, mod); }
ll knapsack(ll wt[], ll val[], ll W, ll n) {
if (n == 0 || W == 0)
return 0;
if (t[n][W] != -1)
return t[n][W];
if (wt[n - 1] <= W)
return t[n][W] = max(val[n - 1] + knapsack(wt, val, W - wt[n - 1], n - 1),
knapsack(wt, val, W, n - 1));
else if (wt[n - 1] > W)
return (t[n][W] = knapsack(wt, val, W, n - 1));
}
void solve() {
ll n, W;
cin >> n >> W;
ll wt[n], val[n];
f0(i, n) {
fill(t, -1);
cin >> wt[i];
cin >> val[i];
}
cout << knapsack(wt, val, W, n) << endl;
;
}
signed main() {
fastii;
int T = 1;
while (T--) {
solve();
}
return 0;
}
/*Author :- Ankit Raj
*Site:-
*Topic:-
* Competative Programming
*email :-
*Technocrats Institute Of Technology
*5th Sem
*/
|
replace
| 22 | 23 | 22 | 23 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <queue>
#include <stack>
#include <vector>
typedef long long ll;
using namespace std;
#define rep(x, y) for (ll i = x; i <= y; i++)
#define repi(x, y) for (ll i = x; i >= y; i--)
priority_queue<ll> cancel;
ll weight[200005];
ll value[200005];
int main() {
ll test = 1;
// cin>>test;
for (ll z = 0; z < test; z++) {
ll n, W;
cin >> n >> W;
ll dp[n][W];
for (ll i = 1; i <= n; i++) {
cin >> weight[i];
cin >> value[i];
}
for (ll i = 0; i <= n; i++) {
dp[0][i] = 0;
}
for (ll i = 1; i <= W; i++) {
dp[i][0] = 0;
}
for (ll i = 0; i <= W; i++) {
for (ll j = 1; j <= n; j++) {
if (weight[j] > i) {
dp[i][j] = dp[i][j - 1];
} else {
dp[i][j] = max(dp[i][j - 1], dp[i - weight[j]][j - 1] + value[j]);
}
}
}
cout << dp[W][n] << endl;
}
}
|
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <queue>
#include <stack>
#include <vector>
typedef long long ll;
using namespace std;
#define rep(x, y) for (ll i = x; i <= y; i++)
#define repi(x, y) for (ll i = x; i >= y; i--)
priority_queue<ll> cancel;
ll weight[200005];
ll value[200005];
int main() {
ll test = 1;
// cin>>test;
for (ll z = 0; z < test; z++) {
ll n, W;
cin >> n >> W;
ll dp[W + 5][n + 5];
for (ll i = 1; i <= n; i++) {
cin >> weight[i];
cin >> value[i];
}
for (ll i = 0; i <= n; i++) {
dp[0][i] = 0;
}
for (ll i = 1; i <= W; i++) {
dp[i][0] = 0;
}
for (ll i = 0; i <= W; i++) {
for (ll j = 1; j <= n; j++) {
if (weight[j] > i) {
dp[i][j] = dp[i][j - 1];
} else {
dp[i][j] = max(dp[i][j - 1], dp[i - weight[j]][j - 1] + value[j]);
}
}
}
cout << dp[W][n] << endl;
}
}
|
replace
| 22 | 23 | 22 | 23 |
0
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.