제출 #1055336

#제출 시각아이디문제언어결과실행 시간메모리
1055336juicy코알라 (JOI13_koala)C++17
80 / 100
47 ms4124 KiB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 1e5 + 5;
const long long inf = 1e18;

int k, m, d, a, n;
int t[N], b[N];
long long ft[2][N], dp[N];

void upd(long long *s, int i, long long x) {
  for (; i < N; i += i & -i) {
    s[i] = max(s[i], x);
  }
}

long long qry(long long *s, int i) {
  long long res = -inf;
  for (; i; i -= i & -i) {
    res = max(res, s[i]);
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);

  cin >> k >> m >> d >> a >> n;
  t[0] = k, t[n + 1] = m;
  vector<int> comp = {k % d, m % d};
  for (int i = 1; i <= n; ++i) {
    cin >> t[i] >> b[i];
    comp.push_back(t[i] % d);
  }
  sort(comp.begin(), comp.end());
  comp.erase(unique(comp.begin(), comp.end()));
  for (auto it : {0, 1}) {
    fill(ft[it] + 1, ft[it] + N, -inf);
  }
  for (int i = 0; i <= n + 1; ++i) {
    int p = lower_bound(comp.begin(), comp.end(), t[i] % d) - comp.begin() + 1;
    if (i) {
      dp[i] = max(qry(ft[0], p), qry(ft[1], N - p)) - (long long) (t[i] / d) * a + b[i];
    }
    upd(ft[0], p + 1, dp[i] + (long long) (t[i] / d - 1) * a);
    upd(ft[1], N - p, dp[i] + (long long) (t[i] / d) * a);
  }
  cout << dp[n + 1];
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...