akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int)x.size()
#define f first
#define s second
#define MAXN 201010

int N,K,L,a,b;
int M[MAXN];
int R[MAXN];
vi V[MAXN];
int fw[MAXN];

void update(int x, int v) {
	// cout<<"Up "<<x<<' '<<v<<'\n';
    for (; x<=N; x+=x&(-x)) fw[x] += v; 
}
int sum(int x) {
    int res = 0;
    for(; x; x-=x&(-x)) res += fw[x];
    return res;
}

int p[MAXN];
int par(int x){return (p[x] == x)?x:p[x] = par(p[x]);}
vpi T;
int out[MAXN];
vi starts;

int main()
{
	cin>>N>>K>>L;
	for (int i=1;i<=N;++i)p[i]=i;
	for (int i=0;i<K;++i){
		cin>>a>>b;
		p[par(a)] = par(b);
	}
	T.pb(-1,-1);
	for (int i=1;i<=N;++i)T.pb(par(i),i);
	sort(ALL(T));
	for (int i=1;i<=N;++i){
		M[T[i].s] = i;
		R[i] = T[i].s;
		if (T[i].f != T[i-1].f)starts.pb(i);
	}
	starts.pb(N+1);

	for (int i=1;i<=N;++i)p[i]=i;
	for (int i=1;i<=L;++i){
		cin>>a>>b;
		a=M[a];
		b=M[b];
		p[par(a)] = par(b);
	}
	for (int i=1;i<=N;++i)V[par(i)].pb(i);
	for (int i=1;i<=N;++i)if(SZ(V[i])){
		for (auto x:V[i])update(x,1);

		for (auto x:V[i]){
			auto t = --ub(ALL(starts), x);
			int st = *t;
			int en = *(++t)-1;
			// cout<<st<<' '<<en<<'\n';
			int ans = sum(en) - sum(st-1);
			out[R[x]] = ans;
		}

		for (auto x:V[i])update(x,-1);
	}
	for(int i=1;i<=N;++i)cout<<out[i]<<' ';
}

Measured memory_bytes: 19436
use proconio::*;

#[fastout]
fn main() {
    input! {
        n: usize,
        s: String,
    };

    let x = s
        .chars()
        .map(|c| c.to_digit(2).unwrap() as i64)
        .collect::<Vec<_>>();
    let pc: i64 = x.iter().sum();
    let mut ans = vec![0; n];

    for b in 0..=1 {
        let mut pc = pc;

        if b == 0 {
            pc += 1;
        } else {
            if pc == 1 {
                continue;
            }
            pc -= 1;
        }

        let mut r0 = 0;
        for &v in &x {
            r0 = ((r0 << 1) + v).rem_euclid(pc);
        }

        let mut k = 1;
        for (i, &v) in x.iter().enumerate().rev() {
            if v == b {
                if v == 0 {
                    ans[i] = f((r0 + k).rem_euclid(pc)) + 1;
                } else {
                    ans[i] = f((r0 - k).rem_euclid(pc)) + 1;
                }
            }

            k = (k << 1).rem_euclid(pc);
        }
    }

    for a in &ans {
        println!("{}", a);
    }
}

fn f(n: i64) -> i64 {
    if n == 0 {
        return 0;
    }

    f(n.rem_euclid(n.count_ones() as i64)) + 1
}


Measured memory_bytes: 5496
"""
四隅に仮想の原点をつくり、各座標からその原点へのマンハッタン距離を計測する。
"""

N = int(input())
XY = [list(map(int,input().split())) for _ in range(N)]

def measure(originX,originY):
    origin = [originX,originY]
    XY.sort(key=lambda x: abs(x[0]-origin[0])+abs(x[1]-origin[1]))
    return abs(XY[-1][0]-origin[0])+abs(XY[-1][1]-origin[1])-(abs(XY[0][0]-origin[0])+abs(XY[0][1]-origin[1]))

ans = 0
ans = max(ans,measure(1,1))
ans = max(ans,measure(1,10**9))
print(ans)

Measured memory_bytes: 124508
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <string>
using namespace std;
 
int main() {
  // input
    int n;cin >> n;
    string s,t; cin >> s >> t;
    string sn,tn;
    if(s == t){
      cout << n << endl;
      return 0;
    }

  // compute

  for(int i = 0; i < n ; i++) {
    sn = s.substr(i + 1);
    tn = t.erase(n - 1 - i);
      if(sn == tn ){
          cout << 2 * n - sn.size() << endl;
          return 0;
      }
  }

  // output  
    cout << 2 * n << endl;

}


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

using namespace std;
typedef long long ll;
//typedef unsigned long long ll;

//#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
//#define sz(x) ll(x.size())
//typedef pair<ll, int> P;
//typedef pair<ll, ll> P;
const double INF = 1e10;
//const ll INF = LONG_LONG_MAX / 10;
//const ll INF = 1e15;
const ll MINF = LONG_LONG_MIN;
//const int INF = INT_MAX / 10;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

//ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);

typedef pair<double, double> P;

void print_line(vector<int> &line) {
    if (line.size() == 0ll) {
        cout << endl;
        return;
    }
    for (ll i = 0; i < line.size(); i++) {
        cout << line[i];
        if (i == line.size() - 1) cout << endl;
        else cout << ' ';
    }
}


typedef priority_queue<long long, vector<long long>, greater<long long>> PQ_ASK;
const int mod = 1000000007;

int dfs(int from, int turn, vector<vector<int>> &tos, set<int> &used, vector<vector<int>> &ans) {
    used.erase(from);
    ans[from][1] = turn;
    for (int to : tos[from]) {
        if (contain(used, to)) {
            turn = dfs(to, turn + 1, tos, used, ans);
        }
    }
    turn++;
    ans[from][2] = turn;
    return turn;
}

int main() {
    int n;
    cin >> n;
    vector<vector<int>> tos(n);
    rep(i, n) {
        int j, k;
        cin >> j >> k;
        j--;
        rep(l, k) {
            int m;
            cin >> m;
            m--;
            tos[j].push_back(m);
        }
    }

    vector<vector<int>> ans(n, vector<int>(3, 0));

    rep(i, n) sort(tos[i].begin(), tos[i].end());
    set<int> used;
    rep(i, n) used.insert(i);
    int turn = 0;
    while (!used.empty()) turn = dfs(*used.begin(), turn + 1, tos, used, ans);

    rep(i, n) ans[i][0] = i + 1;
    rep(i, n) print_line(ans[i]);
}




Measured memory_bytes: 3128
import itertools

n,m,q = map(int,input().split())

lr = [[0 for i in range(n)] for j in range(n)]
for _ in range(m):
    l,r = map(int,input().split())
    lr[l-1][r-1] += 1

acc_lr = []
for l in lr:
    acc_lr.append([0] + list(itertools.accumulate(l)))

for _ in range(q):
    l,r = map(int,input().split())
    ans = 0
    for ls in acc_lr[l-1:r]:
        ans += ls[r] - ls[l-1]
    print(ans)


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

main(){
  int N;
  cin >> N;
  char S[10000];
  for(int ii=0;ii<10000;ii++){
    S[ii]=-1;
  }
  cin >> S;
  for(int ii=0;ii<N;ii++){
    for(int ii=0;ii<10000;ii++){
      if(S[ii]==-1)break;
      else if(65<=S[ii] && S[ii]<90)S[ii]++;
      else if(S[ii]==90)S[ii]-=25;
      if(97<=S[ii] && S[ii]<122)S[ii]++;
      if(S[ii]==122)S[ii]-=25;
    }
    //cout << S << endl;
    }
  //S[4]++;
  cout << S << endl;
  //  cout << (int)S[0] << endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h> 
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;


int main(){
    int r;
    cin >> r;
    int a = 3 * r * r;
    cout << a;
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>

using namespace std;

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

int main(){
    int n;
    cin >> n;
    int l[n], r[n];
    rep(i, n) cin >> l[i] >> r[i];
    int ans = 0;
    rep(i, n) ans += r[i] - l[i] + 1;
    cout << ans << endl;
}

Measured memory_bytes: 256
x,n = map(int,input().split())
if n != 0:
    p = list(map(int,input().split()))

    num = []
    for i in range(-49,150):
        num.append(abs(x-i))

    for i in range(n):
        num[p[i]+49] = 1000

    min = 1000
    ansnum = -49
    for i in range(len(num)):
        if min > num[i]:
            min = num[i]
            ansnum = i-50

    print(ansnum+1)
else:
    print(x)

Measured memory_bytes: 9084
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE BinaryLiterals #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE NumDecimals #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}

import Control.Applicative
import Control.Arrow
import Control.Monad
import Control.Monad.ST

import Data.Char
import Data.List
import Data.Maybe

import qualified Data.ByteString.Char8 as B

import Data.Array.ST.Safe
import Data.STRef

import Debug.Trace
import Text.Printf

readInt = readLn :: IO Int
readInteger = readLn :: IO Integer
readInts = map ( fst . fromJust . B.readInt ) . B.words <$> B.getLine
readIntegers = map ( fst . fromJust . B.readInteger ) . B.words <$> B.getLine

which a b f = if f then a else b
mp [ a, b ] = ( a, b )

modifyArray a i f = writeArray a i =<< f <$> readArray a i

main = do
	s <- getLine
	let
		h = length s `div` 2
		a = take h s
		b = take h $ reverse s
	print $ length $ filter id $ zipWith (/=) a b

Measured memory_bytes: 380
n=int(input())
a=list(map(int,input().split()))
mod=10**9+7
c=[0]*60
for aa in a:
  for i in range(60):
    if aa&(1<<i):c[i]+=1
ans=0
for i,cc in enumerate(c):
  ans+=cc*(n-cc)*2**i
  ans%=mod
print(ans)




Measured memory_bytes: 122936
#include <iostream>

int main()
{
  int a, b, result;
  scanf("%d %d", &a, &b);
  result = a + b;
  if (result < a - b)
  {
    result = a - b;
  }
  if (result < a * b)
  {
    result = a * b;
  }
  printf("%d", result);
  return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#define ll long long
#define rep(i,n) for(ll i=0;i<n;++i)
#define mod 1000000007
using namespace std;

ll mpow(ll a,ll b){
    if(b == 0) return 1;
    if(b == 1) return a%mod;
    if(b%2 == 0) return mpow((a*a)%mod,b/2)%mod;
    else return (a*mpow((a*a)%mod,(b-1)/2)%mod)%mod;
}




int main(void){
    ll n,k;
    cin>>n>>k;
    ll ans = 0;
    vector<ll> d(k+1,0);
    
    for(ll i = k;i>=1;--i){
        d[i] = mpow(k/i,n);
        for(ll j=i+i;j<=k;j+=i){
            d[i] -= d[j];
            if(d[i]<0) d[i] += mod;
        }
        //cout<<d[i]<<endl;
    }
    rep(i,k+1) ans = (ans+d[i]*i)%mod;
    cout<<ans<<endl;
}


Measured memory_bytes: 4060
#include<bits/stdc++.h>
#include<cmath>
using namespace std;
typedef vector<int> vi;
typedef vector< vi > vvi;
typedef pair<int, int> pii;
typedef long long ll;

int v(int x, int y, int z){
//  cout << "set:" << x << ","<< y << "," << z << endl;
  int ans = x*x + x*y + y*y + y*z + z*z + z*x;
  return ans;
}

void f(int N, vi &a){
  int X, Y, Z;
  for (int X = 1; X < sqrt(N); X++) {
    for (int Y = 1; Y < sqrt(N); Y++) {
      for (int Z = 1; Z < sqrt(N); Z++) {
        int ans = v(X,Y,Z);
        //cout <<"ans:" << ans << endl;
        a[ans-1] ++;
      }
    }
  }
  return;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  bool cont = true;
  cin >> N ;
  vi a(100000);
  f(N, a);
  for (int i = 0; i < N; i++) {
    cout << a.at(i) << endl;
  }
}

Measured memory_bytes: 3736
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <stack>
#include <queue>
#include <utility>
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(s) (s).begin(),(s).end()
#define rep(i,l,n) for(int i=l;i<n;i++)
#define rer(i,l,n) for(int i=l;i<=n;i++)
#define rrep(i,n,l) for(int i=n-1;i>=l;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define pb(a) push_back(a);
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vi;
//typedef vector<lint> vli;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

vi v;
int n, l;

string changes(int k) {
	stringstream ss;
	string s;
	ss << k;
	ss >> s;
	return s;
}

int changei(string s) {
	stringstream ss;
	int k;
	ss << s;
	ss >> k;
	return k;
}

int main() {
	while (cin >> n >> l) {
		if (n == 0 && l == 0)break;
		int d[100] = {};
		bool f = 0;
		d[0] = n;
		rep(i,1,21) {
			string s = changes(n);
			while (s.size()<l){
				s += "0";
			}
			string t1 = s;
			sort(ALL(t1));
			string t2 = t1;
			reverse(ALL(t2));
			int mi, ma;
			ma = changei(t2);
			mi = changei(t1);
			n = ma - mi;
			d[i] = n;
			//o(n);
			rep(j, 0, i) {
				if (d[i] == d[j]) {
					cout << j << " " << d[i] << " " << i - j << endl;
					f = 1;
					break;
				}
			}
			if (f)break;
		}
	}
}

Measured memory_bytes: 1248
import sys
input = sys.stdin.readline  # NOQA


def main():
    N, K = map(int, input().split())
    cnt = [0] * (1 + 10**5)

    for i in range(N):
        a, b = map(int, input().split())
        cnt[a] += b

    total = 0
    for i in range(1, 10**5 + 1):
        total += cnt[i]
        if total >= K:
            ans = i
            break
    print(ans)


if __name__ == "__main__":
    main()


Measured memory_bytes: 5736
#include<stdio.h>
#include<string.h>
int main(){
	int a,b,i,j,h=0,l=0;
	char s[101],p[101];

	scanf("%s",s);
	scanf("%s",p);

	a=strlen(s);
	b=strlen(p);

	for(i=0;i<a;i++){
		if(s[i]==p[0]){
			h=0;
		}
		for(j=0;j<b;j++){
			if(s[(j+i)%a]!=p[j]){
				h=1;
			}
		}
		if(h==0){
			l=1;
		}
	}
	if(l==1){
		printf("Yes\n");
	}
	else{
		printf("No\n");
	}
	return 0;
}

Measured memory_bytes: 524
// A - Beginning
#include <bits/stdc++.h>
using namespace std;

int main(){
	char a,b,c,d; cin>>a>>b>>c>>d;
	map<char,bool> m;
	m[a] = m[b] = m[c] = m[d] = true;
	string s ="1974";
	for(auto c:s) if(m[c]==false){ puts("NO"); return 0; }
	puts("YES");
}

Measured memory_bytes: 256
object Main extends App {
  val (_, tLimit) = readLine.split(" ").map(_.toInt) match {
    case Array(nn, tt) => (nn, tt)
  }

  val x: Int = Stream.continually(readLine)
    .takeWhile(_ != null)
    .map(_.split(" ").map(_.toInt) match { case Array(c, t) => (c, t)})
    .filter(_._2 <= tLimit)
    .foldLeft(Int.MaxValue)((acc, h) => if (acc > h._1) h._1 else acc)

  println(if (x == Int.MaxValue) "TLE" else x)
}

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

namespace P0031_Weight
{
    class Program
    {
        static void Main(string[] args)
        {
            var results = new List<string>();

            while (true)
            {
                var line = Console.ReadLine();
                if (line == null) break;
                var weight2 = Convert.ToString(int.Parse(line), 2);

                var result = new List<string>();

                for (int i = 0; i < weight2.Length; i++) 
                {
                    if (weight2.Substring(weight2.Length -1 - i,1) == "1" )
                    {
                        result.Add(Math.Pow((double)(2), (double)i).ToString());
                    }
                }

                results.Add(string.Join(" ", result.ToArray()));
            }

            foreach(var r in results)
            {
                Console.WriteLine(r);
            }

        }
    }
}

Measured memory_bytes: 5856
#include<bits/stdc++.h>
using namespace std;
#define For(a,b,c) for(int a=b;a<=c;++a)
#define Dor(a,b,c) for(int a=b;a>=c;--a)
typedef long long ll;
int n;
ll a[100009],b[100009],sum;
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]),a[i]*=2;
	for(int i=1;i<=n;i++)sum+=a[i];
	sum/=2;
	ll pre=0;
	for(int i=1;i<n;i+=2){
		pre+=a[i];
	}
	ll tmp=sum-pre;
	b[n]=tmp;
	b[1]=a[n]-tmp;
	
	for(int i=2;i<=n-1;i++){
		b[i]=a[i-1]-b[i-1];
	}
	for(int i=1;i<=n;i++)printf("%lld ",b[i]);
	
	
}

Measured memory_bytes: 2816
package main

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

func main() {
	var n int
	fmt.Scan(&n)
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	ls := []int{}
	for _, s := range strings.Split(sc.Text(), " ") {
		num, _ := strconv.Atoi(s)
		ls = append(ls, num)
	}

	length := len(ls)
	ct := 0

	for i, x := range ls {
		if i >= length - 2 {
			break
		}
		for j, y := range ls[i+1:] {
			if i + j + 1 >= length - 1 {
				break
			}
			if x == y {
				continue
			}
			for _, z := range ls[i+j+2:] {
				if y == z || z == x {
					continue
				}
				if x + y > z && y + z > x && z + x > y {
					ct++
				}
			}
		}
	}
	fmt.Println(ct)
}


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

#define LL long long
#define VI vector<int>
#define VVI vector<vector<int>>
#define VVVI vector<vector<vector<int>>>
#define VB vector<bool>
#define VL vector<long long>
#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

const std::string YES = "YES";
const std::string Yes = "Yes";
const std::string NO = "NO";
const std::string No = "No";

bool inside(int y, int x, int H, int W) { return 0 <= y && y < H && 0 <= x && x < W; }

// 4近傍（右, 下, 左, 上）
const std::vector<int> dy = { 0, -1, 0, 1 };
const std::vector<int> dx = { 1, 0, -1, 0 };

using namespace std;


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

    while (true) {
        int m;
        cin >> m;
        if (m == 0) { break; }
        vector<int> A(m), B(m);
        FOR(i, 0, m) {
            cin >> A[i] >> B[i];
        }

        // dp[i][j] = i番目まで見たときに合計jを作る組み合わせの数
        vector<vector<LL>> dp(m + 1, vector<LL>(10000 + 1, 0));
        dp[0][0] = 1;

        FOR(i, 0, A.size()) {
            for (int j = dp[0].size() - 1; j >= 0; --j) {
                FOR(k, 0, B[i] + 1) {

                    // use
                    int num = A[i] * k;
                    if (j + num < dp[0].size()) {
                        dp[i + 1][j + num] += dp[i][j];
                    }
                }
            }
        }

        int g;
        cin >> g;
        FOR(i, 0, g) {
            int n;
            cin >> n;
            cout << dp[m][n] << endl;
        }
    }

    return 0;
}


Measured memory_bytes: 3556
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MOD 1000000007
typedef long long ll;
typedef std::pair<int,int> ii;
using namespace std;
int main(){
	string n;cin >> n;
	if(n.length() < 3){cout << "No";return 0;}
	
	for(int i = 0;i<n.length()-2;i++){
		string temp = n.substr(i,3);
		if(temp[0] == temp[1] && temp[1] == temp[2]){
			cout << "Yes";
			return 0;
		}
	}cout << "No";
}

Measured memory_bytes: 764
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <sstream>
#include <math.h>
#include <queue>
#include <map>
#include <set>
#include <ios>     // std::left, std::right
#include <iomanip> // std::setw(int), std::setfill(char)
using namespace std;


int main(){
int H,W,N;
cin>>H>>W>>N;
int sr,sc;
cin>>sr>>sc;
char S[N],T[N];
cin>>S>>T;
//X回めにここにいればOKという場所を上限、下限、左右はしで定義
int U,D,R,L;
U=1;
D=H;
R=W;
L=1;
bool judge=true;
for(int i=N-1;i>=0;--i){
  //後攻
  if(i<N-1){
  if(T[i]=='U'){
    D=min(H,D+1);
  }
  else if(T[i]=='D'){
    U=max(1,U-1);
  }
  else if(T[i]=='R'){
    L=max(1,L-1);
  }
  else if(T[i]=='L'){
    R=min(W,R+1);
  }
}
  //先行
  if(S[i]=='U'){
    U++;
  }
  else if(S[i]=='D'){
    D--;
  }
  else if(S[i]=='L'){
    L++;
  }
  else if(S[i]=='R'){
    R--;
  }

  if(L>R || U>D){
    judge=false;
  }
}
if(sc<L || sc>R || sr<U || sr>D){
  judge=false;
}
if(judge){
  cout<<"YES"<<"\n";
}
else{
  cout<<"NO"<<"\n";
}
return 0;
}


Measured memory_bytes: 640
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#define REP(i, n) for(int i = 0; i < n; ++i)
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    int N, K;
    cin >> N >> K;
    int A[210000];
    REP(i, N) {
        cin >> A[0];
        ++ A[A[0]];
    }
    sort(A + 1, A + N + 1);
    int ans = 0;
    for (int i = 1; i < N - K + 1; ++i){
        ans += A[i];
    } 
    cout << ans << '\n';
    return 0;
}

Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define Rep(i,n) for(int i=1;i<=(n);++i)
using LL = long long;
using ULL = unsigned long long;

char s[200123];
LL cnt[2019]{1};

int main()
{
	scanf("%s", s);
	int n = strlen(s);
	int temp = 0, base = 1;
	for(int i = n-1; i >= 0; --i) {
		temp = (temp + (s[i] - '0') * base) % 2019;
		++cnt[temp];
		base = base * 10 % 2019;
	}
	LL ans = 0;
	rep(i, 2019) ans += cnt[i] * (cnt[i] - 1) / 2;
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 3836
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main(){
  int64_t n;
  cin >> n;
  vector<int64_t> a(n),b(n);
  rep(i,n){
    cin >> a.at(i);
    b.at(i)=a.at(i);
  }
  sort(b.begin(),b.end());
  int64_t p=b.at(n/2-1),q=b.at(n/2);
  rep(i,n){
    if(a.at(i)<=p){
      cout << q << endl;
    }else{
      cout << p << endl;
    }
  }
}

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

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

  int n, l;
  cin >> n >> l;
  vector<string> aa(110);
  rep(i, n) {
    cin >> aa[i];
  }

  sort(aa.begin(), aa.end());

  string ans;
  for (auto s: aa) {
    ans += s;
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<iomanip>
#include<cstring>
#include<string>
#include<set>

#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

using namespace std;
using ll = long long int;

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

int main(void) {
    int h, w; cin >> h >> w;
    int k; cin >> k;
    vector<vector<char>> m(h, vector<char>(w));
    REP(i, h) {
        REP(j, w) {
            cin >> m[i][j];
        }
    }
    int ans = 0;
    for (int bit1 = 0; bit1 < (1 << h); bit1++) {
        for (int bit2 = 0; bit2 < (1 << w); bit2++) {
            vector<vector<bool>> b(h, vector<bool>(w));
            for (int i = 0; i < h; i++) {
                if (bit1 & (1 << i)) {
                    for (int j = 0; j < w; j++) b[i][j] = true;
                }
            }
            for (int i = 0; i < w; i++) {
                if (bit2 & (1 << i)) {
                    for (int j = 0; j < h; j++) b[j][i] = true;
                }
            }
            int tmp = 0;
            REP(i, h) {
                REP(j, w) {
                    if (b[i][j] == false && m[i][j] == '#') tmp++;
                }
            }
            if (tmp == k) ans++;
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 3600
#include <iostream>
#include <vector>

using namespace std;

int main() {
	int n;

	cin >> n;

	vector<int> a(n);
	vector<int> b(n);

	for (int i = 0; i < n; i++) {
		int p;

		cin >> p;
		p--;

		a[p] = (p + 1) * (n + 1) + i;
		b[p] = (n - p + 1) * (n + 1);
	}

	for (int i = 0; i < n; i++) {
		cout << a[i] << (i == n - 1 ? '\n' : ' ');
	}

	for (int i = 0; i < n; i++) {
		cout << b[i] << (i == n - 1 ? '\n' : ' ');
	}

	return 0;
}

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

public class Main 
{ 

	 static FastReader sc=new FastReader(); 
	  public static void main(String[] args) 
    {
    	int n=i();
    	int x=i();
    	int t=i();
    	if(n%x==0){
    	    System.out.println(t*(n/x));
    	}
    	else{
    	    int y=n/x+1;
    	    System.out.println(y*t);
    	}
        	
        	
			
	
	}    	
    	
   static void input(int A[]) {
	   for(int i=0;i<A.length;i++) {
		   A[i]=sc.nextInt();
	   }
   }
     static int i() {
    	 return sc.nextInt();
     }
     static String s() {
    	 return sc.next();
     }
     static long l() {
    	 return sc.nextLong();
     }
     public static String rev(String str){  
    	    char ch[]=str.toCharArray();  
    	    String rev="";  
    	    for(int i=ch.length-1;i>=0;i--){  
    	        rev+=ch[i];  
    	    }  
    	    return rev;  
    	}  
        static void sort(int[] arr){
        int n = arr.length;
        Random rnd = new Random();
        for(int i=0; i<n; ++i){
            int tmp = arr[i];
            int randomPos = i + rnd.nextInt(n-i);
            arr[i] = arr[randomPos];
            arr[randomPos] = tmp;
        }   
        Arrays.sort(arr);
}
  
        
        
    static class FastReader 
    { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastReader() 
        { 
            br = new BufferedReader(new
                     InputStreamReader(System.in)); 
        } 
  
        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
  
        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 
  
        long nextLong() 
        { 
            return Long.parseLong(next()); 
        } 
  
        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 
    } 
} 






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

int c[9];

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

    int n;cin>>n;

    for(int i=0;i<n;i++)
    {
        int a;cin>>a;
        int num=min(a/400,8);
        c[num]++;
    }

    int ans=0;

    for(int i=0;i<8;i++)if(c[i]>0)ans++;

    cout<<max(ans,1)<<" "<<ans+c[8]<<"\n";

    return 0;
}


Measured memory_bytes: 3624
#include<iostream>
#include<vector>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
typedef boost::multiprecision::cpp_int ulli;

ulli gcd(ulli a, ulli b) {
    if (a > b) {
        ulli temp = a;
        a = b;
        b = temp;
    }
    if (b % a == 0) {
        return a;
    }
    else {
        return gcd(a, b % a);
    }
}

int main() {
    long int n;
    cin >> n;
    ulli ssk = 1;
    vector<ulli> v;
    for (long int i = 0; i < n; i++) {
        ulli temp;
        cin >> temp;
        v.push_back(temp);
        ssk *= temp / (gcd(temp, ssk));
    }
    ulli res = 0;
    for (long int i = 0; i < n; i++) {
        res += ssk / v.at(i);
    }
    res %= 1000000007;
    cout << res << endl;
    return 0;
}


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

int main() {
  int a, b, c = 0;
  string s;
  cin >> a >> b;
  for (int i = a; i <= b; ++i) {
    s = to_string(i);
    REP(i, s.length()) {
      if (s[i] != s[s.length() - i - 1]) break;
      if (i == s.length() - 1) ++c;
    }
  }
  cout << c << endl;
  return 0;
}

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

int main()
{
    int n;
    cin >> n;
    int ans = 0, m = 0;
    int a[100];
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        if (m < a[i])
            m = a[i];
        ans += a[i];
    }
    cout << ans - m / 2 << endl;
}


Measured memory_bytes: 256
#include<stdio.h>

typedef struct{
  char c;
  int e;
}trump;

void bubblesort(trump *);
void selectionsort(trump *);
int isstable(trump *,trump *);

int n;

int main(){

  int i, j;

  scanf("%d", &n);

  trump t[n],a[n],b[n];

  for(i=0;i<n;i++) scanf("%*c%c%d", &t[i].c, &t[i].e);

  for(i=0;i<n;i++){
    a[i]=t[i];
    b[i]=t[i];
  }

  bubblesort(a);

  for(i=0;i<n;i++){
    if(i==0) printf("%c%d",a[i].c,a[i].e);
    else printf(" %c%d",a[i].c,a[i].e);
  }printf("\n");
  if(isstable(t,a)==1) printf("Stable\n");
  else printf("Not stable\n");

  selectionsort(b);

  for(i=0;i<n;i++){
    if(i==0) printf("%c%d",b[i].c,b[i].e);
    else printf(" %c%d",b[i].c,b[i].e);
  }printf("\n");
  if(isstable(t,b)==1) printf("Stable\n");
  else printf("Not stable\n");
}

void bubblesort(trump *t){

  int i,j;
  trump tmp;

  for(i=0;i<n;i++){
    for(j=n-1;j>i;j--){
      if(t[j].e<t[j-1].e){
        tmp=t[j];
        t[j]=t[j-1];
        t[j-1]=tmp;
      }
    }
  }return;
}

void selectionsort(trump *t){

  int i,j,min;
  trump tmp;

  for(i=0;i<n;i++){
    min=i;
    for(j=i;j<n;j++){
      if(t[j].e<t[min].e) min=j;
    }
    tmp=t[i];
    t[i]=t[min];
    t[min]=tmp;
  }return ;
}

int isstable(trump *in,trump *out){

  int i,j,a,b;

  for(i=0;i<n;i++){
    for(j=i+1;j<n;j++){
      for(a=0;a<n;a++){
        for(b=a+1;b<n;b++){
          if(in[i].e==in[j].e&&in[i].e==out[b].e&&in[i].c==out[b].c
             &&in[j].e==out[a].e&&in[j].c==out[a].c) return 0;
        }
      }
    }
  }return 1;
}


Measured memory_bytes: 2052
s = list(input())
n = len(s) - 1
key = "F"
for i in range(n):
    if n % 2 == 0:
        key = "T"
        for i in range(n//2):
            if s[i] != s[i+n//2]:
                key = "F"
        if key == "T":
            print(n)
            break
        else:
            n -= 1
    else:
        n -= 1

Measured memory_bytes: 3060
#include <cstdio>

using namespace std;

using int64 = long long;

class Combination {
 public:
  constexpr Combination() { Build(); }
  static constexpr int mod_ = 1000000007;
  static constexpr int n_ = 800000;
  class LookupTable {
   public:
    int64 factorial_[n_];
    int64 inverse_[n_];
    int64 finverse_[n_];
  };
  LookupTable lt{};
  // return nCk
  constexpr int64 Get(int n, int k) const noexcept {
    if (n < k || n < 0 || k < 0) return 0;
    return lt.factorial_[n] * (lt.finverse_[k] * lt.finverse_[n - k] % mod_) %
           mod_;
  }

 private:
  constexpr void Build() noexcept {
    lt.factorial_[0] = 1;
    lt.factorial_[1] = 1;
    lt.finverse_[0] = 1;
    lt.finverse_[1] = 1;
    lt.inverse_[1] = 1;
    for (int i = 2; i < n_; ++i) {
      lt.factorial_[i] = lt.factorial_[i - 1] * i % mod_;
      lt.inverse_[i] = mod_ - lt.inverse_[mod_ % i] * (mod_ / i) % mod_;
      lt.finverse_[i] = lt.finverse_[i - 1] * lt.inverse_[i] % mod_;
    }
  }
};

template<typename T> void fastin(T &num) {
  T c;
  num = 0;
  c = getchar_unlocked();
  for (; (c > 47 && c < 58); c = getchar_unlocked()) num = (num << 3) + (num << 1) + c - 48;
}

Combination com;

int main() {
  int x, y; fastin(x); fastin(y);

  if (x * 2 < y || x > 2 * y || (x + y) % 3 != 0) {
    printf("%d", 0);
    return 0;
  }
 
  int nn = (x + y) / 3;
  int r = x - nn;
 
  int64 ans = com.Get(nn, r);
  printf("%lld", ans);

  return 0;
}


Measured memory_bytes: 128
n = int(input())
h = list(map(int, input().split()))

dp = [0]*n
dp[0] = 0
dp[1] = dp[0] + abs(h[1] - h[0])

for i in range(2, n):
    dp[i] = min(dp[i-1] + abs(h[i] - h[i-1]), dp[i-2] + abs(h[i] - h[i-2]))

print(dp[n-1])

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

int main() {
    std::vector<int> v;
    int n;
    std::cin>>n;
    for(int i=0;i<n;i++){
        int  t;
        std::cin>>t;
        v.push_back(t);
    }
    std::sort(v.begin(),v.end());
    double value=v[0];
    for(int i=1;i<n;i++){
        value = (value+v[i])/2;
    }
    std::cout<<value<<'\n';
    return 0;
}

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

int main() {
double a, b, c;
char op;

do{
std::cin >> a >> op >> b;

if(op == '+'){
c = floor(a + b);
printf("%.0f\n", c);
}
else if(op == '-'){
c = floor(a - b);
printf("%.0f\n", c);
}
else if(op == '*'){
c = floor(a * b);
printf("%.0f\n", c);
}
else if(op == '/'){
c = floor(a / b);
printf("%.0f\n", c);
}
else{
}
} while(!(op == '?'));
}

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

public class Main {

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

    void solve() {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        boolean ch = true;
        long ans = 0;
        long A = sc.nextLong();
        if (A != 0) {
            ch = false;
        }
        long B;
        for (long i = 1; i < N; i++) {
            B = sc.nextLong();
            if (B - A > 1) {
                ch = false;
            }
            if (B - A == 1) {
                ans++;
            }
            if (B <=A) {
                ans += B;
            }
            A = B;
        }
        if (ch) {
            System.out.println(ans);
        } else {
            System.out.println("-1");
        }
    }
}


Measured memory_bytes: 65532
#include<stdio.h>
#define ll long long
ll MIN(ll a, ll b){return a < b ? a : b;}
int main(){
    ll n, a;
    scanf("%lld%lld",&n,&a);
    ll min = a;
    for(int i = 0; i < 5; i++){
        scanf("%lld",&a);
        min = MIN(min, a);
    }
    ll d = 0;
    if(n % min == 0)
        d --;
    ll ans = n / min + 5 + d;
    printf("%lld\n",ans);
    return 0;
}

Measured memory_bytes: 128
n = int(input())
s = [input() for i in range(n)]
l = [0] * 5
for i in range(n):
  if s[i][0] == "M":
    l[0] += 1
  elif s[i][0] == "A":
    l[1] += 1
  elif s[i][0] == "R":
    l[2] += 1
  elif s[i][0] == "C":
    l[3] += 1
  elif s[i][0] == "H":
    l[4] += 1
ans = 0
for j in range(3):
  for k in range(j+1, 4):
    for i in range(k+1, 5):
      ans += l[j] * l[k] * l[i]
print(ans)

Measured memory_bytes: 16136
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

void upd(int &x, int y) {
  x = min(x, y);
}

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

  vector<vector<int>> g(n);
  for (int i = 1; i < n; i++) {
    int p;
    cin >> p;
    g[p - 1].push_back(i);
  }
  vector<int> x(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }

  vector<int> y(n, 1e9);

  for (int u = n - 1; u >= 0; u--) {
    vector<int> dp(5001, 1e9);
    dp[0] = 0;
    for (int v : g[u]) {
      vector<int> dp1(5001, 1e9);
      for (int i = 0; i <= 5000; i++) {
        if (i + x[v] <= 5000) upd(dp1[i + x[v]], dp[i] + y[v]);
        if (i + y[v] <= 5000) upd(dp1[i + y[v]], dp[i] + x[v]);
      }
      dp = dp1;
    }
    for (int i = 0; i <= x[u]; i++) {
      y[u] = min(y[u], dp[i]);
    }
    if (y[u] == 1e9) {
      puts("IMPOSSIBLE");
      return 0;
    }
  }
  puts("POSSIBLE");
}

Measured memory_bytes: 512
N = int(input())
S = input()

r = S.count('R')
g = S.count('G')
b = S.count('B')
max = r * g * b 

count = 0
for i in range(N-2):
	for j in range(i+1, N-1):
		k = 2 * j - i
		if k > N - 1:
			break
	
		if S[i] == S[j] or S[j] == S[k] or S[i] == S[k]:
			continue
		
		count += 1

print(max - count)		

Measured memory_bytes: 9072
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define REPR(i, n) for(int i = (int)(n); i > 0; i--)
#define FOR(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define INF 2e9
#define ALL(v) (v).begin(), (v).end()
template <typename Iter> std::string join(Iter begin, Iter end, std::string const& separator){ std::ostringstream result; if (begin != end){ result << *begin++; }; while (begin != end){ result << separator << *begin++; }; return result.str(); }
using namespace std;
typedef long long ll;
void solve() {
  ll N, M;
  cin >> N >> M;
  vector<ll> L(M), R(M);
  REP(i, M) {
    cin >> L[i] >> R[i];
  }
  vector<ll> sabun(N+1);
  REP(i, M) {
    sabun[L[i]-1] += 1;
    sabun[R[i]] -= 1;
  }
  vector<ll> count(N);
  ll ret = 0;
  count[0] = sabun[0];
  if(count[0] == M) ret += 1;
  FOR(i, 1, N) {
    count[i] = count[i-1] + sabun[i];
    if(count[i] == M) ret += 1;
  }
  cout << ret;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}


Measured memory_bytes: 3456
a, b = map(int, input().split())

ans = 0
for i in a - 1, b:
    if i % 4 == 0:
        ans ^= i
    elif i % 4 == 1:
        ans ^= 1
    elif i % 4 == 2:
        ans ^= i + 1
    else:
        ans ^= 0
        
print(ans)        

Measured memory_bytes: 3060
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <deque>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <unordered_map>
#include <bitset>
#include <sstream>

typedef  long long ll;
#define mop 1000000007
using namespace std;

int main() {
    
    ll n;
    cin >> n;
    ll cnt = 0;
    for(ll i=1;i<=n;i++){
        ll tmp;
        cin >> tmp;
        if(tmp != i)cnt++;
    }
    if(cnt == 0 or cnt == 2){
        cout << "YES" << endl;
    }else{
        cout << "NO" << endl;
    }
}

Measured memory_bytes: 256
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<cstring>
using namespace std;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0' && ch<='9')x=x*10+ch-'0',ch=getchar();
	return x*f;
}
int stack[20];
inline void write(int x)
{
	if(x<0){putchar('-');x=-x;}
	if(!x){putchar('0');return;}
	int top=0;
	while(x)stack[++top]=x%10,x/=10;
	while(top)putchar(stack[top--]+'0');
}
inline void pr1(int x){write(x),putchar(' ');}
inline void pr2(int x){write(x),puts("");}
int a[110];
int main()
{
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);
    int n=read();
    for(int i=1;i<=n;i++)
    {
    	int d=read();
    	for(int j=1;j<=100;j++)a[j]+=(d-j)*(d-j);
    }
    int ans=a[1];
    for(int i=2;i<=100;i++)ans=min(ans,a[i]);
    pr2(ans);
    return 0;
}

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

#define vi vector<int>
#define vl vector<long>
#define vvi vector<vector<int>>
#define vvl vector<vector<long>>
#define pint pair<int, int>
#define plong pair<long, long>

int main() {
    int a;
    cin>>a;
    cout << a + a*a + a*a*a << endl;
}

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

#define int long long
int sum,tmpa;
const long long LINF=1e13;
typedef pair<int,int> P;
typedef pair<P,int> PI;
map<PI,int> m;
int solve(int A,int B,int bit){
  if(A<0)return LINF;
  if(m.count(PI(P(A,B),bit)))return m[PI(P(A,B),bit)];
  if(!bit)return 0;
  if((sum^A^B)&bit){
    if(sum&bit){
      if(B&bit){
        int move=A-bit+1;
        if(~(B+move)&bit)return LINF;
        return m[PI(P(A,B),bit)]=move+solve((A-move)&~bit,(B+move)&~bit,bit>>1);
      }
      int move=bit-B;
      return m[PI(P(A,B),bit)]=move+solve((A-move)&~bit,(B+move)&~bit,bit>>1);
    }
    else{
      if(B&bit)return LINF;
      int move=A-bit+1;
      if((B+move)&bit){
        move=bit-B;
        if(~(A-move)&bit)return LINF;
      }
      return m[PI(P(A,B),bit)]=move+solve((A-move)&~bit,(B+move)&~bit,bit>>1);
    }
  }
  if(sum&bit){
    if(B&bit)return m[PI(P(A,B),bit)]=solve(A&~bit,B&~bit,bit>>1);
    int tmp=solve(A&~bit,B&~bit,bit>>1);
    if(tmp<tmpa)return m[PI(P(A,B),bit)]=tmp;
    int move=max(A-bit+1,bit-B);
    return m[PI(P(A,B),bit)]=move+solve((A-move)&~bit,(B+move)&~bit,bit>>1);
  }
  return m[PI(P(A,B),bit)]=solve(A&~bit,B&~bit,bit>>1);
}
int solve2(int A,int B){
  for(int i=0;i<A;i++)if(((A-i)^(B+i)^sum)==0)return i;
  return -1;
}

signed main(){

  int n;cin>>n;
  int A,B;cin>>A>>B;tmpa=A;
  int a;
  for(int i=2;i<n;i++){
    scanf("%ld",&a);sum^=a;
  }
  int ans=solve(A,B,1LL<<39);
  if(ans>=A)ans=-1;
  cout<<ans<<endl;


  /*
  for(int i=0;i<1000;i++){
    int A=rand()%100000+1,B=rand()%LINF+1;tmpa=A;
    sum=rand()%LINF;
    int ans=solve(A,B,0,1LL<<39),ans2=solve2(A,B);
    if(ans>=tmpa)ans=-1;
    if(ans!=ans2)cout<<A<<" "<<B<<" "<<sum<<" "<<ans<<" "<<ans2<<endl;
  }
  */
}


Measured memory_bytes: 3776
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<long>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvb = vector<vector<bool>>;
using vvc = vector<vector<char>>;
using pii = pair<int, int>;
using pll = pair<long, long>;
#define fix20 cout << fixed << setprecision(20);
#define rep(i,n) for(int i=0; i<(int)(n);i++)
#define MOD 1000000007

int main(){
  long n;
  cin >> n;
  vector<long> a(n);
  long total = 0;
  rep(i,n){
    cin >> a.at(i);
    total += a.at(i);
  }
  long sum = 0;
  long ans = 1e16;
  rep(i,n){
    sum += a.at(i);
    long rest = total - sum;
    ans = min(ans,abs(sum - rest));
  }
  cout << ans << endl;
}


Measured memory_bytes: 1792
H, W = map(int, input().split())
L = ['.' + input() + '.' for _ in range(H)]
L = ['.' * (W + 2)] + L + ['.' * (W + 2)]
A = []
for i in range(1, H + 1):
  S = ""
  for j in range(1, W + 1):
    if L[i][j] == '.':
      t = [L[i-1][j-1], L[i-1][j], L[i-1][j+1], L[i][j+1], L[i+1][j+1], L[i+1][j], L[i+1][j-1], L[i][j-1]]
      S += str(t.count('#'))
    else:
      S += '#'
  A.append(S)
[print(i) for i in A]

Measured memory_bytes: 9144
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
using namespace std;
int dp[2];
int main()
{
    int n;cin>>n;
    string s;cin>>s;
    int ans=0;
    int t=0;
    for(int i=0;i<=n;i++)
    {
        if(s[i]=='R') dp[0]++;
        else dp[1]++;
        //if(i!=0&&s[i]=='R'&&s[i-1]=='W') ans++;
    }
    int ft=0,ed=n-1;
    while(ft<=ed)
    {
        if(s[ft]=='W'&&s[ed]=='R')
        {
            ans++;
            ft++;ed--;
        }
        else if(s[ft]=='W')
        {
            ed--;
        }
        else
            ft++;
    }
    cout<<(min(ans,dp[0]));
    return 0;
}


Measured memory_bytes: 3716
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
const int maxn = 1e5 + 5;
int book[maxn] , a[15];
bool check (int x)
{
    string d = to_string(x);
    for (auto g : d){
        int v = g - '0';
        if (a[v]) return false;
    }
    return true;
}
int main()
{
    ios::sync_with_stdio(false);
    int n , k; cin >> n >> k;
    for (int i = 1 ; i <= k ; i++){
        int v; cin >> v;
        a[v] = 1;
    }
    while (!check(n)) n++;
    cout << n << endl;
    return 0;
}


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

using namespace std;
#define INFS (1LL<<28)
#define INF (1LL<<60)
#define DEKAI 1000000007
#define lp(i,n) for(int i=0;i<n;i++)
#define int long long
#define all(n) n.begin(),n.end()



signed main(){
	int a,b;
	cin>>a>>b;
	int now=1;
	int ans=0;
	while(now<b){
		ans++;
		now+=a-1;
	}
	cout<<ans<<endl;
}

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

int main(void) 
{
    int x;
    cin >> x;
    if(x == 0) cout << "1" <<endl;
    else  cout << "0" << endl;
    return 0;
}

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

int main(){
    int n, m;
    cin >> n >> m;
    if (n % 2 == 1){
        for(int i = 0; i < m; i++){
            cout << 1+i << " " << n-i << endl;
        }
    }
    else{
        for(int i = 0; i < m; i++){
            int half = n/2;
            int k = i /2;
            if (i % 2 == 0){
                cout << 1+k << " " << n-k << endl;
            }
            else{
                int l = k + 1;
                cout << half + l << " " << half - l << endl;
            }
        }
    }
    return 0;
}


Measured memory_bytes: 3648
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
# 約数列挙
def find_divisor(n):
    divisors=[]
    for i in range(1, int(sqrt(n)) + 1):
        if n % i == 0:
            divisors.append(i)
            divisors.append(n // i)
    return divisors


def main():
    N,M = MI()
    A = LI()
    S = sum(A)
    pri = find_divisor(S)
    ans = 0
    for p in pri:
        
        B = [a%p for a in A]
        B.sort()
        B_rev = [b-p if b else 0 for b in B ]
        *cumsum, = accumulate(B)
        cumsum_rev = [0]*(N+1)
        now = 0
        for i in range(N)[::-1]:
            now += B_rev[i]
            cumsum_rev[i] = now
        
        for i in range(N):
            if (cumsum[i] + cumsum_rev[i+1]) % p == 0:
                c = max(cumsum[i], -cumsum_rev[i+1])
                if c<=M:
                    ans = max(ans, p)
    print(ans)
if __name__ == '__main__':
    main()

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

typedef long long lint;
typedef pair<int, int> P;
#define REP(i, x, n) for(int i = x ; i < n ; ++i)
#define rep(i, n) for(int i = 0 ; i < n ; ++i)
#define repr(i, n) for(int i = n - 1 ; i >= 0 ; --i)
#define ALL(x) (x).begin(), (x).end()
#define SORT(x) sort((x).begin(), (x).end())

const int IINF = 1e9 + 10;
const long long LLINF = (long long)1e18 + 10;
const long long MOD = (long long)1e9 + 7;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

int n;
lint x;
lint layer[55], dp[55];

void solve(int level, lint eat, lint &res){
    if(level == 0){
        res += (eat > 0);
        return;
    }
    eat = min(eat, layer[level]);
    lint tmp = layer[level] / 2LL;
    if(eat > tmp){
        res += dp[level - 1] + 1LL;
        solve(level - 1, eat - tmp - 1LL, res);
    }else{
        solve(level - 1, eat - 1LL, res);
    }
    return;
}

int main(){
    cin >> n >> x;
    layer[0] = 1;
    REP(i, 1, n + 1){
        layer[i] = layer[i - 1] * 2LL + 3LL;
    }
    dp[0] = 1;
    REP(i, 1, n + 1){
        dp[i] = dp[i - 1] * 2LL + 1LL;
    }
    lint ans = 0;
    solve(n, x, ans);
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256