Problem p02877 (C++)

Buggy submission (status: Time Limit Exceeded):
// Zory-2019
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pr pair<int, int>
#define FR first
#define SE second
#define MP make_pair
#define PB push_back
#define vc vector
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define bin(x) (1ll << (x))
#define fo(i, l, r) for (int i = (l), I = (r); i <= I; i++)
#define fd(i, r, l) for (int i = (r), I = (l); i >= I; i--)
#ifdef DEBUG
#define GG(x) assert(x)
#else
#define GG(x) (x)
#endif
namespace mine {
ll qread() {
  ll ans = 0, f = 1;
  char c = getchar();
  while (c < '0' or c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while ('0' <= c and c <= '9')
    ans = ans * 10 + c - '0', c = getchar();
  return ans * f;
}
void write(ll num) {
  if (num < 0)
    putchar('-'), num = -num;
  if (num >= 10)
    write(num / 10);
  putchar('0' + num % 10);
}
void write1(ll num) {
  write(num);
  putchar(' ');
}
void write2(ll num) {
  write(num);
  putchar('\n');
}
template <typename T> void chmax(T &x, const T y) { x = (x > y ? x : y); }
template <typename T> void chmin(T &x, const T y) { x = (x < y ? x : y); }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int mm(const int x) { return x >= MOD ? x - MOD : x; }
template <typename T> void add(T &x, const int &y) {
  x = (x + y >= MOD ? x + y - MOD : x + y);
}
ll qpower(ll x, ll e, int mod = MOD) {
  ll ans = 1;
  GG(e >= 0);
  while (e) {
    if (e & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    e >>= 1;
  }
  return ans;
}
ll invm(ll x) { return qpower(x, MOD - 2); }
const int N = 2e5 + 10;

int A[N];
map<int, int> f[N];
void upd(int x, int pos, int val) { // 差分
  if (pos < 0 or pos > A[x])
    return;
  if (f[x].count(pos))
    chmin(f[x][pos], val);
  else
    f[x][pos] = val;
}
void main() {
  int n = qread();
  fo(i, 1, n) A[i] = qread();
  f[0][0] = 0;
  fo(i, 0, n) {
    int lst = f[i].begin()->SE;
    for (map<int, int>::iterator IT = ++f[i].begin(); IT != f[i].end();) {
      chmin(IT->SE, lst);
      if (lst == IT->SE)
        f[i].erase(IT++);
      else
        IT++;
    }
    for (auto now : f[i]) {
      int xi = now.FR, val = now.SE;
      if (!xi)
        upd(i + 1, 0, val + (A[i + 1] - A[i] > 0)),
            upd(i + 1, A[i + 1] - A[i], val);
      else if (A[i + 1] == A[i])
        upd(i + 1, 0, val + 2), upd(i + 1, xi, val);
      else if (A[i + 1] - A[i] + xi > 0)
        upd(i + 1, 0, val + 2),
            upd(i + 1, min(xi, A[i + 1] - A[i] + xi), val + 1),
            upd(i + 1, max(xi, A[i + 1] - A[i] + xi), val);
      else
        upd(i + 1, 0, val + 1), upd(i + 1, xi, val);
    }
  }
  write(f[n + 1][0]);
}
}; // namespace mine
signed main() {
  srand(time(0));
  mine::main();
}


Fixed submission:
// Zory-2019
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pr pair<int, int>
#define FR first
#define SE second
#define MP make_pair
#define PB push_back
#define vc vector
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define bin(x) (1ll << (x))
#define fo(i, l, r) for (int i = (l), I = (r); i <= I; i++)
#define fd(i, r, l) for (int i = (r), I = (l); i >= I; i--)
#ifdef DEBUG
#define GG(x) assert(x)
#else
#define GG(x) (x)
#endif
namespace mine {
ll qread() {
  ll ans = 0, f = 1;
  char c = getchar();
  while (c < '0' or c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while ('0' <= c and c <= '9')
    ans = ans * 10 + c - '0', c = getchar();
  return ans * f;
}
void write(ll num) {
  if (num < 0)
    putchar('-'), num = -num;
  if (num >= 10)
    write(num / 10);
  putchar('0' + num % 10);
}
void write1(ll num) {
  write(num);
  putchar(' ');
}
void write2(ll num) {
  write(num);
  putchar('\n');
}
template <typename T> void chmax(T &x, const T y) { x = (x > y ? x : y); }
template <typename T> void chmin(T &x, const T y) { x = (x < y ? x : y); }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int mm(const int x) { return x >= MOD ? x - MOD : x; }
template <typename T> void add(T &x, const int &y) {
  x = (x + y >= MOD ? x + y - MOD : x + y);
}
ll qpower(ll x, ll e, int mod = MOD) {
  ll ans = 1;
  GG(e >= 0);
  while (e) {
    if (e & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    e >>= 1;
  }
  return ans;
}
ll invm(ll x) { return qpower(x, MOD - 2); }
const int N = 2e5 + 10;

int A[N];
map<int, int> f[N];
void upd(int x, int pos, int val) { // 差分
  if (pos < 0 or pos > A[x])
    return;
  if (f[x].count(pos))
    chmin(f[x][pos], val);
  else
    f[x][pos] = val;
}
void main() {
  int n = qread();
  fo(i, 1, n) A[i] = qread();
  f[0][0] = 0;
  fo(i, 0, n) {
    int lst = f[i].begin()->SE;
    for (map<int, int>::iterator IT = ++f[i].begin(); IT != f[i].end();) {
      chmin(IT->SE, lst);
      if (lst == IT->SE)
        f[i].erase(IT++);
      else
        lst = IT->SE, IT++;
    }
    for (auto now : f[i]) {
      int xi = now.FR, val = now.SE;
      if (!xi)
        upd(i + 1, 0, val + (A[i + 1] - A[i] > 0)),
            upd(i + 1, A[i + 1] - A[i], val);
      else if (A[i + 1] == A[i])
        upd(i + 1, 0, val + 2), upd(i + 1, xi, val);
      else if (A[i + 1] - A[i] + xi > 0)
        upd(i + 1, 0, val + 2),
            upd(i + 1, min(xi, A[i + 1] - A[i] + xi), val + 1),
            upd(i + 1, max(xi, A[i + 1] - A[i] + xi), val);
      else
        upd(i + 1, 0, val + 1), upd(i + 1, xi, val);
    }
  }
  write(f[n + 1][0]);
}
}; // namespace mine
signed main() {
  srand(time(0));
  mine::main();
}