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

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;

int n, k;
vector<int> b[10];
int dp[10][2001] = {-1};
int d[2001];

int saiki(int index, int g, int m) {
  int ret = 0;
  if (index > k) {
    return 0;
  }
  if (index == k) {
    return dp[g][index] = m;
  } else if (g == 10)
    return 0;
  if (dp[g][index] > 0)
    return dp[g][index];
  REP(i, b[g].size()) { ret = max(ret, saiki(index + i, g + 1, m + b[g][i])); }
  return dp[g][index] = ret;
}

int main() {
  cin >> n >> k;
  memset(dp, -1, sizeof(dp));
  memset(d, 0, sizeof(d));
  REP(i, 10) { b[i].PB(0); }
  REP(i, n) {
    int c, g;
    cin >> c >> g;
    b[g - 1].PB(c);
  }
  REP(i, 10) {
    sort(b[i].begin() + 1, b[i].end(), greater<int>());
    if (b[i].size() <= 0)
      continue;
    REP(j, b[i].size() - 1) { b[i][j + 1] += b[i][j]; }
    REP(j, b[i].size()) { b[i][j] += (j - 1) * j; }
  }

  d[0] = 0;
  REP(i, 10) {
    RREP(j, n + 1) {
      if (d[j] < 0)
        continue;
      int s = min(n - j, (int)b[i].size() - 1);
      for (int l = s; l >= 0; l--) {
        d[j + l] = max(d[j + l], d[j] + b[i][l]);
      }
    }
  }
  int ans = 0;
  REP(i, n) { ans = max(ans, d[i]); }
  cout << d[k] << endl;
  /*
  int ans = saiki(0,0,0);
  cout << ans << endl;
  */
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;

int n, k;
vector<int> b[10];
int dp[10][2001] = {-1};

int saiki(int index, int g, int m) {
  int ret = 0;
  if (index > k) {
    return 0;
  }
  if (index == k) {
    return dp[g][index] = m;
  } else if (g == 10)
    return 0;
  if (dp[g][index] > 0)
    return dp[g][index];
  REP(i, b[g].size()) { ret = max(ret, saiki(index + i, g + 1, m + b[g][i])); }
  return dp[g][index] = ret;
}

int main() {
  cin >> n >> k;
  memset(dp, -1, sizeof(dp));
  REP(i, 10) { b[i].PB(0); }
  REP(i, n) {
    int c, g;
    cin >> c >> g;
    b[g - 1].PB(c);
  }
  REP(i, 10) {
    sort(b[i].begin() + 1, b[i].end(), greater<int>());
    if (b[i].size() <= 0)
      continue;
    REP(j, b[i].size() - 1) { b[i][j + 1] += b[i][j]; }
    REP(j, b[i].size()) { b[i][j] += (j - 1) * j; }
  }
  int ans = saiki(0, 0, 0);
  cout << ans << endl;
  return 0;
}
Problem p00484 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main() {
  for (int n, k; cin >> n >> k && n | k;) {
    vector<vector<int>> ps(10);
    for (int i = 0; i < n; i++) {
      int p, g;
      cin >> p >> g;
      ps[g - 1].push_back(p);
    }

    for (int i = 0; i < 10; i++)
      sort(ps[i].begin(), ps[i].end(), greater<int>());
    vector<vector<int>> ss = ps;
    for (int i = 0; i < 10; i++)
      for (int j = 1; j < ss[i].size(); j++)
        ss[i][j] += ss[i][j - 1] + 2 * j;

    vector<vector<int>> dp(2, vector<int>(n + 1));
    for (int i = 0; i < 10; i++) {
      dp[i + 1 & 1] = dp[i & 1];
      for (int j = 0; j < ss[i].size(); j++)
        for (int k = n; k > j; k--)
          dp[i + 1 & 1][k] =
              max(dp[i + 1 & 1][k], dp[i & 1][k - (j + 1)] + ss[i][j]);
    }
    cout << dp[0][k] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  for (int n, k; cin >> n >> k, n | k;) {
    vector<vector<int>> ps(10);
    for (int i = 0; i < n; i++) {
      int p, g;
      cin >> p >> g;
      ps[g - 1].push_back(p);
    }

    for (int i = 0; i < 10; i++)
      sort(ps[i].begin(), ps[i].end(), greater<int>());
    vector<vector<int>> ss = ps;
    for (int i = 0; i < 10; i++)
      for (int j = 1; j < ss[i].size(); j++)
        ss[i][j] += ss[i][j - 1] + 2 * j;

    vector<vector<int>> dp(2, vector<int>(n + 1));
    for (int i = 0; i < 10; i++) {
      dp[i + 1 & 1] = dp[i & 1];
      for (int j = 0; j < ss[i].size(); j++)
        for (int k = n; k > j; k--)
          dp[i + 1 & 1][k] =
              max(dp[i + 1 & 1][k], dp[i & 1][k - (j + 1)] + ss[i][j]);
    }
    cout << dp[0][k] << endl;
  }
  return 0;
}
Problem p00484 (C++) — buggy submission, verdict: Runtime Error


#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

int dp[16][2048] = {{-1}}; //	dp[ジャンル][売却総数]

int main(void) {
  int have; // 持ってる数
  int sell; // 売る数
  int value[16][2048] = {0};
  int kindnum[16] = {0};

  int ValBuffer, KindBuffer;

  int SoldField[16][2048]; // SoldField[ジャンル][売却数]

  scanf("%d%d", &have, &sell);
  for (int i = 0; i < have; i++) {
    scanf("%d%d", &ValBuffer, &KindBuffer);
    value[KindBuffer][kindnum[KindBuffer]] = ValBuffer;
    kindnum[KindBuffer]++;
  }
  for (int i = 1; i <= 10; i++) {
    sort(value[i], value[i] + kindnum[i]);
  }

  for (int i = 1; i <= 10; i++) {
    SoldField[i][0] = 0;
    for (int j = 1; j <= sell; j++) {
      if (j <= kindnum[i]) {
        SoldField[i][j] =
            SoldField[i][j - 1] + value[i][kindnum[i] - j] + (j - 1) * 2;
      } else
        break;
    }
  }
  for (int i = 0; i <= sell; i++) {
    dp[1][i] = SoldField[1][i];
  }
  // puts("###DBG0");

  for (int i = 1; i <= 10; i++) {     // ジャンル十種
    for (int j = 0; j <= sell; j++) { // 次の種類を売る冊数
      if (j > kindnum[i + 1])
        break;
      for (int k = 0; k <= sell; k++) { // 売られた冊数<=sell
        if (k + j > sell)
          break;
        if (dp[i][k] != -1) {
          // printf("kind:%d all:%d next:%d\\n",i,k,j);
          dp[i + 1][k + j] =
              max(dp[i][k] + SoldField[i + 1][j], dp[i + 1][k + j]);
        }
      }
    }
    // printf("kind = %d\\n",i);
  }

  int ans = dp[10][sell];

  printf("%d\\n", ans);

  return 0;
}

Error: 0

Fixed submission:


#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

int dp[11][2048] = {{-1}}; //	dp[ジャンル][売却総数]

int main(void) {
  int have; // 持ってる数
  int sell; // 売る数
  int value[16][2048] = {0};
  int kindnum[16] = {0};

  int ValBuffer, KindBuffer;

  int SoldField[10][2048]; // SoldField[ジャンル][売却数]

  scanf("%d%d", &have, &sell);
  for (int i = 0; i < have; i++) {
    scanf("%d%d", &ValBuffer, &KindBuffer);
    value[KindBuffer][kindnum[KindBuffer]] = ValBuffer;
    kindnum[KindBuffer]++;
  }
  for (int i = 0; i < 10; i++) {
    sort(value[i], value[i] + kindnum[i]);
  }

  for (int i = 1; i <= 10; i++) {
    SoldField[i][0] = 0;
    for (int j = 1; j <= sell; j++) {
      if (j <= kindnum[i]) {
        SoldField[i][j] =
            SoldField[i][j - 1] + value[i][kindnum[i] - j] + (j - 1) * 2;
      } else
        break;
    }
  }
  for (int i = 0; i <= sell; i++) {
    dp[1][i] = SoldField[1][i];
  }
  // puts("###DBG0");

  for (int i = 1; i <= 9; i++) {      // ジャンル十種
    for (int j = 0; j <= sell; j++) { // 次の種類を売る冊数
      if (j > kindnum[i + 1])
        break;
      for (int k = 0; k <= sell; k++) { // 売られた冊数<=sell
        if (k + j > sell)
          break;
        if (dp[i][k] != -1) {
          // printf("kind:%d all:%d next:%d\\n",i,k,j);
          dp[i + 1][k + j] =
              max(dp[i][k] + SoldField[i + 1][j], dp[i + 1][k + j]);
        }
      }
    }
    // printf("kind = %d\\n",i);
  }

  int ans = dp[10][sell];

  printf("%d\\n", ans);

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

#include <cstdio>
#include <queue>
using namespace std;

int n, k, c, g;
priority_queue<int> que[10];
int dp[10][2000];

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &c, &g);
    g--;
    que[g].push(c);
  }
  for (int i = 0; i < 10; i++)
    for (int j = 0; j < 2000; j++)
      dp[i][j] = -1;
  for (int i = 0; i < 10; i++)
    dp[i][0] = 0;
  int num = 1, sum = 0, money = 0;
  while (!que[0].empty()) {
    sum += que[0].top();
    money = sum + num * (num - 1);
    que[0].pop();
    dp[0][num++] = money;
  }
  for (int i = 1; i < 10; i++) {
    for (int j = 0; dp[i - 1][j] != -1; j++)
      dp[i][j] = dp[i - 1][j];
    num = 1, sum = 0, money = 0;
    while (!que[i].empty()) {
      sum += que[i].top();
      money = sum + num * (num - 1);
      que[i].pop();
      for (int j = 0; j + num <= k && dp[i - 1][j] != -1; j++)
        dp[i][j + num] = max(dp[i][j + num], dp[i - 1][j] + money);
      num++;
    }
  }
  printf("%d\\n", dp[9][k]);
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;

int N, K, c[2000], g[2000];
vector<int> d[10]; //?????£??????i?????????????????????
vector<int> s[10]; //?????£???????????¨????´???????
int dp[10][2001]; // dp[i][j] := i??????????????£???j????????£??????????????§

int main() {
  scanf("%d%d", &N, &K);
  for (int i = 0; i < N; i++) {
    scanf("%d%d", &c[i], &g[i]);
    d[g[i] - 1].push_back(c[i]);
  }
  for (int i = 0; i < 10; i++)
    sort(d[i].begin(), d[i].end(), greater<int>());
  for (int i = 0; i < 10; i++) {
    s[i].push_back(0);
    for (int j = 0; j < d[i].size(); j++)
      s[i].push_back(s[i][j] + d[i][j]);
  }
  for (int i = 0; i < 10; i++)
    for (int j = 0; j <= K; j++)
      for (int k = 0; k <= min(j, (int)s[i].size()); k++)
        dp[i][j] = max(dp[i][j], dp[i - 1][j - k] + k * (k - 1) + s[i][k]);
  printf("%d\\n", dp[9][K]);
}
Problem p00484 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
using namespace std;
int main(void) {
  int n, k, i, j, T;
  int c[2001], g[2001], sum;
  int jan[11][2001], cnt[11], x, kk, temp;
  int janaf[11][2001], dp[11][2001];
  scanf("%d %d", &n, &k);
  for (i = 0; i < n; i++)
    scanf("%d %d", &c[i], &g[i]);
  for (i = 1; i <= 10; i++)
    cnt[i] = 0;
  for (i = 0; i <= 10; i++) {
    for (j = 0; j < 2000; j++) {
      jan[i][j] = 0;
      janaf[i][j] = 0;
    }
  }
  for (i = 0; i < n; i++) {
    x = g[i];
    jan[x][cnt[x]] = c[i];
    cnt[x]++;
  }
  for (i = 1; i <= 10; i++)
    sort(jan[i], jan[i] + cnt[i], greater<int>());
  sum = 0;
  for (i = 1; i <= 10; i++) {
    for (j = 0; j < cnt[i]; j++) {
      sum += jan[i][j];
      janaf[i][j] = sum;
    }
    sum = 0;
  }
  for (i = 1; i <= 10; i++) {
    for (j = 0; j < cnt[i]; j++)
      janaf[i][j] += (j + 1) * j;
  }
  for (i = 0; i <= 10; i++) {
    for (j = 0; j <= k; j++)
      dp[i][j] = -1;
  }
  dp[0][0] = 0;
  for (i = 1; i <= 10; i++) {
    for (j = 0; j <= k; j++) {
      for (kk = 0; kk <= cnt[i]; kk++) {
        if (dp[i - 1][j] > dp[i][j])
          dp[i][j] = dp[i - 1][j];
        if (k >= j + (kk + 1)) {
          if (dp[i][j + (kk + 1)] < dp[i - 1][j] + janaf[i][kk]) {
            dp[i][j + (kk + 1)] = dp[i - 1][j] + janaf[i][kk];
          }
        }
      }
    }
  }
  printf("%d\\n", dp[10][k]);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
using namespace std;
int main(void) {
  int n, k, i, j, T;
  int c[2001], g[2001], sum;
  int jan[11][2001], cnt[11], x, kk, temp;
  int janaf[11][2001], dp[11][2001];
  scanf("%d %d", &n, &k);
  for (i = 0; i < n; i++)
    scanf("%d %d", &c[i], &g[i]);
  for (i = 1; i <= 10; i++)
    cnt[i] = 0;
  for (i = 0; i <= 10; i++) {
    for (j = 0; j < 2000; j++) {
      jan[i][j] = 0;
      janaf[i][j] = 0;
    }
  }
  for (i = 0; i < n; i++) {
    x = g[i];
    jan[x][cnt[x]] = c[i];
    cnt[x]++;
  }
  for (i = 1; i <= n; i++)
    sort(jan[i], jan[i] + cnt[i], greater<int>());
  sum = 0;
  for (i = 1; i <= n; i++) {
    for (j = 0; j < cnt[i]; j++) {
      sum += jan[i][j];
      janaf[i][j] = sum;
    }
    sum = 0;
  }
  for (i = 1; i <= n; i++) {
    for (j = 0; j < cnt[i]; j++)
      janaf[i][j] += (j + 1) * j;
  }
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= n; j++)
      dp[i][j] = -1;
  }
  dp[0][0] = 0;
  for (i = 1; i <= 10; i++) {
    for (j = 0; j <= k; j++) {
      for (kk = 0; kk <= cnt[i]; kk++) {
        if (dp[i - 1][j] > dp[i][j])
          dp[i][j] = dp[i - 1][j];
        if (k >= j + (kk + 1)) {
          if (dp[i][j + (kk + 1)] < dp[i - 1][j] + janaf[i][kk]) {
            dp[i][j + (kk + 1)] = dp[i - 1][j] + janaf[i][kk];
          }
        }
      }
    }
  }
  printf("%d\\n", dp[10][k]);
  return 0;
}
Problem p00484 (C++) — buggy submission, verdict: Runtime Error

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

int memo[12][2010];
int N, K, sum[12][2010];
vector<int> B[11];

int rec(int g, int k) {
  if (memo[g][k] != -1)
    return memo[g][k];
  if (g > 10)
    return 0;

  int res = 0;
  int s = B[g].size();
  for (int i = 0; i <= s && i + k <= K; ++i) {
    res = max(res, rec(g + 1, k + i) + sum[g][i]);
  }

  return memo[g][k] = res;
}

int main() {
  int c, g;
  memset(memo, -1, sizeof(memo));
  cin >> N >> K;
  for (int i = 0; i < N; ++i) {
    scanf("%d%d", &c, &g);
    B[g].emplace_back(c);
  }
  for (int i = 1; i <= 10; ++i) {
    sort(B[i].begin(), B[i].end(), greater<int>());
    for (int j = 1; j <= B[i].size(); ++j) {
      sum[i][j] = sum[i][j - 1] + B[i][j - 1] + (j - 1) * 2;
    }
  }

  cout << rec(1, 0) << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int memo[11][2001];
int N, K, sum[11][2010];
vector<int> B[11];

int rec(int g, int k) {
  if (memo[g][k] != -1)
    return memo[g][k];
  if (g > 10)
    return 0;

  int res = 0;
  int s = B[g].size();
  for (int i = 0; i <= s && i + k <= K; ++i) {
    res = max(res, rec(g + 1, k + i) + sum[g][i]);
  }

  return memo[g][k] = res;
}

int main() {
  int c, g;
  memset(memo, -1, sizeof(memo));
  cin >> N >> K;
  for (int i = 0; i < N; ++i) {
    scanf("%d%d", &c, &g);
    B[g].push_back(c);
  }
  for (int i = 1; i <= 10; ++i) {
    sort(B[i].begin(), B[i].end(), greater<int>());
    for (int j = 1; j <= B[i].size(); ++j) {
      sum[i][j] = sum[i][j - 1] + B[i][j - 1] + (j - 1) * 2;
    }
  }

  cout << rec(1, 0) << endl;
  return 0;
}
Problem p00484 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <functional>
#include <vector>

using namespace std;

int n, k;
int dp[11][2001];

int main() {
  vector<int> vcc[11];
  vector<int> vc[11];
  scanf("%d %d", &n, &k);
  int a, b;
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &a, &b);
    vcc[b].push_back(a);
  }
  for (int i = 1; i < 11; i++) {
    sort(vcc[i].begin(), vcc[i].end(), greater<int>());
    for (int j = 0; j <= vcc[i].size(); j++) {
      int aa = 0;
      for (int l = 0; l < j; l++) {
        aa += vcc[i][l] + j - 1;
      }
      vc[i].push_back(aa);
    }
  }

  fill(&dp[0][0], &dp[10][2000], -1);
  dp[0][0] = 0;
  for (int i = 1; i <= 10; i++) {
    for (int j = 0; j <= k; j++) {
      if (j == 0) {
        dp[i][j] = 0;
        continue;
      }
      int m = dp[i - 1][j];
      for (int l = 0; l < vc[i].size(); l++) {
        if (j - l < 0) {
          break;
        }
        if (dp[i - 1][j - l] == -1) {
          continue;
        }
        int a = dp[i - 1][j - l] + vc[i][l];
        if (a > m) {
          m = a;
        }
      }
      dp[i][j] = m;
    }
  }
  printf("%d\\n", dp[10][k]);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <vector>

using namespace std;

int n, k;
int dp[11][2001];

int main() {
  vector<int> vcc[11];
  vector<int> vc[11];
  scanf("%d %d", &n, &k);
  int a, b;
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &a, &b);
    vcc[b].push_back(a);
  }
  for (int i = 1; i < 11; i++) {
    sort(vcc[i].begin(), vcc[i].end(), greater<int>());
    for (int j = 0; j <= vcc[i].size(); j++) {
      int aa = 0;
      for (int l = 0; l < j; l++) {
        aa += vcc[i][l] + j - 1;
      }
      vc[i].push_back(aa);
    }
  }

  fill(&dp[0][0], &dp[10][2000], -1);
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= k; j++) {
      if (j == 0) {
        dp[i][j] = 0;
        continue;
      }
      int m = dp[i - 1][j];
      for (int l = 0; l < vc[i].size(); l++) {
        if (j - l < 0) {
          break;
        }
        if (dp[i - 1][j - l] == -1) {
          continue;
        }
        int a = dp[i - 1][j - l] + vc[i][l];
        if (a > m) {
          m = a;
        }
      }
      dp[i][j] = m;
    }
  }
  printf("%d\\n", dp[n][k]);
}
Problem p00485 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

const int inf = (int)1e9;

int main() {
  int n, m, k;
  int ans = 0;
  vector<int> dist;
  vector<vector<pair<int, int>>> graph;
  vector<pair<pair<int, int>, int>> edge;

  scanf("%d%d%d", &n, &m, &k);
  dist.resize(n, inf);
  graph.resize(n);
  for (int i = 0; i < m; ++i) {
    int a, b, l;
    scanf("%d%d%d", &a, &b, &l);
    a -= 1, b -= 1;
    edge.push_back(make_pair(make_pair(a, b), l));
    graph[a].push_back(make_pair(b, l));
    graph[b].push_back(make_pair(a, l));
  }

  for (int i = 0; i < k; ++i) {

    int s;
    scanf("%d", &s);
    s -= 1;

    priority_queue<pair<int, int>> q;

    q.push(make_pair(0, s));

    while (!q.empty()) {

      int v = q.top().second;
      int c = -q.top().first;
      q.pop();

      if (dist[v] < c)
        continue;
      dist[v] = c;

      for (int i = 0; i < graph[v].size(); ++i) {
        int w = graph[v][i].first;
        int d = graph[v][i].second + c;
        q.push(make_pair(-d, w));
      }
    }
  }

  // for(int i = 0; i < n; ++i)
  //	printf("[debug] %d -> %d\\n", i, dist[i]);

  for (int i = 0; i < m; ++i) {

    int a = edge[i].first.first;
    int b = edge[i].first.second;
    int l = edge[i].second;
    int cost;

    if (dist[a] > dist[b])
      swap(a, b);

    if (dist[a] + l == dist[b])
      cost = dist[b];
    else
      cost = (l - (dist[b] - dist[a]) + 1) / 2 + dist[b];
    ans = max(ans, cost);
    // printf("[debug] (%d, %d) -> (%d, %d) -> %d -> %d\\n", a, dist[a], b,
    // dist[b], l, cost);
  }

  printf("%d\\n", ans);

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

int main() {
  const int inf = (int)1e9;
  int n, m, k;
  int ans = inf;
  vector<int> dist;
  vector<vector<pair<int, int>>> graph;
  vector<pair<pair<int, int>, int>> edge;
  scanf("%d%d%d", &n, &m, &k);
  dist.resize(n, inf);
  graph.resize(n);
  for (int i = 0; i < m; ++i) {
    int a, b, l;
    scanf("%d%d%d", &a, &b, &l);
    a -= 1, b -= 1;
    edge.push_back(make_pair(make_pair(a, b), l));
    graph[a].push_back(make_pair(b, l));
    graph[b].push_back(make_pair(a, l));
  }
  for (int i = 0; i < k; ++i) {
    int s;
    scanf("%d", &s);
    s -= 1;
    priority_queue<pair<int, int>> q;
    q.push(make_pair(0, s));
    while (!q.empty()) {
      int v = q.top().second;
      int c = -q.top().first;
      q.pop();
      if (dist[v] < c)
        continue;
      dist[v] = c;
      for (int i = 0; i < graph[v].size(); ++i) {
        int w = graph[v][i].second;
        int d = graph[v][i].second + c;
        q.push(make_pair(-d, w));
      }
    }
  }
  for (int i = 0; i < m; ++i) {
    int a = edge[i].first.first;
    int b = edge[i].first.second;
    int l = edge[i].second;
    if (dist[a] > dist[b])
      swap(a, b);
    int cost;
    if (dist[a] + l <= dist[b])
      cost = dist[b];
    else
      cost = (l - (dist[b] - dist[a]) + 1) / 2;
    ans = max(ans, cost);
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p00485 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int road[1000001][3];
int main() {
  int N, M, K;
  vector<pair<int, int>> nGo[3001];
  deque<int> dis;
  int dist[3001];
  int signer[3001] = {};
  for (int i = 1; i <= 3000; i++)
    dist[i] = (1 << 30);
  cin >> N >> M >> K;
  int a, b, c;
  for (int i = 0; i < M; i++) {
    cin >> a >> b >> c;
    nGo[a].push_back(pair<int, int>(b, c));
    nGo[b].push_back(pair<int, int>(a, c));
    road[i][0] = a;
    road[i][1] = b;
    road[i][2] = c;
  }
  for (int i = 0; i < K; i++) {
    cin >> a;
    //	dis.insert(multimap<int,int>::value_type(0,a));
    dis.push_back(a);
    dist[a] = 0;
  }
  while (!dis.empty()) {
    int miner = 0;
    for (int i = 0; i < dis.size(); i++)
      miner = (dist[dis[miner]] < dist[dis[i]]) ? miner : i;
    int r = dis[miner];
    dis[miner] = dis[0];
    dis[0] = r;
    int now = dis[0];
    for (int i = 0; i < nGo[now].size(); i++) {
      if (dist[nGo[now][i].first] > dist[now] + nGo[now][i].second) {
        if (signer[nGo[now][i].first] == 0) {
          dis.push_back(nGo[now][i].first);
          signer[nGo[now][i].first] = 1;
        }
        dist[nGo[now][i].first] = dist[now] + nGo[now][i].second;
      }
    }
    dis.pop_front();
  }
  int maxer = 0;
  for (int i = 0; i < M; i++)
    maxer =
        max(maxer, (dist[road[i][0]] + dist[road[i][1]] + road[i][2] + 1) / 2);
  for (int i = 1; i <= N; i++)
    maxer = max(maxer, dist[i]);
  cout << maxer << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int main() {
  int N, M, K;
  vector<pair<int, int>> nGo[3001];
  deque<int> dis;
  int dist[3001];
  int road[100001][3];
  for (int i = 1; i <= 3000; i++)
    dist[i] = (1 << 30);
  cin >> N >> M >> K;
  int a, b, c;
  for (int i = 0; i < M; i++) {
    cin >> a >> b >> c;
    nGo[a].push_back(pair<int, int>(b, c));
    nGo[b].push_back(pair<int, int>(a, c));
    road[i][0] = a;
    road[i][1] = b;
    road[i][2] = c;
  }
  for (int i = 0; i < K; i++) {
    cin >> a;
    //	dis.insert(multimap<int,int>::value_type(0,a));
    dis.push_back(a);
    dist[a] = 0;
  }
  while (!dis.empty()) {
    sort(dis.begin(), dis.end());
    int now = dis[0];
    for (int i = 0; i < nGo[now].size(); i++) {
      if (dist[nGo[now][i].first] > dist[now] + nGo[now][i].second) {
        dis.push_back(nGo[now][i].first);
        dist[nGo[now][i].first] = dist[now] + nGo[now][i].second;
      }
    }
    while (now == dis[0])
      dis.pop_front();
  }
  int maxer = 0;
  for (int i = 0; i < M; i++)
    maxer =
        max(maxer, (dist[road[i][0]] + dist[road[i][1]] + road[i][2] + 1) / 2);
  for (int i = 1; i <= N; i++)
    maxer = max(maxer, dist[i]);
  cout << maxer << endl;
  return 0;
}