akhauriyash/Code-Regression huggingface dataset:
-- SPDX-License-Identifier: X11
-- 2020-08-23
-- Step

module Main where

solve :: [Integer] -> Integer
solve = h 0
  where
    h :: Integer -> [Integer] -> Integer
    h a []           = a
    h a [_]          = a
    h a (x : y : ys) = h (a + if x - y > 0 then x - y else 0)
                         ((y + if x - y > 0 then x - y else 0) : ys)

parse :: String -> [Integer]
parse = map (read :: String -> Integer) . words . last . lines

main :: IO ()
main = getContents >>= \x -> print . solve . parse $ x


Measured memory_bytes: 96968
function main(input) {
	// 入力
    var s = [];
	s[0] = input.charAt(0);
	s[1] = input.charAt(1);
	s[2] = input.charAt(2);
	var ans = 0;
	// 処理
	for (var i = 0; i < 3; i++) {
		if (s[i] == 1) {
			ans = ans + 1;
		}
	}
	// 出力
	console.log(ans);
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));

Measured memory_bytes: 7372
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <string.h>
#include <vector>
using namespace std;

#define ll long long
#define INF (1 << 30)
#define INFLL (1LL << 60)

#define FOR(i,a,b) for(ll i = (a);i<(b);i++)
#define REP(i,a) FOR(i,0,(a))
#define MP make_pair

vector<ll> a;

int main() {
    ll n;
    cin >> n;
    REP(i, 3*n) {
        int t;
        cin >> t;
        a.push_back(t);
    }

    sort(a.begin(),a.end(),greater<int>());

    ll ans = 0;
    for (int i = 1; i < 2 * n; i+=2) {
        ans += a[i];
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 6256
import java.util.*;

public class Main {
	Scanner sc = new Scanner(System.in);
	int DP(int h,int w,int hw[][]){
		int result = 0;
		int yx[][] = new int [h][w];
		for(int i = 0;i < w;i++){
			if(hw[0][i] == 0){
				yx[0][i] = 1;
			}
		}
		for(int i = 0;i < h-1;i++){
			for(int j = 0;j < w;j++){
				if(j == 0){
					if(hw[i+1][j] == 0){
						if(w == 1){
							yx[i+1][j] = yx[i+1][j] + yx[i][j];
							break;
						}else{
							yx[i+1][j] = yx[i+1][j] + yx[i][j] + yx[i][j+1];
						}
					}
				}else if(j == w-1){
					if(hw[i+1][j] == 0){
						yx[i+1][j] = yx[i+1][j] + yx[i][j] + yx[i][j-1];
					}
				}else{
					if(hw[i+1][j] == 0){
						yx[i+1][j] = yx[i+1][j] + yx[i][j-1] + yx[i][j] + yx[i][j+1];
					}
				}
			}
			for(int j = 0;j < w;j++){
				if(hw[i+1][j] == 2){
					int cnt = 0;
					while(true){
						if(i+cnt+1 == h-2){
							cnt = cnt + 1;
							break;
						}else{
							if(i+cnt+2 == h)break;
							cnt = cnt + 2;
						}
						if(hw[i+cnt+1][j] != 2)break;
					}
					if(hw[i+cnt+1][j] == 0 || hw[i+cnt+1][j] == 2){
						yx[i+cnt+1][j] = yx[i+cnt+1][j] + yx[i][j];
					}else if(hw[i+cnt+1][j] == 1 && (h-i) % 2 == 1 && i+cnt+1 == h-1){
						yx[i+cnt+1][j] = yx[i+cnt+1][j] + yx[i][j];
					}
				}
			}
		}
		for(int i = 0;i < w;i++){
			result = result + yx[h-1][i];
		}
//		for(int i = 0;i < h;i++){
//			for(int j = 0;j < w;j++){
//				System.out.print(yx[i][j]+" ");
//			}
//			System.out.println();
//		}
		return result;
	}
	void doIt() { 
		while(true){
			int w = sc.nextInt();
			int h = sc.nextInt();
			int hw[][] = new int [h][w];
			if(h+w == 0)break;
			for(int i = 0;i < h;i++){
				for(int j = 0;j < w;j++){
					hw[i][j] = sc.nextInt();
				}
			}
			System.out.println(DP(h,w,hw));
		}
     }
    public static void main(String[] args) {
    	// TODO Auto-generated method stub
    	new Main().doIt();
    }
}

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

int main(){
    ll H;
    cin >> H;
    ll ans = 0;
    ll i = 0;
    while (H > 0)
    {
        H /= 2;
        ans = ans + pow(2, i);
        i++;
    }
    cout << ans << endl;
}

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

int main() {
	int c[3][3];
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) cin >> c[i][j];
	}
	int a[] = { 0, 0, 0 };
	int b[] = { 0, 0, 0 };
	for (int i = 0; i <= 100; i++) { // a1を決める
		a[0] = i;
		for (int j = 0; j < 3; j++) b[j] = c[0][j] - a[0]; // bが決まる
		for (int k = 1; k < 3; k++) a[k] = c[k][0] - b[0]; // aの残りも決まる
		// 他の個所の条件を満たすか調べる
		bool correct = true;
		for (int j = 1; j < 3; j++) {
			for (int k = 1; k < 3; k++) {
				if (c[j][k] != a[j] + b[k]) {
					correct = false;
					break;
				}
			}
			if (!correct) break;
		}
		if (correct) {
			cout << "Yes" << endl;
			return 0;
		}
	}
	cout << "No" << endl;
	return 0;
}

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

#define N 200005
int a[200005], tmp[200004], num[200005];
map<int, int> vis;

int main() {
#ifdef whyqx
    freopen("work.in", "r", stdin);
    freopen("work.out", "w", stdout);
#endif
    int n;
    cin >> n;
    int cnt = 0;
    for (int i = 1, x; i <= n; ++i) {
        scanf("%d", a + i);
        if (!vis[a[i]]) x = vis[a[i]] = ++cnt;
        else x = vis[a[i]];
        num[x]++;
        tmp[x] = a[i];
    }
    bool ok = true;
    if (n % 3) {
        if (cnt == 1 && tmp[1] == 0) {
            puts("Yes");
        }
        else puts("No");
        return 0;
    }
    sort(a + 1, a + n + 1);
    for (int i = 1; i < n / 3; ++i)
        if (a[i] != a[i + 1]) ok = false;
    for (int i = n / 3 + 1; i < n / 3 * 2; ++i)
        if (a[i] != a[i + 1]) ok = false;
    for (int i = n / 3 * 2 + 1; i < n; ++i)
        if (a[i] != a[i + 1]) ok = false;
    int x = a[n / 3], y = a[n / 3 + 1], z = a[n / 3 * 2 + 1];
    if (((x ^ y) ^ z) != 0) ok = false;
    else ok = true;
    puts(ok ? "Yes" : "No");
    return 0;
}


Measured memory_bytes: 4736
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<map>
#include<tuple>
#include<algorithm>
#include<cmath>
#include<limits>
#include<set>
#include<deque>
#include<queue>
using namespace std;
#define int long  long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef pair<int, int>P;
const int MOD = 1e9 + 7;
//const int MOD = 998244353;
const int INF = 1e18;
const long double PI = (acos(-1));


signed main() {
	int N, M, X;
	cin >> N >> M >> X;
	vector<int>C(N);
	vector<vector<int>>A(N, vector<int>(M));
	rep(i, N) {
		cin >> C[i];
		rep(j, M) {
			cin >> A[i][j];
		}
	}
	int ans = INF;
	for (int i = 0; i < (1ll << N); i++) {
		vector<int>v(M, 0);
		int money = 0;
		rep(j, N) {
			if ((i >> j) & 1) {
				money += C[j];
				rep(k, M) {
					v[k] += A[j][k];
				}
			}
		}
		bool can = true;
		rep(j, M) {
			if (v[j] < X)can = false;
		}
		if (can)ans = min(ans, money);
	}
	if (ans == INF)cout << -1;
	else cout << ans;
}

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

    	
    	
#define ll long long
#define pb push_back
#define mp make_pair

ll power(ll x,ll b)
{
    ll res=1;
    if(b==0)
    return 1;
    while(b>0)
    {
      if(b%2!=0)
      res=(res*x);
      b=b/2;
      x=(x*x);
      
    }
    return res;
}
 
 
       


  



      
   
    	
using namespace std;

 
vector<ll> primes;
void SieveOfEratosthenes(int n) 
{ 
    
    vector<ll>prime(200001,0);
  
    for (int p=2; p*p<=n;p++) 
    { 
       
        if (prime[p] == 0) 
        { 
           
            for (int i=p*p; i<=n; i += p) 
                prime[i] = 1; 
        } 
    } 
    
  
    // Print all prime numbers 
    for (int p=1; p<=n; p++) 
       if (prime[p]==0) 
        {
            primes.pb(p);
        }
} 












int main()
{
   ll n,i;
   cin>>n;
   ll arr[n][3];
   for(i=0;i<n;i++)
   {
       cin>>arr[i][0]>>arr[i][1]>>arr[i][2];
       
   }
   ll dp[n][3]={0};
   for(i=0;i<n;i++)
   {
       if(i==0)
       {
           dp[i][0]=arr[i][0];
           dp[i][1]=arr[i][1];
           dp[i][2]=arr[i][2];
       }
       else
       {
           dp[i][0]=arr[i][0]+max(dp[i-1][2],dp[i-1][1]);
                      dp[i][1]=arr[i][1]+max(dp[i-1][2],dp[i-1][0]);
           dp[i][2]=arr[i][2]+max(dp[i-1][0],dp[i-1][1]);


       }
       //cout<<dp[n-1][0]<<" "<<dp[n-1][1]<<" "<<dp[n-1][2];
   }
   ll A=dp[n-1][0],B=dp[n-1][1],C=dp[n-1][2];
  ll largest = A > B ? (A > C ? A : C) : (B > C ? B : C); 
        cout<<largest;
    
  
      
       
    

   







}



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

#include <cstdlib>  // abs() for integer 絶対値求めやつ

#include <cmath>    // abs() for float, and fabs()

#include <algorithm>
#include <vector>
#include <string>
#define rep(i,n) for(int i = 0; i< (n);i++)

#define SORT(a) sort((a).begin(),(a).end());

#define che(a,string) cout<<string<<":"<<(a)<<endl;

#define ch(a,string) cout<<string<<":"<<(a)<<" ";

using namespace std;

typedef pair<int,int> p;

int main(){
	int a,b,c,d;
	cin>>a>>b>>c>>d;
	int A=a*b;
	int B=c*d;
	int ans =max(A,B);
	cout<<ans<<endl;
}
	 


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

int main(){
    string s,t;
    cin >> s>>t;
    string temp = s + s;
    int ans = temp.find(t);
    if(ans ==string::npos){
        cout << "No" << endl;
    }else {
        cout << "Yes" << endl;
    }
    return 0;
}

Measured memory_bytes: 256
package main

import (
	"fmt"
	"strconv"
)

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

	ans := 0
	for i := 1; i <= N; i++ {
		s := strconv.Itoa(i)
		if len(s)%2 == 1 {
			ans++
		}
	}
	fmt.Println(ans)
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0, max_i = (n); i < max_i; i++)
#define REPI(i, a, b) for (int i = (a), max_i = (b); i < max_i; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define fi first
#define se second
#define int long long
using namespace std;
using ll = long long;
using II = pair<int, int>; using VII = vector<II>; using VVII = vector<VII>;
using VI = vector<int>; using VVI = vector<VI>; using VVVI = vector<VVI>;
template <class T = int> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template <class T = int> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template <class T> ostream& operator<<(ostream &s, const vector<T>& d) { REP (i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : " "); return s; }
template <class T> ostream& operator<<(ostream &s, const vector<vector<T>>& d) { REP (i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : "\n"); return s; }
template <class T, class S> ostream& operator<<(ostream &s, const pair<T, S>& p) { s << "{" << p.first << ", " << p.second << "}"; return s; }
template <class T, class S> ostream& operator<<(ostream &s, const map<T, S> m) { for (pair<T, S> p: m) s << p << endl; return s; }
#ifdef _MY_DEBUG
  #define dump(...) cerr << "/* " << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << endl, dump_func(__VA_ARGS__), cerr << "*/\n\n";
#else
  #define dump(...)
#endif
void dump_func() { cerr << endl; }
template <class Head, class... Tail> void dump_func(Head&& h, Tail&&... t) { cerr << h << (sizeof...(Tail) == 0 ? "" : ", "), dump_func(forward<Tail>(t)...); }
struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); } } fast;
constexpr int MOD = 1e9 + 7;

int count(int a, int b) {
  if (a <= b && b < 4 * a) return 0;
  else if (a < b) {
    return -1 + count(4 * a, b);
  } else {
    return 1 + count(a, 4 * b);
  }
}
signed main() {
  int N; cin >> N;
  VI A(N);
  REP (i, N) cin >> A[i];
  VVVI dp(2, VVI(N, VI(16)));
  REP (k, 2) {
    REP (x, 16) {
      dp[0][N - 1][x] = dp[1][N - 1][x] = x;
    }
  }
  REP (k, 2) {
    for (int i = N - 2; i >= 0; i--) {
      int dif = count(A[i], A[i + 1]);
      REP (x, 16) {
        int tmp = max(0LL, x + dif);
        dp[k][i][x] = dp[k][i + 1][min(15LL, tmp)] + x + max(0LL, tmp - 15) * (N - i - 1);
      }
    }
    reverse(ALL(A));
  }

  int ans = 1e18;
  REP (i, N) {
    int tmp = i + (dp[0][i][0] + (i == 0 ? 0 : dp[1][N - i][0])) * 2;
    ans = min(ans, tmp);
  }
  cout << ans << endl;
}


Measured memory_bytes: 100224
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define mp make_pair
int main()
{
    int n, x, nr;
    cin >> n >> x;
    vector<int> v;
    rep(i, n)
    {
        cin >> nr;
        v.pb(nr);
    }
    int d = 0;
    int bounces = 0;
    for (int i = 1; i < n+1 ;i++)
    {
        d = d + v[i-1];
        if (d > x)
        {
            break;
        }
        bounces++;
    }
    cout << bounces + 1;
    return 0;
}

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

int main() {
    long long n; cin >> n;
    long long ans=0;
    for (int i = 1; i<=n; i++) {
        ans += (n/i*(2*i+(n/i-1)*i))/2;
    }
    cout << ans << endl;
}

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

//round()で四捨五入できる。

int main(){
    int N,D;
    cin >> N >> D;
    vector<vector<int>> vec(N,vector<int>(D));
    for (int i=0; i<N;i++){
        for (int j=0; j<D;j++){
            cin >> vec.at(i).at(j);
        }
    }
    int count = 0;
    for (int i = 0; i < N; i++){
        for (int j = i+1; j < N; j++){
            int sum = 0;
            for (int k = 0; k < D; k++){
                int diff = abs(vec.at(j).at(k) - vec.at(i).at(k));
                sum += diff*diff;
            }
            bool flag = false;
            for (int l = 1; l < sum+1; l++){
                if (l*l == sum){
                    flag = true;
                }
            if (flag){
                count++;
                break;
            }
            }
        }
    }
    cout << count << endl;
}

Measured memory_bytes: 256
import java.util.Scanner;
public class Main {
	static long gcd(long a,long b) {
		long temp = 0;
		while(a % b != 0) {
			temp = a;
			a = b;
			b = temp % b;
		}
		return b;
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong();
		long b = sc.nextLong();
		int c = sc.nextInt();
		int d = sc.nextInt();
		long p = c > d ? gcd(c,d):gcd(d,c);
		long k = (long)c*d/p;
		
		long ansb = b -(b/c + b/d - b/k);
		long ansa = (a -1) -((a-1)/c+(a-1)/d-(a-1)/k);
		System.out.println(ansb -ansa);

	}

}


Measured memory_bytes: 21716
# include <bits/stdc++.h>
# define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
# define reps(i, n) for(int i=1, i##_len=(n); i<=i##_len; ++i)
# define rrep(i, n) for(int i=((int)(n)-1); i>=0; --i)
# define rreps(i, n) for(int i=((int)(n)); i>0; --i)
# define ALL(x) (x).begin(), (x).end()
# define SZ(x) ((int)(x).size())
# define pb push_back
# define optimize_cin() cin.tie(0); ios::sync_with_stdio(false)
using namespace std;
using lint = long long;
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; }
const long long INF = 1LL << 60;

long long gcd(long long a, long long b)
{
   if (a%b == 0)
   {
       return(b);
   }
   else
   {
       return(gcd(b, a%b));
   }
}

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

    long long lcm = c*d/gcd(c,d);

    long long x = a-1 - (a-1)/c - (a-1)/d + (a-1)/lcm;

    long long y = b - b/c - b/d + b/lcm;

    cout << y-x << endl;
    
    return 0;
}

Measured memory_bytes: 256
use std::io::*;
use std::str::FromStr;

fn main() {
    let n: usize = read();
    let w: Vec<i64> = (0..n).map(|_| read()).collect();

    let mut sum = 0;
    for i in 0..n {
        sum += w[i];
    }

    let mut ans = sum;
    let mut now = 0;
    for i in 0..n {
        now += w[i];
        ans = std::cmp::min(ans, ((sum - now) - now).abs());
    }
    println!("{}", ans);
}

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().unwrap()
}


Measured memory_bytes: 4352
import sys
A, B = map(int, input().split())

b = format(B, 'b')
num = len(b)
cnt1 = [0]*num
for i in range(1, num):
    if (B+1)%(2**(i+1)) <= 2**i:
        continue
    else:
        if (B+1)%2 == 1:
            cnt1[i] = 1
if B % 4 == 1 or B % 4 == 2:
    cnt1[0] = 1

c = ''
if A == 0:
    for i in range(num):
        if cnt1[i] == 0:
            c = '0' + c
        else:
            c = '1' + c
    print(int(c, 2))
    sys.exit()

cnt2 = [0]*num
for i in range(1, num):
    if A%(2**(i+1)) <= 2**i:
        continue
    else:
        if A%2 == 1:
            cnt2[i] = 1
if (A-1) % 4 == 1 or (A-1) % 4 == 2:
    cnt2[0] = 1

for i in range(num):
    if cnt1[i] == cnt2[i]:
        c = '0' + c
    else:
        c = '1' + c
print(int(c, 2))


Measured memory_bytes: 3064
open Batteries
open BatPrintf

let scan fmt f =
  Scanf.sscanf (read_line ()) fmt f

let scan_list cnv =
  read_line ()
  |> String.split_on_char ' '
  |> List.map cnv

let scan_listn n cnv =
  (0 --^ n) |> Enum.map (fun _ -> (cnv % read_line) ())

let bsearch_ge arr a =
  let l = Array.length arr in
  match Array.bsearch Int.ord arr a with
  | `All_lower -> l
  | `All_bigger -> 0
  | `Just_after n -> l - n -1
  | `At n -> l - n
  | `Empty -> 0

let rec zip xs ys =
  match xs, ys with
  | [], _ -> []
  | _, [] -> []
  | x::xs, y::ys ->
    (x, y) :: zip xs ys

let square x = x * x

let dbg n = Printf.printf "%s\n" @@ dump n; n

module Comb = struct
  let prime = 1_000_000_000+7

  let ( * ) a b = a * b mod prime
  let (-) a b = (a+prime - b) mod prime

  let rec pow n m =
    if m = 0 then 1
    else if m mod 2 = 0 then pow (n*n) (m/2)
    else n * pow (n*n) (m/2)

  let fact n =
    let rec aux m ret =
      if m = 0 then ret
      else aux (m-1) @@ m*ret in
    aux n 1

  let comb n k =
    let x = Enum.fold ( * ) 1 (n-k+1 -- n) in
    x * (pow (fact k) (prime - 2))
end

let (n, a, b) = scan "%d %d %d" Tuple.Tuple3.make


let () =
  let open Comb in
  pow 2 n
  |> (fun x -> (-) x 1)
  |> (fun x -> (-) x @@ comb n a)
  |> (fun x -> (-) x @@ comb n b)
  |> printf "%d\n"


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

#define rep(i, n) for(int i=0; i<n; ++i)
#define repit(it, li) for(auto it=li.begin(); it!=li.end(); it++)
#define ll long long int

int main(){
    int n;
    cin>>n;
    unordered_map<string, bool> deta;
    rep(i, n){
        string s;
        cin>>s;
        deta[s]=true;
    }
    cout << deta.size() << endl;
    return 0;
}


Measured memory_bytes: 17556
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <math.h>

int main (void)
{
    intmax_t N;
    intmax_t ans = 0;

    scanf("%jd", &N);

    for (intmax_t ans = 0; ans < 50000; ans++) {
        if (((ans * 108) / 100) == N) {
            printf("%jd\n", ans);
            return 0;
        }
    }
    
    puts(":(");

    return 0;
}


Measured memory_bytes: 128
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <queue>
#include <algorithm>
using namespace std;
int min(int a,int b){
    return a<b?a:b;
}
int main(void){
    long long N,A,B;
    cin >> N >> A >> B;
    long long K =  N / (A+B);
    long long M = N % (A+B);
    long long ans = K * A + min(M,A);
    cout << ans << endl;
}


Measured memory_bytes: 3164
//
// こだわり者いろはちゃん / Iroha's Obsession
//

#include <iostream>
#include <queue>
#include <set>

using namespace std;

int main() {

    int N, K, len;
    cin >> N >> K;
    len = to_string(N).length();

    bool D[10];

    fill(D, D + 10, true);

    for (int i = 0; i < K; i++) {
        int d;
        cin >> d;
        D[d] = false;
    }

    queue<pair<int, int>> que;
    set<int> set;

    que.push(make_pair(0, 0));

    while (!que.empty()) {
        auto &p = que.front();
        que.pop();
        for (int i = 0; i < 10; i++) {
            if (D[i] && p.second <= len) {
                int n = p.first * 10 + i;
                que.push(make_pair(n, p.second + 1));
                set.insert(n);
            }
        }
    }

    cout << *lower_bound(set.begin(), set.end(), N) << endl;
}

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

}

Measured memory_bytes: 256
namespace ABC130.B
{
    using System.Collections.Generic;
    using System.Linq;
    using static System.Console;

    public class Program
    {
        public static void Main(string[] args)
        {
            var l = ReadInputsAsInt().ToList();
            var n = l[0];
            var x = l[1];

            var L = ReadInputsAsInt().ToList();

            WriteLine(Solve(n, x, L));
        }

        public static int Solve(int n, int xMax, List<int> L)
        {
            int x = 0;
            for (int i = 0; i < n; i++)
            {
                x += L[i];
                if (x > xMax) return i + 1;
            }
            return n + 1;
        }


        static IEnumerable<int> ReadInputsAsInt() => ReadLine().Split(' ').Select(x => int.Parse(x));
    }
}


Measured memory_bytes: 13268
#include <bits/stdc++.h>
#define INF (1<<30)
#define MOD 1000000007
#define l_ength size
using ll = long long;
using namespace std;

int main(){
	string a;
	cin >> a;
	cout << ( ( a[0] == '1' ) ? '9' : '1' );
	cout << ( ( a[1] == '1' ) ? '9' : '1' );
	cout << ( ( a[2] == '1' ) ? '9' : '1' );
	cout << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
using namespace std;
int main()
{
  string s;
  cin >> s;
  rep(i,s.size()){
    if(s[i]==','){
      s[i] = ' ';
    }
  }
  cout << s << "\n";
  return 0;
}


Measured memory_bytes: 256
def diff_str(S, T):
    ret = 0
    for i in range(len(S)):
        if S[i] != T[i]:
            ret += 1
    return ret

S = input()
T = input()
ans = len(T)
for i in range(len(S) - len(T) + 1):
    ans = min(ans, diff_str(S[i : i + len(T)], T))
print(ans)


Measured memory_bytes: 9052
#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 a, b; cin >> a >> b;
    if (a % 3 == 0 || b % 3 == 0 || (a+b) % 3 == 0)
        cout << "Possible" << endl;
    else
        cout << "Impossible" << endl;
    return 0;
}


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

int N;
int T;

int minT=1001;
int minc=1001;

int main(){
    cin >> N >> T;
    int c[N][2];
    for(int i=0;i<N;i++){
        for(int j=0;j<2;j++){
            cin >> c[i][j];
            if(j == 1){
                if(c[i][j]<=T){
                    minT = c[i][j];
                    if(minc>c[i][0]) {
                        minc=c[i][0];
                    }
                }
            }
        }
    }
    if(minT > T) cout<<"TLE"<<endl;
    else cout<<minc<<endl;
}

Measured memory_bytes: 256
//Mahir Ratanpara (DA-IICT)
#include<bits/stdc++.h>
using namespace std;
#define Ff(i,a,n) for(i=a;i<n;i++)
#define Fr(i,a,n) for(i=a;i>n;i--)
#define ll long long
#define FAST ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
typedef pair<ll, ll>	pa;
typedef vector<ll>		vc;
typedef vector<pa>		vp;
typedef vector<vc>		vvc;
int mpow(int base, int exp); 
void ipgraph(ll n,ll m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
//=======================

vc g[N];

int main() {
    FAST;
    ll t, i, j, k, p, q, r, x, y, u, v, n, m;
    cin>>n;
    x=0;
    while(1)
    {
      if(x>=n)
      break;
      x+=1000;
    }
    cout<<abs(x-n);
    return 0;
}

int mpow(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((ll)result * base) % mod;
    base = ((ll)base * base) % mod;
    exp >>= 1;
  }
  return result;
}

void ipgraph(ll n, ll m){
	int i, u, v;
	while(m--){
		cin>>u>>v;
		g[u-1].pb(v-1);
		g[v-1].pb(u-1);
	}
}

void dfs(int u, int par){
	for(int v:g[u]){
		if (v == par) continue;
		dfs(v, u);
	}
}

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

int main(){
  int n, s;
  s = 0;
  cin >> n;
  int a[n];
  for(int i = 0; i < n; i++)
    cin >> a[i];
  for(int i = 0; i < n; i++){
    for(int j = i + 1; j < n; j++){
      s = s + a[i] * a[j];
    }
  }
  cout << s << endl;
}

Measured memory_bytes: 256
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0529
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define DIV(a, b) ((a - 1) / b + 1)

signed main() {
    int N, M;
    while (cin >> N >> M, N) {
        vector<int> P(N + 1);
        REP(i, N) { cin >> P[i]; }
        // ダーツ2回投げた組み合わせを作成し、それらの組み合わせを
        // 求めることでダーツ4回の組み合わせになる
        P[N] = 0;
        vector<int> v;
        REP(i, N + 1) REP(j, N + 1) { v.emplace_back(P[i] + P[j]); }
        sort(ALL(v));
        unique(ALL(v));
        int res = 0;
        for (auto &x : v) {
            if (x > M) {
                break;
            }
            res = max(res, x + *(lower_bound(ALL(v), M - x + 1) - 1));
        }
        cout << res << endl;
    }
}


Measured memory_bytes: 18932
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<string.h>
#include<complex>
#include<math.h>
using namespace std;
typedef long long int llint;
#define rep(i,n) for(int i=0;i<n;i++)

bool check(char a, char b) {
	return a > b;
}

int main() {
	char s[110], t[110];
	cin >> s >> t;
	sort(s, s + strlen(s));
	sort(t, t + strlen(t), check);
	string ss = s;
	string tt = t;
	if (ss < tt) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}
	return 0;
}

Measured memory_bytes: 256
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define mod 998244353
#define inf (1LL<<31)
#define int long long
typedef long long ll;
#define ld long double
#define N 301 

int32_t main()
{
    IOS;
   
    int n,a,b;
    cin >> n >> a >> b;
    int v[n];
    int cnt[3];
    memset(cnt,0,sizeof(cnt));
    for(int i = 0;i < n;i++)
    {
        cin >> v[i];
        if(v[i] <=a )
            cnt[0]++;
        if(v[i]> a and v[i]<=b)
            cnt[1]++;
        if(v[i]>b)
            cnt[2]++;
    }
    int ans = min(cnt[0],min(cnt[1],cnt[2]));
    cout << ans;
}

Measured memory_bytes: 256
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Random;

public class Main {

    private void solve() throws IOException {
        new Main().solve();
    }

    public static void main(String[] args) throws IOException {
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);

        Random r = new Random();
        int n = Integer.parseInt(f.readLine());
        long[] nums = new long[n];

        for (int i = 0; i < n; i++) {
            char[] seq = f.readLine().toCharArray();
            long firstMultiply = 1000000000;
            boolean passedDot = false;
            for (int j = 0; j < seq.length; j++) {
                if (passedDot) firstMultiply /= 10;
                if (seq[j] == '.') passedDot = true;
            }

            long value = 0;
            for (int j = seq.length - 1; j >= 0; j--) {
                if (seq[j] == '.') continue;
                value += (seq[j] - '0') * firstMultiply;
                firstMultiply *= 10;
            }
            nums[i] = value;
        }

        int[][] atLeast = new int[50][50];
        long pairs = 0;
        for (int i = 0; i < n; i++) {
            int diviedByTwo = 0;
            int dividedByFive = 0;
            while (nums[i] % 2 == 0) {
                diviedByTwo++;
                nums[i] /= 2;
            }
            while (nums[i] % 5 == 0) {
                dividedByFive++;
                nums[i] /= 5;
            }

            for (int j = Math.max(18 - diviedByTwo, 0); j < 50; j++) {
                for (int k = Math.max(18 - dividedByFive, 0); k < 50; k++) {
                    pairs += atLeast[j][k];
                }
            }

            atLeast[diviedByTwo][dividedByFive]++;
        }

        out.println(pairs);
        out.close();
    }
}

Measured memory_bytes: 54676
_in = input()
h, w = [int(i) for i in _in.split(' ')]

data = []
for i in range(h):
    _input = input()
    data.append([char for char in _input])

del_row_idx = []
for i, row in enumerate(data):
    tf = [char=='.' for char in row]
    flag = True
    for t in tf:
        flag *= t
    if flag:
        del_row_idx.append(i)

del_col_idx = []
for i in range(w):
    tf = [char=='.' for char in [row[i] for row in data]]
    flag = True
    for t in tf:
        flag *= t
    if flag:
        del_col_idx.append(i)

for idx in del_row_idx[::-1]:
    data.pop(idx)

for idx in del_col_idx[::-1]:
    for row in data:
        row.pop(idx)

for d in data:
    print(''.join(d))


Measured memory_bytes: 3188
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> Graph (n, vector<int> (0));
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        Graph[a].push_back(b);
        Graph[b].push_back(a);
    }
    queue <int> que;
    vector <int> ans(n - 1, -1);
    que.push(0);
    while (not que.empty()) {
        int v = que.front();
        que.pop();
        for (int nv : Graph[v]) {
            if (not(ans[nv-1] == -1)) continue;
            ans[nv - 1] = v;
            que.push(nv);
        }
    }
    cout << "Yes" << endl;
    for (int i = 0; i < n - 1; i++) {
         cout << ans[i] + 1 << endl;
     }
    return 0;
}

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

using int32 = int_fast32_t;
using int64 = int_fast64_t;

const int32 INF = 1e9;
const int32 MOD = 1e9+7;
const int64 LLINF = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n"  )
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n"  )
#define ANS(n) cout << (n) << "\n"
#define REP(i,n) for(int64 i=0;i<(n);++i)
#define FOR(i,a,b) for(int64 i=(a);i<(b);i++)
#define FORR(i,a,b) for(int64 i=(a);i>=(b);i--)
#define ALL(obj) (obj).begin(),(obj).end()
#define pii pair<int32,int32>
#define pll pair<int64,int64>
#define pb(a) push_back(a)
#define mp make_pair

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int32 a, b;
    cin >> a >> b;
    cout << max({a + b, a - b, a * b}) << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define sort(vec) sort(vec.begin(),vec.end())
#define reverse(vec) reverse(vec.begin(),vec.end())
#define unsort(vec); sort(vec);reverse(vec);
#define int long long
long long ans;

int gcd(int x,int y){
	if(y==0) return x;
	else return gcd(y,x%y);
}

int lcm(int x,int y){
	return x/gcd(x,y)*y;
}
 
signed main(){
	int n;
	cin>>n;
	vector<int>a(n);
	for(int i=0;i<n;i++)cin>>a[i];
	if(n%2==1){
		for(int i=n-1;i>=0;i-=2){
			cout<<a[i]<<" ";
		}
		for(int i=1;i<n;i+=2){
			cout<<a[i]<<" ";
		}
	}
	else{
		for(int i=n-1;i>=1;i-=2){
			cout<<a[i]<<" ";
		}
		for(int i=0;i<n;i+=2){
			cout<<a[i]<<" ";
		}
	}
	cout<<endl;
}


Measured memory_bytes: 3712
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define c(P)        cout<<P<<"\n"
#define pii         pair<ll,ll>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define F           first
#define S           second
#define sz(a)       (ll)(a.size())
#define mem(a,val)  memset(a,val,sizeof(a))
#define fr(i,x,n)   for(ll i=x;i<n;i++)
#define frr(i,x,n)  for(ll i=n-1;i>=x;i--)
using namespace std;


const ll N   =  500005;
const ll mod = 1e9 + 7;



void solve(){

  ll x=0,y=0,c=0,ans=0;
  ll n,m,k;

  string s,t,r;
  cin>>s;
  n =s.size();
  x = 1;
  for (ll i = 0; i < n/2; ++i)
  {
    if(s[i]!=s[n-i-1])
    	x=  0;
  }
  for (ll i = 0; i <= n/2 -1; ++i)
  {
  	r+=s[i];
  }
  k =r.size();
  for (int i = 0; i < k/2; ++i)
  {
  	if(r[i]!=r[k-i-1]){
  		x = 0;
  	}
  }

  for (ll i = n/2 + 1; i < n; ++i)
  {
  	t+=s[i];
  }
  n =t.size();
  for (int i = 0; i < n/2; ++i)
  {
  	if(t[i]!=t[n-i-1]){
  		x = 0;
  	}
  }
  if(x)c("Yes");
  else c("No");
}

signed main(){
     
  ios_base::sync_with_stdio(false);   
  cin.tie(NULL);
  //int T;cin >> T;while (T--)
  solve();
  return 0;
}


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

#define ll long long 
#define F first
#define S second
#define pb push_back
#define pp pop_back
#define in insert
#define pii pair<int, int>
#define pll pair<ll, ll>
#define N int(1e5) + 5
#define T int(1e4) + 55
#define H int(1e3) + 55

using namespace std;

string q, w, e;
ll n, m, a, sum, num, sam, nam, qwe, s[31], d[31];

int main() {
	cin >> q;
	for(int i = 0;i < q.size(); ++ i) {
	 	if(sam == 0) {
	 		s[q[i] - 'a'] ++;
	 		sum ++;
	 	}	
	 	else {
		 	if(nam % 2 == 1) {
		 	 	fill(s,s + 28, 0);
		 	 	sum = num;
		 	 	num = 0;
	 		}
	 		else {
	 		 	fill(d, d + 28, 0);
	 		 	sum = num;
	 		 	num = 0;
	 		}                                                                                                
	 	}
	 	for(int j = i + 1;j < q.size(); ++ j) {
			if(nam % 2 == 1) {
				s[q[j] - 'a'] ++;
				num ++;
			}                                                                                                
			else {
				d[q[j] - 'a'] ++;
				num ++;
			}
			if(s[q[j] - 'a'] != d[q[j] - 'a'] || sum != num) {
			 	i = j - 1;
			 	sum = 0;
			 	sam = 1;
			 	nam ++;
			 	break;	
			}
	 	}
	}
	cout << nam + 1;
	return 0;
}	

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

#define INF -10000
#define MAX_A 1000

int main(){

   
    int a,b,c,d,e,f;
    cin >> a >> b >> c >> d >> e >> f;
    //vector<int> water(MAX_A,INF);
    //vector<int> sugar(MAX_A,INF);
    double ans = INF;
    int ans_water = INF;
    int ans_sugar = INF;
   
    for(int i = 0; 100*a*i<=f;i++){
        for(int j = 0;100*a*i+100*b*j<=f;j++){
            for (int k = 0; 100*a*i + 100*b*j + c*k <= f && c*k <= (a*i + b*j)*e; k++) {
				for(int l = 0; 100*a*i + 100*b*j + c*k + d*l <= f && c*k + d*l <= (a*i + b*j)*e; l++) {
					int water = 100*a*i + 100*b*j;
					int sugar = c*k + d*l;
                    if(ans < 100*sugar/(double)(water+sugar)){
                        ans = 100*sugar/(double)(water+sugar);
                        ans_water=water;
                        ans_sugar=sugar;
                    }
                }
            }
        }
    }



    cout << ans_water+ans_sugar << " " << ans_sugar << endl;



    return 0;
}

Measured memory_bytes: 256
#most
N = int(input())
K = int(input())
n = list(map(int, str(N)))
dp = [[[0] * 5 for __ in range(2)] for _ in range(101)]
l = len(n)
dp[0][0][0] = 1
for i in range(l):
    for smaller in range(2):
        for j in range(4):
            for x in range((9 if smaller else n[i]) + 1):
                dp[i + 1][smaller or x < n[i]][j + 1 if x != 0 else j] += dp[i][smaller][j]
print(dp[l][0][K] + dp[l][1][K])


Measured memory_bytes: 3188
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0,i##_len=(int)(n);i<i##_len;i++)
#define rep1(i,n)for(int i=1,i##_len=(int)(n);i<=i##_len;i++)
#define rrep(i,n) for(int i=(int)(n)-1;0<=i;i--)
#define rrep1(i,n) for(int i=(int)(n);1<=i;i--)

#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define all(obj) (obj).begin(),(obj).end() 

#define pcount __builtin_popcount
#define SZ(x) ((int)(x).size())
#define LEN(x) ((int)(x).length())

#define dump(x)  cerr << #x << " = " << (x) << endl;

#define endl "\n"

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;}

typedef long long lint;

using namespace std;
int main(){
	string a,b,c;
	cin>>a>>b>>c;
	cout<<(*(a.end()-1)==*b.begin()&&*(b.end()-1)==*c.begin()?"YES":"NO")<<endl;
}


Measured memory_bytes: 256
N, M = map(int, input().split())
bulb = []
count = 0

for _ in range(M):
    tmp = list(map(int, input().split()))
    bulb.append(tmp[1:])

P = list(map(int, input().split()))

for i in range(2 ** N):
    switch = []
    for j in range(N):  # スイッチの押し方を全探索
        if (i >> j) & 1:
            switch.append(j+1)
    bulb_count = M
    for k in range(M):  # M個の電球それぞれがついているか判定
        tmp = bulb[k]  # tmpは電球k につながっているスイッチ
        switch_count = 0
        for num in tmp:  # 電球kのスイッチについて判定
            if num in switch:
                switch_count += 1
        if switch_count % 2 == P[k]:
            bulb_count -= 1
    if bulb_count == 0:
        count += 1

print(count)


Measured memory_bytes: 9208
#include<iostream>
using namespace std;
 
int main(){
 
	char a[2];
 
	cin >> a[0] >> a[1];
 
	if (a[0]=='9'){
		cout << "Yes";
	}
	else if(a[1]=='9'){
		cout << "Yes";
	}
	else{
		cout << "No";
	}
 
 
 
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
int main() {
  std::string a,b;
  std::cin >> a>>b;
  int sum=0;
  for (int i=0;i<3;++i){
    sum+=(int)a[i]==b[i];
  }
  std::cout << sum << '\n';
  return 0;
}


Measured memory_bytes: 256
lst = ""
a = input()
if a[0] == "9":
    lst += "1"
else:
    lst += "9"

if a[1] == "9":
    lst += "1"
else:
    lst += "9"

if a[2] == "9":
    lst += "1"
else:
    lst += "9"

print(lst)

Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
int main()
{
	int x;
	scanf("%d",&x);
	int a=x/100,b=x%100;
	int p=0,q=0;
	if(a>0&&a<=12)p=1;
	if(b>0&&b<=12)q=1;
	if(p==1&&q==1)printf("AMBIGUOUS");
	if(p==1&&q==0)printf("MMYY");
	if(p==0&&q==1)printf("YYMM");
	if(p==0&&q==0)printf("NA"); 
	return 0;
}

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

using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using ld = long double;
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define rep3(i, n) for (int i = 1; i < (int)(n+1); i++)
#define rep4(i, s, n) for (int i = (s); i < (int)(n+1); i++)
#define repr(i,n) for (int i = (n-1); i>=0;i--)
#define repr3(i,n) for(int i = (n);i>0;i--)
#define stlen(s) ll s.size()-1
#define all(v) v.begin(), v.end()
#define cout(n) cout<<std::fixed<<std::setprecision(n)
using Graph = vector<vector<int>>;
const ll INF = 1001001000100010;
const ld PI = 3.14159265358979323846;


int main() {
// 入力
    int N; // 都市数
    cin >> N;
    int P[10],Q[10];
    rep(i,N){
        cin>>P[i];
    }
    rep(i,N){
        cin>>Q[i];
    }


    // 初期順列
    vector<int> order(N);
    rep(i,N) order[i] = i+1;

    int a,b,i=1;
    bool judgeP,judgeQ;


    // 探索
    
    do {
        judgeP=true;
        judgeQ=true;
        
        rep(i,N) {
            if(P[i]!=order[i]){
                judgeP=false;
            }
        }
        if(judgeP){
            a=i;
        }
        rep(i,N) {
            if(Q[i]!=order[i]){
                judgeQ=false;
            }
        }
        if(judgeQ){
            b=i;
        }
        i++;
    } while (next_permutation(order.begin(), order.end()));

    // 答えを出力
    cout << abs(a-b) << endl;
return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <vector>
#include <utility>
#include <climits>
using namespace std;
#define MAXSIZE 200
#define MAXL 100
typedef long long int lli;

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

    int head_b_and_tail_a = 0, head_b = 0, tail_a = 0, others = 0;
    int ans = 0;
    for(int i = 0; i < n; ++i){
        string s;
        cin >> s;
        
        for(int j = 1; s[j] != '\0'; ++j){
            if(s.substr(j - 1, 2) == "AB"){
                ans++;
            }
        }

        if(s[0] == 'B' && s[s.length() - 1] == 'A'){
            head_b_and_tail_a++;
        }else if(s[0] == 'B'){
            head_b++;
        }else if(s[s.length() - 1] == 'A'){
            tail_a++;
        }else{
            others++;
        }
    }

    if(head_b_and_tail_a == 0){
        ans += min(head_b, tail_a);
    }else if(head_b + tail_a >= 1){
        ans += head_b_and_tail_a + min(head_b, tail_a);
    }else{
        ans += head_b_and_tail_a - 1;
    }

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

Measured memory_bytes: 256
#include<algorithm>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<sstream>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<cmath>
#include<stack>
#include<set>
#include<map>
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define per(i,n,x) for(int i=n;i>=x;i--)
#define sz(a) int(a.size())
#define rson mid+1,r,p<<1|1
#define pii pair<int,int>
#define lson l,mid,p<<1
#define ll long long
#define pb push_back
#define mp make_pair
#define se second
#define fi first
using namespace std;
const double eps=1e-8;
const int mod=1e9+7;
const int N=1e5+10;
const int inf=1e9;
int n,m,q;
int a[55],b[55],c[55],d[55];
int A[11],ans;
void dfs(int s,int t){
	if(s==n){
		int ret=0;
		rep(i,1,q) if(A[b[i]]-A[a[i]]==c[i]){
			ret+=d[i];
		}
		ans=max(ans,ret);
		return;
	}
	for(int i=t;i<=m;i++){
		A[s+1]=i;
		dfs(s+1,i);
	}
}
int main(){
	//ios::sync_with_stdio(false);
	//freopen("in","r",stdin);
	cin>>n>>m>>q;
	for(int i=1;i<=q;i++){
		cin>>a[i]>>b[i]>>c[i]>>d[i];
	}
	dfs(0,1);
	cout<<ans<<endl;
	return 0;
}

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

#define ll long long
const int inf = 1e9+7;
const ll INF = 1e18L;

using namespace std;

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

  int n, w;
  cin>>n>>w;
  vector<ll> V(n), W(n);
  int mv = 0;
  for(int i = 0; i < n; ++i){
    cin>>W[i]>>V[i];
    mv += V[i];
  }

  vector<ll> dp(mv+1,INF);
  dp[0] = 0;
  for(int j = 0; j < n; ++j){
    for(int i = mv; i-V[j] >= 0; --i){
      dp[i] = min (dp[i-V[j]]+W[j],dp[i]);
    }
  }
  for(int i= mv; i >= 0; --i){
    if(dp[i] <= w){
      cout<<i<<"\n";
      return 0;
    }
  }

}


Measured memory_bytes: 1024
N = int(input())
A = list(map(int,input().split()))
d4 = 0
d2 = 0
d1 = 0
for a in A:
    if a%4 == 0:
        d4 += 1
    elif a%2 == 0:
        d2 += 1
    else:
        d1 += 1
if d4 >= d1:
    print('Yes')
elif d4+1 == d1 and d2%2 == 0:
    print('Yes')
else:
    print('No')

Measured memory_bytes: 14252
//#include <tourist>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
const int INF = 1e9;
const ll LINF = ll(1e18);
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v)          \
    cout << #v << ":";    \
    for (auto x : v)      \
    {                     \
        cout << x << ' '; \
    }                     \
    cout << endl;
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;
}
//cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
//-std=gnu++17
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
vector<string> split(string str, char del) //delは区切りたい文字
{
    int first = 0;
    int last = str.find_first_of(del);

    vector<string> result;

    while (first < str.size())
    {
        string subStr(str, first, last - first);

        result.push_back(subStr);

        first = last + 1;
        last = str.find_first_of(del, first);

        if (last == string::npos)
        {
            last = str.size();
        }
    }
    if (str == result[0])
    {
        result.pop_back();
    }
    return result;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s;
    cin>>s;
    vector<string>ss=split(s,',');
    cout<<ss[0]<<" "<<ss[1]<<" "<<ss[2]<<"\n";
}


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

using namespace std;
using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
#define REP(i,n) for(ll i = 0;i < (ll)n;i++)
#define REPD(i,n) for(ll i = (ll)n - 1;i >= 0;i--)
#define ALL(x) (x).begin(),(x).end()
#define FILLl(n,first_dimension_size,value) fill((ll*)n,(ll*)(n+first_dimension_size),value)
#define FILL(n,first_dimension_size,value) fill((int*)n,(int*)(n+first_dimension_size),value)
#define INF (1 << 30)
#define MOD 1000000007
//int dy[4] = {-1,0,0,1};
//int dx[4] = {0,1,-1,0};



int main(){
  
  int n;
  cin >> n;
  deque<int> dq;
  REP(i,n){
    int x;cin >> x;
    if(i & 1)dq.push_back(x);
    else dq.push_front(x);
  }
  if(n & 1){
    REP(i,n){
      cout << dq.front() << " ";
      dq.pop_front();
    }
  }else{
    REP(i,n){
      cout << dq.back() << " ";
      dq.pop_back();
    }
  }

  

  return 0;
}


Measured memory_bytes: 2944