Problem:
税の徴収
イヅア村には$1$から$N$までの番号が付いた$N$軒の家がある。この村では、$M$人の徴税官が税を徴収する。徴税官は家を訪れて税金を徴収する。1軒の家で徴収する金額は、徴税官ごとに決まっている。
はじめに、徴税官はそれぞれ異なる家に派遣され、そこで税金を徴収する。
次に、徴税官がいる家のうち番号が一番小さい家にいる徴税官が、それよりも大きい番号の家のうち、まだ徴税が済んでいない最も番号が小さい家に移動し、そこで税金を徴収する。これを繰り返し、どの徴税官も税金を徴収できなくなったら徴収を終える。
家の軒数と徴税官の人数、各徴税官が徴収する金額と最初に派遣される家の番号が与えられたとき、イヅア村から徴収できる税金の総額を求めるプログラムを作成せよ。
入力
入力は以下の形式で与えられる。
$N$ $M$
$t_1$ $h_1$
$t_2$ $h_2$
:
$t_M$ $h_M$
１行目に家の軒数$N$ ($1 \leq N \leq 100,000$)と徴税官の人数$M$ ($1 \leq M \leq N$)が与えられる。続く$M$行に$i$番目の徴税官が徴収する税金の金額$t_i$ ($1 \leq t_i \leq 10,000$)と最初に派遣される家の番号$h_i$ ($1 \leq h_i \leq N$)が与えられる。ただし、異なる徴税官が同じ家に最初に派遣されることはない（$i \ne j$ならば$h_i \ne h_j$）。
出力
イヅア村から徴収できる税金の総額を1行に出力する。
入出力例
入力例１
6 2
2 1
3 3
出力例１
14
入力例２
7 3
3 4
2 3
5 7
出力例２
15

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ul=unsigned long long;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
using Graph=vector<vector<int>>;
ll op(ll a,ll b){return max(a,b);}
ll e(){return -1e18;}
int main(){
    ll N,M;
    cin>>N>>M;
    vector<ll>t(M),h(M);
    set<ll>st;
    queue<int>q;
    vector<bool>T(N);
    priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>>p;
    for(int i=0;i<M;i++){
        cin>>t[i]>>h[i];
        h[i]--;
        p.push({h[i],t[i]});
        T[h[i]]=1;
    }
    for(int i=0;i<N;i++)if(!T[i])q.push(i);
    ll ans=0;
    while(!p.empty()){
        auto[i,t]=p.top();
        ans+=t;
        p.pop();
        while(!q.empty()&&i>=q.front())q.pop();
        if(!q.empty()){
            p.push({q.front(),t});
            q.pop();
        }
    }
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 6412
final_rank: 12

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
int main()
{
  int n,m;
  cin >> n >> m;
  vector<pair<int,int>>iti(m);
  for(int i=0;i<m;i++)cin >> iti[i].first >> iti[i].second;
  for(int i=0;i<m;i++)iti[i].second--;

  priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
  long long ans=0;
  vector<bool>check(n,false);
  for(int i=0;i<m;i++){
    ans+=iti[i].first;
    check[iti[i].second]=true;
    pq.push(make_pair(iti[i].second,i));
  }

  int now=0;
  while(pq.size()){
    pair<int,int>hako=pq.top();
    pq.pop();

    while(now<n){
      if(check[now] || hako.first>now)now++;
      else break;
    }
    if(now==n)break;

    ans+=iti[hako.second].first;
    check[now]=true;
    pq.push(make_pair(now,hako.second));
    now++;
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 4716
final_rank: 9

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

constexpr int BIG_INT = 1'000'000'100;
constexpr long BIG_LONG = 1'000'000'000'000'000'100;

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

    int n, m;
    cin >> n >> m;
    set<pair<int, int>> ht;
    for (int i = 0; i < m; i++) {
        int t, h;
        cin >> t >> h;
        h--;
        ht.insert({h, t});
    }

    set<int> st;
    for (int i = ht.begin()->first; i < n; i++) {
        st.insert(i);
    }
    for (auto [h, t] : ht) {
        st.erase(h);
    }

    int ans = 0;
    while (!ht.empty()) {
        auto [h, t] = *ht.begin();
        ht.erase(ht.begin());
        ans += t;
        if (!st.empty()) {
            auto tmp = st.begin();
            ht.insert({*tmp, t});
            st.erase(tmp);
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 12140
final_rank: 19

code_snippet: #ifdef GOHACHI
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
template<class T> using VC = vector<T>;
template<class T,class TT> using P = pair<T,TT>;
template<class T,class TT,class TTT> using TP = tuple<T,TT,TTT>;
template<class T> using pq = priority_queue<T, VC<T>, greater<T>>;
template<class T> using pqR = priority_queue<T>;
#define F first
#define S second
#define INF LLONG_MAX
#define MOD 998244353
ll DX[4] = { 0 , 0 , -1 , 1 };
ll DY[4] = { -1 , 1 , 0 , 0 };
#define st(x,y) sort(x,x+y)
#define stR(x,y) sort(x,x+y,greater<ll>())
#define ST(x) sort(x.begin(),x.end())
#define STR(x) sort(x.rbegin(),x.rend())
#define rep(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define repR(i,a,b) for(ll i=(ll)(a);i>=(ll)(b);i--)
#define REP(i,a,b,c) for(ll i=(ll)(a);i<(ll)(b);i+=(ll)(c))
#define repE(i,a,b,c) for(ll i=(ll)(a);(ll)b<(ll)(c);i++)
#define all(x) x.begin(),x.end()
#define rev(x,y) reverse(x,x+y)
#define REV(x) reverse(x.begin(),x.end())
#define NP next_permutation
#define dec(x) fixed << setprecision(x)
#define POW(x,y) (ll)pow(x,y)
#define RD(x) (double)round(x)
ll s;
void solve() {
	ll n,m; cin>>n>>m;
	ll t,h;
	map<ll,ll> mp;
	while(m--) { cin>>t>>h; mp[h]=t; }
	queue<ll> qu;
	rep(i,1,n+1) {
		if (mp[i]==0 && (!qu.empty())) {
			ll x=qu.front(); qu.pop();
			s+=x; qu.push(x);
		}
		if (mp[i]!=0) { s+=mp[i]; qu.push(mp[i]); }
	}
	cout<<s<<'\n';
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int T=1;
    while(T--) solve();
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 10216
final_rank: 16

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

using namespace std;
//using namespace atcoder;
#define rep(i,n) for(ll i=0; i<n; i++)
#define rrep(i,n) for(ll i=n-1; i>=0; i--)
#define print(a) cout<<a<<endl
typedef long long ll;
#define yn(flg) if(flg){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define YN(flg) if(flg){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define so(a) sort(a.begin(),a.end())
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define vs vector<string>
#define pb push_back
#define a2i(a,s) (ll)(a-s)
#define i2a(s,a) (char)(s+a)
#define ssize(a) a.size()
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<pair<ll,ll>,ll> P3;
typedef pair<pair<ll,ll>,pair<ll,ll>> P4;

typedef pair<ll, ll> Pll;
typedef pair<ll,Pll> Plll;
typedef pair<Pii, int> Piii;
const ll INF = 1000000000000000000;

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

//vector<ll> saiki(ll bit,ll n){
//	vector<ll> s(n);
//	for(ll i=0;i<n;i++){
//		if(bit&(1<<i)){
//			s[i]=1;
//		}else s[i]=0;
//	}
//	return s;
//}
vector<ll> v(100009);
int main(){
//入力

	cin.tie(0);
	ios::sync_with_stdio(0);

	ll n,m;
	cin>>n>>m;
	priority_queue<Pll,vector<Pll>,greater<Pll> > que;
	ll ans=0,mi=INF;
	rep(i,m){
		ll t,h;
		cin>>t>>h;
		ans+=t;
		v[h]=1;
		que.push({h,t});
		mi=min(mi,h);
	}
//判定
	queue<ll> que2;
	for(ll i=mi+1;i<=n;i++){
		if(v[i]!=1)que2.push(i);
	}

	while(!que.empty()&&!que2.empty()){
		ll cost=que.top().second;
		que.pop();
		ll H=que2.front();
		que2.pop();
		ans+=cost;
		que.push({H,cost});
	}
	cout<<ans<<endl;

	return 0;

}
accuracy: 1
time_ms: 10
memory_kb: 5940
final_rank: 8

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

int main(){
    int n, m;
    cin >> n >> m;
    int ans = 0;
    vector<int> T(m);
    set<array<int, 2>> hito;
    set<int> ie;
    for(int i = 0; i < n; i++) ie.insert(i);
    for(int i = 0; i < m; i++){
        int t, h;
        cin >> t >> h;
        T[i] = t;
        h--;
        ans += t;
        ie.erase(h);
        hito.insert({h, i});
    }
    
    while(hito.size() && ie.size()){
        int pos = (*hito.begin())[0];
        int hi = (*hito.begin())[1];
        hito.erase(hito.begin());
        auto itr = ie.lower_bound(pos);
        if(itr == ie.end()) continue;
        
        hito.insert({*itr, hi});
        ie.erase(itr);
        ans += T[hi];
    }
    
    cout << ans << '\n';
}
accuracy: 1
time_ms: 70
memory_kb: 8132
final_rank: 20

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

int main()
{
    int n, m;
    cin >> n >> m;
    vector<ll> zei(n, 0);
    for (int i = 0; i < m; i++)
    {
        int t, h;
        cin >> t >> h;
        h--;
        zei[h] = t;
    }
    queue<ll> amount;
    for (int i = 0; i < n; i++)
    {
        if (zei[i] > 0)
        {
            amount.push(zei[i]);
        }
        else if (!amount.empty())
        {
            zei[i] = amount.front();
            amount.pop();
            amount.push(zei[i]);
        }
    }
    ll ans = 0;
    for (int i = 0; i < n; i++)
    {
        ans += zei[i];
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4864
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
    ll n,m;cin >> n >> m;
    vector<ll> t(m),h(m);
    vector<ll> nh(n,-1);
    set<ll> al;
    for(ll i=0;i<m;i++){
        cin >> t[i] >> h[i];
        h[i]--;
        nh[h[i]]=t[i];
        al.insert(h[i]);
    }
    queue<ll> y;
    for(ll i=0;i<n;i++){
        if(!al.count(i)){
            y.push(i);
        }
    }
    for(ll i=0;i<n;i++){
        while(!y.empty()&&i>y.front()){
            y.pop();
        }
        if(nh[i]!=-1){
            if(y.empty()){
                continue;
            }
            nh[y.front()]=nh[i];
            y.pop();
        }else{
            nh[i]=0;
        }
    }
    for(ll i=1;i<n;i++){
        nh[i]+=nh[i-1];
    }
    cout << nh[n-1] <<endl;
}
accuracy: 1
time_ms: 40
memory_kb: 9124
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(T) T.begin(), T.end()
using ll = long long;
using P = pair<int, int>;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
    ll n, m;
    cin >> n >> m;
    vector<ll> v(n, 0);
    rep(i, m) {
        ll t, h;
        cin >> t >> h;
        h--;
        v[h] = t;
    }
    vector<ll> emp;
    rep(i, n) if (v[i] == 0) emp.emplace_back(i);

    ll li = 0;
    ll ans = 0;
    ll empsz = emp.size();
    rep(i, n) {
        if (v[i] == 0) {
            li++;
            continue;
        }
        ans += v[i];
        if (li < empsz) v[emp[li]] = v[i];
        li++;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 5344
final_rank: 4

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

int main() {
    int N,M;
    int64_t ans = 0;
    cin >> N >> M;
    vector<int> housemoney(N,0);
    for(int i = 0; i < M;i++) {
        int t,h;
        cin >> t >> h;
        h--; //house number starts with 0
        ans += t;
        housemoney.at(h) = t;
    }
    queue<int> house;

    for(int i = 0; i < N;i++) {
        if (housemoney.at(i) != 0) {
            house.push(housemoney.at(i));
        } else {
            if(!house.empty()) {
                ans += house.front();
                int u = house.front();
                house.pop();
                house.push(u);
            }
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 4096
final_rank: 1

code_snippet: // author: Yuichiro Kurose

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<int> t(m), h(m);
  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que;
  for (int i = 0; i < m; i++) {
    cin >> t[i] >> h[i];
    h[i]--;
    que.push(make_pair(h[i], i));
  }
  set<int> st;
  for (int i = que.top().first; i < n; i++) {
    st.insert(i);
  }
  for (int i = 0; i < m; i++) {
    st.erase(h[i]);
  }
  int ans = 0;
  while (!que.empty()) {
    auto [idx, num] = que.top();
    que.pop();
    ans += t[num];
    if (!st.empty()) {
      int nidx = *begin(st);
      que.push(make_pair(nidx, num));
      st.erase(nidx);
    }
  }
  cout << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 8948
final_rank: 13

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

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

int main(){
    int n,m;
    cin>>n>>m;
    vector<int> pay(n,0);
    vector<int> who(n,0);
    for(int i=0;i<m;i++)
    {
        int t,h;
        cin>>t>>h;
        h--;
        pay[h] = t;
        who[h] = t;
    }
    
    int target = 0;
    for(int i=0;i<n;i++) if(who[i])
    {
        target = max(target , i + 1);
        while(target < n && pay[target] != 0) target++;
        if(target == n) break;
        swap(who[target] , who[i]);
        pay[target] = who[target];
    }
    int ans = 0;
    for(auto payi : pay) ans += payi;
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3880
final_rank: 3

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

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

int main(){
    int n,m;
    cin>>n>>m;
    vector<int> pay(n,0);
    vector<int> taxer(m);
    vector<int> place(n,-1);
    for(int i=0;i<m;i++)
    {
        int t,h;
        cin>>t>>h;
        h--;
        taxer[i] = t;
        pay[h] = t;
        place[h] = i;
    }
    //for(auto placei : place) cout<<placei<<" ";
    ///cout<<endl;
    int target = 0;
    for(int i=0;i<n;i++) if(place[i] != -1)
    {
        target = max(target , i + 1);
        while(target < n && pay[target] != 0) target++;
        if(target == n) break;
        swap(place[target] , place[i]);
        //cout<<target<<" "<<place[target]<<" "<<taxer[place[target]]<<endl;
        pay[target] = taxer[place[target]];
    }
    int ans = 0;
    for(auto payi : pay) ans += payi;
    cout<<ans<<endl;
    //cout<<ans<<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 4064
final_rank: 6

code_snippet: #include <iostream>
#include <vector>
#include <queue>
#include <tuple>
#include <algorithm>
#include <functional>
#include <utility>
#include  <map>
using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;

int main() {
	int N, M;
	ll ans = 0;
	queue<int> que;
	map<int, int> m;
	cin >> N >> M;
	for (int i = 0, t, h; i < M; i++) {
		cin >> t >> h;
		m[h] = t;
	}

	for (int i = 1; i <= N; i++) {
		if (m[i] != 0) {
			ans += m[i];
			que.push(m[i]);
		}
		else if (!que.empty()) {
			ans += que.front();
			que.push(que.front());
			que.pop();
		}
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 8392
final_rank: 15

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

int main()
{
    int n, m; cin >> n >> m;
    vector<int> t(m);
    vector<int> h(m);
    vector<pair<int, int>> homes(n+1, {0, 0});
    for (int i = 0; i < m; i++)
    {
        cin >> t[i] >> h[i];
        homes[h[i]].first = t[i];
        homes[h[i]].second = 2;
    }
    
    queue<int> q;
    for (int i = 1; i <= n; i++)
    {
        if (homes[i].second == 0)
        {
            if (!q.empty())
            {
                int front = q.front();
                homes[i].first = homes[front].first;
                homes[i].second = 2;
                q.pop();
            }
        }
        if (homes[i].second == 1)
        {
            continue;
        }
        if (homes[i].second == 2)
        {
            q.push(i);
        }
    }
    int tax = 0;
    for (int i = 1; i <= n; i++)
    {
        tax += homes[i].first;
    }
    cout << tax << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 4800
final_rank: 10

code_snippet: # include <algorithm>
# include <bitset>
# include <complex>
# include <cmath>
#define _USE_MATH_DEFINES
#include <assert.h>
# include <math.h>
# include <deque>
# include <exception>
# include <fstream>
# include <functional>
# include <iomanip>
# include <ios>
# include <iosfwd>
# include <iostream>
# include <istream>
# include <iterator>
# include <limits>
# include <list>
# include <locale>
# include <map>
# include <memory>
# include <new>
# include <numeric>
# include <ostream>
# include <queue>
# include <set>
# include <sstream>
# include <stack>
# include <stdexcept>
# include <streambuf>
# include <string>
# include <typeinfo>
# include <utility>
# include <valarray>
# include <vector>
# include <array>
# include <atomic>
# include <chrono>
# include <condition_variable>
# include <forward_list>
# include <future>
# include <initializer_list>
# include <mutex>
# include <random>
# include <ratio>
# include <regex>
# include <scoped_allocator>
# include <system_error>
# include <thread>
# include <tuple>
# include <typeindex>
# include <type_traits>
# include <unordered_map>
# include <unordered_set>
# include <bitset>
#pragma GCC target ("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i,n) for (ll i = 0;i < (ll)(n);i++)
#define Yes cout << "Yes" << "\n"// YESの短縮
#define No cout << "No" << "\n"// NOの短縮
#define rtr0 return(0)//return(0)の短縮
#define gyakugen(x) modpow(x,mod - 2,mod);
#define agyakugen(x) modpow(x,amod - 2,amod);
#define st(A) sort(A.begin(),A.end());
#define rst(A) sort(A.rbegin(),A.rend());
#define rev(A) reverse(A.begin(),A.end());
using namespace std;
//using namespace ranges;
using ll = long long;//63bit型整数型
using ld = long double;//doubleよりも長い値を保存できるもの
using ull = unsigned long long;//符号がない64bit型整数
//ld m_pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679;
ll mod = 998244353;
ll amod = 1000000007;
ll MINF = -5000000000000000000;
ll INF = 5000000000000000000;
ll inf = 2000000000;
ll minf = -2000000000;
ll BAD = -1;
ll zero = 0;
ld EPS = 1e-10;
vector<ll>randomhash = {(ll)1e9 + 7,(ll)1e9 + 801,((ll)1e8 * 8) + 29,((ll)1e8 * 7) + 159,((ll)1e8 * 9) + 221};
vector<ll>tate = {0,-1,0,1};//グリッド上の全探索時の四方向の上下のチェック
vector<ll>yoko = {1,0,-1,0};//グリッド上の全探索時の四方向の右左のチェック
vector<ll>etate = {0,-1,-1,-1,0,1,1,1};//グリッド上の全探索時の八方向の上下のチェック
vector<ll>eyoko = {1,1,0,-1,-1,-1,0,1};//グリッド上の全探索時の八方向の右左のチェック
vector<ll>hexsax = {0,1,1,0,-1,-1};
vector<ll>hexsay = {1,1,0,-1,-1,0};
//返り値は素数のリスト。
vector < bool > isprime;
vector < ll > Era(int n){//書き方 vector<ll>[] = Era(x); とやるとxまでの素数が出てくる。
	isprime.resize(n, true);
	vector < ll > res;
	isprime[0] = false;
	isprime[1] = false;
	for(ll i = 2; i < n; ++i) isprime[i] = true;
	for(ll i = 2; i < n; ++i) {
		if(isprime[i]) {
			res.push_back(i);
			for(ll j = i * 2; j < n; j += i) isprime[j] = false;
		}
	}
	return res;
}
ll ceil_div(ll a, ll b){
    if (b <= 0) return INF; // invalid (分母<=0 のときは不可能と扱う)
    return (a + b - 1) / b;
}
//　　　　　　素数判定 21~35
ull modmul(ull a,ull b,ull mod) {
    return (ull)a*b%mod;
}
ll modpow(ll a, ll n, ll mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}

long long npow(long long a, long long n){
    long long res = 1;
    while (n > 0) {
		if (n & 1) res = res * a;
		a = a * a;
		n >>= 1;
	}
	return res;
}
//     モッドを使った累乗
long long isqrt(long long num){
    return((long long)sqrtl(num));
}
ld get_theta(ld px,ld py,ld fx,ld fy,ld sx,ld sy){
    ld fxv = fx-px;
    ld fyv = fy-py;
    ld sxv = sx-px;
    ld syv = sy-py;
    ld fsv = fxv*sxv + fyv*syv;
    ld pfs = (ld)sqrt(fxv*fxv+fyv*fyv);
    ld pss = (ld)sqrt(sxv*sxv+syv*syv);
    return acos(fsv/(pfs*pss))*180/M_PI;
};
ld Euclidean_distance(ll x1,ll y1,ll x2,ll y2){
    ld x = abs((ld)x1 - (ld)x2);
    ld y = abs((ld)y1 - (ld)y2);
    return sqrt((x*x) + (y*y));
};
ll Manhattan_distance(ll x1,ll y1,ll x2,ll y2){
    return abs(x1-x2)+abs(y1-y2);
};
void change_bit(ll N,ll start,vector<ll>&X){
    for(ll i = start;i>=0;i--){
        ll a = npow(2,i);
        if(N/a==1){
            X[i]=1;
            N-=a;
        }
    }
}
ll popcount(ll x) {
    int count = 0;
    while (x) {
        count += x & 1;
        x >>= 1;
    }
    return count;
}

ll cutup(ll a,ll b){
    return (a+b-1)/b;
}
void Run_Length_Encoding(vector<ll>A,vector<pair<ll,ll>>&RLE){
    RLE.push_back({A[0],1});
    ll N = A.size();
    for(int i = 1;i<N;i++){
        if(RLE[RLE.size()-1].first==A[i])RLE[RLE.size()-1].second++;
        else RLE.push_back({A[i],1});
    }
}
bool is_in_grid(ll i,ll ii,ll H,ll W){
    bool res = false;
    if(i>=0&&i<H&&ii>=0&&ii<W)res=true;
    return res;
}
template<class T>
struct rolling_hash{
    vector<ull>Power,hash,InvPower;
    ll B = 0;
    ll MOD = 0;
    void set_number(ll base,ll mod){
        B = base;
        MOD = mod;
    }
    void do_hash(const T &S) {
        ll N = S.size();
        Power.resize(N+1);
        InvPower.resize(N+1);
        hash.resize(N+1);
        Power[0] = 1;
        InvPower[0] = 1;
        for (ll i = 0;i<N;i++) {
            Power[i + 1] = modmul(Power[i],B,MOD);
            InvPower[i + 1] = modpow(Power[i+1],MOD-2,MOD);
            hash[i + 1] = (hash[i] + modmul(S[i], Power[i],MOD))%MOD;
        }
    }
    ll get_hash(ll l, ll r) {
        ull res = (hash[r]+MOD-hash[l])%MOD;
        res = modmul(res,InvPower[l],MOD);
        return res;
    }
};
template<class T>
struct combination{
  public:
    vector<T>factorial;
    ll MOD;
    ll N;
    void reset(T n,T mod){
        N = n+1;
        factorial.assign(N,1);
        MOD = mod;
    }
    void calu(){
        for(ll i = 1;i<N;i++){
            factorial[i] = (factorial[i-1]*(i))%MOD;
        }
    }
    T get(T n, T r){
        ll a = factorial[n];
        ll b = (factorial[r]*(factorial[n-r]))%MOD;
        b = modpow(b,MOD - 2,MOD);
        return (a*b)%MOD;
    }
};
template<class T>
struct dijkstra{
  public:
	vector<vector<pair<T,T>>>graph;
	vector<T>ans;
	priority_queue<pair<T,T>,vector<pair<T,T>>,greater<pair<T,T>>>pq;
    void do_dijkstra(T start){//頂点xからダイクストラを行う
		pq.push({0,start});
		ans[start] = 0;
		while(true){
			if(pq.empty())break;
			T cost = pq.top().first;
			T vertex = pq.top().second;
			pq.pop();
            if (cost > ans[vertex]) continue; 
			for(T i = 0;i<graph[vertex].size();i++){
				T nextvertex = graph[vertex][i].first;
				T nextcost = graph[vertex][i].second + cost;
				if(ans[nextvertex] <= nextcost)continue;
					ans[nextvertex] = nextcost;
					pq.push({nextcost,nextvertex});
			}
		}
	}
	void make_indirectedgraph(T u,T v,T cost){//無向辺のグラフ作成
		graph[u].push_back({v,cost});
		graph[v].push_back({u,cost});
	}
	void make_directedgraph(T u,T v,T cost){//有向辺のグラフ作成
		graph[u].push_back({v,cost});
	}
	T output(T end){//答えを出す
		return ans[end];
	} 
	void reset(T N){//リセット
		graph.assign(N, {});
        ans.assign(N, INF);
	}
};
template<class T>
struct unionfind {
public:
    vector<T> parent, rank;
    void reset(T N) { // 初期化
        parent.resize(N);
        rank.assign(N, 1); // 各集合のサイズを 1 にする
        for (T i = 0; i < N; i++) parent[i] = i;
    }
    T leader(T x) { // 経路圧縮による親の取得
        if (parent[x] == x) return x;
        return parent[x] = leader(parent[x]); // 経路圧縮
    }
    void marge(T x, T y) { // ランクによるマージ
        T a = leader(x), b = leader(y);
        if(a!=b){
            if (rank[a] < rank[b]) swap(a, b);
            parent[b] = a;
            rank[a] += rank[b]; // サイズを更新    
        }
    }
    bool same(T x, T y) { // 同じ集合か判定
        return leader(x) == leader(y);
    }
    T size(T x) { // 集合のサイズを取得
        return rank[leader(x)];
    }
    void check(T N) { // デバッグ用: 親の確認
        for (T i = 0; i < N; i++) cout << parent[i] << " ";
        cout << "\n";
    }
};
//セグ木
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 = 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]); }
        int ceil_pow2(int n) {
            int x = 0;
            while ((1U << x) < (unsigned int)(n)) x++;
            return x;
        }
};

//遅延セグ木
template <class S,S (*op)(S, S),S (*e)(),class F,S (*mapping)(F, S),F (*composition)(F, F),F (*id)()>
struct lazy_segtree {

    public:
        lazy_segtree() : lazy_segtree(0) {}
        lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
        lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) {
            log = ceil_pow2(_n);
            size = 1 << log;
            d = std::vector<S>(2 * size, e());
            lz = std::vector<F>(size, id());
            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;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        return d[p];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return e();

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push(r >> i);
        }

        S sml = e(), smr = e();
        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]; }

    void apply(int p, F f) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = mapping(f, d[p]);
        for (int i = 1; i <= log; i++) update(p >> i);
    }
    void apply(int l, int r, F f) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return;

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        {
            int l2 = l, r2 = r;
            while (l < r) {
                if (l & 1) all_apply(l++, f);
                if (r & 1) all_apply(--r, f);
                l >>= 1;
                r >>= 1;
            }
            l = l2;
            r = r2;
        }

        for (int i = 1; i <= log; i++) {
            if (((l >> i) << i) != l) update(l >> i);
            if (((r >> i) << i) != r) update((r - 1) >> i);
        }
    }

    template <bool (*g)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return g(x); });
    }
    template <class G> int max_right(int l, G g) {
        assert(0 <= l && l <= _n);
        assert(g(e()));
        if (l == _n) return _n;
        l += size;
        for (int i = log; i >= 1; i--) push(l >> i);
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!g(op(sm, d[l]))) {
                while (l < size) {
                    push(l);
                    l = (2 * l);
                    if (g(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 (*g)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return g(x); });
    }
    template <class G> int min_left(int r, G g) {
        assert(0 <= r && r <= _n);
        assert(g(e()));
        if (r == 0) return 0;
        r += size;
        for (int i = log; i >= 1; i--) push((r - 1) >> i);
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!g(op(d[r], sm))) {
                while (r < size) {
                    push(r);
                    r = (2 * r + 1);
                    if (g(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;
        std::vector<F> lz;
        int ceil_pow2(int n) {
            int x = 0;
            while ((1U << x) < (unsigned int)(n)) x++;
            return x;
        }
        void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
        void all_apply(int k, F f) {
            d[k] = mapping(f, d[k]);
            if (k < size) lz[k] = composition(f, lz[k]);
        }
        void push(int k) {
            all_apply(2 * k, lz[k]);
            all_apply(2 * k + 1, lz[k]);
            lz[k] = id();
        }
};
struct Euler_Tour{//最小共通祖先,部分木のコスト,通り道のコスト,のグラフを作る部分(オイラーツアー)
    //必要なもの集
    //セグ木は2*Nがベース
    //LCA(最小共通祖先)discovery,finish,visit,high
    //とある頂点xを根とする部分木の範囲 discovery,finish
    //頂点xを根とする部分木の辺と頂点のコストの総和 discovery,finish,vcost1,ecost1
    //頂点xを根とする頂点yまでの最短の辺と頂点のコストの総和 discovery,finish,vcost2,ecost2
    vector<int>discovery,finish,visit,high,vweight,vcost1,vcost2,ecost1,ecost2;
    vector<int>other;
    vector<vector<pair<int,int>>>graph;
    int cnt;
    int othercnt;
    void reset(ll N){
        cnt = 0;
        othercnt = 0;
        other.assign(2*N,0);
        discovery.assign(N,inf);
        finish.assign(N,minf);
        vweight.assign(N,0);
        graph.assign(N,vector<pair<int,int>>(0));
        visit.assign(2*N,0);
        high.assign(2*N,0);
        vcost1.assign(2*N,0);
        vcost2.assign(2*N,0);
        ecost1.assign(2*N,0);
        ecost2.assign(2*N,0);
    };
    void make_graph(ll u,ll v,ll cost){
        if(max(u,v)>graph.size()){
            cout << "not reset or out of range" << "\n";
            return;
        }
        graph[u].push_back({v,cost});
        graph[v].push_back({u,cost});
    }
    void set_vweight(ll vertex,ll cost){
        vweight[vertex]+=cost;
    }
    void DFS(ll vertex,ll nowdepth,ll cost){
        if(discovery[vertex]==inf){
            vcost1[cnt]=vweight[vertex];
            vcost2[cnt]=vweight[vertex];
        }
        else{
            vcost1[cnt]=0;
            vcost2[cnt]=0;
        }
        ecost1[cnt]=cost;
        ecost2[cnt]=cost;
        discovery[vertex]=min(discovery[vertex],cnt);
        visit[cnt]=vertex;
        high[cnt]=nowdepth;
        for(int i = 0;i<graph[vertex].size();i++){
            int x = graph[vertex][i].first;
            int nextcost = graph[vertex][i].second;
            if(discovery[x]!=inf&&graph[vertex].size()==1)othercnt++;
            other[cnt]=othercnt;
            if(discovery[x]==inf){
                cnt++;
                DFS(x,nowdepth+1,nextcost);
                cnt++;
                visit[cnt]=vertex;
                high[cnt]=nowdepth;
                vcost1[cnt]=0;
                ecost1[cnt]=0;
                vcost2[cnt]=vweight[x]*-1;
                ecost2[cnt]=nextcost*-1;
            }
            other[cnt]=othercnt;
        }
        finish[vertex]=cnt+1;
        return;
    }
    int get_size(){
        ll res = high.size()-1;
        return res;
    }
    int get_vcost1(ll i){
        return ecost1[i];
    }
    int get_vcost2(ll i){
        return vcost2[i];
    }
    int get_ecost1(ll i){
        return vcost1[i];
    }
    int get_ecost2(ll i){
        return ecost2[i];
    }
    int get_high(ll i){
        return high[i];
    }
    int get_visit(ll i){
        return visit[i];
    }
    int get_discovery(ll i){
        return discovery[i];
    }
    int get_finish(ll i){
        return finish[i];
    }
};
//遅延セグ木のベース
//型
using segS = ll;
segS oop(segS a,segS b){
    return (a+b)%mod;
}
segS ee(){
    return 0;
}
/*struct segS{
    ll mi = 0;
    ll ma = 0;
};
segS oop(segS a,segS b){
    segS res;
    res.mi = min(a.mi,b.mi);
    res.ma = max(a.ma,b.ma);
    return res;
}
segS ee(){
    segS res;
    res.mi = INF;
    res.ma = MINF;
    return res;
}*/

//using lazyS = ll;
using lazyS = ll;
using lazyF = ll;
lazyS e(){
    lazyS res;
    res = 1;
    return res;
}//範囲外などを起こしてしまったときの返り値
lazyS op(lazyS a, lazyS b){
    lazyS res;
    res = a+b;
    return res; 
}//操作したいこと
lazyS mapping(lazyF f, lazyS x){ 
    x= (x*f)%mod;
    return x; 
}//下まで伝播させたいこと
lazyF composition(lazyF f, lazyF g){ return g = (g*f)%mod; }//まとめてやった場合おこしたいこと
lazyF id(){ return 1; }//遅延セグ木のベース
/*
using lazyS = ll;
using lazyF = ll;
lazyS e(){ return 0;}//範囲外などを起こしてしまったときの返り値
lazyS op(lazyS a, lazyS b){ return a+b; }//操作したいこと
lazyS mapping(lazyF f, lazyS x){ return x /= f; }//下まで伝播させたいこと
lazyF composition(lazyF f, lazyF g){ return g /= f; }//まとめてやった場合おこしたいこと
lazyF id(){ return 1; }//遅延セグ木のベース
*/

int main(){
ll N,M;
cin >> N >> M;
vector<pair<ll,ll>>A(M);//money,place;
vector<bool>visited(N,false);
queue<ll>que;
set<pair<ll,ll>>P;
ll ans = 0;
ll start = INF;
for(int i = 0;i<M;i++){
    ll x,y;
    cin >> x >> y;
    y--;
    A[i] = {x,y};
    ans+=x;
    visited[y]=true;
    start = min(start,y);
    P.insert({y,x});
}
for(int i = start;i<N;i++)if(!visited[i])que.emplace(i);
while(true){
    if(que.empty())break;
    auto[place,cost] = *P.begin();
    P.erase({place,cost});
    ll x = que.front();
    que.pop();
    ans+=cost;
    P.insert({x,cost});
}
cout << ans << "\n";
}
accuracy: 1
time_ms: 50
memory_kb: 10180
final_rank: 18

code_snippet: #include <bits/stdc++.h>
#include <iomanip>
#include <numbers>
#include <cassert>
#include <numeric>
#ifdef _MSC_VER
#include <intrin.h>
#endif
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pint = pair<int, int>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
template<typename T>
bool chmax(T & a, const T & b) { if (a < b) { a = b; return true; } return false; }
template<typename T>
bool chmin(T & a, const T & b) { if (a > b) { a = b; return true; } return false; }
int main() {
    int n, m; cin >> n >> m;
    set<int> house;
    rep(i, n)house.insert(i);
    set<pint> ps;
    int total = 0;
    rep(i, m) {
        int t, h; cin >> t >> h;
        h--;
        total += t;
        ps.insert({ h,t });
        house.erase(h);
    }
    while (1) {
        auto [h, t] = *ps.begin();
        ps.erase(ps.begin());
        auto it = house.upper_bound(h);
        if (it == house.end()) break;
        total += t;
        ps.insert({ *it,t });
        house.erase(it);
    }
    cout << total << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 8192
final_rank: 17

code_snippet: #include<algorithm>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<iomanip>
#include<utility>
#include<string>
#include<vector>
#include<bit>
#include<bitset>
#include<tuple>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<stack>
using namespace std;

void fix(int x){
  cout << fixed << setprecision(x);
}

int main(){
  long int N, M, ans;
  ans = 0;
  cin >> N >> M;
  vector<long int> A(M);
  vector<long int> B(M);
  vector<long int> C(N, 0);
  priority_queue<pair<long int, long int> > PQ;
  for (long int i = 0; i < M; i++){
    cin >> A.at(i) >> B.at(i);
    B.at(i)--;
    ans += A.at(i);
    C.at(B.at(i)) = 1;
    PQ.push(make_pair(100000000 - B.at(i), A.at(i)));
  }
  for (long int i = 0; i < N; i++){
    if (C.at(i) == 0 && 100000000 - PQ.top().first < i){
      long int D = PQ.top().second;
      C.at(i) = 1;
      ans += D;
      PQ.pop();
      PQ.push(make_pair(100000000 - i, D));
    }
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 7756
final_rank: 11

code_snippet: //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
using ll = long long;
const ll INF32 = 2e9;
const ll INF64 = 4e18;

void printYN(bool ok){
    if(ok)cout << "Yes" << endl;
    else cout << "No" << endl;
    return;
}

int main() {
    int N, M;
    cin >> N >> M;
    vector<int> t(M), h(N, -1);
    rep(i, M){
        int hi;
        cin >> t[i] >> hi;
        hi--;
        h[hi] = i;
    }
    queue<int> q;
    int ans = 0;
    rep(i, N){
        if(h[i]!=-1){
            ans += t[h[i]];
            q.push(h[i]);
        }
        else {
            if(q.empty())continue;
            ans += t[q.front()];
            q.push(q.front());
            q.pop();
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4040
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
int a[200001];
int main(void)
{
	int n,m,t,h,i,p,j,sum,flg;
	cin>>n>>m;
	for(i=0;i<m;i++){
		cin>>t>>h;
		a[h]=t;
	}
	flg=0;
	sum=0;
	for(i=1;i<=n;i++){
		if(a[i]>0){
			if(flg==0){
				p=i+1;
				while(a[p]>0)p++;
				flg=1;
			}
			sum+=a[i];
			a[p]=a[i];
			while(a[p]>0)p++;
		}
	}
	cout<<sum<<endl;
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4076
final_rank: 7