prithivMLmods/Coder-Stat huggingface dataset:
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int n;
long long a[100002], b[100002], aib[100002];
void upd(int poz, long long val) {
  for (; poz <= n; poz += poz & (-poz))
    aib[poz] = max(aib[poz], val);
}
long long compute(int poz) {
  long long ans = 0;
  for (; poz; poz -= poz & (-poz))
    ans = max(ans, aib[poz]);
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  for (int i = 1; i <= n; ++i)
    cin >> b[i];
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    long long sum = compute(a[i] - 1) + b[i];
    ans = max(ans, sum);
    upd(a[i], sum);
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int n;
long long a[200002], b[200002], aib[200002];
void upd(int poz, long long val) {
  for (; poz <= n; poz += poz & (-poz))
    aib[poz] = max(aib[poz], val);
}
long long compute(int poz) {
  long long ans = 0;
  for (; poz; poz -= poz & (-poz))
    ans = max(ans, aib[poz]);
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  for (int i = 1; i <= n; ++i)
    cin >> b[i];
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    long long sum = compute(a[i] - 1) + b[i];
    ans = max(ans, sum);
    upd(a[i], sum);
  }
  cout << ans;
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define N_MAX 100002
#define ll long long

using namespace std;

int n;

int h[N_MAX];
ll b[N_MAX];

ll aib[N_MAX];

void update(int pos, ll val) {
  for (int i = pos; i <= n; i += i & (-i))
    aib[i] = max(aib[i], val);
}

ll query(int pos) {
  ll ans = 0;
  for (int i = pos - 1; i >= 1; i -= i & (-i))
    ans = max(ans, aib[i]);
  return ans;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  for (int i = 1; i <= n; i++)
    cin >> b[i];
  for (int i = 1; i <= n; i++) {
    ll dp = query(h[i]) + b[i];
    update(h[i], dp);
  }
  cout << query(n + 1) << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define N_MAX 200002
#define ll long long

using namespace std;

int n;

int h[N_MAX];
ll b[N_MAX];

ll aib[N_MAX];

void update(int pos, ll val) {
  for (int i = pos; i <= n; i += i & (-i))
    aib[i] = max(aib[i], val);
}

ll query(int pos) {
  ll ans = 0;
  for (int i = pos - 1; i >= 1; i -= i & (-i))
    ans = max(ans, aib[i]);
  return ans;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  for (int i = 1; i <= n; i++)
    cin >> b[i];
  for (int i = 1; i <= n; i++) {
    ll dp = query(h[i]) + b[i];
    update(h[i], dp);
  }
  cout << query(n + 1) << "\n";
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define BPhuong "main"
#define maxn 100005
#define maxm
#define fi(i, a, b) for (int i = a; i <= b; i++)
#define fid(i, a, b) for (int i = a; i >= b; i--)
#define se second
#define f first
#define ll long long
#define pii pair<int, int>
#define pb push_back
#define bit(x, i) ((x >> i) & 1)
#define oo 1e9
struct data {
  int x, y, z;
};
using namespace std;
int n, h[maxn], a[maxn];
ll l[maxn], f[maxn];
void upp(int x, ll j) {
  while (x <= maxn) {
    l[x] = max(l[x], j);
    x += (x & -x);
  }
}
ll get(int x) {
  ll ans = 0;
  while (x >= 1) {
    ans = max(ans, l[x]);
    x -= (x & -x);
  }
  return ans;
}
void nhap() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  // freopen(BPhuong".inp","r",stdin);
  // freopen(BPhuong".out","w",stdout);
  cin >> n;
  fi(i, 1, n) cin >> h[i];
  fi(i, 1, n) cin >> a[i];
}

void xuli() {
  ll res = 0;
  fi(i, 1, n) {
    f[i] = max(f[i], get(h[i] - 1) + a[i]);
    upp(h[i], f[i]);
    res = max(res, f[i]);
  }
  cout << res;
}

int main() {
  nhap();
  xuli();
}


Fixed submission:
#include <bits/stdc++.h>
#define BPhuong "main"
#define maxn 200005
#define maxm
#define fi(i, a, b) for (int i = a; i <= b; i++)
#define fid(i, a, b) for (int i = a; i >= b; i--)
#define se second
#define f first
#define ll long long
#define pii pair<int, int>
#define pb push_back
#define bit(x, i) ((x >> i) & 1)
#define oo 1e9
struct data {
  int x, y, z;
};
using namespace std;
int n, h[maxn], a[maxn];
ll l[maxn], f[maxn];
void upp(int x, ll j) {
  while (x <= maxn) {
    l[x] = max(l[x], j);
    x += (x & -x);
  }
}
ll get(int x) {
  ll ans = 0;
  while (x >= 1) {
    ans = max(ans, l[x]);
    x -= (x & -x);
  }
  return ans;
}
void nhap() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  // freopen(BPhuong".inp","r",stdin);
  // freopen(BPhuong".out","w",stdout);
  cin >> n;
  fi(i, 1, n) cin >> h[i];
  fi(i, 1, n) cin >> a[i];
}

void xuli() {
  ll res = 0;
  fi(i, 1, n) {
    f[i] = max(f[i], get(h[i] - 1) + a[i]);
    upp(h[i], f[i]);
    res = max(res, f[i]);
  }
  cout << res;
}

int main() {
  nhap();
  xuli();
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 1e5;
const LL INF = 1e7;

int n;
int h[N + 5];
LL a[N + 5];

LL ST[4 * N + 5];

void build(int node = 1, int l = 1, int r = n) {
  if (l == r) {
    ST[node] = -INF;
    return;
  }
  build(node * 2, l, (l + r) / 2);
  build(node * 2 + 1, (l + r) / 2 + 1, r);
  ST[node] = max(ST[node * 2], ST[node * 2 + 1]);
  return;
}

void update(int node, int l, int r, int pos, LL val) {
  if (l > pos || r < pos)
    return;
  if (l == r) {
    ST[node] = val;
    return;
  }
  update(node * 2, l, (l + r) / 2, pos, val);
  update(node * 2 + 1, (l + r) / 2 + 1, r, pos, val);
  ST[node] = max(ST[node * 2], ST[node * 2 + 1]);
  return;
}

LL query(int node, int l, int r, int ql, int qr) {
  if (l > qr || r < ql)
    return -INF;
  if (l >= ql && r <= qr)
    return ST[node];
  return max(query(node * 2, l, (l + r) / 2, ql, qr),
             query(node * 2 + 1, (l + r) / 2 + 1, r, ql, qr));
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &h[i]);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  build();
  LL ans = 0LL;
  for (int i = 1; i <= n; i++) {
    LL tmp = max(0LL, query(1, 1, n, 1, h[i]));
    tmp += a[i];
    //		cout << tmp << endl;
    ans = max(ans, tmp);
    update(1, 1, n, h[i], tmp);
  }
  printf("%lld\n", ans);
  return 0;
}

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

typedef long long LL;

const int N = 2e5;
const LL INF = 1e7;

int n;
int h[N + 5];
LL a[N + 5];

LL ST[4 * N + 5];

void build(int node = 1, int l = 1, int r = n) {
  if (l == r) {
    ST[node] = -INF;
    return;
  }
  build(node * 2, l, (l + r) / 2);
  build(node * 2 + 1, (l + r) / 2 + 1, r);
  ST[node] = max(ST[node * 2], ST[node * 2 + 1]);
  return;
}

void update(int node, int l, int r, int pos, LL val) {
  if (l > pos || r < pos)
    return;
  if (l == r) {
    ST[node] = val;
    return;
  }
  update(node * 2, l, (l + r) / 2, pos, val);
  update(node * 2 + 1, (l + r) / 2 + 1, r, pos, val);
  ST[node] = max(ST[node * 2], ST[node * 2 + 1]);
  return;
}

LL query(int node, int l, int r, int ql, int qr) {
  if (l > qr || r < ql)
    return -INF;
  if (l >= ql && r <= qr)
    return ST[node];
  return max(query(node * 2, l, (l + r) / 2, ql, qr),
             query(node * 2 + 1, (l + r) / 2 + 1, r, ql, qr));
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &h[i]);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  build();
  LL ans = 0LL;
  for (int i = 1; i <= n; i++) {
    LL tmp = max(0LL, query(1, 1, n, 1, h[i]));
    tmp += a[i];
    //		cout << tmp << endl;
    ans = max(ans, tmp);
    update(1, 1, n, h[i], tmp);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int mx = 100005;
int n, a, h[mx];

struct SegTree {
  int sz = 2;
  vector<long long> dat;
  SegTree(int n) {
    while (n >= sz)
      sz *= 2;
    dat.assign(sz * 2, 0);
  }
  void set(int x, long long n) {
    int l = sz, r = x - 1 + sz;
    long long res = 0;
    while (l <= r) {
      if (l & 1)
        res = max(res, dat[l++]);
      if (r & 1 ^ 1)
        res = max(res, dat[r--]);
      l /= 2;
      r /= 2;
    }
    dat[x += sz] = n + res;
    for (; x; x /= 2)
      dat[x / 2] = max(dat[x], dat[x ^ 1]);
  }
};
int main() {
  scanf("%d", &n);
  SegTree sg(n);
  for (int i = 0; i < n; i++)
    scanf("%d", h + i);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    sg.set(h[i], a);
  }
  printf("%lld\n", sg.dat[1]);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int mx = 200005;
int n, a, h[mx];

struct SegTree {
  int sz = 2;
  vector<long long> dat;
  SegTree(int n) {
    while (n >= sz)
      sz *= 2;
    dat.assign(sz * 2, 0);
  }
  void set(int x, long long n) {
    int l = sz, r = x - 1 + sz;
    long long res = 0;
    while (l <= r) {
      if (l & 1)
        res = max(res, dat[l++]);
      if (r & 1 ^ 1)
        res = max(res, dat[r--]);
      l /= 2;
      r /= 2;
    }
    dat[x += sz] = n + res;
    for (; x; x /= 2)
      dat[x / 2] = max(dat[x], dat[x ^ 1]);
  }
};
int main() {
  scanf("%d", &n);
  SegTree sg(n);
  for (int i = 0; i < n; i++)
    scanf("%d", h + i);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    sg.set(h[i], a);
  }
  printf("%lld\n", sg.dat[1]);
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int, int>
#define F first
#define S second

const ll P = 1000000007;
const int N = (1 << 17);

using namespace std;

int n;
ll f[N + 1], h[N], a[N];

void add(int i, ll o) {
  for (; i <= N; i += i & -i)
    f[i] = max(f[i], o);
}

ll msum(int i) {
  ll s = 0;
  for (; i > 0; i -= i & -i)
    s = max(f[i], s);
  return s;
}

int main() {

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++)
    cin >> a[i];

  ll ans = 0;

  for (int i = 0; i < n; i++) {
    ll s = msum(h[i] - 1);
    ans = max(ans, s + a[i]);
    add(h[i], s + a[i]);
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int, int>
#define F first
#define S second

const ll P = 1000000007;
const int N = (1 << 18);

using namespace std;

int n;
ll f[N + 1], h[N], a[N];

void add(int i, ll o) {
  for (; i <= N; i += i & -i)
    f[i] = max(f[i], o);
}

ll msum(int i) {
  ll s = 0;
  for (; i > 0; i -= i & -i)
    s = max(f[i], s);
  return s;
}

int main() {

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++)
    cin >> a[i];

  ll ans = 0;

  for (int i = 0; i < n; i++) {
    ll s = msum(h[i] - 1);
    ans = max(ans, s + a[i]);
    add(h[i], s + a[i]);
  }

  cout << ans << endl;

  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int64_t query(vector<int64_t> dp, int l, int r, int v, int b, int e) {
  if (b >= r || e <= l)
    return 0;
  if (b <= l && e >= r)
    return dp[v];
  int h = (l + r) / 2;
  return max(query(dp, l, h, v * 2, b, e), query(dp, h, r, v * 2 + 1, b, e));
}
void update(vector<int64_t> &dp, int l, int r, int v, int i, int64_t x) {
  if (i < l || i >= r)
    return;
  if (l == i && r == i + 1) {
    dp[v] = x;
    return;
  }
  int h = (l + r) / 2;
  update(dp, l, h, v * 2, i, x);
  update(dp, h, r, v * 2 + 1, i, x);
  dp[v] = max(dp[v * 2], dp[v * 2 + 1]);
  return;
}
int main() {
  int n;
  cin >> n;
  vector<int> h(n);
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> h.at(i);
    h.at(i)--;
  }
  for (int i = 0; i < n; i++)
    cin >> a.at(i);
  vector<int64_t> dp(n * 4, 0);
  for (int i = 0; i < n; i++) {
    int64_t q = query(dp, 0, n, 1, 0, h.at(i));
    update(dp, 0, n, 1, h.at(i), q + a.at(i));
  }
  cout << dp[1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int64_t query(vector<int64_t> &dp, int l, int r, int v, int b, int e) {
  if (b >= r || e <= l)
    return 0;
  if (b <= l && e >= r)
    return dp[v];
  int h = (l + r) / 2;
  return max(query(dp, l, h, v * 2, b, e), query(dp, h, r, v * 2 + 1, b, e));
}
void update(vector<int64_t> &dp, int l, int r, int v, int i, int64_t x) {
  if (i < l || i >= r)
    return;
  if (l == i && r == i + 1) {
    dp[v] = x;
    return;
  }
  int h = (l + r) / 2;
  update(dp, l, h, v * 2, i, x);
  update(dp, h, r, v * 2 + 1, i, x);
  dp[v] = max(dp[v * 2], dp[v * 2 + 1]);
  return;
}
int main() {
  int n;
  cin >> n;
  vector<int> h(n);
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> h.at(i);
    h.at(i)--;
  }
  for (int i = 0; i < n; i++)
    cin >> a.at(i);
  vector<int64_t> dp(n * 4, 0);
  for (int i = 0; i < n; i++) {
    int64_t q = query(dp, 0, n, 1, 0, h.at(i));
    update(dp, 0, n, 1, h.at(i), q + a.at(i));
  }
  cout << dp[1] << endl;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld double
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define setpr(pp) fixed << setprecision(pp)
#define FOR(I, N) for (I = 0; I < N; ++I)
#define MS0(x) memset(x, 0, sizeof(x))
#define MS1(x) memset(x, -1, sizeof(x))
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define mod % 1000000007
#define pi 3.141592653
#define MX 1000000007
ll ftree[100005];

ll query(ll x) {
  ll ans = 0;
  for (; x > 0; x -= (x & -x)) {
    ans = max(ans, ftree[x]);
  }
  return ans;
}

void update(ll x, ll delta, ll n) {
  for (; x <= n; x += (x & -x))
    ftree[x] = max(ftree[x], delta);
}

int main() {
  IOS;
  ll n, i, x;
  cin >> n;
  ll h[n + 1], a[n + 1];
  MS0(ftree);
  ll ans = 0;
  for (i = 1; i <= n; ++i)
    cin >> h[i];
  for (i = 1; i <= n; ++i) {
    cin >> a[i];
    ans = max(ans, a[i]);
  }
  for (i = 1; i <= n; ++i) {
    x = query(h[i] - 1);
    x += a[i];
    ans = max(ans, x);
    update(h[i], x, n);
  }
  cout << ans;
}

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

#define ll long long
#define ld double
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define setpr(pp) fixed << setprecision(pp)
#define FOR(I, N) for (I = 0; I < N; ++I)
#define MS0(x) memset(x, 0, sizeof(x))
#define MS1(x) memset(x, -1, sizeof(x))
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define mod % 1000000007
#define pi 3.141592653
#define MX 1000000007
ll ftree[200005];

ll query(ll x) {
  ll ans = 0;
  for (; x > 0; x -= (x & -x)) {
    ans = max(ans, ftree[x]);
  }
  return ans;
}

void update(ll x, ll delta, ll n) {
  for (; x <= n; x += (x & -x))
    ftree[x] = max(ftree[x], delta);
}

int main() {
  IOS;
  ll n, i, x;
  cin >> n;
  ll h[n + 1], a[n + 1];
  MS0(ftree);
  ll ans = 0;
  for (i = 1; i <= n; ++i)
    cin >> h[i];
  for (i = 1; i <= n; ++i) {
    cin >> a[i];
    ans = max(ans, a[i]);
  }
  for (i = 1; i <= n; ++i) {
    x = query(h[i] - 1);
    x += a[i];
    ans = max(ans, x);
    update(h[i], x, n);
  }
  cout << ans;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int n;
long long int dp[400005], a[100001];
int h[100001];

long long int maximum(int a, int b, int curr, int z) {
  if (a > z)
    return 0;
  if (b <= z)
    return dp[curr];
  if (b == a) {
    if (a <= z)
      return dp[curr];
    else
      return 0;
  }
  if (b > a) {
    int mid = (a + b) / 2;
    long long int x = maximum(a, mid, 2 * curr + 1, z);
    long long int y = maximum(mid + 1, b, 2 * curr + 2, z);
    return max(x, y);
  }
  return 0;
}

void update(int curr, int a, int b, long long int z, int i) {
  // cout<<a<<" "<<b<<" "<<i<<endl;
  if (a > i or b < i) {
    return;
  }
  if (b == a) {
    if (a == i)
      dp[curr] = max(z, dp[curr]);
    return;
  }
  if (b > a) {
    dp[curr] = max(z, dp[curr]);
    update(2 * curr + 1, a, (a + b) / 2, z, i);
    update(2 * curr + 2, (a + b) / 2 + 1, b, z, i);
  }
}

void fn(int i) {
  long long int z = maximum(1, n, 0, h[i] - 1) + a[i];
  update(0, 1, n, z, h[i]);
}

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; ++i) {
    fn(i);
    // cout<<h[i]<<" "<<dp[0]<<endl;
  }
  long long int ans = maximum(1, n, 0, n);
  cout << ans << endl;
  return 0;
}

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

int n;
long long int dp[800005], a[200001];
int h[200001];

long long int maximum(int a, int b, int curr, int z) {
  if (a > z)
    return 0;
  if (b <= z)
    return dp[curr];
  if (b == a) {
    if (a <= z)
      return dp[curr];
    else
      return 0;
  }
  if (b > a) {
    int mid = (a + b) / 2;
    long long int x = maximum(a, mid, 2 * curr + 1, z);
    long long int y = maximum(mid + 1, b, 2 * curr + 2, z);
    return max(x, y);
  }
  return 0;
}

void update(int curr, int a, int b, long long int z, int i) {
  // cout<<a<<" "<<b<<" "<<i<<endl;
  if (a > i or b < i) {
    return;
  }
  if (b == a) {
    if (a == i)
      dp[curr] = max(z, dp[curr]);
    return;
  }
  if (b > a) {
    dp[curr] = max(z, dp[curr]);
    update(2 * curr + 1, a, (a + b) / 2, z, i);
    update(2 * curr + 2, (a + b) / 2 + 1, b, z, i);
  }
}

void fn(int i) {
  long long int z = maximum(1, n, 0, h[i] - 1) + a[i];
  update(0, 1, n, z, h[i]);
}

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; ++i) {
    fn(i);
    // cout<<h[i]<<" "<<dp[0]<<endl;
  }
  long long int ans = maximum(1, n, 0, n);
  cout << ans << endl;
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 1 << 18;
int arbre[MAXN];

void ajout(int noeud, int valeur) {
  noeud += MAXN;
  arbre[noeud] = valeur;
  while (noeud > 0) {
    noeud /= 2;
    arbre[noeud] = max(arbre[noeud * 2], arbre[noeud * 2 + 1]);
  }
}

int trouverMax(int gauche, int droite) {
  gauche += MAXN, droite += MAXN;
  int maxi = 0;
  while (gauche <= droite) {
    if (gauche % 2 == 1) {
      maxi = max(maxi, arbre[gauche]);
      gauche++;
    }
    if (droite % 2 == 0) {
      maxi = max(maxi, arbre[droite]);
      droite--;
    }
    gauche /= 2;
    droite /= 2;
  }
  return maxi;
}

int32_t main() {
  int N;
  cin >> N;
  int h[N];
  for (int i = 0; i < N; i++) {
    cin >> h[i];
    h[i]--;
  }
  int v[N];
  for (int i = 0; i < N; i++) {
    cin >> v[i];
  }
  for (int i = 0; i < N; i++) {
    ajout(h[i], v[i] + trouverMax(0, h[i]));
  }
  cout << trouverMax(0, N - 1) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 1 << 18;
int arbre[MAXN * 2];

void ajout(int noeud, int valeur) {
  noeud += MAXN;
  arbre[noeud] = valeur;
  while (noeud > 0) {
    noeud /= 2;
    arbre[noeud] = max(arbre[noeud * 2], arbre[noeud * 2 + 1]);
  }
}

int trouverMax(int gauche, int droite) {
  gauche += MAXN, droite += MAXN;
  int maxi = 0;
  while (gauche <= droite) {
    if (gauche % 2 == 1) {
      maxi = max(maxi, arbre[gauche]);
      gauche++;
    }
    if (droite % 2 == 0) {
      maxi = max(maxi, arbre[droite]);
      droite--;
    }
    gauche /= 2;
    droite /= 2;
  }
  return maxi;
}

int32_t main() {
  int N;
  cin >> N;
  int h[N];
  for (int i = 0; i < N; i++) {
    cin >> h[i];
    h[i]--;
  }
  int v[N];
  for (int i = 0; i < N; i++) {
    cin >> v[i];
  }
  for (int i = 0; i < N; i++) {
    ajout(h[i], v[i] + trouverMax(0, h[i]));
  }
  cout << trouverMax(0, N - 1) << endl;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long t[600000];
int h[200001];
long long query(int index, int lb, int rb, int lq, int rq) {
  if (lb == rb || (lq == lb && rb == rq))
    return t[index];
  int mid = (lb + rb) / 2;
  if (lq > mid)
    return query(index * 2 + 1, mid + 1, rb, lq, rq);
  else if (rq <= mid)
    return query(index * 2, lb, mid, lq, rq);
  else
    return max(query(index * 2, lb, mid, lq, mid),
               query(index * 2 + 1, mid + 1, rb, mid + 1, rq));
}
long long update(int index, int lb, int rb, int leaf_id, long long value) {
  if (lb == rb && lb == leaf_id)
    t[index] = value;
  int mid = (lb + rb) / 2;
  if (leaf_id <= mid)
    return t[index] = max(t[index], update(index * 2, lb, mid, leaf_id, value));
  else
    return t[index] = max(t[index],
                          update(index * 2 + 1, mid + 1, rb, leaf_id, value));
}
int main() {
  int i, n, a;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &h[i]);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a);
    update(1, 1, n, h[i], query(1, 1, n, 1, h[i] - 1) + a);
  }
  printf("%lld", t[1]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long t[600000];
int h[200001];
long long query(int index, int lb, int rb, int lq, int rq) {
  if (lb == rb || (lq == lb && rb == rq))
    return t[index];
  int mid = (lb + rb) / 2;
  if (lq > mid)
    return query(index * 2 + 1, mid + 1, rb, lq, rq);
  else if (rq <= mid)
    return query(index * 2, lb, mid, lq, rq);
  else
    return max(query(index * 2, lb, mid, lq, mid),
               query(index * 2 + 1, mid + 1, rb, mid + 1, rq));
}
long long update(int index, int lb, int rb, int leaf_id, long long value) {
  if (lb == rb && lb == leaf_id)
    return t[index] = value;
  int mid = (lb + rb) / 2;
  if (leaf_id <= mid)
    return t[index] = max(t[index], update(index * 2, lb, mid, leaf_id, value));
  else
    return t[index] = max(t[index],
                          update(index * 2 + 1, mid + 1, rb, leaf_id, value));
}
int main() {
  int i, n, a;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &h[i]);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a);
    update(1, 1, n, h[i], query(1, 1, n, 1, h[i] - 1) + a);
  }
  printf("%lld", t[1]);
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef long long ll;

const int mod = 1e9 + 7;
const ll inf = 3e18 + 5;

const int MAXN = 1 << 18; // 2.6e5
int N = 200000;
ll seg[2 * MAXN]; // root is seg[1]

void update(int p, ll val) {
  for (seg[p += N] = val; p > 0; p >>= 1)
    seg[p >> 1] = max(seg[p], seg[p ^ 1]);
}

ll query(int l, int r) // [l, r]
{
  ll res = 0;
  for (l += N, r += N; l <= r; l >>= 1, r >>= 1) {
    if (l & 1)
      res = max(res, seg[l++]);
    if (!(r & 1))
      res = max(res, seg[r--]);
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n;
  cin >> n;
  vi a(n), h(n);
  for (int i = 0; i < n; i++)
    cin >> h[i];

  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n; i++) {
    update(h[i], query(1, h[i] - 1) + a[i]);
  }
  cout << query(1, N);
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef long long ll;

const int mod = 1e9 + 7;
const ll inf = 3e18 + 5;

const int MAXN = 1 << 18; // 2.6e5
int N = 200000;
ll seg[2 * MAXN]; // root is seg[1]

void update(int p, ll val) {
  for (seg[p += N] = val; p > 0; p >>= 1)
    seg[p >> 1] = max(seg[p], seg[p ^ 1]);
}

ll query(int l, int r) // [l, r]
{
  ll res = 0;
  for (l += N, r += N; l <= r; l >>= 1, r >>= 1) {
    if (l & 1)
      res = max(res, seg[l++]);
    if (!(r & 1))
      res = max(res, seg[r--]);
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  vi a(n), h(n);
  for (int i = 0; i < n; i++)
    cin >> h[i];

  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n; i++) {
    update(h[i], query(1, h[i] - 1) + a[i]);
  }
  cout << query(1, N);
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long LL;
typedef unsigned int uii;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
typedef vector<int> vii;
const int inf = 1e9 + 7;
const int maxn = 1e5 + 5;
LL b[maxn], d[maxn];

LL query(int p) {
  LL res = 0;
  while (p > 0) {
    res = max(b[p], res);
    p -= p & -p;
  }
  return res;
}

void add(LL x, int p) {
  while (p < maxn) {
    b[p] = max(b[p], x);
    p += p & -p;
  }
}

LL w[maxn], h[maxn];
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
  int n;
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;

  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> w[i];
  }
  for (int i = 0; i < n; ++i) {
    d[i] = query(h[i]) + w[i];
    add(d[i], h[i]);
  }
  cout << *max_element(d, d + n) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long LL;
typedef unsigned int uii;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
typedef vector<int> vii;
const int inf = 1e9 + 7;
const int maxn = 2e5 + 5;
LL b[maxn], d[maxn];

LL query(int p) {
  LL res = 0;
  while (p > 0) {
    res = max(b[p], res);
    p -= p & -p;
  }
  return res;
}

void add(LL x, int p) {
  while (p < maxn) {
    b[p] = max(b[p], x);
    p += p & -p;
  }
}

LL w[maxn], h[maxn];
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
  int n;
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;

  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> w[i];
  }
  for (int i = 0; i < n; ++i) {
    d[i] = query(h[i]) + w[i];
    add(d[i], h[i]);
  }
  cout << *max_element(d, d + n) << endl;
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll segtree[500001], n, h[300001], a[300001];

void update(ll node, ll start, ll end, ll ix, ll val) {
  if (start == end) {
    segtree[node] = val;
  } else {
    ll mid = (start + end) / 2;
    if (ix <= mid)
      update(2 * node, start, mid, ix, val);
    else
      update(2 * node + 1, mid + 1, end, ix, val);
    segtree[node] = max(segtree[2 * node], segtree[2 * node + 1]);
  }
}

ll qmax(ll node, ll start, ll end, ll l, ll r) {
  ll mid = (start + end) / 2;
  if (r < start || end < l)
    return 0;
  else if (l <= start && end <= r)
    return segtree[node];
  else
    return max(qmax(2 * node, start, mid, l, r),
               qmax(2 * node + 1, mid + 1, end, l, r));
}

int main() {
  cin >> n;
  memset(segtree, 0, sizeof(segtree));
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    update(1, 1, n, h[i], qmax(1, 1, n, 1, h[i] - 1) + a[i]);
  }
  cout << segtree[1] << "\n";
}

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

ll segtree[1000001], n, h[300001], a[300001];

void update(ll node, ll start, ll end, ll ix, ll val) {
  if (start == end) {
    segtree[node] = val;
  } else {
    ll mid = (start + end) / 2;
    if (ix <= mid)
      update(2 * node, start, mid, ix, val);
    else
      update(2 * node + 1, mid + 1, end, ix, val);
    segtree[node] = max(segtree[2 * node], segtree[2 * node + 1]);
  }
}

ll qmax(ll node, ll start, ll end, ll l, ll r) {
  ll mid = (start + end) / 2;
  if (r < start || end < l)
    return 0;
  else if (l <= start && end <= r)
    return segtree[node];
  else
    return max(qmax(2 * node, start, mid, l, r),
               qmax(2 * node + 1, mid + 1, end, l, r));
}

int main() {
  cin >> n;
  memset(segtree, 0, sizeof(segtree));
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    update(1, 1, n, h[i], qmax(1, 1, n, 1, h[i] - 1) + a[i]);
  }
  cout << segtree[1] << "\n";
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define N 100005
typedef long long int ll;
ll beauty[N];
ll bit[N];
ll height[N];
void update(int i, int n, ll w) {
  int k = i;
  ll sum = 0;
  while (k <= n) {
    bit[k] = max(bit[k], w);
    k = k + (k & (-k));
  }
}

ll getsum(ll x) {
  ll sum = 0;
  while (x != 0) {
    sum = max(sum, bit[x]);
    x = x - (x & (-x));
  }
  return sum;
}

int main() {

  map<ll, ll> unique;
  map<ll, ll>::iterator it;

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> height[i];
  for (int i = 1; i <= n; i++)
    cin >> beauty[i];
  ll ct = 1;
  for (int i = 1; i <= n; i++) {
    unique[height[i]] = 0;
  }
  for (it = unique.begin(); it != unique.end(); it++) {

    if (it->second == 0) {
      it->second = ct;
      ct++;
    }
  }

  for (int i = 0; i <= ct; i++) {
    bit[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    ll max_sum = getsum(unique[height[i]] - 1);
    update(unique[height[i]], ct, max_sum + beauty[i]);
  }

  ll ans = getsum(ct);

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 200005
typedef long long int ll;
ll beauty[N];
ll bit[N];
ll height[N];
void update(int i, int n, ll w) {
  int k = i;
  ll sum = 0;
  while (k <= n) {
    bit[k] = max(bit[k], w);
    k = k + (k & (-k));
  }
}

ll getsum(ll x) {
  ll sum = 0;
  while (x != 0) {
    sum = max(sum, bit[x]);
    x = x - (x & (-x));
  }
  return sum;
}

int main() {

  map<ll, ll> unique;
  map<ll, ll>::iterator it;

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> height[i];
  for (int i = 1; i <= n; i++)
    cin >> beauty[i];
  ll ct = 1;
  for (int i = 1; i <= n; i++) {
    unique[height[i]] = 0;
  }
  for (it = unique.begin(); it != unique.end(); it++) {

    if (it->second == 0) {
      it->second = ct;
      ct++;
    }
  }

  for (int i = 0; i <= ct; i++) {
    bit[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    ll max_sum = getsum(unique[height[i]] - 1);
    update(unique[height[i]], ct, max_sum + beauty[i]);
  }

  ll ans = getsum(ct);

  cout << ans << endl;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int MAXN = 112345;
int n, m, k;
ll ft[MAXN];
ll get(int x) {
  ll ans = 0;
  for (; x; x -= x & -x) {
    ans = max(ans, ft[x]);
  }
  return ans;
}

void update(int x, ll v) {
  for (; x <= n; x += x & -x) {
    ft[x] = max(v, ft[x]);
  }
}

int h[MAXN], v[MAXN];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", h + i);
  }
  for (int i = 1; i <= n; ++i) {
    scanf("%d", v + i);
  }
  ll ans = 0, sum;
  for (int i = 1; i <= n; ++i) {
    sum = get(h[i]) + v[i];
    ans = max(ans, sum);
    update(h[i], sum);
  }
  printf("%lld\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int MAXN = 212345;
int n, m, k;
ll ft[MAXN];
ll get(int x) {
  ll ans = 0;
  for (; x; x -= x & -x) {
    ans = max(ans, ft[x]);
  }
  return ans;
}

void update(int x, ll v) {
  for (; x <= n; x += x & -x) {
    ft[x] = max(v, ft[x]);
  }
}

int h[MAXN], v[MAXN];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", h + i);
  }
  for (int i = 1; i <= n; ++i) {
    scanf("%d", v + i);
  }
  ll ans = 0, sum;
  for (int i = 1; i <= n; ++i) {
    sum = get(h[i]) + v[i];
    ans = max(ans, sum);
    update(h[i], sum);
  }
  printf("%lld\n", ans);
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
vector<ll> dp(N);
int main() {
  int n;
  cin >> n;
  int base = 1;
  while (base <= n) {
    base <<= 1;
  }
  vector<ll> tree(2 * N);
  int h[n], a[n];
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    int x = h[i] + base;
    ll best = 0;
    while (x > 1) {
      if (x % 2 == 1) {
        best = max(best, tree[x - 1]);
      }
      x >>= 1;
    }
    dp[h[i]] = best + a[i];
    for (int j = base + h[i]; j >= 1; j >>= 1) {
      tree[j] = max(tree[j], dp[h[i]]);
    }
  }
  cout << *max_element(dp.begin(), dp.end()) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
vector<ll> dp(N);
int main() {
  int n;
  cin >> n;
  int base = 1;
  while (base <= n) {
    base <<= 1;
  }
  vector<ll> tree(2 * base);
  int h[n], a[n];
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    int x = h[i] + base;
    ll best = 0;
    while (x > 1) {
      if (x % 2 == 1) {
        best = max(best, tree[x - 1]);
      }
      x >>= 1;
    }
    dp[h[i]] = best + a[i];
    for (int j = base + h[i]; j >= 1; j >>= 1) {
      tree[j] = max(tree[j], dp[h[i]]);
    }
  }
  cout << *max_element(dp.begin(), dp.end()) << endl;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const long long int N = 200000;
long long int dp[N + 1], h[N + 1], v[N + 1], bit[N + 1];
long long int sgtr[4 * N + 5];
int n;
void update(int s, int e, int node, int i, long long int y) {
  if (i < s || i > e)
    return;
  if (s == e && s == i) {
    sgtr[node] = y;
    return;
  }
  long long int mid = (s + e) / 2;
  if (i <= mid)
    update(s, mid, 2 * node + 1, i, y);
  else
    update(mid + 1, e, 2 * node + 2, i, y);
  sgtr[node] = max(sgtr[2 * node + 1], sgtr[2 * node + 2]);
  return;
}
long long int query(int s, int e, int node, int i, int j) {
  if (s >= i && e <= j)
    return sgtr[node];
  if (e < i || s > j)
    return -1;
  long long int mid = (s + e) / 2;
  long long int ans1 = query(s, mid, 2 * node + 1, i, j);
  long long int ans2 = query(mid + 1, e, 2 * node + 2, i, j);
  return max(ans1, ans2);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  freopen("intt.txt", "r", stdin);
  freopen("outt.txt", "w", stdout);

  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  for (int i = 1; i <= n; i++) {
    if (h[i] != 1)
      dp[h[i]] = query(1, n, 0, 1, h[i] - 1);
    dp[h[i]] += v[i];
    update(1, n, 0, h[i], dp[h[i]]);
  }
  long long int ans = 0;
  for (long long int i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long int N = 200000;
long long int dp[N + 1], h[N + 1], v[N + 1], bit[N + 1];
long long int sgtr[4 * N + 5];
int n;
void update(int s, int e, int node, int i, long long int y) {
  if (i < s || i > e)
    return;
  if (s == e && s == i) {
    sgtr[node] = y;
    return;
  }
  long long int mid = (s + e) / 2;
  if (i <= mid)
    update(s, mid, 2 * node + 1, i, y);
  else
    update(mid + 1, e, 2 * node + 2, i, y);
  sgtr[node] = max(sgtr[2 * node + 1], sgtr[2 * node + 2]);
  return;
}
long long int query(int s, int e, int node, int i, int j) {
  if (s >= i && e <= j)
    return sgtr[node];
  if (e < i || s > j)
    return -1;
  long long int mid = (s + e) / 2;
  long long int ans1 = query(s, mid, 2 * node + 1, i, j);
  long long int ans2 = query(mid + 1, e, 2 * node + 2, i, j);
  return max(ans1, ans2);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  for (int i = 1; i <= n; i++) {
    if (h[i] != 1)
      dp[h[i]] = query(1, n, 0, 1, h[i] - 1);
    dp[h[i]] += v[i];
    update(1, n, 0, h[i], dp[h[i]]);
  }
  long long int ans = 0;
  for (long long int i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans;
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int n;
const int nax = 1e5 + 10;
ll d[nax];

ll maxi(int k) {
  ll s = 0;
  while (k > 0) {
    s = max(d[k], s);
    k -= k & -k;
  }
  return s;
}

void add(int k, ll x) {
  while (k <= n) {
    d[k] = max(d[k], x);
    k += k & -k;
  }
}

int main() {
  cin >> n;
  ll h[n];
  ll b[n];
  for (int i = 0; i < n; ++i)
    cin >> h[i];
  for (int i = 0; i < n; ++i)
    cin >> b[i];
  for (int i = 0; i < n; ++i) {
    ll t = maxi(h[i] - 1);
    add(h[i], t + b[i]);
  }
  cout << maxi(n);
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int n;
const int nax = 2e5 + 10;
ll d[nax];

ll maxi(int k) {
  ll s = 0;
  while (k > 0) {
    s = max(d[k], s);
    k -= k & -k;
  }
  return s;
}

void add(int k, ll x) {
  while (k <= n) {
    d[k] = max(d[k], x);
    k += k & -k;
  }
}

int main() {
  cin >> n;
  ll h[n];
  ll b[n];
  for (int i = 0; i < n; ++i)
    cin >> h[i];
  for (int i = 0; i < n; ++i)
    cin >> b[i];
  for (int i = 0; i < n; ++i) {
    ll t = maxi(h[i] - 1);
    add(h[i], t + b[i]);
  }
  cout << maxi(n);
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define F first
#define S second
#define show(x) cout << #x << " " << x << " ";
#define f(i, j, k) for (int i = j; i <= k; i++)
#define fr(i, j, k) for (int i = j; i >= k; i--)
#define ll long long
#define all(A) A.begin(), A.end()
#define FIO                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
const int MSIZE = 1e5 + 5;
const int MOD = 1e9 + 7;

int arr[MSIZE], ht[MSIZE];
map<int, ll> m;
ll dp[MSIZE];

ll solve(int &n) {
  dp[0] = arr[0];
  m[ht[0]] = dp[0];
  f(i, 1, n - 1) {
    dp[i] = arr[i];
    auto it = m.lower_bound(ht[i]);
    if (it != m.begin())
      dp[i] += (--it)->second;
    m[ht[i]] = dp[i];
    it = m.upper_bound(ht[i]);
    while (it != m.end() && (it->second) <= dp[i]) {
      auto t = it;
      it++;
      m.erase(t);
    }
  }
  return *max_element(dp, dp + n);
}

int main() {

  FIO int n;
  cin >> n;
  f(i, 0, n - 1) cin >> ht[i];
  f(i, 0, n - 1) cin >> arr[i];
  cout << solve(n) << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define F first
#define S second
#define show(x) cout << #x << " " << x << " ";
#define f(i, j, k) for (int i = j; i <= k; i++)
#define fr(i, j, k) for (int i = j; i >= k; i--)
#define ll long long
#define all(A) A.begin(), A.end()
#define FIO                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
const int MSIZE = 2e5 + 5;
const int MOD = 1e9 + 7;

int arr[MSIZE], ht[MSIZE];
map<int, ll> m;
ll dp[MSIZE];

ll solve(int &n) {
  dp[0] = arr[0];
  m[ht[0]] = dp[0];
  f(i, 1, n - 1) {
    dp[i] = arr[i];
    auto it = m.lower_bound(ht[i]);
    if (it != m.begin())
      dp[i] += (--it)->second;
    m[ht[i]] = dp[i];
    it = m.upper_bound(ht[i]);
    while (it != m.end() && (it->second) <= dp[i]) {
      auto t = it;
      it++;
      m.erase(t);
    }
  }
  return *max_element(dp, dp + n);
}

int main() {

  FIO int n;
  cin >> n;
  f(i, 0, n - 1) cin >> ht[i];
  f(i, 0, n - 1) cin >> arr[i];
  cout << solve(n) << endl;
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)
typedef long long ll;

const int MAX_N = 2e5 + 1010;
const int def = 0;

// セグメント木を持つグローバル配列
ll n, dat[2 * MAX_N - 1];

// 初期化
void init(ll n_) {
  // 簡単のため、要素数を2のべき乗に
  n = 1;
  while (n < n_)
    n *= 2;

  // 全ての値をINT_MAXに
  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = def;
}

// k番目の値(0-indexed)をaに変更
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]);
  }
}

// [a,b)の最小値を求める
// 後ろの方の引数は、計算の簡単のための引数
// kは節点の番号、1,rはその接点が[1,r)に対応づいていることを表す
// したがって、外からはquery(a, b, 0, 0, n)として呼ぶ
ll query(ll a, ll b, ll k = 0, ll l = 0, ll r = n) {
  //[a,b)と[l,r)が交差しなければ、INT_MAX
  if (r <= a || b <= l)
    return def;

  //[a,b)が[l,r)を完全に含んでいれば、この節点の値
  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() {
  ll N;
  cin >> N;
  vector<ll> h(N), a(N);
  rep(i, 0, N) cin >> h[i];
  rep(i, 0, N) cin >> a[i];

  init(N);
  rep(i, 0, N) { update(h[i] - 1, query(0, h[i] - 1) + a[i]); }
  cout << query(0, N) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)
typedef long long ll;

const int MAX_N = 1 << 18;
const int def = 0;

// セグメント木を持つグローバル配列
ll n, dat[2 * MAX_N - 1];

// 初期化
void init(ll n_) {
  // 簡単のため、要素数を2のべき乗に
  n = 1;
  while (n < n_)
    n *= 2;

  // 全ての値をINT_MAXに
  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = def;
}

// k番目の値(0-indexed)をaに変更
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]);
  }
}

// [a,b)の最小値を求める
// 後ろの方の引数は、計算の簡単のための引数
// kは節点の番号、1,rはその接点が[1,r)に対応づいていることを表す
// したがって、外からはquery(a, b, 0, 0, n)として呼ぶ
ll query(ll a, ll b, ll k = 0, ll l = 0, ll r = n) {
  //[a,b)と[l,r)が交差しなければ、INT_MAX
  if (r <= a || b <= l)
    return def;

  //[a,b)が[l,r)を完全に含んでいれば、この節点の値
  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() {
  ll N;
  cin >> N;
  vector<ll> h(N), a(N);
  rep(i, 0, N) cin >> h[i];
  rep(i, 0, N) cin >> a[i];

  init(N);
  rep(i, 0, N) { update(h[i] - 1, query(0, h[i] - 1) + a[i]); }
  cout << query(0, N) << endl;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define PB emplace_back
#define mid ((be + en) / 2)
#define cl c * 2
#define cr cl + 1

using namespace std;

const int N = 1e5 + 100;

long long int mx[N], dp[N], h[N], a[N], n;

long long int que(int l, int r, int be = 0, int en = n, int c = 1) {
  if (r <= be || en <= l)
    return 0;
  if (l <= be && en <= r)
    return mx[c];
  return max(que(l, r, be, mid, cl), que(l, r, mid, en, cr));
}

long long int up(int l, long long int val, int be = 0, int en = n, int c = 1) {
  if (be + 1 == en)
    mx[c] = val;
  else if (l < mid)
    up(l, val, be, mid, cl);
  else
    up(l, val, mid, en, cr);
  if (be + 1 != en)
    mx[c] = max(mx[cl], mx[cr]);
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    long long int xm = que(0, h[i]);
    up(h[i], xm + a[i]);
  }
  cout << que(0, *max_element(a, a + n) + 1);
}


Fixed submission:
#include <bits/stdc++.h>

#define PB emplace_back
#define mid ((be + en) / 2)
#define cl c * 2
#define cr cl + 1

using namespace std;

const int N = (1e5 + 100) * 16;

long long int mx[N], dp[N], h[N], a[N], n;

long long int que(int l, int r, int be = 0, int en = n, int c = 1) {
  if (r <= be || en <= l)
    return 0;
  if (l <= be && en <= r)
    return mx[c];
  return max(que(l, r, be, mid, cl), que(l, r, mid, en, cr));
}

long long int up(int l, long long int val, int be = 0, int en = n, int c = 1) {
  if (be + 1 == en)
    mx[c] = val;
  else if (l < mid)
    up(l, val, be, mid, cl);
  else
    up(l, val, mid, en, cr);
  if (be + 1 != en)
    mx[c] = max(mx[cl], mx[cr]);
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    long long int xm = que(0, h[i]);
    up(h[i], xm + a[i]);
  }
  cout << que(0, *max_element(a, a + n) + 1);
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<ll, ll>
#define sorted(a_1) sort(a_1.begin(), a_1.end())
#define rsorted(a_1) sort(a_1.rbegin(), a_1.rend())
#define t1(a_1) cout << a_1 << endl;
#define t2(a_1)                                                                \
  for (auto it_test : a_1)                                                     \
    cout << it_test << " ";

ll bittree[6];
void update(ll index, ll n, ll val) {
  for (ll i = index; i <= n;) {
    bittree[i] = max(bittree[i], val);
    i += i & (-i);
  }
}

ll get(ll u, ll n) {
  ll index = u;
  ll sum = 0;
  for (ll i = index; i > 0;) {
    sum = max(bittree[i], sum);
    i -= i & (-i);
  }
  return sum;
}

void solve() {
  ll n;
  cin >> n;
  vector<ll> height(n), val(n);
  vector<pair<ll, ll>> pair;
  for (ll i = 0; i < n; i++) {
    cin >> height[i];
    pair.pb({height[i], i});
  }
  for (auto &i : val)
    cin >> i;
  sorted(pair);
  memset(bittree, 0, sizeof(bittree));
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ll res = val[pair[i].second] + get(pair[i].second + 1, n);
    ans = max(ans, res);
    update(pair[i].second + 1, n, res);
  }
  ans = max(ans, get(n, n));
  // t2(bittree)
  // t1(endl)
  t1(ans);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<ll, ll>
#define sorted(a_1) sort(a_1.begin(), a_1.end())
#define rsorted(a_1) sort(a_1.rbegin(), a_1.rend())
#define t1(a_1) cout << a_1 << endl;
#define t2(a_1)                                                                \
  for (auto it_test : a_1)                                                     \
    cout << it_test << " ";

ll bittree[1000001];
void update(ll index, ll n, ll val) {
  for (ll i = index; i <= n;) {
    bittree[i] = max(bittree[i], val);
    i += i & (-i);
  }
}

ll get(ll u, ll n) {
  ll index = u;
  ll sum = 0;
  for (ll i = index; i > 0;) {
    sum = max(bittree[i], sum);
    i -= i & (-i);
  }
  return sum;
}

void solve() {
  ll n;
  cin >> n;
  vector<ll> height(n), val(n);
  vector<pair<ll, ll>> pair;
  for (ll i = 0; i < n; i++) {
    cin >> height[i];
    pair.pb({height[i], i});
  }
  for (auto &i : val)
    cin >> i;
  sorted(pair);
  memset(bittree, 0, sizeof(bittree));
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ll res = val[pair[i].second] + get(pair[i].second + 1, n);
    ans = max(ans, res);
    update(pair[i].second + 1, n, res);
  }
  ans = max(ans, get(n, n));
  // t2(bittree)
  // t1(endl)
  t1(ans);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}