elev2.cpp 788 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include <cstdio>
#include <algorithm>

using namespace std;

#if 0
#define EXPLAIN printf
#else
#define EXPLAIN(...) do {} while(0)
#endif

#define MAX 1000000
int W[MAX];

int main ()
{
#ifdef CONTEST
  freopen("elev2.in", "rt", stdin);
  freopen("elev2.out", "wt", stdout);
#endif
  // read the input
  int N, B;
  scanf("%d %d", &N, &B);
  for (int i=0; i<N; i++)
    scanf("%d", &(W[i]));
  // sort weights in ascending order
  sort(W, W+N);
  // calculate
  int l = 0, r = N-1;
  int count = 0;
  while (l <= r) {
    // the light ones go only if they fit with the heavy ones
    if (l < r && W[l] + W[r] <= B) {
      EXPLAIN("%d ", W[l]);
      l++;
    }
    // the heavy ones go anyway
    EXPLAIN("%d\n", W[r]);
    r--;
    count++;
  }
  printf("%d\n", count);
  return 0;
}