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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;

// container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define MT(a, b, c) MP(a, MP(b, c))

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

class SegTree {
public:
  VLL dat;
  int n;
  LL DEF;
  SegTree(int N) {
    n = 1;
    DEF = 0;
    while (n < N)
      n *= 2;
    dat = VLL(2 * n - 1, DEF);
  }
  void update(int index, LL v) {
    int k = index + n - 1;
    dat[k] = v;
    while (1) {
      k = (k - 1) / 2; // 上に登る
      dat[k] = value(k);
      if (k == 0)
        break;
    }
  }
  inline LL value(int idx) { // Update value with (2*idx+1, 2*idx+2)
    return value(dat[2 * idx + 1], dat[2 * idx + 2]);
  }
  inline LL value(LL v1, LL v2) { return max(v1, v2); }
  LL query(int idx1, int idx2, int node = 0, int range1 = 0, int range2 = -10) {
    if (range2 == -10)
      range2 = n;
    if (range2 <= idx1 || idx2 <= range1)
      return DEF; // 交差しない
    if (idx1 <= range1 && range2 <= idx2)
      return dat[node];
    else {
      LL vl = query(idx1, idx2, node * 2 + 1, range1, (range1 + range2) / 2);
      LL vr = query(idx1, idx2, node * 2 + 2, (range1 + range2) / 2, range2);
      return value(vl, vr);
    }
  }
};

int main() {
  int n;
  cin >> n;
  SegTree seg(n);
  REP(i, n) {
    int tmp;
    scanf("%d", &tmp);
    LL v = seg.query(0, tmp - 1);
    seg.update(tmp - 1, v + tmp);
  }
  cout << ((LL)n) * (n + 1) / 2 - seg.query(0, n) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;

// container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define MT(a, b, c) MP(a, MP(b, c))

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int main() {
  int n;
  cin >> n;
  VI a(n);
  VLL b(n, 0);
  REP(i, n) scanf("%d", &a[i]);
  REP(i, n) {
    // a[i]を使う場合
    int idx = a[i] - 1;
    REP(j, a[i]) {
      b[idx] = max(b[idx], (j == a[i] - 1) ? a[i] : (b[j] + a[i]));
    }
  }
  LL s = 0;
  REP(i, n) s = max(s, b[i]);
  cout << n * (n + 1) / 2 - s << endl;
  return 0;
}