/* Problem URL: https://codeforces.com/gym/103934/problem/B */ #include #include #include using namespace std; using namespace __gnu_pbds; template > using ordered_set = tree; #define V vector #define rmin(a, b) a = min(a, b) #define rmax(a, b) a = max(a, b) #define rep(i, lim) for (int i = 0; i < (lim); i++) #define nrep(i, s, lim) for (int i = s; i < (lim); i++) #define repv(i, v) for (auto &i : (v)) #define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } #define sortv(v) sort(v.begin(), v.end()) #define all(v) (v).begin(), (v).end() using vi = vector; using vvi = vector; using vvvi = vector; using vvvvi = vector; using ll = long long; using vl = vector; using vvl = vector; using vvvl = vector; using vvvvl = vector; template auto operator<<(ostream &os, const vector &vec)->ostream& { os << vec[0]; for (size_t i = 1; i < vec.size(); i++) { os << ' ' << vec[i]; } os << '\n'; return os; } template auto operator>>(istream &is, vector &vec)->istream& { for (auto &i : vec) { is >> i; } return is; } template auto operator<<(ostream &os, const vector> &vec)->ostream& { for (auto &i : vec) { os << i[0]; for (size_t j = 1; j < i.size(); j++) { os << ' ' << i[j]; } os << '\n'; } return os; } template auto operator>>(istream &is, vector> &vec)->istream& { for (auto &i : vec) { for (auto &j : i) { is >> j; } } return is; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll c, x, t, n; cin >> c >> x >> t >> n; ll t1, t2, t3; cin >> t1 >> t2 >> t3; if (c == 1) { cout << max({t - t1, t - t2, t - t3}) << '\n'; return 0; } vl count(3); vl prev(3); vl prevadd(3); vl ti = {t1, t2, t3}; ll ans = 0; V> fds(n); repv(i, fds) { cin >> i.first >> i.second; } sortv(fds); auto binsearch = [&](ll low, ll high, ll lim) { ll ans = 0; while (low <= high) { ll mid = (high + low) >> 1; if (x * mid <= lim) { ans = mid; low = mid + 1; continue; } high = mid - 1; } return ans; }; auto updateprev = [&](int i, ll time) { if (time - prev[i] <= x) { return; } if (prev[i] + x + ti[i] > t) { count[i] = 0; ll diff = time - prev[i]; ll binans = binsearch(0, diff / x, t - ti[i] - prev[i]); if (prev[i] + x * binans + ti[i] < t) { rmax(ans, prev[i] + x * binans); } prev[i] += diff / x * x; return; } ll diff = time - prev[i]; ll binans = binsearch(0, diff / x, t - ti[i] - prev[i]); if (prev[i] + x * binans + ti[i] < t) { rmax(ans, prev[i] + x * binans); } prev[i] += diff / x * x; count[i] = 0; return; }; auto updatetime = [&](int i, ll time) { if (count[i] < c - 1) { return; } if (count[i] == c) { if (prevadd[i] + ti[i] <= min(t, time)) { rmax(ans, min(t, time) - ti[i]); } if (time + ti[i] <= t) { rmax(ans, time); } count[i] = 0; prev[i] = time; prevadd[i] = time; return; } if (time + ti[i] <= t) { rmax(ans, time); } prevadd[i] = time; }; repv(i, fds) { ll time = i.first; updateprev(0, time); updateprev(1, time); updateprev(2, time); count[i.second - 1]++; updatetime(0, time); updatetime(1, time); updatetime(2, time); } cout << ans << '\n'; }