alexjercan/bugnet huggingface dataset:
Problem p01545 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
ll dp[110000], x[110000], n, s, rmq[280000], v, res = 1LL << 60;
void update(ll i, ll x) {
  i += v;
  rmq[i] = x;
  while (i > 1) {
    i /= 2, rmq[i] = max(rmq[i * 2], rmq[i * 2 + 1]);
  }
}
long long query(ll p, ll q, ll k, ll l, ll r) {
  if (r <= p || q <= l)
    return 0;
  if (p <= l && r <= q)
    return rmq[k];
  long long vl = query(p, q, 2 * k, l, (l + r) / 2);
  long long vr = query(p, q, 2 * k + 1, (l + r) / 2, r);
  return max(vl, vr);
}
int main() {
  cin >> n;
  for (int i = 0; i < 20; i++) {
    if ((1 << i) < n) {
      v = (1 << (i + 1));
    }
  }
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    dp[i] = x[i];
    s += x[i];
  }
  update(x[0], dp[0]);
  for (int i = 1; i < n; i++) {
    dp[i] = query(1, x[i], 1, 0, v) + x[i];
    update(x[i], dp[i]);
  }
  for (int i = 0; i < n; i++)
    res = min(res, s - dp[i]);
  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
long long dp[110000], x[110000], n, s;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    dp[i] = x[i];
    s += x[i];
  }
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < i; j++) {
      if (x[j] < x[i])
        dp[i] = max(dp[i], dp[j] + x[i]);
    }
  }
  cout << s - dp[n - 1] << endl;
  return 0;
}
Problem p01545 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
using namespace std;
int n, a[100000], b[100000];
long long sum, ret, dp[100001], rmq[262144];
void update(int i, long long x) {
  i += 131072;
  rmq[i] = x;
  while (i > 1)
    i /= 2, rmq[i] = max(rmq[i * 2], rmq[i * 2 + 1]);
}
long long query(int p, int q, int k, int l, int r) {
  if (r <= p || q <= l)
    return 0;
  if (p <= l && r <= q)
    return rmq[k];
  long long vl = query(p, q, 2 * k, l, (l + r) / 2);
  long long vr = query(p, q, 2 * k + 1, (l + r) / 2, r);
  return max(vl, vr);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]), sum += a[i], b[a[i] - 1] = i;
  for (int i = 1; i <= n; i++) {
    update(a[i - 1] - 1, dp[i] = query(0, a[i - 1], 1, 0, 131072) + a[i - 1]);
    ret = max(ret, dp[i]);
  }
  printf("%lld\\n", sum - ret);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
#pragma warning(disable : 4996)
using namespace std;
int n, a[100000], b[100000];
long long sum, ret, dp[100001];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]), sum += a[i], b[a[i] - 1] = i;
  for (int i = 1; i <= n; i++) {
    dp[i] = a[i - 1];
    for (int j = 0; j < a[i - 1]; j++) {
      if (b[j] < i - 1) {
        dp[i] = max(dp[i], dp[b[j] + 1] + a[i - 1]);
      }
    }
  }
  for (int i = 1; i <= n; i++)
    ret = max(ret, dp[i]);
  printf("%lld\\n", sum - ret);
  return 0;
}
Problem p01545 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

const int MAX_N = 1000000;

typedef long long ll;

using namespace std;

ll n, dat[2 * MAX_N - 1];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = 0;
}

void update(int k, ll a) {
  k += n - 1;
  dat[k] = a;

  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = max(dat[2 * k + 1], dat[2 * k + 2]);
  }
}

ll query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return dat[k];
  else {
    ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
}

int main(void) {

  ll in, n_, ans = 0;
  cin >> n_;

  init(n_);

  for (int i = 0; i < n_; i++) {
    cin >> in;
    update(in, in + query(1, in + 1, 0, 0, n));
    ans = max(ans, dat[in + n - 1]);
  }
  cout << n_ * (n_ + 1) / 2 - ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

const int MAX_N = 100000;

typedef long long ll;

using namespace std;

ll n, dat[2 * MAX_N - 1];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = 0;
}

void update(int k, int a) {
  k += n - 1;
  dat[k] = a;

  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = max(dat[2 * k + 1], dat[2 * k + 2]);
  }
}

int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return dat[k];
  else {
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
}

int main(void) {

  ll in, n_, ans = 0;
  cin >> n_;

  init(n_);

  for (int i = 0; i < n_; i++) {
    cin >> in;
    update(in, in + query(1, in + 1, 0, 0, n));
    ans = max(ans, dat[in + n - 1]);
  }
  cout << n_ * (n_ + 1) / 2 - ans << endl;

  return 0;
}
Problem p01545 (C++) — buggy submission, verdict: Runtime Error

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

ll d[(1 << 18)];
ll n;

void init(ll _n) {
  n = 1;
  while (n < _n)
    n *= 2;
  memset(d, 0, sizeof(d));
}

ll query(ll a, ll b, ll k, ll l, ll r) {
  if (r <= a || b <= l)
    return 0;
  else if (a <= l && r <= b)
    return d[k];
  return max(query(a, b, 2 * k + 1, l, (l + r) / 2),
             query(a, b, 2 * k + 2, (l + r) / 2, r));
}

ll query(ll a, ll b) { return query(a, b, 0, 0, n); }

void sets(ll k, ll x) {
  k += n - 1;
  d[k] = x;
  while (k > 0) {
    k = (k - 1) / 2;
    d[k] = max(d[2 * k + 1], d[2 * k + 2]);
  }
}

typedef pair<ll, ll> P;

ll N;
vector<P> p;
int main() {
  cin >> N;
  init(N);
  for (ll i = 0; i < N; i++) {
    ll x;
    cin >> x;
    p.push_back(P(x, i));
  }

  sort(p.begin(), p.end());

  ll res = 0;
  for (ll i = N - 1; i > -1; i--) {
    ll k = p[i].second;
    ll num = query(k, N);
    res = max(res, num + p[i].first);
    sets(k, num + p[i].first);
  }
  cout << N * (N + 1) / 2 - res << endl;
}

Error: WA

Fixed submission:

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

struct seg {
  ll d[(1 < 18)];
  int n;

  void init(int _n) {
    n = 1;
    while (n < _n)
      n *= 2;
    memset(d, 0, sizeof(d));
  }

  ll query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;
    else if (a <= l && r <= b)
      return d[k];
    ll vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    ll vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return max(vl, vr);
  }

  ll query(int a, int b) { return query(a, b, 0, 0, n); }

  void set(int k, ll x) {
    k += n - 1;
    d[k] = x;
    while (k > 0) {
      k = (k - 1) / 2;
      d[k] = max(d[2 * k + 1], d[2 * k + 2]);
    }
  }
};
typedef pair<ll, int> P;

seg S;
int N;
ll X[100005];
P p[100005];
int main() {
  cin >> N;
  S.init(N);
  for (int i = 0; i < N; i++) {
    cin >> X[i];
    p[i] = P(X[i], i);
  }
  sort(p, p + N);
  ll res = 0;
  for (int i = N - 1; i > -1; i--) {
    int k = p[i].second;
    ll num = S.query(k, N);
    res = max(res, num + p[i].first);
    S.set(k, num + p[i].first);
  }
  cout << N * (N + 1) / 2 - res << endl;
}
Problem p01545 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
ld eps = 1e-9;

#define Seg_Max_N (1 << 18)
using Value = long long int;
Value dat[2 * Seg_Max_N];
struct segtree {
  int N;
  const Value ini = 0;
  segtree() {}
  segtree(int n) {
    N = 1;
    while (N < n)
      N *= 2;
    for (int i = 0; i < 2 * N - 1; i++) {
      dat[i] = ini;
    }
  }
  // update k th element
  void update(int k, Value a) {
    k += N - 1; // leaf
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      const Value al(dat[k * 2 + 1]);
      const Value ar(dat[k * 2 + 2]);
      dat[k] = max(al, ar);
    }
  }
  // min [a, b)
  Value query(int a, int b) { return query(a, b, 0, 0, N); }
  Value query(int a, int b, int k, int l, int r) {
    if (r <= a or b <= l)
      return ini;
    if (a <= l and r <= b)
      return dat[k];
    const int m = (l + r) / 2;
    const Value al(query(a, b, k * 2 + 1, l, m));
    const Value ar(query(a, b, k * 2 + 2, m, r));
    return max(al, ar);
  }
};
int main() {
  long long int N;
  cin >> N;
  vector<long long int> hs;
  vector<long long int> dps;
  segtree seg(N);
  for (size_t i = 0; i < N; i++) {
    int a;
    cin >> a;
    a--;
    hs.emplace_back(a);
  }
  for (size_t i = 0; i < N; i++) {
    long long int next = seg.query(0, hs[i]);
    seg.update(hs[i], next + hs[i] + 1);
  }
  long long int ans = N * (N + 1) / 2 - seg.query(0, N);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;

int main() {
  int N;
  cin >> N;
  vector<long long int> nums(N);
  for (int i = 0; i < N; ++i) {
    cin >> nums[i];
  }
  map<int, long long int> nmp;
  nmp[0] = 0;
  for (int i = 0; i < N; ++i) {
    map<int, long long int> newmp(nmp);
    for (auto j : nmp) {
      if (nums[i] > j.first)
        newmp[nums[i]] = max(newmp[nums[i]], j.second + nums[i]);
      else {
        break;
      }
    }
    nmp = newmp;
    map<int, long long int> cleanmp;
    long long int num = -1;
    for (auto j : nmp) {
      if (j.second <= num) {

      } else {
        num = max(num, j.second);
        cleanmp.emplace(j);
      }
    }
    nmp = cleanmp;
  }
  long long int sum = N * (N + 1) / 2;
  long long int ans = sum;
  for (auto m : nmp) {
    ans = min(ans, sum - m.second);
  }
  cout << ans << endl;
  return 0;
}
Problem p01545 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>

using namespace std;

typedef long long ll;
const int MAX = (1 << 18);

class SegmentTree {
public:
  int n;
  ll dat[MAX];

  void init(int num) {
    n = 1;
    while (n < num)
      n *= 2;
    memset(dat, 0, sizeof(dat));
  }

  void update(int k, ll a) {
    k += n - 1;
    dat[k] = a;

    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  ll query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;

    if (a <= l && r <= b)
      return dat[k];

    ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
};

int main() {

  ll n;
  cin >> n;
  SegmentTree st;
  st.init(n);

  ll ans = 0;

  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    ll nex = st.query(1, x + 1, 0, 0, st.n) + x;
    st.update(x, nex);
    ans = max(ans, nex);
  }
  cout << (n * (n + 1LL)) / 2LL - ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>

using namespace std;

typedef long long ll;
const int MAX = 200000;

class SegmentTree {
public:
  int n;
  ll dat[MAX];

  void init(int num) {
    n = 1;
    while (n < num)
      n *= 2;
    memset(dat, 0, sizeof(dat));
  }

  void update(int k, ll a) {
    k += n - 1;
    dat[k] = a;

    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  ll query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;

    if (a <= l && r <= b)
      return dat[k];

    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
};

int main() {

  ll n;
  cin >> n;
  SegmentTree st;
  st.init(n);

  ll ans = 0;

  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    ll nex = st.query(1, x + 1, 0, 0, st.n) + x;
    st.update(x, nex);
    ans = max(ans, nex);
  }
  cout << (n * (n + 1LL)) / 2LL - ans << endl;

  return 0;
}
Problem p01545 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

typedef long long ll;

const int N = 100010;

class BIT {
  static const int N = 100010;

public:
  ll t[N];
  void init() { rep(i, N) t[i] = 0; }
  ll calc(ll a, ll b) { return max(a, b); }

  void setval(int pos, ll val) {
    pos++;
    for (int i = 0;; i++) {
      if (pos >= N)
        break;
      if (pos & (1 << i)) {
        t[pos] = calc(t[pos], val);
        pos += (1 << i);
      }
    }
  }

  ll getval(int pos) {
    pos++;
    ll ret = 0;
    for (int i = 0;; i++) {
      if (pos == 0)
        break;
      if (pos & (1 << i)) {
        ret = calc(ret, t[pos]);
        pos -= (1 << i);
      }
    }
    return ret;
  }

  void print() {
    printf("t : ");
    rep(i, 10) printf("%d ", t[i]);
    puts("");
  }
};

int main() {
  int n;
  cin >> n;

  int p[N];
  ll sum = 0;
  rep(i, n) {
    cin >> p[i];
    sum += p[i];
  }

  BIT bit;
  bit.init();

  rep(i, n) {
    bit.setval(p[i], bit.getval(p[i]) + p[i]);
    // bit.print();
  }

  cout << sum - bit.getval(n) << endl;
}

/*
5
1 5 3 2 4

*/

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

const int N = 10010;
const int INF = 1000000000;
int n;
int dp[2][N];

void init() { rep(i, 2) rep(j, N) dp[i][j] = INF; }

int solve() {
  init();

  int p[N];
  rep(i, n) cin >> p[i];

  int i2 = 0;
  int i1 = 1;

  rep(i, n) {
    dp[i1][i] = p[0];
    if (i >= p[0])
      dp[i1][i] = 0;
  }
  reps(i, 1, n) {
    rep(j, N) dp[i2][j] = INF;
    rep(j, n + 1) {
      dp[i2][j] = min(dp[i2][j], dp[i1][j] + p[i]);
      if (j - 1 >= 0)
        dp[i2][j] = min(dp[i2][j], dp[i2][j - 1]);

      if (p[i] == j) {
        rep(k, j) { dp[i2][j] = min(dp[i2][j], dp[i1][k]); }
      }
    }
    swap(i2, i1);
  }

  return dp[i1][n];
}

int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    printf("%d\\n", solve());
    break;
  }
}
Problem p01545 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;
typedef long long Int;

Int bit[1080000];

void setval(Int pos, Int val) {
  pos++;
  while (pos < 1080000) {
    bit[pos] = max(bit[pos], val);
    pos += pos & -pos;
  }
}

Int getval(Int pos) {
  pos++;
  Int res = 0;
  while (pos) {
    res = max(bit[pos], res);
    pos -= pos & -pos;
  }
  return res;
}

int main() {
  Int n, x, res = 0;
  cin >> n;
  for (Int i = 0; i < n; i++) {
    cin >> x;
    setval(x, x + getval(x));
    res = max(res, getval(x));
  }
  cout << n * (n + 1) / 2 - res << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

typedef long long Int;

Int bit[105000];

void setnum(int pos, Int val) {
  while (pos < 105000) {
    bit[pos] = max(val, bit[pos]);
    pos += pos & -pos;
  }
}

Int maxnum(int pos) {
  Int res = 0;
  while (pos) {
    res = max(res, bit[pos]);
  }
  return res;
}

int main() {
  Int n, sum = 0, x;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    sum += x;
    Int p = p + maxnum(p);
    setnum(x, p);
  }
  cout << sum - maxnum(10100) << endl;
  return 0;
}
Problem p01545 (C++) — buggy submission, verdict: Time Limit Exceeded

// 2431
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e5;

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

typedef long long ll;

const ll INF = 1e15;

int n;
int x[MAX_N];

ll seg[MAX_N * 4];
int s;

void init() {
  s = 1;
  while (s < MAX_N) {
    s <<= 1;
  }
}

void update(int i, ll x) {
  i += s - 1;
  do {
    seg[i] = max(seg[i], x);
    i /= 2;
  } while (i);
}

ll query(int a, int b, int k = 1, int l = 1, int r = s) {
  if (a <= l && r <= b) {
    return seg[k];
  }
  if (b < l || r < a) {
    return 0;
  }
  int mid = (l + r) / 2;
  return max(query(a, b, k * 2, l, mid), query(a, b, k * 2 + 1, mid + 1, r));
}

int main() {
  scanf("%d", &n);
  ll sum = 0;
  REP(i, n) {
    scanf("%d", &x[i]);
    sum += x[i];
  }
  init();
  REP(i, n) { update(x[i], query(1, x[i]) + x[i]); }
  printf("%lld\\n", sum - seg[1]);
  return 0;
}

Error: TLE

Fixed submission:

// 2431
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e5;

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

typedef long long ll;

int n;
int x[MAX_N];

ll dp[MAX_N];

int main() {
  scanf("%d", &n);
  ll sum = 0;
  REP(i, n) {
    scanf("%d", &x[i]);
    sum += x[i];
  }
  dp[0] = x[0];
  ll lis = dp[0];
  FOR(i, 1, n) {
    dp[i] = x[i];
    FOR(j, 0, i) {
      if (x[j] < x[i]) {
        dp[i] = max(dp[i], dp[j] + x[i]);
      }
    }
    lis = max(lis, dp[i]);
  }
  printf("%lld\\n", sum - lis);
  return 0;
}
Problem p01545 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

const ll MAX = 100000;

int main() {
  int n;
  while (cin >> n) {
    ll a[MAX], ans = 0, v[MAX];
    for (int i = 0; i < n; i++)
      scanf("%lld", &a[i]);
    fill(v, v + n, 0);
    for (int i = 0; i < n; i++) {
      ll in = v[a[i] - 1] + a[i];
      ans = max(ans, in);
      int pos = lower_bound(v + a[i] - 1, v + n, in) - v;
      fill(v + a[i] - 1, v + pos, in);
    }
    printf("%lld\\n", (ll)n * (n + 1) / 2 - ans);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

const ll MAX = 100000;

int main() {
  int n;
  while (cin >> n) {
    ll a[MAX], ans = 0;
    map<ll, ll> s;
    for (int i = 0; i < n; i++)
      scanf("%lld", &a[i]);
    for (int i = 0; i < n; i++) {
      ll cnt = 0;
      if (i > 0) {
        for (map<ll, ll>::iterator it = s.end();; it--) {
          if (it == s.end())
            it--;
          if (it->second < a[i]) {
            cnt = it->first;
            break;
          }
          if (it == s.begin())
            break;
        }
      }
      ans = max(ans, cnt + a[i]);
      if (s.find(cnt + a[i]) == s.end() || s[cnt + a[i]] > a[i])
        s[cnt + a[i]] = a[i];
    }
    printf("%lld\\n", (ll)n * (n + 1) / 2 - ans);
  }
}
Problem p01545 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX_N = 1 << 17;
int n, dat[2 * MAX_N - 1];
void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = 0;
}
void update(int k, int a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}
// find(a,b,0,0,n);
int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return dat[k];
  else {
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
}
signed main() {
  int m;
  cin >> m;
  int dp[m + 1];
  memset(dp, 0, sizeof(dp));
  init(m + 1);
  int ans = 0;
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    dp[x] = query(0, x, 0, 0, n) + x;
    // cout<<x<<":"<<dp[x]<<endl;
    ans = max(ans, dp[x]);
    update(x, dp[x]);
  }
  cout << m * (m + 1) / 2 - ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX_N = 1 << 17;
int bit[MAX_N + 1], n;
int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}
void add(int i, int x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}
signed main() {
  memset(bit, 0, sizeof(bit));
  cin >> n;
  int x[n], ans = 0;
  for (int i = 0; i < n; i++)
    cin >> x[i];
  n++;
  for (int i = n - 1; i >= 0; i--) {
    // cout<<x[i]<<":"<<sum(x[i])<<endl;
    ans += min(x[i], sum(x[i]));
    add(x[i], x[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p01545 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAX_N = 1 << 17;
ll n, dat[2 * MAX_N - 1];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = 0;
}

void update(ll k, ll a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

ll query(ll a, ll b, ll k, ll l, ll r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return dat[k];
  else {
    ll v1 = query(a, b, k * 2 + 1, l, (l + r) / 2);
    ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(v1, vr);
  }
}

int main() {
  cin >> n;
  ll nn = n;
  ll ans = 0;
  init(n);
  for (ll i = 0, x; i < nn; i++) {
    cin >> x;
    ll p = query(0, x, 0, 0, n);
    update(x - 1, p + x);
    ans = max(ans, p + x);
  }

  cout << nn * (nn + 1) / 2 - ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m, s[15], d[100], dp[101][1 << 15];

void minimize(int &a, int b) { a = min(a, b); }

int main() {

  int n, m;
  while (cin >> n >> m, n) {
    for (int i = 0; i < n; i++)
      cin >> s[i];
    for (int i = 0; i < m; i++)
      cin >> d[i];
    sort(d, d + m);

    int sum[1 << 15] = {};
    for (int i = 0; i < (1 << n); i++)
      for (int j = 0; j < n; j++)
        if ((1 << j) & i)
          sum[i] += s[j];

    for (int j = 0; j <= m; j++)
      for (int i = 0; i < (1 << n); i++)
        dp[j][i] = 1e9;
    dp[0][0] = 0;

    for (int i = 0; i < m; i++)
      for (int j = 0; j < (1 << n); j++) {

        for (int k = 0; k < n; k++) {
          if (((1 << k) & j) != 0)
            continue;

          //	  minimize( dp[i+1][j+(1<<k)] ,
          //dp[i][j]+abs(sum[j+(1<<k)]-d[i]));
          minimize(dp[i][j + (1 << k)], dp[i][j]);
        }

        minimize(dp[i + 1][j], dp[i][j] + abs(sum[j] - d[i]));
      }

    int mn = 1e9;
    for (int i = 0; i < (1 << n); i++)
      mn = min(mn, dp[m][i]);
    cout << mn << endl;
  }
  return 0;
}