Problem p02118 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
//<< setprecision(5)
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define dme                                                                    \\
  cout << -1 << endl;                                                          \\
  return 0
const int mod = 1000000007;
const int big = 1e9 + 10;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-7;
template <class T, class U> void mineq(T &a, U b) {
  if (a > b) {
    a = b;
  }
}
template <class T, class U> void maxeq(T &a, U b) {
  if (a < b) {
    a = b;
  }
}
llint gcd(llint a, llint b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
llint lcm(llint a, llint b) { return a / gcd(a, b) * b; }
int main(void) {
  llint n, L, K, i;
  cin >> n >> L >> K;
  llint ans = 0;
  vector<pair<llint, llint>> retu(n + 2);
  vector<llint> wa(n + 2);
  retu[0] = mp(0L, 0L);
  retu[n + 1] = mp(0L, 0L);
  for (i = 1; i <= n; i++) {
    llint a, d;
    cin >> a >> d;
    retu[i] = mp(a, d);
    wa[i] = wa[i - 1] + a * L + d * L * (L - 1) / 2;
  }
  wa[n + 1] = wa[n];
  //????????????????????????,??????????°?????????????
  llint kan = K / L;
  llint ama = K % L;
  for (i = 0; i + kan + 1 <= n + 1; i++) { // i????°?????????????
    llint ou = wa[i + kan] - wa[i];        //?????¨???????????????????????????
    maxeq(ans, ou + retu[i + kan + 1].fir * ama +
                   retu[i + kan + 1].sec * ama * (ama - 1) / 2);
    maxeq(ans, ou + (retu[i].fir + retu[i].sec * L) * ama -
                   retu[i].sec * ama * (ama + 1) / 2);
    llint dx = retu[i].sec - retu[i + kan + 1].sec; // dx?????£????????¶
    llint sy = retu[i].fir + retu[i].sec * L - retu[i + kan + 1].fir -
               retu[i + kan + 1].sec * ama; //??£????????¶
    // cout<<"de"<<ans<<" "<<dx<<" "<<sy<<endl;
    if (dx <= 0 || sy <= 0) {
      continue;
    }
    llint mae = sy / dx;
    if (mae <= 0 || ama <= mae) {
      continue;
    }
    llint syo = retu[i].fir + retu[i].sec * (L - mae); //??????
    ou += syo * mae + retu[i].sec * mae * (mae - 1) / 2;
    llint ato = ama - mae;
    ou += retu[i + kan + 1].fir * ato +
          retu[i + kan + 1].sec * ato * (ato - 1) / 2;
    maxeq(ans, ou);
  }
  kan--;
  ama += L; //?????????????°????????????? kan?????????????????????
  for (i = 0; i + kan + 1 <= n + 1; i++) { // i????°?????????????
    // cout<<"de"<<ans<<endl;
    llint ou = wa[i + kan] - wa[i]; //?????¨???????????????????????????
    llint dx = retu[i].sec - retu[i + kan + 1].sec; // dx?????£????????¶
    llint sy = retu[i].fir + retu[i].sec * L - retu[i + kan + 1].fir -
               retu[i + kan + 1].sec * ama; //??£????????¶
    if (dx <= 0 || sy <= 0) {
      continue;
    }
    llint mae = sy / dx;
    if (mae <= ama - L || L <= mae) {
      continue;
    }
    llint syo = retu[i].fir + retu[i].sec * (L - mae); //??????
    ou += syo * mae + retu[i].sec * mae * (mae - 1) / 2;
    llint ato = ama - mae;
    ou += retu[i + kan + 1].fir * ato +
          retu[i + kan + 1].sec * ato * (ato - 1) / 2;
    maxeq(ans, ou);
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
//<< setprecision(5)
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define dme                                                                    \\
  cout << -1 << endl;                                                          \\
  return 0
const int mod = 1000000007;
const int big = 1e9 + 10;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-7;
template <class T, class U> void mineq(T &a, U b) {
  if (a > b) {
    a = b;
  }
}
template <class T, class U> void maxeq(T &a, U b) {
  if (a < b) {
    a = b;
  }
}
llint gcd(llint a, llint b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
llint lcm(llint a, llint b) { return a / gcd(a, b) * b; }
int main(void) {
  // for check
  llint n, l, k, i, j;
  cin >> n >> l >> k;
  if (n * l > 1e9) {
    cout << "muri" << endl;
  }
  vector<llint> x(n * l + 1); //?´????WA
  for (i = 0; i < n; i++) {
    llint a, d;
    cin >> a >> d;
    if (d > 1000 || a > 100000) {
      a /= x[0];
      x[-23] += 9;
    } // RE
    for (j = 0; j < l; j++) {
      x[1 + i * l + j] = x[i * l + j] + a + d * j;
    }
  }
  llint ans = 0;
  for (i = 0; i + k <= n * l; i++) {
    maxeq(ans, x[i + k] - x[i]);
  }
  cout << ans << endl;
}