Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <cstdio>
- #include <memory.h>
- using namespace std;
- #define fill(a, x) memset (a, x, sizeof(a))
- int n, s, a[111111], l, r, ans, rr[111111], cc;
- bool w[111111], pp;
- int main() {
- scanf("%d %d", &n, &s);
- for (int i = 0; i < n; ++i)
- scanf("%d", a + i);
- sort(a, a + n);
- reverse(a, a + n);
- l = 0;
- r = n - 1;
- ans = 0;
- cc = 0;
- fill(w, 0);
- while (a[l] == s) {
- w[l] = true;
- rr[cc++] = a[l++];
- ++ans;
- }
- while (r > l) {
- while (r > l && a[l] + a[r] <= s) --r;
- if (r == l) break;
- rr[cc++] = a[r];
- w[r] = true;
- rr[cc++] = a[l];
- w[l] = true;
- --r;
- ++l;
- ans += 2;
- }
- pp = false;
- for (int i = 0; i < n; ++i)
- if (!w[i]) {
- rr[cc++] = a[i];
- if (pp) {
- ++ans;
- pp = false;
- } else pp = true;
- }
- ans = 0;
- pp = false;
- for (int i = 0; i < n; ++i) {
- if (pp)
- if (rr[i - 1] + rr[i] > s) ++ans; else {
- ++ans;
- pp = false;
- }
- else pp = true;
- }
- ans += pp;
- printf("%d\n", ans);
- for (int i = 0; i < n; ++i)
- printf("%d ", rr[i]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement