fork download
  1. // ~~ icebear ~~
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7. typedef pair<int, ii> iii;
  8.  
  9. template<class T>
  10. bool minimize(T &a, const T &b) {
  11. if (a > b) return a = b, true;
  12. return false;
  13. }
  14.  
  15. template<class T>
  16. bool maximize(T &a, const T &b) {
  17. if (a < b) return a = b, true;
  18. return false;
  19. }
  20.  
  21. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  22. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  23. #define REP(i, n) for(int i=0; i<(n); ++i)
  24. #define RED(i, n) for(int i=(n)-1; i>=0; --i)
  25. #define MASK(i) (1LL << (i))
  26. #define BIT(S, i) (((S) >> (i)) & 1)
  27. #define mp make_pair
  28. #define pb push_back
  29. #define fi first
  30. #define se second
  31. #define all(x) x.begin(), x.end()
  32. #define task "exchange"
  33.  
  34. const int MOD = 1e9 + 7;
  35. const int inf = 1e9 + 27092008;
  36. const ll INF = 1e18 + 27092008;
  37. const int N = 6e4 + 5;
  38. const int M = 400;
  39. int n, p, c[N], num;
  40. int vals[2 * N], cnt[2 * N], dp[M][2 * N], trace[M][2 * N];
  41. deque<int> dq[2 * N];
  42. bool haveZero = false;
  43.  
  44. void init(void) {
  45. cin >> n >> p;
  46. FOR(i, 1, n) cin >> c[i];
  47. sort(c + 1, c + n + 1);
  48. FOR(i, 1, n) {
  49. if (!c[i]) {
  50. haveZero = true;
  51. continue;
  52. }
  53. if (c[i] != vals[num]) vals[++num] = c[i];
  54. cnt[num]++;
  55. }
  56. }
  57.  
  58. void process(void) {
  59. memset(dp, 0x3f, sizeof dp);
  60. dp[0][0] = 0;
  61. if (2 * p < N) {
  62. FOR(i, 1, num) {
  63. FOR(j, 0, 2 * p) {
  64. if (j >= cnt[i] * vals[i] && minimize(dp[i][j], dp[i-1][j - cnt[i] * vals[i]] + 1))
  65. trace[i][j] = cnt[i];
  66.  
  67. int r = j % vals[i];
  68. if (dp[i - 1][j] >= 0) {
  69. while(!dq[r].empty() && dp[i-1][dq[r].back()] > dp[i][j]) dq[r].pop_back();
  70. dq[r].pb(j);
  71. }
  72.  
  73. while(!dq[r].empty() && (j - dq[r].front()) >= cnt[i] * vals[i]) dq[r].pop_front();
  74. if (!dq[r].empty()) {
  75. int best = dq[r].front();
  76. if (minimize(dp[i][j], dp[i-1][best]))
  77. trace[i][j] = (j - best) / vals[i];
  78. }
  79. }
  80.  
  81. REP(j, vals[i]) dq[j].clear();
  82. }
  83. } else {
  84. FOR(i, 1, num) FOR(j, 0, 2 * p) FOR(k, 0, cnt[i]) {
  85. if (k * vals[i] > j) break;
  86. if (minimize(dp[i][j], dp[i - 1][j - k * vals[i]] + (k == cnt[i])))
  87. trace[i][j] = k;
  88. }
  89. }
  90.  
  91. int res = inf, jres = 0;
  92. FOR(j, p, 2 * p) if (minimize(res, dp[num][j]))
  93. jres = j;
  94. cout << num + haveZero - res << ' ' << jres << '\n';
  95. vector<int> values;
  96. while(jres > 0 && num > 0) {
  97. int k = trace[num][jres];
  98. FOR(i, 1, k) values.pb(vals[num]);
  99. jres -= k * vals[num];
  100. num--;
  101. }
  102.  
  103. sort(all(values));
  104. for(int v : values) cout << v << ' ';
  105. }
  106.  
  107. int main() {
  108. ios_base::sync_with_stdio(0);
  109. cin.tie(0); cout.tie(0);
  110. if (fopen(task".inp", "r")) {
  111. freopen(task".inp", "r", stdin);
  112. freopen(task".out", "w", stdout);
  113. }
  114. int tc = 1;
  115. // cin >> tc;
  116. while(tc--) {
  117. init();
  118. process();
  119. }
  120. return 0;
  121. }
  122.  
Success #stdin #stdout 0.07s 276060KB
stdin
Standard input is empty
stdout
0 0