alexjercan/bugnet huggingface dataset:
Problem p00502 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

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

struct Dress {
  int A, B, C;
};

const int MAX = 210;

bool canWear(int temp, Dress d) { return (d.A <= temp && temp <= d.B); }

int main() {
  int D, N;
  while (cin >> D >> N) {
    vector<Dress> dress(MAX);
    vector<int> temp(MAX);
    rep(i, D) cin >> temp[i];
    rep(i, N) cin >> dress[i].A >> dress[i].B >> dress[i].C;

    int dp[MAX][MAX];
    memset(dp, -1, sizeof(dp));
    rep(i, N) if (canWear(temp[0], dress[i])) dp[0][i] = 0;

    REP(i, 1, D) {
      REP(j, 0, N) {
        REP(k, 0, N) {
          if (dp[i - 1][j] == -1)
            continue;
          if (!canWear(temp[i], dress[k]))
            continue;
          dp[i][k] = max(dp[i][k], dp[i - 1][j] + abs(dress[j].C - dress[k].C));
        }
      }
    }

    int ans = -1;
    rep(i, N) ans = max(ans, dp[D - 1][i]);
    cout << ans << endl;
  }
  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

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

struct Dress {
  int A, B, C;
};

const int MAX = 220;

bool canWear(int temp, Dress d) { return (d.A <= temp && temp <= d.B); }

int main() {
  int D, N;
  while (cin >> D >> N) {
    vector<Dress> dress(D);
    vector<int> temp(N);
    rep(i, D) cin >> temp[i];
    rep(i, N) cin >> dress[i].A >> dress[i].B >> dress[i].C;

    int dp[MAX][MAX];
    memset(dp, -1, sizeof(dp));
    rep(i, N) if (canWear(temp[0], dress[i])) dp[0][i] = 0;

    for (int i = 1; i < D; i++) {
      for (int j = 0; j < N; j++) {
        for (int k = 0; k < N; k++) {
          if (dp[i - 1][j] == -1)
            continue;
          if (!canWear(temp[i], dress[k]))
            continue;
          dp[i][k] = max(dp[i][k], dp[i - 1][j] + abs(dress[j].C - dress[k].C));
        }
      }
    }

    int ans = -1;
    rep(i, N) ans = max(ans, dp[D - 1][i]);
    cout << ans << endl;
  }
  return 0;
}
Problem p00502 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;

#define REP(i, s, n) for (int i = (s); i < int(n); i++)
#define rep(i, n) REP(i, 0, n)
#define all(co) co.begin(), co.end()

int dp[201][201];

int main() {
  int d, n;
  cin >> d >> n;
  vector<int> ts(d), as(n), bs(n), cs(n);
  rep(i, d) cin >> ts[i];
  rep(i, n) cin >> as[i] >> bs[i] >> cs[i];

  rep(i, n) if (ts[0] < as[i] || bs[i] < ts[0]) dp[0][i] = -(1 << 29);
  REP(i, 1, d) rep(j, n) {
    int t = ts[i], a = as[j], b = bs[j], c = cs[j];
    if (a <= t && t <= b)
      rep(k, n) { dp[i][j] = max(dp[i][j], dp[i - 1][k] + abs(cs[k] - c)); }
  }

  cout << *max_element(dp[d - 1], dp[d - 1] + n) << endl;

  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

#define REP(i, s, n) for (int i = (s); i < int(n); i++)
#define rep(i, n) REP(i, 0, n)
#define all(co) co.begin(), co.end()

int dp[201][201];

int main() {
  int d, n;
  cin >> d >> n;
  vector<int> ts(n), as(n), bs(n), cs(n);
  rep(i, d) cin >> ts[i];
  rep(i, n) cin >> as[i] >> bs[i] >> cs[i];

  rep(i, n) if (ts[0] < as[i] || bs[i] < ts[0]) dp[0][i] = -(1 << 29);
  REP(i, 1, d) rep(j, n) {
    int t = ts[i], a = as[j], b = bs[j], c = cs[j];
    if (a <= t && t <= b)
      rep(k, n) { dp[i][j] = max(dp[i][j], dp[i - 1][k] + abs(cs[k] - c)); }
  }

  cout << *max_element(dp[d - 1], dp[d - 1] + n) << endl;

  return 0;
}
Problem p00502 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i <= b; i++)
static const int INF = 8931919114514810;
static const int MAX_N = 300;
using namespace std;

int A[300], B[300], C[300];
int mem[MAX_N][MAX_N];
int N, D;
int Day[MAX_N];
int cal(int day, int pp) {
  int res = 0;
  if (mem[day][pp] != -1) {
    return mem[day][pp];
  }
  if (day == N + 1) {
    return res = 0;
  }
  if (day <= 1) {
    FOR(i, 1, D) {
      if (Day[day] >= A[i] && Day[day] <= B[i]) {
        res = max(cal(day + 1, C[i]), res);
      }
    }
  } else {
    FOR(i, 1, D) {
      if (Day[day] >= A[i] && Day[day] <= B[i]) {
        int temp = abs(pp - C[i]);
        res = max(cal(day + 1, C[i]) + temp, res);
      }
    }
  }
  return mem[day][pp] = res;
}

int main() {
  cin >> N >> D;
  memset(mem, -1, sizeof(mem));
  FOR(i, 1, N) { cin >> Day[i]; }
  FOR(i, 1, D) { cin >> A[i] >> B[i] >> C[i]; }
  cout << cal(1, 0) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
#define FOR(i, a, b) for (int i = a; i <= b; ++i)

using namespace std;

static const LL MAX_N = 1e+5;
static const LL INF = 1 << 25;

struct clothes {
  LL A[MAX_N], B[MAX_N], C[MAX_N];
};

int N, D;
LL res = -INF;
LL Day[MAX_N];
LL mem[250][150];
clothes T;

LL cal(LL day, LL p, LL pp, LL sum) {
  if (!mem[day][p]) {
    res = mem[day][p];
  }
  if (res < sum + abs(pp - p)) {
    mem[day][p] = res = sum + abs(pp - p);
    // cout<<"hoge"<<endl;
  }
  if (day == N + 1)
    return 0;
  else {
    if (day == 0) {
      FOR(i, 1, D)
      if (Day[1] >= T.A[i] && Day[1] <= T.B[i])
        cal(day + 1, T.C[i], p, sum);
    }
    FOR(i, 1, D) {
      if (Day[day] >= T.A[i] && Day[day] <= T.B[i]) {
        if (day > 2)
          cal(day + 1, T.C[i], p, sum + abs(pp - p));
        else
          cal(day + 1, T.C[i], p, sum);
      }
    }
  }
  return mem[day][p] = res;
}

int main() {
  cin >> N >> D;
  FOR(i, 1, N) cin >> Day[i];
  FOR(i, 1, D) { cin >> T.A[i] >> T.B[i] >> T.C[i]; }
  int ans = 0;
  cal(0, 0, 0, 0);
  FOR(i, 1, 150) {
    if (ans < mem[N][i])
      ans = mem[N][i];
  }
  cout << ans << endl;
}
Problem p00502 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
int solve(int hi, int cs);
int d, n;
int t[1000];
int ta[1111], tb[1111], c[1111];
int mem[1111][1111] = {0};
int main() {
  cin >> d >> n;
  for (int i = 0; i < d; i++) {
    cin >> t[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> ta[i] >> tb[i] >> c[i];
  }
  cout << solve(0, 0) << endl;
  ;
}
int solve(int hi, int yc) {
  int maxx = 0;
  if (mem[hi][yc] != 0) {
    return mem[hi][yc];
  }
  if (hi == d) {
    return mem[hi][yc] = 0;
  } else {
    for (int i = 0; i < n; i++) {
      if (ta[i] <= t[hi] && t[hi] <= tb[i]) {
        maxx = max(maxx, solve(hi + 1, c[i]) + (hi != 0 ? abs(yc - c[i]) : 0));
      }
    }
    return mem[hi][yc] = maxx;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
int solve(int hi, int cs);
int d, n;
int t[1000];
int ta[1111], tb[1111], c[1111];
int main() {
  cin >> d >> n;
  for (int i = 0; i < d; i++) {
    cin >> t[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> ta[i] >> tb[i] >> c[i];
  }
  cout << solve(0, 0) << endl;
  ;
}
int solve(int hi, int yc) {
  int maxx = 0;
  if (hi == d) {
    return 0;
  } else {
    for (int i = 0; i < n; i++) {
      if (ta[i] <= t[hi] && t[hi] <= tb[i]) {
        maxx = max(maxx, solve(hi + 1, c[i]) + (hi != 0 ? abs(yc - c[i]) : 0));
      }
    }
    return maxx;
  }
}
Problem p00502 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define chmin(a, b) ((a) = min((a), (b)))
#define chmax(a, b) ((a) = max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);
const double eps = 1e-10;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

int main() {
  int d, n;
  cin >> d >> n;
  vector<int> t(d);
  for (int i = 0; i < d; i++)
    cin >> t[i];
  vector<int> a(n), b(n), c(n);
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i] >> c[i];

  vector<vector<int>> dp(d + 10, vector<int>(n + 10, 0));

  for (int i = 1; i < d; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        if (a[k] <= t[i] && t[i] <= b[k] && a[j] <= t[i - 1] &&
            t[i - 1] <= b[j]) {
          dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + abs(c[k] - c[j]));
        }
      }
    }
  }

  int ans = 0;
  for (int i = 0; i <= n; i++) {
    ans = max(ans, dp[d][i]);
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define chmin(a, b) ((a) = min((a), (b)))
#define chmax(a, b) ((a) = max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);
const double eps = 1e-10;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

int main() {
  int d, n;
  cin >> d >> n;
  vector<int> t(d);
  for (int i = 0; i < d; i++)
    cin >> t[i];
  vector<int> a(n), b(n), c(n);
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i] >> c[i];

  vector<vector<int>> dp(d + 10, vector<int>(110, 0));

  for (int i = 1; i < d; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        if (a[k] <= t[i] && t[i] <= b[k] && a[j] <= t[i - 1] &&
            t[i - 1] <= b[j]) {
          dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + abs(c[k] - c[j]));
        }
      }
    }
  }

  int ans = 0;
  for (int i = 0; i <= 100; i++) {
    ans = max(ans, dp[d][i]);
  }

  cout << ans << endl;
}
Problem p00502 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n, m, a[200], b[200][3], mem[200][101];

int hot(int day, int bef) {

  int ret = 0;
  if (day == n)
    return 0;
  if (mem[day][bef] != -1)
    return mem[day][bef];

  for (int i = 0; i < m; i++) {
    if (b[i][0] <= a[day] && b[i][1] >= a[day]) {
      if (day == 0)
        ret = max(ret, hot(day + 1, b[i][2]));
      else
        ret = max(ret, hot(day + 1, b[i][2]) + abs(bef - b[i][2]));
    }
  }

  return mem[day][bef] = ret;
}
int main() {

  memset(mem, -1, sizeof(mem));

  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i][0] >> b[i][1] >> b[i][2];

  cout << hot(0, 0) << endl;

  return (0);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m, a[201], b[3][201];
int mem[200][20000];
int hot(int day, int bef);

int main() {

  cin >> n >> m;

  memset(mem, -1, sizeof(mem));

  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[0][i] >> b[1][i] >> b[2][i];

  cout << hot(0, 0) << endl;

  return (0);
}

int hot(int day, int bef) {
  int ret = 0;
  // if(mem[day][bef] != -1) return mem[day][bef];
  if (day < n) {
    for (int i = 0; i < m; i++) {
      if (a[day] >= b[0][i] && a[day] <= b[1][i]) {
        if (day == 0) {
          ret = max(ret, hot(day + 1, b[2][i]));
        } else {
          ret = max(ret,
                    hot(day + 1, b[2][i]) + (int)fabs((double)bef - b[2][i]));
        }
      }
    }
  }
  return /*mem[day][bef] =*/ret;
}
Problem p00502 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int MAX_D = 210;
const int MAX_N = 210;
const int MAX_Ci = 110;
int t[MAX_D];
int a[MAX_N], b[MAX_N], c[MAX_N];
int dp[MAX_D][MAX_Ci]; // ??\\??????????????????????´???????

int d, n;

int func(int day, int before) {
  if (day >= d)
    return 0;
  if (dp[day][before] >= 0)
    return dp[day][before];
  int res = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i] <= t[day] && t[day] <= b[i]) {
      int score = before - c[i];
      if (score < 0)
        score = -score;
      res = max(res, score + func(day + 1, c[i]));
    }
  }
  return dp[day][before] = res;
}

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

  cin >> d >> n;

  for (int i = 0; i <= d; ++i) {
    for (int j = 0; j <= MAX_Ci; ++j) {
      dp[i][j] = -1;
    }
  }

  for (int i = 0; i < d; ++i) {
    cin >> t[i];
  }

  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> b[i] >> c[i];
  }

  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i] <= t[0] && t[0] <= b[i])
      ans = max(ans, func(1, c[i]));
  }

  cout << ans << endl;
  // cout << func(0, 0) << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

const int MAX_D = 210;
const int MAX_N = 210;
int t[MAX_D];
int a[MAX_N], b[MAX_N], c[MAX_N];
int dp[MAX_D][MAX_N]; // ??\\??????????????????

int d, n;

int func(int day, int before) {
  if (day >= d)
    return 0;
  int res = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i] <= t[day] && t[day] <= b[i]) {
      int score = before - c[i];
      if (score < 0)
        score = -score;
      res = max(res, score + func(day + 1, c[i]));
    }
  }
  return res;
}

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

  cin >> d >> n;

  for (int i = 0; i < d; ++i) {
    cin >> t[i];
  }

  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> b[i] >> c[i];
  }

  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i] <= t[0] && t[0] <= b[i])
      ans = max(ans, func(1, c[i]));
  }

  cout << ans << endl;
  // cout << func(0, 0) << endl;

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

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

int temp[201];
int a[201], b[201], c[201];

int dp[201][201];

int main() {
  int D, N;
  cin >> D >> N;
  for (int i = 0; i < D; ++i)
    cin >> temp[i];
  for (int i = 0; i < N; ++i)
    cin >> a[i] >> b[i] >> c[i];

  for (int i = 0; i < D; ++i)
    for (int j = 0; j < 201; ++j)
      dp[i][j] = 1 << 29;

  for (int i = 0; i < N; ++i) {
    if (temp[0] <= b[i] && a[i] <= temp[0])
      dp[0][c[i]] = 0;
  }

  for (int i = 1; i < D; ++i) {
    for (int j = 0; j < N; ++j) {
      if (temp[i] <= b[j] && a[j] <= temp[i]) {
        for (int k = 0; k < 201; ++k) {
          if (dp[i - 1][k] == 1 << 29)
            continue;
          if (dp[i][c[j]] == 1 << 29)
            dp[i][c[j]] = dp[i - 1][k] + abs(k - c[j]);
          dp[i][c[j]] = max(dp[i - 1][k] + abs(k - c[j]), dp[i][c[j]]);
        }
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < 201; ++i) {
    if (dp[D - 1][i] == 1 << 29)
      continue;
    ans = max(ans, dp[D - 1][i]);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

int temp[201];
int a[201], b[201], c[201];

int dp[101][201];

int main() {
  int D, N;
  cin >> D >> N;
  for (int i = 0; i < D; ++i)
    cin >> temp[i];
  for (int i = 0; i < N; ++i)
    cin >> a[i] >> b[i] >> c[i];

  for (int i = 0; i < D; ++i)
    for (int j = 0; j < 201; ++j)
      dp[i][j] = 1 << 29;

  for (int i = 0; i < N; ++i) {
    if (temp[0] <= b[i] && a[i] <= temp[0])
      dp[0][c[i]] = 0;
  }

  for (int i = 1; i < D; ++i) {
    for (int j = 0; j < N; ++j) {
      if (temp[i] <= b[j] && a[j] <= temp[i]) {
        for (int k = 0; k < 201; ++k) {
          if (dp[i - 1][k] == 1 << 29)
            continue;
          if (dp[i][c[j]] == 1 << 29)
            dp[i][c[j]] = dp[i - 1][k] + abs(k - c[j]);
          dp[i][c[j]] = max(dp[i - 1][k] + abs(k - c[j]), dp[i][c[j]]);
        }
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < 201; ++i) {
    if (dp[D - 1][i] == 1 << 29)
      continue;
    ans = max(ans, dp[D - 1][i]);
  }
  cout << ans << endl;
}
Problem p00502 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;
int dayCanW[200];
int dayM[200][200];
int days, clothes;
int clothD[200][3];
int temple[200];
int Redirector[201][101];
int bestCloth(int Nowday, int yesterday) {
  int point = 0;
  int pointM = 0;
  if (Nowday < days) {
    if (Redirector[Nowday][yesterday] == -1) {
      for (int i = 0; i < clothes; i++) {
        if (temple[Nowday] >= clothD[i][0] && temple[Nowday] <= clothD[i][1]) {
          point = bestCloth(Nowday + 1, clothD[i][2]);
          if (Nowday != 0) {
            point += (int)abs((double)clothD[i][2] - yesterday);
          }
          if (point > pointM) {
            pointM = point;
          }
        }
      }

      Redirector[Nowday][yesterday] = pointM;
    } else {
    }
  } else {
    return 0;
  }

  return Redirector
      [Nowday]
      [yesterday]; //;//自分の今までの派手さ絶対値の合計（今日ー昨日まで）
}
int main(void) {
  for (int i = 0; i < 201; i++) {
    for (int j = 0; j < 101; j++) {
      Redirector[i][j] = -1;
    }
  }
  cin >> days;
  cin >> clothes;
  for (int i = 0; i < days; i++) {
    cin >> temple[i];
  }
  for (int i = 0; i < clothes; i++) {
    cin >> clothD[i][0];
    cin >> clothD[i][1];
    cin >> clothD[i][2];
  }
  for (int i = 0; i < days; i++) {
    dayCanW[i] = 0;
    for (int j = 0; j < clothes; j++) {
      dayM[i][j] = 0;
    }
  }

  cout << bestCloth(0, 0) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
int dayCanW[200];
int dayM[200][200];
int days, clothes;
int clothD[200][3];
int temple[200];

int bestCloth(int Nowday, int yesterday) {
  int point = 0;
  int pointM = 0;
  if (Nowday < days) {
    for (int i = 0; i < clothes; i++) {
      if (temple[Nowday] >= clothD[i][0] && temple[Nowday] <= clothD[i][1]) {
        point = bestCloth(Nowday + 1, clothD[i][2]);
        if (Nowday != 0) {
          point += (int)abs((double)clothD[i][2] - yesterday);
        }
        if (point > pointM) {
          pointM = point;
        }
      }
    }
  } else {
    return 0;
  }
  return pointM; // 自分の今までの派手さ絶対値の合計（今日ー昨日まで）
}

int main(void) {

  cin >> days;
  cin >> clothes;
  for (int i = 0; i < days; i++) {
    cin >> temple[i];
  }
  for (int i = 0; i < clothes; i++) {
    cin >> clothD[i][0];
    cin >> clothD[i][1];
    cin >> clothD[i][2];
  }
  for (int i = 0; i < days; i++) {
    dayCanW[i] = 0;
    for (int j = 0; j < clothes; j++) {
      dayM[i][j] = 0;
    }
  }
  cout << bestCloth(0, 0) << endl;
  return 0;
}
Problem p00502 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int a[500], b[500], c[500], day[500], d, n, ans[500][500], fans;

int main() {
  cin >> d >> n;
  for (int i = 0; i < d; i++)
    cin >> day[i];
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i] >> c[i];

  for (int i = 1; i < d; i++) {
    for (int j = 0; j < n; j++) {
      if (a[j] <= day[i] && day[i] <= b[j]) {
        for (int k = 0; k < n; k++) {
          if (a[k] <= day[i - 1] && day[i - 1] <= b[k])
            ans[i][j] = max(ans[i][j],
                            ans[i - 1][k] + max(c[j], c[k]) - min(c[j], c[k]));
        }
      }
    }
  }

  for (int i = 0; i < n; i++)
    fans = max(fans, ans[d - 1][i]);

  cout << fans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a, b, c, d, n, memo[500][500], a1, a2, m1, m2;
vector<pair<int, int>> updown;
vector<int> day;
vector<pair<int, int>> huku;
int solve(int dp, int one, int sum);

int main() {

  cin >> d >> n;
  for (int i = 0; i < d; i++) {
    cin >> a;
    day.push_back(a);
  }
  for (int i = 0; i < n; i++) {
    cin >> a >> b >> c;
    updown.push_back(make_pair(a, b));
    huku.push_back(make_pair(c, i));
  }

  std::sort(huku.begin(), huku.end());

  for (int i = 0; i < n; i++) {
    if (updown[huku[i].second].second >= day[0] &&
        day[0] >= updown[huku[i].second].first) {
      m1 = solve(1, huku[i].first, 0);
      break;
    }
  }
  for (int i = n - 1; i > -1; i--) {
    if (updown[huku[i].second].second >= day[0] &&
        day[0] >= updown[huku[i].second].first) {
      m2 = solve(1, huku[i].first, 0);
      break;
    }
  }

  cout << max(m1, m2) << endl;

  return 0;
}

int solve(int dp, int one, int sum) {

  // cout << "#######" << endl;

  int s1 = 0, s2 = 0;
  if (dp == d)
    return sum;
  else {
    if (memo[dp][one] != 0 && memo[dp][one] == sum)
      return memo[dp][one];

    for (int i = 0; i < n; i++) {
      if (updown[huku[i].second].second >= day[dp] &&
          day[dp] >= updown[huku[i].second].first) {
        int sa = max(one, huku[i].first) - min(one, huku[i].first);
        s1 = solve(dp + 1, huku[i].first, sum + sa);
        break;
      }
    }
    for (int i = n - 1; i > -1; i--) {
      if (updown[huku[i].second].second >= day[dp] &&
          day[dp] >= updown[huku[i].second].first) {
        int sa = max(one, huku[i].first) - min(one, huku[i].first);
        s2 = solve(dp + 1, huku[i].first, sum + sa);
        break;
      }
    }
    return memo[dp][one] = max(s1, s2);
  }
}
Problem p00502 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>

const int INF = 33554432;

struct W {
  int min, max;
  int c;
};

std::vector<W> ws;
std::vector<int> ts;
long dp[300][300];
int n;
int m;

long solve(int d, int before) {
  if (d == n) {
    return 0;
  }

  if (dp[d][before] != -1) {
    return dp[d][before];
  }

  long res = -INF;
  for (int i = 0; i < m; ++i) {
    if (ws[i].min <= ts[d] && ts[d] <= ws[i].max) {
      if (d == 0) {
        res = std::max(res, solve(d + 1, i));
      } else {
        res = std::max(res, solve(d + 1, i) + std::abs(ws[before].c - ws[i].c));
      }
    }
  }

  return dp[d][before] = res;
}

int main() {
  memset(dp, -1, sizeof(dp));

  std::cin >> n >> m;
  ts.resize(n);
  ws.resize(m);
  for (int i = 0; i < n; ++i) {
    std::cin >> ts[i];
  }
  for (int i = 0; i < m; ++i) {
    std::cin >> ws[i].min >> ws[i].max >> ws[i].c;
  }

  std::cout << solve(0, 0) << std::endl;
}

Error: TLE

Fixed submission:

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

const int INF = 33554432;

struct W {
  int min, max;
  int c;
};

std::vector<W> ws;
std::vector<int> ts;
int n;
int m;

long solve(int d, W before) {
  if (d == n) {
    return 0;
  }

  long res = -INF;
  for (int i = 0; i < m; ++i) {
    if (ws[i].min <= ts[d] && ts[d] <= ws[i].max) {
      if (d == 0) {
        res = std::max(res, solve(d + 1, ws[i]));
      } else {
        res = std::max(res, solve(d + 1, ws[i]) + std::abs(before.c - ws[i].c));
      }
    }
  }

  return res;
}

int main() {
  std::cin >> n >> m;
  ts.resize(n);
  ws.resize(m);
  for (int i = 0; i < n; ++i) {
    std::cin >> ts[i];
  }
  for (int i = 0; i < m; ++i) {
    std::cin >> ws[i].min >> ws[i].max >> ws[i].c;
  }

  W dummy;
  std::cout << solve(0, dummy) << std::endl;
}