Problem:
引越し
太郎君は引っ越しをすることになりました。太郎君はたくさんの荷物を持っているので、荷物の運搬を引っ越し業者に頼むことにしました。荷物はいろいろな重さの物があるので、わかりやすいように軽い方から順番に並べて置いてもらうように頼みましたが、引っ越し業者の人はばらばらの順番で荷物を置いていってしまいました。そこで太郎君は荷物を並べ替えようとしましたが、荷物は重いので運ぶのには体力が必要です。それぞれの荷物は今ある場所から他の荷物の間や荷物の端など好きな場所に運ぶことができますが、ある荷物を運ぶにはその荷物の重さと同じだけ体力を使います。太郎君はあまり体力がないので、できるだけ体力を使わずに荷物を軽い方から順番に並べる方法を考えることにしました。
Input
n
x
1
x
2
...
x
n
n
は太郎君の持っている荷物の数を表す
x
1
から
x
n
はそれぞれの荷物の重さを表し、現在は
x
1
、
x
2
、…、
x
n
の順に並んでいる
Constraints
1 ≤ n ≤ 10
5
1 ≤ x
i
≤ n (1 ≤ i ≤ n)
x
i
≠ x
j
(
1 ≤ i, j ≤ n
かつ
i ≠ j
)
入力はすべて整数で与えられる
Output
S
荷物を軽い方から順番に並べるのに必要な最小の体力の合計
S
を出力せよ、ただし最後に改行を出力せよ
Sample Input 1
4
1 4 2 3
Output for the Sample Input 1
4
重さ4の荷物を右端に運ぶと重さの軽い順になります
Sample Input 2
5
1 5 3 2 4
Output for the Sample Input 2
7
重さ2の荷物を重さ1の荷物の右側に運び、重さ5の荷物を右端に運ぶと重さの軽い順になります
Sample Input 3
7
1 2 3 4 5 6 7
Output for the Sample Input 3
0
最初から重さの軽い順に並んでいます
Sample Input 4
8
6 2 1 3 8 5 4 7
Output for the Sample Input 4
19

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

// base: bafcf8
unsigned int bit_ceil(unsigned int n) {
   unsigned int x = 1;
   while(x < (unsigned int)(n)) x *= 2;
   return x;
}
int countr_zero(unsigned int n) { return __builtin_ctz(n); }
constexpr int countr_zero_constexpr(unsigned int n) {
   int x = 0;
   while(!(n & (1 << x))) x++;
   return x;
}
template<class S, S (*op)(S, S), S (*e)()> struct segtree {
   public:
   segtree() : segtree(0) {}
   explicit segtree(int n) : segtree(vector<S>(n, e())) {}
   explicit segtree(const vector<S>& v) : _n(int(v.size())) {
      size = (int)bit_ceil((unsigned int)(_n));
      log = countr_zero((unsigned int)size);
      d = vector<S>(2 * size, e());
      for(int i = 0; i < _n; i++) d[size + i] = v[i];
      for(int i = size - 1; i >= 1; i--) { update(i); }
   }

   void set(int p, S x) {
      p += size;
      d[p] = x;
      for(int i = 1; i <= log; i++) update(p >> i);
   }

   S get(int p) const { return d[p + size]; }

   S prod(int l, int r) const {
      S sml = e(), smr = e();
      l += size;
      r += size;

      while(l < r) {
         if(l & 1) sml = op(sml, d[l++]);
         if(r & 1) smr = op(d[--r], smr);
         l >>= 1;
         r >>= 1;
      }
      return op(sml, smr);
   }

   S all_prod() const { return d[1]; }

   template<class F> int max_right(int l, F f) {
      if(l == _n) return _n;
      l += size;
      S sm = e();
      do {
         while(l % 2 == 0) l >>= 1;
         if(!f(op(sm, d[l]))) {
            while(l < size) {
               l = (2 * l);
               if(f(op(sm, d[l]))) {
                  sm = op(sm, d[l]);
                  l++;
               }
            }
            return l - size;
         }
         sm = op(sm, d[l]);
         l++;
      } while((l & -l) != l);
      return _n;
   }  // faa03f

   template<class F> int min_left(int r, F f) {
      if(r == 0) return 0;
      r += size;
      S sm = e();
      do {
         r--;
         while(r > 1 && (r % 2)) r >>= 1;
         if(!f(op(d[r], sm))) {
            while(r < size) {
               r = (2 * r + 1);
               if(f(op(d[r], sm))) {
                  sm = op(d[r], sm);
                  r--;
               }
            }
            return r + 1 - size;
         }
         sm = op(d[r], sm);
      } while((r & -r) != r);
      return 0;
   }  // efa466

   private:
   int _n, size, log;
   vector<S> d;

   void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

ll op(ll a, ll b) {
    return max(a, b);
}
ll e() {
    return 0LL;
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n; cin >> n;
    segtree<ll, op, e> seg(n + 1);
    rep(i, n) {
        ll x; cin >> x;
        seg.set(x, seg.prod(0, x) + x);
    }
    cout << n * (n + 1) / 2 - seg.all_prod() << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 5976
final_rank: 2

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

template <typename M, typename N, M (*f)(M, M), M (*g)(M, N), M (*m1)()>
struct SegmentTree {
    int sz, n;
    vector<M> data;
    SegmentTree(int _n = 0) : n(_n) {
        sz = 1;
        while (sz < _n)
            sz <<= 1;
        data.assign(2 * sz, m1());
    }
    void run(vector<M> &v) {
        for (int i = 0; i < (int)v.size(); i++)
            data[i + sz] = v[i];
        for (int k = sz - 1; k > 0; k--)
            data[k] = f(data[2 * k], data[2 * k + 1]);
    }
    void set(int k, const M &x) {
        k += sz;
        data[k] = x;
        while (k >>= 1)
            data[k] = f(data[2 * k], data[2 * k + 1]);
    }
    void update(int k, const N &x) {
        k += sz;
        data[k] = g(data[k], x);
        while (k >>= 1)
            data[k] = f(data[2 * k], data[2 * k + 1]);
    }
    M query(int a, int b) {
        M L = m1(), R = m1();
        for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if (a & 1)
                L = f(L, data[a++]);
            if (b & 1)
                R = f(data[--b], R);
        }
        return f(L, R);
    }
    M operator[](const int &k) const {
        return data[k + sz];
    }
    vector<M> get() {
        return {data.begin() + sz, data.begin() + sz + n};
    }
    template <class F> int max_right(int L, F ch) const {
        int l = sz + L, w = 1;
        M ansL = m1();
        for (; L + w <= sz; l >>= 1, w <<= 1)
            if (l & 1) {
                if (not ch(f(ansL, data[l])))
                    break;
                ansL = f(ansL, data[l++]);
                L += w;
            }
        while (l <<= 1, w >>= 1) {
            if (L + w <= sz && ch(f(ansL, data[l]))) {
                ansL = f(ansL, data[l++]);
                L += w;
            }
        }
        return L;
    }
    template <class F> int min_left(int R, F ch) const {
        int r = sz + R, w = 1;
        M ansR = m1();
        for (; R - w >= 0; r >>= 1, w <<= 1)
            if (r & 1) {
                if (not ch(f(data[r - 1], ansR)))
                    break;
                ansR = f(data[--r], ansR);
                R -= w;
            }
        while (r <<= 1, w >>= 1) {
            if (R - w >= 0 && ch(f(data[r - 1], ansR))) {
                ansR = f(data[--r], ansR);
                R -= w;
            }
        }
        return R;
    }
};

/**
 * @brief Segment Tree
 */

ll f(ll A, ll B) {return max(A,B);}

ll g(ll A, ll B) {return B;}

ll e() {return 0;}

int main() {
    ll N;
    cin >> N;
    vector<ll> A(N);
    rep(i,0,N) cin >> A[i];
    SegmentTree<ll,ll,f,g,e> Seg(N+1);
    rep(i,0,N) {
        Seg.update(A[i], Seg.query(0,A[i])+A[i]);
    }
    cout << N * (N + 1) / 2 - Seg.query(0,N+1) << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 6068
final_rank: 8

code_snippet: // https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2431&lang=jp
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define rrep(i, a, n) for (int i = a; i >= n; i--)
#define inr(l, x, r) (l <= x && x < r)
#define ll long long
#define ld long double

constexpr int IINF = 1001001001;
constexpr ll INF = 9e18;

template <class t, class u>
void chmax(t& a, u b) {
    if (a < b) a = b;
}
template <class t, class u>
void chmin(t& a, u b) {
    if (b < a) a = b;
}

template <typename X>
struct SegTree {
    using FX = function<X(X, X)>;  // X•X -> X となる関数の型
    int n;
    FX fx;
    const X ex;
    vector<X> dat;
    SegTree(int n_, FX fx_, X ex_) : n(), fx(fx_), ex(ex_), dat(n_ * 4, ex_) {
        int x = 1;
        while (n_ > x) {
            x *= 2;
        }
        n = x;
    }
    void set(int i, X x) { dat[i + n - 1] = x; }
    void build() {
        for (int k = n - 2; k >= 0; k--)
            dat[k] = fx(dat[2 * k + 1], dat[2 * k + 2]);
    }
    void update(int i, X x) {
        i += n - 1;
        dat[i] = x;
        while (i > 0) {
            i = (i - 1) / 2;  // parent
            dat[i] = fx(dat[i * 2 + 1], dat[i * 2 + 2]);
        }
    }
    X query(int a, int b) { return query_sub(a, b, 0, 0, n); }
    X query_sub(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) {
            return ex;
        } else if (a <= l && r <= b) {
            return dat[k];
        } else {
            X vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
            X vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
            return fx(vl, vr);
        }
    }
};

// using S = ll;
auto fx = [](ll x1, ll x2) -> ll { return max(x1, x2); };
ll ex = 0;

int main() {
    int n; cin >> n;
    vector<int> x(n);
    rep(i, 0, n) cin >> x[i];
    SegTree<ll> seg(n + 1, fx, ex);
    ll sum = 0;
    rep(i, 0, n) {
        sum += x[i];
        seg.update(x[i], seg.query(0, x[i]) + x[i]);
    }
    cout << sum - seg.query(0, n + 1) << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 6724
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
ll x;
struct Segtree{
    int n2;
    ll dat[300030];
    Segtree(int n){
        n2 = 1;
        while(n2<n) n2*=2;
        memset(dat, 0, sizeof(dat));
    }
    ll update(int a){
        ll res = query(0, a, 0, 0, n2) + a;
        int k = a+n2-1;
        dat[k] = res;
        while(k>0){
            k = (k-1)/2;
            dat[k] = max(dat[k*2+1], dat[k*2+2]);
        }
        return res;
    }
    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+1, r);
        return max(vl, vr);
    }
};
int main(){
    cin >> n;
    Segtree seg(n+1);
    ll sum=0, ma=0;
    for(int i=0;i<n;i++){
        cin >> x;
        sum += x;
        ma = max(ma, seg.update(x));
    }
    cout << sum-ma << endl;
    return(0);
}
accuracy: 1
time_ms: 30
memory_kb: 5460
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
ll x;
struct Segtree{
    int n2;
    ll dat[300030];
    Segtree(int n){
        n2 = 1;
        while(n2<n) n2*=2;
        memset(dat, 0, sizeof(dat));
    }
    ll update(int a, int r){
        ll res = query(0, a, 0, 0, n2) + a;
        int k = a+n2-1;
        dat[k] = res;
        while(k>0){
            k = (k-1)/2;
            dat[k] = max(dat[k*2+1], dat[k*2+2]);
        }
        return res;
    }
    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+1, r);
        return max(vl, vr);
    }
};
int main(){
    cin >> n;
    Segtree seg(n+1);
    ll sum=0, ma=0;
    for(int i=0;i<n;i++){
        cin >> x;
        sum += x;
        ma = max(ma, seg.update(x, n));
    }
    cout << sum-ma << endl;
    return(0);
}
accuracy: 1
time_ms: 30
memory_kb: 5464
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;
int n, x;
struct Segtree{
    int n2;
    int dat[300030];
    Segtree(int n){
        n2 = 1;
        while(n2<n) n2*=2;
        memset(dat, 0, sizeof(dat));
    }
    int update(int a, int r){
        int res = query(0, a, 0, 0, n2) + a;
        int k = a+n2-1;
        dat[k] = res;
        while(k>0){
            k = (k-1)/2;
            dat[k] = max(dat[k*2+1], dat[k*2+2]);
        }
        return res;
    }
    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];
        int vl = query(a, b, k*2+1, l, (l+r)/2);
        int vr = query(a, b, k*2+2, (l+r)/2+1, r);
        return max(vl, vr);
    }
};
int main(){
    cin >> n;
    Segtree seg(n+1);
    int sum=0, ma=0;
    for(int i=0;i<n;i++){
        cin >> x;
        sum += x;
        ma = max(ma, seg.update(x, n));
    }
    cout << sum-ma << endl;
    return(0);
}
accuracy: 0.10256410256410256
time_ms: 20
memory_kb: 4288
final_rank: 18

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

template<class T>
bool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0;}

template<class T>
bool chmax(T &a, const T &b) {if (a < b) {a = b; return 1;} return 0;}

using ll = long long;
using ld = long double;

bool is_end = false;

struct SegmentTree
{
	int sz;
	vector<ll> seg;
	
	SegmentTree() = default;
	SegmentTree(int n)
	{
		sz = 1;
		while (sz < n) {sz <<= 1;}
		seg.assign(sz * 2, 0);
	}
	
	void update(int k, ll x)
	{
		k += sz;
		seg[k] = x;
		while (k >>= 1)
		{
			seg[k] = max(seg[2*k+0], seg[2*k+1]);
		}
		return;
	}
	
	ll query(int a, int b)
	{
		ll L = 0, R = 0;
		for (a += sz, b += sz; a < b; a >>= 1, b >>= 1)
		{
			if (a & 1) L = max(L, seg[a++]);
			if (b & 1) R = max(seg[--b], R);
		}
		return max(L, R);
	}
	
};


void calc()
{
	ll N; cin >> N;
	SegmentTree seg(N+1);
	for (int i = 0; i < N; ++i)
	{
		int a; cin >> a;
		ll mx = seg.query(0, a);
		seg.update(a, mx + a);
	}
	
	ll res = N * (N+1) / 2;
	res -= seg.query(0, N+1);
	cout << res << endl;
	
	return;
}

int main()
{
	calc();
	
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 5204
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define REP(i, m, n) for (int i = m; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define dbg(x) cerr << #x << ":" << x << endl;
#define all(v) v.begin(), v.end()

constexpr ll LINF = 1e18;

template <typename T, typename F>
struct SegmentTree {
  int n, sz;
  vector<T> seg;

  const F f;
  const T ti;

  SegmentTree() = default;

  explicit SegmentTree(int n, const F f, const T &ti) : n(n), f(f), ti(ti) {
    sz = 1;
    while (sz < n) sz <<= 1;
    seg.assign(2 * sz, ti);
  }

  explicit SegmentTree(const vector<T> &v, const F f, const T &ti)
      : SegmentTree((int)v.size(), f, ti) {
    build(v);
  }

  void build(const vector<T> &v) {
    assert(n == (int)v.size());
    for (int k = 0; k < n; k++) seg[k + sz] = v[k];
    for (int k = sz - 1; k > 0; k--) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  void set(int k, const T &x) {
    k += sz;
    seg[k] = x;
    while (k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  T get(int k) const { return seg[k + sz]; }

  T operator[](const int &k) const { return get(k); }

  void apply(int k, const T &x) {
    k += sz;
    seg[k] = f(seg[k], x);
    while (k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  T prod(int l, int r) const {
    T L = ti, R = ti;
    for (l += sz, r += sz; l < r; l >>= 1, r >>= 1) {
      if (l & 1) L = f(L, seg[l++]);
      if (r & 1) R = f(seg[--r], R);
    }
    return f(L, R);
  }

  T all_prod() const { return seg[1]; }
};

template <typename T, typename F>
SegmentTree<T, F> get_segment_tree(int N, const F &f, const T &ti) {
  return SegmentTree{N, f, ti};
}

/*
template <typename T, typename F>
SegmentTree<T, F> get_segment_tree(const vector<T> &v, const F &f,
                                   const T &ti) {
  return SegmentTree{v, f, ti};
}
*/

// ll op(ll a, ll b) { return max(a, b); }

int main() {
  int n;
  cin >> n;
  vector<ll> x(n);
  rep(i, n) cin >> x[i];

  auto f = [&](ll a, ll b) { return max(a, b); };
  auto seg = get_segment_tree(n + 1, f, 0LL);

  rep(i, n) {
    ll sum = seg.prod(0, x[i]);
    seg.set(x[i], sum + x[i]);
  }
  cout << accumulate(all(x), 0LL) - seg.all_prod() << "\n";
}
accuracy: 1
time_ms: 20
memory_kb: 6012
final_rank: 7

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

namespace zawa {

template <class monoid>
class segmentTree {
private:
	using V = typename monoid::valueType;
	std::size_t N;
	std::vector<V> dat;

public:
	segmentTree() {}
	segmentTree(int _N) : N(_N), dat(2 * _N, monoid::identity) {}
	segmentTree(const std::vector<V>& A) : N(A.size()), dat(2 * N, monoid::identity) {
		for (std::size_t i = 0 ; i < A.size() ; i++) {
			dat[i + N] = A[i];
		}
		for (std::size_t i = N - 1 ; i > 0 ; i--) {
			dat[i] = monoid::operation(dat[i << 1], dat[i << 1 | 1]);		
		}
	}

	void set(std::size_t pos, const V& value) {
		pos += N;
		dat[pos] = value;
		while (pos >>= 1) {
			dat[pos] = monoid::operation(dat[pos << 1], dat[pos << 1 | 1]);
		}
	}

	V update(std::size_t pos, const V& value) {
		pos += N;
		do {
			dat[pos] = monoid::operation(dat[pos], value);
		} while (pos >>= 1);
		return dat[pos];
	}

	V prod(std::size_t l, std::size_t r) const {
		V left = monoid::identity, right = monoid::identity;
		for (l += N, r += N ; l < r ; l >>= 1, r >>= 1) {
			if (l & 1) {
				left = monoid::operation(left, dat[l++]);	
			}
			if (r & 1) {
				right = monoid::operation(dat[--r], right);
			}
		}
		return monoid::operation(left, right);
	}

	template <class functionType>
	int maxRight(int l, const functionType& f) const {
		int L = l + N, w = 1;
		V v = monoid::identity;
		for ( ; l + w <= (int)N ; L >>= 1, w <<= 1) {
			if (L & 1) {
			   	if (f(monoid::operation(v, dat[L]))) {
					v = monoid::operation(v, dat[L++]);
					l += w;
				}
				else {
					break;
				}
			}
		}
		while (L <<= 1, w >>= 1) {
			if (l + w <= (int)N and f(monoid::operation(v, dat[L]))) {
				v = monoid::operation(v, dat[L++]);
				l += w;
			}
		}
		return l;
	}

	template <class functionType>
	int minLeft(int r, const functionType& f) const {	
		int R = r + N, w = 1;
		V v = monoid::identity;
		for ( ; r - w >= 0 ; R >>= 1, w <<= 1) {
			if (R & 1) {
				if (f(monoid::operation(dat[R - 1], v))) {
					v = monoid::operation(dat[--R], v);
					r -= w;
				}
				else {
					break;
				}
			}
		}
		while (R <<= 1, w >>= 1) {
			if (r - w >= 0 and f(monoid::operation(dat[R - 1], v))) {
				v = monoid::operation(dat[--R], v);
				r -= w;
			}
		}
		return r;
	}	
};

} // namespace zawa

namespace zawa {

template <class T>
struct maxMonoid {
	using valueType = T;
	static constexpr valueType identity = std::numeric_limits<valueType>::min();
	static valueType operation(const valueType& a, const valueType& b) {
		return std::max(a, b);
	}
};

};

int main() {
    int N; cin >> N;
    vector<int> A(N);
    for (auto& a : A) cin >> a;

    vector<ll> B(N);
    zawa::segmentTree<zawa::maxMonoid<ll>> S(B);

    for (int i = 0 ; i < N ; i++) {
        ll v = S.prod(0, A[i]);
        B[A[i] - 1] = v + A[i];
        S.set(A[i] - 1, B[A[i] - 1]);
    }

    cout << (ll)N * (N + 1) / 2 - S.prod(0, N) << endl;

    return 0;
    
}
accuracy: 1
time_ms: 20
memory_kb: 5752
final_rank: 5

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

int main() {
    int N; cin >> N;
    vector<int> A(N);
    for (auto& a : A) cin >> a;
    const int INF = (int)2e9;
    vector<int> dp(N + 1, INF);
    vector<map<int, ll>> dp2(N + 1);

    for (auto a : A) {
        auto it = lower_bound(dp.begin(), dp.end(), a);
        if (it == dp.begin()) {
            dp2[0][a] = a;
        }
        else {
            int pos = it - dp.begin() - 1;
            auto it2 = dp2[pos].lower_bound(a);
            if (it2 == dp2[pos].begin()) {
                dp2[pos + 1][a] = (ll)a;
            }
            else {
                it2--;
                dp2[pos + 1][a] = it2->second + a;
            }
        }
        *it = a;
    }

    ll val = 0LL;
    for (auto& d : dp2) {
        for (auto& [_, sum] : d) val = max(val, sum);
    }

    ll ans = (ll)N * (N + 1) / 2 - val;

    cout << ans << endl;
}
accuracy: 0.23076923076923078
time_ms: 30
memory_kb: 14896
final_rank: 17

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

int main() {
    int N; cin >> N;
    vector<int> A(N);
    for (auto& a : A) cin >> a;
    const int INF = (int)2e9;
    vector<int> dp(N + 1, INF);
    vector<map<int, ll>> dp2(N + 1);

    for (auto a : A) {
        auto it = upper_bound(dp.begin(), dp.end(), a);
        if (it == dp.begin()) {
            dp2[0][a] = a;
        }
        else {
            int pos = it - dp.begin() - 1;
            auto it2 = dp2[pos].upper_bound(a);
            if (it2 == dp2[pos].begin()) {
                dp2[pos + 1][a] = max(dp2[pos + 1][a], (ll)a);
            }
            else {
                it2--;
                dp2[pos + 1][a] = max(dp2[pos + 1][a], it2->second + a);
            }
        }
        *it = a;
    }

    ll val = 0LL;
    for (auto& d : dp2) {
        for (auto& [_, sum] : d) val = max(val, sum);
    }

    ll ans = (ll)accumulate(A.begin(), A.end(), 0LL) - val;

    cout << ans << endl;
}
accuracy: 0.23076923076923078
time_ms: 30
memory_kb: 14852
final_rank: 16

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

namespace segtree_impl {

namespace internal {

// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
    int x = 0;
    while((1U << x) < (unsigned int)(n)) x++;
    return x;
}

// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
constexpr int bsf_constexpr(unsigned int n) {
    int x = 0;
    while(!(n & (1 << x))) x++;
    return x;
}

// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}

} // namespace internal

} // namespace segtree_impl

namespace segtree_impl {

template <class S, S (*op)(S, S), S (*e)()> struct segtree {
  public:
    segtree() : segtree(0) {}
    explicit segtree(int n) : segtree(std::vector<S>(n, e())) {}
    explicit segtree(const std::vector<S> &v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        for(int i = 0; i < _n; i++) d[size + i] = v[i];
        for(int i = size - 1; i >= 1; i--) { update(i); }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for(int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) const {
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    S operator[](int k) const { return get(k); }

    S prod(int l, int r) const {
#ifdef ONLINE_JUDGE
        if(l < 0) l = 0;
        if(r > _n) r = _n;
        if(l > r) return e();
#endif
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;

        while(l < r) {
            if(l & 1) sml = op(sml, d[l++]);
            if(r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() const { return d[1]; }

    template <bool (*f)(S)> int max_right(int l) const {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) const {
#ifdef ONLINE_JUDGE
        if(l < 0) l = 0;
        if(l > _n) l = _n;
#endif
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if(l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while(l % 2 == 0) l >>= 1;
            if(!f(op(sm, d[l]))) {
                while(l < size) {
                    l = (2 * l);
                    if(f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while((l & -l) != l);
        return _n;
    }

    template <bool (*f)(S)> int min_left(int r) const {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) const {
#ifdef ONLINE_JUDGE
        if(r < 0) r = 0;
        if(r > _n) r = _n;
#endif
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if(r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while(r > 1 && (r % 2)) r >>= 1;
            if(!f(op(d[r], sm))) {
                while(r < size) {
                    r = (2 * r + 1);
                    if(f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while((r & -r) != r);
        return 0;
    }

    friend std::ostream &operator<<(std::ostream &os, segtree &ls) {
        os << "{";
        for(int i = 0; i < ls._n; i++) os << ls.get(i) << (i == ls._n - 1 ? "" : ", ");
        return os << "}";
    }

  private:
    int _n, size, log;
    std::vector<S> d;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

template <class T> constexpr T inf = std::numeric_limits<T>::max() / 2;
template <class T, class U> constexpr std::pair<T, U> inf<std::pair<T, U>> = {inf<T>, inf<U>};
template <typename T> T min_func(T x, T y) { return min(x, y); }
template <typename T> T max_func(T x, T y) { return max(x, y); }
template <typename T> T plus_func(T x, T y) { return x + y; }
template <typename T, int ID> T set_func(T x, T y) { return (y == ID ? x : y); }
template <typename T> T min_e() { return inf<T>; }
template <typename T> T max_e() { return -inf<T>; }
template <typename T> T plus_e() { return T(); }

template <typename T> using RST = segtree<T, plus_func<T>, plus_e<T>>;
template <typename T> using RmT = segtree<T, min_func<T>, min_e<T>>;
template <typename T> using RMT = segtree<T, max_func<T>, max_e<T>>;

template <typename T> struct mMinfo { T mi, ma; };
template <typename T> mMinfo<T> mM_func(mMinfo<T> x, mMinfo<T> y) { return mMinfo{min(x.mi, y.mi), max(x.ma, y.ma)}; };
template <typename T> mMinfo<T> mM_e() { return {inf<T>, -inf<T>}; }

template <typename T> using RmMT = segtree<mMinfo<T>, mM_func<T>, mM_e<T>>;

} // namespace segtree_impl
using segtree_impl::RmT;
using segtree_impl::RMT;
using segtree_impl::RST;
using segtree_impl::segtree;

int main() {
    int n;
    cin >> n;
    vector<long long> a(n);
    for(auto &e : a) cin >> e;
    RMT<long long> seg(n + 1);
    seg.set(0, 0);
    for(auto &e : a) { seg.set(e, seg.prod(0, e) + e); }
    cout << (1LL * n * (n + 1) / 2 - seg.all_prod()) << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 6780
final_rank: 10

code_snippet: #include<bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<n;i++)
#define all(a) a.begin(),a.end()
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const ll inf=1e18;
struct RMQ{
    vector<ll>node;
    int n;
    RMQ(int N){
        n=1;
        while(n<N)n*=2;
        node.resize(2*n,-inf);
    }
    void upd(int i,ll x){
        i+=n-1;
        node[i]=x;
        while(i>0){
            i=(i-1)/2;
            node[i]=max(node[i*2+1],node[i*2+2]);
        }
    }
    ll query(int a,int b,int k,int l,int r){
        if(b<=l||r<=a)return -inf;
        if(a<=l&&r<=b)return node[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 get(int L,int R){
        return query(L,R,0,0,n);
    }
};
ll p[100005];
int main(){
    ll n;cin>>n;
    rep(i,n)cin>>p[i];
    RMQ segtree(n+1);
    segtree.upd(0,0);
    rep(i,n){
        segtree.upd(p[i],p[i]+segtree.get(0,p[i]));
    }
    cout<<n*(n+1)/2-segtree.get(0,n+1)<<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 6008
final_rank: 6

code_snippet: #include<bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<n;i++)
#define all(a) a.begin(),a.end()
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const ll inf=1e18;
struct RMQ{
    vector<ll>node;
    int n;
    RMQ(int N){
        n=1;
        while(n<N)n*=2;
        node.resize(2*n,-inf);
    }
    void upd(int i,ll x){
        i+=n-1;
        node[i]=x;
        while(i>0){
            i=(i-1)/2;
            node[i]=max(node[i*2+1],node[i*2+2]);
        }
    }
    ll query(int a,int b,int k,int l,int r){
        if(b<=l||r<=a)return -inf;
        if(a<=l&&r<=b)return node[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 get(int L,int R){
        return query(L,R,0,0,n);
    }
};
ll p[100005];
int main(){
    int n;cin>>n;
    rep(i,n)cin>>p[i];
    RMQ segtree(n+1);
    segtree.upd(0,0);
    rep(i,n){
        segtree.upd(p[i],p[i]+segtree.get(0,p[i]));
    }
    cout<<n*(n+1)/2-segtree.get(0,n+1)<<endl;
}
accuracy: 0.10256410256410256
time_ms: 20
memory_kb: 6000
final_rank: 20

code_snippet: #define _USE_MATH_DEFINES
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using H = pair<ll, ll>;
using P = pair<ll, H>;
using vi = vector<int>;
using vl = vector<ll>;
#define all(a) (a).begin(), (a).end()
#define fs first
#define sc second
#define xx first
#define yy second.first
#define zz second.second
#define Q(i, j, k) mkp((i), mkp((j), (k)))
#define rng(i, s, n) for(int i = (s) ; i < (n) ; i++)
#define rep(i, n) rng(i, 0, (n))
#define mkp make_pair
#define vec vector
#define pb emplace_back
#define siz(a) int((a).size())
#define crdcomp(b) sort(all((b))); (b).erase(unique(all((b))), (b).end())
#define getidx(b, i) (lower_bound(all((b)), (i))-(b).begin())
#define ssp(i, n) (i==(ll)(n)-1?"\n":" ")
#define ctoi(c) (int)((c)-'0')
#define itoc(c) (char)((c)+'0')
#define cyes printf("Yes\n")
#define cno printf("No\n")
#define cdf(n) for(int __quetimes=(n); __quetimes>0; __quetimes--)
#define gcj printf("Case #%lld: ", __quetimes+1)
#define readv(a, n) (a).resize((n), 0); rep(i, (n)) (a)[i]=read()
#define found(a, x) (a.find(x)!=a.end())
constexpr ll mod = (ll)1e9 + 7;
constexpr ll Mod = 998244353;
constexpr ld EPS = 1e-10;
constexpr ll inf = (ll)3 * 1e18;
constexpr int Inf = 1e9;
constexpr int dx[] = { -1, 1, 0, 0 }, dy[] = { 0, 0, -1, 1 };
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
template<class T = ll>T read() { T u, k = scanf("%lld", &u); return u; }
string reads() { string s; cin >> s; return s; }
bool ina(H t, int h, int w) { return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w; }
bool ina(int t, int l, int r) { return l <= t && t < r; }
ll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; }
template<typename T>
void fin(T x) { cout << x << endl; exit(0); }
//--------------------------------------------------------------
class BIT {
    int n;
    vl dat;
public:
    BIT(int n) :n(n) { dat.assign(n + 1, 0); }
    void update(int x, ll v) {
        x++;
        while (x <= n) {
            chmax(dat[x], v);
            x += (x & -x);
        }
    }
    ll max(int x) {
        x++;
        ll ret = 0;
        while (x > 0) {
            chmax(ret, dat[x]);
            x -= (x & -x);
        }
        return ret;
    }
};
signed main() {
    int n;cin >> n;
    vl a(n);rep(i, n) cin >> a[i];
    BIT bit(n + 1);
    ll ans = 0;
    rep(i, n) {
        ll val = bit.max(a[i]);
        chmax(ans, val + a[i]);
        bit.update(a[i], val + a[i]);
    }
    ans = -ans;
    rep(i, n) ans += ll(i + 1);
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 4656
final_rank: 3

code_snippet: #include<stdio.h>

long long  BIT[100001] = { 0 };
int BIT_n = 1;

long long max( long long a, long long b )
{
    return a > b ? a : b;
}

void initBIT( int size )
{
    BIT_n = size;
    for( int i = 1; i <= BIT_n; i++ )
    {
        BIT[i] = 0;
    }
}

long long search( int index )
{
    long long max_val = 0;
    while( index > 0 )
    {
        max_val = max( BIT[index], max_val );
        index -= ( index & -index );
    }
    return max_val;
}

void insert( int index, long long value )
{
    while( index <= BIT_n )
    {
        if( value > BIT[index] )
        {
            BIT[index] = value;
            index += ( index & -index );
        }
        else
        {
            break;
        }
    }
}

int main( void )
{
    long long n;
    scanf( "%lld", &n );
    long long items[100000];
    for( int i = 0; i < n; i++ )
    {
        scanf( "%lld", &items[i] );
    }

    long long ans = 0, tmp;
    initBIT( n );
    for( int i = 0; i < n; i++ )
    {
        tmp = search( items[i] ) + items[i];
        insert( items[i], tmp );
        ans = max( ans, tmp );
    }

    printf( "%lld\n", ( ( long long )n * ( long long )( n + 1 ) / 2 ) - ans );

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4320
final_rank: 1

code_snippet: #include <bits/stdc++.h>

using namespace std;
 
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)(1ll << 62)
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD 1000000007
#define P pair<ll, ll>

struct SegTree { //せぐつりー！
    ll sz;
    vector<ll> dat;
    SegTree(ll n) {
        sz = 1;
        while (sz < n)sz *= 2;
        dat.resize(2 * sz - 1, 0);//
    }
    void update(ll a, ll x) {
        a += sz - 1;//
        dat[a] = x;
        while (a > 0) {
            a = (a - 1) / 2;
            dat[a] = max(dat[a * 2 + 1], dat[a * 2 + 2]);//
        }
    }
    ll query(ll a, ll b, ll k = 0, ll l = 0, ll r = -1) {
        if (r < 0) r = sz;
        if (r <= a or b <= l)return 0;//
        if (a <= l and r <= b)return dat[k];//
        ll q1, q2;
        q1 = query(a, b, k * 2 + 1, l, (l + r) / 2);
        q2 = query(a, b, k * 2 + 2, (l + r) / 2, r);
        return max(q1, q2);//
    }
};
SegTree seg(100000);
ll sum;
int main(){
    ll n;
    cin >> n;
    vector<P> s(n);
    REP(i, n){
        ll a;
        cin >> a;
        sum += a;
        s[i] = P(a, i);
    }
    sort(ALL(s));
    REP(i, n){
        seg.update(s[i].second, seg.query(0,s[i].second+1)+s[i].first);
    }
    cout << sum - seg.query(0, n)<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 6808
final_rank: 14

code_snippet: #include <string.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <chrono>
#include <ciso646>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define REP_OVERLOAD(arg1, arg2, arg3, arg4, NAME, ...) NAME
#define REP3(i, l, r, s)                                               \
    for (int i = int(l), rep3_r = int(r), rep3_s = int(s); i < rep3_r; \
         i += rep3_s)
#define REP2(i, l, r) REP3(i, l, r, 1)
#define REP1(i, n) REP2(i, 0, n)
#define rep(...) REP_OVERLOAD(__VA_ARGS__, REP3, REP2, REP1, )(__VA_ARGS__)
#define repin(i, l, r) for (int i = int(l), repin_r = int(r); i <= repin_r; ++i)

#define RREP_OVERLOAD(arg1, arg2, arg3, arg4, NAME, ...) NAME
#define RREP3(i, l, r, s)                                                      \
    for (int i = int(r) - 1, rrep3_l = int(l), rrep3_s = int(s); i >= rrep3_l; \
         i -= rrep3_s)
#define RREP2(i, l, r) RREP3(i, l, r, 1)
#define RREP1(i, n) RREP2(i, 0, n)
#define rrep(...) RREP_OVERLOAD(__VA_ARGS__, RREP3, RREP2, RREP1, )(__VA_ARGS__)
#define rrepin(i, l, r) \
    for (int i = int(r), rrepin_l = int(l); i >= rrepin_l; --i)


#include <algorithm>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <vector>

namespace atcoder {

template <class S, S (*op)(S, S), S (*e)()> struct segtree {
  public:
    segtree() : segtree(0) {}
    segtree(int n) : segtree(std::vector<S>(n, e())) {}
    segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;

        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    template <bool (*f)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) {
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*f)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

}  // namespace atcoder


#include <algorithm>
#include <cassert>
#include <vector>

namespace rklib {

// a <- max(a, b)
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

// a <- min(a, b)
template <class T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

// if a < 0: a <- b
// else: a <- min(a, b)
template <class T>
bool chmin_non_negative(T &a, const T &b) {
    if (a < 0 || a > b) {
        a = b;
        return true;
    }
    return false;
}

// floor(num / den)
template <class T>
T div_floor(T num, T den) {
    if (den < 0) num = -num, den = -den;
    return num >= 0 ? num / den : (num + 1) / den - 1;
}

// ceil(num / den)
template <class T>
T div_ceil(T num, T den) {
    if (den < 0) num = -num, den = -den;
    return num <= 0 ? num / den : (num - 1) / den + 1;
}

namespace internal {

template <class T>
T remainder_count(T r, T b, T m) {
    return r / m * b + std::min(b, r % m);
}

}  // namespace internal

// Number of integer x s.t.
// - x in [l, r)
// - x mod m in [a, b)
template <class T>
T remainder_count(T l, T r, T a, T b, T m) {
    assert(l >= 0);
    assert(m >= 1);

    if (l >= r || a >= b) return 0;
    if (m <= a || b < 0) return 0;
    chmax(a, T(0));
    chmin(b, m);

    auto res = internal::remainder_count(r, b, m);
    if (l >= 1) res -= internal::remainder_count(l, b, m);
    if (a >= 1) res -= internal::remainder_count(r, a, m);
    if (l >= 1 && a >= 1) res += internal::remainder_count(l, a, m);

    return res;
}

}  // namespace rklib


using namespace std;
using namespace rklib;

using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;

lint op(lint a, lint b) { return max(a, b); }
lint e() { return 0; }

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i, n) cin >> a[i], --a[i];

    lint ans = 0;
    rep(i, n) ans += a[i] + 1;

    atcoder::segtree<lint, op, e> st(n);
    rep(i, n) {
        auto tmp = st.prod(0, a[i]);
        st.set(a[i], tmp + a[i] + 1);
    }
    ans -= st.all_prod();
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 6284
final_rank: 9

code_snippet: #include <string.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <chrono>
#include <ciso646>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define REP_OVERLOAD(arg1, arg2, arg3, arg4, NAME, ...) NAME
#define REP3(i, l, r, s)                                               \
    for (int i = int(l), rep3_r = int(r), rep3_s = int(s); i < rep3_r; \
         i += rep3_s)
#define REP2(i, l, r) REP3(i, l, r, 1)
#define REP1(i, n) REP2(i, 0, n)
#define rep(...) REP_OVERLOAD(__VA_ARGS__, REP3, REP2, REP1, )(__VA_ARGS__)
#define repin(i, l, r) for (int i = int(l), repin_r = int(r); i <= repin_r; ++i)

#define RREP_OVERLOAD(arg1, arg2, arg3, arg4, NAME, ...) NAME
#define RREP3(i, l, r, s)                                                      \
    for (int i = int(r) - 1, rrep3_l = int(l), rrep3_s = int(s); i >= rrep3_l; \
         i -= rrep3_s)
#define RREP2(i, l, r) RREP3(i, l, r, 1)
#define RREP1(i, n) RREP2(i, 0, n)
#define rrep(...) RREP_OVERLOAD(__VA_ARGS__, RREP3, RREP2, RREP1, )(__VA_ARGS__)
#define rrepin(i, l, r) \
    for (int i = int(r), rrepin_l = int(l); i >= rrepin_l; --i)


#include <algorithm>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <vector>

namespace atcoder {

template <class S, S (*op)(S, S), S (*e)()> struct segtree {
  public:
    segtree() : segtree(0) {}
    segtree(int n) : segtree(std::vector<S>(n, e())) {}
    segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;

        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    template <bool (*f)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) {
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*f)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

}  // namespace atcoder


#include <algorithm>
#include <cassert>
#include <vector>

namespace rklib {

// a <- max(a, b)
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

// a <- min(a, b)
template <class T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

// if a < 0: a <- b
// else: a <- min(a, b)
template <class T>
bool chmin_non_negative(T &a, const T &b) {
    if (a < 0 || a > b) {
        a = b;
        return true;
    }
    return false;
}

// floor(num / den)
template <class T>
T div_floor(T num, T den) {
    if (den < 0) num = -num, den = -den;
    return num >= 0 ? num / den : (num + 1) / den - 1;
}

// ceil(num / den)
template <class T>
T div_ceil(T num, T den) {
    if (den < 0) num = -num, den = -den;
    return num <= 0 ? num / den : (num - 1) / den + 1;
}

namespace internal {

template <class T>
T remainder_count(T r, T b, T m) {
    return r / m * b + std::min(b, r % m);
}

}  // namespace internal

// Number of integer x s.t.
// - x in [l, r)
// - x mod m in [a, b)
template <class T>
T remainder_count(T l, T r, T a, T b, T m) {
    assert(l >= 0);
    assert(m >= 1);

    if (l >= r || a >= b) return 0;
    if (m <= a || b < 0) return 0;
    chmax(a, T(0));
    chmin(b, m);

    auto res = internal::remainder_count(r, b, m);
    if (l >= 1) res -= internal::remainder_count(l, b, m);
    if (a >= 1) res -= internal::remainder_count(r, a, m);
    if (l >= 1 && a >= 1) res += internal::remainder_count(l, a, m);

    return res;
}

}  // namespace rklib


using namespace std;
using namespace rklib;

using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;

int op(int a, int b) { return max(a, b); }
int e() { return 0; }

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i, n) cin >> a[i], --a[i];

    lint ans = 0;
    rep(i, n) ans += a[i] + 1;

    atcoder::segtree<int, op, e> st(n);
    rep(i, n) {
        auto tmp = st.prod(0, a[i]);
        st.set(a[i], tmp + a[i] + 1);
    }
    ans -= st.all_prod();
    cout << ans << endl;
}
accuracy: 0.10256410256410256
time_ms: 20
memory_kb: 4932
final_rank: 19

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

//Range Maximum Query
const ll SEG_LEN = (1LL << 19);
vector<ll> seg(SEG_LEN * 2, 0);

void update(ll pos, ll val) {
    pos += SEG_LEN;
    seg.at(pos) = val;
    while ((pos /= 2) > 0) {
        seg.at(pos) = max(seg.at(pos * 2), seg.at(pos * 2 + 1));
    }
}

//[ql, qr)
ll get_max(ll ql, ll qr, ll sl = 0, ll sr = (1 << 19), ll pos = 1) {
    if (qr <= sl || sr <= ql) {
        return 0;
    } else if (ql <= sl && sr <= qr) {
        return seg.at(pos);
    } else {
        ll sm = (sl + sr) / 2;
        ll l_min = get_max(ql, qr, sl, sm, pos * 2);
        ll r_min = get_max(ql, qr, sm, sr, pos * 2 + 1);
        return max(l_min, r_min);
    }
}

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

    vector<ll> x(n);
    ll sum = 0;
    for (int i = 0; i < n; i++) {
        cin >> x.at(i);
        sum += x.at(i);
    }

    for (int i = 0; i < n; i++) {
        update(x.at(i), get_max(1, x.at(i)) + x.at(i));
    }

    cout << sum - get_max(0, SEG_LEN) << endl;

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 12148
final_rank: 15