#line 1 "/home/anqooqie/.proconlib/tools/util.hpp"



#include <bits/stdc++.h>

using i64 = ::std::int_fast64_t;
using u64 = ::std::uint_fast64_t;
using i32 = ::std::int_fast32_t;
using u32 = ::std::uint_fast32_t;

#define all(x) ::std::begin((x)), ::std::end((x))

namespace tools {
  template <typename T>
  void read(::std::istream& is, ::std::vector<T>& vector, const typename ::std::vector<T>::size_type size) {
    vector.reserve(size);
    ::std::copy_n(::std::istream_iterator<T>(is), size, ::std::back_inserter(vector));
  }
  template <typename T>
  void read(::std::vector<T>& vector, const typename ::std::vector<T>::size_type size) {
    ::tools::read(::std::cin, vector, size);
  }

  template <typename T, ::std::size_t N>
  void read(::std::istream& is, ::std::array<T, N>& array) {
    ::std::copy_n(std::istream_iterator<T>(is), N, array.begin());
  }
  template <typename T, ::std::size_t N>
  void read(::std::array<T, N>& array) {
    ::tools::read(::std::cin, array);
  }
}


#line 2 "main.cpp"

int main() {
  i64 n;
  std::cin >> n;

  i64 ng = 0;
  i64 ok = 1414213561;
  while (ok - ng > 1) {
    const i64 center = (ng + ok) / 2;
    if (center * (center + 3) >= 2 * n + 4) {
      ok = center;
    } else {
      ng = center;
    }
  }

  std::cout << std::max(n + 1 - ok, ok * (ok - 1) / 2) << '\n';
  return 0;
}

#include <bits/stdc++.h>
//#include <atcoder/modint>
//using namespace atcoder;
//#pragma GCC optimize("O3")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
using tp = tuple<ll, ll, ll>;
const ll INF = (1LL<<61);

template<class T> bool chmin(T &a, const T b){
    if(a > b) {a = b; return true;}
    else return false;
}
template<class T> bool chmax(T &a, const T b){
    if(a < b) {a = b; return true;}
    else return false;
}
template<class T> void my_printv(std::vector<T> v,bool endline = true){
    if(!v.empty()){
        for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
        std::cout<<v.back();
    }
    if(endline) std::cout<<std::endl;
}

template <class T> class BIT {
    //T has operator "+=" and can be initialized with 0.
    int n, n_2k;
    vector<T> bitree;
public:
    //make [0, n) BIT
    BIT(int _n) : bitree(_n + 1, 0) {
        n = n_2k = _n;
        while(n_2k & (n_2k - 1)) n_2k &= n_2k - 1;
    }

    //BIT[id] += x
    void add(int id, T x) {
        ++id;
        while (id <= n) {
            bitree[id] += x;
            id += id & -id;
        }
    }

    //sum of BIT[0, id]
    T sum(int id) {
        ++id;
        T temp(0);
        while (id > 0) {
            temp += bitree[id];
            id -= id & -id;
        }
        return temp;
    }

    //sum of BIT[l, r)
    T range_sum(int l, int r) {
        return sum(r-1) - sum(l-1);
    }

    //return i in [0, n]
    //i = argmin_{j} (BIT[0, j] >= x)
    //T should have bool operator '<'
    int lower_bound(T x){
        int id = n_2k;
        T temp(0);
        for(int plus = id; plus; id |= (plus>>=1)){
            if(id <= n && temp + bitree[id] < x) temp += bitree[id];
            else id ^= plus;
        }
        return id;
    }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin>>N;
    vec a(N * 2);
    rep(i, N * 2) cin>>a[i];
    priority_queue<ll> pque;
    ll res{};
    rep(i, N){
        pque.push(a[N * 2 - 1 - i]);
        pque.push(a[i]);
        ll nxt = pque.top();
        pque.pop();
        res += nxt;
    }
    cout<<res<<endl;
}