akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<iomanip>
#include<math.h>
#include<algorithm>
#include<utility>
#include<queue>
#include<string.h>
#include<string>
#include<set>
#include<map>
#include<unordered_map>
#include<functional>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const ll INF=2e18;
const ll MOD=1e9+7;

ll N,M;
string S;
ll near0[100010];
int main(){
    cin>>N>>M>>S;
    for(ll i=0;i<=N;i++){
        near0[i]=i;
    }
    for(ll i=N-1;i>=N-M;i--){
        if(S[i]=='0'){
            near0[N]=i;
        }
    }
    for(ll i=N-1;i>M;i--){
        if(S[i-M]=='0'){
            near0[i]=i-M;
        }else{
            near0[i]=near0[i+1];
        }
    }
    for(ll i=M;i>=0;i--){
        near0[i]=0;
    }
    ll now=N;
    vector<ll> ans;
    while(now>0){
        if(near0[now]<now){
            ans.push_back(now-near0[now]);
            now=near0[now];
        }else{
            cout<<-1<<endl;
            return 0;
        }
    }
    reverse(ans.begin(),ans.end());
    for(auto x:ans){
        cout<<x<<" ";
    }cout<<endl;
    return 0;
}

Measured memory_bytes: 2296
#include <stdio.h>
#include <string.h>

#pragma warning(disable:4996)

char S[200000 + 1];
int W[200000 + 2];
int B[200000 + 2];

int main(void) {
	int N;
	scanf("%d", &N);
	scanf("%s", &S[0]);

	int numW = 0;
	int numB = 0;
	for (int i = 0; i < N; ++i) {
		if (S[i] == '#') ++numW;
		if (S[N - 1 - i] == '.') ++numB;
		W[i+1] = numW;
		B[N-1-i+1] = numB;
	}
	if (numB == 0) {
		printf("0");
		return 0;
	}
	int min = N;
	for (int i = 0; i < N+1; ++i) {
		if (W[i] + B[i+1] < min) {
			min = W[i] + B[i+1];
		}

	}
	printf("%d", min);

	return 0;
}

Measured memory_bytes: 1920
#include "bits/stdc++.h"
using namespace std; 
#define LIM 100003

#define M 1000000007
#define inf 99999999999999999LL	//long long inf
typedef long long ll;

#define sc(x) scanf("%d",&x);
#define sc2(x,y) scanf("%d%d",&x,&y);
#define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z);
#define scl(x) scanf("%lld",&x);
#define scl2(x,y) scanf("%lld%lld",&x,&y);
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z);

#define pb push_back
#define mp make_pair
#define fi first
#define se second 
ll power(ll x,ll y,ll z) {	ll ret = 1;	while(y > 0) {	if(y & 1) ret = (ret*x)%z;x = (x*x)%z;y >>= 1;}return ret;}


#define debug(x) cerr<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define debug4(x,y,z,a) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\t"<<#a<<" :: "<<a<<"\n";



ll n, arr[LIM][3], dp[LIM][3] ;
ll doit(ll ind, ll last) {
	if(ind > n)
		return 0;
	ll &dpAnswer = dp[ind][last] ;
	if( dpAnswer != -1 )
		return dpAnswer ;
	ll answer = 0 ;
	for(ll i=0; i<3;i++) {
		if(last == i)
			continue;
		answer = max(answer, arr[ind][i]+doit(ind +1, i)) ;
	}
	return dpAnswer = answer ;
}
int main() {
	int i, j, t;
	memset(dp, -1, sizeof dp) ;
	scl(n) ;
	for(i=1; i<=n; i++)
		scl3(arr[i][0], arr[i][1], arr[i][2]) ;
	ll answer = doit(1, -1) ;
	cout << answer << endl ;
	return 0;
}




Measured memory_bytes: 12800
#include<iostream>
#include<algorithm>

using namespace std;

static const int MMAX = 20;
static const int NMAX = 50000;
static const int INFTY = (1 << 29);

int main() {
  int n, m;
  int C[21];
  int T[NMAX + 1];

  cin >> n >> m;

  for ( int i = 1; i <= m; i++ )
    cin >> C[i];

  for ( int i = 0; i <= NMAX; i++ ) T[i] = INFTY;
  T[0] = 0;
  for ( int i = 1; i <= m; i++ )
    for ( int j = 0; j + C[i] <= n; j++ )
      T[j + C[i]] = min(T[j + C[i]], T[j] + 1);

  cout << T[n] << endl;

  return 0;
}


Measured memory_bytes: 3228
#include<iostream>
#include<vector>
#include<queue>
#include<stack>

using namespace std;

int main(){
	while(1){
		string str; getline(cin,str);
		stack<char> st ;
		if (str == ".") break;
		//cout << str.size() << endl;
		//cout << str << endl;
		for (int i = 0; i < str.size(); i++){
			if (str[i] == '('){
				st.push(str[i]);
			}else if (str[i] == '['){
				st.push(str[i]);
			}else if (str[i] == ')'){
				if (st.empty()){
					st.push(str[i]);
				}else if (st.top() == '('){
					st.pop();
				}else{
					st.push(str[i]);
				}
			}else if (str[i] == ']'){
				if (st.empty()){
					st.push(str[i]);
				}else if (st.top() == '['){
					st.pop();
				}else{
					st.push(str[i]);
				}
			}
		}
		if (st.empty()){
			cout << "yes" << endl;
		}else{
			cout << "no" << endl;
		}
	
	}
return 0;
}

Measured memory_bytes: 3128
#include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const long long MOD = 1000000007;
const long long INF = 1LL << 60;
const int INT_INF = 1000000000;

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

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, H;
  cin >> N >> H;
  vector<ll> a(N), b(N);
  for (int i = 0; i < N; i++)
    cin >> a[i] >> b[i];
  sort(a.begin(), a.end(), greater<ll>());
  sort(b.begin(), b.end());
  int index = upper_bound(b.begin(), b.end(), a[0]) - b.begin();
  ll ans = 0;
  for (int i = N - 1; i >= index; i--)
  {
    H -= b[i];
    ans++;
    if (H <= 0)
      break;
  }
  if (H > 0)
  {
    ans += (H + a[0] - 1) / a[0];
  }
  cout << ans << endl;
  return 0;
}

// 投げる方が強いやつはとりあえず投げとく
// ai <= biであるから最後に投げるのは確定
// aは何回も使えるがbは1回しか使えない


Measured memory_bytes: 1792
n = int(input())
h_list = list(map(int, input().split()))
ans = 0
for i in range(max(h_list)):
    count = 0
    b_list = [False]
    for j in range(n):
        if h_list[j] >= i+1 and b_list[-1] == False:
            b_list.append(True)
        elif h_list[j] < i+1 and b_list[-1] == True:
            b_list.append(False)


    ans += sum(b_list)
print(ans)


Measured memory_bytes: 3060
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long lint;

struct solver_t {
  struct entry_t {
    int a, b, id;
    bool operator<(const entry_t &o) const {
      int diff = b - a;
      int o_diff = o.b - o.a;
      if (diff != o_diff)
        return diff > o_diff;
      return id < o.id;
    }
  };
  int choose_count;
  int current_id = 0;
  lint result = 0;
  set<entry_t> included_set;
  solver_t(int _choose_count): choose_count(_choose_count) {}
  void add_values(int a, int b) {
    entry_t new_entry = (entry_t){a, b, current_id++};
    included_set.insert(new_entry);
    result += new_entry.b;

    if ((int)included_set.size() > choose_count) {
      entry_t to_remove = *included_set.rbegin();
      result -= to_remove.b;
      result += to_remove.a;
      included_set.erase(to_remove);
    }
  }
  lint get_result() { return result; }
};

int main() {
  int na, nb, nc;
  scanf("%d %d %d", &na, &nb, &nc);
  const int n = na + nb + nc;
  struct person_t {
    int a, b, c;
  };
  vector<person_t> people(n);
  for (int i = 0; i < n; ++i)
    scanf("%d %d %d", &people[i].a, &people[i].b, &people[i].c);
  sort(people.begin(), people.end(),
       [](const person_t &pa, const person_t &pb) {
         int diff_a = pa.b - pa.c;
         int diff_b = pb.b - pb.c;
         return diff_a > diff_b;
       });

  vector<lint> prefix_result(n, LLONG_MIN);
  solver_t solver_b(nb);
  for (int i = 0; i < n; ++i) {
    solver_b.add_values(people[i].a, people[i].b);
    if (i >= nb - 1)
      prefix_result[i] = solver_b.get_result();
  }

  vector<lint> suffix_result(n, LLONG_MIN);
  solver_t solver_c(nc);
  for (int i = n - 1; i >= 0; --i) {
    solver_c.add_values(people[i].a, people[i].c);
    if (i <= n - nc)
      suffix_result[i] = solver_c.get_result();
  }

  lint result = 0;
  for (int i = nb - 1; i < n - nc; ++i)
    result = max(result, prefix_result[i] + suffix_result[i + 1]);
  printf("%lld\n", result);

  return 0;
}


Measured memory_bytes: 9216
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int[] h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }

        int cnt = 0;
        while (true) {
            int l = n;
            int r = n-1;

            for (int i = 0; i < n; i++) {
                if (h[i] > 0) {
                    l = Math.min(l, i);
                } else if (l < i) {
                    r = i-1;
                    break;
                }
            }

            if (l == n)
                break;

            int min = 110;
            for (int i = l; i <= r; i++) {
                min = Math.min(min, h[i]);
            }
            for (int i = l; i <= r; i++) {
                h[i] -= min;
            }
            cnt += min;
        }

        System.out.println(cnt);
    }
}


Measured memory_bytes: 22612
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_set>
using namespace std;


int main() {
	string s, t;
	cin >> s >> t;

	vector<vector<size_t>> poss(26);

	for (size_t i = 0; i < s.size(); ++i) {
		poss[s[i] - 'a'].push_back(i);
	}

	size_t n = 0;
	for (size_t i = 0; i < t.size(); ++i) {
		vector<size_t> const& pos = poss[t[i] - 'a'];
		auto it = lower_bound(pos.begin(), pos.end(), n % s.size());
		if (it != pos.end()) {
			n = (n / s.size()) * s.size() + *it + 1;
			continue;
		}
		n = (1 + n / s.size()) * s.size();
		auto jt = lower_bound(pos.begin(), pos.end(), n % s.size());
		if (jt == pos.end()) {
			cout << -1 << endl;
			return 0;
		}
		n = (n / s.size()) * s.size() + *jt + 1;
	}

	cout << n << endl;
	return 0;
}

Measured memory_bytes: 1724
#include <bits/stdc++.h>
using namespace std;

int main(){
    int N;
    cin >> N;
    //N += 1;
    vector<int> p(N+1);//Nに到達するまでの最小引き出し数
    p.at(0) = 0;
    vector<int> K(2);
    K.at(0) = 6;
    K.at(1) = 9;
    for(int i = 1; i <= N; i++){
        //cout << i;
        p.at(i)  = p.at(i-1) + 1;
        for(int j =0 ;j<2;j++){
            int amount = K.at(j);
            while(i >= amount ){
                if(p.at(i) > p.at(i-amount)+ 1) p.at(i)  = p.at(i-amount) + 1;
                amount = amount*K.at(j);
            }
        }
            
    }
    cout << p.at(N);
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;

int main() {
  int A, B;
  char op;
  cin >> A >> op >> B;
  int result;
  if      (op == '+') result = A + B;
  else if (op == '-') result = A - B;
  cout << result << endl;
  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef int64_t ll;
typedef long double ld;
const ll MOD=1000000007;
const ll MODA=998244353;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
long long gcd(long long a,long long b){
    ll gcdmax=max(a,b);
    ll gcdmin=min(a,b);
    while(true){
        if(gcdmax%gcdmin==0)break;
        else gcdmax%=gcdmin;
        swap(gcdmin,gcdmax);
    }
    return gcdmin;
}
ll powerup(ll N,ll P,ll M){
    if(P==0)return 1;
    else if(P%2==0){
        ll t=powerup(N,P/2,M);
        return t*t%M;
    }
    else return N*powerup(N,P-1,M)%M;
}
vector<ll> find_divisor(ll N){
  ll k=1;
  while(k*k<=N){
    k++;
  }
  vector<ll> A(1);
  rep(i,k){
    if(i==1)A.at(0)=1;
    else if(i>=2){
      if(N%i==0)A.push_back(i);
    }
  }
  ll t=0;
  t=A.size();
  rep(i,t){
    if(A.at(t-i-1)*A.at(t-i-1)!=N)A.push_back(N/A.at(t-1-i));
  }
  return A;
}
vector<ll> fac;
vector<ll> finv;
vector<ll> inv;
void COMinit(ll N,ll P){
    rep(i,N+1){
        if(i==0){
            fac.push_back(1);
            finv.push_back(1);
            inv.push_back(1);
        }
        else if(i==1){
            fac.push_back(1);
            finv.push_back(1);
            inv.push_back(1);
        }
        else{
            fac.push_back(fac.at(i-1)*i%P);
            inv.push_back(P-inv.at(P%i)*(P/i)%P);
            finv.push_back(finv.at(i-1)*inv.at(i)%P);
        }
    }
}

ll COM(ll n,ll k,ll P){
    if(n<k)return 0;
    if(n<0||k<0)return 0;
    return fac.at(n)*(finv.at(k)*finv.at(n-k)%P)%P;
}
int main()
{
  ll N;
  cin>>N;
  vector<ll> A;
  vector<ll> B(1000010);
  rep(i,N){
    ll K;
    cin>>K;
    if(K>1)A.push_back(K);
  }
  ll L=A.size();
  if(L>1){
  ll G=A.at(0);
  rep(i,L){
    G=gcd(G,A.at(i));
  }
  if(G==1&&L<90000){ 
    bool TF=true;
    rep(i,L){
      vector<ll> D=find_divisor(A.at(i));
      ll M=D.size();
      if(M>1){
        for(ll j=1;j<M;j++){
          B.at(D.at(j))++;
          if(B.at(D.at(j))>1)TF=false;
        }
      }
    }
    if(TF)cout<<"pairwise coprime"<<endl;
    else cout<<"setwise coprime"<<endl;
  }
  else if(L<N)cout<<"setwise coprime"<<endl;
  else if(G==1)cout<<"setwise coprime"<<endl;
  else cout<<"not coprime"<<endl;
  }
  else cout<<"pairwise coprime"<<endl;
}

Measured memory_bytes: 19236
import java.util.*;
import static java.lang.System.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(in);
//処理
int H=sc.nextInt();
int W=sc.nextInt();
int[][] a=new int[H][W];
for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
        a[i][j]=sc.nextInt();
    }
}
String[] s=new String[W*H*2];
int c=0;
for (int i = 0; i < H; i++) {
    if(i%2==0){
        for (int j = 0; j < W; j++) {
            if(a[i][j]%2==1){
                a[i][j]--;
                if(j!=W-1){
                    s[++c]=String.valueOf(i+1)+" "+String.valueOf(j+1)+" "+String.valueOf(i+1)+" "+String.valueOf(j+2);
                    a[i][j+1]++;
                }else if(i!=H-1){
                    s[++c]=String.valueOf(i+1)+" "+String.valueOf(j+1)+" "+String.valueOf(i+2)+" "+String.valueOf(j+1);
                    a[i+1][j]++;
                }
            }
        }
    }else{
        for (int j = W-1; j >=0 ; j--) {
            if(a[i][j]%2==1){
                a[i][j]--;
                if(j!=0){
                    s[++c]=String.valueOf(i+1)+" "+String.valueOf(j+1)+" "+String.valueOf(i+1)+" "+String.valueOf(j);
                    a[i][j-1]++;
                }else if(i!=H-1){
                    s[++c]=String.valueOf(i+1)+" "+String.valueOf(j+1)+" "+String.valueOf(i+2)+" "+String.valueOf(j+1);
                    a[i+1][j]++;
                }
            }
        }
    }
}
s[0]=String.valueOf(c);
for (int j = 0; j <= c; j++) {
    out.println(s[j]);
}
}
}

Measured memory_bytes: 127208
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm> // sort

#define REP(i, n) for (long long i = 0; i < (n); i++) 
typedef long long ll;
static const ll MOD = 1000000007;
static const ll INF = 1000000000000000000LL;
using namespace std;


int main() {
    vector <int> cnt(3);
    string res;

    string s;
    cin >> s;
    REP(i, s.size())
    {
        cnt[s[i] - 'a']++;
    }

    //max(cnt[0]) がその他より多いとだめそう
    //cnt[0]cnt[1]だけ多くてもダメそう
    //maxとminの差が1以下ならいけそう

    int imax = 0;
    int imin = MOD;
    REP(i,3)
    {
        imax = max(imax, cnt[i]);
        imin = min(imin, cnt[i]);
    } 

    if(imax - imin <= 1)res = "YES";
    else res = "NO";

    cout << res << endl;

    return 0;
}



Measured memory_bytes: 512
//file_name:ABC52_D.cpp
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
inline int in() { int x; scanf("%d",&x); return x;}
template<typename T>inline istream& operator>>(istream&i,v(T)&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const v(T)&v)
{stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>inline ostream& operator<<(ostream&o,const v(T)&v)
{if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<typename T1,typename T2>inline ostream& operator<<(ostream&o,const pair<T1,T2>&v)
{return o<<v.fi<<","<<v.se;}
template<typename T>inline ll suma(const v(T)& a) { ll res(0); for (auto&& x : a) res += x; return res;}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
const int MX = 200005;

    // int scan
    /*
    int x;
    scanf("%d",&x);
    int y;
    scanf("%d",&y);
    int z;
    scanf("%d",&z);

    // matrix scan
    /*
    ll a[n] = {};
    rep(i,n){
        scanf("%lld",&a[i]);
    }
    */

    // string scan
    /*
    string s;
    cin >> s;
    */


int main() {
    ll n,a,b;
    scanf("%lld%lld%lld",&n,&a,&b);

    ll x[n] = {};
    rep(i,n){
        scanf("%lld",&x[i]);
    }
    ll ans = 0;
    rep(i,n-1){
        ans += min((x[i+1]-x[i])*a,b);
    }





    cout << ans << endl;
    return 0;
}



Measured memory_bytes: 1024
#include <iostream>
using namespace std;
int main(void){
    int k,x;
    cin>>k>>x;
    
    for(int i=x-(k-1);i<=x+(k-1);i++){
        if(i>=-1000000 && i<=1000000)cout<<i<<" "<<flush;
    }
    cout<<endl;
    
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
long double p[3000];
long double dp[3000][1501];

int main(){
      int n;
      cin>>n;
      for(int i=0; i<n; i++)
            {cin>>p[i+1];p[i+1]=1-p[i+1];/*cout<<p[i+1]<<" ";*/}
      
      dp[0][0]=1;
      dp[0][1]=0;

      for(int i=1; i<(n+1)/2; i++)
            dp[0][i]=0;

      for(int i=1; i<=n; i++){
            for(int j=0; j<(n+1)/2; j++){
                  dp[i][j]=(dp[i-1][j]*(1-p[i]))+(dp[i-1][j-1]*p[i]);
                  //cout<<i<<" "<<j<<' '<<dp[i][j]<<'\n';
            }
            //cout<<'\n';
      }
      for(int i=1; i<(n+1)/2; i++)
            {dp[n][i]+=dp[n][i-1];}

      cout<<fixed<<setprecision(10)<<dp[n][(n+1)/2-1];
}

Measured memory_bytes: 74148
#include<iostream>
using namespace std;
int main(){
string s;
int ta=0;
int tz=0;
int counta=0,countz=0,i;
cin >> s;
for(i=0;i<s.length();i++){

if(s[i]=='A' && ta==0)
{counta=i+1;
ta=1;
}
if(s[s.length()-i-1]=='Z' && tz==0)
{countz=s.length()-i;
tz=1;
}
if(ta==1 && tz==1){

cout<<countz-counta+1;
break;
}

}
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
int main() {
    int N; cin >> N;
    int sum = 0;
    vector<int> A(N); for (auto &a : A) cin >> a, sum ^= a;
    for (int i = 0; i < N; i++) {
        cout << (A[i] ^ sum) << " \n"[i + 1 == N];
    }
    return 0;
}


Measured memory_bytes: 4040
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n, a, b;
    cin >> n;

    int ans = 0;
    for (int i = 0; i < n; i++) {
        cin >> a >> b;
        ans += b - a + 1;
    }
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 256
let n = Scanf.scanf "%d\n" (fun x -> x)

let rec rev_read_ints accu = function
  | 0 -> accu
  | k -> rev_read_ints ((Scanf.scanf "%d\n" (fun x -> x)) :: accu) (k-1)

let a = List.rev (rev_read_ints [] n)

let rec solve = function
  | [] -> 0
  | [x] -> x / 2
  | hd1 :: 0 :: tl -> hd1 / 2 + solve tl
  | hd1 :: hd2 :: tl -> hd1 / 2 + solve ((hd2 + (hd1 mod 2)) :: tl)

let () = print_int (solve a)

Measured memory_bytes: 8832
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int a, b, c, k;
    int pt = 0;
    cin >> a >> b >> c >> k;

    for (int i = 0; i < k; i++)
    {
        if (a != 0)
        {
            pt++;
            a--;
        }
        else if (b != 0)
        {
            b--;
        }
        else if (c != 0)
        {
            pt--;
            c--;
        }
    }
    cout << pt << endl;
}

Measured memory_bytes: 3636
n, d = map(int, input().split())

def dis(y, z):
    return (sum([(y[i] - z[i]) ** 2 for i in range(d)]) ** .5).is_integer()

l = [[int(i) for i in input().split()] for _ in range(n)]

c = 0
for p in range(n - 1):
    for q in range(p + 1, n):
        c += dis(l[p], l[q])
print(c)

Measured memory_bytes: 3060
#include <stdio.h>
#include <stdlib.h>

int main() {
        int i, n, a;
        int *nr_sub;

        if (scanf("%6d", &n) == 0) {
                perror("scanf");
                return 1;
        }

        if ((nr_sub = malloc(sizeof(int) * n)) == 0) {
                perror("malloc");
                return 1;
        }

        for (i = 1; i < n; i++ ) {
                if (scanf(" %6d", &a) == 0) {
                        perror("scanf");
                        free(nr_sub);
                        return 1;
                }
                nr_sub[a-1]++;
        }

        for (i = 0; i < n; i++) {
                printf("%d\n", nr_sub[i]);
        }

        free(nr_sub);

        return 0;
}


Measured memory_bytes: 2224
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;

int main()
{
    int N, sum = 0;
    cin >> N;
    vector<int> A(N - 1);
    for (int j = 0; j < N - 1; j++)
    {
        cin >> A[j];
    }
    sum += A[0] + A[N - 2];
    for (int j = 1; j < N - 1; j++)
    {
        sum += min(A[j], A[j - 1]);
    }

    cout << sum << endl;
}



Measured memory_bytes: 256
#include "bits/stdc++.h"
#define rep(i,n) for (int i=0; i<(n); ++i)
using namespace std;
using ll =long long;
using P =pair<int,int>;

int main(){

    int n;
    cin >> n ;
    vector <int> a(n);
    map<int,int> m;
    int k=0;
    
    rep(i,n){
        cin >> a[i];
        ++m[a[i]];
        if (m[a[i]]>1){
            k=1;
        }
    }
    if (k==0){
        cout << "YES" << endl;
    }
    else{
        cout << "NO" << endl;
    }

    return 0;
}

Measured memory_bytes: 10368
n, k = map(int, input().split())
i_to_a = {i+1: int(a) for i, a in enumerate(input().split())}

seen_d = {i: 0 for i in range(1, n+1)}
seen = []

next_v = 1
loop_start_index = 0
while True:    
    if seen_d[next_v]:
        break
    seen_d[next_v] = 1
    seen.append(next_v)
    next_v = i_to_a[next_v]
loop_start_index = seen.index(next_v)
to_loop = seen[:loop_start_index]
additional = seen[:loop_start_index]
loop = seen[loop_start_index:]

additional_l = len(additional)
loop_l = len(loop)

if k > additional_l:
    s = (k - additional_l) % loop_l
    print(seen[additional_l + s])
else:    
    print(seen[k])

Measured memory_bytes: 140852
#include<bits/stdc++.h>
#include <iomanip>
using namespace std;

int main(){
    string S; cin >> S;
    
    int bw = 0,wb = 0;
    for(int i = 0; i < S.size(); i++){
        if(i % 2 == 0){
            if(S.at(i) == '0') wb++;
            else bw++;
        }
        if(i % 2 == 1){
            if(S.at(i) == '0') bw++;
            else wb++;
        }
    }
    cout << min(bw,wb) << endl;
}



Measured memory_bytes: 512
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <functional>
#include <numeric>

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

int main() {
    int n;
    vector<int> a;
    int ans = 0, temp = 0;

    cin >> n;

    for (int i = 0; i < n; i++) {
         int aa;
         cin >> aa;
         a.push_back (aa);
    }   

    for (int i = 0; i < n-1; i++) {
        if (a[i] >= a[i + 1]) temp++;
        else {
            if (temp > ans) ans = temp;
            temp = 0;
        }
    }

    
    if (temp > ans) ans = temp;
    cout << ans << endl;
}



Measured memory_bytes: 892
def solve():
    A, B, C, X, Y = map(int, input().split())
    C *= 2

    mn = float('inf')
    for i in range(max(X,Y)+1):
        price = 0
        price += C * i
        if Y - i > 0:
            price += B * (Y - i)
        
        if X - i > 0:
            price += A * (X - i)
        
        mn = min(price, mn)
    
    print(mn)

if __name__ == '__main__':
    solve()

Measured memory_bytes: 39536
#include <bits/stdc++.h>

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< pii >
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; cin >> x;
#define int2(x, y) Int(x); Int(y);
#define int3(x, y, z) Int(x); int2(y, z);
#define in(x, a, b) ((a) <= (x) && (x) < (b))
#define fir first
#define sec second
#define ffir first.first
#define fsec first.second
#define sfir second.first
#define ssec second.second
#define Decimal fixed << setprecision(10)

//int dxy[5] = {0, 1, 0, -1, 0};
// cmd

int dp[1000][30][30];

int calc(vi &src, vvi &rate, int targ)
{
    int ret = 0;
    rep(i, src.size()) {
        ret += rate[i][targ] * src[i];
    }
    return ret;
}

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

    int2(d, c);
    vvi data(c, vi(c));
    rep(i, c) rep(j, c) cin >> data[i][j];

    vvi table(3, vi(c, 0));
    rep(i, d) rep(j, d) {
        Int(tmp); tmp--;
        table[(i + j) % 3][tmp]++;
    }

    int ans = INF;
    rep(i, c) {
        rep(j, c) {
            if (i == j) continue;
            rep(k, c) {
                if (i == k || j == k) continue;
                ans = min(ans, 
                        calc(table[0], data, i) +
                        calc(table[1], data, j) +
                        calc(table[2], data, k));
            }
        }
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define int long long
using namespace std;

typedef pair<int,int> P;
typedef pair<int,P> P1;

int n, m, s, t;

class Dijkstra{
public:
  
  typedef tuple<int,int> T;
  long long INF = 1LL<<55;
  int V;
  vector<vector<T> > G;
  
  
  Dijkstra():V(-1){}
  Dijkstra(int V):V(V),G(V){}
  
  void add_edge(int a,int b,int c){
    assert(a >= 0 && b >= 0);
    assert(a < V && b < V);
    G[a].push_back(T(b,c));
    G[b].push_back(T(a,c));
  }

  vector<int> dijkstra(int start){
    vector<int> D(V,INF);
    vector<int> visited(V,0);
    priority_queue<T,vector<T>,greater<T> > Q;
    Q.push(T(0,start));
    D[start] = 0;
    while(!Q.empty()){
      int cost, pos;
      tie(cost,pos) = Q.top(); Q.pop();

      assert(!visited[pos] || D[pos] <= cost);
      if(visited[pos]++) continue;

      for(auto t:G[pos]){
        int to = get<0>(t);
        int ncost = cost + get<1>(t);
        if(D[to] <= ncost) continue;
        D[to] = ncost;
        Q.push(T(ncost,to));
      }
    }
    return D;
  }
};

signed main(){
  
  cin>>n>>m>>s>>t;
  s--;
  t--;
  
  Dijkstra A(n), B(n);
  
  for(int i=0;i<m;i++){
    int u, v, a, b;
    cin>>u>>v>>a>>b;
    u--, v--;
    A.add_edge( u, v, a );
    B.add_edge( u, v, b );
  }
  
  vector<int> d1 = A.dijkstra( s );
  
  vector<int> d2 = B.dijkstra( t );
  
  vector<int> ans(n);
  
  int mincost = 1e18;
  
  for(int i=n-1;i>=0;i--){
    
    mincost = min( mincost, d1[i] + d2[i] );
    
    ans[i] = 1e15 - mincost;
    
  }
  
  for(int i=0;i<n;i++) cout<<ans[i]<<endl;
  
  return 0;
}


Measured memory_bytes: 19364
#include <iostream> 
#include <string> 
#include <vector> 
#include <cmath> 
#include <algorithm> 
#include <cstdlib> 
#include <ctime> 
#include <cstdio> 
#include <functional> 
#include <set> 
#include <sstream> 
#include <cctype>
#include <stack>
#include <queue>
#include <cstring>
#include <map>
 
using namespace std; 

const long long INF=1LL<<50LL;
const double PI=3.14159265358979323846264;
const double eps=1e-8;
typedef vector<double> vec;
typedef vector<vec> mat;

vec gauss_jordan(const mat &A, const vec &b){
	int n=A.size(); //??£??????????????????
	mat B(n,vec(n+1));
	
	//?????§??????
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++) B[i][j]=A[i][j];
	}
	for(int i=0;i<n;i++) B[i][n]=b[i];
	
	for(int i=0;i<n;i++){
		int pivot=i;
		for(int j=i;j<n;j++){
			if(abs(B[j][i]) > abs(B[pivot][i])) pivot=j;
		}
		
		swap(B[i],B[pivot]);
		if(abs(B[i][i]) < eps) return vec();
		
		for(int j=i+1;j<=n;j++) B[i][j]/=B[i][i];
		for(int j=0;j<n;j++){
			if(i!=j){
				for(int k=i+1;k<=n;k++) B[j][k]-=B[j][i]*B[i][k];
			}
		} 
	}
	
	vec x(n);
	for(int i=0;i<n;i++) x[i]=B[i][n];
	return x;
	
}



int main(){

	double a,b,c,d,e,f;
	while(cin>>a>>b>>c>>d>>e>>f){
		
		mat A(2,vec(2));
		vec v(2);
		
		A[0][0]=a; A[0][1]=b;
		A[1][0]=d; A[1][1]=e;
		v[0]=c; v[1]=f;
		
		vec ans=gauss_jordan(A,v);
		printf("%.3f %.3f\n",ans[0],ans[1]);	
	}

	return 0;
}

Measured memory_bytes: 3192
from collections import deque
n,m, = map(int,input().split())
# ab =[list(map(int,input().split())) for i in range(m)]

v = [[i,-1] for i in range(n+1)]
v[1][1] = 0
for i in range(m):
    a,b = map(int,input().split())
    v[a].append(b)
    v[b].append(a)
dq = deque([])
dq.append(v[1])
# print(v)
while dq:
    t = list(deque.popleft(dq))
    # print(t)
    for i in t[1:]:
        if v[i][1] == -1:
            v[i][1] = t[0]
            dq.append(v[i])
print("Yes")
for i in range(n-1):
    print(v[i+2][1])

Measured memory_bytes: 98940
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define tt int t; cin>>t; while(t--)
#define nl cout<<"\n";
#define sp cout<<" ";
#define rep(i, a, b) for(long long i=a; i<b; i++)

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
double PI = 4*atan(1);

int main()
{
    optimize();

    int n, i;
    cin>> n;

    for(i=1; i*i<= n; i++)
    {
        if(n%i== 0 && n <= i*9)
            break;

    }


    cout << (i* i <=n ? "Yes\n" : "No\n");

    return 0;
}




Measured memory_bytes: 3624
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>
#include<limits>

#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ll, char> plc;

int main()
{
	int m, n;
	while (cin >> n >> m, n, m) 
	{
		int a[110] = { 0 };
		for (int i = 0; i < m; i++) 
		{
			char c; cin >> c;
			if (c == 'A') 
			{
				for (int j = 0; j < n; j++)if (!a[j]) { a[j] = 1; break; }
			}
			if (c == 'B') 
			{
				for (int j = n - 1; j >= 0; j--)if (!a[j]) { a[j] = 2; break; }
			}
			if(c == 'C')
			{
				if (!i)a[n / 2] = 3;
				else
				{
					for (int j = 0; j < n; j++) 
					{
						if (j != n - 1 && a[j] && !a[j + 1]) { a[j + 1] = 3; break; }
						if (j && a[j] && !a[j - 1]) { a[j - 1] = 3; break; }
					}
				}
			}
			if (c == 'D') 
			{
				if (!i)a[0] = 4;
				else
				{
					int mc = -1, mi = -1, x, y;
					for (int j = 0; j < n; j++) 
					{
						x = y = 999;
						for (int k = j; k >= 0; k--)if (a[k]) { x = j - k; break; }
						for (int k = j; k < n; k++)if (a[k]) { y = k - j; break; }
						if (x > y)x = y;
						if (mc < x)mc = x, mi = j;
					}
					a[mi] = 4;
				}
			}
		}
		for (int j = 0; j < n; j++)putchar("#ABCD"[a[j]]);
		puts("");
	}
	return 0;
}

Measured memory_bytes: 2908
#include <bits/stdc++.h>
#define INF 100000000000
#define ll long long
#define pll pair<ll, ll>
using namespace std;

int main()
{
  ll N, T;
  cin >> N >> T;
  ll ans = INF;
  for (ll i = 0; i < N; ++i)
  {
    ll c, t;
    cin >> c >> t;
    if (t <= T)
    {
      ans = min(ans, c);
    }
  }
  if (ans == INF)
  {
    cout << "TLE" << endl;
  }
  else
  {

    cout << ans << endl;
  }
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <climits>
using namespace std;

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

#define INF INT_MAX/3
#define REP(i,n) for(int i=0;i<n;i++)

int main(){
	ll N,A,B;
	cin>>A>>B>>N;
	
	ll answer = 0;
	if(N >= B-1){
		answer = (A * (B-1)) / B;
	}
	else{
		answer = (A * N) / B;
	}
	cout<<answer<<endl;
	return 0;
}



Measured memory_bytes: 3596
#include <bits/stdc++.h>
using namespace std;
using ll = long long;using vll = vector<ll>;
int main(){
  ll n;
  cin >> n;
  vll a(n), b(n);
  for (ll i = 0; i < n; i++) cin >> a[i] >> b[i];
  
  ll cnt=0;
  for (ll i = n-1; i >= 0; i--) {
    ll x = (a[i]+cnt) % b[i];
    if (x > 0) x = b[i] - x;
    cnt += x;
  }
  
  cout << cnt << endl;
}

Measured memory_bytes: 1792
def next_lunlun(n)
    if n < 10
        return n + 1
    end

    digit_1  = n % 10
    digit_10 = (n / 10) % 10

    if digit_1 == 9
        m = next_lunlun(n / 10)
        if m % 10 == 0
            return 10 * m
        else
            return 10 * m + m % 10 - 1
        end
    end

    if digit_1 == digit_10 + 1
        m = next_lunlun(n / 10)
        if m % 10 == 0
            return 10 * m
        else
            return 10 * m + m % 10 - 1
        end
    end

    if digit_1 == digit_10
        return n + 1
    end

    if digit_1 == digit_10 - 1
        return n + 1
    end

    raise Exception
end

k = gets.to_i

i = 1
n = 1
while i < k
    n = next_lunlun(n)
    i = i + 1
end
puts n


Measured memory_bytes: 1788
#include <iostream>
#include<math.h>

using namespace std;


int main(){
while(true){
    long ans=0;
    int c=0;
    int n;
    cin>>n;
    if(n==0)break;
    while(1){
        ans+=n%8*pow(10,c);
        n/=8;
        if(n==0)break;
        c++;
    }
    for(int i=0;i<=c;i++){
        int temp=((long)(ans/pow(10,i))%10);
        if(temp==4)ans+=pow(10,i);
        if(temp>=5)ans+=2*pow(10,i);
    }
    cout<<ans<<endl;
}
    return 0;
}

Measured memory_bytes: 1204
object Main extends App {
  val Array(a, b, c, x) = Iterator.continually(io.StdIn.readLine.toInt)
    .take(4).toArray

  val ans = {
    var cnt = 0
      for {
        i <- 0 to a takeWhile (_*500 <= x)
        c0 = 500*i
        j <- 0 to b takeWhile (c0 + _*100 <= x)
        c1 = 100*j
        k <- 0 to c takeWhile(c0 + c1 + _*50 <= x)
        c2 = 50*k
      } if(c0 + c1 + c2 == x) cnt += 1
    cnt
  }

  println(ans)
}

Measured memory_bytes: 30576
program ABC_087_B_Coins
   implicit none
   integer A, B, C, X, count50, count100, count500, i, j, k, co
   read(*, *) A
   read(*, *) B
   read(*, *) C
   read(*, *) X
   count50 = 0
   count100 = 0
   count500 = 0
   co = 0
   
   X = X / 50
   
   do i = 1, A
      if (X < 10 * i) then
	      exit
	  end if
	  count500 = count500 + 1
   end do
   
   do i = 1, B
      if (X < 2 * i) then
	      exit
	  end if
	  count100 = count100 + 1
   end do
   
   do i = 1, C
      if (X < i) then
	      exit
	  end if
	  count50 = count50 + 1
   end do
   
   do k = 0, count500
      do j = 0, count100
	      do i = 0, count50
              if( 10*k + 2*j + 1*i == X ) then
			      co = co + 1
			  end if
		  end do
	  end do
   end do
   write(*, *) co
   
   
end program ABC_087_B_Coins

Measured memory_bytes: 384
N = int(input())
A = list(map(int, input().split()))

ans1 = 0
s = A[0]
if s >= 0:
    ans1 = 1+s
    s = -1
for i in range(1, N):
    s += A[i]
    if i % 2 != 0:
        if s <= 0:
            ans1 += 1-s
            s = 1
    else:
        if s >= 0:
            ans1 -= -1-s
            s = -1

ans2 = 0
s = A[0]
if s <= 0:
    ans2 = 1-s
    s = 1
for i in range(1, N):
    s += A[i]
    if i % 2 == 0:
        if s <= 0:
            ans2 += 1-s
            s = 1
    else:
        if s >= 0:
            ans2 -= -1-s
            s = -1
print(min(ans1, ans2))


Measured memory_bytes: 14468
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#include<cmath>
#include <numeric>
#include<iomanip>
#include <cstdint>

using namespace std;

vector<vector<int>>to;
vector<vector<int>>cost;
vector<int>ans;

void dfs(int v, int p = -1) {
	for (int i = 0; i < to[v].size(); i++) {
		int u = to[v][i];
		int ei = cost[v][i];
		if (u == p) {
			continue;
		}
		ans[u] = (ans[v] + ei) % 2;
		dfs(u, v);
	}
}

int main() {
	int n;
	cin >> n;
	to.resize(n);
	cost.resize(n);
	ans.resize(n);
	for (int i = 0; i < n-1; i++) {
		int u, v, w;
		cin >> u >> v >> w;
		u--; v--;
		to[u].push_back(v);
		to[v].push_back(u);
		cost[u].push_back(w);
		cost[v].push_back(w);
	}
	dfs(0);
	for (int i = 0; i < n; i++) {
		cout << ans[i] << endl;
	}
}

Measured memory_bytes: 13312
s = input()
t = input()


max = 0
for i in range(len(s) -len(t) + 1):
    target = s[i:i+len(t)]
    matched = 0
    for j in range(len(target)):
        if target[j] == t[j]:
            matched += 1
    if matched > max:
        max = matched

print(len(t) - max)


Measured memory_bytes: 72672
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String {
            bytes
                .by_ref()
                .map(|r| r.unwrap() as char)
                .skip_while(|c| c.is_whitespace())
                .take_while(|c| !c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_imports)]
use std::cmp::{min, max};

fn main() {
    input!{
        n: usize,
        c: usize,
        k: usize,
        mut t: [usize; n]
    }
    t.sort();
    let mut ans = 0;
    let mut i = 0;
    while i < n {
        let p = t[i];
        let mut b = 0;
        while b < c && i < n && t[i] <= p + k {
            i += 1;
            b += 1;
        }
        ans += 1;
    }
    println!("{}", ans);
}


Measured memory_bytes: 4476
#include <bits/stdc++.h> 

#define int ll
using namespace std;
using ll=long long;
using ull=unsigned long long;
using pii=pair<int,int>;

#define INF LONG_MAX
#define MOD 1000000007
#define rng(a) a.begin(),a.end()
#define rrng(a) a.end(),a.begin()

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

  int N,M;
  cin>>N>>M;
  vector<int>A(N),B(M),C(M);
  for(int i=0;i<N;i++)cin>>A[i];
  for(int i=0;i<M;i++)cin>>B[i]>>C[i];
  vector<pii>D(M);
  for(int i=0;i<M;i++)D[i]={C[i],B[i]};
  sort(rng(D));
  reverse(rng(D));
  priority_queue<pii>q;
  for(int i=0;i<N;i++)q.push({A[i],1});
  for(int i=0;i<M;i++)q.push(D[i]);
  ll ans=0;
  for(int i=0;i<N;i++){
    pii p=q.top();
    q.pop();
    if(N-i>=p.second)ans+=p.first*p.second,i+=p.second-1;
    else ans+=p.first*(N-i),i=N;
  }
  cout<<ans<<endl;
  return 0;
}


Measured memory_bytes: 10352
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;

int main() {
    int posa = -1, posz = -1;
    string s;
    cin >> s;
    rep(i, s.size()) {
        if(posa == -1 && s.at(i) == 'A') posa = i;
        if(s.at(i) == 'Z') posz = i;
    }
    cout << posz - posa + 1 << endl;
    return 0;
}

Measured memory_bytes: 3688
#include <bits/stdc++.h>
#include <string>
#include <algorithm>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <float.h>
#include <iomanip>
#include <iostream>
using namespace std;
 
int gcd(int x,int y){
    if(y==0)return x;

    return gcd(y,x%y);
}
int lcm(int x,int y){
    return x*y/gcd(x,y);
}

int main(){

    int ans=0,N,maxp=0,p;

    cin>>N;

    for(int i=0;i<N;i++){
        cin>>p;
        if(p>maxp)maxp=p;
        ans+=p;
    }

    ans-=maxp/2;

    cout<<ans<<endl;

    return 0;
}

Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i,n) for( int i = 0; i < n; i++ )
#define rrep(i,n) for( int i = n; i >= 0; i-- )
#define REP(i,s,t) for( int i = s; i <= t; i++ )
#define RREP(i,s,t) for( int i = s; i >= t; i-- )
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000


int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    string S; cin >> S;
    deque<char> L, R;
    rep(i, S.length()/2) {
        L.push_back(S[i]);
    }
    rep(i, (S.length() + 1)/ 2) {
        R.push_back(S[i + S.length()/2]);
    }
    bool isturn = false;
    int Q; cin >> Q;
    
    while(Q--) {
        int T; cin >> T;
        if (T == 1) isturn = !isturn;
        else {
            int F; char c;
            cin >> F >> c;
            if (F == 1 && !isturn) L.push_front(c);
            if (F == 2 && !isturn) R.push_back(c);
            if (F == 1 && isturn) R.push_back(c);
            if (F == 2 && isturn) L.push_front(c);
        }
        // cout << "L";
        // rep(i, L.size()) cout << L[i];
        // cout << endl;
        // cout << "R";
        // rep(i, R.size()) cout << R[i];
        // cout << endl;
    }
    if (!isturn) {
        rep(i, L.size()) cout << L[i];
        rep(i, R.size()) cout << R[i];
    }
    else {
        rep(i, R.size()) cout << R[R.size() - i - 1];
        rep(i, L.size()) cout << L[L.size() - i - 1];
    }
    cout << endl;

    return 0;
}


Measured memory_bytes: 976
# -*- coding: utf-8 -*-

def io_generator():
	return input()

#+++++++++++++++++++
	
def ku(n,a):
	if n % a==0:
		return int(n/a)
	return (n // a)+1
	
def main(io):
	n=int(io())
	l =[ int(io()) for _ in range(5)]
	bn=min(l)
	nd=ku(n,bn)
	return 4+nd

#++++++++++++++++++++

if __name__ == "__main__":
	io= lambda : io_generator()
	print (main(io))

Measured memory_bytes: 3060
#include <math.h>

#include <algorithm>
#include <cassert>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define PI 3.14159265358979323846264338327950L
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))

int main() {
  int N;
  cin >> N;
  vector<pair<pair<string, int>, int> > rs(N);

  rep(i, N) {
    cin >> rs[i].first.first >> rs[i].first.second;
    rs[i].first.second *= -1;
    rs[i].second = i + 1;
  }
  sort(rs.begin(), rs.end());

  for (const auto &r : rs) cout << r.second << endl;
}


Measured memory_bytes: 256
import java.util.*;

class Main {
	  static int mod=1_000_000_007;
  public static void main(String[] args) {
	  Scanner sc = new Scanner(System.in);
      long cnt =0;
      int flg=0;
      long A = sc.nextLong();
      long B = sc.nextLong();
      long C = sc.nextLong();
      long D = sc.nextLong();
      
      long lcm = LCM(C,D);
      
      cnt = -A+B+1;
      long cnt2 = 0;
      long cnt3 = 0;
      long cnt4 = 0;
      
	  cnt2=(long) (A-1)/C;
	  cnt2=(long) B/C-cnt2;
	  cnt3=(long) (A-1)/D;
	  cnt3=(long) B/D-cnt3;
	  cnt4=(long) (A-1)/lcm;
	  cnt4=(long) B/lcm-cnt4;
//      System.out.println(cnt);    	  
//      System.out.println(cnt2);    	  
//      System.out.println(cnt3);    	  
//      System.out.println(cnt4);    	  
      
      System.out.println(cnt-cnt2-cnt3+cnt4);    	  
  }
  
  static long GCD(long a,long b) {
	  long tmp;
	  long r;
	  if(a<b) {
		  tmp = a;
		  a = b;
		  b = tmp;				  
	  }
	  
	  r = a % b;
	  while(r!=0) {
		  a=b;
		  b=r;
		  r=a%b;
	  }
	  
	  return b;
  }
  
  static long LCM(long a,long b) {
	  return a*b/GCD(a,b);
  }
}

Measured memory_bytes: 23252
MAX = 200005
G = [[] for i in range(MAX)]
adds = [0] * MAX
ans = [0] * MAX

# def dfs(c, p, count):
#     count += adds[c]
#     ans[c] += count
#     for i in range(len(G[c])):
#         if (G[c][i] != p):
#             dfs(G[c][i], c, count)

n, q = map(int, input().split())
for i in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)
               
for i in range(q):
    p, x = map(int, input().split())
    p -= 1
    adds[p] += x

# dfs(0, -1, 0)
stack = [(0, -1, adds[0])]
while len(stack) != 0:
    cur, par, count = stack.pop()
    ans[cur] += count
    for i in range(len(G[cur])):
        if G[cur][i] != par:
            stack.append((G[cur][i], cur, count + adds[G[cur][i]]))
            
for i in range(n):
    print(ans[i])


Measured memory_bytes: 119768
import sys
from collections import defaultdict
from collections import deque

n=int(input())
edges=[tuple(map(int,input().split())) for _ in range(n-1)]
d=defaultdict(list)
for e in edges:
  d[e[0]].append(e[1])
  d[e[1]].append(e[0])
fnc=[-1]*(n+1)
snk=[-1]*(n+1)
fnc[1]=0
snk[n]=0
cv=deque([1])
nv=deque([1])
c=-1
while nv:
  c+=1
  cv.extend(nv)
  nv.clear()
  for e in cv:
    fnc[e]=c
    for x in d[e]:
      if fnc[x]<0:
        nv.append(x)
  cv.clear()
cv.append(n)
nv=deque([1])
c=-1
while nv:
  c+=1
  cv.extend(nv)
  nv.clear()
  for e in cv:
    snk[e]=c
    for x in d[e]:
      if snk[x]<0:
        nv.append(x)
  cv.clear()

j=sum([(fnc[i]<=snk[i])-(fnc[i]>snk[i]) for i in range(1,n+1)])
if j>0:
  print('Fennec')
else:
  print('Snuke')

Measured memory_bytes: 43956