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

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef ll Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
// #define mt make_tuple
// typedef tuple<double,int,double> tp;
// typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
const double PI = acos(-1);
const double EPS = 1e-7;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
ll n, l, k;
vp in(n);
ll calc(ll q, ll T) {
  if (q < 0 || q >= n)
    return 0;
  return (ll)(in[q].first * 2 + (T - 1) * in[q].second) * T / 2;
}
ll f(ll a, ll t, ll e) {
  //	cout<<a<<" "<<t<<endl;
  ll T = k - e * l - t;
  ll co = e * l;
  ll sum = 0;
  ll q = a + e;
  if (t < 0 || t > l)
    return 0;
  if (T < 0 || T > l)
    return 0;
  sum += calc(q, T);
  //	cout<<a<<" "<<t<<" "<<sum<<endl;
  sum += calc(a - 1, l) - calc(a - 1, l - t);
  //	cout<<a<<" "<<t<<" "<<sum<<endl;
  return sum;
}
int main() {
  cin >> n >> l >> k;
  in = vp(n);
  rep(i, n) cin >> in[i].first >> in[i].second;
  vi su(n + 1);
  rep(i, n) { su[i + 1] = (in[i].first * 2 + (l - 1) * in[i].second) * l / 2; }
  rep(i, n) su[i + 1] += su[i];
  ll out = 0;
  ll e = k / l;
  rep(q, n - e + 1) {
    //		cout<<su[q+(k-1)/l]-su[q]<<endl;
    ll sum = su[q + e] - su[q];
    ll L = 0, r = k - e * l + 1;
    //		cout<<L<<" "<<r<<endl;
    while (r - L > 10) {
      ll h1 = L + (r - L) / 3;
      ll h2 = L + (r - L) / 3 * 2;
      ll t1 = f(q, h1, e);
      ll t2 = f(q, h2, e);
      if (t1 > t2)
        r = h2;
      else
        L = h1;
    }
    loop(i, max(0ll, L - 100), min(k - e * l + 1, L + 100)) {
      out = max(out, sum + f(q, i, e));
    }
  }
  if (k - (k / l - 1) * l < 2 * l) {
    e = k / l - 1;
    rep(q, n - e + 1) {
      //		cout<<su[q+(k-1)/l]-su[q]<<endl;
      ll sum = su[q + e] - su[q];
      ll L = 0, r = k - e * l + 1;
      //		cout<<L<<" "<<r<<endl;
      while (r - L > 10) {
        ll h1 = L + (r - L) / 3;
        ll h2 = L + (r - L) / 3 * 2;
        ll t1 = f(q, h1, e);
        ll t2 = f(q, h2, e);
        if (t1 > t2)
          r = h2;
        else
          L = h1;
      }
      loop(i, max(0ll, L - 100), min(k - e * l + 1, L + 100)) {
        out = max(out, sum + f(q, i, e));
      }
    }
  }
  cout << out << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef ll Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
// #define mt make_tuple
// typedef tuple<double,int,double> tp;
// typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
const double PI = acos(-1);
const double EPS = 1e-7;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
ll n, l, k;
vp in(n);
ll calc(ll q, ll T) {
  if (q < 0 || q >= n)
    return 0;
  return (ll)(in[q].first * 2 + (T - 1) * in[q].second) * T / 2;
}
ll f(ll a, ll t) {
  //	cout<<a<<" "<<t<<endl;
  ll T = k - (k - 1) / l * l - t;
  ll co = (k - 1) / l * l;
  ll sum = 0;
  ll q = a + (k - 1) / l;
  sum += calc(q, T);
  //	cout<<a<<" "<<t<<" "<<sum<<endl;
  sum += calc(a - 1, l) - calc(a - 1, l - t);
  //	cout<<a<<" "<<t<<" "<<sum<<endl;
  return sum;
}
int main() {
  cin >> n >> l >> k;
  in = vp(n);
  rep(i, n) cin >> in[i].first >> in[i].second;
  vi su(n + 1);
  rep(i, n) { su[i + 1] = (in[i].first * 2 + (l - 1) * in[i].second) * l / 2; }
  rep(i, n) su[i + 1] += su[i];
  ll out = 0;
  rep(q, n - (k - 1) / l + 1) {
    //		cout<<su[q+(k-1)/l]-su[q]<<endl;
    ll sum = su[q + (k - 1) / l] - su[q];
    ll L = 0, r = k - (k - 1) / l * l + 1;
    //		cout<<L<<" "<<r<<endl;
    while (r - L > 5) {
      ll h1 = (r - L) / 3;
      ll h2 = (r - L) / 3 * 2;
      ll t1 = f(q, h1);
      ll t2 = f(q, h2);
      if (t1 > t2)
        r = h2;
      else
        L = h1;
    }
    loop(i, max(0ll, L - 10), min(k - (k - 1) / l * l + 1, L + 10)) {
      out = max(out, sum + f(q, i));
    }
  }
  cout << out << endl;
}