Problem p02883 (C++)

Buggy submission (status: Time Limit Exceeded):
// https://atcoder.jp/contests/past-sample/tasks/abc129_d

// config

const int __SETW__ = 6;

// IO
#include <iomanip>  // setw
#include <iostream> // cin, cout
// MATH & ALGROTHM
#include <algorithm>
#include <cmath>
#include <complex>
#include <numeric>
// CONTAINER
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// FUNCTION
#include <functional>
// META
#include <type_traits>

///////
using std::cin;
using std::cout;
using std::endl;

using INT = unsigned long long int;
const INT INF = INT(1) << 60;

#define FOR(i, N) for (INT i = 0; i < N; i++)
#define FORR(i, N) for (INT i = (N - 1); i >= 0; i--)
#define RANGE(v) begin(v), end(v)

using VEC = std::vector<INT>;
using VECVEC = std::vector<VEC>;
using VECVECVEC = std::vector<VEC>;

template <class T> T safe_get(const std::vector<T> &v, INT idx, T def = -1) {
  if (idx < 0 || v.size() <= idx) {
    return def;
  } else {
    return v[idx];
  }
}
template <class T>
T safe_get(const std::vector<std::vector<T>> &vv, INT idx1, INT idx2,
           T def = -1) {
  if (idx1 < 0 || vv.size() <= idx1) {
    return def;
  } else {
    return safe_get(vv[idx1], idx2, def);
  }
}

template <class T, class F> inline bool ANY(T sequence, F func) {
  for (auto &&t : sequence) {
    if (func(t)) {
      return true;
    }
  }
  return false;
}
template <class T, class F> inline bool ALL(T sequence, F func) {
  for (auto &&t : sequence) {
    if (!func(t)) {
      return false;
    }
  }
  return true;
}

template <class T>
std::ostream &operator<<(std::ostream &lhs, const std::priority_queue<T> &rhs) {
  std::queue<T> copy = rhs;
  while (!copy.empty()) {
    lhs << std::setw(__SETW__) << copy.front() << " ";
    copy.pop();
  }
  return lhs;
}
template <class T>
std::ostream &operator<<(std::ostream &lhs, const std::queue<T> &rhs) {
  std::queue<T> copy = rhs;
  while (!copy.empty()) {
    lhs << std::setw(__SETW__) << copy.front() << " ";
    copy.pop();
  }
  return lhs;
}
template <class T>
std::ostream &operator<<(std::ostream &lhs, const std::stack<T> &rhs) {
  std::stack<T> copy = rhs;
  while (!copy.empty()) {
    lhs << std::setw(__SETW__) << copy.top() << " ";
    copy.pop();
  }
  return lhs;
}

template <class T>
std::ostream &operator<<(std::ostream &lhs,
                         const std::vector<std::vector<T>> &rhs) {
  for (auto &&v : rhs) {
    lhs << v << endl;
  }
  return lhs;
}
template <class T>
std::ostream &operator<<(std::ostream &lhs, const std::vector<T> &rhs) {
  for (auto &&t : rhs) {
    lhs << std::setw(__SETW__) << t << " ";
  }
  return lhs;
}
template <class T>
std::ostream &operator<<(std::ostream &lhs, const std::list<T> &rhs) {
  for (auto &&t : rhs) {
    lhs << std::setw(__SETW__) << t << " ";
  }
  return lhs;
}

template <class T1, class T2>
std::ostream &operator<<(std::ostream &lhs, const std::map<T1, T2> &rhs) {
  for (auto &&m : rhs) {
    lhs << std::setw(__SETW__) << m.first << ":" << std::setw(__SETW__)
        << m.second << endl;
  }
  return lhs;
}
template <class T1, class T2>
std::ostream &operator<<(std::ostream &lhs, const std::multimap<T1, T2> &rhs) {
  for (auto &&m : rhs) {
    lhs << std::setw(__SETW__) << m.first << ":" << std::setw(__SETW__)
        << m.second << endl;
  }
  return lhs;
}
template <class T>
std::ostream &operator<<(std::ostream &lhs, const std::set<T> &rhs) {
  for (auto &&t : rhs) {
    lhs << std::setw(__SETW__) << t << " ";
  }
  return lhs;
}
template <class T>
std::ostream &operator<<(std::ostream &lhs, const std::multiset<T> &rhs) {
  for (auto &&t : rhs) {
    lhs << std::setw(__SETW__) << t << " ";
  }
  return lhs;
}

template <class T>
std::ostream &operator<<(std::ostream &lhs, const std::complex<T> &rhs) {
  std::streamsize ss = lhs.precision();
  lhs << "(" << std::setw(5) << std::setprecision(4) << rhs.real() << ","
      << std::setw(5) << std::setprecision(4) << rhs.imag() << ") ";
  lhs.precision(ss);
  return lhs;
}
template <class T1, class T2>
std::ostream &operator<<(std::ostream &lhs, const std::pair<T1, T2> &rhs) {
  lhs << "(" << rhs.first << "," << rhs.second << ")";
  return lhs;
}

template <class T> T chmax(T &lhs, const T &rhs) {
  lhs = std::max(lhs, rhs);
  return lhs;
}
template <class T> T chmin(T &lhs, const T &rhs) {
  lhs = std::min(lhs, rhs);
  return lhs;
}

using namespace std;

int main() {
  INT N, K;
  cin >> N >> K;

  VEC A(N);
  VEC F(N);

  FOR(i, N) cin >> A[i];
  FOR(i, N) cin >> F[i];

  std::sort(RANGE(A));
  std::sort(RANGE(F), [](INT x, INT y) { return x > y; });

  std::priority_queue<pair<INT, INT>, vector<pair<INT, INT>>,
                      std::less<pair<INT, INT>>>
      que;
  FOR(i, N) { que.emplace(A[i] * F[i], i); }

  FOR(i, K) {
    INT cost, idx;
    tie(cost, idx) = que.top();
    que.pop();

    if (cost == 0)
      break;

    A[idx]--;
    que.emplace(A[idx] * F[idx], idx);
  }

  INT ans = 0;
  FOR(i, N) { chmax(ans, A[i] * F[i]); }
  cout << ans << endl;

  return 0;
}


Fixed submission:
// https://atcoder.jp/contests/past-sample/tasks/abc129_d

// config

const int __SETW__ = 6;

// IO
#include <iomanip>  // setw
#include <iostream> // cin, cout
// MATH & ALGROTHM
#include <algorithm>
#include <cmath>
#include <complex>
#include <numeric>
// CONTAINER
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// FUNCTION
#include <functional>
// META
#include <type_traits>

///////
using std::cin;
using std::cout;
using std::endl;

using INT = unsigned long long int;
const INT INF = INT(1) << 60;

#define FOR(i, N) for (INT i = 0; i < N; i++)
#define FORR(i, N) for (INT i = (N - 1); i >= 0; i--)
#define RANGE(v) begin(v), end(v)

using VEC = std::vector<INT>;
using VECVEC = std::vector<VEC>;
using VECVECVEC = std::vector<VEC>;

template <class T> T safe_get(const std::vector<T> &v, INT idx, T def = -1) {
  if (idx < 0 || v.size() <= idx) {
    return def;
  } else {
    return v[idx];
  }
}
template <class T>
T safe_get(const std::vector<std::vector<T>> &vv, INT idx1, INT idx2,
           T def = -1) {
  if (idx1 < 0 || vv.size() <= idx1) {
    return def;
  } else {
    return safe_get(vv[idx1], idx2, def);
  }
}

template <class T, class F> inline bool ANY(T sequence, F func) {
  for (auto &&t : sequence) {
    if (func(t)) {
      return true;
    }
  }
  return false;
}
template <class T, class F> inline bool ALL(T sequence, F func) {
  for (auto &&t : sequence) {
    if (!func(t)) {
      return false;
    }
  }
  return true;
}

template <class T>
std::ostream &operator<<(std::ostream &lhs, const std::priority_queue<T> &rhs) {
  std::queue<T> copy = rhs;
  while (!copy.empty()) {
    lhs << std::setw(__SETW__) << copy.front() << " ";
    copy.pop();
  }
  return lhs;
}
template <class T>
std::ostream &operator<<(std::ostream &lhs, const std::queue<T> &rhs) {
  std::queue<T> copy = rhs;
  while (!copy.empty()) {
    lhs << std::setw(__SETW__) << copy.front() << " ";
    copy.pop();
  }
  return lhs;
}
template <class T>
std::ostream &operator<<(std::ostream &lhs, const std::stack<T> &rhs) {
  std::stack<T> copy = rhs;
  while (!copy.empty()) {
    lhs << std::setw(__SETW__) << copy.top() << " ";
    copy.pop();
  }
  return lhs;
}

template <class T>
std::ostream &operator<<(std::ostream &lhs,
                         const std::vector<std::vector<T>> &rhs) {
  for (auto &&v : rhs) {
    lhs << v << endl;
  }
  return lhs;
}
template <class T>
std::ostream &operator<<(std::ostream &lhs, const std::vector<T> &rhs) {
  for (auto &&t : rhs) {
    lhs << std::setw(__SETW__) << t << " ";
  }
  return lhs;
}
template <class T>
std::ostream &operator<<(std::ostream &lhs, const std::list<T> &rhs) {
  for (auto &&t : rhs) {
    lhs << std::setw(__SETW__) << t << " ";
  }
  return lhs;
}

template <class T1, class T2>
std::ostream &operator<<(std::ostream &lhs, const std::map<T1, T2> &rhs) {
  for (auto &&m : rhs) {
    lhs << std::setw(__SETW__) << m.first << ":" << std::setw(__SETW__)
        << m.second << endl;
  }
  return lhs;
}
template <class T1, class T2>
std::ostream &operator<<(std::ostream &lhs, const std::multimap<T1, T2> &rhs) {
  for (auto &&m : rhs) {
    lhs << std::setw(__SETW__) << m.first << ":" << std::setw(__SETW__)
        << m.second << endl;
  }
  return lhs;
}
template <class T>
std::ostream &operator<<(std::ostream &lhs, const std::set<T> &rhs) {
  for (auto &&t : rhs) {
    lhs << std::setw(__SETW__) << t << " ";
  }
  return lhs;
}
template <class T>
std::ostream &operator<<(std::ostream &lhs, const std::multiset<T> &rhs) {
  for (auto &&t : rhs) {
    lhs << std::setw(__SETW__) << t << " ";
  }
  return lhs;
}

template <class T>
std::ostream &operator<<(std::ostream &lhs, const std::complex<T> &rhs) {
  std::streamsize ss = lhs.precision();
  lhs << "(" << std::setw(5) << std::setprecision(4) << rhs.real() << ","
      << std::setw(5) << std::setprecision(4) << rhs.imag() << ") ";
  lhs.precision(ss);
  return lhs;
}
template <class T1, class T2>
std::ostream &operator<<(std::ostream &lhs, const std::pair<T1, T2> &rhs) {
  lhs << "(" << rhs.first << "," << rhs.second << ")";
  return lhs;
}

template <class T> T chmax(T &lhs, const T &rhs) {
  lhs = std::max(lhs, rhs);
  return lhs;
}
template <class T> T chmin(T &lhs, const T &rhs) {
  lhs = std::min(lhs, rhs);
  return lhs;
}

using namespace std;

int main() {
  INT N, K;
  cin >> N >> K;

  VEC A(N);
  VEC F(N);

  FOR(i, N) cin >> A[i];
  FOR(i, N) cin >> F[i];

  std::sort(RANGE(A));
  std::sort(RANGE(F), [](INT x, INT y) { return x > y; });

  INT mn = 0, mx = INF;
  do {
    INT mid = (mx + mn) / 2;
    INT s = 0;
    FOR(i, N) {
      if (A[i] * F[i] > mid) {
        s += (A[i] - mid / F[i]);
      }
    }
    if (K >= s) {
      mx = mid;
    } else {
      mn = mid;
    }

  } while (mn != mx && mn + 1 != mx);

  INT s = 0;
  FOR(i, N) {
    if (A[i] * F[i] > mn) {
      s += (A[i] - mn / F[i]);
    }
  }
  if (s <= K) {
    cout << mn << endl;
  } else {
    cout << mx << endl;
  }

  return 0;
}