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

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

template <typename D, typename T = D> class Tree {
public:
  Tree(long long int size = 0, D InitialValueArg = D()) {
    long long int n = 1LL;
    CalcTree.emplace_back(vector<T>(n));
    while (n <= size) {
      n <<= 1LL;
      addOneLine(n);
    }
    InitialValue = InitialValueArg;
    Data.resize(n, InitialValue);
    for (size_t i = 0; i < Data.size(); i++) {
      update(i, Data[i]);
    }
    MaxSize = n;
    NowIndex = 0LL;
  }

  virtual T preprocessing(D a) { return T(a); }

  virtual T treeupdate(T a, T b) { return a; }

  void update(long long int idx, D data) {
    Data[idx] = data;
    (*(CalcTree.rbegin()))[idx] = preprocessing(data);
    for (auto ite = CalcTree.rbegin(); ite + 1 < CalcTree.rend();) {
      idx >>= 1LL;
      T updatedata = treeupdate((*ite)[idx * 2], (*ite)[idx * 2 + 1]);
      ite++;
      if ((*ite)[idx] != updatedata) {
        (*ite)[idx] = updatedata;
      } else {
        break;
      }
    }
  }

  void show() {
    for (auto x : CalcTree) {
      for (auto y : x) {
        cout << y << " ";
      }
      cout << endl;
    }
  }

  void push(D data) {
    if (NowIndex == MaxSize) {
      MaxSize <<= 1LL;
      addOneLine(MaxSize);
      vector<D> Datamemo(MaxSize, InitialValue);
      copy(Data.begin(), Data.end(), Datamemo.begin());
      for (long long int i = 0; i < Datamemo.size(); i++) {
        update(i, Datamemo[i]);
      }
    }
    Data[NowIndex] = data;
    update(NowIndex, data);
    NowIndex++;
  }

  T range(long long int l, long long int r) {
    return rangeProcessing(0, l, r, 0, 0, MaxSize);
  }

  long long int t() { return 10; }

private:
  vector<vector<T>> CalcTree;
  vector<D> Data;
  long long int MaxSize;
  long long int NowIndex;
  D InitialValue;

  void addOneLine(long long int n) { CalcTree.emplace_back(vector<T>(n)); }

  T rangeProcessing(long long int d, long long int a, long long int b,
                    long long int k, long long int l, long long int r) {
    if (!(r <= a || b <= l) && d < CalcTree.size()) {
      if (a <= l && r <= b) {
        return CalcTree[d][k];
      } else {
        return treeupdate(
            rangeProcessing(d + 1, a, b, k * 2, l, (l + r) / 2),
            rangeProcessing(d + 1, a, b, k * 2 + 1, (l + r) / 2, r));
      }
    }
    return InitialValue;
  }
};

class RMQ : public Tree<long long int> {
public:
  RMQ(long long int a, long long int b) : Tree(a, b) {}
  long long int treeupdate(long long int a, long long int b) {
    return max(a, b);
  }
};

int main() {
  long long int N;
  cin >> N;
  vector<long long int> data(N);
  RMQ rmq(N + 1, LLONG_MIN);
  long long int sum = 0;
  for (size_t i = 0; i < N; i++) {
    cin >> data[i];
    sum += data[i];
  }
  vector<long long int> dp(N + 1, LLONG_MIN);
  dp[0] = 0;
  rmq.update(0, 0);
  long long int maxnum = 0;
  for (size_t i = 0; i < N; i++) {
    dp[data[i]] = rmq.range(0, data[i]) + data[i];
    rmq.update(data[i], dp[data[i]]);
  }
  for (size_t i = 0; i < N + 1; i++) {
    maxnum = max(maxnum, dp[i]);
  }
  cout << sum - maxnum << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  long long int N;
  cin >> N;
  vector<long long int> data(N);
  long long int sum = 0;
  for (size_t i = 0; i < N; i++) {
    cin >> data[i];
    sum += data[i];
  }
  vector<long long int> dp(N + 1, LLONG_MIN);
  dp[0] = 0;
  long long int maxnum = 0;
  for (size_t i = 0; i < N; i++) {
    for (size_t j = 0; j < data[i]; j++) {
      dp[data[i]] = max(dp[data[i]], dp[j] + data[i]);
      maxnum = max(dp[data[i]], maxnum);
    }
  }
  cout << sum - maxnum << endl;
}