akhauriyash/Code-Regression huggingface dataset:
from collections import *
import copy
N,K=map(int,input().split())
A=list(map(int,input().split()))

lst=[0]
for i in range(0,N):
    lst.append((A[i]%K+lst[i])%K)
for i in range(len(lst)):
    lst[i]-=i
    lst[i]%=K

dic={}
count=0
for i in range(0,len(lst)):
    if lst[i] in dic:
        count+=dic[lst[i]]
        dic[lst[i]]+=1
    else:
        dic.update({lst[i]:1})
    a=i-K+1
    if a>=0:
        dic[lst[a]]-=1
print(count)


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

int main() {
	double N, a = 0.0;
	cin >> N;
	for (double i = 1.0; i <= N; ++i) {
		if (1.0 == fmod(i, 2.0)) ++a;
	}
	cout << a / N;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int x[100005];
int main(){
    int n, a, b;
    scanf("%d%d%d", &n, &a, &b);
    for(int i=1;i<=n;i++) scanf("%d", &x[i]);   
    long long ans = 0;
    for(int i=2;i<=n;i++) ans += min((x[i]-x[i-1])*1ll*a, b*1ll);
    printf("%lld\n", ans);
}

Measured memory_bytes: 640
#include <iostream>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    int n;
    string a, b;
    cin >> n >> a >> b;
    int odp = 2 * n;
    for (int dl = 1; dl <= n; ++dl)
    {
        bool db = true;
        for (int i = 0; i < dl; ++i)
            if (a[n - dl + i] != b[i])
                db = false;
        if (db)
            odp = 2 * n - dl;
    }
    cout << odp;
    return 0;
}



Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>

using namespace std;

int main(){
	int r, t, n;
	cin >> n;
	while (n--){
		scanf("%d%d", &r, &t);
		int a = t/30 * 5;
		int b = r/100;
		if (t%30 == 0){
			if (r%100 == 0){
				printf("%d\n", a+b);
			}else {
				printf("%d %d\n", a+b, a+b+1);
			}
		}else {
			if (r%100 == 0){
				printf("%d %d\n", a+b, a+b+5);
			}else {
				printf("%d %d %d %d\n", a+b, a+b+1, a+b+5, a+b+6);
			}
		}
	}
	return 0;
}

Measured memory_bytes: 3172
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define F first
#define S second
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int,int>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) { return b?gcd(b, a%b):a;}
ll lcm(ll a, ll b) { return a*b/gcd(a,b);}
ll MOD=1000000007;

int main(){
  int n,m;cin>>n>>m;
  if(n==m){
    cout<<"Yes"<<endl;
  }else{
    cout<<"No"<<endl;
  }
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
Int const INF = 1e10;

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

    int n; cin>>n; Int a[2*n]; Int os=0,es=0,sum=0;
    for (int i = 0; i < n; i++){
        cin>>a[i]; a[i+n]=a[i]; sum+=a[i];
    }
    for (int i = 0; i < n/2; i++){
        os+=a[i*2+1];
        es+=a[i*2+2];
    }
    int l=1,r=l+(n/2-1)*2;
    for (int i = 0; i < n/2; i++){
        cout<<sum-2*os<<' '<<sum-2*es<<' ';
        os-=a[l];es-=a[l+1];
        os+=a[r+2];es+=a[r+3];
        l+=2;r+=2;
    }
    cout<<sum-2*os<<'\n';
}

Measured memory_bytes: 2944
#include "bits/stdc++.h"
using namespace std;

int main() {
	int N, K;
	cin >> N >> K;
	vector<int>L(N);
	for (int i = 0; i < N; ++i) {
		cin >> L[i];
	}
	sort(L.rbegin(), L.rend());
	int ans = 0;
	for (int i = 0; i < K; ++i) {
		ans+= L[i];
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3632
import math

n = int(input())

prime_n = [2]

for i in range(3,n+1,2):
    if all( i % prime != 0 for prime in prime_n):
        prime_n.append(i)

multiply = []

for i in prime_n:
    k = 1
    temp = 0
    while i**k <= n:
        temp += math.floor(n/(i**k))
        k+=1
    
    multiply.append(temp)

ans = 1

for i in multiply:
    ans*=(i+1)
    ans%=((10**9)+7)

print(ans)
        

Measured memory_bytes: 3064
#include<iostream>
#include<math.h>
#include<cstring>
#include<set>
#include<algorithm>
#include<vector>
#include<map>
#include<iomanip>
#include<queue>
#include<string>
#include<bitset>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
typedef long long ll;
using namespace std;

const int N = 3e5 + 10000;
const ll INF = 1 << 31 - 1;
ll MOD =  1000000007;
long long dp[N];
long long a[125];
int main()
{
    string s;
    cin >> s;
    for(int i = 0; i< s.size(); i++)
    {
        if(s[i] == '9')
        {
            cout <<"Yes";
            return 0;
        }
    }
    cout << "No";
    return 0;
}

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

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end(), greater<int>());
  int l = a[0];
  int min_v = 0, min_diff = a[0];
  for (int i = 1; i < n; i++) {
    int d = abs(l / 2 - a[i]);
    if (min_diff > d) {
      min_diff = d;
      min_v = a[i];
    }
  }
  cout << l << " " << min_v << endl;
  return 0;
}

Measured memory_bytes: 764
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Read as T

main :: IO ()
main = do
  s <- T.getLine :: IO T.Text
  let
    f :: Int -> Char
    f = T.index s
  putStrLn $ if f 2 == f 3 && f 4 == f 5 then "Yes" else "No"


Measured memory_bytes: 508
#include<cstdio>
int a[60];
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	int sum=0;
	for(int i=1;i<=n;i++){
		for(int j=i+1;j<=n;j++){
			sum+=a[i]*a[j];
		}
	}
	printf("%d",sum);
	return 0;
}

Measured memory_bytes: 128
using System;
using System.Linq;
using System.Collections.Generic;

class P
{
    static void Main()
    {
        //int[] NMA = Console.ReadLine().Split().Select(int.Parse).ToArray();
        int a = string.Compare(Console.ReadLine().PadLeft(101,'0'), Console.ReadLine().PadLeft(101, '0'));
        
        Console.WriteLine(a > 0 ? "GREATER" : (a < 0 ? "LESS" : "EQUAL"));
    }
}

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

void lcs( string X,string Y, int m, int n )
{
   int L[m+1][n+1];

   /* Following steps build L[m+1][n+1] in bottom up fashion. Note
      that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] */
   for (int i=0; i<=m; i++)
   {
     for (int j=0; j<=n; j++)
     {
       if (i == 0 || j == 0)
         L[i][j] = 0;
       else if (X[i-1] == Y[j-1])
         L[i][j] = L[i-1][j-1] + 1;
       else
         L[i][j] = max(L[i-1][j], L[i][j-1]);
     }
   }

   // Following code is used to print LCS
   int index = L[m][n];

   // Create a character array to store the lcs string
   char lcs[index+1];
   lcs[index] = '\0'; // Set the terminating character

   // Start from the right-most-bottom-most corner and
   // one by one store characters in lcs[]
   int i = m, j = n;
   while (i > 0 && j > 0)
   {
      // If current character in X[] and Y are same, then
      // current character is part of LCS
      if (X[i-1] == Y[j-1])
      {
          lcs[index-1] = X[i-1]; // Put current character in result
          i--; j--; index--;     // reduce values of i, j and index
      }

      // If not same, then find the larger of two and
      // go in the direction of larger value
      else if (L[i-1][j] > L[i][j-1])
         i--;
      else
         j--;
   }

   // Print the lcs
   cout << lcs;
}
int main()
{string s, t;
 cin>>s>>t;
 int m=s.size();
 int n=t.size();
 lcs(s,t,m,n);
 return 0;
}


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

class UnionFind
{
	vector<int> par, rank;

public:
	UnionFind(int n) {
		par = rank = vector<int>(n);
		for (int i = 0; i < n; i++) {
			par[i] = i;
			rank[i] = 0;
		}
	}

	int find(int x) {
		if (x == par[x]) {
			return x;
		}
		else {
			return par[x] = find(par[x]);
		}
	}

	void unite(int x, int y) {
		x = find(x);
		y = find(y);
		if (x == y) {
			return;
		}
		if (rank[x] < rank[y]) {
			par[x] = y;
		}
		else {
			par[y] = x;
			if (rank[x] == rank[y]) {
				rank[x]++;
			}
		}
	}

	bool same(int x, int y) {
		return find(x) == find(y);
	}
};

int main()
{
    int n, m;
    cin >> n >> m;
    int a[51], b[51];
    for(int i = 0; i < m; i++){
        cin >> a[i] >> b[i];
        a[i]--;
        b[i]--;
    }
    int ans = 0;
    for(int i = 0; i < m; i++){
        UnionFind uf(n);
        for(int j = 0; j < i; j++){
            uf.unite(a[j], b[j]);
        }
        for(int j = i + 1; j < m; j++){
            uf.unite(a[j], b[j]);
        }            
        if(!(uf.same(a[i], b[i]))){
            ans++;
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <stdio.h>
#include <cmath>

using namespace std;

typedef unsigned long long ull;
typedef unsigned int uint;
typedef unsigned char uchar;
#define umap unordered_map
const static ull INVALID_DIGIT = ~(ull)0;

static const ull MEMO_MAX_SIZE = 100001;
ull possiblePatternMemo[MEMO_MAX_SIZE];

umap<ull, int> isBroken;

static const ull DIVIDER = 1000000007l;

int main()
{
    ull N;
    cin >> N;
    std::vector<ull> ds(N);

    for (ull i = 0; i < N; ++i) {
        cin >> ds[i];
    }
    std::sort(ds.begin(), ds.end());

    size_t centerPlusOneIdx = ds.size() / 2;
    size_t centerMinusOneIdx = centerPlusOneIdx - 1;

    ull minForARC = ds[centerPlusOneIdx];
    ull exclusiveMaxForABC = ds[centerMinusOneIdx];

    cout << (minForARC - exclusiveMaxForABC) << endl;

    return 0;
}

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

//#pragma GCC optimize ("Ofast")
//#pragma GCC target ("avx,avx2")

#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define dbg(x) cerr<<#x<<" : "<<x<<endl

typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vii;
const ll INF = 1e18+1;
const ll MOD = 1000000007L;
clock_t time_p=clock();
void timer(){time_p=clock()-time_p;cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";}

template <typename Arg1> ostream& operator << (ostream& out, const vector<Arg1> &a) {out<<"[ ";for(const auto &x:a)out<<x<<" ";return out<<"]";}


int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    const ll iter=1e8;
    ll ans = 0;
    ll a,b,c,a1,b1,c1;
    cin>>a>>b>>c;
    ll ctr=0;
    while(((a&1)==0) && ((b&1) == 0) && ((c&1)==0)){
        ctr++;
        if(ctr>=iter) {
            cout<<-1<<endl;
            timer();
            exit(0);
        }
        ans++;
        a1=(b+c)>>1;
        b1=(c+a)>>1;
        c1=(b+a)>>1;
        a=a1;
        b=b1;
        c=c1;
    }
    cout<<ans<<endl;

    timer();
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
int a[100000];
int main(){
	int n,k,best,g;
	while(true){
	cin>>n>>k;
	if(n==0&&k==0)
		break;
	best=0;
	g=0;
	for(int i=0;i<n;i++){
		cin>>a[i];
		if(i<k)
			g+=a[i];
	}
	best=g;
	for(int i=1;i<=n-k;i++){
		g+=a[i+k-1]-a[i-1];
		if(g>best)
			best=g;
	}
	cout<<best<<endl;
	}
    return 0;
}

Measured memory_bytes: 1548
#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <iomanip>	
#include <deque>
#include <set>
#define rep(i,a,b) for(int i=a;i<b;++i)
#define rrep(i,a,b) for(int i=a;i>=b;--i)
#define fore(i,a) for(auto &i:a)
#define INF INT_MAX/2;
const int INF2 = 1001001001;
typedef long long ll;
using namespace std;
using vi = vector<int>;
using vll = vector<ll>;
ll mod = 1e9 + 7;
//ll llmax = 10000000000000000000ll;
using namespace std;
using Graph = vector<vector<int>>;


int cnt_digit(ll N)
{
	int digit = 0;

	while (N > 0)
	{
		N /= 10;
		digit++;
	}
	return digit;
}


 // 最大公約数計算
 
ll gcd(ll a, ll b)
{
	if (b == 0) {
		return a;
	}
	else {
		return gcd(b, a % b);
	}
}
// 最小公倍数の計算

ll lcm(ll a, ll b)
{
	ll g = gcd(a, b);

	return a / g * b;
}

struct union_find
{
	vector<int> par, r;
	union_find(int n)
	{
		par.resize(n);
		r.resize(n);
		init(n);
	}


	void init(int n)
	{
		for (int i = 0; i < n; i++) par[i] = i;
		for (int i = 0; i < n; i++) r[i] = 0;
	}

	int find(int x)
	{
		if (par[x] == x)return x;
		else return find(par[x]);
	}

	void unite(int x, int y)
	{
		x = find(x);
		y = find(y);

		if (r[x] < r[y])
		{
			par[x] = y;
		}
		else
		{
			par[y] = x;
			if (r[x] == r[y])
			{
				r[x]++;
			}
		}
	}
};

const int MAX_N = 110000;

int memo[MAX_N];

int rec(int n) {
	if (n == 0)return 0;
	if (memo[n] != -1)return memo[n];

	int res = n;

	for (int pow6 = 1; pow6 <= n; pow6 *= 6)res = min(res, rec(n - pow6) + 1);

	for (int pow9 = 1; pow9 <= n; pow9 *= 9)res = min(res, rec(n - pow9) + 1);

	return memo[n] = res;
}

int calc(int x) {
	int ret = 0;
	while (x % 2 == 0) {
		x /= 2;
		ret++;
	}

	return ret;
}

int a[12][12];
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	int k;

	string s;

	cin >> k >> s;

	if (s.size() <= k) {
		cout << s << endl;
	}
	else {
		cout << s.substr(0, k) << "..." << endl;
	}
	
}
	

Measured memory_bytes: 3656
#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define mp make_pair
#define bs binary_search
#define FOR(i,a,b) for(int i=a; i<b; i++)
#define For(i,a,b) for(int i=a; i>b ;i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define setprec(x,k) cout << fixed << setprecision(x) << k
#define one(x) __builtin_popcount(x)
#define endl "\n"
#define mod 1000000007

int main()
{
	IOS;
	ll int d, t, s;
	cin>>d>>t>>s;
	double ans=double(d)/double(s);
	if(ans<=t)
		cout<<"Yes"<<endl;
	else
		cout<<"No"<<endl;
}

Measured memory_bytes: 3616
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	Scanner sc = new Scanner(System.in);

	void run() {
		int N = sc.nextInt();
		int M = sc.nextInt();
		int p = sc.nextInt();
		int[] d = new int[M + 1];
		for (int i = 0; i < M; i++) d[i] = sc.nextInt();
		d[M] = p;
		Arrays.sort(d);
		int min = Integer.MAX_VALUE;
		int start = Arrays.binarySearch(d, p);
		for (int i = 0; i <= M; i++) {
			// to right
			int right = 0;
			if (start + i > M) {
				right += N - d[start];
				right += d[(start + i) % (M + 1)];
			} else {
				right += d[start + i] - d[start];
			}
			// to left
			int left = 0;
			if (start + i + 1 > M) {
				left += d[start] - d[(start + i + 1) % (M + 1)];
			} else {
				left += d[start];
				left += N - d[(start + i + 1) % (M + 1)];
			}
			min = Math.min(min, Math.min(right * 2 + left, right + left * 2));
		}
		System.out.println(min * 100);
	}

	public static void main(String[] args) {
		new Main().run();
	}

	void debug(Object... o) {
		System.out.println(Arrays.deepToString(o));
	}
}

Measured memory_bytes: 34432
#include <stdio.h>

int main()
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
if(a<=b){
   if(c<=a){
      printf("%d %d %d\n",c,a,b);
   }else if(b<=c){
      printf("%d %d %d\n",a,b,c);
   }else{
      printf("%d %d %d\n",a,c,b);
}}else{
   if(c<=b){
      printf("%d %d %d\n",c,b,a);
   }else if(a<=c){
      printf("%d %d %d\n",b,a,c);
   }else{
      printf("%d %d %d\n",b,c,a);
}}
return 0;
}

Measured memory_bytes: 596
fun main(args: Array<String>) {
    val (H, W) = listOfInt()
    val S = Array(H) { next() }
    val f = Array(H) { IntArray(W) }
    repeat (H) { i ->
        repeat (W) { j ->
            if (i + j == 0) {
                f[0][0] = if (S[0][0] == '#') 1 else 0
            } else {
                f[i][j] = 1000000007
                if (i > 0) f[i][j] = Math.min(f[i][j], f[i - 1][j] + if (S[i - 1][j] == '.' && S[i][j] == '#') 1 else 0 )
                if (j > 0) f[i][j] = Math.min(f[i][j], f[i][j - 1] + if (S[i][j - 1] == '.' && S[i][j] == '#') 1 else 0 )
            }
        }
    }
    println(f[H - 1][W - 1])
}
fun next() = readLine()!!
fun nextInt() = Integer.parseInt(next())
fun nextInt(offset: Int = 0) = nextInt() + offset
fun nextLong() = java.lang.Long.parseLong(next())
fun nextLong(offset: Long = 0L) = nextLong() + offset
fun nextDouble() = next().toDouble()
fun listOfString() = next().split(" ")
fun listOfInt() = listOfString().map { Integer.parseInt(it) }
fun listOfInt(offset: Int = 0) = listOfString().map { Integer.parseInt(it) + offset }
fun listOfLong() = listOfString().map { java.lang.Long.parseLong(it) }
fun listOfLong(offset: Long = 0L) = listOfString().map { java.lang.Long.parseLong(it) + offset }
fun listOfDouble() = listOfString().map { it.toDouble() }


Measured memory_bytes: 37740
#include <iostream>
#include <math.h>
using namespace std;
int f[100000], n;
int main(void){
    // Your code here!
    cin >> n;
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        f[a]++;
    }
    int ans = 0;
    for (int i = (n + 1) % 2; i < n; i += 2) {
        if (i == 0) {
            if (f[i] != 1) ans++;
        } else {
            if (f[i] != 2) ans++;
        }
    }
    if (ans == 0) {
        int res = 1;
        for (int i = 0; i < n / 2; i++) res = res * 2 % 1000000007;
        cout << res << endl;
    }
    else cout << 0 << endl;
}


Measured memory_bytes: 640
import sys
def inpl(): return list(map(int, sys.stdin.readline().split()))
# 直接输入成list

x = inpl()
y = inpl()
for i in range(0, x[1]):
    x[0] -= y[i]

if x[0] < 0:
    print(-1)
else:
    print(x[0])

Measured memory_bytes: 10224
#include <iostream>
#include <vector>
#include <stack>
#include <cstring>
#define N 101

using namespace std;

int visited[N], nt[N], discovery[N], finish[N], tt, n;
vector<int> G[N];

int next(int u) {
    for (int v = nt[u]; v < G[u].size(); v++) {
        nt[u] = v + 1;
        return G[u][v];
    }
    return -1;
}

void dfs(int r) {
    memset(nt, 0, sizeof(nt));

    stack<int> S;
    S.push(r);

    visited[r] = 1;
    discovery[r] = ++tt;

    while (!S.empty()) {
        int u = S.top();
        int v = next(u);

        if (v != -1) {
            if (visited[v] == 0) {
                visited[v] = 1;
                discovery[v] = ++tt;
                S.push(v);
            }
        }
        else {
            S.pop();
            visited[u] = 1;
            finish[u] = ++tt;
        }
    }
}

int main() {
    int u, k, v;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> u >> k;
        u--;
        for (int j = 0; j < k; j++) {
            cin >> v;
            v--;
            G[u].push_back(v);
        }
    }

    memset(visited, 0, sizeof(visited));
    tt = 0;

    for (int i = 0; i < n; i++) {
        if (visited[i] == 0) {
            dfs(i);
        }
    }

    for (int i = 0; i < n; i++) {
        cout << i + 1 << " " << discovery[i] << " " << finish[i] << endl;
    }

    return 0;
}

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


int main(){
    string S,T;
    cin>>S>>T;
    int result,max=0,Ssize,Tsize;
    Ssize=S.size();
    Tsize=T.size();
    for(int i=0;i<=Ssize-Tsize;i++){
        result=0;
        for(int j=0;j<Tsize;j++){
            if(S.at(i+j)==T.at(j)){
                result++;
            }
        }
        if(result>max){
            max=result;
        }
    }
    cout<<T.size()-max<<endl;
    

}


Measured memory_bytes: 3628
def count(limit, max, n, queue)
  return queue.inject(:+) == max ? 1 : 0 if queue.size == limit

  (n..9).step.inject(0) do |result, i|
    result += count(limit, max, i + 1, queue << i)
    queue.pop
    result
  end
end

def bfs
  while gets
    limit, max = $_.split.map(&:to_i)
    break if limit == 0 && max == 0
    puts count(limit, max, 0, [])
  end
end

bfs

Measured memory_bytes: 8908
;; C - Count Order

(defun main ()
  (let* ((N (read))
         (P (loop repeat N collect (read)))
         (Q (loop repeat N collect (read))))
    (princ (solve N P Q))
    (fresh-line)))

(defun solve (N P Q)
  (let* ((elems (sort (copy-list P) #'<))
         (perms (permutations elems))
         (a (position P perms :test #'equal))
         (b (position Q perms :test #'equal)))
    (abs (- a b))))

(defun permutations (elems)
  (if elems
      (loop for e in elems
        append (mapcar #'(lambda (ls) (cons e ls))
                       (permutations (remove e elems))))
      (list nil)))

(main)


Measured memory_bytes: 29032
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ull unsigned long long int
#define set_bits(a) __builtin_popcount(a)
#define pb push_back
#define pf push_front
#define mod 1000000007
#define M 998244353
#define mp unordered_map
#define fi first
#define se second
#define endl '\n'
#define INF 1e18
#define PI 3.14159265358979323846
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

int main()
{
    ll a,b,c,d;
    cin>>a>>b>>c>>d;

    vector<ll>v;
    ll x;
    x = a*c;
    v.pb(x);
    x = a*d;
    v.pb(x);
    x = b*d;
    v.pb(x);
    x = b*c;
    v.pb(x);

    ll ans = *max_element(v.begin(),v.end());
    cout<<ans<<endl;
}


Measured memory_bytes: 3604
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
bool num[30][20];
int main() {
	//freopen("in","r",stdin);

	memset(num,0,sizeof(num));
	int n; cin>>n;
	for(int i=0;i<n;i++) {
		char a; int b; cin>>a>>b;
		num[a-'A'][b] = 1;
	}

	//S
	for(int i=1;i<=13;i++) {
		if(!num['S'-'A'][i]) cout<<"S "<<i<<endl;
	}

	//H
	for(int i=1;i<=13;i++) {
		if(!num['H'-'A'][i]) cout<<"H "<<i<<endl;
	}

	//C
	for(int i=1;i<=13;i++) {
		if(!num['C'-'A'][i]) cout<<"C "<<i<<endl;
	}

	//D
	for(int i=1;i<=13;i++) {
		if(!num['D'-'A'][i]) cout<<"D "<<i<<endl;
	}
	return 0;
}


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

int main(){
    int x, n;
    vector<int> p(100);
    cin >> x >> n;
    rep(i, n){
        cin >> p.at(i);
    }

    int min = 1000;
    rep(i, 102){
        bool exist = false;
        rep(j, n){
            if(i == p.at(j))exist = true;
        }
        if(!exist && abs(x-i) < abs(x-min))min = i;
    }
    cout << min << endl;
    return 0;
}

Measured memory_bytes: 3592
/**
*    author:  boutarou
*    created: 21.06.2020 21:08:55
**/

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < int(n); i++)
#define fcout cout << fixed << setprecision(15)
using lint = long long;
using P = pair<int, int>;

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

const double PI = 3.1415926535897932;

int n;
int q;
int a[100005];
int cnt[100008];
lint ans = 0;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    cin >> n;
    rep(i, n) {
        cin >> a[i];
        cnt[a[i]]++;
        ans += a[i];
    }
    cin >> q;
    rep(i, q) {
        int b, c;
        cin >> b >> c;
        cnt[c] += cnt[b];
        ans += cnt[b] * (c - b);
        cnt[b] = 0;
        cout << ans << endl;
    }
    
    return 0;
}

Measured memory_bytes: 4392
#include <stdio.h>
int main(void){
  int N, a[100001], sum = 0;
  scanf("%d", &N);
  for(int i = 0; i < N; i++){
    scanf("%d", &a[i]);
  }
  for(int i=0; i<N; i++){
    sum = sum ^ a[i];
  }
  if(sum == 0){printf("Yes");}
  else{printf("No");}
  return 0;
}
        

Measured memory_bytes: 512
/*
confirm 0LL and 1LL
confirm cornercases such as 0
confirm times of cin < 10^6
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecS = vector<string>;
using VecVec = vector<Vec>;
using Tree = vector<VecP>;
#define REP(i, m, n) for(ll (i) = (m); (i) < (n); ++(i))
#define REPN(i, m, n) for(ll (i) = (m); (i) <= (n); ++(i))
#define REPR(i, m, n) for(ll (i) = (m)-1; (i) >= (n); --(i))
#define REPNR(i, m, n) for(ll (i) = (m); (i) >= (n); --(i))
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fs first
#define sc second
template <typename T>
bool chmax(T &a, const T& b){if(a < b){a = b; return true;} return false;}
template <typename T>
bool chmin(T &a, const T& b){if(a > b){a = b; return true;} return false;}
template <typename T>
void co(const T n){cout << n << endl;}
template <typename T>
void cosp(const T n){cout << n << ' ';}
void coVec(const Vec &v){for(ll i : v) cosp(i); cout << endl;}
void sonic(){ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);}
void setp(const ll n){cout << fixed << setprecision(n);}
const ll INF = 1e9+1;
const ll LINF = 1e18+1;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const double PI = acos(-1);
const double EPS = 1e-9;

int main(void){
	ll k, x;
	cin >> k >> x;
	if(k*500 >= x) puts("Yes");
	else puts("No");

	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <iomanip>
#include <bitset>
#include <set>
#include <map>
#include <stdio.h>
#include <numeric>
#include <cstring>

#define rep(i,n) for (int i = 0; i < (n); i++)
#define rep1(i,n) for (int i = 1; i < (n); i++)
#define FOR(i,a,b) for (int i=(a); i < (b); i++)
#define MOD 1000000007 //10^9+7
using namespace std;
using ll = long long;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;
const int INF = numeric_limits<int>::max();
const ll INFL = numeric_limits<ll>::max();


// 負の数にも対応した % 演算
long long mod(long long val, long long m) {
    long long res = val % m;
    if (res < 0) res += m;
    return res;
}

//greatest common divisor
long long gcd(ll a, ll b) 
{
    if (a % b == 0) {
        return b;
    } else {
        return gcd(b, a % b);
    }
}

//least common multiple
long long lcm(ll a, ll b)
{
    return a / gcd(a, b) * b ;
}

ll factorial(ll n) {
    ll res = 1;
    for (ll i = 1; i <= n; i++) {
        res *= i;
    }
    return res;
}

int main()
{
    int k, x; cin>>k>>x;
    if (k + x == 3) {
        cout << 3 << endl;
    } else if (k+x == 4) {
        cout << 2 << endl;
    } else {
        cout << 1 << endl;
    }

    return 0;
}


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

#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef complex<double> point;
#define xx real()
#define yy imag()

#define REP(i, a, b) for(int i = (a); i < (int)(b); i++)
#define REPN(i, a, b) for(int i = (a); i <= (int)(b); i++)
#define FA(it, x) for(__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define SZ(x) (int)(x).size()
#define BE(x) (x).begin(), (x).end()
#define SORT(x) sort(BE(x))
#define _1 first
#define _2 second

#define x1 gray_cat_x1
#define y1 gray_cat_y1

template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

#define file "I1"

const double EPS = 1e-9;
const double PI = acos(-1.);
const ll INF = 1e18;
const ll MOD = 1e9 + 7;

const int MAXN = 2e5 + 5;

ll v[2][MAXN];

ll dp[2][MAXN];

int n;

void calc(int ind) {
	ll lim = 1ll << 34;
	dp[ind][0] = dp[ind][1] = 0ll;
	ll last = 0;
	REPN(i, 2, n) {
		dp[ind][i] = dp[ind][i - 1];
		int j;
		ll mul = 0ll;
		int new_last = last;
		for(j = i - 1; j > last && v[ind][j] < v[ind][j + 1]; j--) {
			for(; v[ind][j] < lim && v[ind][j] < v[ind][j + 1]; v[ind][j] *= 4, dp[ind][i] += 2);
			if (v[ind][j] >= lim) {
				new_last = max(new_last, j);
			}
			if (v[ind][j] < v[ind][j + 1]) {
				mul++;
			}
			dp[ind][i] += 2ll * mul;
		}
		if (last >= 1 && v[ind][last] < v[ind][last + 1]) {
			mul++;
		}
		dp[ind][i] += last * 2ll * mul;
		last = new_last;
	}
}

void solve(){
	scanf("%d", &n);
	REPN(i, 1, n) {
		scanf("%lld", &v[0][i]);
		v[1][n + 1 - i] = v[0][i];
	}
	REP(i, 0, 2) {
		calc(i);
	}
	ll ans = INF;
	REPN(i, 0, n) {
		ans = min(ans, i + dp[0][i] + dp[1][n - i]);
	}
	/*REP(j, 0, 2) {
		REPN(i, 0, n) {
			printf("%lld ", dp[j][i]);
		}
		putchar('\n');
	}*/
	
	cout << ans << endl;
}   

int main(){

    //freopen(file".in", "r", stdin); freopen(file".out", "w", stdout);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();    
    }
}


Measured memory_bytes: 6528
#include<stdio.h>
void selectionSort(int *A,int n,int *cnt);
int main(void){
    int n,A[128],i,cnt=0;
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%d",&A[i]);
    selectionSort(A,n,&cnt);
    for(i=0;i<n-1;i++)
        printf("%d ",A[i]);
    printf("%d\n",A[n-1]);
    printf("%d\n",cnt);
    return 0;
}
void selectionSort(int *A,int n,int *cnt){
    int i,j,minj,temp;
    for(i=0;i<n;i++){
        minj=i;
        for(j=i;j<n;j++)
            if(A[j]<A[minj]){
                minj=j;
            }
        if(minj!=i){
            temp=A[i];
            A[i]=A[minj];
            A[minj]=temp;
            *cnt=*cnt+1;
        }
    }
}

Measured memory_bytes: 600
#include<bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define REP2(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(v) (v).begin(),(v).end()
#define INF 2e9
typedef long long ll;

int main()
{
    int n;
    cin>>n;
    vector<int> a(n);
    REP(i,n) cin>>a[i];

    vector<int> a400(n);
    REP(i,n) a400[i]=(a[i]<3200)? a[i]/400:8;
    sort(ALL(a400));

    int r=0;
    vector<bool> f(8,0);
    REP(i,n){
        if(a400[i]==8) r++;
        else f[a400[i]]=1;
    }
    int v=0;
    REP(i,8) if(f[i]==1) v++;

    cout<<max(v,1)<<" "<<v+r<<endl;

	return 0; 
}

Measured memory_bytes: 256
H,W,D = map(int,input().split())
A = []

for i in range(H):

    a = list(map(int,input().split()))
    A.append(a)

dic = {}


for i in range(H):

    for j in range(W):

        dic[A[i][j]] = [i,j]

Q = int(input())

nlis = [0]
for i in range(H * W):

    i += 1

    if i - D < 1:
        nlis.append(0)

    else:
        now = dic[i]
        las = dic[i-D]
        nlis.append(nlis[i-D] + abs(now[0]-las[0]) + abs(now[1]-las[1]))


for i in range(Q):

    L,R = map(int,input().split())

    print(nlis[R] - nlis[L])
        


Measured memory_bytes: 26120
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n;
long long ans;
int main() {
	cin >>n;
	for(int i = 1;i <= n;++i)
		if(i % 3 && i % 5) ans += i;
	cout << ans << endl;
	return 0;
}


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

class Block{
public:
  char block[2][2];
  Block(char a,char b,char c,char d){
    block[0][0]=a;
    block[0][1]=b;
    block[1][0]=c;
    block[1][1]=d;
  }
  bool full(){
    if(block[0][0]=='#' && block[0][1]=='#' && 
       block[1][0]=='#' && block[1][1]=='#' )return true;
    return false;
  }
};

int h,n,ans;
vector<Block> N;
int X[9]={0,1,0,-1,0,1,1,-1,-1};
int Y[9]={-1,0,1,0,0,1,-1,1,-1};

bool in(int a,int b){
  if(a<0 || b<0 || 2<=a || 2<=b)return false;
  return true;
}

Block move(Block B,int k){
  Block res('.','.','.','.');
  for(int i=0;i<2;i++){
    for(int j=0;j<2;j++){
      int a=i+Y[k],b=j+X[k];
      if(in(a,b) && B.block[i][j]=='#')res.block[a][b]='#';
    }
  }
  return res;
}

vector<Block> fall(vector<Block> H,Block f,Block s){
  H.push_back(Block('.','.','.','.'));
  for(int i=H.size()-1;i>0;i--){
    bool flag=false;
    for(int j=0;j<2;j++){
      for(int k=0;k<2;k++){
	if(f.block[j][k]=='#' && H[i-1].block[j][k]=='#')flag=true;
        if(s.block[j][k]=='#' && H[i].block[j][k]=='#')flag=true;
      }
    }
    if(flag){
      if(i==H.size()-1){
	H.pop_back();
	H.push_back(f);
	H.push_back(s);
      }
      else {
	for(int j=0;j<2;j++){
	  for(int k=0;k<2;k++){
	    if(f.block[j][k]=='#')H[i].block[j][k]='#';
	    if(s.block[j][k]=='#')H[i+1].block[j][k]='#';
	  }
	}
      }
      break;
    }
  }
  return H;
}

void rec(vector<Block> H, int index,int sum){
  if(n*2==index){
    ans=max(ans,sum);
    return;
  }
  else {
    for(int k=0;k<9;k++){
      bool flag=true;
      for(int i=0;i<2;i++){
	for(int j=0;j<2;j++){
	  int ni=i+Y[k],nj=j+X[k];
	  if(N[index].block[i][j]=='#' && !in(ni,nj))flag=false;
	  if(N[index+1].block[i][j]=='#' && !in(ni,nj))flag=false;
	}
      }
      if(flag){
	vector<Block> ch=fall(H,move(N[index],k),move(N[index+1],k));
	int count=0;
	int size=ch.size();
	for(int j=2;j<size;j++){
	  if(ch[j].full()){
	    ch.erase(ch.begin()+j);
	    size=ch.size();
	    j--;
	    count++;
	  }
	}
	rec(ch,index+2,sum+count);
      }
    }
  }
}

int main()
{
  string a,b;
  vector<Block> H;

  while(1){
    cin>>h>>n;
    if(h+n==0)break;
    H.clear();
    N.clear();
    H.push_back(Block('#','#','#','#'));
    H.push_back(Block('#','#','#','#'));
    for(int i=0;i<h;i++){
      cin>>a>>b;
      H.push_back(Block(a[0],a[1],b[0],b[1]));
    }
    for(int i=0;i<n*2;i++){
      cin>>a>>b;
      N.push_back(Block(a[0],a[1],b[0],b[1]));
    }

    ans=0;    
    rec(H,0,0);
    
    cout<<ans<<endl;
  }
  return 0;
}

Measured memory_bytes: 1208
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> G[100000];
ll du[100000];
ll dv[100000];
const ll INF = 10000000000;


void distu(ll u) {
  for (ll i = 0; i < 100000; i++) {
    du[i] = INF;
  }
  du[u] = 0;
  queue<ll> que;
  que.push(u);
  
  while (!que.empty()) {
    ll p = que.front();
    que.pop();
    for (ll i = 0; i < G[p].size(); i++) {
      ll to = G[p][i];
      if (du[to] == INF) {
        du[to] = du[p]+1;
        que.push(to);
      } else {
        continue;
      }
    }
  }
}

void distv(ll v) {
  for (ll i = 0; i < 100000; i++) {
    dv[i] = INF;
  }
  dv[v] = 0;
  queue<ll> que;
  que.push(v);
  
  while (!que.empty()) {
    ll p = que.front();
    que.pop();
    for (ll i = 0; i < G[p].size(); i++) {
      ll to = G[p][i];
      if (dv[to] == INF) {
        dv[to] = dv[p]+1;
        que.push(to);
      } else {
        continue;
      }
    }
  }
}

int main() {
  ll N, u, v; cin >> N >> u >> v; u--; v--;
  for (ll i = 0; i < N-1; i++) {
    ll a, b; cin >> a >> b; a--; b--;
    G[a].push_back(b); G[b].push_back(a);
  }
  
  distu(u);
  distv(v);
  ll ans = 0;
  for (ll i = 0; i < N; i++) {
    if (du[i] < dv[i]) {ans = max(ans, dv[i]-1);}
  }
  cout << ans << endl;
}

Measured memory_bytes: 8064
#include <iostream>
using namespace std;
int main() {
    int A,B,C,D;
    cin>>A>>B>>C>>D;
    bool flg =false;
    
    while(1)
    {
      //高橋ターン
      C-=B;
      if(C<=0){
        flg=true;
        break;
      }
      //相手ターン
      A-=D;
      if(A<=0){
        flg=false;
        break;
      }
      
    }
  
    if(flg)
      cout<<"Yes"<<endl;
    else
      cout<<"No"<<endl;
 
    return 0;
}

Measured memory_bytes: 3636
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;

class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Map<String, List<Integer>> ref = new HashMap<String, List<Integer>>();
		PriorityQueue<String> q = new PriorityQueue<String>();
		while(sc.hasNext()){
			String s = sc.next();
			if(ref.containsKey(s)){
				ref.get(s).add(sc.nextInt());
			}
			else{
				q.add(s);
				List<Integer> list = new ArrayList<Integer>();
				list.add(sc.nextInt());
				ref.put(s, list);
			}
		}
		while(!q.isEmpty()){
			String s = q.poll();
			List<Integer> list = ref.get(s);
			System.out.println(s);
			Collections.sort(list);
			boolean f = true;
			for(int v:list){
				if(!f)System.out.print(" ");
				f = false;
				System.out.print(v);
			}
			System.out.println();
		}
	}
}

Measured memory_bytes: 26208
N, M, X = map(int, input().split())
A = list(map(int, input().split()))

goal_N = 0
goal_0 = 0

for i in range(X, N + 1):
    if i in A:
        goal_N += 1

for i in range(0, X + 1):
    if i in A:
        goal_0 += 1

min_cost = min(goal_N,goal_0)

print(min_cost)

Measured memory_bytes: 9188
a,b,c,d=map(int,input().split())
def gcd(x,y):
    while y!=0:
        x,y=y,x%y
    return x
def lcm(x,y):
    return x*y//gcd(x,y)
ans_b=b-b//c-b//d+b//lcm(c,d)
z=a-1
ans_a=z-z//c-z//d+z//lcm(c,d)
print(ans_b-ans_a)

Measured memory_bytes: 3060
#include<iostream>
#include<fstream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<deque>
#include<set>
#include<map>
#include<bitset>
#include<ctime>
#include<queue>
using namespace std;
#define int long long
#define pii pair <int, int>
#define XX first
#define YY second

// constants:
const int mn = 2005;
const int mod = 1000000007;
const long long inf = 4444444444444444444;
const int sminf = 1111111111;
const bool is_multitest = 0;
const bool is_interactive = 0;

// i/o methods:
const bool input_from_file = 0;
const bool output_to_file = 0;
#define filename ""
#define in_extension "inp"
#define out_extension "out"

// data preprocessing: (e.g.: divisor generating, prime sieve)
void preprocess()
{
    
}

// global variables:
int n;
pii A[mn];

int dpret[mn][mn];
int dp(int u, int l) {
    if (dpret[u][l] != -1) return dpret[u][l];
    if (u == 0) {
        if (l==0) return 0;
        else return -inf;
    }

    int m1 = -inf, m2 = -inf;
    if (l>0) m1 = dp(u-1, l-1);
    if (u>l) m2 = dp(u-1, l);
    return dpret[u][l] = max(m1 + A[u].XX * abs(A[u].YY - l), m2 + A[u].XX * abs(A[u].YY - (n-u+l+1)));
}

// main solution goes here:
void execute(int test_number)
{
    for (int i=0; i<mn; i++) for (int j=0; j<mn; j++) dpret[i][j] = -1;
    cin>>n;
    for (int i=1; i<=n; i++) cin>>A[i].XX, A[i].YY = i;
    sort(A+1, A+n+1);
    reverse(A+1, A+n+1);

    int ans = -inf;
    for (int i=0; i<=n; i++) ans=max(ans, dp(n, i));
    cout<<ans;
}
// REMEMBER TO CHOOSE TEST METHODS
// PLEASE REMOVE cout AND cerr DEBUG LINES BEFORE SUBMITTING PROBLEMS
// Solution by low_



















signed main()
{
#ifdef lowie
    if (!is_interactive)
    {
        freopen("test.inp", "r", stdin);
        freopen("test.out", "w", stdout);
    }
#else
    if (input_from_file) freopen(filename"."in_extension, "r", stdin);
    if (output_to_file) freopen(filename"."out_extension, "w", stdout);
#endif
    if (!is_interactive)
    {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    }

    preprocess();
    int ntest;
    if (is_multitest) cin >> ntest;
    else ntest = 1;

    for (int testno = 1; testno <= ntest; testno++)
    // use for instead of while to serve facebook hacker cup test format
    {
        execute(testno); // only start coding in this function, not in main
    }
}
// Template by low_
// Contact me via mail: ttuandung1803@gmail.com
// ...or codeforces: www.codeforces.com/profiles/low_
// ...or if you're interested in food: www.instagram.com/lowie.exe/

Measured memory_bytes: 35152
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int maxn = 5 * 1e3 + 10;
using namespace std;

int n, m, x;
int a[maxn], k;
int dp_lift[2][maxn];
int dp_right[2][maxn];
vector<int> gl[maxn], gr[maxn];

void init() {
    memset(dp_lift, 0, sizeof dp_lift);
    memset(dp_right, 0, sizeof dp_right);
    for(int i = 0; i < maxn; i++) {
        gl[i].clear();
        gr[i].clear();
    }
}

int main() {
    while(scanf("%d %d", &n, &k) != EOF) {
        init();
        ll sum = 0;
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            sum += a[i];
        }
        sort(a + 1, a + n + 1);
        int t1 = 1, id = 0, tal = 0;
        for(int i = 1; i <= n; i++) {
            if(a[i] < k) id = i;
        }
        if(id) {
            dp_lift[0][0] = dp_lift[0][a[1]] = 1;
            dp_right[0][0] = dp_right[0][a[id]] = 1;
            gl[0].push_back(0);
            gr[id + 1].push_back(0);
            gl[1].push_back(0);
            gl[1].push_back(a[1]);
            gr[id].push_back(0);
            gr[id].push_back(a[id]);
            for(int i = 2; i <= id; i++) {
                for(int j = 0; j <= k; j++) {
                    int il = i, ir = id - i + 1;
                    ///左边dp
                    int j1 = j - a[il];
                    if(dp_lift[t1 ^ 1][j] || (j1 >= 0 && dp_lift[t1 ^ 1][j1])) {
                        dp_lift[t1][j] = 1;
                        gl[il].push_back(j);
                    }
                    ///右边dp
                    int j2 = j - a[ir];
                    if(dp_right[t1 ^ 1][j] || (j2 >= 0 && dp_right[t1 ^ 1][j2])) {
                        dp_right[t1][j] = 1;
                        gr[ir].push_back(j);
                    }
                }
                t1 ^= 1;
            }
            for(int i = 1; i <= id; i++) {
                for(int j = 0; j < gl[i - 1].size(); j++) {
                    int lift_sum = gl[i - 1][j];
                    int min_sum = k - a[i] - lift_sum;
                    int idx = lower_bound(gr[i + 1].begin(), gr[i + 1].end(), min_sum) - gr[i + 1].begin();
                    if(idx >= gr[i + 1].size()) continue;
                    int new_sum = lift_sum + gr[i + 1][idx];
                    if(new_sum >= k - a[i] && new_sum < k) { tal++; break; }
                }
            }
            tal = id - tal;
        }
        if(sum < k) tal = n;
        printf("%d\n", tal);
    }
    return 0;
}


Measured memory_bytes: 189184
#include <iostream>
#include <vector>
#include <stack>

constexpr int MAX = (1 << 9);
constexpr int MAX_W = 100010;

void chmax (int &a, int b){
	if (a < b) a = b;
}

int main() {
	int n;
	std::cin >> n;
	std::vector<int> value(n + 1), weight(n + 1);
	for (int i = 1; i <= n; i++) {
		std::cin >> value[i] >> weight[i];
	}
	
	std::vector<std::vector<int>> dp(MAX, std::vector<int>(MAX_W, 0));
	std::stack<int> sta;
	sta.push(1);
	while (not sta.empty()) {
		int v = sta.top(); sta.pop();
		dp[v] = dp[v >> 1];
		for (int sum_w = MAX_W; sum_w-->0;) {
			if (sum_w + weight[v] < MAX_W) {
				chmax(dp[v][sum_w + weight[v]], dp[v][sum_w] + value[v]);
			}
		}
		
		for (int u : {(v << 1 | 0), (v << 1 | 1)}) {
			if (u <= n and u < MAX) sta.push(u);
		}
	}
	
	int q;
	std::cin >> q;
	while (q--) {
		int v, max_w;
		std::cin >> v >> max_w;
		if (v < MAX) {
			std::cout << dp[v][max_w] << '\n';
			continue;
		}
		
		std::vector<int> ver;
		for (; v >= MAX; v >>= 1) {
			ver.push_back(v);
		}
		
		const int m = int(ver.size());
		int ans = 0;
		
		for (int s = 0; s < (1 << m); s++) {
			int sv = 0, sw = 0;
			for (int i = 0; i < m; i++) {
				if (s & (1 << i)) {
					sv += value[ver[i]];
					sw += weight[ver[i]];
				}
			}
			
			if (sw <= max_w) {
				chmax(ans, sv + dp[v][max_w - sw]);
			}
		}
		
		std::cout << ans << '\n';
	}

}

Measured memory_bytes: 206544
#include "bits/stdc++.h"

using namespace std;

#define int long long
#define ll long long
typedef pair<int, int> P;
#define mod 1000000007
#define INF (1LL<<60)

#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define YES puts("YES\n")
#define Yes puts("Yes\n")
#define NO  puts("NO\n")
#define No  puts("No\n")
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }





signed main() {

	int N;
	cin >> N;

	vector<int> x(N), y(N);
	rep(i, N) cin >> x[i] >> y[i];

	double ans = 0.0;

	for (double rad = 0.0; rad < 2*3.14159265; rad += 3.141592/100000) {
		int xx = 0.0, yy = 0.0;
		rep(i, N) {
			if (cos(rad) * x[i] + sin(rad) * y[i] > 0) {
				xx += x[i];
				yy += y[i];
			}
		}
		ans = max(ans, sqrt(xx * xx + yy * yy));
	}
	cout << fixed << setprecision(15) << ans << endl;
	


	return 0;
}


Measured memory_bytes: 512
package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"strconv"
)

func main() {
	log.SetFlags(log.Lshortfile)
	nextReader = NewScanner()
	n := nextInt()
	a := nextInts(n)
	ans := 0
	for i := 1; i < n; i++ {
		if a[i] == a[i-1] {
			a[i] = -1
			ans++
		}
	}
	fmt.Println(ans)
}
func max(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r < a[i] {
			r = a[i]
		}
	}
	return r
}
func min(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r > a[i] {
			r = a[i]
		}
	}
	return r
}
func sum(a []int) (r int) {
	for i := range a {
		r += a[i]
	}
	return r
}
func minmax(a, b int) (int, int) {
	if a > b {
		return b, a
	}
	return a, b
}
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

type Pair struct {
	a, b int
}

type Pairs []Pair

func (p Pairs) Len() int {
	return len(p)
}
func (p Pairs) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

func (p Pairs) Less(i, j int) bool {
	return p[i].b < p[i].b
}

var nextReader func() string

func NewScanner() func() string {
	r := bufio.NewScanner(os.Stdin)
	r.Buffer(make([]byte, 1024), int(1e+11))
	r.Split(bufio.ScanWords)
	return func() string {
		r.Scan()
		return r.Text()
	}
}
func nextString() string {
	return nextReader()
}
func nextInt64() int64 {
	v, _ := strconv.ParseInt(nextReader(), 10, 64)
	return v
}
func nextInt() int {
	v, _ := strconv.Atoi(nextReader())
	return v
}
func nextInts(n int) []int {
	r := make([]int, n)
	for i := 0; i < n; i++ {
		r[i] = nextInt()
	}
	return r
}
func nextFloat64() float64 {
	f, _ := strconv.ParseFloat(nextReader(), 64)
	return f
}


Measured memory_bytes: 640
#include<iostream>
#include<algorithm> 
#include<cstdio>
#include<cctype>
#include<string>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const int N = 1e6+5;
const double pi = acos(-1.0);
const ll mod = 1e9+7;
int n,m,q;
int mx = -1;

struct Score {
    int a,b,c,d;
} arr[100];

void dfs(int k,int d,vector<int> A) {
    if (d == n) {
        int s = 0;
        for (int j = 0;j < q;j ++) {
            if (A[arr[j].b - 1] - A[arr[j].a - 1] == arr[j].c) {
                s += arr[j].d;
            }
        }
        mx = max(mx,s);
        return;
    }
    for (int i = k;i <= m;i ++) {
        A.push_back(i);
        dfs(i,d + 1,A);
        A.erase(A.end() - 1);
    }
}

int main()
{
    scanf("%d%d%d",&n,&m,&q);
    
    int a,b,c,d;
    for (int i = 0;i < q;i ++) {
        scanf("%d%d%d%d",&a,&b,&c,&d);
        arr[i].a = a;
        arr[i].b = b;
        arr[i].c = c;
        arr[i].d = d;
    }
    dfs(1,0,vector<int>());
    printf("%d\n",mx);
    return 0;
}


Measured memory_bytes: 3756
def SelectionSort(A):
    count = 0
    for i in range(len(A)):
        minj = i
        for j in range(i, len(A)):
            if A[j] < A[minj]:
                minj = j
        if i != minj:
            A[i], A[minj] = A[minj], A[i]
            count += 1
    print(" ".join(map(str, A)))
    print(count)


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

SelectionSort(A)

Measured memory_bytes: 7788