akhauriyash/Code-Regression huggingface dataset:
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class Main {
    public static void main(String[] args){
        FastScanner sc = new FastScanner();
        int n = sc.nextInt();
        int k = sc.nextInt();
        int x = sc.nextInt();
        int y = sc.nextInt();
        int total = 0;
        if(n > k){
            total = (k * x) + (n - k) * y;
        }else{
            total = n*x;
        }

        System.out.println(total);

    }

    static class FastScanner {
        private final InputStream in = System.in;
        private final byte[] buffer = new byte[1024];
        private int ptr = 0;
        private int buflen = 0;
        private boolean hasNextByte() {
            if (ptr < buflen) {
                return true;
            }else{
                ptr = 0;
                try {
                    buflen = in.read(buffer);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (buflen <= 0) {
                    return false;
                }
            }
            return true;
        }
        private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
        private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
        public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
        public String next() {
            if (!hasNext()) throw new NoSuchElementException();
            StringBuilder sb = new StringBuilder();
            int b = readByte();
            while(isPrintableChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }
        public long nextLong() {
            if (!hasNext()) throw new NoSuchElementException();
            long n = 0;
            boolean minus = false;
            int b = readByte();
            if (b == '-') {
                minus = true;
                b = readByte();
            }
            if (b < '0' || '9' < b) {
                throw new NumberFormatException();
            }
            while(true){
                if ('0' <= b && b <= '9') {
                    n *= 10;
                    n += b - '0';
                }else if(b == -1 || !isPrintableChar(b)){
                    return minus ? -n : n;
                }else{
                    throw new NumberFormatException();
                }
                b = readByte();
            }
        }
        public int nextInt() {
            long nl = nextLong();
            if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
            return (int) nl;
        }
        public double nextDouble() { return Double.parseDouble(next());}
    }
}


Measured memory_bytes: 21076
#include <iostream> 
#include<vector>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<queue>
#include<deque>
#include<iomanip>
#include<sstream>
#include<cmath>
#include<bitset>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define req(i,n) for(int i = 1;i <=  n; i++)
#define rrep(i,n) for(ll i = n-1;i >= 0;i--)
#define ALL(obj) begin(obj), end(obj)
#define RALL(a) rbegin(a),rend(a)
typedef long long int ll;
typedef long double ld;
const ll INF = 1e18;
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T & val) {
    std::fill((T*)array, (T*)(array + N), val);
}
const int MOD = 1e9+7;
int main(void) {
    ll n,k; cin >> n>>k; vector<ll> a(n);
    rep(i, n) cin >> a[i];
    ll val = 0;
    rrep(i, 41) {
        int set = 0;
        rep(j, n) {
            if ((a[j]>> i)&1)set++;
        }if (n - set > set && val + (1LL << i) <= k)val += (1LL << i);
    }ll ans = 0;
    rep(i, n) {
        ans += (a[i] ^ val);
    }cout << ans << endl;
}

Measured memory_bytes: 4036
#include <iostream>
using namespace std;

int main() {
	int n, d, x, a, sum = 0;

	cin >> n >> d >> x;

	for (int i = 1; i <= n; i++) {
		cin >> a;
		sum += int((d - 1) / a) + 1;
	}
	sum += x;
	cout << sum;
	return 0;
}

Measured memory_bytes: 256
// using no_way as a way
// using no_limitation as a limitation

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
const double PI = acos(-1.0);
const double EPS = (1e-9);
typedef long long ll;
typedef unsigned long long ull;
#define flush fflush(stdout), cout.flush()
#define all(v) ((v).begin()),((v).end())
#define rall(v) ((v).rbegin()),((v).rend())
#define clr(v,line) memset(v,line,sizeof(v))
#define rep(i,n) for(int(i)=0;(i)<(int)(n);(i)++)
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define RT(x) return cout<<x, 0;
//#define lcm(a,b) (a*(b/__gcd(a,b)))
#define P 1000000007
#define endl '\n'
// const int dx[] = {1,-1,0,0,1,-1,1,-1};
// const int dy[] = {0,0,1,-1,1,-1,-1,1};
// const int dxhorse[] = {-2,-2,-1,-1,1,1,2,2};
// const int dyhorse[] = {1,-1,2,-2,2,-2,1,-1};
 
void file() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
void fast()
{
    std::ios_base::sync_with_stdio(0);
    cin.tie(NULL); cout.tie(NULL);
}
std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());


int main(int argc, char const *argv[])
{
    // file();
    fast();
    
    ull x, y;cin>>x>>y;
    if(!x)RT(0);
    if (x < y)
    {
        if( y % x == 0)
            RT(x);
        RT(min(x , y - x));
    }
    if(x % y == 0)
        RT(0);
    RT(y - (x % y));
}

Measured memory_bytes: 256
i[100012],d[100012],s,n,a,x,y;main(w){for(w=scanf("%d",&n);a++<n;d[y]++)scanf("%d%d",&x,&y),i[x]++;for(a=2;a<=n+1;w-=d[a++])w+=i[a],w>=a?s=a-1:0;printf("%d\n",s);exit(0);}

Measured memory_bytes: 1140
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using static System.Console;
using static System.Math;

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            var n = ReadInt();
            var p = int.Parse(Read().Replace(" ",""));
            var q = int.Parse(Read().Replace(" ",""));

            var x = Enumerable.Range(1, n).ToArray();
            var all = AllPermutation(x);

            var list = new List<int>();
            foreach (var item in all)
            {
                list.Add(int.Parse(string.Join("", item)));
            }
            var p_num = list.BinarySearch(p);
            var q_num = list.BinarySearch(q);

            WriteLine(Abs(p_num - q_num));
        }

        static List<T[]> AllPermutation<T>(params T[] array) where T : IComparable
        {
            var a = new List<T>(array).ToArray();
            var res = new List<T[]>();
            res.Add(new List<T>(a).ToArray());
            var n = a.Length;
            var next = true;
            while (next)
            {
                next = false;

                // 1
                int i;
                for (i = n - 2; i >= 0; i--)
                {
                    if (a[i].CompareTo(a[i + 1]) < 0) break;
                }
                // 2
                if (i < 0) break;

                // 3
                var j = n;
                do
                {
                    j--;
                } while (a[i].CompareTo(a[j]) > 0);

                if (a[i].CompareTo(a[j]) < 0)
                {
                    // 4
                    var tmp = a[i];
                    a[i] = a[j];
                    a[j] = tmp;
                    Array.Reverse(a, i + 1, n - i - 1);
                    res.Add(new List<T>(a).ToArray());
                    next = true;
                }
            }
            return res;
        }
        private static string Read() { return ReadLine(); }
        private static string[] Reads() { return (Read().Split()); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}


Measured memory_bytes: 21580
#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

void minDist(const vector<string>& plane, char wall, int y0, int x0, vector<vector<int> >& dist)
{
    int dy[] = {0, 0, -1, 1};
    int dx[] = {-1, 1, 0, 0};

    int h = plane.size();
    int w = plane[0].size();
    dist.assign(h, vector<int>(w, -1));

    queue<pair<int, int> > q;
    q.push(make_pair(y0, x0));
    dist[y0][x0] = 0;
    int n = 1;
    while(!q.empty()){
        queue<pair<int, int> > q1;
        while(!q.empty()){
            int y = q.front().first;
            int x = q.front().second;
            q.pop();
            for(int i=0; i<4; ++i){
                int y1 = y + dy[i];
                int x1 = x + dx[i];
                if(0 <= y1 && y1 < h && 0 <= x1 && x1 < w && plane[y1][x1] != wall && dist[y1][x1] == -1){
                    q1.push(make_pair(y1, x1));
                    dist[y1][x1] = n;
                }
            }
        }
        ++ n;
        q = q1;
    }
}

int main()
{
    int h, w, k;
    cin >> h >> w >> k;
    vector<string> s(h, string(w, ' '));
    int sy, sx;
    for(int y=0; y<h; ++y){
        for(int x=0; x<w; ++x){
            cin >> s[y][x];
            if(s[y][x] == 'S'){
                sy = y;
                sx = x;
            }
        }
    }

    vector<vector<int> > dist;
    minDist(s, '#', sy, sx, dist);
    int ans = INT_MAX;
    for(int y=0; y<h; ++y){
        for(int x=0; x<w; ++x){
            if(dist[y][x] == -1 || dist[y][x] > k)
                continue;
            int d = min({ y, h-1-y, x, w-1-x });
            ans = min(ans, 1 + (d + k - 1) / k);
        }
    }
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 3456
N, M = map(int,input().split())
G = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int,input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

ans = 0
def calc(G, E, s):
    global ans
    E.append(s)
    for v in G[s]:
        if v in E:
            continue
        if len(E) == N - 1 and v not in E:
            ans += 1
            return
        calc(G, E, v)
        E.remove(v)

calc(G, [], 0)
print(ans)


Measured memory_bytes: 3064
n=int(input())
x=[i for i in map(int, input().split())]
ans=1000000000000000000000000000000000000000
x.sort()

for i in range(x[n-1]+1):
  cnt=0
  for k in x:
    cnt= cnt+(k-i)**2
  ans=min(ans,cnt)
print(ans)

Measured memory_bytes: 3188
#include <bits/stdc++.h>
using namespace std;
long long X, Y;


int main() {
    cin >> X >> Y;

    //Xを考慮して1スタート
    int count = 1;
    long long current = X;

    while(current * 2 <= Y) {
        current *= 2;
        count++;
    }

    cout << count << endl;

    return 0;
}

Measured memory_bytes: 3592
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        
        int a = sc.nextInt();
        int b = sc.nextInt();
        
        
        int ap = a + b;
        int am = a - b;
        int ak = a * b;
        
       
        int maxxx = Math.max(ap, am);
        
        int mayyy = Math.max(maxxx, ak);
        
        System.out.println(mayyy);
    }
}

Measured memory_bytes: 35568
// Practice AtCoder
// author: Leonardone @ NEETSDKASU

fn main() {
    let mut stdin = String::new();
    std::io::Read::read_to_string(&mut std::io::stdin(), &mut stdin).unwrap();
    let mut stdin = stdin.split_whitespace();
    let mut get = || stdin.next().unwrap();
    
    let s = get().as_bytes();
    let mut ans = 0;
    for i in 0..s.len() / 2 {
        if s[i] != s[s.len()-1-i] {
            ans += 1;
        }
    }
    println!("{}", ans);
}

Measured memory_bytes: 4352
def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    X, K, D = read_ints()
    if X < 0:
        X = -X
    closest_K = X // D
    if K <= closest_K:
        return abs(X-K*D)
    if (K-closest_K)%2 == 0:
        return abs(X-closest_K*D)
    return abs(X-(closest_K+1)*D)


if __name__ == '__main__':
    print(solve())


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

int main(){
  int a, b;
  string c;
  cin >> a >> b;
  
  if((a * b) % 2 == 0){
    c = "Even"; 
  }else{
    c = "Odd"; 
  }
  
  cout << c << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const ll INF=mod*mod;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(x) (x).begin(),(x).end()
ll min(ll a,ll b){
  if(a>b){
    return b;
    }
      else{
        return a;
      }
  }
  
  ll max(ll a,ll b){
    if(a>b){
      return a;
    }
    else{
      return b;
    }
  }

ll power(ll a,ll b){
 ll ans=1;
 for(ll i=1;i<=b;i++){
ans*=a;
  }
  return ans;
}

 ll gcd(ll m,ll n){
 	if(n==0){
       return m;
     }
   return gcd(n,m%n);
 }
ll lcm(ll a,ll b){
  ll g=gcd(a,b);
  return a/gcd(a,b)*b;
		

}
int main(){
  
  ll N,K;
  cin>>N>>K;
  vector<ll>A(N);
  vector<ll>C(N);
  
  rep(i,N){
    cin>>A[i];
    C[i]=A[i];
    }
  ll ans=0;
  
  ll Y=(K*(K-1)/2)%mod;
  rep(i,N){//自分がいる列の中
    vector<ll>a;//a[i]の後のうちa[i]以下のものの数
  
    for(ll j=i;j<N;j++){
     a.push_back(A[j]);
    }
    sort(all(a));
      
    ans+=(((lower_bound(a.begin(),a.end(),A[i])-a.begin())%mod)*K)%mod;
  ans%=mod;
  }
  ll ans2=0;
 sort(all(C));
  rep(i,N){//自分がいる列の後
 
 ans2+=((((lower_bound(all(C),C[i])-C.begin()))%mod)*Y)%mod;
    ans2%=mod;
  }

  


  cout<<(ans+ans2)%mod<<endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
	int a,b,c,i,j,n,s=0;
    cin>>a>>b>>c>>n;
    for(i=0;i<=a;i++){
    for(j=0;j<=b;j++){
    if(n-i*500-j*100>=0 && n-i*500-j*100<=c*50){
	s++;
    }}}
	cout<<s<<endl;
    return 0;
}

Measured memory_bytes: 256
import java.io.*;
import java.util.*;

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

      t=Double.parseDouble(sc.next());
      x=Double.parseDouble(sc.next());
      
      System.out.println(t/x);
                
      }catch (Exception e) {
      System.out.println("out");
    }
  }
}


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

int main(){

  string S; cin >> S;
    
  if(S.at(5)=='1' || S.at(6)=='5' || S.at(6)=='6' || S.at(6)=='7' || S.at(6)=='8' || S.at(6)=='9' )
    cout << "TBD" << endl;
  else
    cout << "Heisei" << endl;

}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<long long> vll;
typedef vector<pair<int,int> > vpi;
typedef vector<pair<long long, long long> > vpl;
typedef pair<int,int> pii;
typedef pair<long long, long long> pll;
#define INF (int)(1e9)
#define MAXX 1.1529215e+18
#define inf 999999
#define EPS (1e-7)
#define rep(i,n) for(int i=0; i<(int)(n);i++)
#define REP(i,n) for(int i=1;i<=(int)(n);i++)
#define FOR(i,k,n) for(int i=(k);i<(int)(n);i++)
#define ALL(a)  a.begin(),a.end()
#define RALL(a) a.begin(),a.end(),greater<int>()
#define ROT(a) a.begin(),a.begin()+1,a.end()
#define RROT(a) a.begin(),a.end()-1,a.end()
#define PB push_back
#define MP make_pair
#define PI acos(-1.0)
#define sz(a) a.size()
const ll MOD = 1e9+7;
const int MAX = 10000000;
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }
//cout << 'H' << endl;
/*--------------------------------------------*/

vector<vector<int>> G;
int N,K;
ll ans;
vector<int> seen;
vector<int> color;

void dfs(int p, int v){
	if(seen[v]) return;
	seen[v] = 1;

	if(p == -1){
		ans *= K;
		ans %= MOD;
//		color[v] = K;
	}else{
		color[v]--;
		ans *= color[p]--;
		ans %= MOD;
	}

//	cout << "v = " << v << "color[v]= " << color[v] << "ans= " << ans << endl;
	
	for(int i : G[v]){
		if(!seen[i])dfs(v,i);
	}

	//return ans;	
}


int main(){
    //cout << fixed << setprecision(10)
    cin.tie(0);
    ios::sync_with_stdio(false);
		
	cin >> N >> K;

	G.resize(N);
	seen.assign(N,0);
	color.assign(N,K-1);

	rep(i,N-1){
		int a,b;
		cin >> a >> b;
		--a;--b;
		G[a].PB(b);
		G[b].PB(a);
	}

	ans = 1;

	dfs(-1,0);

	cout << ans << endl;
	
}


Measured memory_bytes: 8832
if isfile("mystdin.txt");mystdin=open("mystdin.txt","r");redirect_stdin(mystdin);end
pI(x)=parse(Int,x)
pM(x::Array{SubString{String},1})=map(pI,x)
function main()
  n,m=pM(split(readline()))
  l=[0 for i=1:n]
  for i=1:m
    a,b=pM(split(readline()))
    l[a]+=1
    l[b]+=1
  end
  for i=1:n
    println(l[i])
  end
end
main()
if isdefined(Base, :mystdin);close(mystdin);end

Measured memory_bytes: 172376
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace A
{
    public class Program
    {
        static void Main(string[] args)
        {
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
            Solve();
            Console.Out.Flush();
        }

        public static void Solve()
        {
            var xyz = Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray();
            Console.WriteLine($"{xyz[2]} {xyz[0]} {xyz[1]}");
        }
    }
}


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

int main() {
  int W, H, N;
  cin >> W >> H >> N;
  int bigx, smallx, bigy, smally;
  bigx = W, smallx = 0, bigy = H, smally = 0;
  int x, y, a;
  
  for (int i = 0; i < N; i++) {
    cin >> x >> y >> a;
    if (a == 1) {
      smallx = max(smallx, x);
    }
    else if (a == 2) {
      bigx = min(bigx, x);
    }
    else if (a == 3) {
      smally = max(smally, y);
    }
    else {
      bigy = min(bigy, y);
    }
  }
  cout << max(0, bigx - smallx) * max(0, bigy - smally) << endl;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1000005;
int n,q,k;
char s[N];
int tot[N];
void solve(){
	cin >> k;
	LL v1=0,v2=0;
	LL ans=0;
	for (int i=1;i<=n;i++){
		if (i-k>0&&s[i-k]=='D')
			v2-=tot[i-1]-tot[i-k],v1--;
		if (s[i]=='D')
			v1++;
		if (s[i]=='M')
			v2+=v1;
		if (s[i]=='C')
			ans+=v2;
	}
	cout << ans << endl;
}
int main(){
	cin >> n;
	scanf("%s",s+1);
	for (int i=1;i<=n;i++)
		tot[i]=tot[i-1]+(s[i]=='M');
	cin >> q;
	while (q--)
		solve();
	return 0;
}

Measured memory_bytes: 5120
#include<bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
typedef long long ll;
#define pb push_back

void solve(){
    string s; cin >> s;
    bool flag = true;
    REP(i, s.size()){
        if(i%2 == 0){
            //cout << s[i] << endl;
            if(s[i] =='R' || s[i] =='U' || s[i] =='D') continue;
            else flag = false;
        }
        else{
            if(s[i] =='L' || s[i] =='U' || s[i] =='D') continue;
            else flag = false;
        }
    }
    if(flag) cout << "Yes" << endl;
    else cout << "No" << endl;
}

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

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <cmath>
#include <limits>
#include <vector>
#include <cstdio>
#include <bits/stdc++.h>
#include <set>
#include <map>
#include <stdio.h>
#include <stack>
#include <queue>
using namespace std;
using ll = long long;
map <int ,int> mpa,mpb;
typedef pair<ll, ll> P;
priority_queue<P, vector<P>, greater<P>> pque;

int main(){
  int N;
  cin >> N;
  if(N%2==0){
    cout << N << endl;
    return 0;
  }
  else{
    cout << 2*N << endl;
  }
  return 0;
}


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

using namespace std;

#define debug(x,y) cout<<(#x)<<" " <<(#y)<<" is " << (x) <<" "<< (y) << endl
#define watch(x) cout<<(#x)<<" is " << (x) << endl
#define fast ios_base::sync_with_stdio(false)
#define fie(i,a,b) for(i=a;i<b;i++)
#define MOD 1000000007
#define mod 998244353
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define ll long long
#define lld long long int
#define ALL(x) (x).begin(),(x).end()

typedef vector<lld> vi;
typedef vector<vector<lld>> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<pair<lld, lld>> vpi;
typedef long long LL;


int main() {
    fast;
    cin.tie(0);
/*#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w" , stdout);
#endif*/
    lld k;
    cin >> k;
    lld a, b;
    cin >> a >> b;
    if (k * (b / k) >= a) cout << "OK" << endl;
    else cout << "NG" << endl;

}

Measured memory_bytes: 3656
#include<stdio.h>
#define min(x, y) ((x) < (y) ? (x) : (y))
#define INF (1<<28)
int n, m, a, b, cost, time, costs[105][106], times[104][103], p, q, r, i, j, k;

int main(){
  while(1){
    scanf("%d%d", &n, &m);
    if(n == 0 && m == 0)return 0;
    for(i = 1;i <= m;i++){
      for(j = 1;j <= m;j++){
	times[i][j] = INF;
	costs[i][j] = INF;
      }
    }
    for(i = 0;i < n;i++){
      scanf("%d%d%d%d", &a, &b, &cost, &time);
      costs[a][b] = costs[b][a] = cost;
      times[a][b] = times[b][a] = time;
    }
    for(k = 1;k <= m;k++){
      for(i = 1;i <= m;i++){
	for(j = 1;j <= m;j++){
	  costs[i][j] = costs[j][i] = min(costs[i][j], costs[i][k] + costs[k][j]);
	  times[i][j] = times[j][i] = min(times[i][j], times[i][k] + times[k][j]);
	}
      }
    }
    
    scanf("%d", &k);
    for(i = 0;i < k;i++){
      scanf("%d%d%d", &p, &q, &r);
      if(r == 0){
	printf("%d\n", costs[p][q]);
      }
      if(r == 1){
	printf("%d\n", times[p][q]);
      }
    }
  }
}

Measured memory_bytes: 684
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
const int MX = 100005;
using ll = long long;
int n, m, a[MX];
ll ans, k, s1[MX], s2[MX];
void add(int l ,int r){
	s1[l]++; s1[r]--;
	s2[l] -= l; s2[r] += l;
	if(r < l){
		s1[0]++;
		s2[0] -= l-m;
		s2[r] -= m;
	}
}
int main(){
	scanf("%d%d", &n, &m);
	rep(i,n) scanf("%d", a+i);
	rep(i,n-1){
		ans += (a[i+1] - a[i] + m) % m;
		add(a[i]%m, a[i+1]%m);
	}
	rep(i,m){
		if(i){
			s1[i] += s1[i-1];
			s2[i] += s2[i-1];
		}
		k = max(k, s1[i] * i + s2[i]);
	}
	printf("%lld\n", ans-k);
}

Measured memory_bytes: 2176
N = int(input())
A = []
B = []
v = []
res = 0

for i in range(N):
    a,b = map(int,input().split())
    A.append(a)
    B.append(b)
    v.append((i,a+b))

a=sorted(v,key = lambda x:x[1],reverse = True)

for i in range(N):
    tmp=a.pop(0)[0]
    if i%2==0:
        res += A[tmp]
    else:
        res -= B[tmp]

print(res)


Measured memory_bytes: 27304
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
typedef long long ll;
typedef double dd;
#define For(i,j,k) for (int i=j;i<=k;++i)
#define Forr(i,j,k) for (int i=j;i>=k;--i)
#define Set(a,p) memset(a,p,sizeof(a))
using namespace std;

template<typename T>bool chkmax(T& a,T b) {return a<b?a=b,1:0;}
template<typename T>bool chkmin(T& a,T b) {return a>b?a=b,1:0;}

const int maxn=10000+100;
int n;
int a[maxn],b[maxn];

inline void read(int &x) {
	x=0;
	int p=1;
	char c=getchar();
	while (!isdigit(c)) {if (c=='-') p=-1; c=getchar();}
	while (isdigit(c)) {x=(x<<1)+(x<<3)+(c-'0'); c=getchar();}
	x*=p;
}

inline void file() {
	freopen("B.in","r",stdin);
	freopen("B.out","w",stdout);
}

int main() {
//	file();
	read(n);
	For (i,1,n) read(a[i]);
	For (i,1,n) read(b[i]);
	ll sum1=0,sum2=0;
	For (i,1,n) {
		if (a[i]>=b[i]) sum1+=(a[i]-b[i]);
		else {
			if ((b[i]-a[i])%2==0) sum2+=(b[i]-a[i])/2;
			else {
				sum2+=(b[i]-a[i])/2+1;
				sum1++;
			}
		}
	}
	if (sum1<=sum2) printf("Yes");
	else printf("No");
/*	int ok=1;
	For (i,1,n) if (a[i]!=b[i]) ok=0;
	if (ok) {
		printf("Yes");
		return 0;
	}
	ll sum1=0,sum2=0,sum=0;
	For (i,1,n) {
		if (a[i]>=b[i]) sum1+=(ll)(a[i]-b[i]);
		if (a[i]<b[i]) sum2+=(ll)(b[i]-a[i]);
		if (abs(a[i]-b[i])%2==1) sum++;
	}
	if (sum1>sum2) {
		printf("No");
		return 0;
	}
	ll step=sum2-sum1;
	if ((step-sum)%2==1) {
		printf("No");
		return 0;
	}
	if (sum1>step) {
		printf("No\n");
		return 0;
	}
	if (sum2<2*sum1) {
		printf("No\n");
		return 0;
	}
	printf("Yes");*/
	return 0;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
int main ()
{
  int N,A;
  cin >> N;
  for (int i=0; i<N ; i++)
  {
    int s,t;
    cin >> s >> t;
    A = A+t-s+1;
  }
  cout << A << endl;
}
    

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int a,b;

    cin >> a >> b;

    if( a*b % 2 == 0 )
    {
        cout << "Even";
    }
    else
    {
        cout << "Odd";
    }

    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
struct _I { _I() {cin.tie(0);ios::sync_with_stdio(false);}} __I;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i < (n); i++)
#define all(a) begin(a),end(a)
template<class F, class S> istream& operator >> (istream& s, pair<F, S>& p)
{ cin >> p.first >> p.second; return s;}
template<class T> istream& operator >> (istream& s, vector<T>& v)
{ for (T& x: v) { s >> x; } return s;}
template<class T> void cout_h(const vector<T>& c, int n=INT_MAX)
{ rep(i, min(n, (int)c.size())) cout << c[i] << (i==c.size()-1?"\n":" "); }
template<class T> inline bool chmin(T& a, T b) {return a>b?a=b,true:false;}
template<class T> inline bool chmax(T& a, T b) {return a<b?a=b,true:false;}
#define D1(a) cout<<#a<<":"<<a<<"\n"
#define D2(a,b) cout<<#a<<":"<<a<<","<<#b<<":"<<b<<endl 
#define D3(a,b,c) cout<<#a<<":"<<a<<","<<#b<<":"<<b<<","<<#c<<":"<<c <<endl
#define D4(a,b,c,d) cout<<#a<<":"<<a<<","<<#b<<":"<<b<<","<<#c<<":"<<c<<","<<#d<<":"<<d<<endl 
#define MOD 1000000007

template<class T> class point2d
{
public:
    T x, y;   
    point2d(T x=0, T y=0) : x(x), y(y) {}
    point2d(const point2d<T>& p) { *this=p; }
    point2d& operator=(const point2d<T>& p) { x = p.x; y = p.y; return *this; }
};
template<class T> istream& operator >> (istream& s, point2d<T>& p)
{ s >> p.x >> p.y; return s;}
template<class T> T distance(const point2d<T>& p1, const point2d<T>& p2)
{
    T dx = p1.x - p2.x;
    T dy = p1.y - p2.y;
    return sqrt(dx*dx+dy*dy);
}

typedef point2d<double> point;

int main()
{
    cout<<std::fixed<<std::setprecision(10);

    int N; cin >>N;
    vector<point> p(N); cin >> p;

    point o(0.0, 0.0);
    double move = 0.5;
    double d_max;
    while (move > 1.0e-7) {
        for (int t = 0; t < 100; t++) {
            d_max = 0.0;
            int j = -1;
            for (int i = 0; i < N; i++) {
                if (chmax(d_max, distance(p[i], o))) {
                    j = i;
                }
            }
            o.x += (p[j].x - o.x) * move;
            o.y += (p[j].y - o.y) * move;
        }
        move /= 2.0;
    }

    cout << d_max << endl;
}


Measured memory_bytes: 256
N = int(input())
P = list(map(int, input().split()))
count = 0
for i, p in enumerate(P):
    if p == i+1 and i == N-1:
        count += 1
    elif p == i+1:
        count += 1
        P[i] = P[i+1]
        P[i+1] = p

print(count)

Measured memory_bytes: 52908
mod=998244353
K,N=map(int,input().split())

Factorial=[1]*(N+K+1)
for i in range(1,N+K+1):
  Factorial[i]=Factorial[i-1]*(i)%mod
def power(x,y):
  if y==0:
    return 1
  elif y==1:
    return x%mod
  elif y%2==0:
    return power(x,y//2)**2%mod
  else:
    return (power(x,y//2)**2)*x%mod

inverseFactorial=[1]*(N+K+1)
inverseFactorial[N+K]=power(Factorial[N+K],mod-2)
for i in range(0,N+K)[::-1]:
  inverseFactorial[i]=(inverseFactorial[i+1]*(i+1))%mod
  
def C(x,y):
  if x<y or y<0:
    return 0
  else:
    return (Factorial[x]*inverseFactorial[y]*inverseFactorial[x-y])%mod
def H(x,y):
  return C(x+y-1,y)

def ans(M,N,K):
  res=0
  for m in range(1,M+1)[::-1]:
    if m%2:
      res+=C(M,m)*H(K,N-2*m)
    else:
      res+=(-1)*C(M,m)*H(K,N-2*m)
  return (H(K,N)-res)%mod
  

for i in range(2,2*K+1):
  if i%2:
    print(ans(min(K,i-1)-i//2,N,K))
  else:
    print((ans(min(K,i-1)-i//2,N-1,K-1)+ans(min(K,i-1)-i//2,N,K-1))%mod)
    


Measured memory_bytes: 62300
from collections import Counter

n = int(input())
a = list(map(int, input().split()))

a.sort()
c = Counter(a)

ans = 0
for x in a[::-1]:
    if c[x]>0:
        c[x]-=1
        y = 2**x.bit_length()-x
        if c[y]>0:
            c[y]-=1
            ans += 1
print(ans)

Measured memory_bytes: 113492
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ULL;
const ULL N = 105;
const double eps = 1e-6;
ULL n, a, b, num = 1, tot;
ULL sum = 0, v[N];
bool cmp(ULL x, ULL y)
{
	return x > y;
}
ULL comb(int n, int k)
{
	if (n < k || k <= 0) return 0;
	ULL p[64] = { 1 };
	for (int i = 1; i <= n; ++i)
	{
		ULL t = 1;
		for (int j = 1; j <= i; ++j)
		{
			ULL s = p[j];
			p[j] += t;
			t = s;
		}
	}
	return p[k];
}
int main(int argc, char const *argv[])
{
	cin >> n >> a >> b;
	for (int i = 1; i <= n; ++i) cin >> v[i];
	sort(v+1, v+1+n, cmp);
	for (int k = a; k <= b; ++k)
	{
		ULL sum1 = 0, cnt = 0, cnt1 = 0;
		for (int i = 1; i <= k; ++i) sum1 += v[i];
		for (int i = 1; i <= k; ++i) if (v[i] == v[k]) cnt++;
		for (int i = 1; i <= n; ++i) if (v[i] == v[k]) cnt1++;
		ULL L = comb(cnt1, cnt);
		if (sum1*num > sum*k)
		{
			sum = sum1;num = k;
			tot = L;
		}
		else if (sum1*num == sum*k) tot += L;
	}
	printf("%.9lf\n%llu\n", (double)sum/num, tot);
	return 0;
}

Measured memory_bytes: 256
#pragma GCC optimize ("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
typedef pair<int,int> P; typedef pair<Int,Int> Pl;
static const int mod = 1e9+7;
#define END {cout<<ans<<'\n'; return 0;}
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
#define Pr(t) priority_queue<t>
#define gPr(t) priority_queue<t,vector<t>,greater<t>>
#define V(t) vector<t>
#define rep(i,n) for(int(i)=0; i<n; i++)
#define rer(i,l,r) for(int i=l; i<=r; i++)
#define gnr(i,l,r) for(int i=r; i>=l; i--)
#define eb emplace_back
#define pri1(a) cout<<(a)<<'\n'
#define pri2(a,n) {rep(pri2i,n-1)cout<<a[pri2i]<<' '; cout<<a[n-1]<<'\n';}
#define prip(p) cout<<p.fi<<' '<<p.se<<'\n'
template<class T> inline bool cmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool cmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int n,m,_,x,y,q,key;
string s,sb;
bool ok;

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

  int muni[3]; cin>>n; rep(i,3)cin>>muni[i]; int l[n]; rep(i,n)cin>>l[i];
	int ans=mod;
	rep(i,(1<<(2*n))){
		V(int)use[3]={};
		rep(j,n){
			if(((i>>j)&1)&&((i>>(j+n))&1))use[2].eb(l[j]);
			else if(((i>>j)&1)&&!((i>>(j+n))&1))use[1].eb(l[j]);
			else if(!((i>>j)&1)&&((i>>(j+n))&1))use[0].eb(l[j]);
		}
		ok=1;
		rep(j,3)if(use[j].empty()){ok=0; break;}
		if(!ok)continue;
		int cost=0;
		rep(j,3){
			int sum=0;
			for(int e:use[j])sum+=e;
			cost+=(use[j].size()-1)*10+abs(sum-muni[j]);
		}
		cmin(ans,cost);
	}
	END
}

Measured memory_bytes: 256
#include<stdio.h>
int main ()
{   
  int m,n,sum;
  scanf("%d %d %d",&m,&n,&sum);
  int a;
  if(m<n)  {a=m;m=n;n=a;}
  a=sum/m;
  if(sum%m) a++;
  printf("%d",a); 
  return 0;
}

Measured memory_bytes: 128
pub fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

pub fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

pub fn read_vec2<T: std::str::FromStr>(n: usize) -> Vec<Vec<T>> {
    (0..n).map(|_| read_vec()).collect()
}

pub fn read_col<T: std::str::FromStr>(n: usize) -> Vec<T> {
    (0..n).map(|_| read()).collect()
}

use std::cmp::min;

fn main() {
    let nw: Vec<usize> = read_vec();
    let n = nw[0];
    let vw: Vec<Vec<u32>> = read_vec2(n);
    let maxw = nw[1] as u32;
    let mut dp: Vec<Vec<u32>> = vec![vec![1000000001; 10001]; n + 1];

    dp[0][0] = 0;
    for i in 1..n + 1 {
        let v = vw[i - 1][0] as usize;
        let w = vw[i - 1][1];
        for j in 0..v {
            dp[i][j] = dp[i - 1][j];
        }
        for j in v..10001 {
            dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - v] + w);
        }
    }

    let mut ans = 0;
    for j in 0..10001 {
        if dp[n][j] <= maxw {
            ans = j;
        }
    }
    println!("{}", ans);
}



Measured memory_bytes: 9028
N, M = map(int, input().split())
A = [int(input()) for _ in range(M)]
mod = 10**9+7
DP = [0] * (N + 2)
DP[0] = 1
STEP = set(range(N + 1)) - set(A)

for s in STEP:
  if s + 1 in STEP:
    DP[s + 1] += DP[s]
  if s + 2 in STEP:
    DP[s + 2] += DP[s]

print(DP[N]%mod)

Measured memory_bytes: 469988
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,a,b) for(int i=a; i<=b; i++)
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define INF 1000000000000
#define MOD 10000007
typedef long long ll;
//
int main(){
   int n;cin>>n;
   int a=0,b=0,c=0,d=0;
   vector<string> v;
   rep(i,n){
      string s;
      cin >>s;
      if(s=="AC") a++;
      if(s=="WA") b++;
      if(s=="TLE")c++;
      if(s=="RE") d++;
   }
   cout<<"AC x "<<a<<endl;
   cout<<"WA x "<<b<<endl;
   cout<<"TLE x "<<c<<endl;
   cout<<"RE x "<<d<<endl;
}


Measured memory_bytes: 3624
//For higher stack size use g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456 a.cpp -o a.exe in cmd
    #include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cstring>
    #include <chrono>
    #include <complex>
    #define ll long long
    #define ld long double
    #define vi vector<int>
    #define vll vector<ll>
    #define vvi vector < vi >
    #define pii pair<int,int>
    #define pll pair<long long, long long>
    #define mod 1000000007
    #define inf 1000000000000000001;
    #define all(c) c.begin(),c.end()
    #define mp(x,y) make_pair(x,y)
    #define mem(a,val) memset(a,val,sizeof(a))
    #define eb emplace_back
    #define pb push_back
    #define f first
    #define s second
    
    using namespace std;

    vector<int> v[200001];

    int main()
    {
        std::ios::sync_with_stdio(false);
        int T;
        T=1;
        mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
        // cin.ignore(); must be there when using getline(cin, s)
        while(T--)
        {
            int n,i,j;
            cin>>n;
            int ar[n+1],ar2[n+1];
            map<int,int> mp,mp2,mp3;
            for(i=1;i<=n;++i)
            {
                cin>>ar[i];
                ++mp[ar[i]];
            }
            for(i=1;i<=n;++i)
            {
                cin>>ar2[i];
                ++mp2[ar2[i]];
                ++mp3[ar2[i]];
            }
            for(auto it:mp)
            {
                if(it.s+mp2[it.f] > n)
                {
                    cout<<"No"<<endl;
                    return 0;
                }
            }
            

                sort(ar2+1,ar2+n+1,greater<int>());

            int ans[n+1];
            
            for(i=1;i<=n;++i)
                ans[i]=ar2[i];
            for(i=1;i<=n;++i)
            {
                if(ans[i]!=ar[i])
                    continue;
                for(j=1;j<=n;++j)
                {
                    if(i==j)
                        continue;
                    if(ar[j]!=ans[i] && ans[j]!=ar[i])
                    {
                        swap(ans[i],ans[j]);
                        break;
                    }
                }
            }
            cout<<"Yes"<<endl;
            for(i=1;i<=n;++i)
                cout<<ans[i]<<" ";
            cout<<endl;
        }
        return 0;
    }


Measured memory_bytes: 38812
#include <iostream>
using namespace std;
int main(void){
    string n;
    cin >> n;
    while(n != "0"){
        int sum = 0;
        for(int i = 0; i < n.size(); i++){
            sum += n[i] - '0';
        }
        cout << sum << endl;
        cin >> n;
    }
}



Measured memory_bytes: 3216
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
//typedef tree <int,null_type,less <int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#define rep(g, i, n) for (ll g = i; g < n; g++)
#define rev(g, n, i) for (ll g = n - 1; g >= i; g--)
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(v) v.begin(), v.end()
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define um unordered_multiset
#define us unordered_set
#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define ve vector
#define cc continue
#define bb break
#define rem 1000000007
#define debug(n1) cout<<"debug... "<<n1<<"\n"
#define PI 3.141592653589793238462643383279502
//fflush(stdin);
// do {
// // process permutation
// } while (next_permutation(permutation.begin(),permutation.end()));
class abc
{
    public:
    ll x,y;
};
bool find(ve <ll> v1,ll x)
{
    if (v1[0]==x||v1[1]==x)
    {
        return true;
    }
    return false;
}
int main()
{
    FAST
    // freopen("input1.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // ll tests;
    // cin>>tests;
    // rep (gg,0,tests)
    // {

    // }
    ve <ll> a(2);
    cin>>a[0]>>a[1];
    rep (i,0,3)
    {
        if (!find(a,i+1))
        cout<<i+1<<"\n";
    }
}


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

int main() {
  int N;
  cin >> N;
  bool x = true;
  for(int i=0;i<50000;i++){
    if(floor(i*1.08) == N){
      cout << i << endl;
      x = false;
      break;
    }
  }
    if(x)
      cout << ":(" << endl;
      
}


Measured memory_bytes: 3620
#include <bits/stdc++.h>
#define REP(i,n) for (long i=0;i<(n);i++)
#define FOR(i,a,b) for (long i=(a);i<(b);i++)
#define RREP(i,n) for(long i=n;i>=0;i--)
#define RFOR(i,a,b) for(long i=(a);i>(b);i--)
#define dump1d_arr(array) REP(i,array.size()) cerr << #array << "[" << (i) << "] ==> " << (array[i]) << endl;
#define dump2d_arr(array) REP(i,array.size()) REP(j,array[i].size()) cerr << #array << "[" << (i) << "]" << "[" << (j) << "] ==> " << (array[i][j]) << endl;
#define dump(x)  cerr << #x << " => " << (x) << endl;
#define MOD 1000000007
using namespace std;

int main(void){
	long N,M;
	cin >> N >> M;

	if (abs(N-M) > 1) {
		cout << 0 << endl;
		exit(0);
	}

	long long ans = 1;
	REP(i,N) {
		ans *= (N - i);
		ans %= MOD;
	}
	REP(i,M){
		ans *= (M - i);
		ans %= MOD;
	}

	if (!((N + M) % 2)) ans *= 2;

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

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

int main(){
    int x, y, a, b, c;
    cin >> a >> b >> c >> x >> y;
    ll sum = 0;
    if(a+b >= c*2){
        sum += min(x,y)*(c*2);
        if(max(x,y) == x){
            sum += min(a, c*2)*(abs(x-y));
        }
        else{
            sum += min(b, c*2)*(abs(x-y));
        }
    }
    else{
        sum += min(x,y)*(a+b);
        if(max(x,y) == x){
            sum += a*(abs(x-y));
        }
        else{
            sum += b*(abs(x-y));
        }
    }
    cout << sum << endl;
}

Measured memory_bytes: 256
package main

import (
	"fmt"
)

func trace(A []int, n int) {
	for i := 0; i < n; i++ {
		if i > 0 {
			fmt.Printf(" ")
		}
		fmt.Printf("%d", A[i])
	}
	fmt.Printf("\n")
}

func main() {
	var N int
	fmt.Scan(&N)

	A := make([]int, N)
	for i := 0; i < N; i++ {
		fmt.Scan(&A[i])
	}

	count := 0
	for i := 0; i < N; i++ {
		min_i := i
		for j := i; j < N; j++ {
			if A[j] < A[min_i] {
				min_i = j
			}
		}
		if i != min_i {
			A[i], A[min_i] = A[min_i], A[i]
			count++
		}
	}
	trace(A, N)
	fmt.Println(count)
}



Measured memory_bytes: 1148
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
// #include<ext/pb_ds/tag_and_trait.hpp>
// using namespace __gnu_pbds;
using namespace std;
using ll=long long;
#define double long double
using datas=pair<ll,ll>;
using ddatas=pair<double,double>;
using tdata=pair<ll,datas>;
using vec=vector<ll>;
using mat=vector<vec>;
using pvec=vector<datas>;
using pmat=vector<pvec>;
// using llset=tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>;
#define For(i,a,b) for(i=a;i<(ll)b;i++)
#define bFor(i,b,a) for(i=b-1;i>=(ll)a;i--)
#define rep(i,N) For(i,0,N)
#define rep1(i,N) For(i,1,N)
#define brep(i,N) bFor(i,N,0)
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define vsort(v) sort(all(v))
#define vrsort(v) sort(allr(v))
#define endl "\n"
#define pb push_back
#define eb emplace_back
#define print(v) cout<<v<<endl
#define printyes cout<<"Yes"<<endl
#define printno cout<<"No"<<endl
#define printYES cout<<"YES"<<endl
#define printNO cout<<"NO"<<endl
#define output(v) do{bool f=0;for(auto outi:v){cout<<(f?" ":"")<<outi;f=1;}cout<<endl;}while(0)
const ll mod=1000000007;
// const ll mod=998244353;
const ll inf=1LL<<60;
const double PI = acos(-1);
const double eps = 1e-9;
template<class T> inline bool chmax(T& a,T b){bool x=a<b;if(x)a=b;return x;} 
template<class T> inline bool chmin(T& a,T b){bool x=a>b;if(x)a=b;return x;} 

void startupcpp(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout<<fixed<<setprecision(15);
}

double distance(ddatas x,ddatas y){
  double a=x.first-y.first,b=x.second-y.second;
  return sqrt(a*a+b*b);
}

ll modinv(ll a) {
  ll b=mod,u=1,v=0,t;
  while(b){
    t=a/b;
    a-=t*b; swap(a,b);
    u-=t*v; swap(u,v);
  }
  return (u+mod)%mod;
}

ll moddevide(ll a,ll b){return (a*modinv(b))%mod;}

vec modncrlistp,modncrlistm;

ll modncr(ll n,ll r){
  if(n<r)return 0;
  ll i,size=modncrlistp.size();
  if(size<=n){
    modncrlistp.resize(n+1);
    modncrlistm.resize(n+1);
    if(!size){
      modncrlistp[0]=modncrlistm[0]=1;
      size++;
    }
    For(i,size,n+1){
      modncrlistp[i]=modncrlistp[i-1]*i%mod;
      modncrlistm[i]=modinv(modncrlistp[i]);
    }
  }
  return modncrlistp[n]*modncrlistm[r]%mod*modncrlistm[n-r]%mod;
}

ll modpow(ll a,ll n){
  ll res=1;
  while(n>0){
    if(n&1)res=res*a%mod;
    a=a*a%mod;
    n>>=1;
  }
  return res;
}

ll gcd(ll a,ll b){if(!b)return a;return (a%b==0)?b:gcd(b,a%b);}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

ll countdigits(ll n){
  ll ans=0;
  while(n){n/=10;ans++;}
  return ans;
}

ll sumdigits(ll n){
  ll ans=0;
  while(n){ans+=n%10;n/=10;}
  return ans;
}

int main(){
  startupcpp();
  int codeforces;cin>>codeforces;while(codeforces--){
  ll A,B,ans=0,ok,ng,mid;
  cin>>A>>B;
  if(A>B)swap(A,B);
  ans=(A-1)<<1;
  if(A==B){
    print(ans);
    continue;
  }
  ok=A;ng=B;
  while(ng-ok>1){
    mid=(ok+ng)>>1;
    ((mid*mid<=A*B-1)?ok:ng)=mid;
  }
  ans+=2*(ok-A)+(ok*ng<A*B);
  print(ans);
}return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <algorithm>
#include <utility>
#include <cstring>
#include <cmath>
using namespace std;


int main() {
    int a,b,k;
    cin>>a>>b>>k;
    int g=min(a,b);
    for(int i=0;i<k;--g){
        if((a%g==0)&&(b%g==0)){
            ++i;
        }
    }
    cout<<(g+1)<<endl;
    return 0;
}


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, m;
  cin >> n >> m;
  int odd=0;
  rep(i,n) {
    int a;
    cin >> a;
    if(a%2!=0) odd++; 
  }
  ll ans = 1;
  rep(i,n-1) ans *= 2;
  if(odd==0&&m==1) ans = 0;
  else if(odd==0&&m==0) ans *= 2;
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define ALL(obj)   (obj).begin(),(obj).end()

#define debug(var)  do{std::cout << __LINE__ << "> " << #var << ": ";view(var);}while(0)
template<typename T> void view(T e){std::cout << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } }

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

using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;

template<int MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) val += MOD;
    }
    constexpr int getmod() { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};
const int MOD = 1000000007;
using mint = Fp<MOD>;

void Main() {
	int S; cin >> S;
	vector<mint> dp(S + 1);
	dp[0] = 0;
	dp[1] = 0;
	dp[2] = 0;
	dp[3] = 1;
	mint sum = 0;
	FOR(i,4,S+1) {
		dp[i] += 1;
		dp[i] += sum;
		sum += dp[i - 2];
	}
	cout << dp[S].val << endl;
}

int main() {
    cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	Main();
}


Measured memory_bytes: 3660