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 <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll wt[102], val[102];
ll dp[100][100001];
ll func(ll n, ll w) {
if (dp[n][w] != -1)
return dp[n][w];
if (w >= wt[n])
dp[n][w] = max(func(n - 1, w), func(n - 1, w - wt[n]) + val[n]);
else
dp[n][w] = func(n - 1, w);
return dp[n][w];
}
int main() {
ios::sync_with_stdio(0);
ll n, w, i, j;
cin >> n >> w;
for (i = 0; i <= n; i++) {
for (j = 0; j <= 100000; j++)
dp[i][j] = -1;
}
for (i = 1; i <= n; i++)
cin >> wt[i] >> val[i];
for (i = 0; i <= w; i++)
dp[0][i] = 0;
for (i = 0; i <= n; i++)
dp[i][0] = 0;
ll ans = func(n, w);
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll wt[102], val[102];
ll dp[102][100001];
ll func(ll n, ll w) {
if (dp[n][w] != -1)
return dp[n][w];
if (w >= wt[n])
dp[n][w] = max(func(n - 1, w), func(n - 1, w - wt[n]) + val[n]);
else
dp[n][w] = func(n - 1, w);
return dp[n][w];
}
int main() {
ios::sync_with_stdio(0);
ll n, w, i, j;
cin >> n >> w;
for (i = 0; i <= n; i++) {
for (j = 0; j <= 100000; j++)
dp[i][j] = -1;
}
for (i = 1; i <= n; i++)
cin >> wt[i] >> val[i];
for (i = 0; i <= w; i++)
dp[0][i] = 0;
for (i = 0; i <= n; i++)
dp[i][0] = 0;
ll ans = func(n, w);
cout << ans << "\n";
return 0;
}
|
replace
| 6 | 7 | 6 | 7 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
int w[10500], v[105];
long long dp[105][10500];
using namespace std;
int main() {
int N, W;
scanf("%d%d", &N, &W);
for (int i = 0; i < N; i++)
scanf("%d%d", &w[i], &v[i]);
for (int i = N - 1; i >= 0; i--) {
for (int j = 0; j <= W; j++) {
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]);
}
}
printf("%lld\n", dp[0][W]);
return 0;
}
|
#include <bits/stdc++.h>
int w[105], v[105];
long long dp[105][105000];
using namespace std;
int main() {
int N, W;
scanf("%d%d", &N, &W);
for (int i = 0; i < N; i++)
scanf("%d%d", &w[i], &v[i]);
for (int i = N - 1; i >= 0; i--) {
for (int j = 0; j <= W; j++) {
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]);
}
}
printf("%lld\n", dp[0][W]);
return 0;
}
|
replace
| 2 | 4 | 2 | 4 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int64_t dp[101][10001];
int main() {
int N, W, w[100], v[100];
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; j++) {
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
if (j + w[i] <= W)
dp[i + 1][j + w[i]] = max(dp[i + 1][j + w[i]], dp[i][j] + v[i]);
}
}
int64_t ans = 0;
for (int j = 0; j <= W; j++)
ans = max(ans, dp[N][j]);
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int64_t dp[101][100001];
int main() {
int N, W, w[100], v[100];
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; j++) {
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
if (j + w[i] <= W)
dp[i + 1][j + w[i]] = max(dp[i + 1][j + w[i]], dp[i][j] + v[i]);
}
}
int64_t ans = 0;
for (int j = 0; j <= W; j++)
ans = max(ans, dp[N][j]);
cout << ans << endl;
return 0;
}
|
replace
| 3 | 4 | 3 | 4 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define int ll
#define D(x) cout << "D: " << #x << '=' << x << endl;
#define R(x, l) \
cout << "R: Array " << #x << endl; \
for (int i = 0; i < l; i++) \
cout << ' ' << i << ':' << *(x + i) << endl;
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template <typename tn> inline tn next(void);
template <typename tn>
inline ostream &operator<<(ostream &os, const vector<tn> &v);
int w[105], p[105], dp[105];
signed main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
int a, b;
cin >> a >> b;
for (int i = 0; i < a; i++)
cin >> w[i] >> p[i];
for (int i = 0; i < a; i++)
for (int j = b; j >= w[i]; j--)
dp[j] = max(dp[j], dp[j - w[i]] + p[i]);
cout << dp[b] << endl;
return ~~(0 - 0);
}
template <typename tn> inline tn next(void) {
tn k;
cin >> k;
return k;
}
template <typename tn>
inline ostream &operator<<(ostream &os, const vector<tn> &v) {
for (unsigned i = 0; i < v.size(); i++)
os << v[i] << ' ';
return os;
}
|
#include <bits/stdc++.h>
#define int ll
#define D(x) cout << "D: " << #x << '=' << x << endl;
#define R(x, l) \
cout << "R: Array " << #x << endl; \
for (int i = 0; i < l; i++) \
cout << ' ' << i << ':' << *(x + i) << endl;
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template <typename tn> inline tn next(void);
template <typename tn>
inline ostream &operator<<(ostream &os, const vector<tn> &v);
int w[105], p[105], dp[100500];
signed main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
int a, b;
cin >> a >> b;
for (int i = 0; i < a; i++)
cin >> w[i] >> p[i];
for (int i = 0; i < a; i++)
for (int j = b; j >= w[i]; j--)
dp[j] = max(dp[j], dp[j - w[i]] + p[i]);
cout << dp[b] << endl;
return ~~(0 - 0);
}
template <typename tn> inline tn next(void) {
tn k;
cin >> k;
return k;
}
template <typename tn>
inline ostream &operator<<(ostream &os, const vector<tn> &v) {
for (unsigned i = 0; i < v.size(); i++)
os << v[i] << ' ';
return os;
}
|
replace
| 14 | 15 | 14 | 15 |
0
| |
p03163
|
C++
|
Runtime Error
|
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
using ll = long long;
using namespace std;
const long long MOD = 1000000007;
const ll LINF = 1LL << 50;
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<ll> vn(100010, 0);
vector<ll> wn(100010, 0);
vector<vector<ll>> dp(100100, vector<ll>(110, 0));
REP(n, N) cin >> wn.at(n) >> vn.at(n);
for (int n = 0; n < N; n++) {
for (int w = 0; w <= W; w++) {
if (w - wn[n] >= 0) {
chmax(dp[n + 1][w], dp[n][w - wn[n]] + vn[n]);
}
chmax(dp[n + 1][w], dp[n][w]);
}
}
cout << dp[N][W] << endl;
}
|
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
using ll = long long;
using namespace std;
const long long MOD = 1000000007;
const ll LINF = 1LL << 50;
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<ll> vn(100010, 0);
vector<ll> wn(100010, 0);
vector<vector<ll>> dp(110, vector<ll>(100100, 0));
REP(n, N) cin >> wn.at(n) >> vn.at(n);
for (int n = 0; n < N; n++) {
for (int w = 0; w <= W; w++) {
if (w - wn[n] >= 0) {
chmax(dp[n + 1][w], dp[n][w - wn[n]] + vn[n]);
}
chmax(dp[n + 1][w], dp[n][w]);
}
}
cout << dp[N][W] << endl;
}
|
replace
| 33 | 34 | 33 | 34 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#if !ONLINE_JUDGE
#define debug
#endif
using namespace std;
/******* All Required define Pre-Processors and typedef Constants *******/
#define mem(a, b) memset(a, (b), sizeof(a))
#define repd(i, k) for (int i = k; i >= 0; i--)
#define rep(i, k) for (int i = 0; i < k; i++)
#define repn(i, k1, k2) for (ll i = k1; i < k2; i++)
#define sz(x) (ll)(x).size()
#define ff first
#define ss second
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define ee6 (ll)1000001
#define ee5 (ll)100001
#define trav(a, v) for (auto &a : v)
#define tt \
ll t; \
cin >> t; \
while (t--)
typedef long long int ll;
typedef pair<ll, ll> pr;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef vector<pr> vpr;
typedef vector<vpr> vvpr;
typedef vector<vi> vvi;
//*X.find_by_order(2) element at index=2
// X.order_of_key(1) how many elements strictly less than 1
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#ifdef debug
#define dbg(...) \
{ \
cerr << "[ "; \
dump(#__VA_ARGS__, __VA_ARGS__); \
}
#undef endl
template <typename Arg1, typename Arg2>
ostream &operator<<(ostream &out, const pair<Arg1, Arg2> &x) {
return out << "(" << x.ff << "," << x.ss << ")";
}
template <typename Arg1>
ostream &operator<<(ostream &out, const vector<Arg1> &a) {
out << "[";
for (const auto &x : a)
out << x << ",";
return out << "]";
}
template <typename Arg1> ostream &operator<<(ostream &out, const set<Arg1> &a) {
out << "[";
for (const auto &x : a)
out << x << ",";
return out << "]";
}
template <typename Arg1, typename Arg2>
ostream &operator<<(ostream &out, const map<Arg1, Arg2> &a) {
out << "[";
for (const auto &x : a)
out << x << ",";
return out << "]";
}
template <typename Arg1, typename Arg2>
ostream &operator<<(ostream &out, const unordered_map<Arg1, Arg2> &a) {
out << "[";
for (const auto &x : a)
out << x << ",";
return out << "]";
}
template <typename Arg1> void dump(const string name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << " ] " << endl;
}
template <typename Arg1, typename... Args>
void dump(const string names, Arg1 &&arg1, Args &&...args) {
const string name = names.substr(0, names.find(','));
cerr << name << " : " << arg1 << " | ";
dump(names.substr(1 + (int)name.size()), args...);
}
#else
#define dbg(args...)
#endif
ll powmod(ll x, ll y) {
ll res = 1;
x = x % MOD;
while (y > 0) {
if (y & 1)
res = (res * x) % MOD;
y = y >> 1; // y = y/2
x = (x * x) % MOD;
}
return res;
}
ll possible[ee5];
ll we[101], v[101];
int main() {
#if !ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n, W;
cin >> n >> W;
rep(i, n) { cin >> we[i + 1] >> v[i + 1]; }
ll maxi = 0;
for (ll i = 1; i <= n; i++) {
for (ll w = W; w >= we[i]; w--) {
possible[w] = max(possible[w], possible[w - we[i]] + v[i]);
maxi = max(maxi, possible[w]);
}
}
cout << maxi;
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#if !ONLINE_JUDGE
#define debug
#endif
using namespace std;
/******* All Required define Pre-Processors and typedef Constants *******/
#define mem(a, b) memset(a, (b), sizeof(a))
#define repd(i, k) for (int i = k; i >= 0; i--)
#define rep(i, k) for (int i = 0; i < k; i++)
#define repn(i, k1, k2) for (ll i = k1; i < k2; i++)
#define sz(x) (ll)(x).size()
#define ff first
#define ss second
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define ee6 (ll)1000001
#define ee5 (ll)100001
#define trav(a, v) for (auto &a : v)
#define tt \
ll t; \
cin >> t; \
while (t--)
typedef long long int ll;
typedef pair<ll, ll> pr;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef vector<pr> vpr;
typedef vector<vpr> vvpr;
typedef vector<vi> vvi;
//*X.find_by_order(2) element at index=2
// X.order_of_key(1) how many elements strictly less than 1
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#ifdef debug
#define dbg(...) \
{ \
cerr << "[ "; \
dump(#__VA_ARGS__, __VA_ARGS__); \
}
#undef endl
template <typename Arg1, typename Arg2>
ostream &operator<<(ostream &out, const pair<Arg1, Arg2> &x) {
return out << "(" << x.ff << "," << x.ss << ")";
}
template <typename Arg1>
ostream &operator<<(ostream &out, const vector<Arg1> &a) {
out << "[";
for (const auto &x : a)
out << x << ",";
return out << "]";
}
template <typename Arg1> ostream &operator<<(ostream &out, const set<Arg1> &a) {
out << "[";
for (const auto &x : a)
out << x << ",";
return out << "]";
}
template <typename Arg1, typename Arg2>
ostream &operator<<(ostream &out, const map<Arg1, Arg2> &a) {
out << "[";
for (const auto &x : a)
out << x << ",";
return out << "]";
}
template <typename Arg1, typename Arg2>
ostream &operator<<(ostream &out, const unordered_map<Arg1, Arg2> &a) {
out << "[";
for (const auto &x : a)
out << x << ",";
return out << "]";
}
template <typename Arg1> void dump(const string name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << " ] " << endl;
}
template <typename Arg1, typename... Args>
void dump(const string names, Arg1 &&arg1, Args &&...args) {
const string name = names.substr(0, names.find(','));
cerr << name << " : " << arg1 << " | ";
dump(names.substr(1 + (int)name.size()), args...);
}
#else
#define dbg(args...)
#endif
ll powmod(ll x, ll y) {
ll res = 1;
x = x % MOD;
while (y > 0) {
if (y & 1)
res = (res * x) % MOD;
y = y >> 1; // y = y/2
x = (x * x) % MOD;
}
return res;
}
ll possible[ee5];
ll we[101], v[101];
int main() {
#if !ONLINE_JUDGE
// freopen("in.txt","r",stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n, W;
cin >> n >> W;
rep(i, n) { cin >> we[i + 1] >> v[i + 1]; }
ll maxi = 0;
for (ll i = 1; i <= n; i++) {
for (ll w = W; w >= we[i]; w--) {
possible[w] = max(possible[w], possible[w - we[i]] + v[i]);
maxi = max(maxi, possible[w]);
}
}
cout << maxi;
}
|
replace
| 128 | 129 | 128 | 129 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
// --------------------<optimizations>--------------------
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
// -------------------</optimizations>--------------------
#include <bits/stdc++.h>
#define ll long long
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp> //lower_bound , upper_bound
// using namespace __gnu_pbds;//kth smallest : *pd.find_by_order(k-1) , no. of
// elem < X : pd.order_of_key(X) , delete X : pd.erase(X). typedef tree<ll,
// null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fb(i, a, b) for (ll i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define nl '\n'
#define pl pair<ll, ll>
#define all(v) v.begin(), v.end()
const ll mod = (1e+9) + 7;
const ll sz = 2e5 + 9;
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll power(ll a, ll b, ll m = mod) {
ll res = 1;
while (b > 0) {
if (b & 1)
res = (res * a) % m;
b = b >> 1;
a = (a * a) % m;
}
return res;
}
void solve() {
ll tt = 1, n;
cin >> tt;
while (tt--) {
ll cap;
cin >> n >> cap;
vector<ll> wt(n), val(n);
f(i, 0, n) { cin >> wt[i] >> val[i]; }
ll ans[n + 1][cap + 1];
f(i, 0, n + 1) {
f(w, 0, cap + 1) {
if (i == 0 || w == 0)
ans[i][w] = 0;
else if (wt[i - 1] <= w)
ans[i][w] =
max(val[i - 1] + ans[i - 1][w - wt[i - 1]], ans[i - 1][w]);
else
ans[i][w] = ans[i - 1][w];
}
}
cout << ans[n][cap] << nl;
}
}
int main() {
#ifdef JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
clock_t beg = clock();
solve();
clock_t end = clock();
fprintf(stderr, "%.3f sec, Copyright %c 2019 PyThor. \n",
double(end - beg) / CLOCKS_PER_SEC, 184);
return 0;
}
|
// --------------------<optimizations>--------------------
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
// -------------------</optimizations>--------------------
#include <bits/stdc++.h>
#define ll long long
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp> //lower_bound , upper_bound
// using namespace __gnu_pbds;//kth smallest : *pd.find_by_order(k-1) , no. of
// elem < X : pd.order_of_key(X) , delete X : pd.erase(X). typedef tree<ll,
// null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fb(i, a, b) for (ll i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define nl '\n'
#define pl pair<ll, ll>
#define all(v) v.begin(), v.end()
const ll mod = (1e+9) + 7;
const ll sz = 2e5 + 9;
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll power(ll a, ll b, ll m = mod) {
ll res = 1;
while (b > 0) {
if (b & 1)
res = (res * a) % m;
b = b >> 1;
a = (a * a) % m;
}
return res;
}
void solve() {
ll tt = 1, n;
// cin>>tt;
while (tt--) {
ll cap;
cin >> n >> cap;
vector<ll> wt(n), val(n);
f(i, 0, n) { cin >> wt[i] >> val[i]; }
ll ans[n + 1][cap + 1];
f(i, 0, n + 1) {
f(w, 0, cap + 1) {
if (i == 0 || w == 0)
ans[i][w] = 0;
else if (wt[i - 1] <= w)
ans[i][w] =
max(val[i - 1] + ans[i - 1][w - wt[i - 1]], ans[i - 1][w]);
else
ans[i][w] = ans[i - 1][w];
}
}
cout << ans[n][cap] << nl;
}
}
int main() {
#ifdef JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
clock_t beg = clock();
solve();
clock_t end = clock();
fprintf(stderr, "%.3f sec, Copyright %c 2019 PyThor. \n",
double(end - beg) / CLOCKS_PER_SEC, 184);
return 0;
}
|
replace
| 42 | 43 | 42 | 43 |
0
|
0.000 sec, Copyright 2019 PyThor.
|
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
#define maxW 100010
#define maxN 102
typedef long long ll;
int n;
ll s;
ll cache[maxN][maxW], v[maxN], w[maxN];
ll rec(int i, ll W) {
if (i < 0 || W < 0)
return 0;
if (cache[i][W] != -1)
return cache[i][W];
if (w[i] > W)
return cache[W][i] = rec(i - 1, W);
return cache[i][W] = max(rec(i - 1, W), v[i] + rec(i - 1, W - w[i]));
}
int main() {
cin >> n >> s;
for (int i = 0; i < n; i++) {
cin >> w[i] >> v[i];
for (int j = 0; j < maxW; j++) {
cache[i][j] = -1;
}
}
cout << rec(n - 1, s) << endl;
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
#define maxW 100010
#define maxN 102
typedef long long ll;
int n;
ll s;
ll cache[maxN][maxW], v[maxN], w[maxN];
ll rec(int i, ll W) {
if (i < 0 || W < 0)
return 0;
if (cache[i][W] != -1)
return cache[i][W];
if (w[i] > W)
return cache[i][W] = rec(i - 1, W);
return cache[i][W] = max(rec(i - 1, W), v[i] + rec(i - 1, W - w[i]));
}
int main() {
cin >> n >> s;
for (int i = 0; i < n; i++) {
cin >> w[i] >> v[i];
for (int j = 0; j < maxW; j++) {
cache[i][j] = -1;
}
}
cout << rec(n - 1, s) << endl;
return 0;
}
|
replace
| 21 | 22 | 21 | 22 |
0
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define D(a) cerr << #a << " = " << a << endl
#else
#define D(a)
#define cerr false && cerr
#endif
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0)
#define dforsn(i, s, n) for (int i = int(n - 1); i >= int(s); i--)
#define forsn(i, s, n) for (int i = int(s); i < int(n); i++)
#define dforn(i, n) dforsn(i, 0, n)
#define forn(i, n) forsn(i, 0, n)
#define all(a) a.begin(), a.end()
#define si(a) int((a).size())
#define pb emplace_back
#define mp make_pair
#define snd second
#define fst first
#define endl '\n'
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;
const int N = 100, W = 1e5 + 1;
int w[N], v[N];
ll t[N][W];
ll dp(int i, int c) {
if (t[i][c])
return t[i][c];
if (i == 0)
t[i][c] = c >= w[i] ? v[i] : 0;
else
t[i][c] = max(dp(i - 1, c),
(c >= w[i] ? dp(i - 1, c - w[i]) + v[i] : dp(i - 1, c)));
return t[i][c];
}
int main() {
fastio;
int n, m;
cin >> n >> m;
forn(i, n) {
int x, y;
cin >> x >> y;
w[i] = x, v[i] = y;
}
cout << dp(n - 1, m) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define D(a) cerr << #a << " = " << a << endl
#else
#define D(a)
#define cerr false && cerr
#endif
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0)
#define dforsn(i, s, n) for (int i = int(n - 1); i >= int(s); i--)
#define forsn(i, s, n) for (int i = int(s); i < int(n); i++)
#define dforn(i, n) dforsn(i, 0, n)
#define forn(i, n) forsn(i, 0, n)
#define all(a) a.begin(), a.end()
#define si(a) int((a).size())
#define pb emplace_back
#define mp make_pair
#define snd second
#define fst first
#define endl '\n'
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;
const int N = 100, W = 1e5 + 1;
int w[N], v[N];
ll t[N][W];
ll dp(int i, int c) {
if (t[i][c])
return t[i][c];
if (i == 0)
t[i][c] = c >= w[i] ? v[i] : 0;
else {
ll x = dp(i - 1, c);
t[i][c] = max(x, (c >= w[i] ? dp(i - 1, c - w[i]) + v[i] : x));
}
return t[i][c];
}
int main() {
fastio;
int n, m;
cin >> n >> m;
forn(i, n) {
int x, y;
cin >> x >> y;
w[i] = x, v[i] = y;
}
cout << dp(n - 1, m) << endl;
return 0;
}
|
replace
| 36 | 39 | 36 | 40 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX_WEIGHTS = 1000005;
ll w[105], v[105], dp[MAX_WEIGHTS];
int N, W;
// ll weights[MAX_WEIGHTS];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> W;
for (int i = 0; i < N; i++) {
cin >> w[i] >> v[i];
// dp[w[i]] = v[i];
}
ll ans = 0;
for (int i = 0; i < N; i++) {
for (int x = MAX_WEIGHTS - 2; x >= 0; x--) {
if (dp[x] || x == 0)
dp[x + w[i]] = max(dp[x + w[i]], dp[x] + v[i]);
}
}
for (int i = 1; i <= W; i++)
ans = max(ans, dp[i]);
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX_WEIGHTS = 10000005;
ll w[105], v[105], dp[MAX_WEIGHTS];
int N, W;
// ll weights[MAX_WEIGHTS];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> W;
for (int i = 0; i < N; i++) {
cin >> w[i] >> v[i];
// dp[w[i]] = v[i];
}
ll ans = 0;
for (int i = 0; i < N; i++) {
for (int x = MAX_WEIGHTS - 2; x >= 0; x--) {
if (dp[x] || x == 0)
dp[x + w[i]] = max(dp[x + w[i]], dp[x] + v[i]);
}
}
for (int i = 1; i <= W; i++)
ans = max(ans, dp[i]);
cout << ans << "\n";
return 0;
}
|
replace
| 4 | 5 | 4 | 5 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
unsigned long long int dp[100][100000];
unsigned long long int knapsack(unsigned long long int *wt,
unsigned long long int *val,
unsigned long long int w,
unsigned long long int n) {
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= w; j++) {
if (i == 0 || j == 0) {
dp[i][j] = 0;
} else {
if (wt[i - 1] > j) {
dp[i][j] = dp[i - 1][j];
} else {
dp[i][j] = max(val[i - 1] + dp[i - 1][j - wt[i - 1]], dp[i - 1][j]);
}
}
}
}
return dp[n][w];
}
int main() {
int n, w;
cin >> n;
cin >> w;
unsigned long long int wt[n];
unsigned long long int val[n];
for (int i = 0; i < n; i++) {
cin >> wt[i];
cin >> val[i];
}
cout << knapsack(wt, val, w, n) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
unsigned long long int dp[105][100005];
unsigned long long int knapsack(unsigned long long int *wt,
unsigned long long int *val,
unsigned long long int w,
unsigned long long int n) {
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= w; j++) {
if (i == 0 || j == 0) {
dp[i][j] = 0;
} else {
if (wt[i - 1] > j) {
dp[i][j] = dp[i - 1][j];
} else {
dp[i][j] = max(val[i - 1] + dp[i - 1][j - wt[i - 1]], dp[i - 1][j]);
}
}
}
}
return dp[n][w];
}
int main() {
int n, w;
cin >> n;
cin >> w;
unsigned long long int wt[n];
unsigned long long int val[n];
for (int i = 0; i < n; i++) {
cin >> wt[i];
cin >> val[i];
}
cout << knapsack(wt, val, w, n) << endl;
return 0;
}
|
replace
| 2 | 3 | 2 | 3 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
template <class T, class U> void ckmin(T &a, U b) {
if (a > b)
a = b;
}
template <class T, class U> void ckmax(T &a, U b) {
if (a < b)
a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a)-1; i >= (b); i--)
#define SZ(x) ((int)((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define INF 1000000007
#define LLINF 2696969696969696969ll
#define MAXN 113
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
int N, W;
ll dp[MAXN];
ll ans;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N >> W;
FOR(i, 0, N) {
int x, y;
cin >> x >> y;
FORD(j, W + 1, 0) {
if (j + x > W)
continue;
ckmax(dp[j + x], dp[j] + y);
}
}
FORD(j, W + 1, 0) { ckmax(ans, dp[j]); }
cout << ans << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T, class U> void ckmin(T &a, U b) {
if (a > b)
a = b;
}
template <class T, class U> void ckmax(T &a, U b) {
if (a < b)
a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a)-1; i >= (b); i--)
#define SZ(x) ((int)((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define INF 1000000007
#define LLINF 2696969696969696969ll
#define MAXN 100013
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
int N, W;
ll dp[MAXN];
ll ans;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N >> W;
FOR(i, 0, N) {
int x, y;
cin >> x >> y;
FORD(j, W + 1, 0) {
if (j + x > W)
continue;
ckmax(dp[j + x], dp[j] + y);
}
}
FORD(j, W + 1, 0) { ckmax(ans, dp[j]); }
cout << ans << '\n';
}
|
replace
| 25 | 26 | 25 | 26 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
long long weights[101];
long long values[101];
long long dp[100][100001];
int main() {
int n;
long long W;
cin >> n >> W;
for (int i = 0; i < n; i++) {
cin >> weights[i] >> values[i];
}
for (int i = 0; i < n; i++) {
for (long long w = 0; w <= W; w++) {
if (w >= weights[i]) {
dp[i + 1][w] = max(dp[i][w - weights[i]] + values[i], dp[i][w]);
} else {
dp[i + 1][w] = dp[i][w];
}
}
}
cout << dp[n][W];
}
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
long long weights[101];
long long values[101];
long long dp[101][100001];
int main() {
int n;
long long W;
cin >> n >> W;
for (int i = 0; i < n; i++) {
cin >> weights[i] >> values[i];
}
for (int i = 0; i < n; i++) {
for (long long w = 0; w <= W; w++) {
if (w >= weights[i]) {
dp[i + 1][w] = max(dp[i][w - weights[i]] + values[i], dp[i][w]);
} else {
dp[i + 1][w] = dp[i][w];
}
}
}
cout << dp[n][W];
}
|
replace
| 7 | 8 | 7 | 8 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
// #define ll long long int;
vector<pair<long long int, long long int>> v;
long long int dp[105][10005];
long long int maxi(long long int w, long long int i, long long int n) {
if (i == n)
return 0;
if (w - v[i].first >= 0) {
if (dp[i][w] == -1)
return dp[i][w] = max(v[i].second + maxi(w - v[i].first, i + 1, n),
maxi(w, i + 1, n));
else
return dp[i][w];
} else {
if (dp[i + 1][w] == -1)
return dp[i + 1][w] = maxi(w, i + 1, n);
else
return dp[i + 1][w];
}
}
int main() {
memset(dp, -1, sizeof(dp));
long long int n, w, x, y;
cin >> n >> w;
for (int i = 0; i < n; i++) {
cin >> x >> y;
v.push_back({x, y});
}
cout << maxi(w, 0, n);
}
|
#include <bits/stdc++.h>
using namespace std;
// #define ll long long int;
vector<pair<long long int, long long int>> v;
long long int dp[105][100005];
long long int maxi(long long int w, long long int i, long long int n) {
if (i == n)
return 0;
if (w - v[i].first >= 0) {
if (dp[i][w] == -1)
return dp[i][w] = max(v[i].second + maxi(w - v[i].first, i + 1, n),
maxi(w, i + 1, n));
else
return dp[i][w];
} else {
if (dp[i + 1][w] == -1)
return dp[i + 1][w] = maxi(w, i + 1, n);
else
return dp[i + 1][w];
}
}
int main() {
memset(dp, -1, sizeof(dp));
long long int n, w, x, y;
cin >> n >> w;
for (int i = 0; i < n; i++) {
cin >> x >> y;
v.push_back({x, y});
}
cout << maxi(w, 0, n);
}
|
replace
| 5 | 6 | 5 | 6 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef string S;
typedef queue<int> qi;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define ft first
#define sd second
#define elif else if
#define unique(a) a.erase(unique(a.begin(), a.end()), a.end())
#define mp make_pair
#define fr(i, n) for (ll i = 0; i < (n); i++)
#define ifr(i, n) for (ll i = (n)-1; i >= 0; i--)
const int MOD = 1e9 + 7;
const ll INF = 1e18;
int main() {
ll n, W;
cin >> n >> W;
vl weight(n), value(n);
fr(i, n) cin >> weight[i] >> value[i];
ll dp[110][100010] = {0};
fr(i, n) {
fr(sum_w, W + 1) {
dp[i + 1][sum_w] = max(dp[i][sum_w], dp[i][sum_w - weight[i]] + value[i]);
}
}
cout << dp[n][W] << endl;
}
|
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef string S;
typedef queue<int> qi;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define ft first
#define sd second
#define elif else if
#define unique(a) a.erase(unique(a.begin(), a.end()), a.end())
#define mp make_pair
#define fr(i, n) for (ll i = 0; i < (n); i++)
#define ifr(i, n) for (ll i = (n)-1; i >= 0; i--)
const int MOD = 1e9 + 7;
const ll INF = 1e18;
int main() {
ll n, W;
cin >> n >> W;
vl weight(n), value(n);
fr(i, n) cin >> weight[i] >> value[i];
ll dp[110][100010] = {0};
fr(i, n) {
fr(sum_w, W + 1) {
if (sum_w >= weight[i])
dp[i + 1][sum_w] =
max(dp[i][sum_w], dp[i][sum_w - weight[i]] + value[i]);
else
dp[i + 1][sum_w] = dp[i][sum_w];
}
}
cout << dp[n][W] << endl;
}
|
replace
| 50 | 51 | 50 | 55 |
-11
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
#define FI freopen("inp.txt", "r", stdin)
#define FO freopen("out.txt", "w", stdout)
#define FOR(v) \
for (int i : v) { \
printf("%d ", i); \
} \
printf("\n");
#define what_is(x) cerr << #x << " = " << x << endl;
#define error(args...) \
{ \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
}
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
/* --------------------Code Bellow--------------------- */
long long W[105], V[105], mem[105][100005];
const int INF = 1e9 + 5;
long long knapsack(int i, int sack) {
if (sack < 0)
return -INF;
if (i < 0)
return 0;
long long ans1 = knapsack(i - 1, sack);
long long ans2 = V[i] + knapsack(i - 1, sack - W[i]);
return mem[i][sack] = max(ans1, ans2);
}
int main() {
// FI;
memset(mem, -1, sizeof(mem));
int n, sack;
scanf("%d %d", &n, &sack);
for (int i = 0; i < n; ++i) {
scanf("%lld %lld", &W[i], &V[i]);
}
printf("%lld\n", knapsack(n - 1, sack));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define FI freopen("inp.txt", "r", stdin)
#define FO freopen("out.txt", "w", stdout)
#define FOR(v) \
for (int i : v) { \
printf("%d ", i); \
} \
printf("\n");
#define what_is(x) cerr << #x << " = " << x << endl;
#define error(args...) \
{ \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
}
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
/* --------------------Code Bellow--------------------- */
long long W[105], V[105], mem[105][100005];
const int INF = 1e9 + 5;
long long knapsack(int i, int sack) {
if (sack < 0)
return -INF;
if (i < 0)
return 0;
if (mem[i][sack] != -1)
return mem[i][sack];
long long ans1 = knapsack(i - 1, sack);
long long ans2 = V[i] + knapsack(i - 1, sack - W[i]);
return mem[i][sack] = max(ans1, ans2);
}
int main() {
// FI;
memset(mem, -1, sizeof(mem));
int n, sack;
scanf("%d %d", &n, &sack);
for (int i = 0; i < n; ++i) {
scanf("%lld %lld", &W[i], &V[i]);
}
printf("%lld\n", knapsack(n - 1, sack));
return 0;
}
|
insert
| 37 | 37 | 37 | 40 |
TLE
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define ll long long
int n, w;
pii arr[101];
ll dp[101][100005];
ll knap(int i, int n, int sm) {
if (i == n)
return 0;
if (dp[i][sm] != -1)
return dp[i][sm];
ll ans = 0;
if (sm + arr[i].first <= w) {
ans = max(ans, arr[i].second + knap(i + 1, n, sm + arr[i].first));
ans = max(ans, knap(i + 1, n, sm));
} else {
ans = max(ans, knap(i + 1, n, sm));
}
return ans;
}
int main(int argc, char const *argv[]) {
cin >> n >> w;
for (int i = 0; i < n; ++i) {
cin >> arr[i].first >> arr[i].second;
}
memset(dp, -1, sizeof(dp));
cout << knap(0, n, 0);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define ll long long
int n, w;
pii arr[101];
ll dp[101][100005];
ll knap(int i, int n, int sm) {
if (i == n)
return 0;
if (dp[i][sm] != -1)
return dp[i][sm];
ll ans = 0;
if (sm + arr[i].first <= w) {
ans = max(ans, arr[i].second + knap(i + 1, n, sm + arr[i].first));
ans = max(ans, knap(i + 1, n, sm));
} else {
ans = max(ans, knap(i + 1, n, sm));
}
return dp[i][sm] = ans;
}
int main(int argc, char const *argv[]) {
cin >> n >> w;
for (int i = 0; i < n; ++i) {
cin >> arr[i].first >> arr[i].second;
}
memset(dp, -1, sizeof(dp));
cout << knap(0, n, 0);
return 0;
}
|
replace
| 21 | 22 | 21 | 22 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
long long dp[100][100010], v[110], w[110];
int main() {
long long n, W;
cin >> n >> W;
for (int i = 0; i < n; i++)
cin >> w[i] >> v[i];
for (int i = n - 1; i >= 0; i--)
for (int curw = 0; curw <= W; curw++)
dp[i][curw] =
max(dp[i + 1][curw],
(curw + w[i] <= W ? dp[i + 1][curw + w[i]] + v[i] : -1));
cout << dp[0][0] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long dp[110][100010], v[110], w[110];
int main() {
long long n, W;
cin >> n >> W;
for (int i = 0; i < n; i++)
cin >> w[i] >> v[i];
for (int i = n - 1; i >= 0; i--)
for (int curw = 0; curw <= W; curw++)
dp[i][curw] =
max(dp[i + 1][curw],
(curw + w[i] <= W ? dp[i + 1][curw + w[i]] + v[i] : -1));
cout << dp[0][0] << endl;
return 0;
}
|
replace
| 3 | 4 | 3 | 4 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
// using INF = INT_MAX / 2;
#define DEBUG
#ifdef DEBUG
#define dump(x) cout << "[*] " #x ": " << x << endl
#define debug(x) x
#else
#define dump(x)
#define debug(x)
#endif
// dp[w][i] := w の重さの時 i 個の荷物を入れた時の価値の総和
long long dp[100][100005];
int main() {
int N, W;
cin >> N >> W;
vector<int> w(N);
vector<int> 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) {
dp[i + 1][j] =
max(dp[i][j], j - w[i] >= 0 ? dp[i][j - w[i]] + v[i] : dp[i][j]);
}
}
long long int ans = 0;
for (int i = 0; i <= W; ++i) {
ans = max(ans, dp[N][i]);
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
// using INF = INT_MAX / 2;
#define DEBUG
#ifdef DEBUG
#define dump(x) cout << "[*] " #x ": " << x << endl
#define debug(x) x
#else
#define dump(x)
#define debug(x)
#endif
// dp[w][i] := w の重さの時 i 個の荷物を入れた時の価値の総和
long long dp[105][100005];
int main() {
int N, W;
cin >> N >> W;
vector<int> w(N);
vector<int> 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) {
dp[i + 1][j] =
max(dp[i][j], j - w[i] >= 0 ? dp[i][j - w[i]] + v[i] : dp[i][j]);
}
}
long long int ans = 0;
for (int i = 0; i <= W; ++i) {
ans = max(ans, dp[N][i]);
}
cout << ans << endl;
return 0;
}
|
replace
| 15 | 16 | 15 | 16 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
void setIO() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
// freopen("solution.out", "w", stdout);
freopen("1.in", "r", stdin);
#endif
}
const int maxn = 100, maxw = 1e5 + 1;
const long long inf = 1e18;
long long dp[maxn][maxw];
int main() {
setIO();
int n, w;
cin >> n >> w;
vector<pair<int, int>> item(n); // first = weight, second = value
for (int i = 0; i < n; ++i) {
cin >> item[i].first >> item[i].second;
for (int j = 0; j < w + 1; ++j) {
dp[i][j] = -inf;
}
}
dp[0][item[0].first] = item[0].second;
long long sol = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j <= w; ++j) {
if (item[i].first <= j) {
dp[i][j] = max(dp[i][j], (long long)item[i].second);
}
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
if (j + item[i + 1].first <= w) {
dp[i + 1][j + item[i + 1].first] = max(
{dp[i + 1][j + item[i + 1].first], dp[i][j] + item[i + 1].second});
}
if (i == n - 1) {
sol = max(sol, dp[i][j]);
}
}
}
cout << sol << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void setIO() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
// freopen("solution.out", "w", stdout);
freopen("1.in", "r", stdin);
#endif
}
const int maxn = 101, maxw = 1e5 + 1;
const long long inf = 1e18;
long long dp[maxn][maxw];
int main() {
setIO();
int n, w;
cin >> n >> w;
vector<pair<int, int>> item(n); // first = weight, second = value
for (int i = 0; i < n; ++i) {
cin >> item[i].first >> item[i].second;
for (int j = 0; j < w + 1; ++j) {
dp[i][j] = -inf;
}
}
dp[0][item[0].first] = item[0].second;
long long sol = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j <= w; ++j) {
if (item[i].first <= j) {
dp[i][j] = max(dp[i][j], (long long)item[i].second);
}
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
if (j + item[i + 1].first <= w) {
dp[i + 1][j + item[i + 1].first] = max(
{dp[i + 1][j + item[i + 1].first], dp[i][j] + item[i + 1].second});
}
if (i == n - 1) {
sol = max(sol, dp[i][j]);
}
}
}
cout << sol << "\n";
return 0;
}
|
replace
| 13 | 14 | 13 | 14 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define int long long
#define vin vector<int>
#define vcn vector<char>
#define ver pair<int, int>
#define pb push_back
#define ld long double
#define x first
#define y second
#define blush \
fflush(stdout); \
fflush(stdin);
#define $(a) (int)a.size()
#define scanf ;
#define printf ;
using namespace std;
void i_love_misaka_mikoto();
int32_t main() {
#ifdef RNS
freopen("input.txt", "r", stdin);
#ifndef DBG
freopen("output.txt", "w", stdout);
#endif
#else
ios_base::sync_with_stdio(false);
#endif
i_love_misaka_mikoto();
#ifdef RNS
cout << '\n' << (ld)clock() / CLOCKS_PER_SEC << '\n';
#endif
return 0;
}
int rz[(int)1e5 + 10];
void i_love_misaka_mikoto() {
int n, w;
cin >> n >> w;
vector<ver> zn(n);
for (int i = 0; i < n; i++) {
cin >> zn[i].x >> zn[i].y;
}
for (int i = 0; i < n; i++) {
for (int j = 1e5 + 9; j >= 0; j--) {
if (rz[j] || j == 0) {
rz[j + zn[i].x] = max(rz[j + zn[i].x], rz[j] + zn[i].y);
}
}
}
int mx = 0;
for (int i = 0; i <= w; i++) {
if (rz[mx] <= rz[i])
mx = i;
}
cout << rz[mx];
}
|
#include <bits/stdc++.h>
#define int long long
#define vin vector<int>
#define vcn vector<char>
#define ver pair<int, int>
#define pb push_back
#define ld long double
#define x first
#define y second
#define blush \
fflush(stdout); \
fflush(stdin);
#define $(a) (int)a.size()
#define scanf ;
#define printf ;
using namespace std;
void i_love_misaka_mikoto();
int32_t main() {
#ifdef RNS
freopen("input.txt", "r", stdin);
#ifndef DBG
freopen("output.txt", "w", stdout);
#endif
#else
ios_base::sync_with_stdio(false);
#endif
i_love_misaka_mikoto();
#ifdef RNS
cout << '\n' << (ld)clock() / CLOCKS_PER_SEC << '\n';
#endif
return 0;
}
int rz[(int)1e6 + 10];
void i_love_misaka_mikoto() {
int n, w;
cin >> n >> w;
vector<ver> zn(n);
for (int i = 0; i < n; i++) {
cin >> zn[i].x >> zn[i].y;
}
for (int i = 0; i < n; i++) {
for (int j = 1e5 + 9; j >= 0; j--) {
if (rz[j] || j == 0) {
rz[j + zn[i].x] = max(rz[j + zn[i].x], rz[j] + zn[i].y);
}
}
}
int mx = 0;
for (int i = 0; i <= w; i++) {
if (rz[mx] <= rz[i])
mx = i;
}
cout << rz[mx];
}
|
replace
| 32 | 33 | 32 | 33 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
long long N, W;
long long w[105], u[105];
long long dp[105][105];
long long ans = 0;
int main() {
scanf("%d%d", &N, &W);
for (int i = 1; i <= N; i++)
scanf("%d%d", &w[i], &u[i]);
for (int i = 1; i <= W; i++)
for (int j = 1; j <= N; j++)
if (i >= w[j])
dp[i][j] = max(dp[i][j - 1], dp[i - w[j]][j - 1] + u[j]);
else
dp[i][j] = max(dp[i][j - 1], dp[i][j]);
for (int i = 1; i <= N; i++)
ans = max(ans, dp[W][i]);
cout << ans;
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
long long N, W;
long long w[105], u[105];
long long dp[100005][105];
long long ans = 0;
int main() {
scanf("%d%d", &N, &W);
for (int i = 1; i <= N; i++)
scanf("%d%d", &w[i], &u[i]);
for (int i = 1; i <= W; i++)
for (int j = 1; j <= N; j++)
if (i >= w[j])
dp[i][j] = max(dp[i][j - 1], dp[i - w[j]][j - 1] + u[j]);
else
dp[i][j] = max(dp[i][j - 1], dp[i][j]);
for (int i = 1; i <= N; i++)
ans = max(ans, dp[W][i]);
cout << ans;
return 0;
}
|
replace
| 12 | 13 | 12 | 13 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define ll long long int
#define ri register int
#define endl "\n"
using namespace std;
ll dp[102][(int)1e5 + 2] = {};
int main() {
ios_base::sync_with_stdio(false);
ll n, w;
cin >> n >> w;
vector<pair<ll, ll>> items(n + 1);
for (ri i = 1; i <= n; i++)
cin >> items[i].first >> items[i].second;
for (ri i = 0; i <= n; i++) {
for (ri j = 0; j <= w; j++) {
if (j - items[i].first >= 0) {
dp[i][j] =
max(dp[i - 1][j - items[i].first] + items[i].second, dp[i - 1][j]);
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
cout << dp[n][w] << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long int
#define ri register int
#define endl "\n"
using namespace std;
ll dp[102][(int)1e5 + 2] = {};
int main() {
ios_base::sync_with_stdio(false);
ll n, w;
cin >> n >> w;
vector<pair<ll, ll>> items(n + 1);
for (ri i = 1; i <= n; i++)
cin >> items[i].first >> items[i].second;
for (ri i = 1; i <= n; i++) {
for (ri j = 0; j <= w; j++) {
if (j - items[i].first >= 0) {
dp[i][j] =
max(dp[i - 1][j - items[i].first] + items[i].second, dp[i - 1][j]);
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
cout << dp[n][w] << endl;
return 0;
}
|
replace
| 19 | 20 | 19 | 20 |
-11
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <iostream>
#define mp make_pair
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<int, LL> pil;
const int MAXN = 105;
const int MAXW = 1e5 + 7;
template <typename T> inline T read() {
T res = 0, flag = 1;
char in = getchar();
while (!isdigit(in)) {
if (in == '-')
flag = -1;
in = getchar();
}
while (isdigit(in)) {
res = (res << 1) + (res << 3) + in - '0';
in = getchar();
}
return res * flag;
}
template <typename T> inline void chkmax(T &a, T b) {
if (a < b)
a = b;
}
template <typename T> inline void chkmin(T &a, T b) {
if (a > b)
a = b;
}
int N, W;
int w[MAXN], val[MAXN];
LL dp[MAXN][MAXW], ans;
inline void solve() {
N = read<int>();
W = read<int>();
for (int i = 1; i <= N; ++i) {
w[i] = read<int>();
val[i] = read<int>();
}
for (int i = 1; i <= N; ++i) {
// for(int j = 0; j < w[i]; ++j) dp[i][j] = dp[i - 1][j];
for (int j = 0; j <= W; ++j) {
if (j >= w[i])
chkmax(dp[i][j], dp[i - 1][j - w[i]] + val[i]);
chkmax(dp[i][j], dp[i - 1][j]);
// printf("%d %d %d\n", i, j, dp[i][j]);
}
}
for (int i = 0; i <= W; ++i)
chkmax(ans, dp[N][i]);
printf("%lld\n", ans);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("D.in", "r", stdin);
freopen("D.out", "w", stdout);
#endif
solve();
return 0;
}
|
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <iostream>
#define mp make_pair
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<int, LL> pil;
const int MAXN = 105;
const int MAXW = 1e5 + 7;
template <typename T> inline T read() {
T res = 0, flag = 1;
char in = getchar();
while (!isdigit(in)) {
if (in == '-')
flag = -1;
in = getchar();
}
while (isdigit(in)) {
res = (res << 1) + (res << 3) + in - '0';
in = getchar();
}
return res * flag;
}
template <typename T> inline void chkmax(T &a, T b) {
if (a < b)
a = b;
}
template <typename T> inline void chkmin(T &a, T b) {
if (a > b)
a = b;
}
int N, W;
int w[MAXN], val[MAXN];
LL dp[MAXN][MAXW], ans;
inline void solve() {
N = read<int>();
W = read<int>();
for (int i = 1; i <= N; ++i) {
w[i] = read<int>();
val[i] = read<int>();
}
for (int i = 1; i <= N; ++i) {
// for(int j = 0; j < w[i]; ++j) dp[i][j] = dp[i - 1][j];
for (int j = 0; j <= W; ++j) {
if (j >= w[i])
chkmax(dp[i][j], dp[i - 1][j - w[i]] + val[i]);
chkmax(dp[i][j], dp[i - 1][j]);
// printf("%d %d %d\n", i, j, dp[i][j]);
}
}
for (int i = 0; i <= W; ++i)
chkmax(ans, dp[N][i]);
printf("%lld\n", ans);
}
int main() {
solve();
return 0;
}
|
delete
| 66 | 70 | 66 | 66 |
TLE
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include "bits/stdc++.h"
using namespace std;
const long long inf = 1e18;
#define ll long long
#define fr(i, a, n) for (int i = a; i < n; i++)
#define rfr(i, n, a) for (int i = n; i >= a; i--)
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define endl "\n"
typedef vector<int> VI;
typedef vector<ll> VL;
ll n, m, k;
ll dp[101][100001];
vector<pair<int, int>> in;
ll solv(int ind, ll curw) {
if (curw > k) {
return -inf;
}
if (ind == n)
return 0;
if (dp[ind][curw] != -1)
return dp[ind][curw];
return max(in[ind].second + solv(ind + 1, curw + in[ind].first),
solv(ind + 1, curw));
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
memset(dp, -1, sizeof dp);
fr(i, 0, n) {
int w, v;
cin >> w >> v;
in.pb({w, v});
}
cout << solv(0, 0) << endl;
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
const long long inf = 1e18;
#define ll long long
#define fr(i, a, n) for (int i = a; i < n; i++)
#define rfr(i, n, a) for (int i = n; i >= a; i--)
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define endl "\n"
typedef vector<int> VI;
typedef vector<ll> VL;
ll n, m, k;
ll dp[101][100001];
vector<pair<int, int>> in;
ll solv(int ind, ll curw) {
if (curw > k) {
return -inf;
}
if (ind == n)
return 0;
if (dp[ind][curw] != -1)
return dp[ind][curw];
return dp[ind][curw] =
max(in[ind].second + solv(ind + 1, curw + in[ind].first),
solv(ind + 1, curw));
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
memset(dp, -1, sizeof dp);
fr(i, 0, n) {
int w, v;
cin >> w >> v;
in.pb({w, v});
}
cout << solv(0, 0) << endl;
return 0;
}
|
replace
| 27 | 29 | 27 | 30 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define nl '\n'
#define repd(i, a, b) for (int i = a; i >= b; --i)
#define pb push_back
#define all(a) a.begin(), a.end()
#define F first
#define S second
const ll N = 1e6 + 9;
const ll mod = 1e9 + 7;
ll a[100][N];
vector<pair<ll, ll>> v;
void solve() {
ll n, w, w1, v1;
cin >> n >> w;
rep(i, 1, n) {
cin >> w1 >> v1;
v.pb({w1, v1});
}
v.pb({0, 0});
sort(all(v));
rep(i, 1, n) {
rep(j, 1, w) {
if (j - v[i].F >= 0)
a[i][j] = max(a[i - 1][j], a[i - 1][j - v[i].F] + v[i].S);
else
a[i][j] = a[i - 1][j]; //,cout<<"A";
// cout<<a[i][j]<<" ";
}
// cout<<nl;
}
cout << a[n][w];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t = 1;
// pre();
// cin>>t;
// if(t^1)exit(0);
while (t--)
solve();
}
|
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define nl '\n'
#define repd(i, a, b) for (int i = a; i >= b; --i)
#define pb push_back
#define all(a) a.begin(), a.end()
#define F first
#define S second
const ll N = 1e6 + 9;
const ll mod = 1e9 + 7;
ll a[105][N];
vector<pair<ll, ll>> v;
void solve() {
ll n, w, w1, v1;
cin >> n >> w;
rep(i, 1, n) {
cin >> w1 >> v1;
v.pb({w1, v1});
}
v.pb({0, 0});
sort(all(v));
rep(i, 1, n) {
rep(j, 1, w) {
if (j - v[i].F >= 0)
a[i][j] = max(a[i - 1][j], a[i - 1][j - v[i].F] + v[i].S);
else
a[i][j] = a[i - 1][j]; //,cout<<"A";
// cout<<a[i][j]<<" ";
}
// cout<<nl;
}
cout << a[n][w];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t = 1;
// pre();
// cin>>t;
// if(t^1)exit(0);
while (t--)
solve();
}
|
replace
| 14 | 15 | 14 | 15 |
-11
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5 + 10;
long long n, W;
vector<long long> v, w;
long long dp[110][MAXN];
long long dfs(long long x, long long curr) {
if (x == n) {
return 0;
}
// if ( dp[x][curr] != -1 ) return dp[x][curr] ;
long long choice1 = 0 + dfs(x + 1, curr);
long long choice2 = -1e18;
if (curr + w[x] <= W) {
choice2 = v[x] + dfs(x + 1, curr + w[x]);
}
long long ans = max(choice1, choice2);
dp[x][curr] = ans;
return ans;
}
int main() {
memset(dp, -1, sizeof dp);
cin >> n >> W;
v.resize(n);
w.resize(n);
for (int i = 0; i < n; i++) {
cin >> w[i] >> v[i];
}
cout << dfs(0, 0) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5 + 10;
long long n, W;
vector<long long> v, w;
long long dp[110][MAXN];
long long dfs(long long x, long long curr) {
if (x == n) {
return 0;
}
if (dp[x][curr] != -1)
return dp[x][curr];
long long choice1 = 0 + dfs(x + 1, curr);
long long choice2 = -1e18;
if (curr + w[x] <= W) {
choice2 = v[x] + dfs(x + 1, curr + w[x]);
}
long long ans = max(choice1, choice2);
dp[x][curr] = ans;
return ans;
}
int main() {
memset(dp, -1, sizeof dp);
cin >> n >> W;
v.resize(n);
w.resize(n);
for (int i = 0; i < n; i++) {
cin >> w[i] >> v[i];
}
cout << dfs(0, 0) << endl;
return 0;
}
|
replace
| 10 | 11 | 10 | 12 |
TLE
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long int l;
l knapsack(l w[], l v[], l W, l n) {
if (n == 0 || W == 0)
return 0;
if (w[n - 1] <= W) {
return max(v[n - 1] + knapsack(w, v, W - w[n - 1], n - 1),
knapsack(w, v, W, n - 1));
} else
return knapsack(w, v, W, n - 1);
}
int main() {
l n, W;
cin >> n >> W;
l v[n], w[n];
for (l i = 0; i < n; i++) {
cin >> w[i] >> v[i];
}
cout << knapsack(w, v, W, n) << "\n";
return 0;
}
|
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long int l;
l knapsack(l wt[], l v[], l W, l n) {
l dp[n + 1][W + 1];
for (l i = 0; i <= n; i++) {
for (l w = 0; w <= W; w++) {
if (i == 0 || w == 0)
dp[i][w] = 0;
else if (wt[i - 1] <= w) {
dp[i][w] = max(v[i - 1] + dp[i - 1][w - wt[i - 1]], dp[i - 1][w]);
} else
dp[i][w] = dp[i - 1][w];
}
}
return dp[n][W];
}
int main() {
l n, W;
cin >> n >> W;
l v[n], w[n];
for (l i = 0; i < n; i++) {
cin >> w[i] >> v[i];
}
cout << knapsack(w, v, W, n) << "\n";
return 0;
}
|
replace
| 4 | 12 | 4 | 18 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define int long long
#define M 998244353
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define ios \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define F first
#define S second
#define zero(dp) memset(dp, 0, sizeof(dp))
#define pb emplace_back
using namespace std;
int32_t main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios int t = 1;
// cin>>t;
while (t--) {
int n;
cin >> n;
int W;
cin >> W;
int w[n], v[n];
for (int i = 0; i < n; i++)
cin >> w[i] >> v[i];
int dp[n][W + 1];
zero(dp);
for (int i = 0; i < n; i++) {
for (int j = 1; j <= W; j++)
dp[i][j] = -1;
}
dp[0][w[0]] = v[0];
for (int i = 1; i < n; i++) {
for (int j = 0; j <= W; j++) {
if (dp[i - 1][j] != -1 and j + w[i] <= W)
dp[i][j + w[i]] = dp[i - 1][j] + v[i];
}
for (int j = 0; j <= W; j++)
dp[i][j] = max(dp[i - 1][j], dp[i][j]);
}
int ans = 0;
for (int i = 0; i <= W; i++)
ans = max(ans, dp[n - 1][i]);
cout << ans;
}
}
|
#include <bits/stdc++.h>
#define int long long
#define M 998244353
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define ios \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define F first
#define S second
#define zero(dp) memset(dp, 0, sizeof(dp))
#define pb emplace_back
using namespace std;
int32_t main() {
/*
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
*/
ios int t = 1;
// cin>>t;
while (t--) {
int n;
cin >> n;
int W;
cin >> W;
int w[n], v[n];
for (int i = 0; i < n; i++)
cin >> w[i] >> v[i];
int dp[n][W + 1];
zero(dp);
for (int i = 0; i < n; i++) {
for (int j = 1; j <= W; j++)
dp[i][j] = -1;
}
dp[0][w[0]] = v[0];
for (int i = 1; i < n; i++) {
for (int j = 0; j <= W; j++) {
if (dp[i - 1][j] != -1 and j + w[i] <= W)
dp[i][j + w[i]] = dp[i - 1][j] + v[i];
}
for (int j = 0; j <= W; j++)
dp[i][j] = max(dp[i - 1][j], dp[i][j]);
}
int ans = 0;
for (int i = 0; i <= W; i++)
ans = max(ans, dp[n - 1][i]);
cout << ans;
}
}
|
replace
| 15 | 21 | 15 | 21 |
-11
| |
p03163
|
C++
|
Memory Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<vector<ll>> dp(200, vector<ll>(1000000, -1));
ll ans(ll a[][2], ll w, ll i) {
if (dp[i][w] != -1)
return dp[i][w];
if (i == 0 || w == 0) {
dp[i][w] = 0;
return dp[i][w];
}
if (w - a[i - 1][0] >= 0) {
dp[i][w] =
max(ans(a, w - a[i - 1][0], i - 1) + a[i - 1][1], ans(a, w, i - 1));
return dp[i][w];
} else {
dp[i][w] = ans(a, w, i - 1);
return dp[i][w];
}
}
int main() {
ll n, w;
cin >> n >> w;
ll a[n][2];
ll sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i][0] >> a[i][1];
}
ll x = ans(a, w, n);
cout << x << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<vector<ll>> dp(108, vector<ll>(100009, -1));
ll ans(ll a[][2], ll w, ll i) {
if (dp[i][w] != -1)
return dp[i][w];
if (i == 0 || w == 0) {
dp[i][w] = 0;
return dp[i][w];
}
if (w - a[i - 1][0] >= 0) {
dp[i][w] =
max(ans(a, w - a[i - 1][0], i - 1) + a[i - 1][1], ans(a, w, i - 1));
return dp[i][w];
} else {
dp[i][w] = ans(a, w, i - 1);
return dp[i][w];
}
}
int main() {
ll n, w;
cin >> n >> w;
ll a[n][2];
ll sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i][0] >> a[i][1];
}
ll x = ans(a, w, n);
cout << x << endl;
}
|
replace
| 4 | 5 | 4 | 5 |
MLE
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
long long n, W;
cin >> n >> W;
long long weight[n], values[n];
long long dp[n][W + 1];
for (int i = 0; i < n; ++i) {
cin >> weight[i] >> values[i];
}
for (int i = 0; i <= n; i++) {
for (int w = 0; w <= W; w++) {
if (i == 0 || w == 0)
dp[i][w] = 0;
else if (weight[i - 1] <= w)
dp[i][w] =
max(values[i - 1] + dp[i - 1][w - weight[i - 1]], dp[i - 1][w]);
else
dp[i][w] = dp[i - 1][w];
}
}
cout << dp[n][W] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
long long n, W;
cin >> n >> W;
long long weight[n], values[n];
long long dp[n + 1][W + 1];
for (int i = 0; i < n; ++i) {
cin >> weight[i] >> values[i];
}
for (int i = 0; i <= n; i++) {
for (int w = 0; w <= W; w++) {
if (i == 0 || w == 0)
dp[i][w] = 0;
else if (weight[i - 1] <= w)
dp[i][w] =
max(values[i - 1] + dp[i - 1][w - weight[i - 1]], dp[i - 1][w]);
else
dp[i][w] = dp[i - 1][w];
}
}
cout << dp[n][W] << endl;
}
|
replace
| 8 | 9 | 8 | 9 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <iostream>
#define ll long long int
using namespace std;
int main() {
ll n, W;
cin >> n >> W;
ll i, j;
ll v[200];
ll w[200];
ll dp[200] = {0};
for (i = 0; i < n; i++) {
cin >> w[i];
cin >> v[i];
}
for (i = 0; i < n; i++) {
for (j = W; j >= w[i]; j--)
dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
}
cout << dp[W];
}
|
#include <iostream>
#define ll long long int
using namespace std;
int main() {
ll n, W;
cin >> n >> W;
ll i, j;
ll v[200];
ll w[200];
ll dp[100005] = {0};
for (i = 0; i < n; i++) {
cin >> w[i];
cin >> v[i];
}
for (i = 0; i < n; i++) {
for (j = W; j >= w[i]; j--)
dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
}
cout << dp[W];
}
|
replace
| 10 | 11 | 10 | 11 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define lli long long int
#define ulli unsigned long long int
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pf pop_front()
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define rep(i, a, b) for (lli i = a; i < b; i++)
#define repe(i, a, b) for (lli i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define mod 1000000007
#define inf 100000000000000000
#define lld long double
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define vll vector<lli>
#define vii vector<int>
#define endl '\n'
#define pie acosl(-1.0);
#define setp(k, x) fixed << setprecision(k) << (x)
#define deb(x) cerr << #x << ": " << x << " " << endl;
#define debug(x) \
cout << "m here-" << #x << " ..." \
<< "\n";
#define printa(a, n) \
for (lli i = 0; i < n; i++) \
cout << a[i] << " "; \
cout << "\n";
#define printv(a) \
for (lli i = 0; i < a.size(); i++) \
cout << a[i] << " "; \
cout << "\n";
bool sortcol(const vector<lli> &v1,
const vector<lli> &v2) // sort on basis of column //use-
// sort(vect.begin(), vect.end(),sortcol);
{
return v1[1] < v2[1];
} //[] o based indexing in column
bool sortpair(const pair<lli, lli> &a, const pair<lli, lli> &b) {
return a.second < b.second;
}
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
//////////////////////////////////////////////////////////////////////////////////////////////
void solve();
//////////////////////////////////////////////////////////////////////////////////////////////
int main() {
// fastio
lli t;
cin >> t;
while (t--) {
solve();
}
}
///////////////////////////////////////////
void solve() {
lli n, i, d, a, b, c, j, k, w;
cin >> n >> w;
lli wt[n], val[n];
for (i = 0; i < n; i++) {
cin >> wt[i] >> val[i];
}
lli dp[n + 1][w + 1];
for (i = 0; i < n + 1; i++) {
dp[i][0] = 0;
}
for (i = 0; i < w + 1; i++) {
dp[0][i] = 0;
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= w; j++) {
if (wt[i - 1] <= j) {
dp[i][j] = max(val[i - 1] + (dp[i - 1][j - wt[i - 1]]), (dp[i - 1][j]));
} else
dp[i][j] = dp[i - 1][j];
}
}
// for(i=0;i<n;i++)
// for(j=0;j<w;j++)
// cout<<dp[i][j]<<" ";
cout << dp[n][w] << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define lli long long int
#define ulli unsigned long long int
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pf pop_front()
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define rep(i, a, b) for (lli i = a; i < b; i++)
#define repe(i, a, b) for (lli i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define mod 1000000007
#define inf 100000000000000000
#define lld long double
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define vll vector<lli>
#define vii vector<int>
#define endl '\n'
#define pie acosl(-1.0);
#define setp(k, x) fixed << setprecision(k) << (x)
#define deb(x) cerr << #x << ": " << x << " " << endl;
#define debug(x) \
cout << "m here-" << #x << " ..." \
<< "\n";
#define printa(a, n) \
for (lli i = 0; i < n; i++) \
cout << a[i] << " "; \
cout << "\n";
#define printv(a) \
for (lli i = 0; i < a.size(); i++) \
cout << a[i] << " "; \
cout << "\n";
bool sortcol(const vector<lli> &v1,
const vector<lli> &v2) // sort on basis of column //use-
// sort(vect.begin(), vect.end(),sortcol);
{
return v1[1] < v2[1];
} //[] o based indexing in column
bool sortpair(const pair<lli, lli> &a, const pair<lli, lli> &b) {
return a.second < b.second;
}
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
//////////////////////////////////////////////////////////////////////////////////////////////
void solve();
//////////////////////////////////////////////////////////////////////////////////////////////
int main() {
// fastio
// lli t;
// cin>>t;
// while(t--)
{ solve(); }
}
///////////////////////////////////////////
void solve() {
lli n, i, d, a, b, c, j, k, w;
cin >> n >> w;
lli wt[n], val[n];
for (i = 0; i < n; i++) {
cin >> wt[i] >> val[i];
}
lli dp[n + 1][w + 1];
for (i = 0; i < n + 1; i++) {
dp[i][0] = 0;
}
for (i = 0; i < w + 1; i++) {
dp[0][i] = 0;
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= w; j++) {
if (wt[i - 1] <= j) {
dp[i][j] = max(val[i - 1] + (dp[i - 1][j - wt[i - 1]]), (dp[i - 1][j]));
} else
dp[i][j] = dp[i - 1][j];
}
}
// for(i=0;i<n;i++)
// for(j=0;j<w;j++)
// cout<<dp[i][j]<<" ";
cout << dp[n][w] << "\n";
}
|
replace
| 79 | 84 | 79 | 83 |
-11
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
ll dp[101][10001] = {0};
ll helper(vector<int> &w, vector<int> &v, int index, int capacity) {
if (dp[index][capacity] != 0)
return dp[index][capacity];
if (index == w.size())
return 0;
if (capacity < w[index])
return helper(w, v, index + 1, capacity);
ll notSelected = helper(w, v, index + 1, capacity);
ll selected = helper(w, v, index + 1, capacity - w[index]) + v[index];
dp[index][capacity] = max(notSelected, selected);
return dp[index][capacity];
}
int main() {
int n, capacity;
cin >> n >> capacity;
vector<int> w(n);
vector<int> v(n);
for (int i = 0; i < n; i++)
cin >> w[i] >> v[i];
cout << helper(w, v, 0, capacity) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
ll dp[101][100001] = {0};
ll helper(vector<int> &w, vector<int> &v, int index, int capacity) {
if (dp[index][capacity] != 0)
return dp[index][capacity];
if (index == w.size())
return 0;
if (capacity < w[index])
return helper(w, v, index + 1, capacity);
ll notSelected = helper(w, v, index + 1, capacity);
ll selected = helper(w, v, index + 1, capacity - w[index]) + v[index];
dp[index][capacity] = max(notSelected, selected);
return dp[index][capacity];
}
int main() {
int n, capacity;
cin >> n >> capacity;
vector<int> w(n);
vector<int> v(n);
for (int i = 0; i < n; i++)
cin >> w[i] >> v[i];
cout << helper(w, v, 0, capacity) << endl;
return 0;
}
|
replace
| 5 | 6 | 5 | 6 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
const ll ARR = 1e5 + 2;
const ll MOD = 1e9 + 7;
const ll INF = MOD;
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define gof for (long long int i = 0; i < n; i++)
#define for1(i, s, e) for (long long int i = s; i < e; ++i)
#define weight first
#define value second
ll power(ll x, ll y) {
ll res = 1;
x = x % MOD;
if (x == 0)
return 0;
while (y > 0) {
if (y & 1)
res = (res * x) % MOD;
y = y >> 1;
x = (x * x) % MOD;
}
return res;
}
ll gcd(ll a, ll b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
ll max(ll a, ll b) {
if (a > b)
return a;
else
return b;
}
ll min(ll a, ll b) {
if (a < b)
return a;
else
return b;
}
ll max(ll a[], ll n) {
ll ans = 0;
for (ll i = 0; i < n; i++)
ans = max(ans, a[i]);
return ans;
}
ll min(ll a[], ll n) {
ll ans = INF;
for (ll i = 0; i < n; i++)
ans = min(ans, a[i]);
return ans;
}
bool isPrime(ll N) {
for (int i = 2; i * i <= N; ++i) {
if (N % i == 0)
return false;
}
return true;
}
void solve() {
ll n, W;
cin >> n >> W;
ll wt[n + 1], val[n + 1];
for1(i, 0, n) cin >> wt[i] >> val[i];
ll i, w;
ll K[n + 1][W + 1];
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];
}
}
cout << K[n][W];
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("test.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
const ll ARR = 1e5 + 2;
const ll MOD = 1e9 + 7;
const ll INF = MOD;
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define gof for (long long int i = 0; i < n; i++)
#define for1(i, s, e) for (long long int i = s; i < e; ++i)
#define weight first
#define value second
ll power(ll x, ll y) {
ll res = 1;
x = x % MOD;
if (x == 0)
return 0;
while (y > 0) {
if (y & 1)
res = (res * x) % MOD;
y = y >> 1;
x = (x * x) % MOD;
}
return res;
}
ll gcd(ll a, ll b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
ll max(ll a, ll b) {
if (a > b)
return a;
else
return b;
}
ll min(ll a, ll b) {
if (a < b)
return a;
else
return b;
}
ll max(ll a[], ll n) {
ll ans = 0;
for (ll i = 0; i < n; i++)
ans = max(ans, a[i]);
return ans;
}
ll min(ll a[], ll n) {
ll ans = INF;
for (ll i = 0; i < n; i++)
ans = min(ans, a[i]);
return ans;
}
bool isPrime(ll N) {
for (int i = 2; i * i <= N; ++i) {
if (N % i == 0)
return false;
}
return true;
}
void solve() {
ll n, W;
cin >> n >> W;
ll wt[n + 1], val[n + 1];
for1(i, 0, n) cin >> wt[i] >> val[i];
ll i, w;
ll K[n + 1][W + 1];
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];
}
}
cout << K[n][W];
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
|
delete
| 92 | 96 | 92 | 92 |
-11
| |
p03163
|
C++
|
Runtime Error
|
#include <iostream>
using namespace std;
typedef long long ll;
int main() {
int n, w;
cin >> n >> w;
ll weight[n], value[n];
for (int i = 0; i < n; i++) {
cin >> weight[i] >> value[i];
}
ll dp[n + 1][w + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= w; j++) {
if (i == 0 || j == 0)
dp[i][j] = 0;
else if (weight[i - 1] <= w)
dp[i][j] =
max(dp[i - 1][j], value[i - 1] + dp[i - 1][j - weight[i - 1]]);
else
dp[i][j] = dp[i - 1][j];
}
}
cout << dp[n][w];
}
|
#include <iostream>
using namespace std;
typedef long long ll;
int main() {
int n, w;
cin >> n >> w;
ll weight[n], value[n];
for (int i = 0; i < n; i++) {
cin >> weight[i] >> value[i];
}
ll dp[n + 1][w + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= w; j++) {
if (i == 0 || j == 0)
dp[i][j] = 0;
else if (weight[i - 1] <= j)
dp[i][j] =
max(dp[i - 1][j], value[i - 1] + dp[i - 1][j - weight[i - 1]]);
else
dp[i][j] = dp[i - 1][j];
}
}
cout << dp[n][w];
}
|
replace
| 20 | 21 | 20 | 21 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll w_max = 10005;
const ll n_max = 105;
ll dp[w_max][n_max];
int main() {
ll N, W;
cin >> N >> W;
vector<ll> v(N + 1);
vector<ll> w(N + 1);
for (ll i = 1; i <= N; i++)
cin >> w[i] >> v[i];
for (ll i = 0; i <= W; i++)
dp[i][0] = 0;
for (ll i = 0; i <= N; i++)
dp[0][i] = 0;
for (ll i = 1; i <= W; i++) {
for (ll j = 1; j <= N; j++) {
if (i >= w[j]) {
dp[i][j] = max(dp[i - w[j]][j - 1] + v[j], dp[i][j - 1]);
} else
dp[i][j] = dp[i][j - 1];
}
}
cout << dp[W][N] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll w_max = 100005;
const ll n_max = 105;
ll dp[w_max][n_max];
int main() {
ll N, W;
cin >> N >> W;
vector<ll> v(N + 1);
vector<ll> w(N + 1);
for (ll i = 1; i <= N; i++)
cin >> w[i] >> v[i];
for (ll i = 0; i <= W; i++)
dp[i][0] = 0;
for (ll i = 0; i <= N; i++)
dp[0][i] = 0;
for (ll i = 1; i <= W; i++) {
for (ll j = 1; j <= N; j++) {
if (i >= w[j]) {
dp[i][j] = max(dp[i - w[j]][j - 1] + v[j], dp[i][j - 1]);
} else
dp[i][j] = dp[i][j - 1];
}
}
cout << dp[W][N] << endl;
}
|
replace
| 4 | 5 | 4 | 5 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
using namespace std;
ll power(ll x, ll y) {
ll res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
x = (x * x);
y = y / 2;
}
return res;
}
int main() {
ll n, W;
cin >> n >> W;
ll val[n + 1], wt[n + 1], knap[n + 1][W + 1];
for (ll i = 0; i < n; i++)
cin >> wt[i] >> val[i];
// W=8
// val{2,3,1,4}//see video abdul bari
// wt{3,4,6,5}
// 0 1 2 3 4 5 6 7 8 i--->
// vi wi 0 0 0 0 0 0 0 0 0 0 j---v (downwards)
// 2 3 1 0
// 3 4 2 0
// 1 6 3 0
// 4 5 4 0
for (ll i = 0; i <= n; i++) {
for (ll j = 0; j <= W; j++) {
if (i == 0 || j == 0)
knap[i][j] = 0;
else if (j >= wt[i])
knap[i][j] =
max(knap[i - 1][j], knap[i - 1][j - wt[i - 1]] + val[i - 1]);
else
knap[i][j] = knap[i - 1][j];
}
}
cout << knap[n][W];
}
|
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
using namespace std;
ll power(ll x, ll y) {
ll res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
x = (x * x);
y = y / 2;
}
return res;
}
int main() {
ll n, W;
cin >> n >> W;
ll val[n + 1], wt[n + 1], knap[n + 1][W + 1];
for (ll i = 0; i < n; i++)
cin >> wt[i] >> val[i];
// W=8
// val{2,3,1,4}//see video abdul bari
// wt{3,4,6,5}
// 0 1 2 3 4 5 6 7 8 i--->
// vi wi 0 0 0 0 0 0 0 0 0 0 j---v (downwards)
// 2 3 1 0
// 3 4 2 0
// 1 6 3 0
// 4 5 4 0
for (ll i = 0; i <= n; i++) {
for (ll j = 0; j <= W; j++) {
if (i == 0 || j == 0)
knap[i][j] = 0;
else if (j >= wt[i - 1])
knap[i][j] =
max(knap[i - 1][j], knap[i - 1][j - wt[i - 1]] + val[i - 1]);
else
knap[i][j] = knap[i - 1][j];
}
}
cout << knap[n][W];
}
|
replace
| 34 | 35 | 34 | 35 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
using namespace std;
ll power(ll x, ll y) {
ll res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
x = (x * x);
y = y / 2;
}
return res;
}
int main() {
ll n, W;
cin >> n >> W;
ll val[n + 1], wt[n + 1], knap[n + 1][W + 1];
for (ll i = 0; i < n; i++)
cin >> wt[i] >> val[i];
// W=8
// val{2,3,1,4}//see video abdul bari
// wt{3,4,6,5}
// 0 1 2 3 4 5 6 7 8 i--->
// vi wi 0 0 0 0 0 0 0 0 0 0 j---v (downwards)
// 2 3 1 0
// 3 4 2 0
// 1 6 3 0
// 4 5 4 0
for (ll i = 0; i <= n; i++) {
for (ll j = 0; j <= W; j++) {
if (i == 0 || j == 0)
knap[i][j] = 0;
else if (j - wt[i] >= 0)
knap[i][j] =
max(knap[i - 1][j], knap[i - 1][j - wt[i - 1]] + val[i - 1]);
else
knap[i][j] = knap[i - 1][j];
}
}
cout << knap[n][W];
}
|
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
using namespace std;
ll power(ll x, ll y) {
ll res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
x = (x * x);
y = y / 2;
}
return res;
}
int main() {
ll n, W;
cin >> n >> W;
ll val[n + 1], wt[n + 1], knap[n + 1][W + 1];
for (ll i = 0; i < n; i++)
cin >> wt[i] >> val[i];
// W=8
// val{2,3,1,4}//see video abdul bari
// wt{3,4,6,5}
// 0 1 2 3 4 5 6 7 8 i--->
// vi wi 0 0 0 0 0 0 0 0 0 0 j---v (downwards)
// 2 3 1 0
// 3 4 2 0
// 1 6 3 0
// 4 5 4 0
for (ll i = 0; i <= n; i++) {
for (ll j = 0; j <= W; j++) {
if (i == 0 || j == 0)
knap[i][j] = 0;
else if (j - wt[i - 1] >= 0)
knap[i][j] =
max(knap[i - 1][j], knap[i - 1][j - wt[i - 1]] + val[i - 1]);
else
knap[i][j] = knap[i - 1][j];
}
}
cout << knap[n][W];
}
|
replace
| 34 | 35 | 34 | 35 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long n, W;
cin >> n >> W;
long w[n], v[n];
for (int i = 0; i < n; i++) {
cin >> w[i] >> v[i];
}
long dp[n][W];
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] = 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 namespace std;
int main() {
long n, W;
cin >> n >> W;
long w[n], v[n];
for (int i = 0; i < n; i++) {
cin >> w[i] >> v[i];
}
long dp[n + 1][W + 1];
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] = 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++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 105;
const int MAX_W = 1e3 + 5;
int n_test;
int n, W, w, v;
long long solve() {
scanf("%d %d", &n, &W);
long long res[MAX_N][MAX_W] = {{0L}};
for (int i = 1; i <= n; ++i) {
scanf("%d %d", &w, &v);
for (int j = 0; j <= W; ++j)
res[i][j] = res[i - 1][j];
for (int j = w; j <= W; ++j) {
res[i][j] = max(res[i][j], res[i - 1][j - w] + v);
}
}
return res[n][W];
}
int main() {
n_test = 1;
// scanf("%d", &n_test);
for (int i = 0; i < n_test; ++i)
printf("%lld\n", solve());
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 105;
const int MAX_W = 1e5 + 5;
int n_test;
int n, W, w, v;
long long solve() {
scanf("%d %d", &n, &W);
long long res[MAX_N][MAX_W] = {{0L}};
for (int i = 1; i <= n; ++i) {
scanf("%d %d", &w, &v);
for (int j = 0; j <= W; ++j)
res[i][j] = res[i - 1][j];
for (int j = w; j <= W; ++j) {
res[i][j] = max(res[i][j], res[i - 1][j - w] + v);
}
}
return res[n][W];
}
int main() {
n_test = 1;
// scanf("%d", &n_test);
for (int i = 0; i < n_test; ++i)
printf("%lld\n", solve());
}
|
replace
| 4 | 5 | 4 | 5 |
0
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
long long memo[1001][100001];
long long knapsack(long long value[], long long weight[], int index, int n,
long long w) {
if (index >= n || w <= 0)
return 0;
if (weight[index] > w)
return knapsack(value, weight, index, n, w);
if (memo[index][w] != -1)
return memo[index][w];
long long temp1;
long long temp2;
temp1 =
value[index] + knapsack(value, weight, index + 1, n, w - weight[index]);
temp2 = knapsack(value, weight, index + 1, n, w);
memo[index][w] = max(temp1, temp2);
return memo[index][w];
// return
// memo[index][w]=max(value[index]+knapsack(value,weight,index+1,n,w-weight[index]),knapsack(value,weight,index+1,n,w));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
long long w;
cin >> w;
long long weight[n], value[n];
for (int i = 0; i < n; i++)
cin >> weight[i] >> value[i];
for (long long i = 0; i <= n; i++) {
for (long long j = 0; j <= w; j++)
memo[i][j] = -1;
}
cout << knapsack(value, weight, 0, n, w) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long memo[1001][100001];
long long knapsack(long long value[], long long weight[], int index, int n,
long long w) {
if (index >= n || w <= 0)
return 0;
if (weight[index] > w) {
if (memo[index][w] != -1)
return memo[index][w];
return memo[index][w] = knapsack(value, weight, index + 1, n, w);
}
if (memo[index][w] != -1)
return memo[index][w];
long long temp1;
long long temp2;
temp1 =
value[index] + knapsack(value, weight, index + 1, n, w - weight[index]);
temp2 = knapsack(value, weight, index + 1, n, w);
memo[index][w] = max(temp1, temp2);
return memo[index][w];
// return
// memo[index][w]=max(value[index]+knapsack(value,weight,index+1,n,w-weight[index]),knapsack(value,weight,index+1,n,w));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
long long w;
cin >> w;
long long weight[n], value[n];
for (int i = 0; i < n; i++)
cin >> weight[i] >> value[i];
for (long long i = 0; i <= n; i++) {
for (long long j = 0; j <= w; j++)
memo[i][j] = -1;
}
cout << knapsack(value, weight, 0, n, w) << endl;
return 0;
}
|
replace
| 7 | 9 | 7 | 13 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
//*******Abhijit Burman***********//
// Jalpaiguri Government Engineering College//
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mk make_pair
#define MAXX (1000000000000000000 + 7)
#define fio \
ios_base::sync_with_stdio(0); \
cin.tie(NULL); \
cout.tie(NULL)
ll dp[110][100100];
ll knapsack(ll a[], ll b[], ll wt, ll index) {
// cout<<wt<<endl;
if (index < 0) {
return 0;
}
if (dp[wt][index] != -1) {
return dp[wt][index];
} else {
if (wt - a[index] < 0) {
dp[wt][index] = knapsack(a, b, wt, index - 1);
} else
dp[wt][index] = max(b[index] + knapsack(a, b, wt - a[index], index - 1),
knapsack(a, b, wt, index - 1));
return dp[wt][index];
}
}
void solve(ll t) {
ll n, w;
cin >> n >> w;
ll a[n], b[n];
memset(dp, -1, sizeof(dp));
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
}
ll ans = knapsack(a, b, w, n - 1);
cout << ans << endl;
}
int main() {
ll t = 1;
// cin>>t;
while (t--) {
solve(t);
}
return 0;
}
|
//*******Abhijit Burman***********//
// Jalpaiguri Government Engineering College//
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mk make_pair
#define MAXX (1000000000000000000 + 7)
#define fio \
ios_base::sync_with_stdio(0); \
cin.tie(NULL); \
cout.tie(NULL)
ll dp[100100][110];
ll knapsack(ll a[], ll b[], ll wt, ll index) {
// cout<<wt<<endl;
if (index < 0) {
return 0;
}
if (dp[wt][index] != -1) {
return dp[wt][index];
} else {
if (wt - a[index] < 0) {
dp[wt][index] = knapsack(a, b, wt, index - 1);
} else
dp[wt][index] = max(b[index] + knapsack(a, b, wt - a[index], index - 1),
knapsack(a, b, wt, index - 1));
return dp[wt][index];
}
}
void solve(ll t) {
ll n, w;
cin >> n >> w;
ll a[n], b[n];
memset(dp, -1, sizeof(dp));
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
}
ll ans = knapsack(a, b, w, n - 1);
cout << ans << endl;
}
int main() {
ll t = 1;
// cin>>t;
while (t--) {
solve(t);
}
return 0;
}
|
replace
| 15 | 16 | 15 | 16 |
0
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll wt[101], v[101];
ll fun(int n, int W, ll **dp) {
if (W == 0 || n < 0)
return 0;
if (W - wt[n] >= 0) {
dp[n][W] = max(dp[n][W], fun(n - 1, W - wt[n], dp) + v[n]);
}
dp[n][W] = max(dp[n][W], fun(n - 1, W, dp));
return dp[n][W];
}
int main() {
int n, W;
cin >> n >> W;
for (int i = 0; i < n; i++) {
cin >> wt[i] >> v[i];
}
ll **dp = new ll *[n];
for (int i = 0; i < n; i++)
dp[i] = new ll[W + 1];
for (ll i = 0; i < n; i++) {
for (ll j = 0; j < W + 1; j++) {
dp[i][j] = -1;
}
}
cout << fun(n - 1, W, dp);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll wt[101], v[101];
ll fun(int n, int W, ll **dp) {
if (W == 0 || n < 0)
return 0;
if (dp[n][W] != -1)
return dp[n][W];
if (W - wt[n] >= 0) {
dp[n][W] = max(dp[n][W], fun(n - 1, W - wt[n], dp) + v[n]);
}
dp[n][W] = max(dp[n][W], fun(n - 1, W, dp));
return dp[n][W];
}
int main() {
int n, W;
cin >> n >> W;
for (int i = 0; i < n; i++) {
cin >> wt[i] >> v[i];
}
ll **dp = new ll *[n];
for (int i = 0; i < n; i++)
dp[i] = new ll[W + 1];
for (ll i = 0; i < n; i++) {
for (ll j = 0; j < W + 1; j++) {
dp[i][j] = -1;
}
}
cout << fun(n - 1, W, dp);
return 0;
}
|
replace
| 8 | 9 | 8 | 10 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
long long dp[100][100000];
int main() {
long long n, W, i, x, y, tw, j;
cin >> n >> W;
vector<long long> a, w;
for (i = 1; i <= n; i++) {
cin >> y >> x;
a.push_back(x);
w.push_back(y);
}
for (i = 0; i <= n; i++) {
for (j = 0; j <= W; j++) {
if (i == 0 || j == 0)
dp[i][j] = 0;
else if (j >= w[i - 1])
dp[i][j] = max(dp[i - 1][j], a[i - 1] + dp[i - 1][j - w[i - 1]]);
else
dp[i][j] = dp[i - 1][j];
// cout<<dp[i][j]<<" ";
}
// cout<<endl;
}
cout << dp[n][W] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long dp[101][100001];
int main() {
long long n, W, i, x, y, tw, j;
cin >> n >> W;
vector<long long> a, w;
for (i = 1; i <= n; i++) {
cin >> y >> x;
a.push_back(x);
w.push_back(y);
}
for (i = 0; i <= n; i++) {
for (j = 0; j <= W; j++) {
if (i == 0 || j == 0)
dp[i][j] = 0;
else if (j >= w[i - 1])
dp[i][j] = max(dp[i - 1][j], a[i - 1] + dp[i - 1][j - w[i - 1]]);
else
dp[i][j] = dp[i - 1][j];
// cout<<dp[i][j]<<" ";
}
// cout<<endl;
}
cout << dp[n][W] << endl;
return 0;
}
|
replace
| 2 | 3 | 2 | 3 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
const int MAX = 1e5 + 10;
ll N, W, w, v;
ll dp[111];
ll ans;
int main() {
cin >> N >> W;
dp[0] = 1;
REP(i, N) {
cin >> w >> v;
for (int j = W - w; j >= 0; --j)
if (dp[j] > 0)
dp[j + w] = max(dp[j + w], dp[j] + v);
}
REP(i, W + 1) ans = max(ans, 1ll * dp[i]);
cout << ans - 1 << endl;
}
|
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
const int MAX = 1e5 + 10;
ll N, W, w, v;
ll dp[MAX];
ll ans;
int main() {
cin >> N >> W;
dp[0] = 1;
REP(i, N) {
cin >> w >> v;
for (int j = W - w; j >= 0; --j)
if (dp[j] > 0)
dp[j + w] = max(dp[j + w], dp[j] + v);
}
REP(i, W + 1) ans = max(ans, 1ll * dp[i]);
cout << ans - 1 << endl;
}
|
replace
| 11 | 12 | 11 | 12 |
0
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include "bits/stdc++.h"
using namespace std;
const int MAXN = 1e5 + 5;
long long DP[MAXN];
int main(int argc, char *argv[]) {
int N, W, w, v;
long long answer = 0;
scanf("%d%d", &N, &W);
for (int i = 0; i < W; i++) {
scanf("%d%d", &w, &v);
for (int j = W; 0 <= j; j--)
if ((DP[j] or j == 0) and j + w <= W) {
DP[j + w] = max(DP[j + w], DP[j] + v);
answer = max(answer, DP[j + w]);
}
}
printf("%lld\n", answer);
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
const int MAXN = 1e5 + 5;
long long DP[MAXN];
int main(int argc, char *argv[]) {
int N, W, w, v;
long long answer = 0;
scanf("%d%d", &N, &W);
for (int i = 0; i < N; i++) {
scanf("%d%d", &w, &v);
for (int j = W; 0 <= j; j--)
if ((DP[j] or j == 0) and j + w <= W) {
DP[j + w] = max(DP[j + w], DP[j] + v);
answer = max(answer, DP[j + w]);
}
}
printf("%lld\n", answer);
return 0;
}
|
replace
| 12 | 13 | 12 | 13 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll n, w;
cin >> n >> w;
vector<ll> weight(n), value(n);
for (ll i = 1; i <= n; ++i) {
cin >> weight[i] >> value[i];
}
ll arr[n + 1][w + 1];
for (ll i = 0; i < n + 1; ++i) {
for (ll j = 0; j < w + 1; ++j)
arr[i][j] = 0;
}
for (ll i = 1; i <= n; ++i) {
for (ll j = 1; j <= w; ++j) {
if (j < weight[i]) {
arr[i][j] = arr[i - 1][j];
} else
arr[i][j] = max(value[i] + arr[i - 1][j - weight[i]], arr[i - 1][j]);
}
}
cout << *max_element(arr[n], arr[n] + w + 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll n, w;
cin >> n >> w;
vector<ll> weight(101), value(101);
for (ll i = 1; i <= n; ++i) {
cin >> weight[i] >> value[i];
}
ll arr[n + 1][w + 1];
for (ll i = 0; i < n + 1; ++i) {
for (ll j = 0; j < w + 1; ++j)
arr[i][j] = 0;
}
for (ll i = 1; i <= n; ++i) {
for (ll j = 1; j <= w; ++j) {
if (j < weight[i]) {
arr[i][j] = arr[i - 1][j];
} else
arr[i][j] = max(value[i] + arr[i - 1][j - weight[i]], arr[i - 1][j]);
}
}
cout << *max_element(arr[n], arr[n] + w + 1);
return 0;
}
|
replace
| 10 | 11 | 10 | 11 |
-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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef map<string, int> msi;
typedef set<ll> sl;
typedef set<char> sc;
typedef pair<int, int> pii;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const int INF = 1 << 29;
#define MOD (ll)(1e9 + 7)
#define eps 1e-6
#define pb push_back
#define mp make_pair
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define F first
#define S second
#define all(s) s.begin, s.end()
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
freopen("input.txt", "r", stdin);
int n, W;
cin >> n >> W;
int w[n], v[n];
REP(i, n) cin >> w[i] >> v[i];
long long dp[n + 1][W + 1];
REP(i, n + 1) REP(j, W + 1) dp[i][j] = 0;
REP(i, n) {
REP(j, W + 1) {
if (j >= w[i])
dp[i + 1][j] = max(dp[i][j], v[i] + dp[i][j - w[i]]);
else
dp[i + 1][j] = dp[i][j];
}
}
/*
REP(i,n+1) {
REP(j,W+1) {
cout << dp[i][j] << " " ;
}
cout << endl;
}
*/
cout << dp[n][W] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef map<string, int> msi;
typedef set<ll> sl;
typedef set<char> sc;
typedef pair<int, int> pii;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const int INF = 1 << 29;
#define MOD (ll)(1e9 + 7)
#define eps 1e-6
#define pb push_back
#define mp make_pair
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define F first
#define S second
#define all(s) s.begin, s.end()
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
// freopen("input.txt","r",stdin);
int n, W;
cin >> n >> W;
int w[n], v[n];
REP(i, n) cin >> w[i] >> v[i];
long long dp[n + 1][W + 1];
REP(i, n + 1) REP(j, W + 1) dp[i][j] = 0;
REP(i, n) {
REP(j, W + 1) {
if (j >= w[i])
dp[i + 1][j] = max(dp[i][j], v[i] + dp[i][j - w[i]]);
else
dp[i + 1][j] = dp[i][j];
}
}
/*
REP(i,n+1) {
REP(j,W+1) {
cout << dp[i][j] << " " ;
}
cout << endl;
}
*/
cout << dp[n][W] << endl;
return 0;
}
|
replace
| 33 | 34 | 33 | 34 |
-11
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define debug(x) cerr << #x << " = " << x << endl
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const int maxN = 1e3 + 5;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll modP(ll a, ll b) {
return (!b ? 1 : (modP(a, b / 2) * modP(a, b / 2) * (b % 2 ? a : 1)) % MOD);
}
ll n, m, W, dp[maxN], w[maxN], v[maxN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
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 = W; j > -1; j--) {
if (j == 0 || j < w[i])
continue;
dp[j] = max(dp[j], v[i] + dp[j - w[i]]);
}
}
cout << dp[W];
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define debug(x) cerr << #x << " = " << x << endl
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const int maxN = 1e3 + 5;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll modP(ll a, ll b) {
return (!b ? 1 : (modP(a, b / 2) * modP(a, b / 2) * (b % 2 ? a : 1)) % MOD);
}
ll n, m, W, dp[maxN * maxN], w[maxN], v[maxN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
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 = W; j > -1; j--) {
if (j == 0 || j < w[i])
continue;
dp[j] = max(dp[j], v[i] + dp[j - w[i]]);
}
}
cout << dp[W];
return 0;
}
|
replace
| 26 | 27 | 26 | 27 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 1000000007
using namespace std;
using P = pair<int, int>;
using ll = long long;
int main() {
int n, m;
cin >> n >> m;
vector<ll> w(n);
vector<ll> v(n);
for (int i = 0; i < n; i++)
cin >> w[i] >> v[i];
vector<vector<ll>> dp(n + 1, vector<ll>(m, 0));
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j <= m; j++) {
if (w[i] > j)
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][m] << endl;
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 1000000007
using namespace std;
using P = pair<int, int>;
using ll = long long;
int main() {
int n, m;
cin >> n >> m;
vector<ll> w(n);
vector<ll> v(n);
for (int i = 0; i < n; i++)
cin >> w[i] >> v[i];
vector<vector<ll>> dp(n + 1, vector<ll>(m + 1, 0));
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j <= m; j++) {
if (w[i] > j)
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][m] << endl;
return 0;
}
|
replace
| 23 | 24 | 23 | 24 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;
long long INF = LLONG_MAX;
int n, W;
pll w[101];
ll v[100111];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
cin >> n >> W;
memset(v, 0, sizeof(v));
for (int i = 0; i < n; i++) {
cin >> w[i].fi >> w[i].se;
}
sort(w, w + n);
for (auto &cur : w) {
ll cw = cur.fi;
ll cv = cur.se;
for (int i = W; i > 0; i--) {
if (v[i] > 0)
v[i + cw] = max(v[i + cw], cv + v[i]);
}
v[cw] = max(v[cw], cv);
}
/*
for(int i=0; i<=W; i++){
cout << i << ' ' << v[i] << endl;
}
*/
ll ans = 0;
for (int i = 0; i <= W; i++)
ans = max(ans, v[i]);
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;
long long INF = LLONG_MAX;
int n, W;
pll w[101];
ll v[100111];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
cin >> n >> W;
memset(v, 0, sizeof(v));
for (int i = 0; i < n; i++) {
cin >> w[i].fi >> w[i].se;
}
sort(w, w + n);
for (auto &cur : w) {
ll cw = cur.fi;
ll cv = cur.se;
for (int i = W; i > 0; i--) {
if (v[i] > 0 && i + cw <= W)
v[i + cw] = max(v[i + cw], cv + v[i]);
}
v[cw] = max(v[cw], cv);
}
/*
for(int i=0; i<=W; i++){
cout << i << ' ' << v[i] << endl;
}
*/
ll ans = 0;
for (int i = 0; i <= W; i++)
ans = max(ans, v[i]);
cout << ans << endl;
}
|
replace
| 38 | 39 | 38 | 39 |
0
| |
p03163
|
C++
|
Runtime Error
|
/**
* Title: D - Knapsack 1
* Url: https://atcoder.jp/contests/dp/tasks/dp_d
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int N, W;
const int MAX_N = 100;
const int MAX_W = 100000;
int w[MAX_N + 1], v[MAX_N + 1];
ll dp[MAX_N + 1][MAX_W + 1];
ll rec(int i, int j) {
if (dp[i][j] != -1)
return dp[i][j];
if (i == N + 1)
return dp[i][j] = 0;
if (j < w[i])
return dp[i][j] = rec(i + 1, j);
return dp[i][j] = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]);
}
int main() {
cin >> N >> W;
for (int i = 0; i < N; i++)
cin >> w[i + 1] >> v[i + 1];
memset(dp, -1, sizeof(dp));
cout << rec(1, W) << endl;
return 0;
}
|
/**
* Title: D - Knapsack 1
* Url: https://atcoder.jp/contests/dp/tasks/dp_d
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int N, W;
const int MAX_N = 100;
const int MAX_W = 100000;
int w[MAX_N + 1], v[MAX_N + 1];
ll dp[MAX_N + 2][MAX_W + 2];
ll rec(int i, int j) {
if (dp[i][j] != -1)
return dp[i][j];
if (i == N + 1)
return dp[i][j] = 0;
if (j < w[i])
return dp[i][j] = rec(i + 1, j);
return dp[i][j] = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]);
}
int main() {
cin >> N >> W;
for (int i = 0; i < N; i++)
cin >> w[i + 1] >> v[i + 1];
memset(dp, -1, sizeof(dp));
cout << rec(1, W) << endl;
return 0;
}
|
replace
| 15 | 16 | 15 | 16 |
0
| |
p03163
|
C++
|
Runtime Error
|
// Created by sz
#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
const int maxw = 10005;
int N, W, w[maxn], v[maxn];
long long dp[maxn][maxw];
int main() {
#ifdef LOCAL
freopen("./input.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> W;
for (int i = 1; i <= N; i++)
cin >> w[i] >> v[i];
for (int i = 0; i <= W; i++) {
if (i < w[1])
dp[1][i] = 0;
else
dp[1][i] = v[1];
}
for (int i = 2; i <= N; i++) {
for (int j = 0; j <= W; j++) {
dp[i][j] = dp[i - 1][j];
if (w[i] <= j)
dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i]] + v[i]);
}
}
cout << dp[N][W] << endl;
return 0;
}
|
// Created by sz
#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
const int maxw = 100005;
int N, W, w[maxn], v[maxn];
long long dp[maxn][maxw];
int main() {
#ifdef LOCAL
freopen("./input.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> W;
for (int i = 1; i <= N; i++)
cin >> w[i] >> v[i];
for (int i = 0; i <= W; i++) {
if (i < w[1])
dp[1][i] = 0;
else
dp[1][i] = v[1];
}
for (int i = 2; i <= N; i++) {
for (int j = 0; j <= W; j++) {
dp[i][j] = dp[i - 1][j];
if (w[i] <= j)
dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i]] + v[i]);
}
}
cout << dp[N][W] << endl;
return 0;
}
|
replace
| 5 | 6 | 5 | 6 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define endl "\n"
#define F first
#define S second
#define mod 1000000007
#define pb push_back
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define REV(i, a, n) for (int i = a; i >= n; i--)
#define all(a) a.begin(), a.end()
#define UB upper_bound
#define LB lower_bound
const int NUM = 2e5 + 5;
int dp[100][10001];
int w[100], v[100];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int num_tests = 1;
// cin >> num_tests;
while (num_tests-- > 0) {
int n, W;
cin >> n >> W;
FOR(i, 0, n) { cin >> w[i] >> v[i]; }
dp[0][0] = 0;
dp[0][w[0]] = v[0];
for (int i = 1; i < n; i++) {
dp[i][0] = 0; // bag is empty
for (int j = 1; j <= W; j++) {
// discarding an object
dp[i][j] = dp[i - 1][j];
// picking element
if (j >= w[i])
dp[i][j] = max(dp[i][j], v[i] + dp[i - 1][j - w[i]]);
}
}
int ans = -1e18;
FOR(i, 0, W + 1)
ans = max(ans, dp[n - 1][i]);
cout << ans;
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define endl "\n"
#define F first
#define S second
#define mod 1000000007
#define pb push_back
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define REV(i, a, n) for (int i = a; i >= n; i--)
#define all(a) a.begin(), a.end()
#define UB upper_bound
#define LB lower_bound
const int NUM = 2e5 + 5;
int dp[100][100001];
int w[100], v[100];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int num_tests = 1;
// cin >> num_tests;
while (num_tests-- > 0) {
int n, W;
cin >> n >> W;
FOR(i, 0, n) { cin >> w[i] >> v[i]; }
dp[0][0] = 0;
dp[0][w[0]] = v[0];
for (int i = 1; i < n; i++) {
dp[i][0] = 0; // bag is empty
for (int j = 1; j <= W; j++) {
// discarding an object
dp[i][j] = dp[i - 1][j];
// picking element
if (j >= w[i])
dp[i][j] = max(dp[i][j], v[i] + dp[i - 1][j - w[i]]);
}
}
int ans = -1e18;
FOR(i, 0, W + 1)
ans = max(ans, dp[n - 1][i]);
cout << ans;
}
}
|
replace
| 14 | 15 | 14 | 15 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
int N, W;
cin >> N >> W;
vector<vector<int>> dp(N + 10, vector<int>(W, 0));
int v, w;
for (int i = 0; i < N; i++) {
cin >> w >> v;
for (int j = 0; j <= W; j++) {
if (j - w < 0) {
dp[i + 1][j] = dp[i][j];
} else {
dp[i + 1][j] = max(dp[i][j - w] + v, dp[i][j]);
}
}
}
cout << dp[N][W] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
int N, W;
cin >> N >> W;
vector<vector<int>> dp(110, vector<int>(100010, 0));
int v, w;
for (int i = 0; i < N; i++) {
cin >> w >> v;
for (int j = 0; j <= W; j++) {
if (j - w < 0) {
dp[i + 1][j] = dp[i][j];
} else {
dp[i + 1][j] = max(dp[i][j - w] + v, dp[i][j]);
}
}
}
cout << dp[N][W] << endl;
}
|
replace
| 7 | 8 | 7 | 8 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
vector<int> V;
ll n, W, w, v;
ll dp[100][100010];
int main() {
cin >> n >> W;
for (int i = 1; i <= n; i++) {
cin >> w >> v;
for (int j = 1; j <= W; j++) {
dp[i][j] = dp[i - 1][j];
if (j - w >= 0)
dp[i][j] = max(dp[i][j], dp[i - 1][j - w] + v);
}
}
cout << dp[n][W];
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
vector<int> V;
ll n, W, w, v;
ll dp[110][100010];
int main() {
cin >> n >> W;
for (int i = 1; i <= n; i++) {
cin >> w >> v;
for (int j = 1; j <= W; j++) {
dp[i][j] = dp[i - 1][j];
if (j - w >= 0)
dp[i][j] = max(dp[i][j], dp[i - 1][j - w] + v);
}
}
cout << dp[n][W];
}
|
replace
| 7 | 8 | 7 | 8 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
constexpr ll MOD = (1e9 + 7);
constexpr int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
constexpr int lcm(int a, int b) { return a / gcd(a, b) * b; }
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;
}
ll factorial(ll n, ll m = 2) {
// calculate n!
m = max(2LL, m);
ll rtn = 1;
for (ll i = m; i <= n; i++) {
rtn = (rtn * i) % MOD;
}
return rtn;
}
ll modinv(ll a, ll m) {
ll b = m, u = 1, v = 0;
while (b) {
ll t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
ll modpow(ll a, ll n) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return res;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int N, W;
cin >> N >> W;
vector<ll> w(N), v(N);
rep(i, N) cin >> w[i] >> v[i];
vector<vector<ll>> dp(N + 1, vector<ll>(W, 0));
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>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
constexpr ll MOD = (1e9 + 7);
constexpr int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
constexpr int lcm(int a, int b) { return a / gcd(a, b) * b; }
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;
}
ll factorial(ll n, ll m = 2) {
// calculate n!
m = max(2LL, m);
ll rtn = 1;
for (ll i = m; i <= n; i++) {
rtn = (rtn * i) % MOD;
}
return rtn;
}
ll modinv(ll a, ll m) {
ll b = m, u = 1, v = 0;
while (b) {
ll t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
ll modpow(ll a, ll n) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return res;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int N, W;
cin >> N >> W;
vector<ll> w(N), v(N);
rep(i, N) cin >> w[i] >> v[i];
vector<vector<ll>> dp(N + 1, vector<ll>(W + 1, 0));
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
| 70 | 71 | 70 | 71 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define mx 105
int n, W;
int weight[mx], value[mx];
#define ll long long
ll dp[mx][mx];
ll func(int i, int w) {
if (i >= n + 1)
return 0;
if (dp[i][w] != -1)
return dp[i][w];
ll p1 = 0, p2 = 0;
if (w + weight[i] <= W) {
p1 = value[i] + func(i + 1, w + weight[i]);
}
p2 = func(i + 1, w);
dp[i][w] = max(p1, p2);
return dp[i][w];
}
int main() {
memset(dp, -1, sizeof dp);
scanf("%d %d\n", &n, &W);
for (int i = 1; i <= n; i++)
scanf("%d %d", &weight[i], &value[i]);
ll ans = func(1, 0);
printf("%lld\n", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
#define mx 105
int n, W;
int weight[mx], value[mx];
#define ll long long
ll dp[mx][100000 + mx];
ll func(int i, int w) {
if (i >= n + 1)
return 0;
if (dp[i][w] != -1)
return dp[i][w];
ll p1 = 0, p2 = 0;
if (w + weight[i] <= W) {
p1 = value[i] + func(i + 1, w + weight[i]);
}
p2 = func(i + 1, w);
dp[i][w] = max(p1, p2);
return dp[i][w];
}
int main() {
memset(dp, -1, sizeof dp);
scanf("%d %d\n", &n, &W);
for (int i = 1; i <= n; i++)
scanf("%d %d", &weight[i], &value[i]);
ll ans = func(1, 0);
printf("%lld\n", ans);
}
|
replace
| 8 | 9 | 8 | 9 |
0
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
#define MAXN 110
using namespace std;
long long v[MAXN], dp[MAXN][100010];
int p[MAXN], ct = 1;
map<long long, int> m;
int hsh(long long p) {
if (m[p] == 0)
return m[p] = ct++;
else
return m[p];
}
long long res(int i, int n, int w) {
if (i == n)
return 0;
if (dp[i][hsh(w)] != -1)
return dp[i][hsh(w)];
long long n_pega = res(i + 1, n, w), pega = 0;
if (w >= p[i]) {
pega = v[i] + res(i + 1, n, w - p[i]);
}
return dp[i][hsh(w)] = max(pega, n_pega);
}
int main() {
int n, w;
cin >> n >> w;
memset(dp, -1, sizeof(dp));
for (int i = 0; i < n; ++i) {
cin >> p[i] >> v[i];
}
cout << res(0, n, w) << endl;
// for(auto i : m){
// cout << i.first << ' ' << i.second << endl;
// }
// cout << endl;
}
|
#include <bits/stdc++.h>
#define MAXN 110
using namespace std;
long long v[MAXN], dp[MAXN][100010];
int p[MAXN], ct = 1;
unordered_map<long long, int> m;
int hsh(long long p) {
if (m[p] == 0)
return m[p] = ct++;
else
return m[p];
}
long long res(int i, int n, int w) {
if (i == n)
return 0;
if (dp[i][hsh(w)] != -1)
return dp[i][hsh(w)];
long long n_pega = res(i + 1, n, w), pega = 0;
if (w >= p[i]) {
pega = v[i] + res(i + 1, n, w - p[i]);
}
return dp[i][hsh(w)] = max(pega, n_pega);
}
int main() {
int n, w;
cin >> n >> w;
memset(dp, -1, sizeof(dp));
for (int i = 0; i < n; ++i) {
cin >> p[i] >> v[i];
}
cout << res(0, n, w) << endl;
// for(auto i : m){
// cout << i.first << ' ' << i.second << endl;
// }
// cout << endl;
}
|
replace
| 6 | 7 | 6 | 7 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
#include <iostream>
using namespace std;
#define MAX_N 100
#define MAX_W 100000
int N, W;
long long dp[MAX_N][MAX_W + 1];
int main() {
cin >> N >> W;
int w[N + 1], v[N + 1];
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]) {
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 <iostream>
using namespace std;
#define MAX_N 100
#define MAX_W 100000
int N, W;
long long dp[MAX_N + 1][MAX_W + 1];
int main() {
cin >> N >> W;
int w[N + 1], v[N + 1];
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]) {
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
| 5 | 6 | 5 | 6 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define SZ(items) (int)items.size()
#define CLR(a) memset(a, 0, sizeof(a))
#define SET(a) memset(a, -1, sizeof(a))
#define nl "\n";
int dx[] = {0, 0, 1, -1, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
template <class T> inline T biton(T n, T pos) { return n | ((T)1 << pos); }
template <class T> inline T bitoff(T n, T pos) { return n & ~((T)1 << pos); }
template <class T> inline T ison(T n, T pos) {
return (bool)(n & ((T)1 << pos));
}
template <class T> inline T gcd(T a, T b) {
while (b) {
a = a % b;
swap(a, b);
}
return a;
}
template <class T> inline T bigmod(T p, T e, T m) {
T ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1)
ret = (ret * p) % m;
p = (p * p) % m;
}
return (T)ret;
};
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " is " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " is " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define debug(...)
#endif
int dp[101][10005];
int n, tw;
vector<pair<long long, long long>> items;
long long recur(int pos, int w) {
if (pos == n) {
return 0;
}
if (dp[pos][w] != -1) {
return dp[pos][w];
}
long long mx = 0;
if (w + items[pos].first <= tw) {
mx = max(mx, items[pos].second + recur(pos + 1, w + items[pos].first));
}
mx = max(mx, recur(pos + 1, w));
return dp[pos][w] = mx;
}
void solve() {
SET(dp);
cin >> n >> tw;
items.resize(n);
for (int i = 0; i < n; i++) {
cin >> items[i].first >> items[i].second;
}
cout << recur(0, 0) << nl;
}
int main() {
ios_base::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(10);
cout.tie(nullptr);
cin.tie(nullptr);
int t = 1;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define SZ(items) (int)items.size()
#define CLR(a) memset(a, 0, sizeof(a))
#define SET(a) memset(a, -1, sizeof(a))
#define nl "\n";
int dx[] = {0, 0, 1, -1, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
template <class T> inline T biton(T n, T pos) { return n | ((T)1 << pos); }
template <class T> inline T bitoff(T n, T pos) { return n & ~((T)1 << pos); }
template <class T> inline T ison(T n, T pos) {
return (bool)(n & ((T)1 << pos));
}
template <class T> inline T gcd(T a, T b) {
while (b) {
a = a % b;
swap(a, b);
}
return a;
}
template <class T> inline T bigmod(T p, T e, T m) {
T ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1)
ret = (ret * p) % m;
p = (p * p) % m;
}
return (T)ret;
};
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " is " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " is " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define debug(...)
#endif
long long dp[101][100005];
int n, tw;
vector<pair<long long, long long>> items;
long long recur(int pos, int w) {
if (pos == n) {
return 0;
}
if (dp[pos][w] != -1) {
return dp[pos][w];
}
long long mx = 0;
if (w + items[pos].first <= tw) {
mx = max(mx, items[pos].second + recur(pos + 1, w + items[pos].first));
}
mx = max(mx, recur(pos + 1, w));
return dp[pos][w] = mx;
}
void solve() {
SET(dp);
cin >> n >> tw;
items.resize(n);
for (int i = 0; i < n; i++) {
cin >> items[i].first >> items[i].second;
}
cout << recur(0, 0) << nl;
}
int main() {
ios_base::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(10);
cout.tie(nullptr);
cin.tie(nullptr);
int t = 1;
while (t--) {
solve();
}
return 0;
}
|
replace
| 49 | 50 | 49 | 50 |
0
| |
p03163
|
C++
|
Runtime Error
|
#pragma GCC optimize("O3")
#pragma GCC target("avx")
// #include<bits/stdc++.h>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
const int cm = 1 << 11;
char cn[cm], *ci = cn, ct;
inline int getint() {
int A = 0;
while ((ct = *ci++) >= '0')
A = A * 10 + ct - '0';
return A;
}
int main() {
// cin.tie(0);
// ios::sync_with_stdio(false);
fread(cn, 1, cm, stdin);
int N = getint(), W = getint();
ll dp[100001] = {};
rep(i, N) {
int w = getint();
int v = getint();
for (int j = 0; j <= W - w; j--) {
chmax(dp[j], dp[j + w] + v);
}
}
printf("%lld", dp[0]);
Would you please return 0;
}
|
#pragma GCC optimize("O3")
#pragma GCC target("avx")
// #include<bits/stdc++.h>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
const int cm = 1 << 11;
char cn[cm], *ci = cn, ct;
inline int getint() {
int A = 0;
while ((ct = *ci++) >= '0')
A = A * 10 + ct - '0';
return A;
}
int main() {
// cin.tie(0);
// ios::sync_with_stdio(false);
fread(cn, 1, cm, stdin);
int N = getint(), W = getint();
ll dp[100001] = {};
rep(i, N) {
int w = getint();
int v = getint();
rep(j, W - w + 1) { chmax(dp[j], dp[j + w] + v); }
}
printf("%lld", dp[0]);
Would you please return 0;
}
|
replace
| 41 | 44 | 41 | 42 |
0
| |
p03163
|
C++
|
Runtime Error
|
/*
Author : Simanta Deb Turja
*/
#include <bits/stdc++.h>
using namespace std;
#define Professor
using ll = long long;
using i64 = unsigned long long;
template <typename T> inline T Min(T a, T b, T c) { return min(a, min(b, c)); }
template <typename T> inline T Min(T a, T b, T c, T d) {
return min(a, min(b, min(c, d)));
}
template <typename T> inline T Max(T a, T b, T c) { return max(a, max(b, c)); }
template <typename T> inline T Max(T a, T b, T c, T d) {
return max(a, max(b, max(c, d)));
}
template <typename T> inline T Ceil(T a, T b) {
return ((a % b == 0) ? (a / b) : (a / b + 1));
}
template <typename T> inline T Floor(T a, T b) { return a / b; }
template <typename T> inline T Power(T a, T p) {
T res = 1, x = a;
while (p) {
if (p & 1)
res = (res * x);
x = (x * x);
p >>= 1;
}
return res;
}
template <typename T> inline T BigMod(T a, T p, T M) {
a %= M;
T ret = 1;
while (p) {
if (p & 1)
ret = (ret * a) % M;
a = (a * a) % M;
p = p >> 1;
}
return ret;
}
template <typename T> inline T InverseMod(T a, T M) {
return BigMod(a, M - 2, M);
}
template <typename T> inline T gcd(T a, T b) {
a = abs(a);
b = abs(b);
while (b) {
a = a % b;
swap(a, b);
}
return a;
}
template <typename T> inline T lcm(T x, T y) {
return (((x) / gcd((x), (y))) * (y));
}
#define endl '\n'
#define eb emplace_back
const int N = (int)4e5 + 10;
const double EPS = 1e-7;
const double PI = acos(-1.0);
const ll LLINF = (ll)1e18 + 1;
const int INF = (int)1e9 + 1;
const ll MOD = (ll)1e9 + 7;
template <typename T> bool cmp(const pair<T, T> &a, const pair<T, T> &b) {
return a.first < b.first;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
auto Solve = [&]() {
#ifdef __APPLE__
cout << "Running\n";
#endif
int n, W;
cin >> n >> W;
vector<ll> w(n), v(n);
for (int i = 0; i < n; ++i) {
cin >> w[i] >> v[i];
}
vector<vector<ll>> dp(n, vector<ll>(W));
vector<vector<bool>> vis(n, vector<bool>(W, 0));
function<ll(int, int)> memo = [&](int pos, int cap) {
if (pos == n) {
return 0LL;
}
if (vis[pos][cap] != 0) {
return dp[pos][cap];
}
vis[pos][cap] = 1;
ll ret = -LLINF;
if (cap - w[pos] >= 0) {
ret = max(ret, memo(pos + 1, cap - w[pos]) + v[pos]);
}
ret = max(ret, memo(pos + 1, cap));
return dp[pos][cap] = ret;
};
cout << memo(0, W) << endl;
};
Solve();
return 0;
}
|
/*
Author : Simanta Deb Turja
*/
#include <bits/stdc++.h>
using namespace std;
#define Professor
using ll = long long;
using i64 = unsigned long long;
template <typename T> inline T Min(T a, T b, T c) { return min(a, min(b, c)); }
template <typename T> inline T Min(T a, T b, T c, T d) {
return min(a, min(b, min(c, d)));
}
template <typename T> inline T Max(T a, T b, T c) { return max(a, max(b, c)); }
template <typename T> inline T Max(T a, T b, T c, T d) {
return max(a, max(b, max(c, d)));
}
template <typename T> inline T Ceil(T a, T b) {
return ((a % b == 0) ? (a / b) : (a / b + 1));
}
template <typename T> inline T Floor(T a, T b) { return a / b; }
template <typename T> inline T Power(T a, T p) {
T res = 1, x = a;
while (p) {
if (p & 1)
res = (res * x);
x = (x * x);
p >>= 1;
}
return res;
}
template <typename T> inline T BigMod(T a, T p, T M) {
a %= M;
T ret = 1;
while (p) {
if (p & 1)
ret = (ret * a) % M;
a = (a * a) % M;
p = p >> 1;
}
return ret;
}
template <typename T> inline T InverseMod(T a, T M) {
return BigMod(a, M - 2, M);
}
template <typename T> inline T gcd(T a, T b) {
a = abs(a);
b = abs(b);
while (b) {
a = a % b;
swap(a, b);
}
return a;
}
template <typename T> inline T lcm(T x, T y) {
return (((x) / gcd((x), (y))) * (y));
}
#define endl '\n'
#define eb emplace_back
const int N = (int)4e5 + 10;
const double EPS = 1e-7;
const double PI = acos(-1.0);
const ll LLINF = (ll)1e18 + 1;
const int INF = (int)1e9 + 1;
const ll MOD = (ll)1e9 + 7;
template <typename T> bool cmp(const pair<T, T> &a, const pair<T, T> &b) {
return a.first < b.first;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
auto Solve = [&]() {
#ifdef __APPLE__
cout << "Running\n";
#endif
int n, W;
cin >> n >> W;
vector<ll> w(n), v(n);
for (int i = 0; i < n; ++i) {
cin >> w[i] >> v[i];
}
vector<vector<ll>> dp(n, vector<ll>(W + 1));
vector<vector<bool>> vis(n, vector<bool>(W + 1, false));
function<ll(int, int)> memo = [&](int pos, int cap) {
if (pos == n) {
return 0LL;
}
if (vis[pos][cap] != 0) {
return dp[pos][cap];
}
vis[pos][cap] = 1;
ll ret = -LLINF;
if (cap - w[pos] >= 0) {
ret = max(ret, memo(pos + 1, cap - w[pos]) + v[pos]);
}
ret = max(ret, memo(pos + 1, cap));
return dp[pos][cap] = ret;
};
cout << memo(0, W) << endl;
};
Solve();
return 0;
}
|
replace
| 91 | 93 | 91 | 93 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <fstream>
#include <iostream>
#include <math.h>
#include <set>
#include <string>
#include <vector>
using namespace ::std;
#define int long long
vector<int> v;
signed main() {
int n, k;
cin >> n >> k;
v.resize(k + 1, -1);
v[0] = 0;
int f = 0;
int ans = 0;
for (int i = 0; i < n; i++) {
int w;
int q;
cin >> w >> q;
for (int j = f; j >= 0; j--) {
if (v[j] == -1)
continue;
if (j + w > q)
continue;
v[w + j] = max(v[w + j], v[j] + q);
f = max(f, w + j);
ans = max(ans, v[w + j]);
}
}
cout << ans;
}
// aaaddda
|
#include <algorithm>
#include <fstream>
#include <iostream>
#include <math.h>
#include <set>
#include <string>
#include <vector>
using namespace ::std;
#define int long long
vector<int> v;
signed main() {
int n, k;
cin >> n >> k;
v.resize(k + 1, -1);
v[0] = 0;
int f = 0;
int ans = 0;
for (int i = 0; i < n; i++) {
int w;
int q;
cin >> w >> q;
for (int j = f; j >= 0; j--) {
if (v[j] == -1)
continue;
if (j + w > k)
continue;
v[w + j] = max(v[w + j], v[j] + q);
f = max(f, w + j);
ans = max(ans, v[w + j]);
}
}
cout << ans;
}
// aaaddda
|
replace
| 25 | 26 | 25 | 26 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
long long int n, W, i, j, weight[1005], val[1005], mat[100][100005];
int main() {
cin >> n >> W;
for (i = 0; i < n; i++) {
cin >> weight[i] >> val[i];
}
for (i = 0; i <= W; i++)
mat[0][i] = 0;
for (i = 0; i <= n; i++)
mat[i][0] = 0;
for (i = 1; i <= n; i++) {
for (j = 1; j <= W; j++) {
if (weight[i - 1] <= j) {
mat[i][j] =
max(mat[i - 1][j], val[i - 1] + mat[i - 1][j - weight[i - 1]]);
} else {
mat[i][j] = mat[i - 1][j];
}
}
}
cout << mat[n][W] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int n, W, i, j, weight[1005], val[1005], mat[105][100005];
int main() {
cin >> n >> W;
for (i = 0; i < n; i++) {
cin >> weight[i] >> val[i];
}
for (i = 0; i <= W; i++)
mat[0][i] = 0;
for (i = 0; i <= n; i++)
mat[i][0] = 0;
for (i = 1; i <= n; i++) {
for (j = 1; j <= W; j++) {
if (weight[i - 1] <= j) {
mat[i][j] =
max(mat[i - 1][j], val[i - 1] + mat[i - 1][j - weight[i - 1]]);
} else {
mat[i][j] = mat[i - 1][j];
}
}
}
cout << mat[n][W] << endl;
}
|
replace
| 3 | 4 | 3 | 4 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
struct wow {
int x, y;
} v[105];
long long din[10005], n, val, maxim, j, i;
int main() {
cin >> n >> val;
for (i = 1; i <= n; i++) {
cin >> v[i].x >> v[i].y;
}
for (i = 1; i <= val; i++) {
din[i] = -1;
}
for (i = 1; i <= n; i++) {
for (j = val; j >= v[i].x; j--) {
if (din[j - v[i].x] != -1) {
din[j] = max(din[j], din[j - v[i].x] + v[i].y);
}
}
}
for (j = val; j >= 1; j--) {
maxim = max(maxim, din[j]);
}
cout << maxim;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct wow {
int x, y;
} v[105];
long long din[100005], n, val, maxim, j, i;
int main() {
cin >> n >> val;
for (i = 1; i <= n; i++) {
cin >> v[i].x >> v[i].y;
}
for (i = 1; i <= val; i++) {
din[i] = -1;
}
for (i = 1; i <= n; i++) {
for (j = val; j >= v[i].x; j--) {
if (din[j - v[i].x] != -1) {
din[j] = max(din[j], din[j - v[i].x] + v[i].y);
}
}
}
for (j = val; j >= 1; j--) {
maxim = max(maxim, din[j]);
}
cout << maxim;
return 0;
}
|
replace
| 6 | 7 | 6 | 7 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <iostream>
using namespace std;
int w[110], v[110];
long long int dp[110][100345];
int N, W;
int main() {
cin >> N >> W;
for (int i = 0; i < N; i++)
for (int j = 0; j <= W; j++)
dp[i][j] = -1;
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 = 0; i < N; i++) {
for (int we = 0; we <= W; we++) {
if (dp[i][we - w[i]] >= 0 && we >= w[i]) {
dp[i + 1][we] = max(dp[i][we - w[i]] + v[i], dp[i][we]);
} else
dp[i + 1][we] = dp[i][we];
}
}
long long int ans = 0;
for (int i = 0; i <= W; i++)
ans = max(ans, dp[N][i]);
cout << ans << endl;
return 0;
}
|
#include <iostream>
using namespace std;
int w[110], v[110];
long long int dp[110][100345];
int N, W;
int main() {
cin >> N >> W;
for (int i = 0; i < N; i++)
for (int j = 0; j <= W; j++)
dp[i][j] = -1;
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 = 0; i < N; i++) {
for (int we = 0; we <= W; we++) {
if (w[i] > we)
dp[i + 1][we] = dp[i][we];
if (dp[i][we - w[i]] >= 0 && w[i] <= we) {
dp[i + 1][we] = max(dp[i][we - w[i]] + v[i], dp[i][we]);
} else
dp[i + 1][we] = dp[i][we];
}
}
long long int ans = 0;
for (int i = 0; i <= W; i++)
ans = max(ans, dp[N][i]);
cout << ans << endl;
return 0;
}
|
replace
| 19 | 20 | 19 | 22 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define MOD 1000000007
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define f(i, a, n) for (int i = a; i < n; i++)
using namespace std;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<string> vs;
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// int t; cin>>t; while(t--) {
int n, w;
cin >> n >> w;
vll dp(w + 1, 0);
f(items, 0, n) {
int weight, value;
cin >> weight >> value;
for (int weight_already = w - weight; weight_already >= 0; weight_already--)
dp[weight_already + weight] =
max(dp[weight_already + weight], dp[weight_already] + value);
}
ll ans = 0;
f(i, 0, w + 1) ans = max(ans, dp[i]);
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define MOD 1000000007
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define f(i, a, n) for (int i = a; i < n; i++)
using namespace std;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<string> vs;
int main() {
/*#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin) ;
freopen("output.txt", "w", stdout) ;
#endif*/
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// int t; cin>>t; while(t--) {
int n, w;
cin >> n >> w;
vll dp(w + 1, 0);
f(items, 0, n) {
int weight, value;
cin >> weight >> value;
for (int weight_already = w - weight; weight_already >= 0; weight_already--)
dp[weight_already + weight] =
max(dp[weight_already + weight], dp[weight_already] + value);
}
ll ans = 0;
f(i, 0, w + 1) ans = max(ans, dp[i]);
cout << ans;
return 0;
}
|
replace
| 24 | 28 | 24 | 28 |
-6
|
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p03163
|
C++
|
Runtime Error
|
#include <iostream>
using namespace std;
const int maxn = 13010;
typedef long long ll;
ll n, W, w[maxn], v[maxn], f[maxn];
int main() {
cin >> n >> W;
for (int i = 1; i <= n; i++)
cin >> w[i] >> v[i];
for (int i = 1; i <= n; i++)
for (ll l = W; l >= w[i]; l--)
if (f[l - w[i]] + v[i] > f[l])
f[l] = f[l - w[i]] + v[i];
cout << f[W];
return 0;
}
|
#include <iostream>
using namespace std;
const int maxn = 1e5 + 5;
typedef long long ll;
ll n, W, w[maxn], v[maxn], f[maxn];
int main() {
cin >> n >> W;
for (int i = 1; i <= n; i++)
cin >> w[i] >> v[i];
for (int i = 1; i <= n; i++)
for (ll l = W; l >= w[i]; l--)
if (f[l - w[i]] + v[i] > f[l])
f[l] = f[l - w[i]] + v[i];
cout << f[W];
return 0;
}
|
replace
| 2 | 3 | 2 | 3 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll dp[100][100002];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll n, w;
cin >> n >> w;
for (int i = 1; i <= w; i++)
dp[0][i] = -1ll;
dp[0][0] = 0ll;
for (ll i = 1; i <= n; i++) {
ll hz, zh;
cin >> hz >> zh;
for (ll j = 1; j <= w; j++) {
dp[i][j] = dp[i - 1][j];
if (j - hz >= 0)
dp[i][j] = max(dp[i][j], dp[i - 1][j - hz] + zh);
}
}
ll ax = 0ll;
for (ll i = 1; i <= w; i++)
ax = max(dp[n][i], ax);
cout << ax;
}
|
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll dp[102][100002];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll n, w;
cin >> n >> w;
for (int i = 1; i <= w; i++)
dp[0][i] = -1ll;
dp[0][0] = 0ll;
for (ll i = 1; i <= n; i++) {
ll hz, zh;
cin >> hz >> zh;
for (ll j = 1; j <= w; j++) {
dp[i][j] = dp[i - 1][j];
if (j - hz >= 0)
dp[i][j] = max(dp[i][j], dp[i - 1][j - hz] + zh);
}
}
ll ax = 0ll;
for (ll i = 1; i <= w; i++)
ax = max(dp[n][i], ax);
cout << ax;
}
|
replace
| 3 | 4 | 3 | 4 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
vector<int> weight;
vector<int> value;
long long ans[101][100000];
int main() {
int N, W, w, v;
cin >> N >> W;
weight.resize(N + 1, 0);
value.resize(N + 1, 0);
for (int i = 0; i < N; ++i) {
cin >> w >> v;
weight[i] = w;
value[i] = v;
}
for (int i = 0; i <= N; ++i) {
for (int j = 0; j <= W; ++j) {
if (i == 0 || j == 0) {
ans[i][j] = 0;
} else if (weight[i - 1] <= j) {
ans[i][j] =
max(value[i - 1] + ans[i - 1][j - weight[i - 1]], ans[i - 1][j]);
} else {
ans[i][j] = ans[i - 1][j];
}
}
}
cout << ans[N][W] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
vector<int> weight;
vector<int> value;
long long ans[101][100001];
int main() {
int N, W, w, v;
cin >> N >> W;
weight.resize(N + 1, 0);
value.resize(N + 1, 0);
for (int i = 0; i < N; ++i) {
cin >> w >> v;
weight[i] = w;
value[i] = v;
}
for (int i = 0; i <= N; ++i) {
for (int j = 0; j <= W; ++j) {
if (i == 0 || j == 0) {
ans[i][j] = 0;
} else if (weight[i - 1] <= j) {
ans[i][j] =
max(value[i - 1] + ans[i - 1][j - weight[i - 1]], ans[i - 1][j]);
} else {
ans[i][j] = ans[i - 1][j];
}
}
}
cout << ans[N][W] << endl;
return 0;
}
|
replace
| 5 | 6 | 5 | 6 |
0
| |
p03163
|
C++
|
Memory Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define si set<int>
#define vi vector<int>
#define pii pair<int, int>
#define vpii vector<pii>
#define vpp vector<pair<int, pii>>
#define mii map<int, int>
#define mpi map<pii, int>
#define msi map<string, int>
#define loop(i, s, e) for (int i = s; i < e; i++)
#define rloop(i, e, s) for (int i = e; i >= s; i--)
#define mset(a, f) memset(a, f, sizeof(a))
#define spi set<pii>
#define endl "\n"
#define sz(x) ((int)x.size())
#define all(p) p.begin(), p.end()
#define que_max priority_queue<int>
#define que_min priority_queue<int, vi, greater<int>>
#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)
#define print(a) \
for (auto x : a) \
cout << x << " "; \
cout << endl
#define Print(a, x, y) \
for (int i = x; i < y; i++) \
cout << a[i] << " "; \
cout << endl
inline int power(int a, int b) {
int x = 1;
while (b) {
if (b & 1)
x *= a;
a *= a;
b >>= 1;
}
return x;
}
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
const int N = 2000005;
int v[N], w[N], n, maxwt;
int dp[105][N];
int help(int i, int wt) {
if (i >= n)
return 0;
if (dp[i][wt] != -1)
return dp[i][wt];
int op1 = -1, op2 = -1;
if (w[i] <= wt)
op1 = v[i] + help(i + 1, wt - w[i]);
op2 = help(i + 1, wt);
return dp[i][wt] = max(op1, op2);
}
void solve() {
cin >> n >> maxwt;
loop(i, 0, n) cin >> w[i] >> v[i];
mset(dp, -1);
cout << help(0, maxwt);
return;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define si set<int>
#define vi vector<int>
#define pii pair<int, int>
#define vpii vector<pii>
#define vpp vector<pair<int, pii>>
#define mii map<int, int>
#define mpi map<pii, int>
#define msi map<string, int>
#define loop(i, s, e) for (int i = s; i < e; i++)
#define rloop(i, e, s) for (int i = e; i >= s; i--)
#define mset(a, f) memset(a, f, sizeof(a))
#define spi set<pii>
#define endl "\n"
#define sz(x) ((int)x.size())
#define all(p) p.begin(), p.end()
#define que_max priority_queue<int>
#define que_min priority_queue<int, vi, greater<int>>
#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)
#define print(a) \
for (auto x : a) \
cout << x << " "; \
cout << endl
#define Print(a, x, y) \
for (int i = x; i < y; i++) \
cout << a[i] << " "; \
cout << endl
inline int power(int a, int b) {
int x = 1;
while (b) {
if (b & 1)
x *= a;
a *= a;
b >>= 1;
}
return x;
}
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
const int N = 2000005;
int v[N], w[N], n, maxwt;
int dp[105][100005];
int help(int i, int wt) {
if (i >= n)
return 0;
if (dp[i][wt] != -1)
return dp[i][wt];
int op1 = -1, op2 = -1;
if (w[i] <= wt)
op1 = v[i] + help(i + 1, wt - w[i]);
op2 = help(i + 1, wt);
return dp[i][wt] = max(op1, op2);
}
void solve() {
cin >> n >> maxwt;
loop(i, 0, n) cin >> w[i] >> v[i];
mset(dp, -1);
cout << help(0, maxwt);
return;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
|
replace
| 59 | 60 | 59 | 60 |
MLE
| |
p03163
|
C++
|
Runtime Error
|
#include <iostream>
#define endl '\n'
using namespace std;
const int MAXN = 103, MAXW = 1e5 + 3;
long long cost[MAXN], dp[MAXN][MAXN];
int weight[MAXN];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, w;
cin >> n >> w;
for (int i = 1; i <= n; ++i) {
cin >> weight[i] >> cost[i];
}
for (int i = 1; i <= n; ++i) {
for (int i2 = 1; i2 <= w; ++i2) {
if (weight[i] > i2) {
dp[i][i2] = dp[i - 1][i2];
} else {
dp[i][i2] = max(cost[i] + dp[i - 1][i2 - weight[i]], dp[i - 1][i2]);
}
}
}
cout << dp[n][w] << endl;
return 0;
}
|
#include <iostream>
#define endl '\n'
using namespace std;
const int MAXN = 103, MAXW = 1e5 + 3;
long long cost[MAXN], dp[MAXN][MAXW];
int weight[MAXN];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, w;
cin >> n >> w;
for (int i = 1; i <= n; ++i) {
cin >> weight[i] >> cost[i];
}
for (int i = 1; i <= n; ++i) {
for (int i2 = 1; i2 <= w; ++i2) {
if (weight[i] > i2) {
dp[i][i2] = dp[i - 1][i2];
} else {
dp[i][i2] = max(cost[i] + dp[i - 1][i2 - weight[i]], dp[i - 1][i2]);
}
}
}
cout << dp[n][w] << endl;
return 0;
}
|
replace
| 7 | 8 | 7 | 8 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long unsigned int ll;
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define ALL(x) (x).begin(), (x).end()
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;
}
ll dp[101][10001] = {};
int main() {
int N, W;
cin >> N;
cin >> W;
int w[N], v[N];
REP(i, N) {
cin >> w[i];
cin >> v[i];
}
REPS(i, N) {
REP(j, W + 1) {
if (j - w[i - 1] >= 0) {
chmax(dp[i][j], dp[i - 1][j - w[i - 1]] + v[i - 1]);
}
chmax(dp[i][j], dp[i - 1][j]);
}
}
ll res = 0;
REP(i, W + 1) { chmax(res, dp[N][i]); }
cout << res;
}
|
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long unsigned int ll;
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define ALL(x) (x).begin(), (x).end()
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;
}
ll dp[101][100001] = {};
int main() {
int N, W;
cin >> N;
cin >> W;
int w[N], v[N];
REP(i, N) {
cin >> w[i];
cin >> v[i];
}
REPS(i, N) {
REP(j, W + 1) {
if (j - w[i - 1] >= 0) {
chmax(dp[i][j], dp[i - 1][j - w[i - 1]] + v[i - 1]);
}
chmax(dp[i][j], dp[i - 1][j]);
}
}
ll res = 0;
REP(i, W + 1) { chmax(res, dp[N][i]); }
cout << res;
}
|
replace
| 41 | 42 | 41 | 42 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define MAX_W 100010
#define MAX 100
long long dp[MAX][MAX_W];
long long weight[MAX], value[MAX];
int main() {
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX_W; j++) {
dp[i][j] = 0;
}
}
int n, w;
cin >> n >> w;
for (int i = 0; i < n; i++) {
cin >> weight[i] >> value[i];
}
for (int i = 0; i < n; i++) {
for (int W = 0; W <= w; W++) {
if (W - weight[i] >= 0) {
dp[i + 1][W] = max(dp[i + 1][W], dp[i][W - weight[i]] + value[i]);
}
dp[i + 1][W] = max(dp[i + 1][W], dp[i][W]);
}
}
// for(int i=0;i<=n;i++){
// for(int j=0;j<=w;j++)printf("%3d ",dp[i][j]);
// cout<<endl;
// }
cout << dp[n][w] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define MAX_W 100010
#define MAX 110
long long dp[MAX][MAX_W];
long long weight[MAX], value[MAX];
int main() {
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX_W; j++) {
dp[i][j] = 0;
}
}
int n, w;
cin >> n >> w;
for (int i = 0; i < n; i++) {
cin >> weight[i] >> value[i];
}
for (int i = 0; i < n; i++) {
for (int W = 0; W <= w; W++) {
if (W - weight[i] >= 0) {
dp[i + 1][W] = max(dp[i + 1][W], dp[i][W - weight[i]] + value[i]);
}
dp[i + 1][W] = max(dp[i + 1][W], dp[i][W]);
}
}
// for(int i=0;i<=n;i++){
// for(int j=0;j<=w;j++)printf("%3d ",dp[i][j]);
// cout<<endl;
// }
cout << dp[n][w] << endl;
return 0;
}
|
replace
| 4 | 5 | 4 | 5 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
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;
}
typedef long long ll;
const long long INF = 1LL << 60;
long long dp[10010][110];
int main() {
int n, w;
cin >> n >> w;
vector<int> h(n), v(n);
for (int i = 0; i < n; i++) {
cin >> h[i] >> v[i];
}
for (int i = 0; i <= w; i++) {
for (int j = 0; j <= n; j++) {
dp[i][j] = 0;
}
}
for (int i = 1; i <= w; i++) {
for (int j = 1; j <= n; j++) {
if (i - h.at(j - 1) >= 0)
chmax(dp[i][j], (ll)(dp[i - h.at(j - 1)][j - 1] + v.at(j - 1)));
chmax(dp[i][j], dp[i][j - 1]);
}
}
cout << dp[w][n] << endl;
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
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;
}
typedef long long ll;
const long long INF = 1LL << 60;
ll dp[100100][110];
int main() {
int n, w;
cin >> n >> w;
vector<int> h(n), v(n);
for (int i = 0; i < n; i++) {
cin >> h[i] >> v[i];
}
for (int i = 0; i <= w; i++) {
for (int j = 0; j <= n; j++) {
dp[i][j] = 0;
}
}
for (int i = 1; i <= w; i++) {
for (int j = 1; j <= n; j++) {
if (i - h.at(j - 1) >= 0)
chmax(dp[i][j], (ll)(dp[i - h.at(j - 1)][j - 1] + v.at(j - 1)));
chmax(dp[i][j], dp[i][j - 1]);
}
}
cout << dp[w][n] << endl;
return 0;
}
|
replace
| 25 | 26 | 25 | 26 |
0
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
#include <stack>
using namespace std;
typedef long long ll;
typedef long double lxd;
#define INF 1000000007
#define mem(dp, a) memset(dp, a, sizeof dp)
#define flash \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define repb(i, a, b) for (ll i = a; i >= b; i--)
#define f(i, n) for (ll i = 0; i < n; i++)
#define fo(i, x, n) for (ll i = x; i <= n; i++)
#define pb(x) push_back(x)
#define tr(c, it) for (((c).begin())it = (c).begin(); it != (c).end(); it++)
#define test \
ll t; \
cin >> t;
#define fls fflush(stdout);
ll fn(ll w[], ll v[], ll sw, ll idx) {
if (idx < 0)
return 0;
if (sw < w[idx])
return fn(w, v, sw, idx - 1);
else
return max(v[idx] + fn(w, v, sw - w[idx], idx - 1), fn(w, v, sw, idx - 1));
}
int main() {
ll n, sw;
cin >> n >> sw;
ll w[n], v[n];
for (ll i = 0; i < n; i++)
cin >> w[i] >> v[i];
ll ans = fn(w, v, sw, n - 1);
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
#include <stack>
using namespace std;
typedef long long ll;
typedef long double lxd;
#define INF 1000000007
#define mem(dp, a) memset(dp, a, sizeof dp)
#define flash \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define repb(i, a, b) for (ll i = a; i >= b; i--)
#define f(i, n) for (ll i = 0; i < n; i++)
#define fo(i, x, n) for (ll i = x; i <= n; i++)
#define pb(x) push_back(x)
#define tr(c, it) for (((c).begin())it = (c).begin(); it != (c).end(); it++)
#define test \
ll t; \
cin >> t;
#define fls fflush(stdout);
ll fn(ll w[], ll v[], ll sw, ll idx) {
if (idx < 0)
return 0;
if (sw < w[idx])
return fn(w, v, sw, idx - 1);
else
return max(v[idx] + fn(w, v, sw - w[idx], idx - 1), fn(w, v, sw, idx - 1));
}
int main() {
ll n, sw;
cin >> n >> sw;
ll w[n], v[n];
for (ll i = 0; i < n; i++)
cin >> w[i] >> v[i];
ll dp[n + 1][sw + 1];
memset(dp, 0, sizeof(dp));
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= sw; j++) {
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];
}
}
ll mx = dp[n][sw];
cout << mx << endl;
// ll ans=fn(w,v,sw,n-1);
// cout<<ans<<endl;
/* for(ll i=0;i<=n;i++)
{
for(ll j=0;j<=sw;j++)
{
cout<<dp[i][j];
}cout<<endl;
}*/
return 0;
}
|
replace
| 34 | 36 | 34 | 56 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define mp make_pair
#define S second
#define F first
ll mod = 1e9 + 7;
#define input_from_file freopen("input.txt", "r", stdin);
int main() {
input_from_file;
// int t; cin>>t;
// while(t--){
ll n, sum;
cin >> n >> sum;
vector<vector<ll>> vec(n, vector<ll>(2, 0));
for (ll i = 0; i < n; i++) {
for (ll j = 0; j < 2; j++)
cin >> vec[i][j];
}
vector<vector<ll>> dp(n + 1, vector<ll>(sum + 1, 0));
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= sum; j++) {
if (j < vec[i - 1][0])
dp[i][j] = dp[i - 1][j];
else {
dp[i][j] =
max(dp[i - 1][j], dp[i - 1][j - vec[i - 1][0]] + vec[i - 1][1]);
}
}
}
// for(ll i=0;i<=n;i++){
// for(ll j=0;j<=sum;j++) cout<<dp[i][j]<<" ";cout<<endl;}
cout << dp[n][sum];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define mp make_pair
#define S second
#define F first
ll mod = 1e9 + 7;
#define input_from_file freopen("input.txt", "r", stdin);
int main() {
// input_from_file;
// int t; cin>>t;
// while(t--){
ll n, sum;
cin >> n >> sum;
vector<vector<ll>> vec(n, vector<ll>(2, 0));
for (ll i = 0; i < n; i++) {
for (ll j = 0; j < 2; j++)
cin >> vec[i][j];
}
vector<vector<ll>> dp(n + 1, vector<ll>(sum + 1, 0));
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= sum; j++) {
if (j < vec[i - 1][0])
dp[i][j] = dp[i - 1][j];
else {
dp[i][j] =
max(dp[i - 1][j], dp[i - 1][j - vec[i - 1][0]] + vec[i - 1][1]);
}
}
}
// for(ll i=0;i<=n;i++){
// for(ll j=0;j<=sum;j++) cout<<dp[i][j]<<" ";cout<<endl;}
cout << dp[n][sum];
return 0;
}
|
replace
| 10 | 11 | 10 | 11 |
-6
|
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p03163
|
C++
|
Runtime Error
|
#include <cstdio>
#include <iostream>
using namespace std;
struct item {
int v;
int w;
} a[101];
long long f[10001];
int main() {
int N, W;
scanf("%d%d", &N, &W);
for (int i = 1; i <= N; ++i)
scanf("%d%d", &a[i].w, &a[i].v);
for (int i = 1; i <= W; ++i) {
if (a[1].w <= i)
f[i] = a[1].v;
}
for (int i = 2; i <= N; ++i) {
for (int j = W; j >= 0; --j) {
if (j >= a[i].w)
f[j] = max(f[j], f[j - a[i].w] + a[i].v);
}
}
cout << f[W] << endl;
return 0;
}
|
#include <cstdio>
#include <iostream>
using namespace std;
struct item {
int v;
int w;
} a[101];
long long f[100001];
int main() {
int N, W;
scanf("%d%d", &N, &W);
for (int i = 1; i <= N; ++i)
scanf("%d%d", &a[i].w, &a[i].v);
for (int i = 1; i <= W; ++i) {
if (a[1].w <= i)
f[i] = a[1].v;
}
for (int i = 2; i <= N; ++i) {
for (int j = W; j >= 0; --j) {
if (j >= a[i].w)
f[j] = max(f[j], f[j - a[i].w] + a[i].v);
}
}
cout << f[W] << endl;
return 0;
}
|
replace
| 9 | 10 | 9 | 10 |
0
| |
p03163
|
C++
|
Runtime Error
|
// define DEBUG for debug
// #define DEBUG
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define MAX 100000
#define MOD 1000000007
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll n, w;
pll arr[100];
ll dp[100][MAX];
ll dfs(ll i, ll wei) {
if (dp[i][wei] == 0) {
if (i <= n) {
if (wei + arr[i].F <= w)
dp[i][wei] = dfs(i + 1, wei + arr[i].F) + arr[i].S;
dp[i][wei] = max(dp[i][wei], dfs(i + 1, wei));
} else
return 0;
}
return dp[i][wei];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifdef DEBUG
freopen("i.txt", "r", stdin);
freopen("o.txt", "w", stdout);
#endif
cin >> n >> w;
for (ll i = 0; i < n; i++)
cin >> arr[i].F >> arr[i].S;
cout << dfs(0, 0);
return 0;
}
|
// define DEBUG for debug
// #define DEBUG
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define MAX 100000
#define MOD 1000000007
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll n, w;
pll arr[100 + 100];
ll dp[100 + 100][MAX + 100];
ll dfs(ll i, ll wei) {
if (dp[i][wei] == 0) {
if (i <= n) {
if (wei + arr[i].F <= w)
dp[i][wei] = dfs(i + 1, wei + arr[i].F) + arr[i].S;
dp[i][wei] = max(dp[i][wei], dfs(i + 1, wei));
} else
return 0;
}
return dp[i][wei];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifdef DEBUG
freopen("i.txt", "r", stdin);
freopen("o.txt", "w", stdout);
#endif
cin >> n >> w;
for (ll i = 0; i < n; i++)
cin >> arr[i].F >> arr[i].S;
cout << dfs(0, 0);
return 0;
}
|
replace
| 15 | 17 | 15 | 17 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
long long w[100], v[100], MaxSum[100][100000];
int N;
long long W;
int main() {
scanf("%d %llu", &N, &W);
for (int i = 1; i <= N; i++) {
scanf("%llu %llu", &w[i], &v[i]);
}
for (int i = 1; i <= N; i++) {
for (int j = 0; j <= W; j++) {
MaxSum[i][j] = MaxSum[i - 1][j];
if (j >= w[i]) {
MaxSum[i][j] = max(MaxSum[i][j], MaxSum[i - 1][j - w[i]] + v[i]);
}
}
}
printf("%llu", MaxSum[N][W]);
}
|
#include <bits/stdc++.h>
using namespace std;
long long w[200], v[200], MaxSum[200][200000];
int N;
long long W;
int main() {
scanf("%d %llu", &N, &W);
for (int i = 1; i <= N; i++) {
scanf("%llu %llu", &w[i], &v[i]);
}
for (int i = 1; i <= N; i++) {
for (int j = 0; j <= W; j++) {
MaxSum[i][j] = MaxSum[i - 1][j];
if (j >= w[i]) {
MaxSum[i][j] = max(MaxSum[i][j], MaxSum[i - 1][j - w[i]] + v[i]);
}
}
}
printf("%llu", MaxSum[N][W]);
}
|
replace
| 4 | 5 | 4 | 5 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
freopen("test.inp", "r", stdin);
long long int n, i, j;
long long int w;
cin >> n >> w;
long long int m[n + 10], v[n + 10];
long long int f[n + 10][w + 10];
for (i = 1; i <= n; i++) {
cin >> m[i] >> v[i];
}
for (j = 0; j <= w; j++) {
f[0][j] = 0;
}
for (i = 1; i <= n; i++) {
f[i][0] = 0;
}
for (i = 1; i <= n; i++) {
for (j = 0; j <= w; j++) {
if (m[i] > j) {
f[i][j] = f[i - 1][j];
} else {
f[i][j] = max((f[i - 1][j - m[i]] + v[i]), (f[i - 1][j]));
}
}
}
cout << f[n][w];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("test.inp","r",stdin);
long long int n, i, j;
long long int w;
cin >> n >> w;
long long int m[n + 10], v[n + 10];
long long int f[n + 10][w + 10];
for (i = 1; i <= n; i++) {
cin >> m[i] >> v[i];
}
for (j = 0; j <= w; j++) {
f[0][j] = 0;
}
for (i = 1; i <= n; i++) {
f[i][0] = 0;
}
for (i = 1; i <= n; i++) {
for (j = 0; j <= w; j++) {
if (m[i] > j) {
f[i][j] = f[i - 1][j];
} else {
f[i][j] = max((f[i - 1][j - m[i]] + v[i]), (f[i - 1][j]));
}
}
}
cout << f[n][w];
return 0;
}
|
replace
| 8 | 9 | 8 | 9 |
-11
| |
p03163
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
long long KnapSack(int W, vector<int> wt, vector<int> val, int n) {
if (n == 0 || W == 0)
return 0;
if (wt[n - 1] > W)
return KnapSack(W, wt, val, n - 1);
return max(val[n - 1] + KnapSack(W - wt[n - 1], wt, val, n - 1),
KnapSack(W, wt, val, n - 1));
}
int main() {
int n, W;
cin >> n >> W;
vector<int> wt(n), val(n);
for (int i = 0; i < n; i++)
cin >> wt[i] >> val[i];
cout << KnapSack(W, wt, val, n);
}
|
#include <bits/stdc++.h>
using namespace std;
long long KnapSack(int W, vector<int> wt, vector<int> val, int n) {
vector<vector<long>> dp(n + 1, vector<long>(W + 1));
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= W; j++) {
if (i == 0 || j == 0)
dp[i][j] = 0;
else if (wt[i - 1] <= j)
dp[i][j] = max(val[i - 1] + dp[i - 1][j - wt[i - 1]], dp[i - 1][j]);
else
dp[i][j] = dp[i - 1][j];
}
}
return dp[n][W];
}
int main() {
int n, W;
cin >> n >> W;
vector<int> wt(n), val(n);
for (int i = 0; i < n; i++)
cin >> wt[i] >> val[i];
cout << KnapSack(W, wt, val, n);
}
|
replace
| 4 | 10 | 4 | 16 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
using LL = long long int;
using uLL = unsigned long long int;
using uint = unsigned int;
using ld = long double;
const int W = 200007;
const int N = 207;
LL w[N], v[N];
LL dp[N];
LL czy[N];
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(0);
int n, WW;
cin >> n >> WW;
for (int i = 0; i < n; i++) {
cin >> w[i] >> v[i];
}
czy[0] = 1;
for (int i = 0; i < n; i++) {
for (int j = WW; j >= w[i]; j--) {
dp[j] = max(dp[j], czy[j - w[i]] * (dp[j - w[i]] + v[i]));
czy[j] = max(czy[j - w[i]], czy[j]);
}
}
LL res = 0;
for (int i = 1; i <= WW; i++) {
res = max(dp[i], res);
}
cout << res << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using LL = long long int;
using uLL = unsigned long long int;
using uint = unsigned int;
using ld = long double;
const int W = 200007;
const int N = 207;
LL w[N], v[N];
LL dp[W];
LL czy[W];
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(0);
int n, WW;
cin >> n >> WW;
for (int i = 0; i < n; i++) {
cin >> w[i] >> v[i];
}
czy[0] = 1;
for (int i = 0; i < n; i++) {
for (int j = WW; j >= w[i]; j--) {
dp[j] = max(dp[j], czy[j - w[i]] * (dp[j - w[i]] + v[i]));
czy[j] = max(czy[j - w[i]], czy[j]);
}
}
LL res = 0;
for (int i = 1; i <= WW; i++) {
res = max(dp[i], res);
}
cout << res << '\n';
return 0;
}
|
replace
| 12 | 14 | 12 | 14 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#include <dirent.h>
#include <stdlib.h>
using namespace std;
#define sp << " " <<
typedef vector<int> vi;
typedef vector<long int> vli;
typedef vector<long long int> vlli;
typedef long int li;
typedef long long int lli;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<lli, lli>> vplli;
typedef unordered_map<int, int> ump;
int main() {
int n, w;
cin >> n >> w;
vector<pair<long long int, int>> arr;
// vector<int> value;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
arr.push_back({b, a});
}
// sort(arr.begin(), arr.end());
vector<vector<long long int>> dp(w + 1, vector<long long int>(n, -1));
for (int i = 0; i < n; i++) {
dp[0][i] = 0;
}
for (int i = 0; i < w + 1; i++) {
if (arr[0].second <= i) {
dp[i][0] = arr[0].first;
} else {
dp[i][0] = 0;
}
}
// for(int i=0;i<w+1;i++){
// for(int j=0;j<n;j++){
// cout << dp[i][j] << " ";
// }
// cout << endl;
// }
for (int i = 1; i < n; i++) {
for (int j = 1; j < w + 1; j++) {
if (arr[i].second <= j) {
// cout << "Entering" << i sp j << endl;
// cout << dp[j][i-1] sp arr[i].first+dp[j-arr[i].second][i-1] << endl;
dp[j][i] =
max(dp[j][i - 1], arr[i].first + dp[j - arr[i].second][i - 1]);
} else {
dp[j][i] = dp[j][i - 1];
}
}
}
// for(int i=0;i<w+1;i++){
// for(int j=0;j<n;j++){
// cout << dp[i][j] << " ";
// }
// cout << endl;
// }
cout << dp[w][n - 1];
return dp[w][n - 1];
}
|
#include <bits/stdc++.h>
#include <dirent.h>
#include <stdlib.h>
using namespace std;
#define sp << " " <<
typedef vector<int> vi;
typedef vector<long int> vli;
typedef vector<long long int> vlli;
typedef long int li;
typedef long long int lli;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<lli, lli>> vplli;
typedef unordered_map<int, int> ump;
int main() {
int n, w;
cin >> n >> w;
vector<pair<long long int, int>> arr;
// vector<int> value;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
arr.push_back({b, a});
}
// sort(arr.begin(), arr.end());
vector<vector<long long int>> dp(w + 1, vector<long long int>(n, -1));
for (int i = 0; i < n; i++) {
dp[0][i] = 0;
}
for (int i = 0; i < w + 1; i++) {
if (arr[0].second <= i) {
dp[i][0] = arr[0].first;
} else {
dp[i][0] = 0;
}
}
// for(int i=0;i<w+1;i++){
// for(int j=0;j<n;j++){
// cout << dp[i][j] << " ";
// }
// cout << endl;
// }
for (int i = 1; i < n; i++) {
for (int j = 1; j < w + 1; j++) {
if (arr[i].second <= j) {
// cout << "Entering" << i sp j << endl;
// cout << dp[j][i-1] sp arr[i].first+dp[j-arr[i].second][i-1] << endl;
dp[j][i] =
max(dp[j][i - 1], arr[i].first + dp[j - arr[i].second][i - 1]);
} else {
dp[j][i] = dp[j][i - 1];
}
}
}
// for(int i=0;i<w+1;i++){
// for(int j=0;j<n;j++){
// cout << dp[i][j] << " ";
// }
// cout << endl;
// }
cout << dp[w][n - 1];
return 0;
}
|
replace
| 66 | 67 | 66 | 67 |
90
| |
p03163
|
C++
|
Runtime Error
|
// nani?
#include <bits/stdc++.h>
#define pi acos(-1);
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define sor(a) sort(a.begin(), a.end())
#define rsor(a) sort(a.rbegin(), a.rend())
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0)
// #define inf 1LL<<62
typedef long long ll;
typedef double ld;
using namespace std;
const ll inf = 1e9;
const ll MOD = 1e9 + 7;
// ac cmtr;
const ll MAX_N = 1e5;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define trace(...)
#endif
//---------------------------
ll dp[100][100005];
ll peso[100005];
ll val[100005];
int main() {
fastio;
ll n, w;
cin >> n >> w;
f(i, 0, n) {
ll a, b;
cin >> a >> b;
peso[i + 1] = a;
val[i + 1] = b;
}
f(i, 1, n + 1) {
f(j, 1, w + 1) {
if (j >= peso[i])
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - peso[i]] + val[i]);
else
dp[i][j] = dp[i - 1][j];
}
}
cout << dp[n][w] << endl;
return 0;
}
|
// nani?
#include <bits/stdc++.h>
#define pi acos(-1);
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define sor(a) sort(a.begin(), a.end())
#define rsor(a) sort(a.rbegin(), a.rend())
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0)
// #define inf 1LL<<62
typedef long long ll;
typedef double ld;
using namespace std;
const ll inf = 1e9;
const ll MOD = 1e9 + 7;
// ac cmtr;
const ll MAX_N = 1e5;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define trace(...)
#endif
//---------------------------
ll dp[105][100005];
ll peso[100005];
ll val[100005];
int main() {
fastio;
ll n, w;
cin >> n >> w;
f(i, 0, n) {
ll a, b;
cin >> a >> b;
peso[i + 1] = a;
val[i + 1] = b;
}
f(i, 1, n + 1) {
f(j, 1, w + 1) {
if (j >= peso[i])
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - peso[i]] + val[i]);
else
dp[i][j] = dp[i - 1][j];
}
}
cout << dp[n][w] << endl;
return 0;
}
|
replace
| 39 | 40 | 39 | 40 |
0
| |
p03163
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define fastio() \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double
#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
const ll MOD = 1000 * 1000 * 1000 + 7;
using namespace std;
ll dp[100][100005];
ll N, W;
ll seno(ll idx, ll w, ll C[][2]) {
ll Q, R;
if (idx == N) {
return 0;
}
if (dp[idx][w] != INT_MAX)
return dp[idx][w];
if (w + C[idx][0] <= W)
Q = seno(idx + 1, w + C[idx][0], C) + C[idx][1];
else
Q = 0;
R = seno(idx + 1, w, C);
return dp[idx][w] = max(Q, R);
}
int main() {
fastio();
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cin >> N >> W;
ll A[N][2];
for (int i = 0; i < N; i++) {
for (int j = 0; j < 2; j++)
cin >> A[i][j];
}
for (int i = 0; i < 100; i++) {
for (int j = 0; j <= 100004; j++)
dp[i][j] = INT_MAX;
}
ll x = seno(0, 0, A);
cout << x;
return 0;
}
|
#include <bits/stdc++.h>
#define fastio() \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double
#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
const ll MOD = 1000 * 1000 * 1000 + 7;
using namespace std;
ll dp[100][100005];
ll N, W;
ll seno(ll idx, ll w, ll C[][2]) {
ll Q, R;
if (idx == N) {
return 0;
}
if (dp[idx][w] != INT_MAX)
return dp[idx][w];
if (w + C[idx][0] <= W)
Q = seno(idx + 1, w + C[idx][0], C) + C[idx][1];
else
Q = 0;
R = seno(idx + 1, w, C);
return dp[idx][w] = max(Q, R);
}
int main() {
fastio();
cin >> N >> W;
ll A[N][2];
for (int i = 0; i < N; i++) {
for (int j = 0; j < 2; j++)
cin >> A[i][j];
}
for (int i = 0; i < 100; i++) {
for (int j = 0; j <= 100004; j++)
dp[i][j] = INT_MAX;
}
ll x = seno(0, 0, A);
cout << x;
return 0;
}
|
delete
| 47 | 51 | 47 | 47 |
0
| |
p03163
|
C++
|
Time Limit Exceeded
|
/*
_ __ _ _ _ ____ _____
| |/ / / \ | | | / ___|_ _|
| ' / / _ \| | | \___ \ | |
| . \ / ___ \ |_| |___) || |
|_|\_\/_/ \_\___/|____/ |_|
*/
#include <bits/stdc++.h>
#include <fstream>
#define rep(i, a, b) for (long long i = (a); i < (b); i++)
#define per(i, a, b) for (long long i = (a); i > (b); i--)
#define a(x) (x.begin(), x.end())
#define ar(x) (x.rbegin(), x.rend())
#define pb push_back
#define Pb() pop_back()
#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 prllf
#define ms(a, b) memset(a, b, sizeof(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) prllf("Case %d: ", t)
#define CASEL(t) prllf("Case %d:\n", t)
#define intlimit 2147483647
#define longlimit 9223372036854775807
#define infinity (1 << 28)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a) * (b) / gcd(a, b))
#define PI 2 * acos(0.0)
using namespace std;
vector<pair<ll, ll>> v;
ll n, w;
vll dp;
ll find_max(ll index, ll weight) {
if (weight == 0 || (weight > 0 && index >= n)) {
return 0;
}
if (weight < 0 || index >= n) {
return -100000000000;
}
if (dp[index][weight] != -1) {
return dp[index][weight];
}
ll maxi = 0;
rep(i, index, n) {
maxi = max(maxi, v[i].second + find_max(i + 1, weight - v[i].first));
}
dp[index][weight] = maxi;
return maxi;
}
int main() {
CIN;
cin >> n >> w;
v.resize(n);
dp.resize(n, vl(w + 1, -1));
rep(i, 0, n) { cin >> v[i].first >> v[i].second; }
cout << find_max(0, w);
}
|
/*
_ __ _ _ _ ____ _____
| |/ / / \ | | | / ___|_ _|
| ' / / _ \| | | \___ \ | |
| . \ / ___ \ |_| |___) || |
|_|\_\/_/ \_\___/|____/ |_|
*/
#include <bits/stdc++.h>
#include <fstream>
#define rep(i, a, b) for (long long i = (a); i < (b); i++)
#define per(i, a, b) for (long long i = (a); i > (b); i--)
#define a(x) (x.begin(), x.end())
#define ar(x) (x.rbegin(), x.rend())
#define pb push_back
#define Pb() pop_back()
#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 prllf
#define ms(a, b) memset(a, b, sizeof(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) prllf("Case %d: ", t)
#define CASEL(t) prllf("Case %d:\n", t)
#define intlimit 2147483647
#define longlimit 9223372036854775807
#define infinity (1 << 28)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a) * (b) / gcd(a, b))
#define PI 2 * acos(0.0)
using namespace std;
vector<pair<ll, ll>> v;
ll n, w;
vll dp;
ll find_max(ll index, ll weight) {
if (weight == 0 || (weight > 0 && index >= n)) {
return 0;
}
if (weight < 0 || index >= n) {
return -100000000000;
}
if (dp[index][weight] != -1) {
return dp[index][weight];
}
ll maxi = 0;
maxi = max(v[index].second + find_max(index + 1, weight - v[index].first),
find_max(index + 1, weight));
dp[index][weight] = maxi;
return maxi;
}
int main() {
CIN;
cin >> n >> w;
v.resize(n);
dp.resize(n, vl(w + 1, -1));
rep(i, 0, n) { cin >> v[i].first >> v[i].second; }
cout << find_max(0, w);
}
|
replace
| 64 | 67 | 64 | 66 |
TLE
| |
p03163
|
C++
|
Runtime Error
|
// Author:: MUKUL KUMAR RAI
// Institution:: Jalpaiguri Government Engineering College
// If you are not doing well now just stick to it and learn new things one day
// you will be... Compete with yourself
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
///*template{{{
#define sz(x) (ll) x.size()
#define all(x) (x.begin(), x.end())
#define trav(a, x) for (auto &a : x)
#define fr(i, a, b) for (ll i = a; i <= b; i++)
#define fr1(i, a) for (ll i = 0; i < a; i++)
#define frr(i, a, b) for (ll i = b; i >= a; i--)
#define frr1(i, a) for (ll i = a - 1; i >= 0; i--)
#define sorta(a, n) sort(a, a + n)
#define sortd(a, n) sort(a, a + n, greater<ll>())
#define sortva(a) sort(a.begin(), a.end())
#define sortvd(a) sort(a.begin(), a.end(), greater<ll>())
#define tc(t) while (t--)
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
using vi = vector<ll>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vc = vector<char>;
using vs = vector<string>;
using vld = vector<ld>;
using pii = pair<ll, ll>;
using psi = pair<string, ll>;
using pci = pair<char, ll>;
using vpii = vector<pii>;
//}}}template*/
ll mod = 1e9 + 7;
ll const maxn = 1e6 + 5;
ll const inf = 1e18;
bool ss(const pair<ll, ll> &a, const pair<ll, ll> &b) {
return a.first * b.second + a.second < b.first * a.second + b.second;
}
ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll powm(ll x, ll n, ll M) {
ll result = 1;
while (n > 0) {
if (n % 2 == 1)
result = (result * x) % M;
x = (x * x) % M;
n = n / 2;
}
return result;
}
ll modinverse(ll a, ll m) { return powm(a, m - 2, m); }
bool prime(ll x) {
if (x <= 1)
return false;
for (int i = 2; i <= sqrt(x); i++)
if (x % i == 0)
return false;
return true;
}
ll divisor(ll x) {
ll cnt = 0;
for (int i = 1; i <= sqrt(x); i++) {
if (x % i == 0) {
if (i != x / i)
cnt += 2;
else
cnt += 1;
}
}
return cnt;
}
vector<ll> sieve(ll n) {
bool prim[n + 1];
memset(prim, true, sizeof(prim));
for (ll p = 2; p * p <= n; p++) {
if (prim[p] == true) {
for (int i = p * p; i <= n; i += p)
prim[i] = false;
}
}
vector<ll> v;
for (int i = 2; i <= n; i++)
if (prim[i])
v.push_back(i);
return v;
}
bool check(string s) {
ll l = 0, r = sz(s) - 1;
while (l <= r) {
if (s[l] != s[r])
return false;
l++;
r--;
}
return true;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fio;
ll n, w;
cin >> n >> w;
vi weight(n + 1, 0);
vi value(n + 1, 0);
ll dp[n + 1][w + 1];
fr(i, 1, n) cin >> weight[i] >> value[i];
fr(i, 0, n) {
fr(j, 0, w) {
if (i == 0 or j == 0)
dp[i][j] = 0;
else if (weight[i] <= j) {
dp[i][j] = max(value[i] + dp[i - 1][j - weight[i]], dp[i - 1][j]);
} else
dp[i][j] = dp[i - 1][j];
}
}
cout << dp[n][w];
return 0;
}
|
// Author:: MUKUL KUMAR RAI
// Institution:: Jalpaiguri Government Engineering College
// If you are not doing well now just stick to it and learn new things one day
// you will be... Compete with yourself
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
///*template{{{
#define sz(x) (ll) x.size()
#define all(x) (x.begin(), x.end())
#define trav(a, x) for (auto &a : x)
#define fr(i, a, b) for (ll i = a; i <= b; i++)
#define fr1(i, a) for (ll i = 0; i < a; i++)
#define frr(i, a, b) for (ll i = b; i >= a; i--)
#define frr1(i, a) for (ll i = a - 1; i >= 0; i--)
#define sorta(a, n) sort(a, a + n)
#define sortd(a, n) sort(a, a + n, greater<ll>())
#define sortva(a) sort(a.begin(), a.end())
#define sortvd(a) sort(a.begin(), a.end(), greater<ll>())
#define tc(t) while (t--)
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
using vi = vector<ll>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vc = vector<char>;
using vs = vector<string>;
using vld = vector<ld>;
using pii = pair<ll, ll>;
using psi = pair<string, ll>;
using pci = pair<char, ll>;
using vpii = vector<pii>;
//}}}template*/
ll mod = 1e9 + 7;
ll const maxn = 1e6 + 5;
ll const inf = 1e18;
bool ss(const pair<ll, ll> &a, const pair<ll, ll> &b) {
return a.first * b.second + a.second < b.first * a.second + b.second;
}
ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll powm(ll x, ll n, ll M) {
ll result = 1;
while (n > 0) {
if (n % 2 == 1)
result = (result * x) % M;
x = (x * x) % M;
n = n / 2;
}
return result;
}
ll modinverse(ll a, ll m) { return powm(a, m - 2, m); }
bool prime(ll x) {
if (x <= 1)
return false;
for (int i = 2; i <= sqrt(x); i++)
if (x % i == 0)
return false;
return true;
}
ll divisor(ll x) {
ll cnt = 0;
for (int i = 1; i <= sqrt(x); i++) {
if (x % i == 0) {
if (i != x / i)
cnt += 2;
else
cnt += 1;
}
}
return cnt;
}
vector<ll> sieve(ll n) {
bool prim[n + 1];
memset(prim, true, sizeof(prim));
for (ll p = 2; p * p <= n; p++) {
if (prim[p] == true) {
for (int i = p * p; i <= n; i += p)
prim[i] = false;
}
}
vector<ll> v;
for (int i = 2; i <= n; i++)
if (prim[i])
v.push_back(i);
return v;
}
bool check(string s) {
ll l = 0, r = sz(s) - 1;
while (l <= r) {
if (s[l] != s[r])
return false;
l++;
r--;
}
return true;
}
int main() {
fio;
ll n, w;
cin >> n >> w;
vi weight(n + 1, 0);
vi value(n + 1, 0);
ll dp[n + 1][w + 1];
fr(i, 1, n) cin >> weight[i] >> value[i];
fr(i, 0, n) {
fr(j, 0, w) {
if (i == 0 or j == 0)
dp[i][j] = 0;
else if (weight[i] <= j) {
dp[i][j] = max(value[i] + dp[i - 1][j - weight[i]], dp[i - 1][j]);
} else
dp[i][j] = dp[i - 1][j];
}
}
cout << dp[n][w];
return 0;
}
|
replace
| 104 | 108 | 104 | 105 |
-6
|
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p03163
|
C++
|
Runtime Error
|
/*coderanant*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define f1(i, a, b) for (i = a; i < b; i++)
#define f2(i, a, b) for (i = a; i >= b; i--)
#define endl '\n'
#define pb push_back
#define gp " "
#define ff first
#define ss second
#define mp make_pair
const int mod = 1000000007;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("/home/akmittal/Desktop/Competitive Programming/in.txt", "r", stdin);
freopen("/home/akmittal/Desktop/Competitive Programming/out.txt", "w",
stdout);
#endif
int n, w;
cin >> n >> w;
vector<int> dp(w + 1, 0ll);
int i;
f1(i, 0, n) {
int weight, value;
cin >> weight >> value;
int j;
for (int j = w - weight; j >= 0; j--) {
dp[j + weight] = max(dp[j + weight], dp[j] + value);
}
// f1(j,0,w+1)
// cout<<dp[j]<<gp;
// cout<<endl;
}
int ans = 0ll;
f1(i, 0, w + 1) { ans = max(ans, dp[i]); }
cout << ans << endl;
return 0;
}
|
/*coderanant*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define f1(i, a, b) for (i = a; i < b; i++)
#define f2(i, a, b) for (i = a; i >= b; i--)
#define endl '\n'
#define pb push_back
#define gp " "
#define ff first
#define ss second
#define mp make_pair
const int mod = 1000000007;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// #ifndef ONLINE_JUDGE
// freopen("/home/akmittal/Desktop/Competitive
// Programming/in.txt","r",stdin); freopen("/home/akmittal/Desktop/Competitive
// Programming/out.txt","w",stdout); #endif
int n, w;
cin >> n >> w;
vector<int> dp(w + 1, 0ll);
int i;
f1(i, 0, n) {
int weight, value;
cin >> weight >> value;
int j;
for (int j = w - weight; j >= 0; j--) {
dp[j + weight] = max(dp[j + weight], dp[j] + value);
}
// f1(j,0,w+1)
// cout<<dp[j]<<gp;
// cout<<endl;
}
int ans = 0ll;
f1(i, 0, w + 1) { ans = max(ans, dp[i]); }
cout << ans << endl;
return 0;
}
|
replace
| 20 | 25 | 20 | 24 |
-6
|
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p03163
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
int n, W;
vector<long long> w(101), v(101);
long long tb[100][100001];
long long dp(int i, int weight) {
if (weight > W)
return -1000000000000LL;
if (i >= n) {
return 0;
}
if (tb[i][weight] != -1)
return tb[i][weight];
long long total = 0;
for (int j = i; j <= n; ++j) {
total = max(total, dp(j + 1, weight + w[j]) + v[j]);
}
return tb[i][weight] = total;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
memset(tb, -1, sizeof(tb));
cin >> n >> W;
for (int i = 0; i < n; ++i) {
cin >> w[i] >> v[i];
}
cout << dp(0, 0) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, W;
vector<long long> w(101), v(101);
long long tb[100][100001];
long long dp(int i, int weight) {
if (weight > W)
return -1000000000000LL;
if (i >= n) {
return 0;
}
if (tb[i][weight] != -1)
return tb[i][weight];
return tb[i][weight] =
max(dp(i + 1, weight), dp(i + 1, weight + w[i]) + v[i]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
memset(tb, -1, sizeof(tb));
cin >> n >> W;
for (int i = 0; i < n; ++i) {
cin >> w[i] >> v[i];
}
cout << dp(0, 0) << endl;
return 0;
}
|
replace
| 19 | 25 | 19 | 21 |
TLE
| |
p03164
|
C++
|
Runtime Error
|
#include "bits/stdc++.h"
using namespace std;
long long n, c, value;
long long f[101][100001];
long long v[101], w[101];
int main() {
scanf("%lld%lld", &n, &c);
for (int i = 1; i <= n; i++) {
scanf("%lld%lld", &w[i], &v[i]);
value += v[i];
}
for (int j = 1; j <= value; j++)
f[0][j] = (long long)10e11 + 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= value; j++) {
f[i][j] = f[i - 1][j];
if (j >= v[i])
f[i][j] = min(f[i][j], f[i - 1][j - v[i]] + w[i]);
}
for (int i = value; i >= 1; i--) {
if (f[n][i] <= c) {
printf("%d", i);
return 255;
}
}
}
|
#include "bits/stdc++.h"
using namespace std;
long long n, c, value;
long long f[101][100001];
long long v[101], w[101];
int main() {
scanf("%lld%lld", &n, &c);
for (int i = 1; i <= n; i++) {
scanf("%lld%lld", &w[i], &v[i]);
value += v[i];
}
for (int j = 1; j <= value; j++)
f[0][j] = (long long)10e11 + 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= value; j++) {
f[i][j] = f[i - 1][j];
if (j >= v[i])
f[i][j] = min(f[i][j], f[i - 1][j - v[i]] + w[i]);
}
for (int i = value; i >= 1; i--) {
if (f[n][i] <= c) {
printf("%d", i);
break;
}
}
}
|
replace
| 22 | 23 | 22 | 23 |
255
| |
p03164
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define allarr(a) a, a + n
#define ll long long
#define pb push_back
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<int, pi> trp;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
ll _abs(ll x) { return (x > 0 ? x : -x); }
const int N = 105, MV = 1e3 + 2;
int n, W, v[N], w[N], V;
ll dp[N][MV]; // max value at index i with weight
int main() {
fastio;
cin >> n >> W;
for (int i = 0; i < n; i++) {
cin >> w[i] >> v[i];
V += v[i];
}
ll ans = 0;
for (int i = 0; i <= V; i++)
dp[0][i] = 2e9;
dp[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= V; j++) {
ll &r = dp[i][j];
r = 2e9;
r = min(r, dp[i - 1][j]);
if (j - v[i - 1] >= 0)
r = min(r, dp[i - 1][j - v[i - 1]] + w[i - 1]);
if (r <= W)
ans = max(ans, (ll)j);
// cout<<i<<" "<<j<<" "<<r<<endl;
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define allarr(a) a, a + n
#define ll long long
#define pb push_back
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<int, pi> trp;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
ll _abs(ll x) { return (x > 0 ? x : -x); }
const int N = 105, MV = 1e5 + 5;
int n, W, v[N], w[N], V;
ll dp[N][MV]; // max value at index i with weight
int main() {
fastio;
cin >> n >> W;
for (int i = 0; i < n; i++) {
cin >> w[i] >> v[i];
V += v[i];
}
ll ans = 0;
for (int i = 0; i <= V; i++)
dp[0][i] = 2e9;
dp[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= V; j++) {
ll &r = dp[i][j];
r = 2e9;
r = min(r, dp[i - 1][j]);
if (j - v[i - 1] >= 0)
r = min(r, dp[i - 1][j - v[i - 1]] + w[i - 1]);
if (r <= W)
ans = max(ans, (ll)j);
// cout<<i<<" "<<j<<" "<<r<<endl;
}
}
cout << ans << endl;
}
|
replace
| 22 | 23 | 22 | 23 |
0
| |
p03164
|
C++
|
Runtime Error
|
#include "bits/stdc++.h"
using namespace std;
#define db(x) // cerr << #x << " == " << x << endl
#define all(container) container.begin(), container.end()
#define mp(i, j) make_pair(i, j)
#define pb push_back
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vii;
template <typename T> void print_vector_debug(const T &t) {
cout << "[DEBUG] VECTOR:";
for (auto i = t.cbegin(); i != t.cend(); ++i) {
if ((i + 1) != t.cend()) {
cout << *i << " ";
} else {
cout << *i << endl;
}
}
}
#define MAXN 105
#define MAXV 100005
#define INF (ll)20000000000000000LL
ll N, W;
ll v[MAXN], w[MAXN];
int vis[MAXN][MAXV];
ll dp[MAXN][MAXV];
ll solve_dp(int idx, ll r) {
if (idx == N) {
return r <= 0 ? 0 : INF;
}
if (r <= 0) {
return 0;
}
if (vis[idx][r]) {
return dp[idx][r];
}
vis[idx][r] = 1;
ll res = min(solve_dp(idx + 1, r), solve_dp(idx + 1, r - v[idx]) + w[idx]);
dp[idx][r] = res;
return res;
}
ll solve() {
ll upper = 2000000;
ll lower = 0;
ll mid, best;
while (lower <= upper) {
mid = lower + (upper - lower) / 2;
db(upper);
db(lower);
db(mid);
if (solve_dp(0, mid) <= W) {
db("possible");
best = mid;
lower = mid + 1;
} else {
db("impossible");
upper = mid - 1;
}
}
return best;
}
int main() {
scanf("%lld%lld", &N, &W);
for (int i = 0; i < N; i++) {
scanf("%lld%lld", &w[i], &v[i]);
}
printf("%lld\n", solve());
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
#define db(x) // cerr << #x << " == " << x << endl
#define all(container) container.begin(), container.end()
#define mp(i, j) make_pair(i, j)
#define pb push_back
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vii;
template <typename T> void print_vector_debug(const T &t) {
cout << "[DEBUG] VECTOR:";
for (auto i = t.cbegin(); i != t.cend(); ++i) {
if ((i + 1) != t.cend()) {
cout << *i << " ";
} else {
cout << *i << endl;
}
}
}
#define MAXN 105
#define MAXV 100005
#define INF (ll)20000000000000000LL
ll N, W;
ll v[MAXN], w[MAXN];
int vis[MAXN][MAXV];
ll dp[MAXN][MAXV];
ll solve_dp(int idx, ll r) {
if (idx == N) {
return r <= 0 ? 0 : INF;
}
if (r <= 0) {
return 0;
}
if (vis[idx][r]) {
return dp[idx][r];
}
vis[idx][r] = 1;
ll res = min(solve_dp(idx + 1, r), solve_dp(idx + 1, r - v[idx]) + w[idx]);
dp[idx][r] = res;
return res;
}
ll solve() {
ll upper = MAXV - 1;
ll lower = 0;
ll mid, best;
while (lower <= upper) {
mid = lower + (upper - lower) / 2;
db(upper);
db(lower);
db(mid);
if (solve_dp(0, mid) <= W) {
db("possible");
best = mid;
lower = mid + 1;
} else {
db("impossible");
upper = mid - 1;
}
}
return best;
}
int main() {
scanf("%lld%lld", &N, &W);
for (int i = 0; i < N; i++) {
scanf("%lld%lld", &w[i], &v[i]);
}
printf("%lld\n", solve());
return 0;
}
|
replace
| 55 | 56 | 55 | 56 |
-11
| |
p03164
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
// A utility function that returns maximum of two integers
int max(int a, int b) { return (a > b) ? a : b; }
// Returns the maximum value that can be put in a knapsack of capacity W
long long knapSack1(int W, int weight[], int val[], int n) {
int k, w;
long long **F = new long long *[n + 1];
for (int i = 0; i < n + 1; i++)
F[i] = new long long[W + 1];
// Build table K[][] in bottom up manner
for (k = 0; k <= n; k++) {
for (w = 0; w <= W; w++) {
if (k == 0 || w == 0)
F[k][w] = 0;
else if (weight[k - 1] <= w)
F[k][w] = max(F[k - 1][w], F[k - 1][w - weight[k - 1]] + val[k - 1]);
else
F[k][w] = F[k - 1][w];
// cout<<std::setw(2)<<F[k][w]<<" ";
}
// cout<<endl;
}
long long result = F[n][W];
for (int i = 0; i < n + 1; i++)
delete[] F[i];
delete[] F;
return result;
}
long long knapSack2(int W, int weight[], int val[], int n) {
long long F[n + 1][W + 1];
// Build table K[][] in bottom up manner
for (int k = 0; k <= n; k++) {
for (int w = 0; w <= W; w++) {
if (k == 0 || w == 0)
F[k][w] = 0;
else if (weight[k - 1] <= w)
F[k][w] = max(F[k - 1][w], F[k - 1][w - weight[k - 1]] + val[k - 1]);
else
F[k][w] = F[k - 1][w];
}
}
return F[n][W];
}
long long knapSack3(int W, int weight[], int val[], int n) {
long long vsum = 0;
for (int i = 0; i < n; i++) {
vsum += val[i];
}
long long *dp = new long long[vsum + 1];
for (long long i = 0; i < vsum + 1; i++)
dp[i] = pow(10, 18) + 5;
dp[0] = 0;
for (int i = 0; i < n; i++) {
for (long long j = vsum; j >= val[i]; j--) {
dp[j] = min(dp[j], dp[j - val[i]] + weight[i]);
}
}
long long i;
for (i = vsum; i >= 0; i--) {
if (dp[i] <= W) {
// cout << i;
break;
}
}
delete[] dp;
return i;
}
int main() {
std::ios::sync_with_stdio(false);
int n, W;
std::cin >> n >> W;
int *val = new int[n];
int *wt = new int[n];
for (int i = 0; i < n; i++) {
cin >> wt[i] >> val[i];
}
std::cout << knapSack2(W, wt, val, n) << endl;
delete[] val;
delete[] wt;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
// A utility function that returns maximum of two integers
int max(int a, int b) { return (a > b) ? a : b; }
// Returns the maximum value that can be put in a knapsack of capacity W
long long knapSack1(int W, int weight[], int val[], int n) {
int k, w;
long long **F = new long long *[n + 1];
for (int i = 0; i < n + 1; i++)
F[i] = new long long[W + 1];
// Build table K[][] in bottom up manner
for (k = 0; k <= n; k++) {
for (w = 0; w <= W; w++) {
if (k == 0 || w == 0)
F[k][w] = 0;
else if (weight[k - 1] <= w)
F[k][w] = max(F[k - 1][w], F[k - 1][w - weight[k - 1]] + val[k - 1]);
else
F[k][w] = F[k - 1][w];
// cout<<std::setw(2)<<F[k][w]<<" ";
}
// cout<<endl;
}
long long result = F[n][W];
for (int i = 0; i < n + 1; i++)
delete[] F[i];
delete[] F;
return result;
}
long long knapSack2(int W, int weight[], int val[], int n) {
long long F[n + 1][W + 1];
// Build table K[][] in bottom up manner
for (int k = 0; k <= n; k++) {
for (int w = 0; w <= W; w++) {
if (k == 0 || w == 0)
F[k][w] = 0;
else if (weight[k - 1] <= w)
F[k][w] = max(F[k - 1][w], F[k - 1][w - weight[k - 1]] + val[k - 1]);
else
F[k][w] = F[k - 1][w];
}
}
return F[n][W];
}
long long knapSack3(int W, int weight[], int val[], int n) {
long long vsum = 0;
for (int i = 0; i < n; i++) {
vsum += val[i];
}
long long *dp = new long long[vsum + 1];
for (long long i = 0; i < vsum + 1; i++)
dp[i] = pow(10, 18) + 5;
dp[0] = 0;
for (int i = 0; i < n; i++) {
for (long long j = vsum; j >= val[i]; j--) {
dp[j] = min(dp[j], dp[j - val[i]] + weight[i]);
}
}
long long i;
for (i = vsum; i >= 0; i--) {
if (dp[i] <= W) {
// cout << i;
break;
}
}
delete[] dp;
return i;
}
int main() {
std::ios::sync_with_stdio(false);
int n, W;
std::cin >> n >> W;
int *val = new int[n];
int *wt = new int[n];
for (int i = 0; i < n; i++) {
cin >> wt[i] >> val[i];
}
std::cout << knapSack3(W, wt, val, n) << endl;
delete[] val;
delete[] wt;
return 0;
}
|
replace
| 87 | 88 | 87 | 88 |
0
| |
p03164
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ri(x) scanf("%d", &(x))
#define ri2(x, y) scanf("%d %d", &(x), &(y))
#define ri3(x, y, z) scanf("%d %d %d", &(x), &(y), &(z))
#define rll(x) scanf("%lld", &(x))
#define rll2(x, y) scanf("%lld %lld", &(x), &(y))
#define rll3(x, y, z) scanf("%lld %lld %lld", &(x), &(y), &(z))
#define gc(x) ((x) = getchar())
using namespace ::std;
const long double PI = acos(-1);
const long long MOD = 1000000000 + 7;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> tll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<tll> vtll;
typedef vector<string> vs;
typedef set<int> si;
typedef set<ii> sii;
typedef set<iii> siii;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll add(ll a, ll b, ll m = MOD) { return (a % m + b % m + 2 * m) % m; }
ll mul(ll a, ll b, ll m = MOD) { return ((a % m + m) * (b % m + m)) % m; }
ll pow_mod(ll a, ll b, ll m = MOD) {
ll res = 1LL;
a = a % m;
while (b) {
if (b & 1)
res = mul(res, a, m);
b >>= 1;
a = mul(a, a, m);
}
return res;
}
ll fastexp(ll a, ll b) {
ll res = 1LL;
while (b) {
if (b & 1)
res = res * a;
b >>= 1;
a *= a;
}
return res;
}
int gcdExtendido(int a, int b, int *x, int *y) {
if (a == 0) {
*x = 0;
*y = 1;
return b;
}
int x1, y1;
int gcd = gcdExtendido(b % a, a, &x1, &y1);
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
int modInverso(int a, int m) {
int x, y;
int g = gcdExtendido(a, m, &x, &y);
if (g != 1)
return -1;
else
return (x % m + m) % m;
}
/****************************************
*************P*L*A*N*T*I*L*L*A************
*****************************************/
const int N = 100 + 5;
const int V = 10000 + 5;
const ll inf = 1LL << 60;
int n;
int C;
int w[N];
int v[N];
ll memo[N][V];
int main() {
ri2(n, C);
int S = 0;
for (int i = 1; i <= n; i++) {
ri2(w[i], v[i]);
S += v[i];
}
for (int i = 0; i <= S; i++)
memo[0][i] = inf;
memo[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= S; j++) {
memo[i][j] = memo[i - 1][j];
if (j >= v[i]) {
if (memo[i - 1][j - v[i]] + w[i] <= C) {
memo[i][j] = min(memo[i][j], memo[i - 1][j - v[i]] + w[i]);
}
}
}
}
int ans = 0;
for (int i = 0; i <= S; i++) {
if (memo[n][i] <= C) {
ans = i;
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ri(x) scanf("%d", &(x))
#define ri2(x, y) scanf("%d %d", &(x), &(y))
#define ri3(x, y, z) scanf("%d %d %d", &(x), &(y), &(z))
#define rll(x) scanf("%lld", &(x))
#define rll2(x, y) scanf("%lld %lld", &(x), &(y))
#define rll3(x, y, z) scanf("%lld %lld %lld", &(x), &(y), &(z))
#define gc(x) ((x) = getchar())
using namespace ::std;
const long double PI = acos(-1);
const long long MOD = 1000000000 + 7;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> tll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<tll> vtll;
typedef vector<string> vs;
typedef set<int> si;
typedef set<ii> sii;
typedef set<iii> siii;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll add(ll a, ll b, ll m = MOD) { return (a % m + b % m + 2 * m) % m; }
ll mul(ll a, ll b, ll m = MOD) { return ((a % m + m) * (b % m + m)) % m; }
ll pow_mod(ll a, ll b, ll m = MOD) {
ll res = 1LL;
a = a % m;
while (b) {
if (b & 1)
res = mul(res, a, m);
b >>= 1;
a = mul(a, a, m);
}
return res;
}
ll fastexp(ll a, ll b) {
ll res = 1LL;
while (b) {
if (b & 1)
res = res * a;
b >>= 1;
a *= a;
}
return res;
}
int gcdExtendido(int a, int b, int *x, int *y) {
if (a == 0) {
*x = 0;
*y = 1;
return b;
}
int x1, y1;
int gcd = gcdExtendido(b % a, a, &x1, &y1);
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
int modInverso(int a, int m) {
int x, y;
int g = gcdExtendido(a, m, &x, &y);
if (g != 1)
return -1;
else
return (x % m + m) % m;
}
/****************************************
*************P*L*A*N*T*I*L*L*A************
*****************************************/
const int N = 100 + 5;
const int V = 100000 + 5;
const ll inf = 1LL << 60;
int n;
int C;
int w[N];
int v[N];
ll memo[N][V];
int main() {
ri2(n, C);
int S = 0;
for (int i = 1; i <= n; i++) {
ri2(w[i], v[i]);
S += v[i];
}
for (int i = 0; i <= S; i++)
memo[0][i] = inf;
memo[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= S; j++) {
memo[i][j] = memo[i - 1][j];
if (j >= v[i]) {
if (memo[i - 1][j - v[i]] + w[i] <= C) {
memo[i][j] = min(memo[i][j], memo[i - 1][j - v[i]] + w[i]);
}
}
}
}
int ans = 0;
for (int i = 0; i <= S; i++) {
if (memo[n][i] <= C) {
ans = i;
}
}
cout << ans << endl;
return 0;
}
|
replace
| 90 | 91 | 90 | 91 |
0
| |
p03164
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set \
tree<ll, null_type, less_equal<ll>, rb_tree_tag, \
tree_order_statistics_node_update>
#define PI 3.14159265
#define ll long long
#define ld long double
#define vi vector<int>
#define pb push_back
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 300000000000000001
#define infx 9223372036854775806
#define all(c) c.begin(), c.end()
#define mk(x, yy) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define ff first
#define ss second
#define re return
// #define endl "\n"
#define max2(x, y) (x > y) ? x : y
#define min2(x, y) (x < y) ? x : y
#define mid(s, e) (s + (e - s) / 2)
#define mini INT_MIN
#define maxo INT_MAX
#define rep(i, a, b) for (int i = a; i < (int)(b); ++i)
#define read(a, n) \
for (int i = 0; i < n; i++) \
cin >> a[i]
#define show(a, n) \
for (int i = 0; i < n; i++) \
cout << a[i] << ' '; \
cout << endl
#define ene cout << "\n";
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll power(ll x, ll y, ll p) {
ll res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0) {
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
bool compr(std::vector<ll> a, std::vector<ll> b) {
if (a[0] == b[0])
return a[1] < b[1];
else
return a[0] < b[0];
}
bool comp(std::vector<ll> a, std::vector<ll> b) {
if (a[1] == b[1] && a[2] == b[2])
return a[0] < b[0];
else if (a[1] == b[1])
return a[2] > b[2];
else
return a[1] < b[1];
}
int main() {
#ifdef SHIVANSH
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
ll w;
cin >> w;
ll a[n][2];
for (ll i = 0; i < n; i++) {
ll x, y;
cin >> x >> y;
a[i][0] = x;
a[i][1] = y;
}
ll dp[100000 + 1] = {};
for (ll i = 0; i <= 100001; i++)
dp[i] = INT_MAX;
dp[0] = 0;
for (ll i = 0; i < n; i++) {
for (ll j = 100000; j >= 1; j--) {
if (j >= a[i][1] && dp[j - a[i][1]] < INT_MAX) {
dp[j] = min(dp[j], a[i][0] + dp[j - a[i][1]]);
}
}
}
ll ans = 0;
for (ll i = 0; i <= 100000; i++) {
if (dp[i] <= w && dp[i] >= 0)
ans = max(ans, i);
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set \
tree<ll, null_type, less_equal<ll>, rb_tree_tag, \
tree_order_statistics_node_update>
#define PI 3.14159265
#define ll long long
#define ld long double
#define vi vector<int>
#define pb push_back
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 300000000000000001
#define infx 9223372036854775806
#define all(c) c.begin(), c.end()
#define mk(x, yy) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define ff first
#define ss second
#define re return
// #define endl "\n"
#define max2(x, y) (x > y) ? x : y
#define min2(x, y) (x < y) ? x : y
#define mid(s, e) (s + (e - s) / 2)
#define mini INT_MIN
#define maxo INT_MAX
#define rep(i, a, b) for (int i = a; i < (int)(b); ++i)
#define read(a, n) \
for (int i = 0; i < n; i++) \
cin >> a[i]
#define show(a, n) \
for (int i = 0; i < n; i++) \
cout << a[i] << ' '; \
cout << endl
#define ene cout << "\n";
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll power(ll x, ll y, ll p) {
ll res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0) {
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
bool compr(std::vector<ll> a, std::vector<ll> b) {
if (a[0] == b[0])
return a[1] < b[1];
else
return a[0] < b[0];
}
bool comp(std::vector<ll> a, std::vector<ll> b) {
if (a[1] == b[1] && a[2] == b[2])
return a[0] < b[0];
else if (a[1] == b[1])
return a[2] > b[2];
else
return a[1] < b[1];
}
int main() {
#ifdef SHIVANSH
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
ll w;
cin >> w;
ll a[n][2];
for (ll i = 0; i < n; i++) {
ll x, y;
cin >> x >> y;
a[i][0] = x;
a[i][1] = y;
}
ll dp[100000 + 1] = {};
for (ll i = 0; i < 100001; i++)
dp[i] = INT_MAX;
dp[0] = 0;
for (ll i = 0; i < n; i++) {
for (ll j = 100000; j >= 1; j--) {
if (j >= a[i][1] && dp[j - a[i][1]] < INT_MAX) {
dp[j] = min(dp[j], a[i][0] + dp[j - a[i][1]]);
}
}
}
ll ans = 0;
for (ll i = 0; i <= 100000; i++) {
if (dp[i] <= w && dp[i] >= 0)
ans = max(ans, i);
}
cout << ans << endl;
}
|
replace
| 101 | 102 | 101 | 102 |
-6
|
*** stack smashing detected ***: terminated
|
p03164
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i > 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main() {
int N, W;
cin >> N >> W;
vector<ll> w(N), v(N);
REP(i, N)
cin >> w[i] >> v[i];
int V = accumulate(ALL(v), 0);
ll dp[N + 1][V + 1];
REP(i, V + 1)
dp[0][i] = INF;
REP(i, N)
REP(j, V + 1) {
if (j <= v[i]) {
dp[i + 1][j] = min(dp[i][j], w[i]);
} else {
FOR(k, 1, j - v[i] + 1) {
dp[i + 1][k + v[i]] = min(dp[i][k] + w[i], dp[i][k + v[i]]);
}
}
}
REPR(i, V) {
if (dp[N][i] <= W) {
cout << i << endl;
break;
}
}
return 0;
}
|
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i > 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main() {
int N, W;
cin >> N >> W;
vector<ll> w(N), v(N);
REP(i, N)
cin >> w[i] >> v[i];
int V = accumulate(ALL(v), 0);
ll dp[N + 1][V + 1];
REP(i, V + 1)
dp[0][i] = INF;
REP(i, N)
REP(j, V + 1)
dp[i + 1][j] = min((j <= v[i] ? 0 : dp[i][j - v[i]]) + w[i], dp[i][j]);
REPR(i, V) {
if (dp[N][i] <= W) {
cout << i << endl;
break;
}
}
return 0;
}
|
replace
| 23 | 32 | 23 | 25 |
TLE
| |
p03164
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
freopen("caitui.inp", "r", stdin);
long long int n, w;
cin >> n >> w;
long long int m[n + 10], v[n + 10];
long long int tong = 0;
for (int i = 1; i <= n; i++) {
cin >> m[i] >> v[i];
tong += v[i];
}
long long int f[tong + 10];
for (int i = 1; i <= tong; i++) {
f[i] = 1000000000;
}
f[0] = 0;
for (int i = 1; i <= n; i++) {
for (int t = tong; t >= v[i]; t--) {
f[t] = min(m[i] + f[t - v[i]], f[t]);
}
}
for (int t = tong; t >= v[1]; t--) {
if (f[t] <= w) {
cout << t;
return 0;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("caitui.inp", "r", stdin);
long long int n, w;
cin >> n >> w;
long long int m[n + 10], v[n + 10];
long long int tong = 0;
for (int i = 1; i <= n; i++) {
cin >> m[i] >> v[i];
tong += v[i];
}
long long int f[tong + 10];
for (int i = 1; i <= tong; i++) {
f[i] = 1000000000;
}
f[0] = 0;
for (int i = 1; i <= n; i++) {
for (int t = tong; t >= v[i]; t--) {
f[t] = min(m[i] + f[t - v[i]], f[t]);
}
}
for (int t = tong; t >= v[1]; t--) {
if (f[t] <= w) {
cout << t;
return 0;
}
}
}
|
replace
| 6 | 7 | 6 | 7 |
-11
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.