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

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

ll Stoi(string s) {
  ll res = 0;
  for (ll i = 0; i < (int)s.size(); i++)
    res = res * 10 + s[i] - '0';
  return res;
}

ll Pow(ll a, ll b) {
  ll res = 1;
  while (b--)
    res *= a;
  return res;
}

string n;
ll dp[20][2][10][10][10];
ll dfs(ll id, ll f, ll a, ll b, ll c) {
  if (dp[id][f][a][b][c] != -1)
    return dp[id][f][a][b][c];
  if (id == (int)n.size())
    return 0;
  ll r = 9, res = 0;
  if (!f)
    r = n[id] - '0';
  for (ll d = 0; d <= r; d++) {
    ll nf = f;
    if (d < r)
      nf = 1;
    if (a == 5 && b == 1 && d == 3)
      if (nf)
        res += Pow(10, n.size() - id - 1);
      else
        res += Stoi(n.substr(id + 1)) + 1;
    else
      res += dfs(id + 1, nf, b, c, d);
  }
  return dp[id][f][a][b][c] = res;
}

int main() {
  cin >> n;
  memset(dp, -1, sizeof(dp));
  cout << dfs(0, 0, 0, 0, 0) << endl;
  return 0;
}


Error: 0

Fixed submission:

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

ll Stoi(string s) {
  ll res = 0;
  for (ll i = 0; i < (int)s.size(); i++)
    res = res * 10 + s[i] - '0';
  return res;
}

string n;
ll dp[20][2][10][10][10];
ll dfs(ll id, ll f, ll a, ll b, ll c) {
  if (dp[id][f][a][b][c] != -1)
    return dp[id][f][a][b][c];
  if (id == (int)n.size())
    return 0;
  ll r = 9, res = 0;
  if (!f)
    r = n[id] - '0';
  for (ll d = 0; d <= r; d++) {
    ll nf = f;
    if (d < r)
      nf = 1;
    if (a == 5 && b == 1 && d == 3)
      if (nf)
        res += pow(10, n.size() - id - 1);
      else
        res += stoi(n.substr(id + 1)) + 1;
    else
      res += dfs(id + 1, nf, b, c, d);
  }
  return dp[id][f][a][b][c] = res;
}

int main() {
  cin >> n;
  memset(dp, -1, sizeof(dp));
  cout << dfs(0, 0, 0, 0, 0) << endl;
  return 0;
}
Problem p01985 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int SIZE = 100010;

vector<int> way[SIZE];
bool visited[SIZE];
int id[SIZE];
int counter[2];

bool dfs(int now, int f) {
  if (visited[now]) {
    return id[now] == f;
  }

  visited[now] = true;
  id[now] = f;
  counter[f]++;

  for (int i = 0; i < (int)way[now].size(); i++)
    if (dfs(way[now][i], !f) == false)
      return false;

  return true;
}

bool solve() {
  int n, m;
  int a, b;

  scanf("%d%d", &n, &m);

  if (n == 0)
    return false;

  for (int i = 0; i < n; i++)
    way[i] = vector<int>();
  memset(visited, 0, sizeof(visited));
  counter[0] = counter[1] = 0;

  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    a--;
    b--;

    way[a].push_back(b);
    way[b].push_back(a);
  }

  if (!dfs(0, 0)) {
    puts("0");
  } else {
    // cerr << counter[0] << " " << counter[1] << endl;
    if (counter[0] > counter[1])
      swap(counter[0], counter[1]);

    vector<int> vec;

    if (counter[0] % 2 == 0) {
      vec.push_back(counter[0] / 2);
    }

    if (counter[0] != counter[1] && counter[1] % 2 == 0) {
      vec.push_back(counter[1] / 2);
    }

    printf("%d\\n", (int)vec.size());

    for (int i = 0; i < vec.size(); i++) {
      printf("%d\\n", vec[i]);
    }
  }

  return true;
}

int main() {
  while (solve())
    ;
}


Error: 0
4 2
1 1
2 2
3 2


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int SIZE = 100010;

vector<int> way[SIZE];
bool visited[SIZE];
int id[SIZE];
int counter[2];

bool dfs(int now, int f) {
  if (visited[now]) {
    return id[now] == f;
  }

  visited[now] = true;
  id[now] = f;
  counter[f]++;

  for (int i = 0; i < (int)way[now].size(); i++)
    if (dfs(way[now][i], !f) == false)
      return false;

  return true;
}

bool solve() {
  int n, m;
  int a, b;

  scanf("%d%d", &n, &m);

  if (n == 0)
    return false;

  for (int i = 0; i < n; i++)
    way[i] = vector<int>();
  memset(visited, 0, sizeof(visited));
  counter[0] = counter[1] = 0;

  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    a--;
    b--;

    way[a].push_back(b);
    way[b].push_back(a);
  }

  if (!dfs(0, 0)) {
    puts("0");
  } else {
    cerr << counter[0] << " " << counter[1] << endl;
    if (counter[0] > counter[1])
      swap(counter[0], counter[1]);

    vector<int> vec;

    if (counter[0] % 2 == 0) {
      vec.push_back(counter[0] / 2);
    }

    if (counter[0] != counter[1] && counter[1] % 2 == 0) {
      vec.push_back(counter[1] / 2);
    }

    printf("%d\\n", (int)vec.size());

    for (int i = 0; i < vec.size(); i++) {
      printf("%d\\n", vec[i]);
    }
  }

  return true;
}

int main() {
  while (solve())
    ;
}
Problem p02022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
using namespace std;
int a[114514];
int b[114514];
#define rep(i, n) for (long long i = 0; i < n; i++)
int main() {
  long long n, m;
  cin >> n >> m;
  long long iwa = 0;
  long long ta = 0;
  rep(i, n) {
    cin >> a[i];
    iwa += a[i];
  }
  rep(i, m) {
    cin >> b[i];
    ta += b[i];
  }
  cout << iwa * ta << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
using namespace std;
int a[114514];
int b[114514];
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  int n, m;
  cin >> n >> m;
  int iwa = 0;
  rep(i, n) { cin >> a[i]; }
  rep(i, m) { cin >> b[i]; }
  rep(i, n) {
    rep(j, m) { iwa += a[i] * b[j]; }
  }
  cout << iwa << endl;
}
Problem p02022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main(void) {
  long int N, M, A[100001] = {}, B[100001] = {}, sum_A = 0, sum_B = 0;
  cin >> N >> M;

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    sum_A += A[i];
  }

  for (int j = 1; j <= M; j++) {
    cin >> B[j];
    sum_B += B[j];
  }
  cout << sum_A * sum_B << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main(void) {
  int N, M, A[100001] = {}, B[100001] = {};
  long int Deli = 0;
  cin >> N >> M;

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }

  for (int j = 1; j <= M; j++) {
    cin >> B[j];
    for (int i = 1; i <= N; i++) {
      Deli += A[i] * B[j];
    }
  }
  cout << Deli << endl;
  return 0;
}
Problem p02022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  long long a[100000], b[100000];
  long long sum = 0, s = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  for (int k = 0; k < m; k++) {
    cin >> b[k];
    s += b[k];
  }
  cout << sum * s << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  long long a[100000], b[100000];
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int k = 0; k < m; k++) {
    cin >> b[k];
  }
  for (int l = 0; l < n; l++) {
    for (int j = 0; j < m; j++) {
      sum += a[l] * b[j];
    }
  }
  cout << sum << endl;
}
Problem p02022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  long long int n, m;
  cin >> n >> m;
  vector<long long int> a(n);
  vector<long long int> b(m);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  long long int sum = 0;
  for (int i = 0; i < m; i++) {
    cin >> b.at(i);
    sum += b.at(i);
  }
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += a.at(i) * sum;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  long long int n, m;
  cin >> n >> m;
  vector<long long int> a(n);
  vector<long long int> b(m);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  for (int i = 0; i < m; i++) {
    cin >> b.at(i);
  }
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      ans += a.at(i) * b.at(j);
    }
  }
  cout << ans << endl;
}
Problem p02022 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, m;
  cin >> n >> m;

  long long a[100010];
  long long ans = 0;
  long long tmp = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  long long b;
  for (int i = 0; i < m; i++) {
    cin >> b;
    tmp += b;
  }

  for (int i = 0; i < n; i++) {
    ans += a[i] * tmp;
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int n, m;
  cin >> n >> m;

  long long a[1010];
  long long ans = 0;
  long long tmp = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  long long b;
  for (int i = 0; i < m; i++) {
    cin >> b;
    tmp += b;
  }

  for (int i = 0; i < n; i++) {
    ans += a[i] * tmp;
  }

  cout << ans << endl;

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define int long long
using namespace std;
int a[191919], b[191919];
signed main() {
  int n, m, cnt = 0, cnta = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int j = 0; j < m; j++)
    cin >> b[j];
  for (int i = 0; i < n; i++) {
    cnt += a[i];
  }
  for (int j = 0; j < m; j++) {
    cnta += b[j];
  }
  cout << cnt * cnta << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define int long long
using namespace std;
int a[1919], b[1919];
signed main() {
  int n, m, cnt = 0, cnta = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int j = 0; j < m; j++)
    cin >> b[j];
  for (int i = 0; i < n; i++) {
    cnt += a[i];
  }
  for (int j = 0; j < m; j++) {
    cnta += b[j];
  }
  cout << cnt * cnta << endl;
}
Problem p02022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6;
const ll LINF = 1e18;
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (int i = (a); i < (b); i++)
#define rrrep(i, a, b) for (int i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl

ll n, cnt, ans, a[ohara], b[ohara], c, d, tmp, tmpp, m, h, w, x, y, sum, pos, k;
ld doua;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
// int dy[]={-1,0,1,-1,1,-1,0,1};
// int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"), s;
bool fl;
struct edge {
  int to, cost;
};

//-------------------------↓↓↓↓↓↓------------------------

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

  cin >> n >> m;
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i], sum += b[i];
  rep(i, n) { ans += a[i] * sum; }
  Cout(ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6;
const ll LINF = 1e18;
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (int i = (a); i < (b); i++)
#define rrrep(i, a, b) for (int i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl

ll n, cnt, ans, a[ohara], b[ohara], c, d, tmp, tmpp, m, h, w, x, y, sum, pos, k;
ld doua;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
// int dy[]={-1,0,1,-1,1,-1,0,1};
// int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"), s;
bool fl;
struct edge {
  int to, cost;
};

//-------------------------↓↓↓↓↓↓------------------------

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

  cin >> n >> m;
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  rep(i, n) {
    rep(j, m) { ans += a[i] * b[j]; }
  }
  Cout(ans);
  return 0;
}
Problem p02023 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = a; i < n; i++)
#define RREP(i, a, n) for (ll i = n - 1; i >= a; i--)
typedef long long ll;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
using namespace std;

ll n;
ll ans;
map<ll, ll> mp;
int main() {
  cin >> n;
  REP(i, 0, n) {
    ll aa, bb;
    cin >> aa >> bb;

    mp[aa]++;
    mp[bb + 1]--;
  }

  ll cnt = 0;
  for (auto itr : mp) {
    cnt += itr.second;
    ans = max(ans, cnt);
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = a; i < n; i++)
#define RREP(i, a, n) for (ll i = n - 1; i >= a; i--)
typedef long long ll;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
using namespace std;

ll n;
ll ans;
ll imos[101010];
int main() {
  cin >> n;
  REP(i, 0, n) {
    ll aa, bb;
    cin >> aa >> bb;
    imos[aa]++;
    imos[bb + 1]--;
  }
  REP(i, 0, 100010) imos[i + 1] += imos[i];

  REP(i, 0, 100010) ans = max(ans, imos[i]);

  cout << ans << endl;

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

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#define int long long
short map[600][600];
signed main() {
  int h, w;
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> map[i][j];
    }
  }
  int ans = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      ans += map[i][j] * ((i + 1) * (j + 1) * (h - i) * (w - j));
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#define int long long
short map[600][600];
signed main() {
  int h, w;
  cin >> h >> w;
  for (int i = 0; i < h; i++)
    map[i][0] = 0;
  for (int j = 0; j < w; j++)
    map[0][j] = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> map[i][j];
      map[i][j] += map[i - 1][j] + map[i][j - 1] - map[i - 1][j - 1];
    }
  }
  int ans = 0;
  for (int i = 0; i < h; i++) {
    for (int i_ = i + 1; i_ <= h; i_++) {
      for (int j = 0; j < w; j++) {
        for (int j_ = j + 1; j_ <= w; j_++) {
          ans += map[i_][j_] - map[i][j_] - map[i_][j] + map[i][j];
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p02026 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
#define int long long
using namespace std;
typedef pair<int, int> P;
#define fi first
#define se second

int dp[100009], n, a[100009], b[100009];
vector<P> v1, v2;
vector<P> v[100009];

signed main() {
  r(i, 100009) dp[i] = 1e18;
  cin >> n;
  r(i, n) cin >> a[i];
  r(i, n) cin >> b[i];
  r(i, n) {
    v1.push_back(P(b[i], i));
    v2.push_back(P(a[i], i));
  }
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  r(i, n - 1) {
    for (int j = i + 1, k = 0; j < n && k < 20; k++, j++) {
      int x = v1[i].se;
      int y = v1[j].se;
      int z = abs(v1[i].fi - v1[j].fi);
      v[x].push_back(P(y, z));
      v[y].push_back(P(x, z));
    }
  }
  r(i, n - 1) {
    for (int j = i + 1, k = 0; j < n && k < 20; k++, j++) {
      int x = v2[i].se;
      int y = v2[j].se;
      int z = abs(v2[i].fi - v2[j].fi);
      v[x].push_back(P(y, z));
      v[y].push_back(P(x, z));
    }
  }
  priority_queue<P, vector<P>, greater<P>> q;
  r(i, n) {
    if (!i) {
      q.push(P(0, i));
      dp[i] = 0;
    }
  }
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int now = p.second;
    int cost = p.first;
    if (dp[now] < cost)
      continue;
    r(i, v[now].size()) {
      int nex = v[now][i].fi;
      int nc = v[now][i].se + cost;
      if (dp[nex] <= nc)
        continue;
      dp[nex] = nc;
      q.push(P(nc, nex));
    }
  }
  r(i, n) cout << dp[i] << endl;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
#define int long long
using namespace std;
typedef pair<int, int> P;
#define fi first
#define se second

int dp[100009], n, a[100009], b[100009];
vector<P> v1, v2;
vector<P> v[100009];

signed main() {
  r(i, 100009) dp[i] = 1e18;
  cin >> n;
  r(i, n) cin >> a[i];
  r(i, n) cin >> b[i];
  r(i, n) {
    v1.push_back(P(b[i], i));
    v2.push_back(P(a[i], i));
  }
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  r(i, n - 1) {
    for (int j = i + 1, k = 0; j < n && k < 50; k++, j++) {
      int x = v1[i].se;
      int y = v1[j].se;
      int z = abs(v1[i].fi - v1[j].fi);
      v[x].push_back(P(y, z));
      v[y].push_back(P(x, z));
    }
  }
  r(i, n - 1) {
    for (int j = i + 1, k = 0; j < n && k < 50; k++, j++) {
      int x = v2[i].se;
      int y = v2[j].se;
      int z = abs(v2[i].fi - v2[j].fi);
      v[x].push_back(P(y, z));
      v[y].push_back(P(x, z));
    }
  }
  priority_queue<P, vector<P>, greater<P>> q;
  r(i, n) {
    if (a[i] == 1 || b[i] == 1) {
      q.push(P(0, i));
      dp[i] = 0;
    }
  }
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int now = p.second;
    int cost = p.first;
    if (dp[now] < cost)
      continue;
    r(i, v[now].size()) {
      int nex = v[now][i].fi;
      int nc = v[now][i].se + cost;
      if (dp[nex] <= nc)
        continue;
      dp[nex] = nc;
      q.push(P(nc, nex));
    }
  }
  r(i, n) cout << dp[i] << endl;
}
Problem p02026 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;

int a[200000], b[200000];
ll d[200000];
vector<P> E[200000];

int main() {
  int n;
  cin >> n;
  vector<P> A, B;
  rep(i, n) scanf("%d", &a[i]), A.push_back(P(a[i], i));
  rep(i, n) scanf("%d", &b[i]), B.push_back(P(b[i], i));
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  rep(i, n - 1) {
    E[A[i + 1].second].push_back(P(A[i + 1].first - A[i].first, A[i].second));
    E[A[i].second].push_back(P(A[i + 1].first - A[i].first, A[i + 1].second));
    E[B[i + 1].second].push_back(P(B[i + 1].first - B[i].first, B[i].second));
    E[B[i].second].push_back(P(B[i + 1].first - B[i].first, B[i + 1].second));
  }
  memset(d, 0x3f, sizeof(d));
  priority_queue<P, vector<P>, greater<P>> que;
  d[0] = 0;
  que.push(P(0, 0));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (d[p.second] != p.first)
      continue;
    for (auto u : E[p.second]) {
      if (p.first + u.first < d[u.second]) {
        d[u.second] = p.first + u.first;
        que.push(P(d[u.second], u.second));
      }
    }
  }
  rep(i, n) { printf("%lld\\n", d[i]); }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;

int a[200000], b[200000];
ll d[200000];
vector<P> E[200000];

int main() {
  int n;
  cin >> n;
  vector<P> A, B;
  rep(i, n) scanf("%d", &a[i]), A.push_back(P(a[i], i));
  rep(i, n) scanf("%d", &b[i]), B.push_back(P(b[i], i));
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  rep(i, n - 1) {
    E[A[i + 1].second].push_back(P(A[i + 1].first - A[i].first, A[i].second));
    E[A[i].second].push_back(P(A[i + 1].first - A[i].first, A[i + 1].second));
    E[B[i + 1].second].push_back(P(B[i + 1].first - B[i].first, B[i].second));
    E[B[i].second].push_back(P(B[i + 1].first - B[i].first, B[i + 1].second));
  }
  memset(d, 0x3f, sizeof(d));
  priority_queue<P> que;
  d[0] = 0;
  que.push(P(0, 0));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (d[p.second] != p.first)
      continue;
    for (auto u : E[p.second]) {
      if (p.first + u.first < d[u.second]) {
        d[u.second] = p.first + u.first;
        que.push(P(d[u.second], u.second));
      }
    }
  }
  rep(i, n) { printf("%lld\\n", d[i]); }
}
Problem p02030 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

int main(void) {
  int n, m;
  scanf("%d %d", &n, &m);
  int a[n];
  int b[m];
  vector<int> dup, all;

  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  for (int j = 0; j < m; j++) {
    scanf("%d", &b[j]);
  }

  int ap = 0;
  int bp = 0;
  int i = 1;
  while (ap != n or bp != m) {

    if (a[ap] == i and b[bp] == i)
      dup.push_back(i);
    if (a[ap] == i or b[bp] == i)
      all.push_back(i);

    while (a[ap] <= i and ap != n)
      ap++;
    while (b[bp] <= i and bp != m)
      bp++;

    if (ap != n and a[ap] < b[bp]) {
      i = a[ap];
    } else if (bp != m and a[ap] >= b[bp]) {
      i = b[bp];
    } else if (ap == n) {
      i = b[bp];
    } else {
      i = a[ap];
    }
  }

  printf("%d %d\\n", dup.size(), all.size());

  for (auto itr = dup.begin(); itr != dup.end(); ++itr) {
    printf("%d\\n", *itr);
  }
  for (auto itr = all.begin(); itr != all.end(); ++itr) {
    printf("%d\\n", *itr);
  }

  return 0;
}


Error: MLE

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

int main(void) {
  int n, m;
  scanf("%d %d", &n, &m);
  int a[n];
  int b[m];
  vector<int> dup, all;

  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  for (int j = 0; j < m; j++) {
    scanf("%d", &b[j]);
  }

  int ap = 0;
  int bp = 0;
  int i = 1;
  while (ap != n or bp != m) {

    if (a[ap] == i and b[bp] == i)
      dup.push_back(i);
    if (a[ap] == i or b[bp] == i)
      all.push_back(i);

    while (a[ap] <= i and ap != n)
      ap++;
    while (b[bp] <= i and bp != m)
      bp++;

    if (ap != n and a[ap] < b[bp]) {
      i = a[ap];
    } else if (bp != n and a[ap] >= b[bp]) {
      i = b[bp];
    } else if (ap == n) {
      i = b[bp];
    } else {
      i = a[ap];
    }
  }

  printf("%d %d\\n", dup.size(), all.size());

  for (auto itr = dup.begin(); itr != dup.end(); ++itr) {
    printf("%d\\n", *itr);
  }
  for (auto itr = all.begin(); itr != all.end(); ++itr) {
    printf("%d\\n", *itr);
  }

  return 0;
}