Problem:
F: Absum
問題
長さ $N$ の数列 $A$ が与えられる．あなたは高々 $M$ 回まで，数列の $i$ 番目と $j$ 番目（$0 \leq i, j \leq N-1$）の要素を入れ替える操作を行うことができる．
操作を行なってできる数列の $\sum_{i = 0}^{N - 1} abs(A_i - i)$ の最大値を求めよ．
制約
$2 \leq N \leq 10^5$
$0 \leq M \leq N$
$0 \leq A_i \leq 10^5$
入力形式
入力は以下の形式で与えられる.
$N\ M$（15:15修正）
$A_0\ A_1\ A_2\ \dots\ A_{N - 1}$
出力
操作を行なってできる数列の $\sum_{i = 0}^{N - 1} abs(A_i - i)$ の最大値を求めよ.また, 末尾に改行も出力せよ.
サンプル
サンプル入力 1
5 2
0 3 2 1 4
サンプル出力 1
12
$0$ 番目の要素と $4$ 番目の要素に操作を行うと，操作後の数列は $(4, 3, 2, 1, 0)$ となり， $|4 - 0| + |3 - 1| + |2 - 2| + |1 - 3| + |0 - 4| = 12$ で最大となる．必ずしも操作を
$M$ 回行う必要がない
ことに注意せよ．
サンプル入力 2
3 2
0 0 0
サンプル出力 2
3
操作を行うことなく最大値となる．
サンプル入力 3
6 2
1 0 3 6 5 4
サンプル出力 3
20

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
ll N, M;
ll A[100050];
int main() {
    ll ans = 0;
    cin >> N >> M;
    priority_queue<l_l> indexes;
    priority_queue<l_l> values;
    for(int i = 0; i < N; i++) {
        cin >> A[i];
        ans += abs(i - A[i]);
        values.push({A[i], i});
        indexes.push({i, i});
    }
    values.push({-1, 0});
    indexes.push({-1, 0});
    priority_queue<ll> profit;
    priority_queue<ll, vector<ll>, greater<ll> > cost;
    for(int i = 1; i <= N; i++) {
        l_l now;
        //cerr << i << " " << values.top().first << " "<< indexes.top().first << endl;
        if(values.top() > indexes.top()) {
            now = values.top();
            values.pop();
            ll index = now.second;
            if(A[index] < index) profit.push(2 * A[index]);
        } else {
            now = indexes.top();
            indexes.pop();
            ll index = now.second;
            if(index <= A[index]) profit.push(2 * index);
        }
    }
    for(int i = 1; i <= N; i++) {
        l_l now;
        //cerr << i << " " << values.top().first << " "<< indexes.top().first << endl;
        if(values.top() > indexes.top()) {
            now = values.top();
            values.pop();
            ll index = now.second;
            if(A[index] >= index) cost.push(2 * A[index]);
        } else {
            now = indexes.top();
            indexes.pop();
            ll index = now.second;
            if(A[index] < index) cost.push(2 * index);
        }
    }
    //cerr << profit.size() << " " << cost.size() << endl;
    while(M--) {
        if(profit.empty()) break;
        ll PROFIT = profit.top();
        profit.pop();
        ll COST = cost.top();
        cost.pop();
        //cout << PROFIT << " " << COST << endl;
        ans += PROFIT - COST;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 7800
final_rank: 13

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

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
#define rep3(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define rep2(i, a, b) rep3(i, a, b, 1)
#define rep1(i, n) rep2(i, 0, n)
#define rep0(n) rep1(aaaaa, n)
#define ov4(a, b, c, d, name, ...) name
#define rep(...) ov4(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)
#define per(i, a, b) for (ll i = (a) - 1; i >= (b); i--)
#define fore(e, v) for (auto&& e : v)
#define all(a) begin(a), end(a)
#define sz(a) (int)(size(a))
#define lb(v, x) (lower_bound(all(v), x) - begin(v))
#define eb emplace_back

template <typename T, typename S>
bool chmin(T& a, const S& b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T, typename S>
bool chmax(T& a, const S& b) {
  return a < b ? a = b, 1 : 0;
}
const int INF = 1e9 + 100;
const ll INFL = 3e18 + 100;
#define i128 __int128_t
struct _ {
  _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); }
} __;

int main(){
  int N,M;
  cin>>N>>M;
  vl A(N);
  fore(i,A)cin>>i;
  vl B=A;
  rep(i,N)B.eb(i);
  sort(all(B));
  multiset<ll>l,g;
  rep(i,N)l.insert(B[i]);
  rep(i,N)g.insert(B[i+N]);
  ll ans=0;
  rep(i,N)ans+=abs(A[i]-i);
  ll x=B[N];
  priority_queue<ll> ge;
  priority_queue<ll,vl,greater<ll>> le;
  rep(i,N){
    if(l.contains(i)&&g.contains(A[i])){
      l.erase(l.lower_bound(i));
      g.erase(g.lower_bound(A[i]));
    }else if(g.contains(i)&&l.contains(A[i])){
      g.erase(g.lower_bound(i));
      l.erase(l.lower_bound(A[i]));
    }else{
      if(l.contains(i)&&l.contains(A[i])){
        le.push(max(i,A[i]));
        l.erase(l.lower_bound(i));
        l.erase(l.lower_bound(A[i]));
      }else{
        ge.push(min(i,A[i]));
        g.erase(g.lower_bound(i));
        g.erase(g.lower_bound(A[i]));
      }
    }
  }
  while(M>0&&!le.empty()){
    // cerr<<ge.top()<<' '<<le.top()<<'\n';
    ans+=ge.top()*2;
    ge.pop();
    ans-=le.top()*2;
    le.pop();
    M--;
  }
  cout<<ans<<'\n';
}
accuracy: 1
time_ms: 70
memory_kb: 15748
final_rank: 14

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

using namespace std;

int main() {
	int n, m;
	cin >> n >> m;
	vector<int> a(n);

	vector<int> mn(n), mx(n);

	long long ans = 0;

	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		ans += abs(a[i] - i);
		mn[i] = min(i, a[i]);
		mx[i] = max(i, a[i]);
	}

	sort(mn.rbegin(), mn.rend());
	/* sort(mn.begin(), mn.end()); */
	sort(mx.begin(), mx.end());

	for (int i = 0; i < m; ++i) {
		ans += 2 * max(0, mn[i] - mx[i]);
	}

	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4164
final_rank: 4

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

using namespace std;

int main() {
	int n, m;
	cin >> n >> m;
	vector<int> a(n);

	vector<int> mn(n), mx(n);

	int ans = 0;

	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		ans += abs(a[i] - i);
		mn[i] = min(i, a[i]);
		mx[i] = max(i, a[i]);
	}

	sort(mn.rbegin(), mn.rend());
	sort(mx.begin(), mx.end());

	for (int i = 0; i < m; ++i) {
		ans += 2 * max(0, mn[i] - mx[i]);
	}

	cout << ans << endl;
	return 0;
}
accuracy: 0.2222222222222222
time_ms: 10
memory_kb: 3660
final_rank: 20

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

using namespace std;

typedef long long ll;

int main() {
    ll n, m;
    cin >> n >> m;
    vector<ll> a(n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<ll> b(n), c(n);
    for (ll i = 0; i < n; i++) {
        b[i] = min(i, a[i]);
        c[i] = max(i, a[i]);
    }
    sort(b.begin(), b.end());
    reverse(b.begin(), b.end());
    sort(c.begin(), c.end());

    ll ans = 0;
    for (ll i = 0; i < n; i++) {
        ans += abs(i - a[i]);
    }
    for (ll i = 0; i < m; i++) {
        ans += 2 * max(0ll, b[i] - c[i]);
    }
    cout << ans << '\n';
}
accuracy: 1
time_ms: 20
memory_kb: 5488
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <array>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
#include <random>
#include <set>

int main(void) {
	int n, m; std::cin >> n >> m;
	std::vector<int> numbers(n); for (auto& a : numbers) std::cin >> a;
	std::priority_queue<int> greater, less;
	for (auto i = 0; i < n; ++i) {
		if (i > numbers[i]) {
			greater.push(-i);
			less.push(numbers[i]);
		}
		else {
			greater.push(-numbers[i]);
			less.push(i);
		}
	}
	for (auto i = 0; i < m; ++i) {
		if (less.top() + greater.top() > 0) {
			const auto l = less.top(); less.pop();
			const auto g = greater.top(); greater.pop();
			less.push(-g);
			greater.push(-l);
		}
		else break;
	}
	long long int result = 0;
	for (auto i = 0; i < n; ++i) {
		result += -less.top() + -greater.top();
		less.pop(); greater.pop();
	}
	std::cout << result << std::endl;
}
accuracy: 1
time_ms: 30
memory_kb: 4400
final_rank: 8

code_snippet: #ifdef LOCAL
    #define _GLIBCXX_DEBUG
    #define __clock__
#else
    #pragma GCC optimize("Ofast")
    // #define NDEBUG
#endif
#define __precision__ 10
#define iostream_untie true
#define debug_stream std::cerr

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>

#define all(v) std::begin(v), std::end(v)
#define rall(v) std::rbegin(v), std::rend(v)
#define odd(n) ((n) & 1)
#define even(n) (not __odd(n))
#define __popcount(n) __builtin_popcountll(n)
#define __clz32(n) __builtin_clz(n)
#define __clz64(n) __builtin_clzll(n)
#define __ctz32(n) __builtin_ctz(n)
#define __ctz64(n) __builtin_ctzll(n)

using i32 = int_least32_t; using i64 = int_least64_t; using u32 = uint_least32_t; using u64 = uint_least64_t;
using pii = std::pair<i32, i32>; using pll = std::pair<i64, i64>;
template <class T> using heap = std::priority_queue<T>;
template <class T> using rheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class K, class T> using hashmap = std::unordered_map<K, T>;
template <class T> using hashset = std::unordered_set<T>;

namespace execution
{
    using namespace std::chrono;
    system_clock::time_point start_time, end_time;
    long long get_elapsed_time() { end_time = system_clock::now(); return duration_cast<milliseconds>(end_time - start_time).count(); }
    void print_elapsed_time() { std::cerr << "\n----- Exec time : " << get_elapsed_time() << " ms -----\n\n"; }
    struct setupper
    {
        setupper()
        {
            if(iostream_untie) std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
            std::cout << std::fixed << std::setprecision(__precision__);
    #ifdef stderr_path
            if(freopen(stderr_path, "a", stderr))
            {
                std::cerr << std::fixed << std::setprecision(__precision__);
            }
    #endif
    #ifdef stdout_path
            if(not freopen(stdout_path, "w", stdout))
            {
                freopen("CON", "w", stdout);
                std::cerr << "Failed to open the stdout file\n\n";
            }
            std::cout << "";
    #endif
    #ifdef stdin_path
            if(not freopen(stdin_path, "r", stdin))
            {
                freopen("CON", "r", stdin);
                std::cerr << "Failed to open the stdin file\n\n";
            }
    #endif
    #ifdef LOCAL
            std::cerr << "----- stderr at LOCAL -----\n\n";
            atexit(print_elapsed_time);
    #else
            fclose(stderr);
    #endif
    #ifdef __clock__
            start_time = system_clock::now();
    #endif
        }
    } __setupper;
    class myclock_t
    {
        system_clock::time_point built_pt, last_pt; int built_ln, last_ln;
        std::string built_func, last_func; bool is_built;
    public:
        myclock_t() : is_built(false) {}
        void build(int crt_ln, const std::string &crt_func)
        {
            is_built = true, last_pt = built_pt = system_clock::now(),  last_ln = built_ln = crt_ln, last_func = built_func = crt_func;
        }
        void set(int crt_ln, const std::string &crt_func)
        {
            if(is_built) last_pt = system_clock::now(), last_ln = crt_ln, last_func = crt_func;
            else debug_stream << "[ " << crt_ln << " : " << crt_func << " ] " << "myclock_t::set failed (yet to be built!)\n";
        }
        void get(int crt_ln, const std::string &crt_func)
        {
            if(is_built)
            {
                system_clock::time_point crt_pt(system_clock::now());
                long long diff = duration_cast<milliseconds>(crt_pt - last_pt).count();
                debug_stream << diff << " ms elapsed from" << " [ " << last_ln << " : " << last_func << " ]";
                if(last_ln == built_ln) debug_stream << " (when built)";
                debug_stream << " to" << " [ " << crt_ln << " : " << crt_func << " ]" << "\n";
                last_pt = built_pt, last_ln = built_ln, last_func = built_func;
            }
            else
            {
                debug_stream << "[ " << crt_ln << " : " << crt_func << " ] " << "myclock_t::get failed (yet to be built!)\n";
            }
        }
    };
} // namespace execution

#ifdef __clock__
    execution::myclock_t __myclock;
    #define build_clock() __myclock.build(__LINE__, __func__)
    #define set_clock() __myclock.set(__LINE__, __func__)
    #define get_clock() __myclock.get(__LINE__, __func__)
#else
    #define build_clock() ((void)0)
    #define set_clock() ((void)0)
    #define get_clock() ((void)0)
#endif

namespace std
{
    template <class P> void rsort(P __first, P __last) { sort(__first, __last, greater<>()); }
    template <class T> size_t hash_combine(size_t seed, T const &key) { return seed ^ (hash<T>()(key) + 0x9e3779b9 + (seed << 6) + (seed >> 2)); }
    template <class T, class U> struct hash<pair<T, U>> { size_t operator()(pair<T, U> const &pr) const { return hash_combine(hash_combine(0, pr.first), pr.second); } };
    template <class tuple_t, size_t index = tuple_size<tuple_t>::value - 1> struct tuple_hash_calc { static size_t apply(size_t seed, tuple_t const &t) { return hash_combine(tuple_hash_calc<tuple_t, index - 1>::apply(seed, t), get<index>(t)); } };
    template <class tuple_t> struct tuple_hash_calc<tuple_t, 0> { static size_t apply(size_t seed, tuple_t const &t) { return hash_combine(seed, get<0>(t)); } };
    template <class... T> struct hash<tuple<T...>> { size_t operator()(tuple<T...> const &t) const { return tuple_hash_calc<tuple<T...>>::apply(0, t); } };
    template <class T, class U> istream &operator>>(std::istream &s, pair<T, U> &p) { return s >> p.first >> p.second; }
    template <class T, class U> ostream &operator<<(std::ostream &s, const pair<T, U> &p) { return s << p.first << " " << p.second; }
    template <class T> istream &operator>>(istream &s, vector<T> &v) { for(T &e : v) s >> e; return s; }
    template <class T> ostream &operator<<(ostream &s, const vector<T> &v)  { bool is_front = true; for(const T &e : v) { if(not is_front) s << ' '; else is_front = false; s << e; } return s; }
    template <class tuple_t, size_t index> struct tupleos { static ostream &apply(ostream &s, const tuple_t &t) { tupleos<tuple_t, index - 1>::apply(s, t); return s << " " << get<index>(t); } };
    template <class tuple_t> struct tupleos<tuple_t, 0> { static ostream &apply(ostream &s, const tuple_t &t) { return s << get<0>(t); } };
    template <class... T> ostream &operator<<(ostream &s, const tuple<T...> &t)  { return tupleos<tuple<T...>, tuple_size<tuple<T...>>::value - 1>::apply(s, t); }
    template <> ostream &operator<<(ostream &s, const tuple<> &t) { return s; }
    string revstr(string str) { reverse(str.begin(), str.end()); return str; }
} // namespace std

#ifdef LOCAL
    #define dump(...)                                                              \
        debug_stream << "[ " << __LINE__ << " : " << __FUNCTION__ << " ]\n",       \
            dump_func(#__VA_ARGS__, __VA_ARGS__)
    template <class T> void dump_func(const char *ptr, const T &x)
    {
        debug_stream << '\t';
        for(char c = *ptr; c != '\0'; c = *++ptr) if(c != ' ') debug_stream << c;
        debug_stream << " : " << x << '\n';
    }
    template <class T, class... rest_t> void dump_func(const char *ptr, const T &x, rest_t... rest)
    {
        debug_stream << '\t';
        for(char c = *ptr; c != ','; c = *++ptr) if(c != ' ') debug_stream << c;
        debug_stream << " : " << x << ",\n"; dump_func(++ptr, rest...);
    }
#else
    #define dump(...) ((void)0)
#endif
template <class P> void read_range(P __first, P __second) { for(P i = __first; i != __second; ++i) std::cin >> *i; }
template <class P> void write_range(P __first, P __second) { for(P i = __first; i != __second; std::cout << (++i == __second ? '\n' : ' ')) std::cout << *i; }

// substitue y for x if x > y.
template <class T> inline bool sbmin(T &x, const T &y) { return x > y ? x = y, true : false; }
// substitue y for x if x < y.
template <class T> inline bool sbmax(T &x, const T &y) { return x < y ? x = y, true : false; }
// binary search.
i64 bin(const std::function<bool(i64)> &pred, i64 ok, i64 ng)
{
    while(std::abs(ok - ng) > 1) { i64 mid = (ok + ng) / 2; (pred(mid) ? ok : ng) = mid; }
    return ok;
}
double bin(const std::function<bool(double)> &pred, double ok, double ng, const double eps)
{
    while(std::abs(ok - ng) > eps) { double mid = (ok + ng) / 2; (pred(mid) ? ok : ng) = mid; }
    return ok;
}
// be careful that val is type-sensitive.
template <class T, class A, size_t N> void init(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); }
// reset all bits.
template <class A> void reset(A &array) { memset(array, 0, sizeof(array)); }


/* The main code follows. */

using namespace std;

main()
{
    void __solve();
    u32 t = 1;
#ifdef LOCAL
    t = 1;
#endif
    // t = -1;
    // cin >> t;
    while(t--)
    {
        __solve();
    }
}

void __solve()
{
    int n,m; cin>>n>>m;
    vector<int> a(n); cin>>a;
    // vector<int> plu,minu;
    rheap<int> poq;
    heap<int> neq;
    for(int i=0; i<n; i++)
    {
        int po,ne;
        po=max(i,a[i]);
        ne=min(i,a[i]);
        // plu.emplace_back(po);
        // minu.emplace_back(ne);
        poq.emplace(po);
        neq.emplace(ne);
    }
    // sort(all(plu));
    // sort(rall(minu));
    while(m && poq.top()<neq.top())
    {
        m--;
        int pt=poq.top();
        int nt=neq.top();
        poq.pop();
        neq.pop();
        poq.emplace(nt);
        neq.emplace(pt);
    }
    i64 ans=0;
    while(!poq.empty())
    {
        ans+=poq.top();
        poq.pop();
    }
    while(!neq.empty())
    {
        ans-=neq.top();
        neq.pop();
    }
    cout << ans << "\n";
}
accuracy: 1
time_ms: 20
memory_kb: 4424
final_rank: 5

code_snippet: #include "bits/stdc++.h"
#include<vector>
#include<iostream>
#include<queue>
#include<algorithm>
#include<map>
#include<set>
#include<iomanip>
#include<assert.h>
#include<unordered_map>
#include<unordered_set>
#include<string>
#include<stack>
#include<complex>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;
	
#define WHATS(var)cout<<__LINE__<<' '<<#var<<"="<<var<<endl;
	
template<class S, class T> ostream& operator <<(ostream &os, const pair<S, T> v){
	os << "( " << v.first << ", " << v.second << ")"; return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<T> &v){
	for(int i = 0; i < v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<vector<T>> &v){
	for(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<set<T>> &v){
	for(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const set<T> &v){
	int i=0;
	for(auto it:v){
		if(i > 0){os << ' ';}
		os << it;
		i++;
	} 
	return os;
}

using ll =long long ;
ll mod;

void solve(vector<vector<int>>&cango){
	int tt=cango.size();
	for(int i=0;i<tt;++i){
		for(int j=0;j<tt;++j){
			for(int k=0;k<tt;++k){
				cango[j][k]|=cango[j][i]&cango[i][k];
			}
		}
	}
}
vector<int> dfs(int now,int N,vector<pair<int,int>>&edges){
	vector<int>av{now};
	if(now*2+1<N){
		auto v=dfs(now*2+1,N,edges);
		av.insert(av.end(),v.begin(),v.end());
	}
	if(now*2+2<N){
		auto v=dfs(now*2+2,N,edges);
		for(auto e:v){
			edges.push_back(make_pair(now,e));
		}
		av.insert(av.end(),v.begin(),v.end());
	}
	return av;
}

int ask(int x){
	cout<<x+1<<endl;
	fflush(stdout);
	string st;cin>>st;
	if(st=="Yes")return -1;
	else if(st=="No")return 0;
	else return 1;
}
int main() {
	ios::sync_with_stdio(false);
	int N,K;cin>>N>>K;
	vector<pair<int,int>>v(N);
	for(int i=0;i<N;++i){
		int k;cin>>k;
		v[i]=make_pair(k,i);
	}
	pair<int,int> xx=make_pair(-1,-1);
	{
		auto nv(v);
		sort(nv.begin(),nv.end(),greater<pair<int,int>>());
		int place=N;
		for(int i=0;i<N;++i){
			if(nv[i]<=make_pair(i,0)){
				place=i;
				break;
			}
		}
		if(place!=N){
			xx=nv[place];
		}
	}
	//WHATS(xx)
	{
		int l=0,r=N-1;
		for(int i=0;i<K;++i){
			while(l<=N-1&&v[l]>xx)l++;
			while(r>=0&&v[r]<=xx)r--;
			if(l<r){
				//WHATS(l)
				//WHATS(r)
				swap(v[l],v[r]);
			}
			l++;
			r--;
		}
		ll answer=0;
		for(int i=0;i<N;++i)answer+=abs(i-v[i].first);
		cout<<answer<<endl;
	}
	
	return 0;	
}
accuracy: 0.25555555555555554
time_ms: 10
memory_kb: 4068
final_rank: 19

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

int main(){
    int N, M;
    cin >> N >> M;
    vector<int> ub(N), lb(N);
    int64_t ans = 0;
    for(int i=0; i<N; i++){
        int a;
        cin >> a;
        ub[i] = max(a, i);
        lb[i] = min(a, i);
        ans += abs(a-i);
    }
    sort(ub.begin(), ub.end());
    sort(lb.rbegin(), lb.rend());
    for(int i=0; i<M; i++) ans += 2 * max(0, lb[i] - ub[i]);
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3608
final_rank: 2

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 100005

int N,M;
int table[NUM];
vector<int> PLUS,MINUS;

int main(){

	ll ans = 0;

	scanf("%d %d",&N,&M);

	for(int i = 0; i < N; i++){

		scanf("%d",&table[i]);
		ans += abs(i-table[i]);

		PLUS.push_back(min(i,table[i]));
		MINUS.push_back(max(i,table[i]));
	}

	sort(PLUS.begin(),PLUS.end());
	sort(MINUS.begin(),MINUS.end());

	reverse(PLUS.begin(),PLUS.end());

	for(int i = 0; i < M; i++){

		ans += 2*max(0,PLUS[i]-MINUS[i]);
	}

	printf("%lld\n",ans);

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

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

using ll = long long;

int main() {
    int n, m; cin >> n >> m;
    vector<ll> a(n);
    for(int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    vector<ll> plus, minus;
    ll ans = 0;
    for(int i = 0; i < n; ++i) {
        if(a[i] < i) {
            plus.push_back(i);
            minus.push_back(a[i]);
        } else {
            plus.push_back(a[i]);
            minus.push_back(i);
        }
        ans += abs(a[i] - i);
    }
    sort(begin(plus), end(plus));
    sort(rbegin(minus), rend(minus));

    for(int i = 0; i < m; ++i) {
        if(plus[i] > minus[i]) break;
        ans += 2 * (minus[i] - plus[i]);
    }

    cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 5556
final_rank: 11

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

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

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

ll n,m;
ll a[100100];

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

  cin>>n>>m;
  rep(i,n)cin>>a[i];

  ll crt=0;
  vector<ll> pos,neg;
  rep(i,n){
    crt+=abs(a[i]-i);
    pos.push_back(max(a[i],i));
    neg.push_back(min(a[i],i));
  }
  sort(all(pos));
  sort(all(neg)); reverse(all(neg));

  rep(i,m)crt+=max(0LL,-pos[i]+neg[i])*2;
  cout<<crt<<endl;

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 5568
final_rank: 6

code_snippet: #include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
const int INF=1e9,MOD=1e9+7;
const ll LINF=1e18;
using namespace std;
#define int long long
//template
//main
signed main(){
  int N,M;cin>>N>>M;
  std::vector<int> v(N);
  for(int i=0;i<N;i++)cin>>v[i];
  std::vector<int> ma(N),mi(N);
  for(int i=0;i<N;i++){
    ma[i]=max(i,v[i]);
    mi[i]=min(i,v[i]);
  }
  sort(ma.begin(),ma.end());
  sort(mi.rbegin(),mi.rend());
  int ans=0;
  for(int i=0;i<N;i++)ans+=abs(i-v[i]);
  for(int i=0;i<M;i++)ans+=2*max(mi[i]-ma[i],0LL);
  cout<<ans<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 5184
final_rank: 9

code_snippet: #include <bits/stdc++.h>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;
const double pi=acos(-1);
const double eps=1e-10;

int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};

int main(){
    int n, m; cin>>n>>m;
    vector<int> a(n);
    vector<int> head(n), tail(n);
    ll ans=0;
    for(int i=0; i<n; i++){
        cin>>a[i];
        ans+=abs(a[i]-i);
        head[i]=(min(i, a[i]));
        tail[i]=(max(i, a[i]));
    }

    sort(head.begin(), head.end(), greater<int>());
    sort(tail.begin(), tail.end());

    int cnt=0;
    for(int i=0; i<m; i++){
        if(head[cnt]-tail[cnt]>0){
            ans+=(head[cnt]-tail[cnt])*2;
            cnt++;
        }
        else break;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3924
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


template <typename T>
struct SegmentTree{
  using F = function<T(T,T)>;
  Int n;
  F f;
  T ti;
  vector<T> dat;
  SegmentTree(){};
  SegmentTree(F f,T ti):f(f),ti(ti){}
  void init(Int n_){    
    n=1;
    while(n<n_) n<<=1;
    dat.assign(n<<1,ti);
  }
  void build(const vector<T> &v){
    Int n_=v.size();
    init(n_);
    for(Int i=0;i<n_;i++) dat[n+i]=v[i];
    for(Int i=n-1;i;i--)
      dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
  }
  void set_val(Int k,T x){
    dat[k+=n]=x;
    while(k>>=1)
      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);    
  }
  T query(Int a,Int b){
    T vl=ti,vr=ti;
    for(Int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
      if(l&1) vl=f(vl,dat[l++]);
      if(r&1) vr=f(dat[--r],vr);
    }
    return f(vl,vr);
  }
  template<typename C>
  Int find(Int a,Int b,C &check,Int k,Int l,Int r){
    if(!check(dat[k])||r<=a||b<=l) return -1;
    if(k>=n) return k-n;
    Int m=(l+r)>>1;
    Int vl=find(a,b,check,(k<<1)|0,l,m);
    if(~vl) return vl;
    return find(a,b,check,(k<<1)|1,m,r);
  }
  template<typename C>
  Int find(Int a,Int b,C &check){
    return find(a,b,check,1,0,n);
  }
};

//INSERT ABOVE HERE
signed main(){
  Int n,m;
  cin>>n>>m;
  vector<Int> v(n);
  for(Int i=0;i<n;i++) cin>>v[i];
  Int ans=0;
  for(Int i=0;i<n;i++) ans+=abs(v[i]-i);
  
  using P = pair<Int, Int>;
  struct T{
    P a,b,c;
    T(){}
    T(P a,P b,P c):a(a),b(b),c(c){}
  };
  auto f=[&](T x,T y){
           return T(max(x.a,y.a),min(x.b,y.b),x.c);
         };

  vector<T> vt(n);
  for(Int i=0;i<n;i++){
    vt[i]=T(P(min(v[i],i),i),P(max(v[i],i),i),P(i,i));    
  }

  const Int INF = 1e9;
  T ti(P(-INF,-INF),P(INF,INF),P(0,0));
  SegmentTree<T> seg(f,ti);
  seg.build(vt);

  for(Int i=0;i<m;i++){
    T res=seg.query(0,n);
    Int x=res.b.second;
    Int y=res.a.second;
    if(x==y) break;
    if(min(v[y],y)<=max(v[x],x)) break;
    //cout<<x<<" "<<y<<endl;
    ans+=(min(v[y],y)-max(v[x],x))*2;
    swap(v[x],v[y]);
    vt[x]=T(P(min(v[x],x),x),P(max(v[x],x),x),P(x,x));
    vt[y]=T(P(min(v[y],y),y),P(max(v[y],y),y),P(y,y));
    seg.set_val(x,vt[x]);
    seg.set_val(y,vt[y]);
  }
  Int res=0;
  for(Int i=0;i<n;i++) res+=abs(v[i]-i);
  assert(ans==res);  
  //cout<<ans<<endl;
  cout<<res<<endl;
  
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 20544
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
typedef long long LL;
int N,M;
LL A[100001];
vector<LL>v;
typedef pair<LL,LL>P;
priority_queue<P>q;
priority_queue<P,vector<P>,greater<P> >qq;

int main(){
	cin>>N>>M;
	REP(i,N){
		cin>>A[i];
		v.push_back(A[i]);
	}
	sort(v.begin(),v.end());
	reverse(v.begin(),v.end());
	int mid=N-1;
	REP(i,N){
		if(v[i]<=i){
			mid=i;
			break;
		}
	}
	REP(i,mid){
		if(A[i]<=mid){
			qq.push(P(max((LL)i,A[i]),i));
		}
	}
	for(int i=mid;i<N;i++){
		if(A[i]>=mid){
			q.push(P(min((LL)i,A[i]),i));
		}
	}
	REP(i,M){
		if(q.empty())break;
		if(qq.empty())break;
		P p=q.top();q.pop();
		P pp=qq.top();qq.pop();
		if(pp.first>p.first)continue;
		//cout<<pp.second<<" "<<p.second<<endl;
		LL temp=A[p.second];
		A[p.second]=A[pp.second];
		A[pp.second]=temp;
	}
	LL ans=0;
	REP(i,N){
		ans+=(LL)abs(i-A[i]);
	}
	
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 6344
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <deque>
#include <iomanip>
#include <cstdio>
#include <stack>
#include <numeric>

using namespace std;
typedef  long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define  MP make_pair
#define  PB push_back
#define inf  1000000007
#define rep(i,n) for(int i=0;i<(int)(n);++i)

#define int long long 
signed main(){
    int n,m;
    cin >> n >> m;
    vector<int>a(n);
    rep(i,n) cin >> a[i];
    set<pair<int,pair<int,int> > > st;
    set<pair<int,pair<int,int> >,greater< pair<int,pair<int,int> > >  > st2;
    rep(i,n){
        st.insert(MP(max(i,a[i]),MP(i,a[i])));
        st2.insert(MP(min(i,a[i]),MP(i,a[i])));
    }
    ll ans = 0;
    rep(i,m){
        auto x = *st.begin();
        auto y = *st2.begin();
        //cerr << x.second.first << " " << x.second.second << " " << y.second.first << " " << y.second.second << endl; 
        if(y.second.first > x.second.first&& y.second.second > x.second.second &&x.second!=y.second){
            st.erase(x);
            st.erase(MP(max(y.second.first,y.second.second),y.second));
            st.erase(MP(min(x.second.first,x.second.second),x.second));
            st2.erase(y);
            pair<int,pair<int,int> > a,b,c,d;
            a = MP(max(x.second.first,y.second.second),MP(x.second.first,y.second.second));
            b = MP(max(y.second.first,x.second.second),MP(y.second.first,x.second.second));
            c = MP(min(x.second.first,y.second.second),MP(x.second.first,y.second.second));
            d = MP(min(y.second.first,x.second.second),MP(y.second.first,x.second.second));
            st.insert(a);
            st.insert(b);
            st2.insert(c);
            st2.insert(d);
        }
    }
    for(auto x:st){
        //cerr << x.second.second << " ";
        ans += abs(x.second.first-x.second.second);
    }
    //cerr << endl;
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 17564
final_rank: 16

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <deque>
#include <iomanip>
#include <cstdio>
#include <stack>
#include <numeric>

using namespace std;
typedef  long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define  MP make_pair
#define  PB push_back
#define inf  1000000007
#define rep(i,n) for(int i=0;i<(int)(n);++i)

#define int long long 
signed main(){
    int n,m;
    cin >> n >> m;
    vector<int>a(n);
    rep(i,n) cin >> a[i];
    set<pair<int,pair<int,int> > > st;
    set<pair<int,pair<int,int> >,greater< pair<int,pair<int,int> > >  > st2;
    rep(i,n){
        st.insert(MP(max(i,a[i]),MP(i,a[i])));
        st2.insert(MP(min(i,a[i]),MP(i,a[i])));
    }
    ll ans = 0;
    rep(i,m){
        auto x = *st.begin();
        auto y = *st2.begin();
        //cerr << x.second.first << " " << x.second.second << " " << y.second.first << " " << y.second.second << endl; 
        if(y.second.first > x.second.first&&x.second!=y.second){
            st.erase(x);
            st.erase(MP(max(y.second.first,y.second.second),y.second));
            st.erase(MP(min(x.second.first,x.second.second),x.second));
            st2.erase(y);
            pair<int,pair<int,int> > a,b,c,d;
            a = MP(max(x.second.first,y.second.second),MP(x.second.first,y.second.second));
            b = MP(max(y.second.first,x.second.second),MP(y.second.first,x.second.second));
            c = MP(min(x.second.first,y.second.second),MP(x.second.first,y.second.second));
            d = MP(min(y.second.first,x.second.second),MP(y.second.first,x.second.second));
            st.insert(a);
            st.insert(b);
            st2.insert(c);
            st2.insert(d);
        }
    }
    for(auto x:st){
        //cerr << x.second.second << " ";
        ans += abs(x.second.first-x.second.second);
    }
    //cerr << endl;
    cout << ans << endl;
    return 0;
}
accuracy: 0.6666666666666666
time_ms: 140
memory_kb: 17592
final_rank: 17

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <deque>
#include <iomanip>
#include <cstdio>
#include <stack>
#include <numeric>

using namespace std;
typedef  long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define  MP make_pair
#define  PB push_back
#define inf  1000000007
#define rep(i,n) for(int i=0;i<(int)(n);++i)


int main(){
    int n,m;
    cin >> n >> m;
    vector<int>a(n);
    rep(i,n) cin >> a[i];
    set<pair<int,pair<int,int> > > st;
    set<pair<int,pair<int,int> >,greater< pair<int,pair<int,int> > >  > st2;
    rep(i,n){
        st.insert(MP(max(i,a[i]),MP(i,a[i])));
        st2.insert(MP(min(i,a[i]),MP(i,a[i])));
    }
    ll ans = 0;
    rep(i,m){
        auto x = *st.begin();
        auto y = *st2.begin();
        //cerr << x.second.first << " " << x.second.second << " " << y.second.first << " " << y.second.second << endl; 
        if(y.second.first > x.second.first&&x.second!=y.second){
            st.erase(x);
            st.erase(MP(max(y.second.first,y.second.second),y.second));
            st.erase(MP(min(x.second.first,x.second.second),x.second));
            st2.erase(y);
            pair<int,pair<int,int> > a,b,c,d;
            a = MP(max(x.second.first,y.second.second),MP(x.second.first,y.second.second));
            b = MP(max(y.second.first,x.second.second),MP(y.second.first,x.second.second));
            c = MP(min(x.second.first,y.second.second),MP(x.second.first,y.second.second));
            d = MP(min(y.second.first,x.second.second),MP(y.second.first,x.second.second));
            st.insert(a);
            st.insert(b);
            st2.insert(c);
            st2.insert(d);
        }
    }
    for(auto x:st){
        //cerr << x.second.second << " ";
        ans += abs(x.second.first-x.second.second);
    }
    //cerr << endl;
    cout << ans << endl;
    return 0;
}
accuracy: 0.6666666666666666
time_ms: 160
memory_kb: 17232
final_rank: 18

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
    int N, M; cin >> N >> M;
    vector<long long> a(N);
    for (int i = 0; i < N; ++i) cin >> a[i];

    long long res = 0;
    vector<long long> plus, minus;
    for (int i = 0; i < N; ++i) {
        res += abs(a[i] - i);
        if (a[i] >= i) plus.push_back(a[i]), minus.push_back(i);
        else plus.push_back(i), minus.push_back(a[i]);
    }
    sort(plus.begin(), plus.end());
    sort(minus.begin(), minus.end(), greater<long long>());

    for (int i = 0; i < M; ++i) {
        if (plus[i] < minus[i]) {
            res += (minus[i] - plus[i]) * 2;
        }
        else {
            break;
        }
    }
    cout << res << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 5532
final_rank: 10