int n, m, k, p; int h[kMaxN], a[kMaxN], now[kMaxN];
boolcheck(int x){ std::priority_queue<std::pair<int, int>> q; for (int i = 1; i <= n; ++i) { now[i] = x; if (x - a[i] * m < h[i]) q.emplace(-(now[i] / a[i]), i); } for (int c = 1; c <= m; ++c) { for (int j = 1; j <= k && !q.empty(); ++j) { auto [d, i] = q.top(); q.pop(); if (now[i] - c * a[i] < 0) return0; now[i] += p; if (now[i] - a[i] * m < h[i]) q.emplace(-(now[i] / a[i]), i); } } return q.empty(); }
voiddickdreamer(){ std::cin >> n >> m >> k >> p; for (int i = 1; i <= n; ++i) std::cin >> h[i] >> a[i]; int L = -1, R = 1e18, res = 1e18; while (L + 1 < R) { int mid = (L + R) >> 1; if (check(mid)) R = res = mid; else L = mid; } std::cout << res << '\n'; }