akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<(int)(n); i++)
#define rep1(i, n) for(int i=1; i<=(int)(n); i++)
#define rep2(i, n, m) for(int i=n; i<=m; i++)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> wi;
typedef vector<ll> vl;
const ll inf=1LL << 60;
const ll MOD=1000000007;

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

  int n;
  cin >> n;
  int a[n+1];
  rep1(i, n)cin >> a[i];

  int b=1, cnt=0;
  while(b!=2){
    b=a[b];
    cnt++;
    if(cnt==n){
      if(b!=2)cnt++;
      break;
    }
  }

  cout << (cnt<=n ? cnt : -1) << endl;

  return 0;
}

Measured memory_bytes: 4060
#include<iostream>
#include<iomanip>
#include<stdio.h>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<algorithm>
#include<functional>
#include<math.h>
#include<unordered_set>
using namespace std;

int main() {
	int a, b, c;

	cin >> a >> b >> c;

	if (a + b >= c)cout << "Yes" << endl;
	else cout << "No" << endl;

	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, s, n) for (int i = (s); i < (n); ++i)
#define rrep(i, n, g) for (int i = (n)-1; i >= (g); --i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define len(x) (int)(x).size()
#define dup(x, y) (((x) + (y)-1) / (y))
#define pb push_back
#define Field(T) vector<vector<T>>
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, k;
  string s;
  cin >> n >> k >> s;
  s[k - 1] = tolower(s[k - 1]);
  cout << s << endl;
}

Measured memory_bytes: 3628
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long M = sc.nextLong();

		List<Drink> drinkList = new ArrayList<>();
		sc.nextLine();
        for (int i = 0; i < N; i++) {
        	Drink drink = new Drink(sc.nextInt(), sc.nextInt());
        	drinkList.add(drink);
        	sc.nextLine();
        }

        long count = 0;
        long ansPrice = 0;
        Comparator<Drink> comparator = Comparator.comparing(Drink::getA);
        drinkList = drinkList.stream().sorted(comparator).collect(Collectors.toList());
        for(Drink drink : drinkList){
        	if(M > count + drink.B) {
        		count += drink.B;
        		ansPrice += drink.A * drink.B;
        	}else{
        		ansPrice += drink.A * (M- count);
        		break;
        	}
        }

        System.out.println(ansPrice);

	}


}

class Drink {

	Drink(long a, long b){
		this.A = a;
		this.B = b;
	}

	long A;
	long B;

	long getA(){
		return this.A;
	}

}


Measured memory_bytes: 157624
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define mt make_tuple
#define MOD 1000000007
#define fo(i,a,b) for(i=a;i<b;i++)
#define foe(i,a,b) for(i=a;i<=b;i++)
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector <long long int>
#define pii pair <int,int>
#define pll pair <long long int, long long int>
#define vpii vector< pair<int,int> >
#define vpll vector < pair <long long int,long long int> >
#define boost ios::sync_with_stdio(false); cin.tie(0)
using namespace std;
const int inf = 1e9 + 5;
const ll inf64 = 1e18 + 5;
int main()
{
	boost;
	string s;
	cin >> s;
	if(s == "ABC") cout << "ARC";
	else cout << "ABC";
}


Measured memory_bytes: 3612
#![allow(unused_imports)]
use std::io;
use std::cmp::*;
use std::collections::*;
#[allow(dead_code)]
const M: i64 = 1e9 as i64 + 7;

fn read_line() -> String {
    let stdin = io::stdin();
    let mut buf = String::new();
    stdin.read_line(&mut buf).unwrap();
    buf
}
#[allow(dead_code)]
fn read<T>() -> T where T: std::str::FromStr, T::Err : std::fmt::Debug {
    read_line().trim().parse().unwrap()
}
#[allow(dead_code)]
fn readv<T>() -> Vec<T> where T: std::str::FromStr, T::Err : std::fmt::Debug {
    read_line().split(' ').map(|a| a.trim().parse().unwrap()).collect()
}

use std::time::Instant;
fn main() {
    let now = Instant::now();
    let _ = read::<usize>();
    let aa = readv::<usize>();
    let bb = readv::<usize>();
    let x = aa.iter().fold(0, |acc, &a| acc ^ a);
    let (mut aa, bb): (Vec<_>, Vec<_>) = aa.iter().zip(bb.iter()).filter(|&(&a, &b)| a != b).unzip();
    let n = aa.len();
    aa.push(x);
    let mut nums = HashMap::with_capacity(n + 1);
    for &a in &aa {
        *nums.entry(a).or_insert(0) += 1;
    }
    // let mut nums: HashMap<_, i32> = aa.iter()
    //     .fold(HashMap::with_capacity(n + 1), |mut acc, &a| { *acc.entry(a).or_insert(0) += 1; acc });
    for &b in &bb {
        *nums.entry(b).or_insert(0) -= 1;
        if nums[&b] < 0 {
            println!("{}", -1);
            return;
        }
    }

    let mut dests = bb.iter().enumerate()
        .fold(HashMap::new(), |mut acc, (a, b)| { (acc.entry(b).or_insert(Vec::new())).push(a); acc });
    let mut q: VecDeque<_> = std::iter::once(n).collect();
    let mut rems: HashSet<_> = (0..n + 1).collect();
    let mut ans = n;
    #[cfg(local)] println!("{}: {:?}", "(aa, bb)", (&aa, &bb));
    if now.elapsed().as_secs() >= 1 {
        std::process::exit(1);
    }
    while !rems.is_empty() {
        while let Some(s_idx) = q.pop_front() {
            if !rems.remove(&s_idx) { continue; }
            let s_num = aa[s_idx];
            if let Some(destv) = dests.remove(&s_num) {
                for dest in destv {
                    q.push_back(dest);
                }
            }
        }
        if let Some(&rem) = rems.iter().next() {
            q.push_back(rem);
            ans += 1;
        }
    }
    println!("{}", ans);
}


Measured memory_bytes: 27576
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int n,k;
    cin>>n>>k;

    int a,b;
    map<int,int>mp;
    for (int i=0; i<k; ++i)
    {
        cin>>a;
        for (int i=0; i<a; ++i)
        {
            cin>>b;
            mp[b]=1;
        }
    }
    int ans=0;
    for (int i=1; i<=n; ++i)
    {
        if (mp[i]==0)
            ++ans;
    }
    cout<<ans;








}


Measured memory_bytes: 3644
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

"""
URL: https://atcoder.jp/contests/dp/tasks/dp_l
L - Deque / 
実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 
100 点

問題文
太郎君と次郎君が次のゲームで勝負します。
最初に、数列 a=(a1,a2,…,aN) が与えられます。 
a が空になるまで、二人は次の操作を交互に行います。 
先手は太郎君です。a の先頭要素または末尾要素を取り除く。 
取り除いた要素を x とすると、操作を行った人は x 点を得る。
ゲーム終了時の太郎君の総得点を X、次郎君の総得点を Y とします。 
太郎君は X−Y を最大化しようとし、次郎君は X−Y を最小化しようとします。

二人が最適に行動すると仮定したとき、X−Y を求めてください。

解説:
ただの区間DP
しかし、Pythonはカスなので工夫が必要
知見
    ====================================================================
    |dp[i][j]に-dp[i][j-1] or -dp[i+1][j]の加算結果を与えれば             |
    |    右辺の式はすべて符号反転するので－側の最小を目指すというのと         |
    |        ＋の最大を目指すというのを満たすのでその区間における            |
    |           プレイヤーの最善スコアが出る                               |
    ====================================================================|

"""

#solve
def solve():
    n = II()
    a = LI()
    dp = [[0]*n for i in range(n)]
    for i in range(n):
        dp[i][i] = a[i]
    for i in range(n - 2, -1, -1):
        dpi = dp[i]
        dpi1 = dp[i + 1]
        ai = a[i]
        for j in range(i + 1, n):
            dpi[j] = max(ai-dpi1[j], a[j]-dpi[j-1])
    print(dp[0][n - 1])
    return


#main
if __name__ == '__main__':
    solve()


Measured memory_bytes: 112348
#include<bits/stdc++.h>                                                                                                                                                                                     
using namespace std;
int main()
{
  int N,M;
  int i;
  cin >> N >> M;
  vector<int> count(N);
  vector<int> a(M);
  vector<int> b(M);
  for(i=0; i<M; i++){
    cin >> a.at(i);
    cin >> b.at(i);
  }
  for(i=0; i<M; i++){
    count[a.at(i)-1]++;
    count[b.at(i)-1]++;
  }
  for(i=0; i<N; i++)
    cout << count[i] << endl;
}


Measured memory_bytes: 256
#include <cassert>
#include <cstdio>
#include <algorithm>
#include <vector>
static const int MAXN = 100005;
static const int INF = 0x3fffffff;

int n, k;
std::vector<int> e[MAXN];
int val[MAXN];

bool feasible = true;
int min[MAXN], max[MAXN], parity[MAXN];

void dfs_traverse(int u, int p = -1)
{
    min[u] = -INF, max[u] = INF;
    parity[u] = (val[u] == INF ? -1 : (val[u] & 1));
    for (std::vector<int>::iterator v = e[u].begin(); v != e[u].end(); ++v) if (*v != p) {
        dfs_traverse(*v, u);
        min[u] = std::max(min[u], min[*v] - 1);
        max[u] = std::min(max[u], max[*v] + 1);
        if (parity[*v] != -1) {
            if (parity[u] == -1) parity[u] = parity[*v] ^ 1;
            else if (parity[u] == parity[*v]) feasible = false;
        }
    }
    if (min[u] > max[u]) feasible = false;
    if (val[u] != INF) { 
        if (val[u] < min[u] || val[u] > max[u]) feasible = false;
        min[u] = max[u] = val[u];
    }
    //printf("%d | %d %d %d\n", u, min[u], max[u], parity[u]);
}       
        
void dfs_fill(int u, int p, int val)
{       
    ::val[u] = val;
    for (std::vector<int>::iterator v = e[u].begin(); v != e[u].end(); ++v) if (*v != p) {
        if (val + 1 >= min[*v] && val + 1 <= max[*v]) dfs_fill(*v, u, val + 1);
        else if (val - 1 >= min[*v] && val - 1 <= max[*v]) dfs_fill(*v, u, val - 1);
        else puts("> <"), assert(false);
    }
}       
        
int main()

{
    scanf("%d", &n);
    int u, v;
    for (int i = 0; i < n - 1; ++i) {
        scanf("%d%d", &u, &v); --u, --v;
        e[u].push_back(v);
        e[v].push_back(u);
    }

    for (int i = 0; i < n; ++i) val[i] = INF;
    scanf("%d", &k);
    for (int i = 0; i < k; ++i) {
        scanf("%d%d", &u, &v); --u;
        val[u] = v;
    }

    dfs_traverse(0);

    if (!feasible) puts("No");
    else {
        puts("Yes");
        dfs_fill(0, -1, min[0]);
        for (int i = 0; i < n; ++i) printf("%d\n", val[i]);
    }

    return 0;
}


Measured memory_bytes: 15744
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <math.h>
#include <string>
#include <map>
#include <cstdlib>
using namespace std;

typedef long long ll;

struct PAIR{
    int a;
    int b;
    int c;
    int d;
};

int main() {
    int H,W;
    cin >> H >> W;
    vector< vector<int> > coins(H, vector<int>(W));
    for(int i=0; i<H; i++){
        for(int j=0; j<W; j++){
            cin >> coins[i][j];
        }
    }
    
    vector<PAIR> ans;
    for(int h=0; h<H-1; h++){
        for(int w=0; w<W; w++){
            if(coins[h][w]%2==1){
                coins[h+1][w]++;
                PAIR p={h+1, w+1, h+2, w+1};
                ans.push_back(p);
            }
        }
    }
    for(int w=0; w<W-1; w++){
        if(coins[H-1][w]%2==1){
            coins[H-1][w+1]++;
            PAIR p={H, w+1, H, w+2};
            ans.push_back(p);
        }
    }
    cout << ans.size() << endl;
    for(int i=0; i<ans.size(); i++){
        cout << ans[i].a << ' ' << ans[i].b << ' ' << ans[i].c << ' ' << ans[i].d << endl;
    }
 
    return 0;
}


Measured memory_bytes: 5232
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <functional>
#include <utility>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PI 4*atan(1.0)
typedef long long ll;
typedef long double ld;
using namespace std;
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};

const int MAX=200001;

int rootA[MAX]={},rootB[MAX]={};
int ans[MAX]={};
int findA(int x){
	if(rootA[x]==x)
		return x;
	else
		return (rootA[x]=findA(rootA[x]));
}
 
void uniteA(int x,int y){
	x=findA(x);
	y=findA(y);
	if(x==y)
		return;
	else if(x>y)
		rootA[x]=y;
	else
		rootA[y]=x;
}

int findB(int x){
	if(rootB[x]==x)
		return x;
	else
		return (rootB[x]=findB(rootB[x]));
}
 
void uniteB(int x,int y){
	x=findB(x);
	y=findB(y);
	if(x==y)
		return;
	else if(x>y)
		rootB[x]=y;
	else
		rootB[y]=x;
}

int main(){
	multiset<pair<pair<int,int>,int>> tes;
	int N,K,L;
	cin>>N>>K>>L;
	for(int i=0;i<=N;i++){
		rootA[i]=i;
		rootB[i]=i;
	}
	for(int i=0;i<K;i++){
		int p,q;
		cin>>p>>q;
		uniteA(p,q);
	}
	for(int i=0;i<L;i++){
		int r,s;
		cin>>r>>s;
		uniteB(r,s);
	}
	for(int i=1;i<=N;i++){
		tes.insert(make_pair(make_pair(findA(i),findB(i)),i));
	}
	
	vector<int> vec;
	pair<int,int> pai;
	for(auto it=tes.begin();it!=tes.end();it++){
		if((*it).first==pai){
			vec.push_back((*it).second);
		}
		else{
			for(int i=0;i<vec.size();i++){
				ans[vec[i]]+=vec.size();
			}
			vec.clear();
			vec.push_back((*it).second);
		}

		pai=(*it).first;
	}
	for(int i=0;i<vec.size();i++){
		ans[vec[i]]+=vec.size();
	}
	for(int i=1;i<=N;i++){
		printf("%d ",ans[i]);
	}
	cout<<endl;
	return 0;
}


Measured memory_bytes: 15616
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        int[] a = new int[n];

        for(int i = 0;i<n;i++){
            a[i] = sc.nextInt();
        }
        Arrays.sort(a);
        int answer = a[n-1]-a[0];
        System.out.println(answer);
    }
}

Measured memory_bytes: 21844
#include <bits/stdc++.h>
#define SUM(x) ((x)*(x+1)/2)
using namespace std;
using ll = long long;
using P = pair<int,int>;

int main() {
    mt19937 mt((int)time(0));
    int D;
    cin >> D;
    vector<int> c(26);
    for (int i = 0; i < 26; i++)
        cin >> c[i];
    vector<vector<int>> s(D, vector<int>(26));
    for (int i = 0; i < D; i++)
        for (int j = 0; j < 26; j++)
            cin >> s[i][j];

    vector<int> t(D);
    for (int i = 0; i < D; i++)
      t[i] = mt()%26+1;
    
    int score = 0;
    {
        vector<int> last(26, 0);
        for (int i = 1; i <= D; i++) {
            score += s[i-1][t[i-1]-1];
            last[t[i-1]-1] = i;
            for (int j = 0; j < 26; j++)
                score -= c[j] * (i - last[j]);
        }
    }
    int m = 200000;
    for (int i = 0; i < m; i++) {
        int d = mt() % D + 1, q = mt() % 26 + 1;
        int p = t[d-1];
        t[d-1] = q;
        
        int score2 = 0;
        vector<int> last(26, 0);
        for (int i = 1; i <= D; i++) {
            score2 += s[i-1][t[i-1]-1];
            last[t[i-1]-1] = i;
        for (int j = 0; j < 26; j++)
            score2 -= c[j] * (i - last[j]);
        }
        if (score < score2)
            score = score2;
        else
            t[d-1] = p;
    }
    
    for (int i = 0; i < D; i++)
        cout << t[i] << endl;
    
    //cout << "score:" << score << endl;
    
    return 0;
}


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

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
using ll = long long;
string char_to_string(char val) {
  return string(1, val);
}
int char_to_int(char val) {
  return val - '0';
}
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;
}
int vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance( vec.begin(), itr );
  if (index != vec.size()) { // 発見できたとき
    return 1;
  }
  else { // 発見できなかったとき
    return 0;
  }
}

int main() {
  int H, N; cin >> H >> N;
  vector<int> A(N), B(N); REP(i, N) cin >> A[i] >> B[i];
  int dp[N + 10][H + 10];
  REP(i, N + 10) {
    REP(k, H + 10) {
      dp[i][k] = pow(10, 9) + 7;
    }
  }
  dp[0][0] = 0;
  dp[0][A[0]] = B[0];
  for(int i = 0; i < N; ++i) {
    for(int k = 0; k <= H; ++k) {
      if(k <= A[i]) {
        dp[i + 1][k] = min(dp[i][k], B[i]);
      } else {
        dp[i + 1][k] = min(dp[i][k], dp[i + 1][k - A[i]] + B[i]);
      }
    }
  }
  cout << dp[N][H] << endl;
}

Measured memory_bytes: 43052
#include <iostream>
#include <string>

#include <math.h>
#include <algorithm>
#include <set>
using namespace std;
string board[50];
int h = 0;
int w = 0;
int num = 0;
bool can = true;
//上下左右参照用
int dx[4] = { 1, -1, 0, 0 };
int dy[4] = { 0, 0, 1, -1 };
int main()
{
	
	cin >> h >> w;


	//文字列を入力
	for (int i = 0; i < h; i++) {
		cin >> board[i];

	}

	for (int i = 0; i < h; i++) {

		if (can == false) break;

		for (int j = 0; j < w; j++) {



			if (board[i][j] == '#') {

				//上下左右を調べ、一つもない場合は目標達成不可
				num = 0;
				for (int k = 0; k < 4; k++) {

					int ni = i + dy[k];
					int nj = j + dx[k];

					if (ni < 0 || ni >= h) continue;
					if (nj < 0 || nj >= w) continue;
					if (board[ni][nj] == '#') num++;


				}
				if (num == 0) {

					can = false;
					break;
				}
				else {

					can = true;
				}

			}


		}

	}

	if (can) cout << "Yes" << endl;
	else cout << "No" << endl;



}

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

ll powmod(ll x,ll y){
    ll res=1;
    for(ll i=0;i<y;i++){
        res=res*x%mod;
    }
    return res;
}
int main(){
    ll n;
    cin>>n;
    ll ans=powmod(10,n)-powmod(9,n)-powmod(9,n)+powmod(8,n);
    ans%=mod;
    ans=(ans+mod)%mod;
    cout<<ans<<"\n";
}


Measured memory_bytes: 3576
S=input().rstrip()
N=len(S)
ans=0
for b in range(2**(N-1)):
  a=[]
  p=0
  for i in range(N):
    if b&(1<<i):
      a.append(S[p:i+1])
      p=i+1
  a.append(S[p:])
  ans+=eval("+".join(a))
print(ans)


Measured memory_bytes: 3060
import java.util.*;
import static java.lang.System.*;
import static java.lang.Math.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(in);
    int n = Integer.parseInt(sc.next());
    int m = Integer.parseInt(sc.next());
    out.println((n-1)*(m-1));
}
}

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  rep(i, n) {
    cin >> a[i] >> b[i];
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());

  int lower, upper;
  if (n % 2 == 1) {
    lower = a[n/2];
    upper = b[n/2];
  } else {
    lower = a[n/2-1] + a[n/2];
    upper = b[n/2-1] + b[n/2];
  }

  cout << upper - lower + 1 << endl;

  return 0;
}

/*
*/


Measured memory_bytes: 4792
def base10toN(num, base):
    converted_string, modstring = "", ""
    currentnum = num
    if not num:
        return '0'
    while currentnum:
        mod = currentnum % base
        currentnum = currentnum // base
        converted_string = chr(48 + mod + 7*(mod > 10)) + converted_string
    return converted_string
n=int(input())
ans=0
for i in range(4**9):
  s=base10toN(i,4)
  a=[]
  for j in range(len(s)):
    a.append(s[j])
  a.reverse()
  for j in range(9-len(s)):
    a.append('0')
  a.reverse()
  count0=0
  count1=0
  count2=0
  for j in range(9):
    if a[j]=='1':
      count0+=1
    if a[j]=='2':
      count1+=1
    if a[j]=='3':
      count2+=1
  if count0==0 or count1==0 or count2==0:
    continue
  b=[]
  for j in range(9):
    if a[j]=='0':
      b.append('0')
    if a[j]=='1':
      b.append('3')
    if a[j]=='2':
      b.append('5')
    if a[j]=='3':
      b.append('7')
  flag=0
  fail=0
  for j in range(9):
    if a[j]!='0':
      flag=1
    if flag==1 and a[j]=='0':
      fail=1
      break
  if fail==1:
    continue
  p=int(''.join(b))
  if p<=n:
    ans+=1
  else:
    break
print(ans)
  
  

Measured memory_bytes: 73916
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');

var n = +lines.shift();
var A = lines.shift().trim().split(' ').map(function(i){return +i;});
var q = +lines.shift();
var M = lines.shift().trim().split(' ').map(function(i){return +i;});

M.forEach(function(m) {
	if (solve(0, m)) {
		console.log('yes')
	}
	else {
		console.log('no');
	}
});

function solve(i, m) {
	if (i >= n) {
		return false;
	}

	if (A[i] == m) {
		return true;
	}
	return solve(i + 1, m) || solve(i + 1, m - A[i]);
}

Measured memory_bytes: 8860
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cmath>
#include<numeric>
#include<vector>
#include<tuple>
using namespace std;
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define vint vector<int> 
#define vvint vector<vint>
#define ct(a) cout<<a<<endl
typedef long long ll;
const int MOD = 1e9 + 7;
ll x, y,cnt=0;
int main(void)
{
	cin >> x >> y;
	if (x >= 0) {
		if (y > x)cout << y - x << endl;
		else if(y>0)cout << abs(x + y) << endl;
		else ct(1+abs(x + y));
	}
	else {
		if (y > x&&-x > y&&y>=0)cout<<((y!=0)?abs(x + y)+1:abs(x))<<endl;
		else if (y >= -x)ct(abs(x + y)+1);
		else if(y<x)ct(abs(x -y)+2);
		else ct(abs(x - y));
	}

	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M = 1e5+6;
int n, T, c, x;
string a, b;
int dp[M] , h[M];
int go (int i) {
    if (i==n-1)
        return 0;
    int &res = dp[i];
    if (res+1)return res;
    res = 1e9;
    for (int j=1;j<=2;j++) {
            if (i+j<n)
        res = min(res,go(i+j)+abs(h[i+j] - h[i]));

    }
    return res;
}
int main () {
    memset(dp,-1,sizeof dp);
    scanf("%d",&n);
    for (int i=0;i<n;i++)
        scanf("%d",h+i);
    h[n] = h[n+1] =   h[n+2] = 0;
    cout << go(0) << endl;
   // for (int i=0;i<n;i++)
  //      cout << dp[i] << ' ';
}


Measured memory_bytes: 4096
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.solve();
    }
    public void solve() {
        Scanner scan = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int N = scan.nextInt();
        List<Task> tasklist = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            Task t = new Task();
            t.time = scan.nextLong();
            t.dead = scan.nextLong();
            tasklist.add(t);
        }
        tasklist.sort(new TaskComparator());
        boolean answer = simulate(N, tasklist);
        System.out.println(answer ? "Yes" : "No");
    }
    public boolean simulate(int N, List<Task> tasklist) {
        long elapsed_time = 0;
        for (int i = 0; i < N; i++) {
            Task t = tasklist.get(i);
            elapsed_time += t.time;
            if (t.dead < elapsed_time) {
                return false;
            }
        }
        return true;
    }
    class Task {
        long time;
        long dead;
    }
    class TaskComparator implements Comparator<Task> {
        @Override
        public int compare(Task o1, Task o2) {
            if (o1.dead == o2.dead) {
                return Long.compare(o1.time, o2.time);
            }
            return Long.compare(o1.dead, o2.dead);
        }
    }
}


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

int main() {
  ll N;
  cin>>N;
  cout<<2*N*3.1415<<endl;
}

Measured memory_bytes: 3772
//#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<utility>
#include<algorithm>
#include<cstring>
#include<sstream>
#include <iomanip>

using namespace std;

typedef long long ll ;
typedef double db;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector< pair<int,int> > vii;
const double pi = 2*acos(0) ;

#define pf		  	printf
#define sf			  scanf
#define pb(a)         push_back(a)
#define mp			make_pair
#define fi			 first
#define se			 second
#define for0(i,n)        for(int i=0;i<n;i++)
#define for1(i,n)        for(int i=1;i<=n;i++)
#define forab(i,a,b)       for(int i=a;i<=b;i++)
#define lcm(a, b)     ((a)*((b)/gcd(a,b)))
#define sq(a)		   (a)*(a)
#define nw				"\n"
#define abs(x)		fabs(x)
#define pcase(z,x)    cout<<"Case "<<z<<": "<<x<<"\n"
int main()
{
	int i,j,x,y,t,ck=0;
	cin>>x>>y;
	for(i=x,j=0 ;i>=0; i--,j++){
		t = 2*i + 4*j;
		//cout<<t<<nw;
		if(t==y){ck=1; break;}
		}
	pf(ck?  "Yes" : "No");
	return 0;
}

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

int main(){
  int n;
  cin >> n;
  vector<int> vec(n);
  rep(i,n){
    cin >> vec.at(i);
  }
  int dif = 0;
  rep(i,n){
    if((vec.at(i)-(i+1))==0) continue;
    dif++;
  }
  if (dif==2 || dif ==0){
    cout << "YES";
  }
  else{
    cout << "NO";
  }
  return 0;
}
    

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define MOD 1000000007
#define INF 2000000000
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define reprev(i,n) for(int (i)=(n-1);(i)>=0;(i)--)
#define Flag(x) (1<<(x))
#define Flagcount(x) __builtin_popcount(x)
typedef unsigned long long int ull;

using namespace std;

int main(void){
  int N,ans;
  cin >> N;
  int s[N];
  rep(i,N) cin >> s[i];
  ans=accumulate(s,s+N,0);
  sort(s,s+N);
  rep(i,N){
    if(ans%10!=0) break;
    if(s[i]%10!=0) ans-=s[i];
  }
  if(ans%10==0) cout << "0" << endl;
  else cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using lint = long long int;
using ulint = unsigned long long int;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
template<class T, class U> void assign(V<T>& v, int n, const U& a) { v.assign(n, a); }
template<class T, class... Args> void assign(V<T>& v, int n, const Args&... args) { v.resize(n); for (auto&& e : v) assign(e, args...); }


int main() {
  cin.tie(nullptr); ios_base::sync_with_stdio(false);
  string s;
  while (getline(cin, s)) {
    int d = stoi(s);
    lint res = 0;
    for (int i = 0; i < 600; i += d) {
      res += i * i * d;
    }
    cout << res << '\n';
  }
}


Measured memory_bytes: 3080
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

    static class Task {

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int a = in.nextInt();
            int b = in.nextInt();
            int h = in.nextInt();
            out.println((a + b) * h / 2);
        }
    }

    private static void sort(double[] arr) {
        Double[] objArr = Arrays.stream(arr).boxed().toArray(Double[]::new);
        Arrays.sort(objArr);
        for (int i = 0; i < arr.length; i++) {
            arr[i] = objArr[i];
        }
    }

    private static void sort(int[] arr) {
        Integer[] objArr = Arrays.stream(arr).boxed().toArray(Integer[]::new);
        Arrays.sort(objArr);
        for (int i = 0; i < arr.length; i++) {
            arr[i] = objArr[i];
        }
    }

    private static void sort(long[] arr) {
        Long[] objArr = Arrays.stream(arr).boxed().toArray(Long[]::new);
        Arrays.sort(objArr);
        for (int i = 0; i < arr.length; i++) {
            arr[i] = objArr[i];
        }
    }

    private static void solve() {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task task = new Task();
        task.solve(1, in, out);
        out.close();
    }

    public static void main(String[] args) {
        new Thread(null, () -> solve(), "1", 1 << 26).start();
    }

    static class InputReader {

        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

    }

}

Measured memory_bytes: 28372
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long int ll;
const int N = 1e5 + 5;
const int INF = 1e9 + 5;
int a[N];
void solve_test_case(){
	int n,k;
	cin>>n>>k;
	for(int i = 0;i < n;++i){
		cin>>a[i];
	}
	vector<int> dp(n,INF);
	dp[0] = 0;
	for(int i = 0;i < n;++i){
		for(int j = i + 1;j < i + k + 1;++j){
			if(j < n){
				dp[j] = min(dp[j],dp[i] + abs(a[i] - a[j]));
			}
		}
	}
	cout<<dp[n-1]<<"\n";
}
 
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t = 1;
	//cin>>t;
	for(int i = 0;i < t;++i){
		solve_test_case();
	}
}


Measured memory_bytes: 4012
#include<bits/stdc++.h>
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define elif else if
#define INF 1000000007
using namespace std;
int a,b[110000];
string s;
signed main()
{
	cin>>s;
	if(s[0]=='M'){
		cout<<6;
	}elif(s[0]=='W'){
		cout<<4;
	}elif(s[0]=='F'){
		cout<<2;
	}elif(s[0]=='T'){
		if(s[1]=='U'){
			cout<<5;
		}else{
			cout<<3;
		}
	}else{
		if(s[1]=='U'){
			cout<<7;
		}else{
			cout<<1;
		}
	}
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define RI register int
typedef long long LL;
#define int LL

#define FILEIO(name) freopen(name".in", "r", stdin), freopen(name".out", "w", stdout);

using namespace std;

char buf[1000000], *p1 = buf, *p2 = buf;
inline char gc() {
  if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin);
  return p1 == p2 ? EOF : *(p1++);
}
template <class T> inline void read(T &n) {
  n = 0; RI ch = gc(), f;
  while ((ch < '0' || ch > '9') && ch != '-') ch = gc();
  f = (ch == '-' ? ch = gc(), -1 : 1);
  while (ch >= '0' && ch <= '9') n = n * 10 + (ch ^ 48), ch = gc();
  n *= f;
}

int const MAXN = 2e5 + 5;
int a[MAXN];
int f[2][MAXN][2];

signed main() {
  
#ifdef LOCAL
  FILEIO("a");
#endif

  int n; read(n);
  for (RI i = 1; i <= n; ++i) read(a[i]);
  memset(f, -0x7f, sizeof(f));
  f[0][0][0] = 0;
  int cur = 0, last = 1;
  for (RI i = 1; i <= n; ++i) {
    swap(cur, last);
    int R = (i + 1) / 2 + 1, L = (n / 2) - ((n - i) / 2) - 1;
    for (RI j = R; j >= L; --j) {
      f[cur][j][0] = max(f[last][j][0], f[last][j][1]);
      f[cur][j][1] = f[last][j - 1][0] + a[i];
    }
  }
  printf("%lld\n", max(f[cur][n / 2][0], f[cur][n / 2][1]));

  return 0;
}

// created by Daniel yuan


Measured memory_bytes: 12532
loop do
  n, k = gets.split.map(&:to_i)
  break if n == 0
  points =
    k.times.map do
      c, a, b = gets.split
      a = a.to_i; b = b.to_i
      case c
      when 'xy'
        [a, b, 0]
      when 'yz'
        [0, a, b]
      when 'xz'
        [a, 0, b]
      end
    end

  cross_points = {}
  points.combination(2).each do |a, b|
    c = a.zip(b)
    if c.all?{ |p, q| !(p == 0 && q == 0) } && c.any?{ |p, q| p == q }
      p = c.map{ |p, q| [p, q].max }
      cross_points[p] ||= 0
      cross_points[p] += 1
    end
  end
  puts n * n * n - k * n + cross_points.values.reduce(0){ |s, v| v < 2 ? s += v : s += v - 1 }
end

Measured memory_bytes: 8756
n = int(input())
a = list(map(int, input().split()))
q = int(input())
r = [0]*(10**5+10)
for i in range(n):
  r[a[i]] +=1
b = [0]*q
c = [0]*q
for i in range(q):
  b[i],c[i] = map(int,input().split())
s = 0
for m in range(n):
  s += a[m] 
for ii in range(q):
  s = s + (c[ii]-b[ii])*r[b[ii]]
  r[c[ii]] +=r[b[ii]]
  r[b[ii]]=0
  print(s)

Measured memory_bytes: 22232
#include <stdio.h>

int main(void) {
    int a, b;
    int ans=-1;
    scanf("%d%d", &a, &b);

    if (a >= 10 || b >= 10) {
        printf("%d\n", ans);
    }
    else {
        ans = a*b;
        printf("%d\n", ans);
    }

    return 0;
}

Measured memory_bytes: 1696
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define NUM 1001

ll N,K;
ll dp[NUM][NUM];

ll recursive(ll num_ball,ll num_box){

	if(dp[num_ball][num_box] != -1)return dp[num_ball][num_box];

	if(num_ball == 0 || num_box == 1)return 1;

	ll ret = 0;

	for(ll next_box = 1; next_box <= num_box; next_box++){
		if(next_box > num_ball)break;
		ret += recursive(num_ball-next_box,next_box);
		ret %= MOD;
	}

	return dp[num_ball][num_box] = ret;
}

int main(){

	scanf("%lld %lld",&N,&K);

	if(N < K){
		printf("0\n");
		return 0;
	}

	N -= K;

	for(ll i = 0; i <= N; i++){
		for(ll p = 0; p <= K; p++){
			dp[i][p] = -1;
		}
	}

	printf("%lld\n",recursive(N,K));

	return 0;
}



Measured memory_bytes: 7572
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,k;
string s;
int main(){
	cin >> n >> s >> k;
	k--;
	for(int i=0;i<n;i++)
		if(s[i]==s[k])cout << s[i];
		else cout << "*";
    return 0;
}

Measured memory_bytes: 256
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  n, m = map(int, input().split())
  la, lb = [], []
  lc = []
  MAX = 10**9
  dp = [MAX] * (2 ** 13)
  dp[0] = 0
  
  for _ in range(m):
    a, b = map(int, input().split())
    la.append(a)
    lb.append(b)
    lc.append(sum([1 << c for c in map(int, input().split())]))
    
  for a, c in zip(la, lc):
    for ic in range(len(dp)):
      dp[c | ic] = min(a + dp[ic], dp[c | ic])
  if dp[2**(n+1)-2] != MAX:
    print(dp[2**(n+1)-2])
  else:
    print(-1)

if __name__=='__main__':
  main()



Measured memory_bytes: 74732
#include <bits/stdc++.h>
using namespace std;
#define endl '\n';

int main()
{
    int xi, r, d, x2000, aux;
    aux = 0;
    xi = 0;
    cin >> r >> d >> x2000;
   while(aux < 10){
        xi = r * x2000 - d;
        x2000 = xi;
        cout << xi << endl;
        aux++;
   }
}


Measured memory_bytes: 3576
#include<iostream>
#include<vector>
#include<string>
#include<stack>
using namespace std;
int main(){
  int w,h;
  string line;
  while(true){
    cin>>w>>h;
    if(!w&&!h)
      break;
    vector<string> castle;
    for(int i=0;i<h;i++){
      cin>>line;
      castle.push_back(line);
    }
    int count[w+2][h+2];
    int sx,sy;
    const int MAX = 1000;
    for(int y=1;y<h+1;y++){
      for(int x=1;x<w+1;x++){
	count[x][y] = MAX;
	if (castle[y-1][x-1]=='&'){
	  sx = x;
	  sy = y;
	  castle[y-1][x-1]='.';
	}
      }
    }
    int min = MAX;
    int dx[4] = {1,0,-1,0};
    int dy[4] = {0,1,0,-1};
    count[sx][sy] = 0;
    stack<int> st;
    st.push(sy*(w+2)+sx);
    while(!st.empty()){
      int x = st.top()%(w+2);
      int y = st.top()/(w+2);
      st.pop();
      char c = castle[y-1][x-1];
      int n;
      for(int d=0;d<4;d++){
	n = count[x][y];
	if(x+dx[d]>0&&y+dy[d]>0&&x+dx[d]<w+1&&y+dy[d]<h+1){
	  if(c=='.'&&castle[y+dy[d]-1][x+dx[d]-1]=='#')
	    n++;
	  if(n<count[x+dx[d]][y+dy[d]]){
	    count[x+dx[d]][y+dy[d]] = n;
	    st.push((y+dy[d])*(w+2)+x+dx[d]);
	  }
	}
	else{
	  if(n<min)
	    min = n;
	}
      }
    }
    cout<<min<<endl;
  }
  return 0;
}

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

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (int i=(int)from; i<(int)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (int i=(int)N-1; i>=0; i--)

const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const int INF = pow(2,30)-1;

vector<ll> fac;
void c_fac(int x=pow(10,6)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }
ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { int d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }
ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }


int main() {
  ll N; cin >> N; 
  ll A[N]; rep(i,N) cin >> A[i];
  ll n = A[0] + A[1];
  ll x = 0; repr(i,2,N) x ^= A[i];
  if (n<x||(n-x)%2==1) { cout << -1 << endl; return 0; }

  ll l = (n-x)/2, r = n - (n-x)/2;
  // cout << l << " " << r << endl;
  if ((l^r)!=x) { cout << -1 << endl; return 0; }

  ll lxr = l^r;
  ll stone = l&r;
  if (stone>A[0]) { cout << -1 << endl; return 0; }
  per(i,60) {
    if ((lxr&(1ll<<i)) && stone+(1ll<<i)<=A[0]) {
      stone += (1ll<<i);
      // cout << i << endl;
    }
  }
  // cout << stone << endl;

  ll result = stone==0 ? -1 : A[0] - stone;
  cout << result << endl;
  return 0;
}

Measured memory_bytes: 3612
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7, maxn = 1e9;
int n, k; vector<vector<int>>sum;

int f(int i, int j) {
	return sum[i][j] - sum[i - k][j] - sum[i][j - k] + sum[i - k][j - k];
}

int main() {
	cin >> n >> k;
	int t = 2 * k, s = 4 * k;
	sum.resize(s + 5, vector<int>(s + 5, 0));
	for (int i = 0, x, y; i < n; i++) {
		char c; cin >> x >> y >> c;
		x %= t, y %= t;
		if (c == 'B')x += k;
		x %= t;
		sum[x][y]++, sum[x + t][y]++, sum[x][y + t]++, sum[x + t][y + t]++;
	}
	for (int i = 1; i < s; i++)for (int j = 1; j < s; j++) {
		sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
	}
	int ans = 0;
	for (int i = 0; i < t; i++)for (int j = 0; j < t; j++) {
		ans = max(ans, f(i + k, j + k) + f(i + t, j + t));
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 63104
n,m,x=(int(i) for i in input().split())
l=[]
ans=None
for i in range(n):
  l.append([int(i) for i in input().split()])
for i in range(2**n):
  k=i
  r=[0 for i in range(m)]
  cost=0
  for j in range(n):
    if k%2==1:
      for y in range(m):
        r[y]+=l[j][y+1]
      cost+=l[j][0]
    k=k//2
  if min(r)>=x:
    if ans==None:
      ans=cost
    elif ans>cost:
      ans=cost
if ans==None:
  print('-1')
else:
  print(ans)

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

void replace_h(string& s)
{
  for (int i = 0; i < s.size(); ++i) s[i] = 'x';
}
void replace_v(vector<string> &s, int v)
{
  for (int i = 0; i < s.size(); ++i) s[i][v] = 'x';
}

int main()
{
  int h, w;
  cin >> h >> w;
  vector<string> a(h);
  for (int i = 0; i < h; ++i) cin >> a[i];
  
  for (int i = 0, k = 0; i < h; ++i, k = 0)
  {
    for (; k < w; ++k)
    {
      if (a[i][k] == '#') break;
    }
    if (k == w) replace_h(a[i]);
  }
  for (int k = 0, i = 0; k < w; ++k, i = 0)
  {
    for (; i < h; ++i)
    {
      if (a[i][k] == '#') break;
    }
    if (i == h) replace_v(a, k);
  }
  
  for (int i = 0; i < h; ++i)
  {
    for (int k = 0; k < w; ++k)
    {
      if (a[i][k] != 'x') cout << a[i][k];
    }
    cout << endl;
  }
}

Measured memory_bytes: 256
#include<iostream>
#include<stdio.h>
//#include <bits/stdc++.h>
#include<vector>
#include<float.h>
#include<iomanip>
#include<algorithm>
#include<string>
#include<cstring>
#include<math.h>
#include<cmath>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include <cassert>
#include <cmath>
#include<cstdint>

#define INF 1e9
#define rep(i,n)for(int i=0;(i)<(int)(n);i++)
#define REP(i,a,b)for(int i=(int)(a);(i)<=(int)(b);i++)
#define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i;
#define vec(type,n) vector<type>(n)
#define vvec(m,n) vector<vector<int>> (int(m),vector<int>(n))
#define ALL(a)  (a).begin(),(a).end()

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<int,int>;

vector<int>bitSearch(int bit,int n){
    vector<int>S;
    rep(i,n)if(bit&(1<<i))S.push_back(i);
    return S;
}

struct UnionFind{
    vector<int>par;

    UnionFind(int n) : par(n){
        rep(i,n)par[i] = i;
    }

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

    void unite(int x,int y){
        if(root(x)==root(y))return;
        par[root(x)] = root(y);
    }

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




int main(){
   int a,b,c;cin>>a>>b>>c;
   vector<ll>s(a),t(b);
   rep(i,a)cin>>s[i];
   rep(i,b)cin>>t[i];

   rep(ii,c){
       ll now;cin>>now;
       vector<ll>ss(2,INF*INF),tt(2,INF*INF);
       if(now == *lower_bound(ALL(s),now))ss[0] = now;
       else {
           ll id = lower_bound(ALL(s),now)-s.begin();
           if(id-1>=0)ss[0] = s[id-1];
           if(id<s.size())ss[1] = s[id];
       }
       if(now == *lower_bound(ALL(t),now))tt[0] = now;
       else {
           ll id = lower_bound(ALL(t),now)-t.begin();
           if(id-1>=0)tt[0] = t[id-1];
           if(id<t.size())tt[1] = t[id];
       }

       ll res = INF*INF;
       rep(i,2){
           ll dist;
           rep(j,2){
               dist = abs(now-ss[i])+abs(ss[i]-tt[j]);
               res = min(res,dist);
           }
       }
       rep(i,2){
           ll dist;
           rep(j,2){
               dist = abs(now-tt[i])+abs(tt[i]-ss[j]);
               res = min(res,dist);
           }
       }
       cout<<res<<endl;
   }
}



Measured memory_bytes: 2944
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
class Simple {
    int N, M;
    string[] A, B;
    void Solve() {
        //input
        N = io.Int;
        M = io.Int;
        A = new string[N];
        B = new string[M];
        for (int i = 0; i < N; ++i)
            A[i] = io.String;
        for (int j = 0; j < M; ++j)
            B[j] = io.String;
        //cal
        for (int shiftX = 0; shiftX < N - M + 1; ++shiftX) {
            for (int shiftY = 0; shiftY < N - M + 1; ++shiftY) {
                for (int i = 0; i < M; ++i) {
                    for (int j = 0; j < M; ++j) {
                        if (A[i + shiftY][j + shiftX] != B[i][j])
                            goto BR;
                    }
                }
                //res(Contain)
                Console.WriteLine("Yes");
                return;
            BR:;
            }
        }
        //res(!Contain)
        Console.WriteLine("No");
    }

    SimpleIO io = new SimpleIO();
    public static void Main(string[] args) { new Simple().Stream(); }
    void Stream() {
        Solve();
        io.writeFlush();
    }
}

class SimpleIO {
    string[] nextBuffer;
    int BufferCnt;
    char[] cs = new char[] { ' ' };
    StreamWriter sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
    public SimpleIO() {
        nextBuffer = new string[0];
        BufferCnt = 0;
        Console.SetOut(sw);
    }
    public string Next() {
        if (BufferCnt < nextBuffer.Length)
            return nextBuffer[BufferCnt++];
        string st = Console.ReadLine();
        while (st == "")
            st = Console.ReadLine();
        nextBuffer = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        BufferCnt = 0;
        return nextBuffer[BufferCnt++];
    }
    public string String => Next();
    public char Char => char.Parse(String);
    public int Int => int.Parse(String);
    public long Long => long.Parse(String);
    public double Double => double.Parse(String);
    public void writeFlush() { Console.Out.Flush(); }
}


Measured memory_bytes: 11092
n = int(input())
a = list(map(int,input().split()))
# a1 = [a[i]+i for i in range(n)]
# a2 = [-(a[i]-i) for i in range(n)]
# print(a)
# print(a1)
# print(a2)
d1 = {}
d2 = {}
for i in range(n):
    if a[i]+i in d1:
        d1[a[i]+i] += 1
    else:
        d1[a[i]+i] = 1
    if -(a[i]-i) in d2:
        d2[-(a[i]-i)] += 1
    else:
        d2[-(a[i]-i)] = 1
ans = 0
for i in d1:
    if i in d2:
        ans += d1[i] * d2[i]
print(ans)
# print(d1)
# print(d2)

Measured memory_bytes: 139112
#include <stdio.h>
 
int main(){
    int n,x,count;
    while(true){
        scanf("%d %d",&n,&x);
        if(n == 0 && x == 0) break;
        count = 0;
        for(int min = 1; min < x/3 && min <= n-2;min++){
            for(int middle = min+1;middle < x - (min+middle) && middle <= n-1; middle++){
                if(x-(min+middle) <= n) count++;
            }
        }
        printf("%d\n",count);
    }
 
}


Measured memory_bytes: 2628
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<ll, ll>;
priority_queue<pi> qless;
priority_queue<pi, vector<pi>, greater<pi>> qgreat;
int q;
pi mid;
ll lsum, gsum, off;
int cnt;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> q;
    for(int i=0;i<q;i++)
    {
        int t;
        int a=0, b=0;
        cin >> t;
        if(t == 1)
        {
            cnt++;
            cin >> a >> b;
            off += b;
            if(i == 0)
            {
                mid = {a, 0};
                continue;
            }
            if(mid < (pi){a, i})
            {
                qgreat.push({a,i});
                gsum += a-mid.first;
            }
            else
            {
                qless.push({a,i});
                lsum += mid.first-a;
            }
            while(qless.size() > (cnt-1)/2)
            {
                lsum -= (mid.first - qless.top().first) * qless.size();
                gsum += (mid.first - qless.top().first) * qgreat.size();
                qgreat.push(mid);
                mid = qless.top();
                qless.pop();
                gsum += (qgreat.top().first - mid.first);
            }
            while(qgreat.size() > (cnt)/2)
            {
                lsum -= (mid.first - qgreat.top().first) * qless.size();
                gsum += (mid.first - qgreat.top().first) * qgreat.size();
                qless.push(mid);
                mid = qgreat.top();
                qgreat.pop();
                lsum += (mid.first - qless.top().first);
            }
        }
        else
        {
            cout << mid.first << ' ' /*<< lsum << ' ' << gsum << ' '  << off << ' ' */ << lsum + gsum + off <<  '\n';
        }
    }
}

Measured memory_bytes: 4588