Problem p03801 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 1e18
#define INF 1e9

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()

using namespace std;

using ll = long long;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;

template <typename T> void chmax(T &a, T &b) { a = max(a, b); }

template <typename T> void chmin(T &a, T &b) { a = min(a, b); }

class Union_find {
private:
  vector<int> par;
  vector<int> rank;
  int n;

public:
  Union_find(int a) {
    n = a;
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};
PLL a[114514];
ll res[114514] = {};

bool comp(PLL l, PLL r) {
  return l.first == r.first ? l.second > r.second : l.first > r.first;
}

int main(void) {
  int N;
  cin >> N;
  REP(i, N) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(a, a + N, comp);
  a[N] = {0, -1};
  int in = 0;
  int add = a[0].second;
  REP(i, N) {
    in = i;
    while (a[i].first == a[in].first && in < N) {
      if (add > a[in].second)
        add = a[in].second;
      in++;
    }
    in--;
    res[add] += (a[in].first - a[in + 1].first) * (in + 1);
    a[in].first = a[in + 1].first;
    sort(a, a + N, comp);
    i = in;
  }
  REP(i, N) { cout << res[i] << endl; }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 1e18
#define INF 1e9

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()

using namespace std;

using ll = long long;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;

template <typename T> void chmax(T &a, T &b) { a = max(a, b); }

template <typename T> void chmin(T &a, T &b) { a = min(a, b); }

class Union_find {
private:
  vector<int> par;
  vector<int> rank;
  int n;

public:
  Union_find(int a) {
    n = a;
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};
PLL a[114514];
ll res[114514] = {};

bool comp(PLL l, PLL r) {
  return l.first == r.first ? l.second > r.second : l.first > r.first;
}

int main(void) {
  int N;
  cin >> N;
  REP(i, N) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(a, a + N, comp);
  a[N] = {0, -1};
  int in = 0;
  int add = a[0].second;
  REP(i, N) {
    in = i;
    while (a[i].first == a[in].first && in < N) {
      if (add > a[in].second)
        add = a[in].second;
      in++;
    }
    in--;
    res[add] += (a[in].first - a[in + 1].first) * (in + 1);
    a[in].first = a[in + 1].first;
    i = in;
  }
  REP(i, N) { cout << res[i] << endl; }
}