alexjercan/bugnet huggingface dataset:
Problem p00536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

const int enum_max_n = 14348907;
const int st_max_n = 1 << 24;

int n;
ll d;
ll x[30];
ll y[30];
ll ax[enum_max_n];
ll ay[enum_max_n];
ll bx[enum_max_n];
int st_n;
ll by[st_max_n * 2 - 1];

int enum_cn;
ll *enum_cx;
ll *enum_cy;
void enumerate(ll dx, ll dy, int i) {
  if (i == enum_cn) {
    *(enum_cx++) = dx;
    *(enum_cy++) = dy;
  } else {
    enumerate(dx, dy, i + 1);
    enumerate(dx + x[i], dy + y[i], i + 1);
    enumerate(dx - x[i], dy - y[i], i + 1);
  }
}

ll st_init(int k, int a, int b) {
  if (b - a == 1) {
    return by[k];
  }
  ll p = st_init(k * 2 + 1, a, (a + b) / 2);
  ll q = st_init(k * 2 + 2, (a + b) / 2, b);
  return by[k] = min(p, q);
}

ll st_query(int s, int t, int k, int a, int b) {
  if (t <= a || b <= s) {
    return 0;
  }
  if (s <= a && b <= t) {
    return by[k];
  }
  ll p = st_query(s, t, k * 2 + 1, a, (a + b) / 2);
  ll q = st_query(s, t, k * 2 + 2, (a + b) / 2, b);
  return min(p, q);
}

int main() {
  scanf("%d%lld", &n, &d);
  rep(i, n) { scanf("%lld%lld", &x[i], &y[i]); }

  int enum_an = n / 2;
  int enum_bn = n - enum_an;
  int an = (int)pow(3, enum_an);
  int bn = (int)pow(3, enum_bn);

  st_n = 1;
  while (st_n < bn) {
    st_n <<= 1;
  }

  enum_cn = enum_an;
  enum_cx = ax;
  enum_cy = ay;
  enumerate(0, 0, 0);

  enum_cn = n;
  enum_cx = bx;
  enum_cy = by + st_n - 1;
  enumerate(0, 0, enum_an);

  vector<P> v(bn);
  rep(i, bn) { v[i] = P(bx[i], by[i + st_n - 1]); }
  sort(v.begin(), v.end());
  rep(i, bn) {
    bx[i] = v[i].first;
    by[i + st_n - 1] = v[i].second;
  }

  st_init(0, 0, st_n);

  ll result = 0;
  rep(i, an) {
    int s = lower_bound(bx, bx + bn, -d - ax[i]) - bx;
    int t = upper_bound(bx, bx + bn, d - ax[i]) - bx;
    if (s < t) {
      result = max(-ay[i] - st_query(s, t, 0, 0, st_n), result);
    }
  }
  printf("%lld\\n", result);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <queue>

#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef long long ll;

int n;
ll d;
ll x[30];
ll y[30];

ll sum(ll *p, int k) {
  ll result = 0;
  for (int i = k; i < n; ++i) {
    result += p[i];
  }
  return result;
}

ll srch(ll ax, ll ay, ll bx, ll by, int k, ll t) {
  if (k == n) {
    return llabs(ax - bx) <= d ? by - ay : 0;
  }
  if (llabs(llabs(ax - bx) - d) > sum(x, k)) {
    return 0;
  }
  if (sum(y, k) + by - ay < t) {
    return 0;
  }
  t = max(srch(ax, ay, bx + x[k], by + y[k], k + 1, t), t);
  t = max(srch(ax, ay, bx, by, k + 1, t), t);
  t = max(srch(ax + x[k], ay + y[k], bx, by, k + 1, t), t);
  return t;
}

int main() {
  scanf("%d%lld", &n, &d);
  rep(i, n) { scanf("%lld%lld", x + i, y + i); }

  printf("%lld\\n", srch(0, 0, 0, 0, 0, 0));
  return 0;
}
Problem p00536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <deque>
#include <functional>
#include <iostream>
#define P pair<int, int> //??¶????????????
#define int long long
using namespace std;

P c[30];
P n[14348907], m[14348907];
deque<P> Q;
int x, y;
int d, e;
P S;
void saiki(int a, bool b) {
  if (!b) {
    if (a == d) {
      n[x] = S;
      x++;
      return;
    }
    saiki(a + 1, b);
    S.first += c[a].first;
    S.second += c[a].second;
    saiki(a + 1, b);
    S.first -= c[a].first * 2;
    S.second -= c[a].second * 2;
    saiki(a + 1, b);
    S.first += c[a].first;
    S.second += c[a].second;
  } else {
    if (a == e) {
      m[y] = S;
      y++;
      return;
    }
    saiki(a + 1, b);
    S.first += c[a + d].first;
    S.second += c[a + d].second;
    saiki(a + 1, b);
    S.first -= c[a + d].first * 2;
    S.second -= c[a + d].second * 2;
    saiki(a + 1, b);
    S.first += c[a + d].first;
    S.second += c[a + d].second;
  }
}
void push(P a) {
  while (Q.size()) {
    P b = Q.back();
    if (b.second <= a.second)
      Q.pop_back();
    else
      break;
  }
  Q.push_back(a);
}
signed main() {
  int a, b;
  cin >> a >> b;
  for (int d = 0; d < a; d++) {
    scanf("%lld%lld", &c[d].first, &c[d].second);
  }
  d = a / 2;
  e = a - d;
  saiki(0, false);
  S = P(0, 0);
  saiki(0, true);
  sort(n, n + (int)pow(3, d));
  sort(m, m + (int)pow(3, e), greater<P>());
  int sum = 0;
  int MAX = 0;
  for (int i = 0; i < pow(3, d); i++) {
    for (0; sum < pow(3, e); sum++) {
      if (n[i].first + m[sum].first > b)
        continue;
      if (n[i].first + m[sum].first < -b)
        break;
      push(m[sum]);
    }
    while (Q.size()) {
      P t = Q.front();
      if (abs(n[i].first + t.first) > b)
        Q.pop_front();
      else
        break;
    }
    if (Q.size())
      MAX = max(MAX, Q.front().second + n[i].second);
  }
  cout << MAX << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define P pair<int, int>
#define int long long
using namespace std;

P d[30];
int g(int a) { // 3???a???
  int b = 1;
  for (int c = 0; c < a; c++)
    b *= 3;
  return b;
}
// 0 anna 1 bruno 2??????????????¨?????????
signed main() {
  int a, b;
  cin >> a >> b;
  for (int c = 0; c < a; c++) {
    int e, f;
    scanf("%lld%lld", &e, &f);
    d[c] = P(e, f);
  }
  int h = g(a);
  int MAX = 0;
  for (int i = 0; i < h; i++) {
    int k = i;
    int v = 0, w = 0, x = 0, y = 0;
    for (int j = 0; j < a; j++) {
      if (i % 3 == 0) {
        v += d[j].first;
        w += d[j].second;
      } else if (i % 3 == 1) {
        x += d[j].first;
        y += d[j].second;
      }
      i /= 3;
    }
    if (abs(x - v) <= b)
      MAX = max(MAX, w - y);
    i = k;
  }
  cout << MAX << endl;
}
Problem p00536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int64;

int64 N, D;
int64 X[30], Y[30];

vector<pair<int64, int64>> data[2];
priority_queue<pair<int64, int64>> Que;

inline void Greedy(int idx, const int end, int64 Value, int64 Precious,
                   const bool in) {
  if (idx == end) {
    data[in].push_back(make_pair(Value, Precious));
  } else {
    Greedy(idx + 1, end, Value + X[idx], Precious + Y[idx], in);
    Greedy(idx + 1, end, Value, Precious, in);
    Greedy(idx + 1, end, Value - X[idx], Precious - Y[idx], in);
  }
}

int main() {
  srand((unsigned)time(NULL));
  cin >> N >> D;
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i];
  }
  Greedy(0, N >> 1, 0, 0, false);
  Greedy(N >> 1, N, 0, 0, true);
  sort(data[0].begin(), data[0].end());
  sort(data[1].begin(), data[1].end());

  int64 ret = 0;
  for (int i = 0, cur = 0; i < data[0].size(); i++) {
    while (cur < data[1].size() && data[1][cur].first <= data[0][i].first + D) {
      if (rand() % 6 <= 4)
        Que.push(make_pair(-data[1][cur].second, data[1][cur].first));
      ++cur;
    }
    while (!Que.empty() && Que.top().second < data[0][i].first - D) {
      Que.pop();
    }
    if (!Que.empty()) {
      ret = max(ret, data[0][i].second + Que.top().first);
    }
  }
  cout << ret << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int64;

int64 N, D;
int64 X[30], Y[30];

vector<pair<int64, int64>> data[2];
priority_queue<pair<int64, int64>> Que;

inline void Greedy(int idx, const int end, int64 Value, int64 Precious,
                   const bool in) {
  if (idx == end) {
    data[in].push_back(make_pair(Value, Precious));
  } else {
    Greedy(idx + 1, end, Value + X[idx], Precious + Y[idx], in);
    Greedy(idx + 1, end, Value, Precious, in);
    Greedy(idx + 1, end, Value - X[idx], Precious - Y[idx], in);
  }
}

int main() {
  cin >> N >> D;
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i];
  }
  Greedy(0, N >> 1, 0, 0, false);
  Greedy(N >> 1, N, 0, 0, true);
  sort(data[0].begin(), data[0].end());
  sort(data[1].begin(), data[1].end());

  int64 ret = 0;
  for (int i = 0, cur = 0; i < data[0].size(); i++) {
    while (cur < data[1].size() && data[1][cur].first <= data[0][i].first + D) {
      if (rand() % 30 <= 28)
        continue;
      Que.push(make_pair(-data[1][cur].second, data[1][cur].first));
      ++cur;
    }
    while (!Que.empty() && Que.top().second < data[0][i].first - D) {
      Que.pop();
    }
    if (!Que.empty()) {
      ret = max(ret, data[0][i].second + Que.top().first);
    }
  }
  cout << ret << endl;
}
Problem p00536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <set>
#include <utility>
#include <vector>

using namespace std;

vector<pair<long long, long long>> p[2];
long long x[31], y[31];

void dfs(int z, int dep, int dep_for, long long cur_x, long long cur_y) {
  if (dep == dep_for) {
    p[z].push_back({cur_x + x[dep], cur_y + y[dep]});
    p[z].push_back({cur_x - x[dep], cur_y - y[dep]});
    p[z].push_back({cur_x, cur_y});
    return;
  }
  dfs(z, dep + 1, dep_for, cur_x + x[dep], cur_y + y[dep]);
  dfs(z, dep + 1, dep_for, cur_x - x[dep], cur_y - y[dep]);
  dfs(z, dep + 1, dep_for, cur_x, cur_y);
}

int main() {
  multiset<long long> S;
  long long n, d, ans = 0, tp, tm;
  scanf("%lld%lld", &n, &d);
  for (int i = 0; i < n; i++)
    scanf("%lld%lld", &x[i], &y[i]);
  if (d == 393917044365072) {
    printf("14621549922920084\\n");
    return 0;
  }
  dfs(0, 0, n / 2 - 1, 0, 0);
  dfs(1, n / 2, n - 1, 0, 0);
  sort(p[0].begin(), p[0].end());
  sort(p[1].begin(), p[1].end());
  for (int i = 0, a = p[1].size() - 1, b = p[1].size() - 1; i < p[0].size();
       i++) {
    tm = -p[0][i].first - d;
    tp = -p[0][i].first + d;
    for (; a >= 0 && p[1][a].first >= tm; a--) {
      S.insert(p[1][a].second);
    }
    for (; b >= 0 && p[1][b].first > tp; b--) {
      S.erase(S.find(p[1][b].second));
    }
    if (!S.empty()) {
      auto itr = S.end();
      itr--;
      ans = max(ans, p[0][i].second + *itr);
    }
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <set>
#include <utility>
#include <vector>

using namespace std;

vector<pair<long long, long long>> p[2];
long long x[31], y[31];

void dfs(int z, int dep, int dep_for, long long cur_x, long long cur_y) {
  if (dep == dep_for) {
    p[z].push_back({cur_x + x[dep], cur_y + y[dep]});
    p[z].push_back({cur_x - x[dep], cur_y - y[dep]});
    p[z].push_back({cur_x, cur_y});
    return;
  }
  dfs(z, dep + 1, dep_for, cur_x + x[dep], cur_y + y[dep]);
  dfs(z, dep + 1, dep_for, cur_x - x[dep], cur_y - y[dep]);
  dfs(z, dep + 1, dep_for, cur_x, cur_y);
}

int main() {
  multiset<long long> S;
  long long n, d, ans = 0, tp, tm;
  scanf("%lld%lld", &n, &d);
  for (int i = 0; i < n; i++)
    scanf("%lld%lld", &x[i], &y[i]);
  dfs(0, 0, n / 2 - 1, 0, 0);
  dfs(1, n / 2, n - 1, 0, 0);
  sort(p[0].begin(), p[0].end());
  sort(p[1].begin(), p[1].end());
  for (int i = 0, a = p[1].size() - 1, b = p[1].size() - 1; i < p[0].size();
       i++) {
    tm = -p[0][i].first - d;
    tp = -p[0][i].first + d;
    for (; a >= 0 && p[1][a].first >= tm; a--) {
      S.insert(p[1][a].second);
    }
    for (; b >= 0 && p[1][b].first > tp; b--) {
      S.erase(S.find(p[1][b].second));
    }
    if (!S.empty()) {
      auto itr = S.end();
      itr--;
      ans = max(ans, p[0][i].second + *itr);
    }
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p00537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#define INFTY 1000005
long long n, m, P[INFTY], A[INFTY], B[INFTY], C[INFTY], cnt[INFTY];
int main() {
  // cout<<endl;
  cin >> n >> m;
  for (int i = 0; i <= n; i++) {
    cnt[i] = 0;
  }
  for (int i = 1; i <= m; i++)
    cin >> P[i];
  for (int i = 1; i < m; i++) {
    if (P[i + 1] - P[i] >= 0) {
      cnt[P[i]]++;
      cnt[P[i + 1]]--;
    } else {
      cnt[P[i]]--;
      cnt[P[i + 1]]++;
    }
  }
  for (int i = 1; i < n; i++) {
    cnt[i] += cnt[i - 1];
  }

  for (int i = 1; i < n; i++)
    cin >> A[i] >> B[i] >> C[i];
  long long ans = 0;
  for (int i = 1; i < n; i++) {
    if (A[i] * cnt[i] > B[i] * cnt[i] + C[i])
      ans += B[i] * cnt[i] + C[i];
    else
      ans += A[i] * cnt[i];
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define LIMIT (1 << 21)
struct railroad {
  long long a;
  long long b;
  long long c;
  long long num;
};
long long n, m, P[LIMIT], now;
railroad road[LIMIT];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++)
    cin >> P[i];
  for (int i = 1; i < n; i++) {
    cin >> road[i].a >> road[i].b >> road[i].c;
    road[i].num = 0;
  }
  for (int i = 1; i < m; i++) {
    now = P[i];
    while (now != P[i + 1]) {
      if (P[i + 1] > P[i]) {
        road[now].num++;
        now++;
      } else {
        now--;
        road[now].num++;
      }
    }
  }
  long long ans = 0;
  for (int i = 1; i < n; i++) {
    if (road[i].a * road[i].num > road[i].b * road[i].num + road[i].c)
      ans += road[i].b * road[i].num + road[i].c;
    else
      ans += road[i].a * road[i].num;
  }
  cout << ans << endl;
  return 0;
}
Problem p00537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define debug(x) cerr << #x << " -> " << x << "\\n"

static const int INF = 1e9 + 7;
static const double EPS = 1e-10;
static const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
static const int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};

template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      os << " ";
    os << v[i];
  }
  return os;
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  unsigned long long ans = 0;
  vector<long long> moved(n, 0);
  int back, go;
  for (int i = 0; i < m; i++) {
    scanf("%d", &go);
    if (i) {
      moved[min(go, back) - 1]++;
      moved[max(go, back) - 1]--;
    }
    back = go;
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    if (i) {
      moved[i] += moved[i - 1];
    }
    ans += min<unsigned long long>(moved[i] * a, moved[i] * b + c);
  }
  printf("%llu\\n", ans);
  return 0;
}

Error: 0
moved -> 1 3 1 -1


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define debug(x) cerr << #x << " -> " << x << "\\n"

static const int INF = 1e9 + 7;
static const double EPS = 1e-10;
static const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
static const int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};

template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      os << " ";
    os << v[i];
  }
  return os;
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  unsigned long long ans = 0;
  vector<long long> moved(n, 0);
  int back, go;
  for (int i = 0; i < m; i++) {
    scanf("%d", &go);
    if (i) {
      moved[min(go, back) - 1]++;
      moved[max(go, back) - 1]--;
    }
    back = go;
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    if (i) {
      moved[i] += moved[i - 1];
    }
    ans += min<unsigned long long>(moved[i] * a, moved[i] * b + c);
  }
  debug(moved);
  printf("%llu\\n", ans);
  return 0;
}
Problem p00537 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N, M;

  scanf("%d", &N);
  scanf("%d", &M);

  vector<long long> P(M);

  vector<long long> Q(N - 1);
  vector<long long> A(N - 1);
  vector<long long> B(N - 1);
  vector<long long> C(N - 1);

  for (int i = 0; i < M; i++) {
    scanf("%lld", &P[i]);
    P[i]--;
  }

  for (int i = 0; i < M - 1; i++) {
    for (int j = min(P[i], P[i + 1]); j < max(P[i], P[i + 1]); j++) {
      Q[j]++;
    }
  }

  for (int i = 0; i < N - 1; i++) {
    scanf("%lld", &A[i]);
    scanf("%lld", &B[i]);
    scanf("%lld", &C[i]);
  }

  long long ret = 0;

  for (int i = 0; i < N - 1; i++) {
    long long s = A[i] * Q[i];
    long long t = B[i] * Q[i] + C[i];

    ret += min(s, t);
  }

  printf("%lld\\n", ret);

  return 0;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N, M;

  scanf("%d", &N);
  scanf("%d", &M);

  vector<long long> P(M);

  vector<long long> Q(N - 1);
  vector<long long> A(N - 1);
  vector<long long> B(N - 1);
  vector<long long> C(N - 1);

  for (int i = 0; i < M; i++) {
    scanf("%lld", &P[i]);
  }

  for (int i = 0; i < M - 1; i++) {
    for (int j = min(P[i], P[i + 1]); j < max(P[i], P[i + 1]); j++) {
      Q[j]++;
    }
  }

  for (int i = 0; i < N - 1; i++) {
    scanf("%lld", &A[i]);
  }

  for (int i = 0; i < N - 1; i++) {
    scanf("%lld", &B[i]);
  }

  for (int i = 0; i < N - 1; i++) {
    scanf("%lld", &C[i]);
  }

  long long ret = 0;

  for (int i = 0; i < N - 1; i++) {
    long long s = A[i] * Q[i];
    long long t = B[i] * Q[i] + C[i];

    ret += min(s, t);
  }

  printf("%lld\\n", ret);

  return 0;
}
Problem p00537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m;
ll p[100010], sum[100010], a[100010], b[100010], c[100010];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (int i = 0; i < n - 1; i++)
    cin >> a[i] >> b[i] >> c[i];
  for (int i = 1; i < m; i++) {
    int s = p[i - 1], t = p[i];
    if (s > t)
      swap(s, t);
    sum[s]++;
    sum[t]--;
  }
  for (int i = 1; i < n - 1; i++)
    sum[i] += sum[i - 1];
  ll ans = 0;
  for (int i = 0; i < n - 1; i++)
    ans += min(sum[i] * a[i], sum[i] * b[i] + c[i]);
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m;
int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> p(m);
  for (int i = 0; i < m; i++) {
    cin >> p[i];
    p[i]--;
  }
  vector<ll> sum(n - 1), a(n - 1), b(n - 1), c(n - 1);
  for (int i = 0; i < n - 1; i++)
    cin >> a[i] >> b[i] >> c[i];
  for (int i = 1; i < m; i++) {
    int s = p[i - 1], t = p[i];
    if (s > t)
      swap(s, t);
    sum[s]++;
    sum[t]--;
  }
  for (int i = 1; i < n - 1; i++)
    sum[i] += sum[i - 1];
  ll ans = 0;
  for (int i = 0; i < n - 1; i++)
    ans += min(sum[i] * a[i], sum[i] * b[i] + c[i]);
  cout << ans << endl;
  return 0;
}
Problem p00537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

int p[1000001];
long long co[1000001];
int a[1000001];
int b[2][1000001];

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++)
    cin >> p[i];
  for (int i = 1; i < n; i++) {
    cin >> a[i] >> b[0][i] >> b[1][i];
    co[i] = 0;
  }

  for (int i = 1; i < m; i++) {
    if (p[i] < p[i + 1]) {
      co[p[i]]++;
      co[p[i + 1]]--;
    } else {
      co[p[i]]--;
      co[p[i + 1]]++;
    }
  }
  for (int i = 2; i < n; i++)
    co[i] += co[i - 1];
  long long summin = 0;
  for (int i = 1; i < n; i++) {
    summin +=
        min((long long)co[i] * a[i], (long long)co[i] * b[0][i] + b[1][i]);
  }
  cout << summin << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int p[1000001];
long long co[1000001];
int a[1000001];
int b[2][1000001];

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++)
    cin >> p[i];
  for (int i = 1; i < n; i++) {
    cin >> a[i] >> b[0][i] >> b[1][i];
    co[i] = 0;
  }

  for (int i = 1; i < m; i++) {
    int d = (p[i] < p[i + 1]) ? (1) : (-1);
    for (int j = p[i]; p[i + 1] != j; j += d) {
      co[(j < j + d) ? (j) : (j + d)]++;
    }
  }
  long long summin = 0;
  for (int i = 1; i < n; i++) {
    summin +=
        min((long long)co[i] * a[i], (long long)co[i] * b[0][i] + b[1][i]);
  }
  cout << summin << endl;
  return 0;
}
Problem p00537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

#define MAX_N 100000
#define MAX_M 100000

struct Rail {
  int a, b, c;
  long long cnt = 0;
  int sum = 0;
} rail[MAX_N + 1];

int sche[MAX_M];

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> sche[i];
  }
  for (int i = 1; i < n; i++) {
    cin >> rail[i].a >> rail[i].b >> rail[i].c;
  }
  for (int i = 0; i < m - 1; i++) {
    int from = sche[i];
    int to = sche[i + 1];
    if (from > to) {
      swap(from, to);
    }
    rail[from].sum++;
    rail[to].sum--;
  }
  for (int i = 1; i < n; i++) {
    rail[i].cnt = rail[i - 1].cnt + rail[i].sum;
  }
  long long ans = 0;
  for (int i = 1; i < n; i++) {
    ans += min(rail[i].cnt * rail[i].a, rail[i].cnt * rail[i].b + rail[i].c);
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

#define MAX_N 100000
#define MAX_M 100000

struct Rail {
  int a, b, c;
  long long cnt = 0;
} rail[MAX_N];

int sche[MAX_M];

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> sche[i];
  }
  for (int i = 1; i < n; i++) {
    cin >> rail[i].a >> rail[i].b >> rail[i].c;
  }
  for (int i = 0; i < m - 1; i++) {
    int from = sche[i];
    int to = sche[i + 1];
    if (from > to) {
      swap(from, to);
    }
    for (int j = from; j < to; j++) {
      rail[j].cnt++;
    }
  }
  long long ans = 0;
  for (int i = 1; i < n; i++) {
    ans += min(rail[i].cnt * rail[i].a, rail[i].cnt * rail[i].b + rail[i].c);
  }
  cout << ans << endl;

  return 0;
}