Problem p00952 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
using namespace std;
map<int, char> e;
void read(int &x) {
  x = 0;
  int y = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      y = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x = x * y;
}
int main() {
  int x, i, n, now = 1, cnt = 0;
  read(n);
  for (i = 1; i <= n; ++i) {
    read(x);
    if (x < now) {
      printf("No\\n");
      continue;
    }
    printf("Yes\\n");
    while (e[x] > 0 && x > 0) {
      e[x] = 0;
      --cnt;
      --x;
    }
    ++cnt;
    e[x] = 1;
    while (e[now] > 0 && now <= 500010) {
      ++now;
    }
    now = min(now, cnt);
    if (!x)
      now = 1e9;
  }
  return 0;
}

Error: TLE

Fixed submission:

#prag\\
ma GCC optimize(3)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int A[510000];
struct BigInteger {
  static const int BASE = 10;
  static const int WIDTH = 1;
  int size, s[41000];
  inline void killzero() {
    while (s[size - 1] == 0 && size > 1)
      size--;
  }
  inline void clear() {
    size = 0;
    s[0] = 0;
  }
  inline void reverse() {
    for (int i = 0; i < size >> 1; ++i)
      swap(s[i], s[size - i - 1]);
  }
  BigInteger(long long num = 0) { *this = num; }

  BigInteger operator=(long long num) {
    clear();
    do {
      s[size++] = num % BASE;
      num /= BASE;
    } while (num > 0);
    return *this;
  }
  BigInteger operator=(const string &str) {
    clear();
    int x, len = (str.length() - 1) / WIDTH + 1;
    for (int i = 0; i < len; ++i) {
      int end = str.length() - i * WIDTH;
      int start = max(0, end - WIDTH);
      sscanf(str.substr(start, end - start).c_str(), "%d", &x);
      s[size++] = x;
    }
    return *this;
  }
  BigInteger operator+(const BigInteger &b) const {
    BigInteger c;
    c.clear();
    for (int i = 0, g = 0;; ++i) {
      if (g == 0 && i >= size && i >= b.size)
        break;
      int x = g;
      if (i < size)
        x += s[i];
      if (i < b.size)
        x += b.s[i];
      c.s[c.size++] = x % BASE;
      g = x / BASE;
    }
    c.killzero();
    return c;
  }
  BigInteger operator-(const BigInteger &b) const {
    BigInteger c;
    c.clear();
    for (int i = 0, g = 0;; ++i) {
      if (g == 0 && i >= size && i >= b.size)
        break;
      int x = -g;
      if (i < size)
        x += s[i];
      if (i < b.size)
        x -= b.s[i];
      g = 0;
      while (x < 0) {
        x += BASE;
        ++g;
      }
      c.s[c.size++] = x;
    }
    while (c.s[c.size - 1] == 0 && c.size > 1)
      c.size--;
    return c;
  }
  BigInteger operator*(const BigInteger &b) const {
    BigInteger c;
    c.clear();
    for (int i = 0; i < b.size; ++i) {
      BigInteger t;
      t.clear();
      if (b.s[i] == 0)
        continue;
      for (int j = 0, g = 0; j < size || g != 0; ++j) {
        int x = g;
        if (j < size)
          x += b.s[i] * s[j];
        t.s[t.size++] = x % BASE;
        g = x / BASE;
      }
      BigInteger tmp;
      tmp.clear();
      for (int j = 0, g = 0;; ++j) {
        if (g == 0 && j >= t.size + i && j >= c.size)
          break;
        int x = g;
        if (j < c.size)
          x += c.s[j];
        if (j >= i && j < t.size + i)
          x += t.s[j - i];
        tmp.s[tmp.size++] = x % BASE;
        g = x / BASE;
      }
      c = tmp;
    }
    c.killzero();
    return c;
  }
  bool operator<(const BigInteger &b) const {
    if (size != b.size)
      return size < b.size;
    for (int i = size - 1; i >= 0; --i)
      if (s[i] != b.s[i])
        return s[i] < b.s[i];
    return false;
  }
  bool operator>(const BigInteger &b) const { return b < *this; }
  bool operator<=(const BigInteger &b) const { return !(b < *this); }
  bool operator>=(const BigInteger &b) const { return !(*this < b); }
  bool operator!=(const BigInteger &b) const { return b < *this || *this < b; }
  bool operator==(const BigInteger &b) const {
    return !(b < *this) && !(*this < b);
  }
};
BigInteger fastpow(BigInteger n, int k) {
  BigInteger res = 1, tmp = n;
  for (; k; k >>= 1) {
    if (k & 1)
      res = res * tmp;
    tmp = tmp * tmp;
  }
  return res;
}
BigInteger all, tot, t;
int MAXN;
int main() {
  int N;
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i)
    scanf("%d", &A[i]);
  for (int i = 1; i <= N; ++i)
    MAXN = max(MAXN, A[i]);
  all = fastpow(2, MAXN);
  for (int i = 1, a; i <= N; ++i) {
    a = A[i];
    t = fastpow(2, MAXN - a);
    if (t + tot <= all) {
      puts("Yes");
      tot = tot + t;
    } else
      puts("No");
  }
  return 0;
}