akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp> //required

using namespace __gnu_pbds; 
using namespace std;

template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; 
typedef long long  ll;
typedef vector<ll> VL;
typedef vector<int> VI;
typedef pair<ll,ll> PLL;
typedef pair<int,int> PII;
#define pb push_back

#define F first
#define S second
#define SZ(a) int((a).size())
#define ALL(a) a.begin(),a.end()
#define fr(i,x,y) for(int i=x;i<y;i++)
#define frr(i,x,y) for(int i=x-1;i>=y;i--)
#define inf 1e18+1
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const int mod=1000000007;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
ll power(ll a,ll b){ll ans=1;while(b!=0){if(b&1){ans*=a;}a=a*a;b>>=1;}return ans;}
ll powerm(ll a,ll b){ll ans=1;while(b!=0){if(b&1){ans*=a;ans%=mod;}a=a*a;a%=mod;b>>=1;}return ans%mod;}
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();

void solve()
{
    ll n;
    cin>>n;
    ll x=n/1000;
    if(n%1000!=0)
    {
        x++;
    }
    x*=1000;
    x-=n;
    cout<<x<<endl;
    return;
}
int main()
{
    IOS;
    ll t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}
/* stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
*/
// BITMASK:
// 1)When some constrall is of the order of 15-20, think of bitmask DP.
// 2)When some constrall is around 40, try out meet in the middle
// 3) See Strings,palindromes,prefix,suffix etc -> KMP,Z algorithm

Measured memory_bytes: 3688
#include<stdio.h>
#include<string.h>
#include<ctype.h>

int main(){

	int i;
	char pro[85];

	while(scanf("%84[^\n]\*[^\n]",pro)!=EOF){
		getchar();
		while(1){
			if(strstr(pro,"the")!=NULL || strstr(pro,"that")!=NULL || strstr(pro,"this")!=NULL) break;
			for(i=0;pro[i]!='\0';i++){
				if(islower(pro[i]))
					pro[i]=(pro[i]-'a'+1)%26+'a';
			}
		}
		printf("%s\n",pro);
	}

	return 0;
}

Measured memory_bytes: 536
import java.util.*;
import java.util.stream.Collectors;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class Main {
	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		int[] x = new int[5];
		String[] tokens = in.readLine().split(" ");
		for (int i = 0; i < 5; ++i) {
			x[i] = Integer.parseInt(tokens[i]);
		}
		for (int i = 0; i < 5; ++i) {
			if (x[i] == 0) {
				System.out.println((i + 1));
				break;
			}
		}

	}

	void calc() {
	}

	void showResult() {
	}
}

Measured memory_bytes: 32428
#include <stdio.h>
int main(void){
    
    int a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    int m=0;
    for(int i=a;i<=b;i++)
    {   
        if(i%c==0)
        m++;
        
    }
    printf("%d",m);
    return 0;
}


Measured memory_bytes: 1720
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

n,k = map(int,readline().split())
data = [i.split() for i in readlines()]

cumsum_w = [[0] * (k+1) for _ in range(k+1)]
cumsum_b = [[0] * (k+1) for _ in range(k+1)]
for x,y,c in data:
    x = int(x)
    y = int(y)
    c = 0 if (c=='W') else 1
    c += x//k + y//k
    c %= 2
    x %= k
    y %= k
    if(c==0):
        cumsum_w[x][y] += 1
    else:
        cumsum_b[x][y] += 1

for i in range(k):
    for j in range(1,k):
        cumsum_w[i][j] += cumsum_w[i][j-1]
        cumsum_b[i][j] += cumsum_b[i][j-1]

for i in range(1,k):
    for j in range(k):
        cumsum_w[i][j] += cumsum_w[i-1][j]
        cumsum_b[i][j] += cumsum_b[i-1][j]

ans = 0
for x in range(k):
    for y in range(k):
        tmp = (cumsum_w[k-1][k-1]
                - cumsum_w[x-1][k-1]
                - cumsum_w[k-1][y-1]
                + 2 * cumsum_w[x-1][y-1]
                + cumsum_b[x-1][k-1]
                + cumsum_b[k-1][y-1]
                - 2 * cumsum_b[x-1][y-1])
        ans = max(ans,tmp)

        tmp = (cumsum_b[k-1][k-1]
                - cumsum_b[x-1][k-1]
                - cumsum_b[k-1][y-1]
                + 2 * cumsum_b[x-1][y-1]
                + cumsum_w[x-1][k-1]
                + cumsum_w[k-1][y-1]
                - 2 * cumsum_w[x-1][y-1])
        ans = max(ans,tmp)

print(ans)

Measured memory_bytes: 123000
#include <bits/stdc++.h>
#define rep(i,j) for (int (i)=0;(i)<(int)(j);++(i))
#define put(i) cout << (i) << endl
using namespace std;
typedef long long ll;

int main() {
    vector<int> a(3);
    rep(i,3) cin >> a[i];
    sort(a.begin(),a.end());
    int b[] = {5,5,7};
    rep(i,3) {
        if (a[i] != b[i]) {
            cout << "NO" << endl;
            return 0;
        }
    }
    cout << "YES" << endl;
}


Measured memory_bytes: 764
#include <stdio.h>

int main () {
  int i,j,n,minj,N,tmp,count=0;

  scanf("%d",&N);

  int A[N];
  
   for(i=0;i<N;i++) {
    scanf("%d",&A[i]);
  }

  for(i=0;i<=N-1;i++) {
    minj =i;
    for(j=i;j<=N-1;j++) {
      if(A[j] < A[minj]) minj = j;
    }
    if(minj!=i) {
    tmp = A[i];
    A[i] = A[minj];
    A[minj] = tmp;
    count++;
    }
  }

  for(i=0;i<N;i++) {
    if(i==0) printf("%d",A[i]);

    else printf(" %d",A[i]);
    if(i==N-1) printf("\n");
  }
  printf("%d\n",count);

  return 0;
  }



Measured memory_bytes: 2100
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define All(V) v.begin(), v.end()
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1e9+7, INF = 1e9;

int main() 
{
  int n,k,res;
    cin >> n >> k;
    int r = min(n,k);
    vector<int> v(n);
    rep(i,n)cin >> v[i];
    int ans = 0;
    rep(a,r+1)rep(b,r+1){
        if(a+b>r)break;
        res = 0;
        vector<int> minus;
        rep(i,a){
            res += v[i];
            if(v[i]<0)minus.push_back(v[i]);
        }
        rep(i,b){
            res += v[n-1-i];
            if(v[n-1-i] < 0)minus.push_back(v[n-1-i]);
        }
        sort(minus.begin(),minus.end());
        rep(i,min((int)minus.size(),k-(a+b)))res -= minus[i];
        ans = max(ans,res);
        
    }
    cout << ans << endl;
  system("pause");
}

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

int main(){
	int n,d,x; cin>>n>>d>>x;
	int ans=0;
	rep(i,n){
		int a; cin>>a;
		int j=1;
		while(1){
			if(a*j+1>d) break;
			ans++;
			j++;
		}
	}
	cout<<ans+n+x<<endl;
}

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

typedef long long ll;
const ll INF = 1e18;
const int BIG = 1e9 + 555;
const int maxN = 1000 + 9;

int n;
int a[maxN][maxN], idx[maxN];
bool mrk[maxN];

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

	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n - 1; j++){
			cin >> a[i][j];
		}
		idx[i] = 1;
	}
	
	vector<int> que[2];
	int cur = 0;
	
	fill_n(mrk + 1, n, false);
	for(int i = 1; i <= n; i++){
		int j = a[i][idx[i]];
		if(mrk[i] || mrk[j]){
			continue;
		}

		if(a[j][idx[j]] == i){
			//cerr << "1 " << i << ' ' << j << '\n';
			que[cur].push_back(i);	que[cur].push_back(j);
			idx[i]++;	idx[j]++;
			mrk[i] = mrk[j] = true;
		}
	}
	
	int ans = 0;
	while(!que[cur].empty()){
		que[1 - cur].clear();
		ans++;

		fill_n(mrk + 1, n, false);
		for(int i = 0, L = que[cur].size(); i < L; i++){
			int x = que[cur][i];
			if(idx[x] > n - 1){
				continue;
			}

			int y = a[x][idx[x]];

			if(mrk[x] || mrk[y]){
				continue;
			}

			if(a[y][idx[y]] == x){
				//cerr << ans << ' ' << x << ' ' << y << '\n';
				que[1 - cur].push_back(x);	que[1 - cur].push_back(y);
				idx[x]++;	idx[y]++;
				mrk[x] = mrk[y] = true;
			}
		}

		cur = 1 - cur;
	}
	
	for(int i = 1; i <= n; i++){
		if(idx[i] <= n - 1){
			cout << "-1\n";
			return 0;
		}
	}
	
	cout << ans << '\n';

	return 0;
}


Measured memory_bytes: 4224
import qualified Data.Vector.Unboxed as V
import qualified Data.Vector.Unboxed.Mutable as MV
import qualified Data.ByteString.Char8 as BS
import Data.Maybe

main :: IO ()
main = do
  [n, m] <- map read . words <$> getLine
  q <- V.replicateM m $ do
    [x, y] <- map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine
    return (x-1, y-1)
  let
    ans = V.create $ do
      vn <- MV.replicate n (1 :: Int)
      vb <- MV.replicate n False
      MV.write vb 0 True
      V.forM_ q $ \(x, y) -> do
        red <- MV.read vb x
        MV.modify vn (+1) y
        MV.modify vb (\b -> red || b) y
        MV.modify vn (subtract 1) x
        l <- MV.read vn x
        MV.modify vb (\b -> (l /= 0) && b) x
      return vb
  print $ V.length $ V.filter id ans


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

using namespace std;
using ll = long long;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main() {
  int a, b;
  cin >> a >> b;
  if (a == b) {
    puts("0");
    return 0;
  }

  int k = (a + b) / 2;
  if (abs(a - k) == abs(b - k))
    cout << k << endl;
  else
    puts("IMPOSSIBLE");

  return 0;
}


Measured memory_bytes: 256
use std::io::Read;


#[derive(Copy, Clone)]
struct Edge{
    from: usize,
    to: usize,
    weight: i64,
}

fn main() {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    let mut iter = buf.split_whitespace();
    
    let n: usize = iter.next().unwrap().parse().unwrap();
    let m: usize = iter.next().unwrap().parse().unwrap();
    
    let edges: Vec<(usize, usize)> = (0..m)
        .map(|_| {
            (
                iter.next().unwrap().parse::<usize>().unwrap() - 1,
                iter.next().unwrap().parse::<usize>().unwrap() - 1,
            )
        })
        .collect();


    // println!("{} {}",n, m);


    let mut graph: Vec<Vec<Edge>> = (0..n)
        .map(|_| {
            Vec::new()
        }).collect();

    for e in edges.iter() {
        graph[e.0].push( Edge{from:e.0, to:e.1, weight:1});
        graph[e.1].push( Edge{from:e.1, to:e.0, weight:1})
    }

    
    let mut stack: Vec<Edge> = Vec::new();
    stack.push(Edge{from:0, to:0, weight:0});

    let mut visited: Vec<bool> = vec![false; n];
    let mut color: Vec<i32> = vec![-1; n];
    color[0] = 2;

    let mut is_partite : bool = true;
    while let Some(e) = stack.pop()  {
        // println!("{} {} {}",e?.from, e.to, e.weight);
        if visited[e.to] {
            continue;
        }
        visited[e.to] = true;
        color[e.to] = match color[e.from] {
            1 => 2,
            2 => 1,
            _ => 0,
        };
        // println!("edge:{} color:{}", e.to, color[e.to]);

        for i in graph[e.to].iter() {
            if ! visited[i.to] {
                stack.push(*i);
            } else {
                if color[i.from] == color[i.to] {
                    is_partite = false;
                }
            }
        }
    }
    
    let whites = color.iter().filter(|&x| *x == 1).collect::<Vec<&i32>>().len();
    if is_partite {
        println!("{}",  whites * (n-whites) - m);
    } else {
        println!("{}", n*(n-1)/2 - m);
    }

}


Measured memory_bytes: 26876
#include <stdio.h>

int main(void){
    int a,b,c,d,pos;
    scanf("%d %d %d %d",&a,&b,&c,&d);
    if(c>a){
        pos = a;
        a = c;
        c = pos;
    }
    if(a-c<=d){
        printf("Yes");
    }else if(a>b){
        if(a-b<=d && b-c<=d){
            printf("Yes");
        }else{
            printf("No");
        }
    }else{
        printf("No");
    }
    return 0;
}


Measured memory_bytes: 128
def main():
    n = int(input())
    a = list(map(int, input().split()))

    dif_max = -1
    for i in range(n):
        for j in range(n):
            if abs(a[i]-a[j]) > dif_max and i != j:
                dif_max = abs(a[i]-a[j])

    print(dif_max)


if __name__ == '__main__':
    main()


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

int main() {
  string s, out="";
  cin >> s;
  for (int i=0; i<s.size(); i++){
    if (s.at(i) == '0') out.push_back('0');
    else if (s.at(i) == '1') out.push_back('1');
    else if (out.size()>0) out.pop_back();
  }
  cout << out << endl;
}

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

using namespace std;
using ll = long long;
using vec = vector<int>;

#define rep(i, n)  for(int (i) = 0; (i) < (n); ++(i))
#define rrep(i, n) for(int (i) = (n); (i) >= 0; --(i))
#define Sort(a)    sort(a.begin(), a.end())
#define gSort(a)   sort(a.begin(), a.end(), greater<int>())
#define pout(n, a) cout << fixed << setprecision(n) << (a)
#define Cast(dig, n) static_cast<std::bitset<(dig)>>(n)

const int INF = 2147483647;
const ll  MOD = 1000000007;

int main() {
    int r; cin >> r;

    pout(10, (2*r)*3.14);
}

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

int main()
{
	bool flag = true;
	string S;
	cin >> S;

	if ( S[0] != 'A' ) flag = false;

	int cntc = 0;
	for ( int i = 2; i < S.size() - 1; i++ ) {
		if ( S[i] == 'C' ) cntc++;
	}

	if ( cntc != 1 ) flag = false;

	for ( int i = 0; i < S.size(); i++ ) {
		if ( isupper(S[i]) && (S[i] != 'A' &&  S[i] != 'C' ) ) {
			flag = false;
		}
	}

	if ( flag ) {
		cout << "AC" << endl;
	} else {
		cout << "WA" << endl;
	}
	return ( 0 );
}


Measured memory_bytes: 256
#include<iostream>
#include<stack>
using namespace std;
#define N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n,M[N][N];
int color[N],d[N],f[N],tt;
int nt[N];

int next(int u)
{
    for(int v=nt[u];v<n;v++)
    {
        nt[u]=v+1;
        if(M[u][v])return v;
    }
    return -1;
}

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

    stack<int> s;
    s.push(r);
    color[r]=GRAY;
    d[r]=++tt;

    while(!s.empty())
    {
        int u=s.top();
        //cout << "s.top=" << u << endl;
        int v=next(u);
        //cout << "u.next=" << v << endl;
        if(v!=-1)
        {
            if(color[v]==WHITE)
            {
                color[v]=GRAY;
                d[v]=++tt;
                s.push(v);
            }
        }
        else
        {
            s.pop();
            color[u]=BLACK;
            f[u]=++tt;
        }
    }
}

void dfs()
{
    for(int i=0;i<n;i++)
    {
        color[i]=WHITE;
        nt[i]=0;
    }


    tt=0;

    for(int u=0;u<n;u++)
    {
        if(color[u]==WHITE)
        {
            //cout << "visit " << u << endl;
            dfs_visit(u);
        }
    }

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

int main()
{
    int u,k,v,node;
    cin >> n;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            M[i][j]=0;
        }
    }

    for(int i=0;i<n;i++)
    {
        cin >> u;//row
        if(i==u-1)
        {
            cin >> node;//number of node
            for(int j=0;j<node;j++)
            {
                cin >> v;
                M[i][v-1]=1;
            }
        }
    }

    /*
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cout << M[i][j] << " " ;
        }
        cout << endl;
    }*/

    dfs();
    return 0;
}



Measured memory_bytes: 3120
#include <iostream>
#include <cstring>
#include <map>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <set>
#include <vector>
#include <queue>
#include <list>
#include <numeric>

using namespace std;
typedef long long ll;

int main(){
  int a[3];
  for(int i = 0; i<3; i++){
    cin>>a[i];
  }
  sort(a,a+3);
  cout<<abs(a[2]-a[1])+abs(a[1]-a[0])<<endl;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

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

		int n = sc.nextInt();
		int m = sc.nextInt();
		int l = sc.nextInt();
		long cal = 0;
		long[][] mt1 = new long[n][m];
		long[][] mt2 = new long[m][l];
		long[][] mt3 = new long[n][l];

		for(int i = 0; i < n; i++) {
			for(int j = 0; j < m; j++) {
				mt1[i][j] = sc.nextInt();
			}
		}

		for(int i = 0; i < m; i++) {
			for(int j = 0; j < l; j++) {
				mt2[i][j] = sc.nextInt();
			}
		}

		for(int i = 0; i < n; i ++) {
			for(int j = 0; j < l; j++) {
				for(int k = 0; k < m ; k++) {
					cal += mt1[i][k] * mt2[k][j];
				}
				mt3[i][j] = cal;
				cal = 0;
			}
		}

		 for(int i = 0; i < n ; i++) {
			 for(int j = 0; j < l; j++) {
				 if(j != l-1) {
					 System.out.print(mt3[i][j] + " ");
				 }else {
					 System.out.println(mt3[i][j]);
				 }
			 }
		 }
	}
}



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

/* 按顺序输出数组元素 */
void trace(int A[], int N) {
	for ( int i = 0; i < N; i++ ) {
		if ( i > 0 ) cout << " ";
		cout << A[i];
	}
	cout << endl;
}

/* 插入排序 */
void insertionSort(int A[], int N) {
	for ( int i = 1; i < N; i++ ) {
		int v = A[i];
		int j = i - 1;
		while ( j >= 0 && A[j] > v ) {
			A[j + 1] = A[j];
			j--;
		}
		A[j + 1] = v;
		trace(A, N);
	}
}

int main() {
	int N, A[100];
	
	cin >> N;
	for ( int i = 0; i < N; i++) cin >> A[i];
	trace(A, N);
	insertionSort(A, N);

	return 0;
}


Measured memory_bytes: 3080
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define TEST cout << "PASS" << endl;
#define all(x) (x).begin(),(x).end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9 + 1e6 + 1e3 + 1;
const ll INFL = 1e15 + 1e12 + 1e9 + 1e6 + 1e3 + 1;
const ll MOD = 1e9 + 7;
const double PI = 3.141592653589793;

int main(){
    int n;
    cin >> n;
    vector<int> v[100100];
    rep(i, n-1){
        int a, b;
        cin >> a >> b;
        a--; b--;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    int d[100100];
    d[0] = 0;
    fill(d, d + 100100, -1);
    queue<int> q;
    q.push(0);
    while(!q.empty()){
        int current_place = q.front();
        q.pop();
        for(int x : v[current_place]){
            if(d[x] >= 0) continue;
            d[x] = d[current_place] + 1;
            q.push(x);
        }
    }
    int color[100100];
    fill(color, color + 100100, -1);
    color[0] = 0;
    color[n-1] = 1;
    vector<int> path;
    path.push_back(n-1);
    while(!q.empty()) q.pop();
    q.push(n-1);
    
    //経路復元
    while(!q.empty()){
        int current_place = q.front();
        if(current_place == 0) break;
        q.pop();
        for(int x : v[current_place]){
            if(d[x] == d[current_place] - 1){
                q.push(x);
                path.push_back(x);
                break;
            }
        }
    }
    path.push_back(0);
    reverse(all(path));

    for(size_t i = 0; i < (path.size() + 1)/2; i++){
        color[path[i]] = 0;
    }
    for(size_t i = (path.size() + 1)/2; i < path.size(); i++){
        color[path[i]] = 1;
    }

    //着色
    int cnt_b = 1, cnt_w;
    while(!q.empty()) q.pop();
    q.push(0);
    bool used[100000];
    fill(used, used + 100000, false);
    used[0] = true;
    while(!q.empty()){
        int current_place = q.front();
        q.pop();
        for(int x : v[current_place]){
            if(color[x] == 1 || used[x]) continue;
            cnt_b++;
            color[x] = 0;
            q.push(x);
            used[x] = true;
        }
    }
    rep(i, n) if(color[i] == -1) color[i] = 1;
    cnt_w = n - cnt_b;
    if(cnt_w >= cnt_b) cout << "Snuke" << endl;
    else cout << "Fennec" << endl;
    //rep(i, n) printf("%d : %d\n", i+1, color[i]);
}

Measured memory_bytes: 10088
/*COMPETITIVE PROGRAMMING C++ TEMPLATE */

#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <cmath>
#include <iostream>
#include <stack>
#include <queue>
#include <climits>
#include <iomanip>
#include <iterator>

#define along long(x) x.begin(), x.end()
#define MOD 998244353
const unsigned long long M = 1000000007;
const long double PI = 3.141592653589793236L;

typedef long long ll;
typedef long double ld;
#define MAX 50
using namespace std;
void solve(){
    int n;
    cin>>n;
    int res=n%1000;
    int ans=0;
    if(res==0)ans=0;
    else ans=1000-res;
    cout<<ans;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
   
   int t=1;
   //cin>>t;
   while(t--){
       solve();
   }
}

Measured memory_bytes: 3612
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using str = string;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
const double PI = 3.141592653589793238;
const ll mod = 1000000007;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define REP(i, m, n) for (int i = m; i < (int)n; i++)
#define all(a) (a).begin(), (a).end()
#define allg(a) (a).begin(), (a).end(), greator<>()
#define d20 std::setprecision(20)
#define veci vector<int>
#define vecll vector<long long int>
#define vecb vector<bool>
#define vecd vector<double>
template <typename T>
T lcm(T a, T b)
{
	return a / __gcd(a, b) * b;
}

int main()
{
	int n;
	cin >> n;
	vecll a(n);
	cin >> a[0];
	ll sum_xor = a[0];
	for (int i = 1; i < n; i++)
	{
		cin >> a[i];
		sum_xor ^= a[i];
	}
	for (size_t i = 0; i < n - 1; i++)
	{
		ll tmp = sum_xor ^ a[i];
		cout << tmp << " ";
	}
	ll tmp = sum_xor ^ a[n - 1];
	cout << tmp << endl;

	return 0;
}

Measured memory_bytes: 4824
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

int main(){
	int n,m,c;
	cin>>n>>m>>c;
	int a[m];
	rep(i,m)cin>>a[i];
	int cnt=0;
	rep(j,n){
		int b[m];
		rep(i,m)cin>>b[i];
		int ret=0;
		rep(i,m)ret+=a[i]*b[i];
		if(ret+c>0)++cnt;
	}
	cout<<cnt<<endl;
	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define int long long
#define ld long double
#define ri register int
#define il inline
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define all(x) x.begin(),x.end() 
#define rep(i,a,b) for(ri i=(a);i<=(b);++i)
#define per(i,a,b) for(ri i=(a);i>=(b);--i)
#define ls (x<<1)
#define rs (x<<1|1)
#define lb() i&-i
#define mid (l+r>>1)
#define db(x) cout << "#x:" << x << endl
#define dd() cout << "Out put!\n"
const int mod=1e9+7,eps=1e-6,pi=acos(-1.0),maxn=100010;
const int inf=0x7f7f7f7f7f7f7f7f;
using namespace std;
int v[maxn];
int n; 
int a[maxn],b[maxn];
signed main()
{
	ios::sync_with_stdio(0);
	int t=1;
//	cin >> t; 
	while(t--)
	{
		int n;
		cin >> n;
		int cnt[30]={0};
		int sum = 0;
		rep(i,1,n)
		{
			cin >> a[i];
			sum^= a[i];
		}
		rep(i,1,n)
			cout << (sum^a[i]) << " ";
	} 
	return 0;
}

Measured memory_bytes: 5208
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
	long long int a, b, c, d;
	cin >> a >> b >> c >> d;
	if (b < d){
		if (b < c){
			cout << 0 << endl;
			return 0;
		}
		else{
			if (a < c){
				cout << b - c << endl;
				return 0;
			}
			else{
				cout << b - a << endl;
				return 0;
			}
		}
	}
	else{
		if (c >= a){
			cout << d - c << endl;
			return 0;
		}
		else{
			if (d >= a){
				cout << d - a << endl;
				return 0;
			}
			else{
				cout << 0 << endl;
				return 0;
			}
		}
	}
}

Measured memory_bytes: 256
object Main extends App{
    val n = io.StdIn.readInt
    val a = io.StdIn.readLine.split(" ").map(_.toLong)
    
    val upper  = BigDecimal(10).pow(18)
	val a_bd = a.map(BigDecimal(_))
  	val output = a_bd.reduce(_ * _)
  	
  	if(a.contains(0)) println(0)
    else{
      if (upper < output) println(-1)
  	  else println(output)
    }
}


Measured memory_bytes: 77508
# coding: utf-8


def solve(*args: str) -> str:
    n = int(args[0])
    A = list(map(int, args[1].split()))

    rem = sum(A)
    cur = 1
    ret = 0
    for a in A:
        cur = min(rem, cur)
        rem -= a
        ret += cur
        cur = 2*(cur-a)
        if cur < 0:
            ret = -1
            break

    return str(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))


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

#define PI (acos(-1.0))
#define DBG printf("Hi\n")
#define loop(i,n) for(i =1 ; i<=n; i++)
#define mp make_pair
#define pb push_back
#define mod 998244353LL
#define INF 1000000000
#define xx first
#define yy second
#define sq(x) ((x)*(x))
#define eps 0.0000000001
#define i64 long long int
#define ui64 unsigned long long int

using namespace std ;

#define maxn 100005

i64 f[4004][4004] , p[4002] , dp[2002] ;

i64 C( i64 n, i64 r )
{
    if( r < 0 || n<r ) return 0LL ;
    return f[n][r] ;
}

int main()
{
    i64 i ,  j , k , l , m , n ;

    for(i=0 ; i<=4000 ; i++)
    {
        f[i][0] = 1LL ;
        for(j=1 ; j<=i ; j++)
        {
            f[i][j] = ( f[i-1][j-1] + f[i-1][j] )%mod ;
        }
    }

    p[0] = 1LL ;
    for(i=1 ; i<=4000 ; i++) p[i] = (p[i-1]*2LL)%mod ;

    scanf("%lld %lld",&k,&n) ;

    dp[0] = C(n+k-1,n) ;

    for(i=1 ; i<=k ; i++)
    {
        if( i%2==1 )
        {
            for(j=0 ; j<=(i-1)/2 ; j++ )
            {
                dp[i] = ( dp[i] + (C( n+k-i-2 , n-1-j )+C( n+k-i-1 , n-j ))*((p[j]*C((i-1)/2,j))%mod) )%mod ;
            }
        }
        else{
            for(j=0 ; j<=(i/2) ; j++)
            {
                dp[i] = ( dp[i] + C( n+k-i-1 , n-j )*((p[j]*C(i/2,j))%mod) )%mod ;
            }
        }
    }

    for(i=2 ; i<=2*k ; i++)
    {
        printf("%lld\n", dp[ min( i-1,2*k-i+1 ) ] ) ;
    }

    return 0 ;
}


Measured memory_bytes: 123776
def main():
    S = list(str(input()))
    T = list(str(input()))
    q = int(input())
    S_cum = [0]
    T_cum = [0]
    for i in S:
        if i == "A":
            S_cum.append((S_cum[-1]+1)%3)
        else:
            S_cum.append((S_cum[-1]+2)%3)
    for i in T:
        if i == "A":
            T_cum.append((T_cum[-1]+1)%3)
        else:
            T_cum.append((T_cum[-1]+2)%3)
    for i in range(q):
        a,b,c,d = map(int,input().split())
        s_ = S_cum[b]-S_cum[a-1]
        t_ = T_cum[d]-T_cum[c-1]
        if s_%3 == t_%3:
            print("YES")
        else:
            print("NO")

if __name__ == "__main__":
    main()

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

using namespace std;

int main()
{
  while(1){
    string AC;
    cin>>AC;

    if(AC == "#"){
      break;
    }

    int count = 0;
    int i = 0;
    for(char e : AC){
      if(i == 0){
        if(e == 'y' || e == 'u' || e == 'i' || e == 'o' || e == 'p' || e == 'h' || e == 'j' || e == 'k' || e == 'l' || e == 'n' || e == 'm'){
          i = 2;
        }
        else{
          i = 1;
        }
      }
      else if(i == 1){
        if(e == 'y' || e == 'u' || e == 'i' || e == 'o' || e == 'p' || e == 'h' || e == 'j' || e == 'k' || e == 'l' || e == 'n' || e == 'm'){
          i = 2;
          ++count;
        }
        else{
          continue;
        }
      }
      else{
        if(e == 'y' || e == 'u' || e == 'i' || e == 'o' || e == 'p' || e == 'h' || e == 'j' || e == 'k' || e == 'l' || e == 'n' || e == 'm'){
          continue;
        }
        else{
          i = 1;
          ++count;
        }
      }
    }

    cout<<count<<endl;
  }

  return 0;
}


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

def count_loop(start, idx_map):
    count = 0
    idx = start
    while True:
        idx = idx_map[idx]
        count += 1
        if idx == 0:
            break
    return count

A, B = zip(*sorted(zip(A, B), key=lambda x: x[1]))
idx_map = sorted(range(N), key=lambda i: A[i])
A = sorted(A)

check_1 = all(A[i] <= B[i] for i in range(N))
check_2 = any(A[i + 1] <= B[i] for i in range(N - 1))
check_3 = count_loop(0, idx_map) <= N - 2
print("Yes" if (check_1 and (check_2 or check_3)) else "No")


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

int main() {
    int n;
    int i = 1;
    cin >> n;
    while (i <= n) {
        int x = i;
        if (x % 3 == 0) {
            cout << " " << i;
        } else {
            while (x > 0) {
                if (x % 10 == 3) {
                    cout << " " << i;
                    break;
                }
                x /= 10;
            }
        }
        i++;
    }
    cout << endl;
}

Measured memory_bytes: 3096
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <set>
#include <cmath>
using namespace std;

void solve(){
  int a, b; cin >> a >> b;
  if(a % 3 == 0 || b % 3 == 0 || (a + b) % 3 == 0) cout << "Possible\n";
  else cout << "Impossible\n";
  return;
}

int main(){
  solve();
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <cstring>
#include <cmath>

using namespace std;

int main()
{
    int w, n, a, b, i, j, len, arr[32], temp;
    char pair[10];

    cin>>w>>n;

    for(i=1; i<=w; i++)
        arr[i] = i;

    for(i=1; i<=n; i++)
    {
        cin>>pair;

        a = 0;
        b = 0;

        len = strlen(pair);

        for(j=len-1; pair[j]!=','; j--)
        {
            b = b+ (pair[j] - '0') * pow(10, len-1-j);
        }

        len = j;

        for(j=len-1; j>=0; j--)
        {
            a = a+ (pair[j] - '0') * pow(10, len-1-j);
        }

        temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }

    for(i=1; i<=w; i++)
        cout<<arr[i]<<endl;

    return 0;
}

Measured memory_bytes: 1188
#include<iostream>
using namespace std;
int main(){
    int a , b , c , d;
    cin >> a >> b >> c;
    if (a > b){
        d = a;
        a = b;
        b = d;

    }
    if (b > c){
        d = b;
        b = c;
        c = d;

    }
    if (a > b){
        d = a;
        a = b;
        b = d;
    }
    cout << a << " " << b << " " << c << endl;
    return 0;

}


Measured memory_bytes: 3076
info = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
num = -1
for i in range(info[2]):
    temp = [int(i) for i in input().split()]
    price = a[temp[0]-1]+b[temp[1]-1]-temp[2]
    if (num == -1 or num > price):
        num = price
if min(a)+min(b) < num:
    num = min(a)+min(b)
print(num)


Measured memory_bytes: 18488
#include<bits/stdc++.h>
#define dbug printf("I am here\n");
#define Fast ios_base::sync_with_stdio(false); cin.tie(0);
#define vs              v.size()
#define sot(v)        sort(v.begin(),v.end())
#define rev(v)        reverse(v.begin(),v.end())
#define ii                pair<int,int>
#define ll                long long
#define ull              unsigned long long
#define pb              push_back
#define mpp          make_pair
#define Okay         0
#define pi               3.14159

static ll int inf = 1e9;
const int cont = 10e5+10;
using namespace std;

void solve(){
   ll a, b,c,d,n,k;
  cin>>n;
  vector<ll>v(n);
  ll sum = 0,last = 0;
  for(int i=0; i<n; i++){
    cin>>v[i];
    if(i>0){
       if(v[i] < v[i-1]){
        last = v[i-1] - v[i];
        sum += last;
        v[i] += last;
        last = 0;
            }
        }
  }
  //for(int i=0;i<n; i++)cout<<v[i]<<" ";
  cout<<sum<<endl;


}

int main(){
    Fast;

    solve();

	return Okay;
}



Measured memory_bytes: 4764
#include<cstring>
#include<cstdio>
#define ll long long
using namespace std;
int n,m;
int a[1000010][26],t[26];
char s[1000010];
int main()
{
//	freopen("ted.in","r",stdin);
	memset(a,-1,sizeof(a));
	memset(t,-1,sizeof(t));
	scanf("%s",s+1);
	n=strlen(s+1);
	for(int i=n;i>=1;i--)t[s[i]-'a']=n+i;
	for(int i=n;i>=1;i--)
	{
		memcpy(a[i],t,sizeof(t));
		t[s[i]-'a']=i;
	}
//	for(int i=1;i<=n;i++,puts(""))
//	    for(int j=0;j<26;j++)printf("%d ",a[i][j]);
	scanf("%s",s+1);
	m=strlen(s+1);
	int pos=t[s[1]-'a'];
	ll ans=0;
	for(int i=2;i<=m;i++)
	{
//		printf("%d\n",pos);
		if(pos==-1)return puts("-1")&0;
		pos=a[pos][s[i]-'a'];
		if(pos>n)ans+=n,pos-=n;
	}
	if(pos==-1)return puts("-1")&0;
	printf("%lld\n",ans+pos);
}

Measured memory_bytes: 101888
def main():
    from collections import deque
    import sys
    input = sys.stdin.readline
    h, w = map(int, input().split())
    ch, cw = map(int, input().split())
    dh, dw = map(int, input().split())
    # 迷路の四方を##で囲む。
    # 移動で枠外にでてしまってindexがないことを阻止するための
    # if文を回避
    maze = ['#' * (w+4)]
    maze.append('#' * (w+4))
    for _ in range(h):
        maze.append('##' + input()[:-1] + '##')
    maze.append('#' * (w+4))
    maze.append('#' * (w+4))
    visited = [[-1] * (w+4) for _ in range(h+4)]
    for i in range(h+4):
        for j in range(w+4):
            if maze[i][j] == '#':
                visited[i][j] = -2
    # この条件ではindexを+1しないとダメ
    ch += 1
    cw += 1
    dh += 1
    dw += 1
    visited[ch][cw] = 0
    walk = [[1, 0], [-1, 0], [0, 1], [0, -1]]
    telepo = [[i,j] for i in range(-2, 3) for j in range(-2, 3)]

    q_walk = deque([[ch, cw]])
    q_telepo = deque([])
    while q_walk:
        yw, xw = q_walk.popleft()
        # 行った場所を管理
        q_telepo.append([yw, xw])
        for i, j in walk:
            ny, nx = yw + i, xw + j
            # if ny < 0 or h <= ny or nx < 0 or w <= nx:
            #     continue
            if visited[ny][nx] == -1:
                # 行ったことあれば0にしていく
                visited[ny][nx] = visited[yw][xw]
                q_walk.append([ny, nx])
        # 歩ける場所なくなったらワープ
        if len(q_walk) == 0:
            while q_telepo:
                yt, xt = q_telepo.popleft()
                for i, j in telepo:
                    my, mx = yt + i, xt + j
                    # if my < 0 or h <= my or mx < 0 or w <= mx:
                    #     continue
                    # 次がまだ行ったことないところだったら、今までの値に+1
                    # つまり、テレポートしないといけない場所
                    if visited[my][mx] == -1:
                        visited[my][mx] = visited[yt][xt] + 1
                        q_walk.append([my, mx])
    print(visited[dh][dw])

if __name__ == '__main__':
    main()


Measured memory_bytes: 23548
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int inf = 1000000000;
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};
const ll mod = 1e9+7;

ll solve(ll a){
    if(a == 0){
        return 1;
    }
    else if(a == 1 || a == 2){
        return 0;
    }
    else if(a >= 3 && a<= 5){
        return 1;
    }
    else{
        ll b=0;
        for(ll i=a-3; i>=0; i--){
            b = (b+solve(i))%mod;
        }
        return b;
    }
}

int main(){
    ll s;
    cin >> s;
    vector<ll> ans(s+10);
    rep(i,s+10){
        ans[i] = 0;
    }
    ans[0] = 1;
    ans[1] = 0;
    ans[2] = 0;
    ans[3] = 1;
    ans[4] = 1;
    ans[5] = 1;
    for(ll i = 6; i<=s; i++){
        for(ll j=i-3; j>=0; j--){
            ans[i] = (ans[i]+ans[j])%mod;
        }
    }
    cout << ans[s] << endl;

}

Measured memory_bytes: 3608
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

cnt = 0
for i in range(N):
    cnt += b[a[i]-1]

for i in range(N-1):
    if a[i]+1 == a[i+1]:
        cnt += c[a[i]-1]

print(cnt)

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

int main(){
  
  int P,Q,R; cin >> P >> Q >> R;
  
  if(P+Q <= Q+R && P+Q <= R+P){
    cout << P+Q << endl;
  }
  else if(Q+R <= P+Q && Q+R <= R+P){
    cout << Q+R << endl;
  }
  else{
    cout << R+P << endl;
  }
  return 0;
}
    

Measured memory_bytes: 256
#include<stdio.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
 
 
using namespace std;
 
//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
 
//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}
 
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
 
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
 
//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
 
//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
 
//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))
 
//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

//6^6 or 9^5 < 100000
int main(void){
  int n;
  int dp[200000];
  
  int cnt =0;
  cin>>n;
  REP(i,200000){
    if(i<=5)dp[i] = i;
    else dp[i] =0;
  }

  for(int i=6; i <= 100000; i++){
    int d=5;
    int e=6;
    for(int k=d; k>=0; k--){
      if(i < pow(9,k))d--;
    }
    for(int k=e; k>=0; k--){
      if(i < pow(6,k))e--;
    }
    dp[i] = min(min(dp[i - (int)pow(9,d)]+1, dp[i - (int)pow(6,e)]+1), i);
    // debug(dp[i]);
  }
  cout<<dp[n]<<endl;
  // for(int i=d; i>=0; i--){
  //   if(n < pow(9,i))d--;
  // }
  // for(int i=e; i>=0; i--){
  //   if(n < pow(6,i))e--;
  // }
  // cout<<d<<e<<endl;
  // while(true){
  //   debug(d);
  //   debug(e);
  //   debug(n);
  //   debug(cnt);
  //   if(n >= pow(9,d) && n >= pow(6,e)){
  //     cout<<n<<"-"<<max(pow(9,d),pow(6,e))<<endl;
  //     n-=max(pow(9,d),pow(6,e));
  //     cnt++;
  //   }
  //   // if(n >= pow(6,e)){
  //   //   cout<<n<<"-"<<pow(6,e)<<endl;
  //   //   n-=pow(6,e);
  //   //   cnt++;
  //   // }
  //   if(n < pow(9,d))d--;
  //   if(n < pow(6,e))e--;
  //   if(n<6){
  //     cout<<n<<"-"<<n<<endl;
  //     cout<<cnt+n<<endl;
  //     return 0;
  //   }
  // }
}


Measured memory_bytes: 1024
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

int main(int argc, char const *argv[]) {
  ll n, k;std::cin >> n >> k;
  vector<bool> x(n, true);

  for(int i=0;i<k;i++){
    ll a;std::cin >> a;
    for(int j=0;j<a;j++){
      ll b;std::cin >> b;
      x[b-1] = false;
    }
  }
  int ans = 0;
  re(i, n) ans += x[i];
  std::cout << ans << '\n';
  return 0;
}


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


void solve(){
    int s;
    cin >> s;
    vector<int> newint;
    int a;
    int count = 0;
    bool find = false;
    a = s;
    newint.emplace_back(s);
    while(!find){
        if(a%2==0) a = a/2;
        else a = 3*a+1;
        int siz = newint.size();
        rep(i,siz){
            if(newint[i]==a) {
                find = true;
                break;
            }
        }
        //if(find) break;
        newint.emplace_back(a);
        count++;
    }
    cout << count+1 << endl;
}

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

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
#include <cstring>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

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

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

    int N, K;
    cin >> N >> K;
    vector<vector<int>> graph(N);
    vector<P> es;
    for(int i = 0; i < N - 1; ++i){
        int A, B;
        cin >> A >> B;
        --A, --B;
        graph[A].emplace_back(B);
        graph[B].emplace_back(A);
        es.emplace_back(A, B);
    }
    vector<int> dist(N);
    auto dfs = [&](auto&& self, int from, int d = 0, int ban = -1, int par = -1) -> void {
        dist[from] = d;
        for(int to : graph[from]){
            if(to == par || to == ban)  continue;
            self(self, to, d + 1, ban, from);
        }
    };
    int ans = INF;
    if(K & 1){
        int bound = (K - 1) / 2;
        for(auto& e : es){
            int u = e.first, v = e.second;
            dfs(dfs, u, 0, v);
            dfs(dfs, v, 0, u);
            int count = 0;
            for(int i = 0; i < N; ++i){
                if(dist[i] > bound) ++count;
            }
            chmin(ans, count);
        }
    }
    else{
        int bound = K / 2;
        for(int v = 0; v < N; ++v){
            dfs(dfs, v);
            int count = 0;
            for(int i = 0; i < N; ++i){
                if(dist[i] > bound) ++count;
            }
            chmin(ans, count);
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 3900
import itertools
N = int(input())
P = list(map(int,input().split()))
P = tuple(P)
Q = list(map(int,input().split()))
Q = tuple(Q)
dic = list(itertools.permutations(range(1,N+1)))
dic.sort()
a = 0
b = 0
for i in range(len(dic)):
    if dic[i] == P:
        a = i
    if dic[i] == Q:
        b = i
print(abs(a-b))

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

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,K;
    cin >> N >> K;
    int p[N];
    rep(i,N) cin >> p[i];
    int sum[N+1];
    sum[0] = 0;
    //    float p_float[N];
    for(int i = 1; i <= N;i++){
        //        cin >> p[i];
        //        p_float[i] = (1.0 + (float)p[i])/2.0;
        sum[i] = sum[i-1] + p[i-1]; //p = {1,2,3} sum = {0,1,3,6}
    }
    int max_val = 0;
    int res = 0;
    for(int i = 0; i <= N - K; i++){
        res = sum[i+K] - sum[i];
        max_val = max(res,max_val);
    }
    double ans = (max_val + K)/2.0;
    printf("%.10f\n",ans);
//    cout << (float)(max_val + K)/2.0 << endl;
}



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

using namespace std;

const int MAXN = 500;

long long arr[MAXN];

long long d[MAXN][MAXN];

long long solve(int l, int r)
{
    if (~d[l][r])
        return d[l][r];
    if (l == r)
        return 0;
    if (l+1 == r)
        return d[l][r] = arr[r]-arr[l-1];
    d[l][r] = 1LL<<60;
    for (int x = l; x < r; x++)
        d[l][r] = min(d[l][r], solve(l, x) + solve(x+1, r) + arr[r]-arr[l-1]);
    return d[l][r];
}


int main()
{
    memset(d, -1, sizeof d);
    int n;
    scanf("%i", &n);
    for (int x = 1; x <= n; x++)
    {
        scanf("%lli", &arr[x]);
        arr[x] += arr[x-1];
    }
    printf("%lli\n", solve(1, n));
}


Measured memory_bytes: 2176
#include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod=1000000007;
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;

int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    ll n;
    double num,ans=0,w;
    cin>>n>>w;
    double v[n],b[n];
    vector<pair<double,int> > x;
    for(int i=0;i<n;i++){
    	cin>>v[i]>>b[i];
    	x.pb(make_pair(v[i]/b[i],i));
    }
    sort(be(x));
    reverse(be(x));
    for(auto& i:x){
    	num=min(b[i.second],w);
    	w-=num;
    	ans+=num*i.first;
    }
    doublecout(ans);
    return 0;
}



Measured memory_bytes: 6140
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end())
#define VRSORT(v) sort(v.rbegin(), v.rend())
#define ll long long
#define pb(a) push_back(a)
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

typedef vector<unsigned int>vec;
typedef vector<vec> mat;

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

const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const double PI = acos(-1.0);
const double EPS = 1e-10;

vector<vector<int>> G;

int N,K;

ll dfs(int now, int from){
    int color;
    if(from == -1){
        color = K-1;
    }
    else color = K-2;
    if(K<G[now].size()) return 0;
    else{
        ll res = 1;
        for(auto e:G[now]){
            if(e==from) continue;
            res *= color;
            color--;
            res %= MOD;
        }
        for(auto e:G[now]){
            if(e==from) continue;
            res *= dfs(e,now);
            res %= MOD;
        }
        return res;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin>>N>>K;
    G.resize(N);
    REP(i,N-1){
        int a,b;
        cin>>a>>b;
        a--,b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    ll ans = K*dfs(0,-1);
    ans %= MOD;
    cout<<ans<<endl;
}


Measured memory_bytes: 10368
using System;
using System.Collections.Generic;

namespace ALDS1_3
{
    class Program
    {
        static void Main(string[] args)
        {
            var stack = new Stack<int>();
            foreach (var op in Console.ReadLine().Split())
            {
                int num;
                if (int.TryParse(op, out num))
                {
                    stack.Push(num);
                }
                else
                {
                    stack.Push(Calc(op, stack.Pop(), stack.Pop()));
                }
            }

            Console.WriteLine(stack.Pop());
        }

        private static int Calc(string operat, int operand1, int operand2)
        {
            switch (operat)
            {
                case "+":
                    return operand2 + operand1;
                case "-":
                    return operand2 - operand1;
                case "*":
                    return operand2 * operand1;
                default:
                    throw new ArgumentException();
            }
        }

    }
}

Measured memory_bytes: 20280