Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n, d, cnt, day, price;
int val[55];
int dp[500001];
int main() {
cin >> n >> d;
for (int i = 1; i <= n; i++) cin >> val[i];
sort(val + 1, val + n + 1);
for (int i = 1; i <= n; i++) {
if (price + d >= val[i])
price += val[i];
else
break;
}
dp[0] = 1;
for (int i = 1; i <= n; i++)
for (int j = price; j >= val[i]; j--) {
if (dp[j - val[i]] == 1) dp[j] = dp[j - val[i]];
}
for (int i = 0;; day++) {
if (i == price) break;
for (int j = i + d; j > i; j--) {
if (dp[j]) i = j;
}
}
cout << price << " " << day << endl;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 510000;
int f[maxn];
int n, d, up;
int c[66];
int b[66];
int main() {
cin >> n >> d;
f[0] = 1;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
for (int j = (up += x); j >= x; j--)
if (f[j - x]) f[j] = 1;
}
int num = 0, ans = 0;
while (true) {
int to = ans + d;
while (!f[to] && to > ans) to--;
if (ans == to) break;
ans = to;
num++;
}
cout << ans << ' ' << num << endl;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const long long int inf = 1000000000;
const long long int mod = 1000000000 + 7;
inline void IO() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
inline int dcmp(long double x) { return x < -1e-12 ? -1 : (x > 1e-12); }
template <class T>
inline int CHECK(T MASK, int i) {
return (MASK >> i) & 1;
}
template <class T>
inline T ON(T MASK, int i) {
return MASK | (T(1) << i);
}
template <class T>
inline T OFF(T MASK, int i) {
return MASK & (~(T(1) << i));
}
template <typename T>
inline int CNT(T MASK) {
if (numeric_limits<T>::digits <= numeric_limits<unsigned int>::digits)
return __builtin_popcount(MASK);
else
return __builtin_popcountll(MASK);
}
template <class T>
inline int RIGHT(T MASK) {
return log2(MASK & -MASK);
}
int dx4[] = {0, 0, -1, +1};
int dy4[] = {+1, -1, 0, 0};
int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1, 0};
int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1, 0};
inline void I(int& a) { scanf("%d", &a); }
inline void I(long long int& a) { scanf("%I64d", &a); }
inline void I(unsigned long long int& a) { scanf("%I64u", &a); }
inline void I(char* a) { scanf("%s", a); }
char Iarr[2000010];
inline void I(string& a) {
scanf("%s", Iarr);
a = Iarr;
}
template <typename T, typename... Args>
void I(T& a, Args&... args) {
I(a);
I(args...);
}
inline void OUT(int a) { printf("%d", a); }
inline void OUT(long long int a) { printf("%I64d", a); }
inline void OUT(const char* a) { printf("%s", a); }
inline void OUT(char* a) { printf("%s", a); }
inline void OUT(bool a) { printf("%d", a); }
inline void OUT(string a) {
for (__typeof(a.end()) it = (a.begin()) - ((a.begin()) > (a.end()));
it != (a.end()) - ((a.begin()) > (a.end()));
it += 1 - 2 * ((a.begin()) > (a.end())))
printf("%c", *it);
}
inline void OUT(unsigned long long int a) { printf("%I64u", a); }
template <typename T, typename... Args>
void OUT(T a, Args... args) {
OUT(a);
OUT(" ");
OUT(args...);
}
template <typename... Args>
void O(Args... args) {
OUT(args...);
OUT("\n");
}
template <typename T1, typename T2>
inline ostream& operator<<(ostream& os, pair<T1, T2> p) {
os << "{" << p.first << ", " << p.second << "}";
return os;
}
template <typename T>
inline ostream& operator<<(ostream& os, vector<T>& a) {
os << "[";
for (int i = 0; i < (int)a.size(); i++) {
if (i) os << ", ";
os << a[i];
}
os << "]";
return os;
}
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cout << *it << " = " << a << endl;
err(++it, args...);
}
const int M = 1000010;
bool dp[500001];
int main() {
int n, d;
I(n, d);
dp[0] = 1;
for (int i = 0; i < n; i++) {
int x;
I(x);
for (int j = 500000; j >= 0; j--) {
if (dp[j]) {
if (j + x <= 500000) dp[j + x] = 1;
}
}
}
vector<int> v;
for (int i = 0; i <= 500000; i++) {
if (dp[i]) v.push_back(i);
}
int in = 0;
int days = 0;
while (1) {
int low = in + 1, high = (int)v.size() - 1;
if (low > high) break;
while (low < high) {
int mid = (low + high + 1) / 2;
if (v[mid] - v[in] <= d) {
low = mid;
} else {
high = mid - 1;
}
}
if (v[low] - v[in] <= d) {
days++;
in = low;
} else {
break;
}
}
O(v[in], days);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int MALO = 64;
const int DUZO = 10010 * MALO;
int n, d;
int price[MALO];
int backpack[DUZO];
void backpackize(int max_cost);
int main() {
scanf("%d %d", &n, &d);
int summa = 0;
for (int i = 0; i < n; i++) {
scanf("%d", price + i);
summa += price[i];
}
summa += d + 10;
backpackize(summa);
int days = 0;
int value = 0;
while (true) {
int next_best = -1;
for (int i = value + d; i > value; i--) {
if (backpack[i]) {
next_best = i;
break;
}
}
if (next_best == -1) break;
days++;
value = next_best;
}
printf("%d %d\n", value, days);
return 0;
}
void backpackize(int max_cost) {
backpack[0] = 1;
for (int i = 0; i < n; i++) {
for (int j = max_cost - price[i]; j >= 0; j--) {
if (!backpack[j]) continue;
backpack[j + price[i]] = 1;
}
}
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
int n, d;
cin >> n >> d;
int vetor[n];
for (int i = 0; i < n; i++) cin >> vetor[i];
long long combinacoes[600000];
memset(combinacoes, 0, sizeof combinacoes);
for (int i = 0; i < n; i++) {
for (int j = 600000 - 1; j > 0; j--) {
if (combinacoes[j] != 0) combinacoes[vetor[i] + j] = 1;
}
combinacoes[vetor[i]] = 1;
}
int dia = 0;
int maximo = 0;
bool possivel = true;
while (possivel) {
possivel = false;
for (long long i = maximo + d; i > maximo; i--) {
if (combinacoes[i] != 0) {
maximo = i;
dia++;
possivel = true;
break;
}
}
}
cout << maximo << " " << dia << endl;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); }
const int MAXN = 700010;
int N, D;
int dp[MAXN];
int main() {
while (scanf("%d%d", &N, &D) != EOF) {
memset(dp, 0, sizeof(dp));
dp[0] = 1;
int sum = 0;
for (int i = 1; i <= N; i++) {
int x;
scanf("%d", &x);
sum += x;
for (int j = sum; j >= x; j--)
if (dp[j - x]) dp[j] = 1;
}
int day = 0, ret = 0;
while (true) {
ret += D;
int pos = D;
for (int i = 0; i < D; i++)
if (dp[ret - i]) {
pos = i;
break;
}
if (pos == D) {
ret -= D;
break;
}
day++;
ret -= pos;
}
printf("%d %d\n", ret, day);
}
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n, d, mx = 0;
int c[60], ok[500010] = {0}, dp[500010];
vector<int> v;
int main() {
scanf("%d%d", &n, &d);
ok[0] = 1;
for (int i = 1; i <= n; i++) {
scanf("%d", &c[i]);
mx += c[i];
}
for (int i = 1; i <= n; i++)
for (int j = mx; j >= c[i]; j--) ok[j] |= ok[j - c[i]];
for (int i = 0; i <= mx; i++)
if (ok[i]) v.push_back(i);
memset(dp, -1, sizeof(dp));
dp[0] = 0;
int day = 0, val = 0, l = v.size();
for (int i = 0; i < l; i++) {
if (dp[i] < 0) continue;
int p = upper_bound(v.begin(), v.end(), v[i] + d) - v.begin() - 1;
if (p < 0) continue;
if (dp[p] < 0 || dp[i] + 1 <= dp[p]) {
dp[p] = dp[i] + 1;
day = dp[p];
val = v[p];
}
}
printf("%d %d\n", val, day);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 50 * 1e4 + 9;
bool dp[N];
int main() {
int n, d;
scanf("%d%d", &n, &d);
dp[0] = 1;
int mx = 0;
for (int i = 1; i <= n; ++i) {
int x;
scanf("%d", &x);
for (int j = mx += x; j >= x; --j) dp[j] |= dp[j - x];
}
int ans1 = 0, ans2 = 0;
for (int i = 0;;) {
bool ok = 0;
for (int j = min(mx, i + d); j > i; --j) {
if (dp[j]) {
i = j;
ans1 = i;
ans2++;
ok = 1;
break;
}
}
if (!ok) break;
}
printf("%d %d\n", ans1, ans2);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int MAXX = 500000;
int dp[MAXX + 2];
int main() {
int n, d, a;
cin >> n >> d;
dp[0] = 1;
for (int i = 0; i < n; i++) {
cin >> a;
for (int j = MAXX; j >= 0; j--)
if (dp[j] == 1 && j + a <= MAXX) dp[j + a] = 1;
}
int pos = 0, steps = 0;
while (1) {
int i, npos = pos;
for (i = pos; pos + d >= i && i <= MAXX; i++)
if (dp[i]) npos = i;
if (npos == pos)
break;
else
pos = npos, steps++;
}
cout << pos << " " << steps << endl;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
bool dp[1000005];
int main() {
int n, d, x;
scanf("%d%d", &n, &d);
int sum = 0;
dp[0] = true;
for (int i = 0; i < n; i++) {
scanf("%d", &x);
sum += x;
for (int j = sum; j >= x; j--) dp[j] |= dp[j - x];
}
int day = 0, cost = 0;
int i;
while (1) {
for (i = cost + d; i >= cost; i--)
if (dp[i]) break;
if (i == cost) break;
day++;
cost = i;
}
printf("%d %d\n", cost, day);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int dp[500010];
int c[100];
int main() {
int n, d;
scanf("%d%d", &n, &d);
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
dp[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 500000 - c[i]; j >= 0; j--) {
if (dp[j]) dp[j + c[i]] = 1;
}
}
int mas = 0;
int day = 0;
while (1) {
int x = mas + d;
if (x >= 500000) x = 500000;
while (x > mas && !dp[x]) x--;
if (x == mas) {
printf("%d %d\n", mas, day);
break;
}
mas = x;
day++;
}
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
bool dp[51 * 10001];
int main() {
int n, d, x, cnt = 0, sum = 0, w = 0;
cin >> n >> d;
dp[0] = 1;
for (int i = 0; i < n; i++) {
cin >> x;
for (int j = (sum += x); j >= x; j--) {
if (dp[j - x]) dp[j] = true;
}
}
while (true) {
int j = w + d;
while (!dp[j] && j > w) j--;
if (j == w) break;
w = j;
cnt++;
}
cout << w << " " << cnt << endl;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int mx = 50 + 5;
const int mxv = 10000 + 5;
const int mxs = mxv * mx;
int data[mx];
bool possible[mxs];
int possibles[mxs], np;
int main() {
int n, d, i, j, k, l, v, tmp;
cin >> n >> d;
for (i = 0; i < n; i++) cin >> data[i];
fill(possible, possible + mxs, 0);
possible[0] = 1;
possibles[0] = 0, np = 1;
for (i = 0; i < n; i++) {
tmp = np;
for (j = 0; j < tmp; j++) {
v = possibles[j] + data[i];
if (!possible[v]) {
possible[v] = true;
possibles[np++] = v;
}
}
}
sort(possibles, possibles + np);
int ndays = 0, sx = 0, bt, sy;
for (i = 0; i < np;) {
sy = 0;
for (; i < np && sx + d >= possibles[i]; i++) {
sy = possibles[i];
}
if (sy > 0)
ndays++, sx = sy;
else
break;
}
printf("%d %d\n", sx, ndays);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 51;
const int MAXVAL = MAXN * (1e4) + 123;
int main(void) {
static char bio[MAXVAL] = {0};
int n, d;
scanf("%d %d", &n, &d);
bio[0] = true;
for (int i = 0; i < (n); ++i) {
int x;
scanf("%d", &x);
for (int j = MAXVAL - 1; j >= 0; --j) {
if (bio[j] && j + x < MAXVAL) bio[j + x] = true;
}
}
int cost = 0, days = 0;
for (int i = 1; i < MAXVAL;) {
int last = -1;
for (; i <= cost + d; ++i)
if (bio[i]) last = i;
if (last != -1) {
cost = last;
++days;
} else {
break;
}
}
printf("%d %d\n", cost, days);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
#pragma comment(linker, "/stack:16777216")
using namespace std;
const double PI = acos(-1.0);
const int INF = 1000000000;
const int MAX = 2048;
const int MAX2 = 1000007;
const int MOD = 1000000007;
bool dp[500007];
int main() {
int n, d;
cin >> n >> d;
dp[0] = 1;
for (int i = (0); i < (n); ++i) {
int x;
cin >> x;
for (int j = (500007 - x) - 1; j >= (0); --j)
if (dp[j]) dp[j + x] = 1;
}
vector<int> a;
a.push_back(0);
for (int i = (1); i < (500007); ++i)
if (dp[i]) {
if (i - a.back() > d) break;
a.push_back(i);
}
int res = 0;
int id = 0;
while (id != a.size() - 1) {
++res;
if (a.back() - a[id] <= d) {
id = a.size() - 1;
} else {
for (int i = (id + 1); i < (a.size()); ++i) {
if (a[i] - a[id] > d) {
id = i - 1;
break;
}
}
}
}
cout << a.back() << ' ' << res << endl;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n, d;
int a[60];
int dp[60][500000 + 10];
void solve() {
int i, j, ans, days, temp;
bool flag;
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for (i = 0; i < n; i++)
for (j = 0; j <= 500000; j++)
if (dp[i][j]) {
dp[i + 1][j] = 1;
dp[i + 1][j + a[i]] = 1;
}
ans = 0;
days = 0;
while (true) {
flag = false;
for (i = ans + 1; i <= min(ans + d, 500000); i++)
if (dp[n][i]) {
temp = i;
flag = true;
}
if (!flag) break;
ans = temp;
days++;
}
printf("%d %d\n", ans, days);
}
void input() {
int i;
scanf("%d %d", &n, &d);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
solve();
}
int main() {
input();
fclose(stdin);
fclose(stdout);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
bool pack[52 * 10005];
int items[52];
int sum;
;
void Read(int n, int d) {
sum = 0;
for (int i = 0; i < n; ++i) {
scanf("%d", &items[i]);
sum += items[i];
}
}
void Solve(int n, int d) {
memset(pack, 0, sizeof(pack));
pack[0] = true;
for (int i = 0; i < n; ++i) {
for (int j = sum; j >= items[i]; --j) {
if (pack[j - items[i]]) {
pack[j] = true;
}
}
}
int last = -52 * 10005;
int buy = 0;
int step = 0;
for (int i = 1; i <= sum; ++i) {
if (pack[i]) {
if (last + d >= i) {
buy = i;
} else if (buy + d >= i) {
last = buy;
buy = i;
++step;
} else {
break;
}
}
}
printf("%d %d\n", buy, step);
}
int main() {
int n, d;
while (EOF != scanf("%d %d", &n, &d)) {
Read(n, d);
Solve(n, d);
}
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int N, D;
int A[55];
bool dp[500005];
int kk[500005];
int main() {
scanf("%d %d", &N, &D);
for (int c = 1; c <= N; c++) scanf("%d", &A[c]);
dp[0] = true;
for (int c = 1; c <= N; c++)
for (int d = 500000; d >= 0; d--)
if (dp[d]) dp[d + A[c]] = true;
int pt = 0;
for (int c = 1; c <= 500000; c++) {
if (dp[c]) pt = c;
kk[c] = pt;
}
int now = 0, day = 0;
while (now < 500000) {
if (kk[min(now + D, 500000)] == kk[now]) break;
now = kk[min(now + D, 500000)];
day++;
}
printf("%d %d\n", now, day);
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int MAX = 5e5;
int main() {
int n, D;
cin >> n >> D;
vector<int> c(n);
for (auto &ci : c) cin >> ci;
vector<int> dp(MAX + 1);
dp[0] = true;
for (auto ci : c) {
for (int j = MAX; j >= ci; j--) dp[j] |= dp[j - ci];
}
int cur = 0, cnt = 0;
bool update = true;
while (update) {
update = false;
for (int i = min(MAX, cur + D); i > cur; i--)
if (dp[i]) {
cur = i, cnt += update = true;
break;
}
}
cout << cur << " " << cnt << endl;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
bool dp[5 * 100000 + 100];
int c[55];
int main() {
int n, d;
int sum = 0;
cin >> n >> d;
for (int i = 0; i < n; i++) {
cin >> c[i];
sum += c[i];
}
dp[0] = 1;
for (int i = 0; i < n; ++i)
for (int j = sum; j >= c[i]; --j) dp[j] |= dp[j - c[i]];
int val = 0, cnt = 0;
while (1) {
int i, tmp = val;
for (i = val + d; i > val; --i)
if (i <= sum && dp[i]) {
val = i;
++cnt;
break;
}
if (i == tmp) break;
}
cout << val << " " << cnt << endl;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
bool dp[500050];
int trace[500050];
int n, d;
int a[55];
int main() {
scanf("%ld%ld", &n, &d);
for (int i = 1; i <= n; i++) scanf("%ld", &a[i]);
memset(dp, false, sizeof(dp));
dp[0] = true;
for (int i = 1; i <= n; i++)
for (int j = 500000; j >= 0; j--)
if ((dp[j] == true) && (j + a[i] <= 500000)) dp[j + a[i]] = true;
int pos;
for (int i = 0; i <= 500000; i++) {
if (dp[i] == true) pos = i;
trace[i] = pos;
}
int now = 0, day = 0;
while (now <= 500000) {
if (trace[min(now + d, 500000)] == now) break;
now = trace[min(now + d, 500000)];
day++;
}
printf("%ld %ld", now, day);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
const int MAXN = 55, MAX = 6e5 + 10, littleMAX = 5e5 + 10;
using namespace std;
int n, d, last, ant, cnt;
int c[MAXN];
bool dp[MAX];
int main() {
scanf("%d%d", &n, &d);
for (int i = 1; i < n + 1; i++) scanf("%d", &c[i]);
dp[0] = true;
for (int i = 1; i < n + 1; i++)
for (int j = littleMAX - c[i]; j >= 0; j--) dp[j + c[i]] |= dp[j];
while (true) {
int bef = ant;
for (int i = ant + d; i > last; i--)
if (dp[i]) {
last = ant + d;
ant = i;
break;
}
cnt++;
if (ant == bef) break;
}
printf("%d %d\n", ant, cnt - 1);
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
bool dp[555556];
int main() {
long long t, i, j, k, ans, n, d, sum;
t = 1;
while (t--) {
cin >> n >> d;
vector<long long> v(n);
sum = 0;
dp[0] = 1;
for (long long p = 0; p < n; p++) {
cin >> v[p];
for (long long q = 555555; q >= v[p]; q--) dp[q] |= dp[q - v[p]];
}
long long mx = 0, nx, days = 0;
while (1) {
nx = mx + d;
while (!dp[nx]) nx--;
if (nx == mx) break;
days++;
mx = nx;
}
cout << mx << " " << days;
}
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int f[5000010], d, n, w;
int main() {
cin >> n >> d;
f[0] = 1;
int sum = 0;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
sum += x;
for (int j = sum; j >= x; j--)
if (f[j - x]) f[j] = 1;
}
int j = 0, ans = 0;
while (1) {
j = w + d;
while (!f[j] && j > w) j--;
if (j == w) break;
w = j;
ans++;
}
cout << w << " " << ans;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int MX = (1 << 20), INF = (1 << 31) - 1;
int bit[MX], n, lim;
bool dp[MX];
void update(int x, int V) {
while (x > 0) {
bit[x] = min(bit[x], V);
x -= x & (-x);
}
}
int get(int x) {
if (x == 0) return 0;
int res = INF;
while (x <= lim) {
res = min(res, bit[x]);
x += x & (-x);
}
return res;
}
int arr[100], bfs[MX], K;
int main() {
cin >> n >> K;
for (int j = 1; j <= n; j++) {
cin >> arr[j];
lim += arr[j];
}
dp[0] = 1;
for (int j = 1; j <= lim; j++) bit[j] = INF;
for (int j = 1; j <= n; j++)
for (int i = lim; i >= arr[j]; i--) dp[i] |= dp[i - arr[j]];
int maxy = 0, days = 0;
for (int j = 1; j <= lim; j++) {
if (!dp[j]) continue;
int prev = max(0, j - K);
bfs[j] = get(prev);
if (bfs[j] == INF) break;
++bfs[j];
update(j, bfs[j]);
maxy = j;
days = bfs[j];
}
cout << maxy << ' ' << days << endl;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int dp[20000006], n, d, c[55], sum, j, now, day = -1;
int main() {
cin >> n >> d;
dp[0] = 1;
for (int i = 1; i <= n; i++) {
cin >> c[i];
sum += c[i];
for (j = sum; j >= c[i]; j--)
if (dp[j - c[i]] == 1) dp[j] = dp[j - c[i]];
}
while (1) {
day++;
j = now + d;
while (dp[j] == 0 && j > now) j--;
if (j == now) break;
now = j;
}
cout << j << " " << day << endl;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
int n, d, x, max, i, j, ans;
bool f[5100000];
int main() {
scanf("%d%d", &n, &d);
f[0] = true;
for (i = 1; i <= n; i++) {
scanf("%d", &x);
for (j = (max += x); j >= x; j--)
if (f[j - x]) f[j] = true;
}
x = 0;
while (1) {
j = ans + d;
while (!f[j] && (j > ans)) j--;
if (j == ans) break;
ans = j;
x++;
}
printf("%d %d\n", ans, x);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int INF = 1061109567;
const int MAXN = 510005;
int dp[MAXN];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout.precision(20);
int n, d;
cin >> n >> d;
memset(dp, 0, sizeof dp);
dp[0] = 1;
int items[n];
for (int i = 0; i < n; i++) cin >> items[i];
for (int j = 0; j < n; j++) {
for (int i = MAXN - items[j]; i >= 0; i--)
if (dp[i]) dp[i + items[j]] = 1;
}
int itCount = 0;
int i = 0;
while (i < 500000) {
bool flag = false;
for (int j = i + d; j >= i + 1; j--) {
if (dp[j]) {
itCount++;
i = j;
flag = true;
break;
}
}
if (!flag) break;
}
cout << i << " " << itCount << endl;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const double INF = 1e20;
const double eps = 1e-6;
int n, m, d, sum, ma, co, x;
int dp[500005];
int main() {
int i, j, k;
int T;
scanf("%d%d", &n, &d);
memset(dp, 0, sizeof(dp));
ma = co = sum = 0;
for (i = 1; i <= n; i++) {
scanf("%d", &x);
for (j = sum; j > 0; j--)
if (dp[j]) dp[j + x] = 1;
dp[x] = dp[sum += x] = 1;
}
while (1) {
for (i = min(ma + d, sum); i > ma; i--)
if (dp[i]) break;
if (i == ma) break;
co++;
ma = i;
}
printf("%d %d\n", ma, co);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.IntStream;
import static java.lang.Integer.min;
public class C365D {
private static int n, d;
private static int[] c;
private static boolean[] dp;
private static int maxSum;
public static void main(String[] args) throws IOException {
readInput();
PrintWriter out = new PrintWriter(System.out);
out.println(solve());
out.close();
}
private static void readInput() {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
d = sc.nextInt();
c = new int[n];
IntStream.range(0, n).forEach(i -> c[i] = sc.nextInt());
maxSum = Arrays.stream(c).sum();
sc.close();
}
private static String solve() {
reachableSum();
int value = 0;
int day = 0;
while (true) {
boolean canExchange = false;
for (int potential = min(value + d, maxSum); potential > value; potential--) {
if (dp[potential]) {
day++;
value = potential;
canExchange = true;
break;
}
}
if (!canExchange) break;
}
return value + " " + day;
}
private static void reachableSum() {
dp = new boolean[maxSum + 1];
dp[0] = true;
for (int i = 0; i < n; i++) {
for (int j = maxSum; j >= c[i]; j--) {
dp[j] = dp[j] || dp[j - c[i]];
}
}
}
} | JAVA |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n;
int d;
int a[57];
int pos[500007];
vector<int> v;
void input();
void solve();
int main() {
input();
solve();
return 0;
}
void input() {
scanf("%d%d", &n, &d);
int i, j;
pos[0] = 1;
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
for (j = 500000 - a[i]; j >= 0; j--) {
if (pos[j] == 1) pos[j + a[i]] = 1;
}
}
for (i = 0; i <= 500000; i++) {
if (pos[i] == 1) v.push_back(i);
}
}
void solve() {
int now = 0;
int cur = 0;
int days = 0;
int i;
for (i = 0; i < v.size(); i++) {
if (v[i] - now <= d)
cur = v[i];
else {
if (now != cur) days++;
now = cur;
if (v[i] - now <= d)
cur = v[i];
else
break;
}
}
if (now != cur) days++;
now = cur;
printf("%d %d\n", now, days);
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n, m, sum;
bool f[520000];
int main() {
scanf("%d%d", &n, &m);
f[0] = true;
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
sum += x;
for (int j = sum; j >= x; j--) {
f[j] |= f[j - x];
}
}
int ans = 0, tot = 0;
while (true) {
int pre_ans = ans;
ans += m;
for (; !f[ans]; ans--)
;
if (ans == pre_ans) {
break;
}
tot++;
}
printf("%d %d\n", ans, tot);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 500010;
bool f[MAXN];
int main() {
int n, d;
scanf("%d %d", &n, &d);
f[0] = 1;
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
for (int j = MAXN - 1 - x; j >= 0; --j) f[j + x] |= f[j];
}
int r = 1, sum = 0, ans = 0;
while (r < MAXN) {
int next = -1;
while (r <= sum + d && r < MAXN) {
if (f[r]) next = r;
r++;
}
if (next == -1) break;
sum = next;
ans++;
}
printf("%d %d\n", sum, ans);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
bool f[1000 * 1000];
int s, ans, j, c;
int main() {
int n, d;
cin >> n >> d;
f[0] = 1;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
s += x;
for (int j = s; j >= x; j--)
if (f[j - x]) f[j] = 1;
}
while (1) {
j = ans;
ans += d;
while (!f[ans]) ans--;
if (j == ans) break;
c++;
}
cout << ans << " " << c;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int d[600050];
int a[100];
int main() {
int x, n;
scanf("%d%d", &n, &x);
for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
sort(a, a + n);
int sum = 0;
d[0] = 1;
for (int i = 0; i < n; ++i) {
if (sum + x < a[i]) break;
for (int j = sum; j >= 0; --j) {
if (d[j]) d[j + a[i]] = 1;
}
sum += a[i];
}
int cnt = 0;
int now = 0;
while (true) {
int pos = -1;
for (int i = now + x; i > now; --i) {
if (d[i]) {
pos = i;
break;
}
}
if (pos <= now) break;
cnt++;
now = pos;
}
printf("%d %d\n", sum, cnt);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed;
cout.precision(12);
solve();
return 0;
}
template <typename T>
void sc(T& x) {
cin >> x;
}
template <typename Head, typename... Tail>
void sc(Head& head, Tail&... tail) {
cin >> head;
sc(tail...);
}
template <typename T>
void pr(const T& x) {
cout << x << '\n';
}
template <typename Head, typename... Tail>
void pr(const Head& head, const Tail&... tail) {
cout << head << ' ';
pr(tail...);
}
const int MAX_SUM = 5e5 + 10;
bool vis[MAX_SUM];
void solve() {
fill(vis, vis + MAX_SUM, false);
int n, d;
sc(n, d);
vector<int> oks;
oks.push_back(0);
for (int i = 0; i < n; i++) {
int x;
sc(x);
for (int j = ((int)oks.size()) - 1; j >= 0; j--) {
if (x + oks[j] < MAX_SUM && !vis[x + oks[j]]) {
oks.push_back(x + oks[j]);
vis[x + oks[j]] = true;
}
}
}
sort(oks.begin(), oks.end());
int ans = 0, cur = 0;
while (true) {
int idx =
upper_bound(oks.begin(), oks.end(), oks[cur] + d) - oks.begin() - 1;
if (idx >= ((int)oks.size()) || idx == cur) {
break;
}
ans++;
cur = idx;
}
pr(oks[cur], ans);
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int dp[550005];
int main() {
int n, d, c, i, j, x, flog;
scanf("%d %d", &n, &d);
dp[0] = 1;
for (i = 0; i < n; i++) {
scanf("%d", &c);
for (j = 500000; j >= c; j--) {
dp[j] |= dp[j - c];
}
}
int day = 0;
x = 0;
while (1) {
flog = 0;
for (j = x + d; j > x; j--) {
if (dp[j]) {
flog = 1;
day++;
x = j;
break;
}
}
if (!flog) break;
}
printf("%d %d\n", x, day);
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int MAXS = 50 * 10000;
const int INF = 1000000000;
int main() {
ios_base::sync_with_stdio(0);
int n, d;
cin >> n >> d;
vector<int> c(n);
for (int i = 0; i < n; ++i) cin >> c[i];
vector<bool> sumy(MAXS + 1, false);
sumy[0] = true;
for (int i = 0; i < n; ++i)
for (int j = MAXS; j >= 0; --j)
if (j - c[i] >= 0 && sumy[j - c[i]]) sumy[j] = true;
vector<int> dp(MAXS + 1);
multiset<int> ost;
dp[0] = 0;
for (int i = 1; i <= MAXS; ++i) {
ost.insert(dp[i - 1]);
if (i - d - 1 >= 0) ost.erase(ost.find(dp[i - d - 1]));
if (*ost.begin() != INF && sumy[i])
dp[i] = *ost.begin() + 1;
else
dp[i] = INF;
}
int i = MAXS;
while (dp[i] == INF) --i;
cout << i << " " << dp[i];
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 500000;
bool ok[N + 10];
int dist[N + 10];
int main() {
int n, d;
cin >> n >> d;
vector<int> c(n);
for (int i = 0; i < (n); i++) cin >> c[i];
ok[0] = 1;
for (int i = 0; i < (n); i++) {
for (int j = N; j >= c[i]; j--) {
if (ok[j - c[i]]) ok[j] = 1;
}
}
vector<int> v;
for (int i = 0; i < (N + 10); i++) {
if (ok[i]) v.push_back(i);
}
memset(dist, -1, sizeof(dist));
dist[0] = 0;
for (int i = 0; i < (v.size()); i++) {
if (dist[i] < 0) continue;
int pos = upper_bound((v).begin(), (v).end(), v[i] + d) - v.begin() - 1;
if (pos < 0) continue;
if (dist[pos] < 0 || dist[pos] > dist[i] + 1) {
dist[pos] = dist[i] + 1;
}
}
int ans = 0, days = 0;
for (int i = v.size() - 1; i >= 0; i--) {
if (dist[i] > 0) {
ans = v[i], days = dist[i];
break;
}
}
cout << ans << ' ' << days << endl;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, d, sum = 0;
cin >> n >> d;
vector<int> dp(1000000, 0);
dp[0] = 1;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
sum += a;
for (int j = sum; j >= a; j--) dp[j] |= dp[j - a];
}
int i;
int days = 0, cost = 0;
while (1) {
for (i = cost + d; i >= cost; i--)
if (dp[i]) break;
if (i == cost) break;
cost = i;
days++;
}
cout << cost << " " << days << endl;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | import java.io.*;
import java.util.*;
public class D implements Runnable {
private void solve() throws IOException {
int n = nextInt(), d = nextInt(), sum = 0;
int[] c = new int[n];
for (int i = 0; i < n; i++) c[i] = nextInt();
for (int i = 0; i < n; i++) sum += c[i];
boolean[][] dp = new boolean[n + 1][sum + 1];
for (int i = 0; i <= n; i++) dp[i][0] = true;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= sum; j++) {
dp[i][j] = dp[i - 1][j];
if (c[i - 1] <= j) dp[i][j] |= dp[i - 1][j - c[i - 1]];
}
int cur = 0, steps = 0;
while (true) {
boolean found = false;
for (int i = Math.min(cur + d, sum); i > cur; i--)
for (int j = 1; j <= n; j++)
if (dp[j][i]) {
found = true;
cur = i;
break;
}
if (found) steps++; else break;
}
print(cur, steps);
}
public static void main(String[] args) {
new D().run();
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
public void run() {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
writer = new PrintWriter(System.out);
solve();
writer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(261);
}
}
void halt() {
writer.close();
System.exit(0);
}
void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
void println(Object... objects) {
print(objects);
writer.println();
}
String nextLine() throws IOException {
return reader.readLine();
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(nextLine());
return tokenizer.nextToken();
}
int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextToken());
}
}
| JAVA |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
int dp[500010];
int main() {
int n, d;
scanf("%d %d", &n, &d);
dp[0] = 1;
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
for (int i = 499999; i >= 0; i--) {
if (i + x > 500000) continue;
dp[i + x] |= dp[i];
}
}
int st = 0, nxt = -1;
int ans = 0;
for (int i = 1; i <= 500000; i++) {
if (dp[i] == 0) continue;
if (i - st > d) {
if (nxt == -1) break;
st = nxt;
ans++;
nxt = -1;
}
if (i - st <= d) nxt = i;
}
if (nxt != -1) {
st = nxt;
ans++;
}
printf("%d %d\n", st, ans);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
int vis[5000005] = {0};
int main() {
int n, d, i, j, a[55] = {0};
scanf("%d%d", &n, &d);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
vis[0] = 1;
for (i = 0; i < n; i++)
for (j = n * 10000; j >= 0; j--) {
if (j - a[i] >= 0 && vis[j - a[i]] == 1) {
vis[j] = 1;
}
}
int ans = 0, co = 0;
while (ans <= n * 10000) {
int x = ans + d;
while (vis[x] == 0) x--;
if (x == ans)
break;
else
ans = x;
co++;
}
printf("%d %d\n", ans, co);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int M = 10000 * 50 + 1;
int v[50], valid[M], days[M];
int main() {
int n, d;
cin >> n >> d;
for (int i = 0; i < n; i++) cin >> v[i];
sort(v, v + n);
valid[0] = 1;
for (int i = 0; i < n; i++) {
int can = 0, d1 = M;
for (int j = 0; j < M; j++)
if (valid[j] && !(j + d < v[i])) can = 1;
if (can)
for (int j = M - v[i] - 1; j >= 0; j--)
if (valid[j]) valid[j + v[i]] = 1;
}
for (int i = 0; i < M; i++) days[i] = 1 << 30;
days[0] = 0;
for (int i = 1; i <= d; i++)
if (valid[i]) days[i] = 1;
for (int i = d + 1; i < M; i++)
if (valid[i])
for (int j = i - d; j <= i; j++)
if (valid[j]) {
days[i] = days[j] + 1;
break;
}
for (int i = M - 1; i >= 0; i--)
if (valid[i]) {
cout << i << " " << days[i] << endl;
break;
}
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n, d, w, nex, cnt;
int a[55], f[550005];
int inline read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int main() {
n = read(), d = read();
f[0] = 1;
for (int i = 1; i <= n; i++) {
a[i] = read();
for (int j = 500000; j >= a[i]; j--) f[j] |= f[j - a[i]];
}
w = 0;
while (1) {
nex = w + d;
while (nex > w && !f[nex]) nex--;
if (nex == w) break;
w = nex;
cnt++;
}
printf("%d %d", w, cnt);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n, arr[55], d;
bool can[520005];
int vis[55][500005];
void solve(int idx, int sum) {
if (idx == n) {
can[sum] = 1;
return;
}
if (vis[idx][sum]) return;
vis[idx][sum] = 1;
solve(idx + 1, sum);
solve(idx + 1, sum + arr[idx]);
}
int main() {
cin >> n >> d;
for (int i = 0; i < n; ++i) scanf("%d", &arr[i]);
solve(0, 0);
int cur = 0, s = 0, f = 1;
while (f) {
f = 0;
for (int i = cur + d; i > cur; i--) {
if (can[i]) {
f = 1;
cur = i;
s++;
break;
}
}
}
cout << cur << " " << s << endl;
};
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n, d;
int a[51], dp[51 * 10000], sum;
int main() {
memset(dp, 0, sizeof(dp));
cin >> n >> d;
sum = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
}
dp[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = sum; j >= a[i]; j--)
if (dp[j - a[i]]) dp[j] = 1;
}
int x = 0;
for (int i = 1;; i++) {
int t = 0;
for (int j = x + d; j >= x + 1; j--) {
if (dp[j]) {
t = j;
break;
}
}
if (t == 0) {
printf("%d %d\n", x, i - 1);
break;
} else
x = t;
}
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.SortedSet;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import javax.net.ssl.SSLContext;
public class Main
{
/********************************************** a list of common variables **********************************************/
private MyScanner scan = new MyScanner();
private PrintWriter out = new PrintWriter(System.out);
private final double PI = Math.acos(-1.0);
private final double EPS = 1e-6;
private final int SIZEN = (int)(1e5);
private final int MOD = (int)(1e9 + 7);
private final long MODH = 10000000007L, BASE = 10007;
private final int[] DX = {0, 1, 0, -1}, DY = {-1, 0, 1, 0};
private ArrayList<Integer>[] edge;
public void foo()
{
int n = scan.nextInt();
int d = scan.nextInt();
int[] a = new int[n];
for(int i = 0;i < n;++i) a[i] = scan.nextInt();
boolean[] dp = new boolean[510001];
int sum = 0;
dp[0] = true;
for(int i = 0;i < n;++i)
{
for(int j = sum + a[i];j >= a[i];--j)
{
if(dp[j - a[i]]) dp[j] = true;
}
sum += a[i];
}
int pre = 0, day = 0;
while(true)
{
int i = pre + d;
while(!dp[i] && i > pre) --i;
if(i == pre) break;
pre = i;
++day;
}
out.println(pre + " " + day);
}
public static void main(String[] args)
{
Main m = new Main();
m.foo();
m.out.close();
}
/********************************************** a list of common algorithms **********************************************/
/**
* 1---Get greatest common divisor
* @param a : first number
* @param b : second number
* @return greatest common divisor
*/
public long gcd(long a, long b)
{
return 0 == b ? a : gcd(b, a % b);
}
/**
* 2---Get the distance from a point to a line
* @param x1 the x coordinate of one endpoint of the line
* @param y1 the y coordinate of one endpoint of the line
* @param x2 the x coordinate of the other endpoint of the line
* @param y2 the y coordinate of the other endpoint of the line
* @param x the x coordinate of the point
* @param y the x coordinate of the point
* @return the distance from a point to a line
*/
public double getDist(long x1, long y1, long x2, long y2, long x, long y)
{
long a = y2 - y1;
long b = x1 - x2;
long c = y1 * (x2 - x1) - x1 * (y2 - y1);
return Math.abs(a * x + b * y + c) / Math.sqrt(a * a + b * b);
}
/**
* 3---Get the distance from one point to a segment (not a line)
* @param x1 the x coordinate of one endpoint of the segment
* @param y1 the y coordinate of one endpoint of the segment
* @param x2 the x coordinate of the other endpoint of the segment
* @param y2 the y coordinate of the other endpoint of the segment
* @param x the x coordinate of the point
* @param y the y coordinate of the point
* @return the distance from one point to a segment (not a line)
*/
public double ptToSeg(long x1, long y1, long x2, long y2, long x, long y)
{
double cross = (x2 - x1) * (x - x1) + (y2 - y1) * (y - y1);
if(cross <= 0)
{
return (x - x1) * (x - x1) + (y - y1) * (y - y1);
}
double d = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
if(cross >= d)
{
return (x - x2) * (x - x2) + (y - y2) * (y - y2);
}
double r = cross / d;
double px = x1 + (x2 - x1) * r;
double py = y1 + (y2 - y1) * r;
return (x - px) * (x - px) + (y - py) * (y - py);
}
/**
* 4---KMP match, i.e. kmpMatch("abcd", "bcd") = 1, kmpMatch("abcd", "bfcd") = -1.
* @param t: String to match.
* @param p: String to be matched.
* @return if can match, first index; otherwise -1.
*/
public int kmpMatch(char[] t, char[] p)
{
int n = t.length;
int m = p.length;
int[] next = new int[m + 1];
next[0] = -1;
int j = -1;
for(int i = 1;i < m;++i)
{
while(j >= 0 && p[i] != p[j + 1])
{
j = next[j];
}
if(p[i] == p[j + 1])
{
++j;
}
next[i] = j;
}
j = -1;
for(int i = 0;i < n;++i)
{
while(j >= 0 && t[i] != p[j + 1])
{
j = next[j];
}
if(t[i] == p[j + 1])
{
++j;
}
if(j == m - 1)
{
return i - m + 1;
}
}
return -1;
}
/**
* 5---Get the hash code of a String
* @param s: input string
* @return hash code
*/
public long hash(String s)
{
long key = 0, t = 1;
for(int i = 0;i < s.length();++i)
{
key = (key + s.charAt(i) * t) % MODH;
t = t * BASE % MODH;
}
return key;
}
/**
* 6---Get x ^ n % MOD quickly.
* @param x: base
* @param n: times
* @return x ^ n % MOD
*/
public long quickMod(long x, long n)
{
long ans = 1;
while(n > 0)
{
if(1 == n % 2)
{
ans = ans * x % MOD;
}
x = x * x % MOD;
n >>= 1;
}
return ans;
}
/**
* 7---judge if a point is located inside a polygon
* @param x0 the x coordinate of the point
* @param y0 the y coordinate of the point
* @return true if it is inside the polygon, otherwise false
*/
/*public boolean contains(double x0, double y0)
{
int cross = 0;
for(int i = 0;i < n;++i)
{
double s = x[i + 1] == x[i] ? 100000000 : (double)(y[i + 1] - y[i]) / (x[i + 1] - x[i]);
boolean b1 = x[i] <= x0 && x0 < x[i + 1];
boolean b2 = x[i + 1] <= x0 && x0 < x[i];
boolean b3 = y0 < s * (x0 - x[i]) + y[i];
if((b1 || b2) && b3) ++cross;
}
return cross % 2 != 0;
}*/
/**
* 8---judge if a point is located on the segment
* @param x1 the x coordinate of one point of the segment
* @param y1 the y coordinate of one point of the segment
* @param x2 the x coordinate of another point of the segment
* @param y2 the y coordinate of another point of the segment
* @param x the x coordinate of the point
* @param y the y coordinate of the point
* @return true if it is located on the segment, otherwise false
*/
public boolean isOnSeg(long x1, long y1, long x2, long y2, long x, long y)
{
return (x - x1) * (y2 - y1) == (x2 - x1) * (y - y1) &&
x >= Math.min(x1, x2) && x <= Math.max(x1, x2) &&
y >= Math.min(y1, y2) && y <= Math.max(y1, y2);
}
/**
* 9---get the cross product
* @param p1 point A
* @param p2 point B
* @param p point O
* @return cross product of OA x OB
*/
/*public long cross(Point p1, Point p2, Point p)
{
return (long)(p1.x - p.x) * (p2.y - p.y) - (long)(p2.x - p.x) * (p1.y - p.y);
}*/
class MyScanner
{
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
BufferedInputStream bis = new BufferedInputStream(System.in);
public int read()
{
if (-1 == numChars)
{
throw new InputMismatchException();
}
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = bis.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if (numChars <= 0)
{
return -1;
}
}
return buf[curChar++];
}
public int nextInt()
{
int c = read();
while (isSpaceChar(c))
{
c = read();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
int res = 0;
do
{
if (c < '0' || c > '9')
{
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = read();
while (isSpaceChar(c))
{
c = read();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
long res = 0;
do
{
if (c < '0' || c > '9')
{
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble()
{
int c = read();
while (isSpaceChar(c))
{
c = read();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
{
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9')
{
throw new InputMismatchException();
}
res *= 10;
res += c & 15;
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
{
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9')
{
throw new InputMismatchException();
}
m /= 10;
res += (c & 15) * m;
c = read();
}
}
return res * sgn;
}
public String next()
{
int c = read();
while (isSpaceChar(c))
{
c = read();
}
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c)
{
return ' ' == c || '\n' == c || '\r' == c || '\t' == c || -1 == c;
}
}
} | JAVA |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.IntStream;
import static java.lang.Integer.min;
public class C365D {
private static int n, d;
private static int[] c;
private static boolean[] dp;
private static int maxSum;
public static void main(String[] args) throws IOException {
readInput();
PrintWriter out = new PrintWriter(System.out);
out.println(solve());
out.close();
}
private static void readInput() {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
d = sc.nextInt();
c = new int[n];
IntStream.range(0, n).forEach(i -> c[i] = sc.nextInt());
maxSum = Arrays.stream(c).sum();
sc.close();
}
private static String solve() {
reachableSum();
int value = 0;
int day = 0;
while (true) {
boolean canExchange = false;
for (int potential = min(value + d, maxSum); potential > value; potential--) {
if (dp[potential]) {
day++;
value = potential;
canExchange = true;
break;
}
}
if (!canExchange) break;
}
return value + " " + day;
}
private static void reachableSum() {
dp = new boolean[maxSum + 1];
dp[0] = true;
for (int i = 0; i < n; i++) {
for (int j = maxSum; j >= 0; j--) {
if (dp[j] && j + c[i] <= maxSum) {
dp[j + c[i]] = true;
}
}
}
}
} | JAVA |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 10000 + 233;
bool ok[maxn * 55];
int f[maxn * 55];
vector<int> a;
int main() {
int n, d;
cin >> n >> d;
memset(ok, 0, sizeof(ok));
ok[0] = 1;
int sum = 0;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
for (int i = sum; i >= 0; i--)
if (ok[i]) ok[i + x] = 1;
sum += x;
}
for (int i = 0; i <= sum; i++)
if (ok[i]) a.push_back(i);
memset(f, -1, sizeof(f));
f[0] = 0;
int ans = 0, buf = 0;
for (int i = 1; i < a.size(); i++) {
int k = lower_bound(a.begin(), a.end(), a[i] - d) - a.begin();
if (a[i] == a[k]) break;
f[a[i]] = f[a[k]] + 1;
ans = a[i];
buf = f[a[i]];
}
cout << ans << " " << buf << endl;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n, m, a[55], d, nb, b;
bool p[800005];
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
p[0] = true;
for (int i = 0; i < n; i++) {
for (int j = 500000; j >= 0; j--) {
if (p[j]) {
p[j + a[i]] = true;
}
}
}
while (1) {
for (int i = 1; i <= m; i++)
if (p[b + i]) nb = b + i;
if (nb == b) break;
b = nb;
d++;
}
cout << b << ' ' << d;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* @author Don Li
*/
public class FreeMarket2 {
int V = (int) 5e5 + 10;
void solve() {
int n = in.nextInt(), d = in.nextInt();
int[] c = new int[n];
for (int i = 0; i < n; i++) c[i] = in.nextInt();
int[] dp = new int[V];
dp[0] = 1;
for (int i = 0; i < n; i++) {
for (int j = V - 1; j >= c[i]; j--) {
dp[j] |= dp[j - c[i]];
}
}
int cnt = 0;
for (int i = 0; i < V; i++) if (dp[i] > 0) cnt++;
int[] sta = new int[cnt];
for (int i = 0, j = 0; i < V; i++) if (dp[i] > 0) sta[j++] = i;
int days = 0, p = 0;
while (p < cnt) {
int q = p;
while (q + 1 < cnt && sta[q + 1] <= sta[p] + d) q++;
if (p < q) {
days++;
p = q;
} else {
break;
}
}
out.printf("%d %d%n", sta[p], days);
}
public static void main(String[] args) {
in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
new FreeMarket2().solve();
out.close();
}
static FastScanner in;
static PrintWriter out;
static class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
| JAVA |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
long n, k, ar[200000];
long long reach[777777];
long long q;
long long ans[777777];
multiset<long long> bst;
multiset<long long>::iterator it;
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> ar[i];
reach[0] = 1;
for (int i = 1; i <= 666666; i++) reach[i] = 0;
for (int i = 1; i <= n; i++)
for (int j = 555555; j + 1; --j)
if (reach[j]) reach[j + ar[i]] = 1;
ans[0] = 0;
bst.insert(0);
for (int i = 1; i <= 666666; i++) {
ans[i] = 1e18;
if (i > k && ans[i - k - 1] < 1e18) {
it = bst.find(ans[i - k - 1]);
bst.erase(it);
}
if (reach[i] == 0) continue;
if (bst.size() == 0) continue;
it = bst.begin();
q = (*it);
ans[i] = q + 1;
bst.insert(ans[i]);
}
q = 666666;
while (ans[q] > 1e17) --q;
cout << q << " " << ans[q] << endl;
cin.get();
cin.get();
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n, m, i, j, dp[555555], ans, sum, x;
int main() {
cin >> n >> m;
for (i = 1; i <= n; i++) {
cin >> x;
for (j = 500000; j >= x; j--) {
dp[j] |= dp[j - x] || j - x == 0;
}
}
bool d = 1;
while (d && ++ans) {
d = 0;
for (i = m + sum; i > sum; i--) {
if (dp[i]) {
d = 1;
sum = i;
break;
}
}
}
cout << sum << ' ' << ans - 1;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int mod = 1000000007;
int main() {
int n, d, sum = 0;
cin >> n >> d;
vector<int> c(n);
for (int i = 0; i < n; ++i) {
cin >> c[i];
sum += c[i];
}
vector<bool> can(sum + 1);
can[0] = true;
for (int i = 1; i <= sum; ++i) can[i] = false;
for (int i = 0; i < n; ++i) {
for (int j = sum - c[i]; j >= 0; --j)
if (can[j]) can[j + c[i]] = true;
}
int days = 0, ans = 0;
bool uze;
while (1) {
uze = false;
for (int i = min(sum, ans + d); i > ans; --i) {
if (can[i]) {
++days;
ans = i;
uze = true;
break;
}
}
if (!uze) break;
}
printf("%d %d\n", ans, days);
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 51 * 10000 + 10;
bitset<maxn> bs;
int main() {
int n, d;
scanf("%d%d", &n, &d);
bs.reset();
bs.set(0);
int sum = 0;
while (n--) {
int c;
scanf("%d", &c);
sum += c;
for (int i = sum; i >= c; i--)
if (bs.test(i - c)) bs.set(i);
}
int day = 0, cur = 0;
while (1) {
bool ok = 0;
for (int i = cur + d; i > cur; i--) {
if (bs.test(i)) {
cur = i;
ok = 1;
break;
}
}
if (!ok) break;
++day;
}
printf("%d %d\n", cur, day);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int dp[5000100];
int main() {
int i, j, n, d;
int cnt = 0, sum = 0, c = 0;
dp[0] = 1;
cin >> n >> d;
int p;
for (i = 0; i < n; i++) {
cin >> p;
for (j = (sum += p); j >= p; j--)
if (dp[j - p] == 1) dp[j] = 1;
}
while (1 == 1) {
j = d + c;
while (!dp[j] && j > c) j--;
if (j == c) break;
c = j;
cnt++;
}
cout << c << ' ' << cnt << endl;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
const int MOD = 1e9 + 7;
int dp[52][maxn];
int c[52];
void calc(int n, int maxW) {
for (int i = 1; i <= n; ++i) {
for (int w = 0; w < c[i - 1]; ++w) dp[i][w] = dp[i - 1][w];
for (int w = c[i - 1]; w <= maxW; ++w)
dp[i][w] = max(dp[i - 1][w - c[i - 1]] + c[i - 1], dp[i - 1][w]);
}
}
int main() {
int n, d;
scanf("%d %d", &n, &d);
for (int i = 0; i < n; ++i) scanf("%d ", &(c[i]));
int maxW = accumulate(c, c + n, 0);
calc(n, maxW);
int ans = 0, days = 0;
while (1) {
int max_cost = min(ans + d, maxW);
int new_ans = dp[n][max_cost];
if (ans == new_ans) break;
ans = new_ans;
days++;
}
cout << ans << " " << days << endl;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const double eps = 1e-8;
const int maxn = 511010;
int a[maxn];
int dp[maxn];
int main() {
int n, d;
while (cin >> n >> d) {
for (int i = (1); i <= (n); ++i) scanf("%d", &a[i]);
memset(dp, 0, sizeof(dp));
dp[0] = 1;
for (int i = (1); i <= (n); ++i) {
for (int j = (50 * 10000); j >= (0); --j) {
if (dp[j] && j + a[i] <= 50 * 10000) dp[j + a[i]] = 1;
}
}
int sum = 0, num = 0;
while (1) {
int flag = 0;
for (int i = (sum + d); i >= (sum + 1); --i) {
if (i > 50 * 10000) continue;
if (dp[i]) {
sum = i;
num++;
flag = 1;
break;
}
}
if (flag == 0) break;
}
cout << sum << " " << num << endl;
}
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args) throws IOException
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int d = in.nextInt();
int[] c = new int[n];
for(int i = 0; i < n; ++i)
c[i] = in.nextInt();
boolean[] feasible = new boolean[500001];
feasible[0] = true;
int sum = 0;
for(int ci: c)
{
for(int i = sum; i >=0; --i)
feasible[i + ci] |= feasible[i];
sum += ci;
}
ArrayList<Integer> values = new ArrayList<Integer>();
for(int i = 0; i <= sum; ++i)
if(feasible[i])
values.add(i);
values.add(Integer.MAX_VALUE);
int step = 0, result = 0;
int index = 0;
while(true)
{
int originIndex = index;
while(result + d >= values.get(index))
++index;
--index;
if(originIndex == index)
break;
++step;
result = values.get(index);
}
System.out.println(result + " " + step);
}
}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
class FunLib
{
public static long gcd(long a, long b)
{
if(b == 0)
return a;
return gcd(b, a % b);
}
public static int gcd(int a, int b)
{
if(b == 0)
return a;
return gcd(b, a % b);
}
public static long lcm(long a, long b)
{
return a / gcd(a, b) * b;
}
public static int[][] transpose(int[][] mat)
{
int r = mat.length;
int c = mat[0].length;
int[][] result = new int[c][r];
for(int i = 0; i < c; ++i)
for(int j = 0; j < r; ++j)
result[i][j] = mat[j][i];
return result;
}
}
abstract class AbstractSegTree
{
public AbstractSegTree(int aSize)
{
size = aSize;
storage = new int[4 * size];
}
public int getVal(int index)
{
return getValHelper(0, 0, size - 1, index);
}
public void rangeSet(int lower, int upper, int val)
{
rangeSetHelper(0, 0, size - 1, lower, upper, val);
}
protected abstract int getValHelper(int node, int beg, int end, int index);
protected abstract void rangeSetHelper(int node, int beg, int end, int lower, int upper, int val);
protected int[] storage;
protected int size;
}
class Trie
{
public Trie()
{
currentNode = root;
}
public void reset()
{
currentNode = root;
}
public void add(String word)
{
currentNode = root;
for(int i = 0; i < word.length(); ++i)
{
// Only upper case characters
int nodeIndex = word.charAt(i) - 'A';
if(currentNode.children[nodeIndex] == null)
currentNode.children[nodeIndex] = new Node();
currentNode = currentNode.children[nodeIndex];
}
currentNode.isWord = true;
currentNode = root;
}
public boolean feed(char c)
{
if(currentNode.children[c - 'A'] == null)
return false;
currentNode = currentNode.children[c - 'A'];
return true;
}
public boolean isCurrentWord()
{
return currentNode.isWord;
}
private static class Node
{
public boolean isWord;
// Only upper case characters
public Node[] children = new Node[26];
}
private Node root = new Node();
private Node currentNode;
} | JAVA |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
bool flag[500005];
int main() {
int n, k, i, j, in, ans = 0, now = 0;
scanf("%d%d", &n, &k);
vector<int> pos;
pos.push_back(0);
for (i = 0; i < n; i++) {
scanf("%d", &in);
for (j = pos.size(); j--;) {
if (pos[j] + in < 500005 && flag[pos[j] + in] == 0) {
pos.push_back(pos[j] + in);
flag[pos[j] + in] = 1;
}
}
}
sort(pos.begin(), pos.end());
for (i = 0; i < pos.size(); ans++) {
for (; i < pos.size() && now + k >= pos[i]; i++)
;
if (pos[i - 1] != now)
now = pos[i - 1];
else
break;
}
printf("%d %d\n", now, ans);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n, d;
int maxi;
bool book[5000001];
int main() {
std::ios::sync_with_stdio(false);
int a;
cin >> n >> d;
book[0] = 1;
for (register int i = 1; i <= n; i++) {
cin >> a;
for (register int j = 5000000; j >= a; j--) book[j] |= book[j - a];
}
int ans = 0, tmp, x = 0;
bool st = 0;
while (1) {
st = 0;
for (tmp = x + d; tmp > x; tmp--) {
if (book[tmp]) {
st = 1;
ans++;
x = tmp;
break;
}
}
if (!st) break;
}
cout << tmp << ' ' << ans;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX;
const long long INFL = LLONG_MAX;
const int output_precision = 15;
const bool debug = true;
stringstream ss;
int N, D, C[1000];
bool f[500100];
int main() {
ios_base::sync_with_stdio(0);
cout.precision(output_precision);
cout << fixed;
ss.precision(output_precision);
ss << fixed;
cin >> N >> D;
for (int(i) = 1, j1234 = N; (i) <= j1234; (i)++) cin >> C[i];
sort(C + 1, C + N + 1);
f[0] = 1;
for (int(i) = 1, j1234 = N; (i) <= j1234; (i)++)
for (int ii = N * 10000 - C[i] + 1; ii >= 0; ii--)
if (f[ii]) f[C[i] + ii] = 1;
int from = 0;
int cnt = 0;
while (true) {
int mx = -1;
int cur = from + 1;
while (cur <= N * 10000 && from + D >= cur) {
if (f[cur]) {
mx = cur;
}
cur++;
}
if (mx == -1) {
break;
} else {
cnt++;
from = mx;
}
}
cout << from << " " << cnt << '\n';
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n, d, all = 0, ans;
bool f[900001];
int g[901001], c[100];
map<int, int> ma;
int main() {
srand(time(NULL));
ios_base::sync_with_stdio(0);
f[0] = 1;
cin >> n >> d;
for (int i = 1; i <= n; ++i) {
cin >> c[i];
all += c[i];
}
for (int i = 1; i <= n; ++i) {
for (int j = all; j >= c[i]; --j)
if (f[j - c[i]]) {
f[j] = 1;
}
}
for (int i = 1; i <= all; ++i) g[i] = 1000000000;
int l = 0;
g[0] = 0;
ma[0]++;
for (int i = 1; i <= all; ++i) {
int r = i;
while (r - l > d) {
--ma[g[l]];
if (ma[g[l]] == 0) ma.erase(g[l]);
++l;
}
if (!f[r]) {
++ma[g[r]];
continue;
}
int tmp = ma.begin()->first;
if (tmp < 1000000000) {
ans = i;
g[ans] = tmp + 1;
}
++ma[g[r]];
}
cout << ans << " " << g[ans];
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int arr[50 + 5];
bool Sum[(int)1e4 * 50 + 5];
vector<int> vec;
int main() {
int N, D, ans = 0, maxi = 0, tot = 0;
scanf("%d %d", &N, &D);
for (int i = 0; i < N; i++) {
scanf("%d", &arr[i]);
tot += arr[i];
}
Sum[0] = 1;
for (int i = 0; i < N; i++) {
for (int j = tot - arr[i]; j >= 0; j--)
if (Sum[j] && !Sum[j + arr[i]]) {
vec.push_back(j + arr[i]);
Sum[j + arr[i]] = 1;
}
}
sort(vec.begin(), vec.end());
while (1) {
int low = lower_bound(vec.begin(), vec.end(), maxi + D) - vec.begin();
if (low == (int)vec.size() || vec[low] > maxi + D) low--;
if (vec[low] <= maxi || vec[low] > maxi + D) break;
maxi = vec[low];
ans++;
}
printf("%d %d\n", maxi, ans);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
bool dp[1000000];
int n, k, i, j, ans, d, res, x, calc = 0;
int main() {
ios::sync_with_stdio(false);
scanf("%d %d", &n, &d);
dp[0] = true;
for (int i = 0; i < n; ++i) {
scanf("%d", &x);
for (int j = 500000; j >= x; --j) dp[j] |= dp[j - x];
}
for (;;) {
int res = -1;
for (int i = ans + d; i > ans; --i)
if (dp[i]) {
res = i;
break;
}
if (res < 0) break;
ans = res;
calc++;
}
printf("%d %d\n", ans, calc);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n, d, c[55];
int can[55 * 10000], sum = 0;
int main() {
cin >> n >> d;
for (int i = 0; i < n; i++) {
cin >> c[i];
sum += c[i];
}
can[0] = 1;
for (int i = 0; i < n; i++)
for (int j = sum - c[i]; j >= 0; j--)
if (can[j]) can[j + c[i]] = 1;
int mx = 0, nmx = 0, ans = 0;
while (1) {
mx = nmx;
for (int i = mx + 1; i <= min(sum, mx + d); i++)
if (can[i]) nmx = i;
if (mx == nmx) break;
mx = nmx;
ans++;
}
cout << mx << " " << ans << endl;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 50;
int main() {
int N, D;
cin >> N >> D;
int a[MAXN];
for (int i = 0; i < N; i++) cin >> a[i];
const int MAXSUM = accumulate(a, a + N, 0);
vector<bool> dp(MAXSUM + 1, false);
dp[0] = true;
for (int i = 0; i < N; i++)
for (int j = MAXSUM; j - a[i] >= 0; j--) dp[j] = dp[j] || dp[j - a[i]];
int maxsum = 0, day = 0;
while (1) {
int t = -1;
for (int i = maxsum + 1; i <= min(MAXSUM, maxsum + D); i++)
if (dp[i] == true) t = i;
if (t == -1) break;
maxsum = t;
day++;
}
cout << maxsum << " " << day << endl;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | import java.util.*;
public class D {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
int n = sc.nextInt();
int d = sc.nextInt();
int[] item = new int[n];
int[] daysToValue = new int[1000000];
int[] topDay = new int[1000000];
boolean[] values = new boolean[1000000];
values[0] = true;
int max = 0;
int days = 0;
for (int i = 0; i < n; i++) {
item[i] = sc.nextInt();
if (item[i] <= d) {
daysToValue[item[i]] = 1;
}
for (int j = values.length -1; j >= 0; j--) {
if (values[j]) {values[j+item[i]] = true;}
}
}
int nextTop = 0;
for (int i = topDay.length-1; i >= 0; i--) {
if(values[i]) {
topDay[i] = nextTop;
nextTop = i;
} else {
topDay[i] = nextTop;
}
}
int next = max;
while (true) {
int nextValue = topDay[next];
if (nextValue == 0) {
if (max == next) break;
else {
max = next;
days++;
break;
}
}
if (max + d >= nextValue) {
next = nextValue;
} else {
if (max == next) {
break;
} else {
max = next;
days++;
}
}
}
if (days == 0) max = 0;
System.out.println("" + max + " " + days);
}
} | JAVA |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n, d;
int a[55];
int f[55][550010];
int dp(int l, int last) {
for (int i = 1; i <= n; i++)
for (int j = last; j <= l; j++)
f[i][j] = max((j >= a[i] ? f[i - 1][j - a[i]] + a[i] : 0), f[i - 1][j]);
return f[n][l];
}
int main() {
scanf("%d%d", &n, &d);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
int t = dp(d, 0), cnt = 1;
if (t == 0) {
printf("0 0\n");
return 0;
}
while (1) {
int q = dp(d + t, t);
if (q <= t)
break;
else
t = q, cnt++;
}
printf("%d %d\n", t, cnt);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed;
cout.precision(12);
solve();
return 0;
}
template <typename T>
void sc(T& x) {
cin >> x;
}
template <typename Head, typename... Tail>
void sc(Head& head, Tail&... tail) {
cin >> head;
sc(tail...);
}
template <typename T>
void pr(const T& x) {
cout << x << '\n';
}
template <typename Head, typename... Tail>
void pr(const Head& head, const Tail&... tail) {
cout << head << ' ';
pr(tail...);
}
const int MAX_SUM = 5e5 + 10;
bool vis[MAX_SUM];
void solve() {
fill(vis, vis + MAX_SUM, false);
int n, d;
sc(n, d);
vector<int> oks;
oks.push_back(0);
for (int i = 0; i < n; i++) {
int x;
sc(x);
for (int j = ((int)oks.size()) - 1; j >= 0; j--) {
if (x + oks[j] < MAX_SUM && !vis[x + oks[j]]) {
oks.push_back(x + oks[j]);
vis[x + oks[j]] = true;
}
}
}
sort(oks.begin(), oks.end());
int ans = 0, cur = 0;
while (true) {
int idx =
upper_bound(oks.begin(), oks.end(), oks[cur] + d) - oks.begin() - 1;
if (idx >= ((int)oks.size()) || idx == cur) {
break;
}
ans++;
cur = idx;
}
pr(oks[cur], ans);
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | import java.util.Scanner;
public class D_213 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int d = in.nextInt();
int c[] = new int[n+1];
int total = 0;
for(int i = 1; i <= n; i++){
c[i] = in.nextInt();
total += c[i];
}
boolean t[][] = new boolean[n+1][total+1];
for(int i = 0; i <= n; i++) t[i][0] = true;
//for(int i = 0; i <= total; i++) t[0][i] = true;
for(int i = 1; i < total+1; i++){
for(int j = 1; j <= n; j++){
t[j][i] = t[j-1][i];
if(i-c[j] >= 0) t[j][i] = t[j][i] || t[j-1][i-c[j]];
}
}
/*for(int i = 1; i < total+1; i++){
for(int j = 1; j <= n; j++){
System.out.print(t[j][i] + " ");
}
System.out.println();
}*/
int x = 0;
int days = 0;
while(true){
int canBuy = Math.min(x + d, total);
//System.out.println(canBuy);
int buys = x;
for(int p = canBuy; p > x; p--){
if(t[n][p]){
buys = p;
break;
}
}
if(buys > x){
x = buys;
days++;
} else {
break;
}
}
System.out.println(x + " " + days);
}
}
| JAVA |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
long long LLMAX = 9223372036854775807LL;
const int MOD = 1000000007;
const int maxn = 4000 + 10;
int n, d, c[55];
vector<int> s;
int main() {
scanf("%d %d", &(n), &(d));
for (int(i) = 0; (i) < (int)(n); ++(i)) scanf("%d", &(c[i]));
bitset<500001> dp;
dp.set(0);
s.push_back(0);
for (int i = 0; i < n; ++i)
for (int j = 500001; j - c[i] >= 0; --j)
if (dp[j - c[i]] && dp[j] == 0) {
dp.set(j);
s.push_back(j);
}
sort(s.begin(), s.end());
int ans = 0, now = 0;
for (int i = 0; i < s.size(); ++ans) {
for (; i < s.size() && now + d >= s[i]; ++i)
;
if (s[i - 1] != now)
now = s[i - 1];
else
break;
}
printf("%d %d\n", (now), (ans));
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const long long maxN = 1e6 + 5;
const long long inf = 1e10;
const long long mod = 1e9 + 7;
long long n, d;
long long f[maxN];
long long x;
long long used[maxN];
vector<long long> a;
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> d;
used[0] = 1;
a.push_back(0);
for (long long i = 1; i <= n; i++) {
cin >> x;
for (long long j = a.size() - 1; j >= 0; j--) {
if (a[j] + x < maxN && used[a[j] + x] == 0) {
used[a[j] + x] = 1;
a.push_back(a[j] + x);
}
}
}
sort(a.begin(), a.end());
long long i = 0;
long long ans = 0;
long long now = 0;
while (i < a.size()) {
while (now + d >= a[i] && i < a.size()) i++;
if (a[i - 1] != now)
now = a[i - 1];
else
break;
ans++;
}
cout << now << " " << ans;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int N = 500010;
const int M = 1e9 + 0.5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int pi = cos(-1.0);
const long long oo = 0x3f3f3f3f3f3f3f3fll;
int n, dp[N], d;
int main() {
scanf("%d%d", &n, &d);
dp[0] = 1;
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
for (int i = 500000; i >= x; i--) dp[i] |= dp[i - x];
}
int ret = 0, cnt = 0;
for (; 1;) {
int t = ret, x = ret;
for (int j = x + 1; j <= x + d && j <= 500000; j++) {
if (dp[j]) t = j;
}
if (ret < t)
ret = t, cnt++;
else
break;
}
cout << ret << " " << cnt << endl;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed;
cout.precision(12);
solve();
return 0;
}
template <typename T>
void sc(T& x) {
cin >> x;
}
template <typename Head, typename... Tail>
void sc(Head& head, Tail&... tail) {
cin >> head;
sc(tail...);
}
template <typename T>
void pr(const T& x) {
cout << x << '\n';
}
template <typename Head, typename... Tail>
void pr(const Head& head, const Tail&... tail) {
cout << head << ' ';
pr(tail...);
}
const int MAX_SUM = 5e5 + 10;
bool vis[MAX_SUM];
void solve() {
fill(vis, vis + MAX_SUM, false);
int n, d;
sc(n, d);
vector<int> oks;
oks.push_back(0);
for (int i = 0; i < n; i++) {
int x;
sc(x);
for (int j = ((int)oks.size()) - 1; j >= 0; j--) {
if (x + oks[j] < MAX_SUM && !vis[x + oks[j]]) {
oks.push_back(x + oks[j]);
vis[x + oks[j]] = true;
}
}
}
sort(oks.begin(), oks.end());
int ans = 0, cur = 0;
while (true) {
int idx =
upper_bound(oks.begin(), oks.end(), oks[cur] + d) - oks.begin() - 1;
if (idx >= ((int)oks.size()) || idx == cur) {
break;
}
ans++;
cur = idx;
}
pr(oks[cur], ans);
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
struct node {
int a;
int b;
int c;
int d;
};
bool cmp(const node &first, const node &sec) {
if (first.a != sec.a) return first.a < sec.a;
return first.b < sec.b;
}
bool operator<(const node &left, const node &right) { return left.b > right.b; }
int n, d, da, i, j, x, cost;
map<int, int> m;
int arr[100];
vector<int> v, v1;
int main() {
scanf("%d", &n);
scanf("%d", &d);
for (i = 1; i <= n; i++) scanf("%d", &arr[i]);
for (i = 1; i <= n; i++) {
if (!v.empty()) {
for (j = 0; j < v.size(); j++) {
x = v[j] + arr[i];
if (m[x] == 0) {
m[x] = 1;
v1.push_back(x);
}
}
}
x = arr[i];
if (m[x] == 0) {
m[x] = 1;
v1.push_back(x);
}
for (j = 0; j < v1.size(); j++) v.push_back(v1[j]);
v1.clear();
}
sort(v.begin(), v.end());
cost = 0;
if (v[0] > d) {
cout << 0 << " " << 0;
return 0;
}
i = 0;
while (i < v.size()) {
if (((cost + d) >= v[i]) && (i != (v.size() - 1)))
i++;
else if ((cost + d) < v[i]) {
da++;
cost = v[i - 1];
if (v[i] > (v[i - 1] + d)) break;
} else if (((cost + d) >= v[i]) && (i == v.size() - 1)) {
da++;
cost = v[i];
i++;
}
}
cout << cost << " " << da;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n, d;
int v[50 + 5];
bool F[5000000 + 5];
int main() {
scanf("%d%d", &n, &d);
for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
F[0] = true;
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += v[i];
for (int j = sum; j >= v[i]; j--) F[j] |= F[j - v[i]];
}
int ans = 0, day = 0;
bool found = false;
while (1) {
for (int i = d; i >= 1; i--) {
if (F[ans + i]) {
ans += i;
day++;
found = true;
break;
}
}
if (!found)
break;
else
found = false;
}
printf("%d %d\n", ans, day);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
bool sum[550000];
int main() {
memset(sum, 0, sizeof sum);
sum[0] = 1;
int n, d, aux;
cin >> n >> d;
int max = 0;
for (int i = 0; i < n; i++) {
cin >> aux;
for (int j = max; j >= 0; j--) {
if (sum[j] == 1) {
sum[j + aux] = 1;
if (j + aux > max) max = j + aux;
}
}
}
aux = 0;
int dia;
bool flag;
for (dia = 0;; dia++) {
flag = 0;
for (int i = aux + d; i > aux; i--) {
if (sum[i]) {
aux = i;
flag = 1;
break;
}
}
if (flag == 0) break;
}
cout << aux << " " << dia << endl;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed;
cout.precision(12);
solve();
return 0;
}
template <typename T>
void sc(T& x) {
cin >> x;
}
template <typename Head, typename... Tail>
void sc(Head& head, Tail&... tail) {
cin >> head;
sc(tail...);
}
template <typename T>
void pr(const T& x) {
cout << x << '\n';
}
template <typename Head, typename... Tail>
void pr(const Head& head, const Tail&... tail) {
cout << head << ' ';
pr(tail...);
}
const int MAX_SUM = 5e5 + 10;
bool vis[MAX_SUM];
void solve() {
fill(vis, vis + MAX_SUM, false);
int n, d;
sc(n, d);
vector<int> oks;
oks.push_back(0);
for (int i = 0; i < n; i++) {
int x;
sc(x);
for (int j = ((int)oks.size()) - 1; j >= 0; j--) {
if (x + oks[j] < MAX_SUM && !vis[x + oks[j]]) {
oks.push_back(x + oks[j]);
vis[x + oks[j]] = true;
}
}
}
sort(oks.begin(), oks.end());
int ans = 0, cur = 0;
while (true) {
int idx =
upper_bound(oks.begin(), oks.end(), oks[cur] + d) - oks.begin() - 1;
if (idx >= ((int)oks.size()) || idx == cur) {
break;
}
ans++;
cur = idx;
}
pr(oks[cur], ans);
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
mt19937_64 random_num(
chrono::high_resolution_clock::now().time_since_epoch().count());
const int MOD = 1000000007;
const int base = 26;
int n, d;
int a[55];
bool f[500001];
int res[500001];
multiset<int> s;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> d;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + 1 + n);
int pos = 1, sum = 0;
while (pos <= n && sum + d >= a[pos]) sum += a[pos++];
n = --pos;
f[0] = 1;
for (int i = 1; i <= n; i++)
for (int j = 500000; j >= a[i]; j--) f[j] |= f[j - a[i]];
for (int i = 1; i <= 500000; i++) res[i] = MOD;
res[0] = 0;
s.insert(0);
int t = 0;
for (int i = 1; i <= 500000; i++) {
while (t + d < i) s.erase(s.find(res[t++]));
if (f[i]) res[i] = min(res[i], (*s.begin() + 1));
s.insert(res[i]);
}
for (int i = 500000; i >= 0; i--)
if (res[i] != MOD) {
cout << i << " " << res[i];
return 0;
}
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:268435456,268435456")
using namespace std;
bool dp[510001];
int main() {
int n, d, t;
scanf("%d %d", &n, &d), dp[0] = 1;
for (int i = 0; i < n; i++) {
scanf("%d", &t);
for (int j = 510001; j - t >= 0; j--) dp[j] |= dp[j - t];
}
int curr = 0, num = 0, sum = 0;
while (true) {
for (int i = d; i >= 1; i--)
if (dp[curr + i]) {
curr += i, num++;
break;
}
if (curr > sum)
sum = curr;
else
break;
}
printf("%d %d\n", sum, num);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int dp[550005];
int main() {
int n, d, c, i, j, x, flog;
scanf("%d %d", &n, &d);
dp[0] = 1;
for (i = 0; i < n; i++) {
scanf("%d", &c);
for (j = 500000; j >= c; j--) {
dp[j] |= dp[j - c];
}
}
int day = 0;
x = 0;
while (1) {
flog = 0;
for (j = x + d; j > x; j--) {
if (dp[j]) {
flog = 1;
day++;
x = j;
break;
}
}
if (!flog) break;
}
printf("%d %d\n", x, day);
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
inline int in() {
int32_t x;
scanf("%d", &x);
return x;
}
inline string get() {
char ch[1000000];
scanf("%s", ch);
return ch;
}
template <class P, class Q>
inline P smin(P &a, Q b) {
if (b < a) a = b;
return a;
}
template <class P, class Q>
inline P smax(P &a, Q b) {
if (a < b) a = b;
return a;
}
const int MAX_LG = 17;
const long long INF = 1e9;
const long long maxn = 1e6 + 10;
bool mark[maxn];
long long a[maxn];
long long maxi, day;
int32_t main() {
long long n = in(), d = in();
for (long long i = 0; i < n; i++) a[i] = in();
sort(a, a + n);
for (long long i = 0; i < n; i++) {
for (long long j = maxn - 1; j >= 0; j--) {
if (mark[j] && j + a[i] < maxn) mark[j + a[i]] = true;
}
mark[a[i]] = true;
}
while (1) {
long long best = -INF;
for (long long i = maxi + d; i > maxi; i--) {
if (mark[i]) {
best = i;
break;
}
}
if (best == -INF) break;
maxi = best, day++;
}
cout << maxi << " " << day << "\n";
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int MAXINT = 1 << 31 - 1;
const int MAXN = 55 * 1e4 + 5;
int dp[MAXN];
int arr[60];
int n, d;
int main() {
while (cin >> n >> d) {
int big = 0;
for (int i = (0); i < (n); i += 1) {
scanf("%d", &arr[i]);
big += arr[i];
}
fill(dp, dp + big + d + 1, MAXINT);
dp[0] = 0;
for (int i = (0); i < (n); i += 1) {
for (int j = (big); j > (0); j -= 1) {
int prev = j - arr[i];
if (prev < 0) {
break;
}
if (dp[prev] == MAXINT) {
continue;
}
dp[j] = min(dp[j], dp[prev] + 1);
}
}
int times = 0;
int cur = 0, next = 0;
while (true) {
next = cur;
for (int i = (1); i < (d + 1); i += 1) {
if (dp[cur + i] == MAXINT) {
continue;
}
next = cur + i;
}
if (next == cur) {
break;
}
cur = next;
times++;
}
printf("%d %d\n", next, times);
}
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n, d;
int c[51], dp[500001];
bool v[500001];
int main() {
cin >> n >> d;
for (int i = 1; i <= n; i++) cin >> c[i];
sort(c + 1, c + 1 + n);
int sum = 0, cnt;
for (int i = 1; i <= n; i++) {
if (sum + d >= c[i]) {
sum += c[i];
} else {
cnt = i - 1;
break;
}
}
v[0] = 1;
for (int i = 1; i <= n; i++)
for (int j = sum; j - c[i] >= 0; j--) {
v[j] |= v[j - c[i]];
}
int t = 0;
for (int i = 0;; t++) {
if (i == sum) break;
for (int j = i + d; j > i; j--) {
if (v[j]) {
i = j;
}
}
}
cout << sum << " " << t;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
long long n, k, s, sum = 0, ans = 0;
int f[1000005];
int a[1000005];
long long p, q, x = 0;
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
f[0] = 1;
for (int i = 1; i <= n; i++) {
sum += a[i];
for (long long j = sum; j >= a[i]; j--) {
if (f[j - a[i]] != 0) {
f[j] = 1;
}
}
}
while (1) {
p = x + k;
while (f[p] == 0 && p > x && p >= 0) {
p--;
}
if (p == x) {
break;
}
x = p;
ans++;
}
cout << x << ' ' << ans << endl;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
const int inf32 = 1e9 + 9;
const long long inf64 = 1e18 + 18;
const int N = 6e5 + 5;
const long long mod = 1e9 + 7;
bitset<N> bits;
void solve() {
int n, d, a, ans = 0;
scanf("%d%d", &n, &d);
bits[0] = true;
while (n--) {
scanf("%d", &a);
bits |= (bits << a);
}
int nxt;
for (int i = 0; i < N; ++ans) {
nxt = i;
for (int j = i + 1; j <= i + d; ++j) {
if (bits[j]) nxt = j;
}
if (nxt == i) break;
i = nxt;
}
printf("%d %d", nxt, ans);
}
int main() {
solve();
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 55 * 10007;
vector<int> v;
bool mark[10000000];
int main() {
int n, d;
cin >> n >> d;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
v.push_back(x);
}
sort(v.begin(), v.end());
mark[0] = 1;
for (int i = 0; i <= n; i++) {
for (int j = maxn; j >= 0; j--)
if (mark[j] == 1) mark[j + v[i]] = 1;
}
int maxi = 0, day = 0;
while (true) {
int p = -1;
for (int i = maxi + d; i > maxi; i--) {
if (mark[i] == 1) {
p = i;
break;
}
}
if (p == -1) break;
maxi = p;
day++;
}
cout << maxi << " " << day << endl;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int max_n = 50;
const int max_d = 1e4 + 100;
const int max_sum = max_n * max_d;
int n, d;
int sum[max_sum];
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> d;
sum[0] = 1;
for (int i = 0; i < n; ++i) {
int c;
cin >> c;
for (int i = max_sum - 1; i >= 0; --i)
if (sum[i] && i + c < max_sum) sum[i + c] = 1;
}
int ans_sum = 0;
int ans_step = 0;
while (true) {
int new_ans_sum = ans_sum;
for (int i = ans_sum + 1; i <= min(ans_sum + d, max_sum - 1); ++i)
if (sum[i]) new_ans_sum = i;
if (new_ans_sum == ans_sum) break;
ans_sum = new_ans_sum;
++ans_step;
}
cout << ans_sum << " " << ans_step << endl;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
int dp[MAXN];
int main() {
int n, d, x;
int cnt, sum, ans;
cnt = sum = ans = 0;
scanf("%d %d", &n, &d);
dp[0] = 1;
for (int i = 0; i < n; i++) {
scanf("%d", &x);
sum += x;
for (int j = sum; j >= x; j--) {
if (dp[j - x]) dp[j] = 1;
}
}
while (1) {
int t = ans + d;
while (!dp[t] && t > ans) t--;
if (t == ans) break;
ans = t;
cnt++;
}
printf("%d %d\n", ans, cnt);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxN = 51 * 52;
const int INF = maxN * maxN;
const int maxE = 51 * 10000;
vector<int> G[maxN];
vector<int> G2;
queue<pair<int, int> > Q;
bool mark[maxN];
int sum[maxN], a[maxN], dist[maxN], dp[maxN][maxN], dp2[51][maxE], n, d, tul,
ans1, ans2;
long long getSum(int substr) {
if (!substr) return 0;
return sum[substr % 51] - sum[substr / 51 - 1];
}
bool could(int st, int en, int st2, int en2, int d) {
if (st == st2 && en == en2) return 0;
if (st2 <= st && en <= en2)
return sum[st - 1] - sum[st2 - 1] + sum[en2] - sum[en] <= d;
if (st > en2 || st2 > en)
return sum[en2] - sum[st2 - 1] - sum[en] + sum[st - 1] <= d;
if (st <= st2 && en2 <= en) return 0;
if (st <= en2 && st >= st2) return 0;
return 0;
}
void makeGraph() {
for (int st = 1; st <= n; st++)
for (int en = st; en <= n; en++)
for (int st2 = 1; st2 <= n; st2++)
for (int en2 = st2; en2 <= n; en2++)
if (could(st, en, st2, en2, d))
G[st * 51 + en].push_back(st2 * 51 + en2);
for (int st = 1; st <= n; st++)
for (int en = st; en <= n; en++)
if (d >= sum[en] - sum[st - 1]) G[0].push_back(st * 51 + en);
}
int bfs() {
Q.push(make_pair(0, 0));
while (!Q.empty()) {
int now = Q.front().first, dis = Q.front().second;
Q.pop();
for (int i = 0; i < G[now].size(); i++)
if (!dist[G[now][i]]) {
Q.push(make_pair(G[now][i], dis + 1));
dist[G[now][i]] = dis + 1;
if (getSum(G[now][i]) == ans1) return dis + 1;
}
}
return -1;
}
void makeGraph2() {
dp2[0][0] = 1;
for (int i = 1; i <= n; i++)
for (int j = 0; j < maxE; j++) {
if (dp2[i - 1][j - a[i]]) dp2[i][j] = 1;
if (dp2[i - 1][j]) dp2[i][j] = 1;
}
for (int i = 0; i < maxE; i++)
if (dp2[n][i]) G2.push_back(i);
}
int greedy() {
long long now = 0, day = 0, a2 = 0;
for (; now < ans1 && day < G2.size(); day++) {
while (day < G2.size() && now + d >= G2[day]) day++;
day--;
now = G2[day];
a2++;
}
return a2;
}
int main() {
cin >> n >> d;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i];
for (int i = tul = 1; i <= n; i++) {
if (ans1 + d >= a[i])
ans1 += a[i];
else
break;
tul++;
}
makeGraph2();
ans2 = greedy();
cout << ans1 << " " << ans2 << endl;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* @author Don Li
*/
public class FreeMarket {
int V = (int) 5e5 + 10;
void solve() {
int n = in.nextInt(), d = in.nextInt();
int[] c = new int[n];
for (int i = 0; i < n; i++) c[i] = in.nextInt();
int[] dp = new int[V];
dp[0] = 1;
for (int i = 0; i < n; i++) {
for (int j = V - 1; j >= c[i]; j--) {
dp[j] |= dp[j - c[i]];
}
}
int days = 0, cur = 0;
while (true) {
int nxt = Math.min(V - 1, cur + d);
while (nxt > cur) {
if (dp[nxt] > 0) break;
nxt--;
}
if (nxt > cur) {
days++;
cur = nxt;
} else {
break;
}
}
out.printf("%d %d%n", cur, days);
}
public static void main(String[] args) {
in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
new FreeMarket().solve();
out.close();
}
static FastScanner in;
static PrintWriter out;
static class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
| JAVA |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int DEBUG = 0;
int isatt[2][600000];
int ispurable[600000];
int steps[600000];
int main(int argc, char **argv) {
DEBUG = (argc >= 2) ? atoi(argv[1]) : 0;
int n, d;
cin >> n >> d;
int c[n];
for (int i = 0; i < n; i++) cin >> c[i];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 600000; j++) {
isatt[i][j] = 0;
}
}
isatt[1][0] = 1;
isatt[0][0] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 600000; j++) isatt[i % 2][j] = 0;
for (int j = 0; j < 600000; j++) {
if (isatt[(i + 1) % 2][j] == 1) {
isatt[i % 2][j] = 1;
isatt[i % 2][j + c[i]] = 1;
}
}
}
for (int i = 0; i < 600000; i++) {
steps[i] = 0;
ispurable[i] = 0;
}
int troll = (n + 1) % 2;
ispurable[0] = 1;
int tmp = 0;
int tt = 0;
while (tmp < 600000) {
int cmax = -1;
tt++;
for (int i = 1; i <= d; i++) {
if (isatt[troll][tmp + i] == 1) {
ispurable[tmp + i] = 1;
cmax = max(cmax, tmp + i);
steps[tmp + i] = tt;
}
}
if (cmax == -1) {
break;
}
tmp = cmax;
}
for (int i = 600000 - 1; i >= 0; i--) {
if (ispurable[i] == 1) {
cout << i << ' ' << steps[i] << endl;
return 0;
}
}
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 500010;
bool f[MAXN];
int a[MAXN];
vector<int> sta;
int main() {
int n, d;
while (scanf("%d%d", &n, &d) != EOF) {
memset(f, false, sizeof(f));
f[0] = true;
int tot = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
tot += a[i];
}
for (int i = 1; i <= n; i++)
for (int j = tot; j >= a[i]; j--) f[j] |= f[j - a[i]];
sta.clear();
for (int i = 0; i <= tot; i++)
if (f[i]) {
sta.push_back(i);
}
int day = 0, p = 0;
int size = sta.size();
while (true) {
if (p + 1 >= size) break;
if (sta[p + 1] > sta[p] + d) break;
int nxt = p + 1;
while (nxt < size && sta[nxt] <= sta[p] + d) nxt++;
p = nxt - 1;
day++;
}
printf("%d %d\n", sta[p], day);
}
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int a[55];
int dp[55][505005];
int main() {
cin.sync_with_stdio(0);
int n, d;
cin >> n >> d;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
dp[0][0] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 505005; j++) {
if (dp[i][j]) {
dp[i + 1][j] = 1;
dp[i + 1][j + a[i]] = 1;
}
}
}
int ans = 0;
int days = 0;
while (true) {
bool flag = false;
int temp;
for (int i = min(505005 - 1, ans + d); i > ans; i--) {
if (dp[n][i]) {
temp = i;
flag = true;
break;
}
}
if (flag == false) break;
ans = temp;
days++;
}
cout << ans << " " << days << endl;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int dp[500001];
int main() {
int n, d, x = 0, i, j;
int a[50];
scanf("%d %d", &n, &d);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
dp[0] = 1;
for (i = 0; i < n; i++) {
for (j = 500000; j >= 0; j--) {
if (dp[j] == 1) dp[j + a[i]] = 1;
}
}
for (i = 0;; i++) {
int y = 0;
for (j = x + 1; j <= x + d && j <= 500000; j++) {
if (dp[j] == 1) y = j;
}
if (y > x) {
x = y;
} else {
printf("%d %d\n", x, i);
return 0;
}
}
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
using namespace std;
int dp[5000005], d, n, cnt, ans;
int main() {
cin >> n >> d;
dp[0] = 1;
int sum = 0, x;
for (int i = 0; i < n; i++) {
cin >> x;
sum += x;
for (int j = sum; j - x >= 0; j--) {
if (dp[j - x]) {
dp[j] = 1;
}
}
}
while (true) {
int j = d + ans;
while (!dp[j] && j > ans) {
j--;
}
if (j == ans) {
break;
}
ans = j;
cnt++;
}
cout << ans << " " << cnt << endl;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | import java.util.*;
import java.io.*;
public class Main {
BufferedReader in;
StringTokenizer str = null;
PrintWriter out;
private String next() throws Exception{
if (str == null || !str.hasMoreElements())
str = new StringTokenizer(in.readLine());
return str.nextToken();
}
private int nextInt() throws Exception{
return Integer.parseInt(next());
}
private long nextLong() throws Exception{
return Long.parseLong(next());
}
private double nextDouble() throws Exception{
return Double.parseDouble(next());
}
public void run() throws Exception{
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
int n = nextInt();
int d = nextInt();
int cost[] = new int[n];
int sum = 0;
for(int i=0;i<n;i++) {
cost[i] = nextInt();
sum+=cost[i];
}
boolean dp[][] = new boolean[n+1][sum+1];
for(int i=0;i<=n;i++){
Arrays.fill(dp[i], false);
}
dp[0][0] = true;
for(int i=1;i<=n;i++){
for(int j=0;j<=sum;j++){
dp[i][j] = dp[i-1][j];
if (j - cost[i-1] >= 0){
dp[i][j]|=dp[i-1][j - cost[i-1]];
}
}
}
int s = 0, days = 0;
while(true){
int next = -1;
for(int i=Math.min(s + d, sum);i>s;i--){
if (dp[n][i]){
next = i;
break;
}
}
if (next == -1) break;
days++;
s = next;
}
out.println(s + " " + days);
out.close();
}
public static void main(String[] args) throws Exception{
new Main().run();
}
}
| JAVA |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int sum[500001];
int main() {
int n, d;
scanf("%d%d", &n, &d);
sum[0] = 1;
for (int i = 0, c; i < n; ++i) {
scanf("%d", &c);
for (int s = 500000; s >= 0; --s)
if (sum[s]) sum[s + c] = 1;
}
vector<int> var;
for (int i = 1; i < 500001; ++i)
if (sum[i]) var.push_back(i);
int s = 0, c = 0;
for (int i = 0; i < var.size(); ++i) {
while (s + d >= var[i] && i < var.size()) i++;
if (s + d >= var[--i] && s != var[i]) {
s = var[i];
c++;
} else
break;
}
printf("%d %d\n", s, c);
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.