akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <numeric>
using namespace std;
typedef long long ll;

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    ll a,b,c,k;cin>>a>>b>>c>>k;
    if(a>=k)cout<<k;
    else if(a+b>=k)cout<<a;
    else cout<<a-(k-a-b);
    
}

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

int main(){
  int N;
  cin >> N;
  vector<int64_t> A(N);
  for(int i=0; i<N; i++){
    cin >> A.at(i);
  }

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

  int64_t sum = 0;
  int die = 0;
  for(int i=0; i<N-1; i++){
    sum += A.at(i);
    if(sum * 2 < A.at(i + 1)){
      die = i + 1;
    }
  }

  int ans = N - die;
  cout << ans << endl;
}

Measured memory_bytes: 1024
from collections import Counter

def ans_solve(N, As):
    C = Counter(As)
    ans = 0
    for d in reversed(sorted(As)):
        n = (2 ** d.bit_length()) - d
        if d != n:
            if C[d] > 0 and C.get(n, 0) > 0:
                C[d] -= 1
                C[n] -= 1
                ans += 1
        else:
            ans += C[d]//2
            C[d] = 0
    return ans
 
 
if __name__ == "__main__":
    N = input()
    As = [int(c) for c in input().split()]
    print(ans_solve(N, As))

Measured memory_bytes: 33344
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using std::cin;
using std::cout;

std::vector<std::vector<int>> gr;
std::vector<int> parent;
std::vector<int> toposort;

void dfs(int u)
{
  for (auto v : gr[u])
  {
    if (parent[v]) continue;
    parent[v] = u;
    dfs(v);
  }

  toposort.push_back(u);
}

int main(int argc, char* argv[])
{
  std::ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int N, M;
  cin >> N >> M;

  gr.resize(N + 1);
  parent = std::vector<int>(N+1, 0);

  std::vector<int> ax(N + 1, 0);

  for (int i = 1; i < N+M; ++i)
  {
    int u, v;
    cin >> u >> v;
    gr[u].push_back(v);
    ++ax[v];
  }

  int root = 0;
  for (int i = 1; !root && i <= N; ++i)
  {
    root = (ax[i] ? 0 : i);
  }

  toposort.reserve(N);
  dfs(root);
  std::reverse(toposort.begin(), toposort.end());

  for (int i = 1; i <= N; ++i)
  {
    parent[i] = 0;
    ax[toposort[i-1]] = i;
  }

  for (int i = 1; i <= N; ++i)
  {
    std::sort(gr[i].begin(), gr[i].end(), [&ax](int i, int j)
    {
      return ax[i] < ax[j];
    });
  }

  toposort.resize(0);
  dfs(root);

  for (int i = 1; i <= N; ++i)
  {
    cout << parent[i] << "\n";
  }

  return 0;
}


Measured memory_bytes: 12160
N,S = open(0).read().split()
N=int(N)
S=[s == "." for s in list(S)]

w = sum([1 if s else 0 for s in S])
b = 0
m = w + b
for s in S:
    if s:
        w -= 1
    else:
        b += 1
    m = min(m, w + b)
print(m)


Measured memory_bytes: 12344
import java.util.Arrays;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		int a[]=new int[3];
		a[0]=scan.nextInt();
		a[1]=scan.nextInt();
		a[2]=scan.nextInt();
		Arrays.sort(a);
		System.out.println(a[0]+a[1]+a[2]*10);
		
		}
}

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

int main() {

    int N;
    cin>>N;
    int x=N%10;

    if(x==0||x==1||x==6||x==8){

        cout<<"pon"<<endl;
    }

    else if (x==3){

        cout<<"bon"<<endl;

    }

    else {

        cout<<"hon"<<endl;
    }


}

Measured memory_bytes: 3636
#include <bits/stdc++.h>
#define GET_REP(_1, _2, _3, NAME, ...) NAME
#define rep(...) GET_REP(__VA_ARGS__, irep, _rep)(__VA_ARGS__)
#define rep1(...) GET_REP(__VA_ARGS__, irep1, _rep1)(__VA_ARGS__)
#define _rep(i, n) irep (i, 0, n)
#define _rep1(i, n) irep1(i, 1, n)
#define irep(i, a, n) for (int i = a; i < (int)(n); ++i)
#define irep1(i, a, n) for (int i = a; i <= (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep1(i, n) for (int i = (int)(n); i >= 1; --i)
#define allrep(X, x) for (auto &&X : x)
#define all(x) begin(x), end(x)
#ifdef LOCAL
  #include "../../Lib/cout_container.hpp"
  #define debug(x) cout << #x " => " << (x) << endl
#else
  #define debug(x) 0
#endif
using lint = long long;
constexpr int    INF  = 1 << 28;
constexpr lint   INFL = 1LL << 60;
constexpr int    MOD  = (int)1e9 + 7;
constexpr double EPS  = 1e-9;
using namespace std;
namespace { struct INIT { INIT() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } INIT; }

int main(void) {
  int r;
  cin >> r;
  cout << r * r << endl;
  return 0;
}

Measured memory_bytes: 256
n, t = gets.split.map(&:to_i)
ci = []
ti = []
n.times.each{|i|
    a = gets.split.map(&:to_i)
    if a[1] <= t
        ci << a[0]
        ti << a[1]
    end
}

if ci.empty?
    puts 'TLE'
else
    puts ci.min
end


Measured memory_bytes: 1788
#include <stdio.h>
int main(void){
  long a,b;
  while(scanf("%ld %ld\n",&a,&b)!=EOF) {
    long k=1,c=a+b;
    while(c/10){c/=10;k++;}
    printf("%ld\n",k);
  }
  return 0;
}

Measured memory_bytes: 604
#include <bits/stdc++.h>
  
using namespace std;
  
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second
  
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;
ll mod = 1e9+7;

//Mod が素数の時
//前計算O(nlogn)
//本計算O(1)
 
const int MAX_N=200010;
 
ll gcd(ll a, ll b){
    return b == 0LL ? a : gcd(b, a%b);
}
ll lcm(ll a, ll b){
    return a / gcd(a, b) * b;
}
ll extgcd(ll a, ll b, ll &x, ll &y){
    ll g = a;
    x = 1LL;
    y = 0LL;
    if(b != 0) g = extgcd(b, a%b, y, x), y-= (a/b)*x;
    return g;
}
ll fact[MAX_N], ifact[MAX_N];
ll mod_inverse(int a){
    ll x, y;
    extgcd(a, mod, x, y);
    return(mod+x%mod)%mod;
}
 
void makeFact(){
    fact[0]=ifact[0] = 1LL;
    for(int i = 1; i < MAX_N; i++){
        fact[i] = fact[i-1]*i%mod;
        ifact[i] = mod_inverse(fact[i]);
    }
}
 
ll c(int n, int r){
    if(n < 0 || r < 0 || r > n) return 0;
    if(r > n / 2) r = n-r;
    return fact[n]*ifact[n-r]%mod*ifact[r]%mod;
}

int main(){
	makeFact();
	int n, m;
	cin >> n >> m;
	if(n > m) swap(n, m);
	if(m - n > 1) {
		cout << 0 << endl;
		return 0;
	}
	if(m - n ==1) {
		cout << fact[n]*fact[m]%MOD << endl;
	}
	else {
		cout << fact[n] * fact[m] % MOD * 2 %MOD << endl;
	}
}


Measured memory_bytes: 3328
#!/usr/bin/env/ python

A=[[0]]*3
A[0]=[1,3,5,7,8,10,12]
A[1]=[4,6,9,11]
A[2]=[2]

x,y=map(int,raw_input().split())

for i in range(3):
    if x in A[i]:
        xx=i
    if y in A[i]:
        yy=i

if xx==yy:
    print 'Yes'
else:
    print 'No'

Measured memory_bytes: 2568
S = input()

S_list = [s for s in S]

# print(S_list)

min_answer = float("inf")
for i in range(0, len(S)-2):
    # print(int("".join(a)))
    # print(S[i:i+3])
    min_answer = min(min_answer, abs(753 - int(S[i:i+3])))

print(min_answer)

Measured memory_bytes: 38384
/*********************************************JMD**********************************************/
//                                  |\  |     | |   |   /\                                   //
    //                              | \ |     | |---|  /--\                              //
        //                          |  \| |___| |   | /    \1999                     //
/*******************************************************************************************/
#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;

#define mp make_pair
#define pb push_back
#define pf push_front
#define ss second
#define ff first
#define mem(x,val) memset(x,val,sizeof(x))
#define forr(i,a,b) for(ll i=a;i<b;i++)
#define f(i,n) forr(i,0,n)
#define sz(x) ll((x).size())
#define all(x) ((x).begin(),(x).end())
#define asc(x) ((int)(x))
#define precision(x) cout<<fixed<<setprecision((x));

const ll mod=1e9 + 7;

/*******************************************************************************************/
bool c1(string s)
{
    //yymm
    ll val=0;
    val+=((int) s[0]-48)*10+(int) s[1]-48;

    ll val2=((int) s[2]-48)*10+(int) s[3]-48;

    if(val2>=1 && val2<=12) return 1;
    return 0;
}
bool c2(string s)
{
    //mmyy
    ll val=0;
    val+=((int) s[0]-48)*10+(int) s[1]-48;


    if(val>=1 && val<=12) return 1;
    return 0;
}
void solve()
{
    string s;
    cin>>s;

    if(c1(s) && c2(s))
    {
        cout<<"AMBIGUOUS";
    }
    else if(c1(s))
    {
        cout<<"YYMM";
    }
    else if(c2(s))
    {
        cout<<"MMYY";
    }
    else cout<<"NA";
    return;
}

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

    ll t=1;
    //cin>>t;
    clock_t launch=clock();

    while(t--)
    {
        solve();
    }

    clog<<((long double)(clock()-launch)/CLOCKS_PER_SEC)<<"\n";
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main{
 public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    long n = sc.nextLong();
    long d = sc.nextLong();
    int ans = 0;
    for (int i = 0; i < n; i++) {
      long x = sc.nextLong();
      long y = sc.nextLong();
      if(x*x+y*y <= d*d){
        ans += 1;
      }
    }
    System.out.println(ans);

  }
}


Measured memory_bytes: 60896
// ABC165C - Many Requirements

#include <algorithm>
#include <iostream>
using namespace std;


int main() 
{
	int N, R;
	cin >> N >> R;

	if (N < 10)
	{
		cout << R+100 * (10 - N) << endl;
	}
	else
	{
		cout << R << endl;
	}
}

Measured memory_bytes: 256
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <iomanip>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define repr(e,x) for(auto& e:x)
using namespace std;
typedef long long ll;
typedef long double ld;
//typedef pair<int,int> P;
int const INF=1001001001;
ll const LINF=1001001001001001001;
ll const MOD=1000000007;

int N;
int s[100];

int main(){
    cin>>N;
    rep(i,N) cin>>s[i];

    sort(s,s+N);

    int ans=0;
    rep(i,N) ans+=s[i];

    if(ans%10!=0){
        cout<<ans<<endl;
        return 0;
    }

    rep(i,N){
        if(s[i]%10!=0){
            ans-=s[i];
            break;
        }    
    }

    if(ans%10!=0){
        cout<<ans<<endl;
        return 0;
    }

    cout<<0<<endl;
}

Measured memory_bytes: 3596
#include <bits/stdc++.h>
#define WHOLE(v) (v).begin(), (v).end()
#define REV_WHOLE(v) (v).rbegin(), (v).rend()
using i64 = int64_t;
using namespace std;
template <size_t I, class H, class... T> struct TupleReaderWriter{static tuple<H,T...> r(istream&i){H v;i>>v;return tuple_cat(tuple<H>(v),TupleReaderWriter<sizeof...(T)-1,T...>::r(i));}static void w(ostream&o,tuple<H,T...>&t,string d){TupleReaderWriter<I-1,H,T...>::w(o,t,d);o<<d<<get<I>(t);}};
template <class H, class... T> struct TupleReaderWriter<0,H,T...>{static tuple<H,T...> r(istream&i){H v;i>>v;return tuple<H>(v);}static void w(ostream&o,tuple<H,T...>&t,string d){o<<get<0>(t);}};
template<class T>istream&operator>>(istream&i,vector<T>&v){for(auto&x:v)i>>x;return i;}
template<class T>ostream&operator<<(ostream&o,vector<T>&v){string delim="";for(auto&x:v)o<<delim<<x,delim=" ";return o;}
template <class... T> istream& operator>>(istream&i,tuple<T...>& t) { t = TupleReaderWriter<sizeof...(T), T...>::r(i); return i; }
template <class... T> ostream& operator<<(ostream& o, tuple<T...>& t) { string delim = " "; TupleReaderWriter<sizeof...(T) - 1, T...>::w(o, t, delim); return o; }
template<typename T> using rev_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using vector2d = vector<vector<T>>;
void R_YESNO(bool p) { cout << (p ? "YES" : "NO") << endl; }
void R_YesNo(bool p) { cout << (p ? "Yes" : "No") << endl; }
int main() {
    int A, B, K;
    cin >> A >> B >> K;
    for(int i = 0; i < K; i++) {
        A /= 2;
        B += A;
        swap(A, B);
    }
    if(K & 1) swap(A, B);
    cout << A << " " << B << endl;
    return 0;
}

Measured memory_bytes: 256
# ABC058B - ∵∴∵
o, e = [list(input().rstrip()) for _ in range(2)]
ans = []
for i in range(min(len(o), len(e))):
    ans.append(o[i])
    ans.append(e[i])
if len(o) - len(e):
    ans.append(o[-1])
print("".join(ans))

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

int find_nearest_check_point(int a,int b,vector<int>c,vector<int>d,int m) {
	int manhattan_distance(abs(c[0]-a)+abs(d[0]-b));
	int manhattan_number(1);
	for (int i(1); i < m; i++) {
		if (manhattan_distance > abs(c[i] - a) + abs(d[i] - b)) {
			manhattan_distance = (abs(c[i] - a) + abs(d[i] - b));
			manhattan_number = (i + 1);
		}
	}
	return manhattan_number;
}

int main(void) {
	//入力
	int n, m;
	cin >> n >> m;
	vector<int>a;
	vector<int>b;
	for (int i(0); i < n; i++) {
		int atmp, btmp;
		cin >> atmp >> btmp;
		a.push_back(atmp);
		b.push_back(btmp);
	}
	vector<int>c;
	vector<int>d;
	for (int i(0); i < m; i++) {
		int ctmp, dtmp;
		cin >> ctmp >> dtmp;
		c.push_back(ctmp);
		d.push_back(dtmp);
	}
	//処理
	for (int i(0); i < n; i++) {
		cout << find_nearest_check_point(a[i],b[i],c,d,m) << endl;
	}
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
#define pi 3.174
int main() {
    int n,m;
    cin>>n>>m;
    int a[m];
    int sum=0;
    for(int i=0;i<m;i++)
    {
        cin>>a[i];
        sum+=a[i];
    }
    if(sum<=n)
        cout<<n-sum<<endl;
    else
        cout<<"-1\n";
}

Measured memory_bytes: 3604
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int x = sc.nextInt();
		int[] xx = new int[n];
		for (int i = 0; i < n; i++) {
			xx[i] = sc.nextInt();
			xx[i] = Math.abs(x-xx[i]);
		}
		if (n == 1) {
			System.out.println(xx[0]);
			return;
		}
		System.out.println(multi_gcd(xx, n));
	}
	
	private static int multi_gcd(int[] xx, int rem) {
		if (rem == 2) {
			return gcd(xx[1], xx[0]);
		}
		return gcd(xx[rem - 1], multi_gcd(xx, rem - 1));
	}
	
	private static int gcd(int a, int b) {
		int temp;
		while((temp = a%b) != 0) {
			a = b;
			b = temp;
		}
		return b;
	}
}


Measured memory_bytes: 55980
import sys
DAYDREAM = ["dream", "dreamer", "erase", "eraser" ]
S = input()

ind = len(S)
res = True

while ind > 0:
	ok = False
	for i in DAYDREAM:
#		print("ind:", ind, "subS: ", S[ind - len(i):ind], file=sys.stderr)
		if S[ind - len(i):ind] == i:
			ind -= len(i)
			ok = True
			break;
	if ok == False:
		res = False
		break

print("YES" if res == True else "NO")

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

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

      string s;
      cin >> s;

      if(s.size() > k){
          for (size_t i = 0; i < k; i++) {
              cout << s.at(i);
          }
          cout << "..."<<endl;
      }
      else {
          for(char c: s){
              cout <<c;
          }
          cout <<endl;
      }
}


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

using namespace std;

#define INF 210000000
#define int long long

bool cmp(int a,int b){
		return a > b;
}

signed main() {
		int n;
		cin >> n;
		map<int,int> cnt;
		for(int i=0;i<n;i++){
				int tmp;
				cin >> tmp;
				cnt[tmp]++;
		}
		int ans = 0;
		for(auto& A : cnt){
				if(A.second % 2 == 1) ans++;
		}
		cout << ans << endl;
}


Measured memory_bytes: 6528
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>

using namespace std;

int a[100000];
vector <int> v[100000];

long long dfs(int x, int y) {
	int i;
	long long sum = 0, ans, p;
	vector <long long> w;
	
	if (v[x].size() == 1) return a[x];
	
	for (i = 0; i < v[x].size(); i++) {
		long long c;
		
		if (v[x][i] == y) continue;
		
		c = dfs(v[x][i], x);
		sum += c;
		w.push_back(c);
	}
	
	if (sum < a[x] || a[x] * 2 - sum < 0) {
		puts("NO");
		
		exit(0);
	}
	
	ans = a[x] * 2 - sum;
	p = sum - a[x];
	
	sort(w.begin(), w.end());
	
	if (sum - w.back() < p) {
		puts("NO");
		
		exit(0);
	}
	
	return ans;
}

int main() {
	int n, i;
	
	scanf("%d", &n);
	
	for (i = 0; i < n; i++) scanf("%d", &a[i]);
	
	for (i = 0; i < n - 1; i++) {
		int x, y;
		
		scanf("%d %d", &x, &y);
		
		x--;
		y--;
		
		v[x].push_back(y);
		v[y].push_back(x);
	}
	
	if (n == 2) {
		if (a[0] == a[1]) {
			puts("YES");
		} else {
			puts("NO");
			
			return 0;
		}
	}
	
	for (i = 0; i < n; i++) {
		if (v[i].size() == 1) continue;
		
		if (dfs(i, -1) == 0) {
			puts("YES");
		} else {
			puts("NO");
		}
		
		return 0;
	}
	
	return 0;
}


Measured memory_bytes: 16768
# -*- coding: utf-8 -*-

N, Q = map(int,input().split())
Node = [-1] * N
Counter = [0] * N
Result = [0] * N

for i in range(N-1):
    a, b = map(int,input().split())
    Node[b-1] = a-1

for i in range(Q):
    p, x = map(int,input().split())
    Counter[p-1] += x

for i in range(N):
    p = i
    ans = 0
    while p >= i:
        ans += Counter[p]
        p = Node[p]
    Result[i] = ans
    if p >= 0:
        Result[i] += Result[p]

print(" ".join(list(map(str,Result))))


Measured memory_bytes: 42904
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")

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

using ll = long long;
using ull = unsigned long long;
using namespace std;

int main() {
    int A, P;
    cin >> A >> P;
    cout << (A*3+P)/2 << endl;
    return 0;
}


Measured memory_bytes: 256
#include<stdio.h>
#include<stdlib.h>

struct node{
  struct node *right;
  struct node *left;
  struct node *parent;
  int key;
};
typedef struct node * Node;
#define NIL NULL

Node root;

Node treeMinimum(Node x){


}

Node treeMaximum(Node x){


}

Node treeSearch(Node u, int k){


}

Node treeSuccessor(Node x){


}

void treeDelete(Node z){
  Node y; // node to be deleted
  Node x; // child of y



}

void insert(int k){
  Node y = NIL;
  Node x = root;
  Node z;

  z = malloc(sizeof(struct node));
  z->key = k;
  z->left = NIL;
  z->right = NIL;

  while(x!=NIL){
      y=x;
      if(z->key<x->key){
          x=x->left;
      }
      else{
          x=x->right;
      }    
  }
  z->parent=y;

  if(y==NIL){
      root=z;
  }
  else if(z->key<y->key){
      y->left=z;
  }
  else{
      y->right=z;
  }



}

void inorder(Node u){
    if(u!=NIL){
        inorder(u->left);
        printf(" %d",u->key);
        inorder(u->right);
    }

}
void preorder(Node u){
    if(u!=NIL){
        printf(" %d",u->key);
        preorder(u->left);
        preorder(u->right);
    }

}


int main(){
  int n, i, x;
  char com[20];
  scanf("%d", &n);

  for ( i = 0; i < n; i++ ){
    scanf("%s", com);
    if ( com[0] == 'f' ){
      scanf("%d", &x);
      Node t = treeSearch(root, x);
      if ( t != NIL ) printf("yes\n");
      else printf("no\n");
    } else if ( com[0] == 'i' ){
      scanf("%d", &x);
      insert(x);
    } else if ( com[0] == 'p' ){
      inorder(root);
      printf("\n");
      preorder(root);
      printf("\n");
    } else if ( com[0] == 'd' ){
      scanf("%d", &x);
      treeDelete(treeSearch(root, x));
    }
  }

  return 0;
}


Measured memory_bytes: 25140
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i,n) for (int i = 0; i < (n); i++)
typedef pair<int,int> P;
const int INF = 100000000;

int W,H;

int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};

bool movable(vector<vector<char>> &c, vector<vector<int>> &mat, int h, int w) {
  if ((h<0) || (h>=H) || (w<0) || (w>=W)) {
    return false;
  }
  else if (mat[h][w]!=INF || (c[h][w]=='#')) {
    return false;
  }
  else {
    return true;
  }
}


int bfs(vector<vector<char>> &c, vector<vector<int>> &mat) {

  queue<P> que;
  
  rep(h,H) {
      rep(w,W) {
          if(c[h][w]=='#') {
              que.push(P(h,w));
              mat[h][w] = 0;
          }
      }
  }

  while(que.size()) {
    P p = que.front();
    que.pop();

    rep(i,4){
      if (movable(c,mat,p.first+dy[i],p.second+dx[i])) {
        que.push(P(p.first+dy[i],p.second+dx[i]));
        mat[p.first+dy[i]][p.second+dx[i]]=mat[p.first][p.second]+1;
      }
    }
  }
  return 0;
}

int main() {
  int black = 0;
  cin >> H >> W;

  vector<vector<char>> c(H,vector<char> (W));
  
  rep(h,H) {
    rep(w,W) {
      cin >> c[h][w]; 
      if (c[h][w] == '#') {
        black++;
      }
    }
  }

  vector<vector<int>> mat(H,vector<int> (W, INF));
  int ans = bfs(c,mat);
  ans = 0;
  rep(h,H) {
    rep(w,W) {
      if (mat[h][w] > ans) {
        ans = mat[h][w];
      }
    }
  } 
  cout << ans << endl;
}

Measured memory_bytes: 13328
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 100010
#define mod 1000000007
#define ll long long
using namespace std;
ll fac[N],inv[N],f[N];
ll ksm(ll a,ll b=mod-2)
{
	ll ans=1;
	for(;b;b>>=1)
	{
		if(b&1) ans=ans*a%mod;
		a=a*a%mod;
	}
	return ans;
}
int main()
{
	int n,l,r,lt,rt;
	scanf("%d%d%d%d%d",&n,&l,&r,&lt,&rt);
	fac[0]=1;
	for(int i=1;i<=n;i++) fac[i]=fac[i-1]*i%mod;
	inv[n]=ksm(fac[n]);
	for(int i=n-1;i>=0;i--) inv[i]=inv[i+1]*(i+1)%mod;
	f[0]=1;
	for(int i=l;i<=r;i++)
		for(int j=min(i*rt,n);j>=i*lt;j--)
		{
			ll u=ksm(inv[i],lt);
			int v=j-i*lt;
			for(int k=lt;k<=rt;k++)
			{
				f[j]=(f[j]+fac[j]*u%mod*inv[v]%mod*inv[k]%mod*f[v])%mod;
				v-=i,u=u*inv[i]%mod;
				if(v<0) break;
			}
		}
	printf("%d\n",f[n]);
	return 0;
}


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

int main(){
	string s;
	cin >> s;
	static int nex[200010][26];
	static int dp[200010][26];
	for(int i = 0 ; i < 26 ; i ++){
		dp[s.size()][i] = 1;
	}
	for(int i = s.size()-1 ; i >= 0 ; i --){
		for(int j = 0 ; j < 26 ; j ++){
			nex[i][j] = j;
			dp[i][j] = dp[i+1][j];
		}
		int sma = 200010;
		int x = -1;
		for(int j = 0 ; j < 26 ; j ++){
			if(sma > dp[i+1][j]){
				sma = dp[i+1][j];
				x = j;
			}
		}
		nex[i][s[i]-'a'] = x;
		dp[i][s[i]-'a'] = sma+1;
	}
	
	int sma = 200010;
	int x = -1;
	for(int i = 0 ; i < 26 ; i ++){
		if(sma > dp[0][i]){
			sma = dp[0][i];
			x = i;
		}
	}
	string ret = "";
	for(int i = 0 ; i < s.size() ; i ++){
		if(x == s[i]-'a')ret += s[i];
		x = nex[i][x];
	}
	ret += 'a'+x;
	cout << ret << endl;
}



Measured memory_bytes: 41220
#include<iostream>
#include<cmath>

int main(){
  int N,result,count=0;
  std::cin>>N;
  for(int i=0;i<4;i++){
    result = N/std::pow(10,3-i);
    if(result==2) count++;
    N=N-result * std::pow(10,3-i);
  }
  std::cout<<count;
  return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;
public class Main {
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		while(scan.hasNext()){
			String[] str = (scan.nextLine()).split(",");
			int[] dis = new int[10];
			int sum = 0;
			for(int i = 0;i < 10;i++){
				dis[i] = Integer.parseInt(str[i]);
				sum += dis[i];
			}
			int v1 = Integer.parseInt(str[10]);
			int v2 = Integer.parseInt(str[11]);
			int a = (int)(((double)(sum*v1)/(double)(v1+v2))+0.5);
			sum = 0;
			for(int i = 0;i < 10;i++){
				sum += dis[i];
				if(sum >= a){
					System.out.println(i+1);
					break;
				}
			}
		}
	}
}

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

template <typename T, T (*op)(T, T), T (*ie)()> class SegmentTree {
private:
  int N, size, log;
  vector<T> D;
  void apply(int k) { D.at(k) = op(D.at(2 * k), D.at(2 * k + 1)); }
  int ceil_pow(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
  }

public:
  SegmentTree() : SegmentTree(0) {}
  SegmentTree(int n) : SegmentTree(vector<T>(n, ie())) {}
  SegmentTree(const vector<T> &v) : N((int)v.size()) {
    log = ceil_pow(N);
    size = 1 << log;
    D = vector<T>(2 * size, ie());
    for (int i = 0; i < N; i++) D.at(size + i) = v.at(i);
    for (int i = size - 1; i >= 1; i--) apply(i);
  }
  void set(int p, T x) {
    p += size;
    D.at(p) = x;
    for (int i = 1; i <= log; i++) apply(p >> i);
  }
  T get(int p) { return D.at(p + size); }
  T query(int l, int r) {
    r++;
    T sml = ie(), smr = ie();
    l += size, r += size;
    while (l < r) {
      if (l & 1) sml = op(sml, D.at(l++));
      if (r & 1) smr = op(D.at(--r), smr);
      l >>= 1, r >>= 1;
    }
    return op(sml, smr);
  }
  T query_all() { return D.at(1); }
  template <bool (*f)(T)> int max_right(int l) {
    return max_right(l, [](T x) { return f(x); });
  }
  template <class F> int max_right(int l, F f) {
    if (l == N) return N;
    l += size;
    T sm = ie();
    do {
      while (l % 2 == 0) l >>= 1;
      if (!f(op(sm, D.at(l)))) {
        while (l < size) {
          l = (2 * l);
          if (f(op(sm, D.at(l)))) sm = op(sm, D.at(l++));
        }
        return l - size;
      }
      sm = op(sm, D.at(l++));
    } while ((l & -l) != l);
    return N;
  }
  template <bool (*f)(T)> int min_left(int r) {
    return min_left(r, [](T x) { return f(x); });
  }
  template <class F> int min_left(int r, F f) {
    if (r == 0) return 0;
    r += size;
    T sm = ie();
    do {
      r--;
      while (r > 1 && (r % 2)) r >>= 1;
      if (!f(op(D.at(r), sm))) {
        while (r < size) {
          r = (2 * r + 1);
          if (f(op(D.at(r), sm))) sm = op(D.at(r--), sm);
        }
        return r + 1 - size;
      }
      sm = op(D.at(r), sm);
    } while ((r & -r) != r);
    return 0;
  }
};

using mono = int;
mono op(mono a, mono b) { return max(a, b); };
mono ie() { return 0; };

template <typename T, typename U> bool cmin(T &a, U b) { return a > b && (a = b, 1); }
template <typename T, typename U> bool cmax(T &a, U b) { return a < b && (a = b, 1); }

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

  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) cin >> A.at(i);

  int MX = 3e5;
  SegmentTree<mono, op, ie> ST(MX + 1);
  for (int i = 0; i < N; i++) {
    int l = A.at(i) - K;
    cmax(l, 0);
    int r = A.at(i) + K;
    cmin(r, MX);
    ST.set(A.at(i), ST.query(l, r) + 1);
  }
  cout << ST.query_all() << "\n";
}

Measured memory_bytes: 9784
ni,ki = [int(i) for i in input().split()]
arri = [int(i) for i in input().split()]
    
def getRes(arr,n,k):
    dp = [0]*n
    for i in range(1,n):
        H = arr[i]
        dp[i] = min([dp[j]+abs(arr[j]-H) for j in range(max(0,i-k),i)])
    return dp[-1]

print(getRes(arri,ni,ki))


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

bool is_in(int i, vector<int> vec) {
    for (int j : vec) {
        if (j == i) {
            return true;
        }
    }

    return false;
}

bool is_valid(int n, vector<int> D) {
    while(n) {
        int digit = n % 10;
        if (is_in(digit, D)) {
            return false;
        }
        n = n / 10;
    }

    return true;
}

int main()
{
    int N, K;
    cin >> N >> K;

    vector<int> D;
    int d;
    for (int i = 0; i < K; i++) {
        cin >> d;
        D.push_back(d);
    }

    int ans = N;
    while(1) {
        if (is_valid(ans, D)) {
            break;
        } else {
            ans++;
        }
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL<<60;
const long long int Mod = 1000000007;
using ll = long long int; using ci = const int;
using vi = vector<int>;  using Vi = vector<long long int>;
using P = pair<int, int>;  using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define rp(i,N) for(ll i = 0; i < (ll)N; i++)
#define repi(i,a,b) for(ll i = ll(a); i < ll(b); ++i)
template<class T>bool chmax(T &former, const T &b) { if (former<b) { former=b; return true; } return false; }
template<class T>bool chmin(T &former, const T &b) { if (b<former) { former=b; return true; } return false; }
template<class T>T sqar(T x){ return x*x; }//sqrt(x)は平方根;
#define Sort(v) std::sort(v.begin(), v.end(), std::greater<decltype(v[0])>()) //降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v> >
template<class T> inline void princ(T x){cout<<x<<" ";}; 
template<class T> inline void print(T x){cout<<x<<"\n";};
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
///////////////////////////////////////////////////////////////////////////////////
int ctr[505][505];
int sum[505][505];
int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    std::cout<<std::fixed<<std::setprecision(30);
    int n,m,q;
    cin >> n >> m >> q;
    vi ans;
    rp(i,m){
        int l,r;
        cin >> l >> r;
        ctr[l][r-l]++;
    }
    repi(i,1,n+1){
            sum[i][0]=ctr[i][0];
        repi(j,1,n){
            sum[i][j]=sum[i][j-1]+ctr[i][j];
        }
    }
    rp(i,q){
        int p,s;
        cin >> p >> s;
        int res=0;
        repi(j,p,s+1){
            res += sum[j][s-j];
        }
        ans.pb(res);
    }
    for(auto a:ans){
        cout << a << endl;
    }
    return 0;
}

Measured memory_bytes: 3448
import bisect
import heapq
import sys
N,Q = [int(x) for x in sys.stdin.readline().split()]

hitos = []
ans = [-1 for _ in range(Q)]
queries =[]
jump = ans[::]



queries = [[int(x) for x in sys.stdin.readline().split()] for i in range(N)]
hitos = [int(sys.stdin.readline()) for i in range(Q)]


# for i in range(N):
    # S,T,X =list(map(int,input().split(" ")))
    # lt = S -X -0.5
    # rt = T -X -0.5
    # queries.append([lt,rt,X]) #-1 → 挿入クエリ lが時間、-1がクエリの種類、Xが場所


queries.sort(key=lambda x:x[2])





for l,r,x in queries:
     lind  = bisect.bisect_left(hitos,l-x - 0.5)
     rind  = bisect.bisect_left(hitos,r -x - 0.5)
     while lind < rind:
         if ans[lind] == -1:
            ans[lind] = x
            jump[lind] = rind
            lind += 1
         else:

             lind = jump[lind]

for i  in ans:
    print(i)


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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i,n) for(int i=0;i<(int)n;++i)

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

template < typename T >
void vprint(T &v){
	REP(i, v.size()){
		cout << v[i] << " ";
	}
	cout << endl;
}

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

	ll res = 0;
	REP(i, N-1){
		if(a[i]==a[i+1]){
			res++;
			a[i+1] = -1;
		}
	}
	cout << res << endl;
    return 0;
}

Measured memory_bytes: 256
def main():

    def hurui():
        N = 10 ** 5
        p = [1] * (N+1)
        p[0], p[1] = 0, 0
        for i in range(4, N+1, 2): p[i] = 0
        for i in range(3, N+1):
            if i * i > N: break
            if p[i]:
                for j in range(i, N+1):
                    if i*j > N: break
                    p[j*i] = 0
        return p

    p = hurui()
    q = [0] * (10**5+1)
    for i in range(1, 10 ** 5 + 1):
        if i%2 == 0: continue
        if p[i] and p[(i+1)//2]: q[i] = 1
    r = [0] * (10**5+2)
    for i in range(10**5+1):
        r[i+1] = r[i] + q[i]

    Q = int(input())
    query = [[int(_) for _ in input().split()] for __ in range(Q)]
    for a, b in query:
        print(r[b+1] - r[a])
    return

if __name__ == '__main__':
    main()


Measured memory_bytes: 26556
N, X, M = map(int, input().split())
N -= 1
L = 1
while 1 << L <= N:
    L += 1
table = [[None] * M for _ in range(L)]
for i in range(M):
    x = i * i % M
    table[0][i] = (x, x)
for i in range(1, L):
    for j in range(M):
        k, s = table[i-1][j]
        l, t = table[i-1][k]
        table[i][j] = (l, s + t)
ans = X
for i in range(L)[::-1]:
    if (1 << i) <= N:
        j, s = table[i][X]
        ans += s
        N -= (1 << i)
        X = j
print(ans)

Measured memory_bytes: 172260
def mergecount(A):
    cnt = 0
    n = len(A)
    if n>1:
        A1 = A[:n>>1]
        A2 = A[n>>1:]
        cnt += mergecount(A1)
        cnt += mergecount(A2)
        i1 = 0
        i2 = 0
        for i in range(n):
            if i2 == len(A2):
                A[i] = A1[i1]
                i1 += 1
            elif i1 == len(A1):
                A[i] = A2[i2]
                i2 += 1
            elif A1[i1] <= A2[i2]:
                A[i] = A1[i1]
                i1 += 1
            else:
                A[i] = A2[i2]
                i2 += 1
                cnt += n//2 - i1
    return cnt
 
 
n = int(input())
l = [int(_) for _ in input().split()]
p = (n*n+n)/2
q = 0
if p%2 == 0:
    q = p//2
else:
    q = p//2 + 1
l_set = list(set(l))
l_sorted = sorted(l_set)
ans = 0
l_t = [0 for _ in range(n)]
s = [0 for _ in range(n+1)]
 
low = 0
high = len(l_sorted) - 1
 
 
while low <= high:
    mid = (low + high) // 2
    m = l_sorted[mid]
    for j in range(n):
        if l[j] < m:
            l_t[j] = -1
        else:
            l_t[j] = 1
    for k in range(n):
        s[k+1] = s[k] + l_t[k]
 
    x = mergecount(s)
    if p-x >= q:
        low = mid+1
        ans = max(m,ans)
    else:
        high = mid-1
print(ans)

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

using namespace std;
using ll = long long;

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

  ll n;
  cin >> n;
  vector<ll>a (n);
  for (ll i = 0; i < n; i++) cin >> a[i];
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < i; j++) {
      ans += (a[i] * a[j]);
    }
  }
  cout << ans << '\n';
  return 0;
}



Measured memory_bytes: 256
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cstdio>
#include<cmath>
#include<deque>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<string>
#include<tuple>
#include<utility>
#include<vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
typedef vector<vector<int>> vvi;

#define rep(i, a, n)  for(int i=a; i<n; i++)
#define REP(i, n)  for(int i=0; i<(int)(n); i++)
#define REPS(i, n)  for(int i=1; i<=(int)(n); i++)
#define PER(i, n) for(int i=(int)(n)-1; i>= 0; i--)
#define PERS(i, n)  for(int i=(int)(n); i>0; i--)
#define FOR(i, c) for(__typeof((c).begin()) i = (c).begin(); i!=(c).end(); i++)
#define RFOR(i, c)  for(__typeof((c).rbegin()) i=(c).rbegin(); i!=(c).end(); i++)
#define ALL(container)  (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) (container).size()
#define FILL0(n)  setfill('0') << right << setw(n)
#define mp(a, b)  make_pair(a, b)
#define toLower(c)  c+0x20
#define toUpper(c)  c-0x20
#define pb  push_back
#define eb  emplace_back

const int INF = 1e9;
const long long LLINF = 1e18;
const ull MOD = 998244353;
const int MSIZE = 100007;
const double Pi = 3.14159265358979323146;

int main(){
  bool P[101];
  REP(i, 101){ P[i] = true;}
  int x, n; cin >> x >> n;
  REP(i, n){
    int p;  cin >> p;
    P[p] = false;
  }

  REP(i, 101){
    if(P[x-i]){
      cout << x-i << endl;
      return 0;
    }
    if(P[x+i]){
      cout << x+i << endl;
      return 0;
    }
  }
}


Measured memory_bytes: 3600
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String []args){
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(1,in,out);
        out.close();
    }
    static class Task{
        public void solve(int testNumber,Scanner in,PrintWriter out){
            String n = in.next();
            if(n.charAt(0) == n.charAt(1) && n.charAt(1) == n.charAt(2)){
                out.println("Yes");
            } else if(n.charAt(1) == n.charAt(2) && n.charAt(3) == n.charAt(2)){
                out.println("Yes");
            } else {
                out.println("No");
            }
        }
    }
}


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

int n; string s;

signed main() {
    cin>>n>>s;
    vi r, g, b;
    rep(i,n) {
        switch (s[i])
        {
        case 'R':
            r.push_back(i);
            break;
        case 'G':
            g.push_back(i);
            break;
        case 'B':
            b.push_back(i);
            break;
        default:
            break;
        }
    }
    int n2 = 0;
    rep(i,n) {
        FOR(k,i+1,n) {
            if(s[i] == s[k] || (i+k)%2!=0) continue;
            int j = (i+k)/2;
            if(s[j] != s[i] && s[j] != s[k]) n2++;
        }
    }
    int ans = r.size() * g.size() * b.size() - n2;
    cout<<ans;
    return 0;
}

Measured memory_bytes: 3680
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <utility>
#include <tuple>
#include <math.h>
#include <set>
#include <map>
using namespace std ;
using ll = long long ;
using ld = long double ;
using vll = vector<ll> ;
using vvll = vector<vll> ;
using vc = vector<char> ;
using vvc = vector<vc> ;
using vb = vector<bool> ;
using vvb = vector<vb> ;
using pll = pair<ll,ll> ;
#define all(v) v.begin(),v.end() 
const ll INF = 1e18 ;
const ll mod = 1e9+7 ;
const double pie = acos(-1); 
vll dx4 = {-1,0,1,0} ;
vll dy4 = {0,-1,0,1} ;
vll dx8 = {-1,0,1,1,1,0,-1,-1} ;
vll dy8 = {-1,-1,-1,0,1,1,1,0} ; 

void fix_cout(){cout << fixed << setprecision(20) ; }
ll gcd(ll a,ll b){if(b==0) return a ; return gcd(b,a%b) ; }
ll lcm(ll a,ll b){return a/gcd(a,b)*b ; }
void chmax(ll &a,ll b){if(a<b) a = b ; }
void chmin(ll &a,ll b){if(a>b) a = b ; }

int main(){
	ll n ;
	cin >> n ;
	cout << 360/gcd(n,360) << endl ;
}

Measured memory_bytes: 3628
object Main{
  def main(args: Array[String]){
    if (sys.env.getOrElse("TEST", "")=="1"){
      println(test());
    }else{
      val input=io.Source.stdin.getLines().mkString("\n");
      println(solve(input).trim());
    }
  }

  def solve(input:String):String={
    val (a,b)=input.split("\n")(1).split(" ").map(_.toInt).sorted.reverse.zipWithIndex.foldRight((0,0)){case ((x,i),(an,bo)) => if(i%2==0){(an+x,bo)}else{(an,bo+x)}};
    (a-b).toString()
  }

  def bitween(x:Int,min:Int,max:Int)=min<=x&&x<=max;

  val tests=List("""2
3 1""" -> """2""",
    """3
2 7 4""" -> """5""",
"""4
20 18 2 18
"""->"""18""");

  def test():String= {
    return tests.map{case (i,o)=>(i.trim(),o.trim())}.zipWithIndex.map{case ((input,outputExpect),i)=>{
      val output=solve(input).trim();
      s"test${i+1}:"+(if(output==outputExpect){
        "Passed"
      }else{
        s"Failed\nexpect:\n${outputExpect}\noutput:\n${output}"
      })
    }}.mkString("\n");
  }
}

Measured memory_bytes: 27212
n=int(input())
l=[]
k=0
for i in range(n):
    d=[x for x in input().split()]
    l.append(d)
    
for i in range(n-2):
    if l[i][0]==l[i][1] and l[i+1][0]==l[i+1][1] and l[i+2][0]==l[i+2][1]:
        k+=1
        break

if k==0:
    print("No")
else:
    print("Yes")

Measured memory_bytes: 9200
n = int(input())
a = [int(input()) for _ in range(n)]
lis = ["0"]*n
bot = a[0]
lis[0] = "1"
cnt = 1

while True:
    if bot == 2:
        print(cnt)
        break
    elif bot != 2 and lis[bot-1] == "1":
        print(-1)
        break
    else:
        cnt += 1
        lis[bot-1] = "1"
        bot = a[bot-1]
        

Measured memory_bytes: 7852
/*Function Template*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pint;
const int MAX = 510000;
const int MOD = 1000000007;
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define Rep(i, n) for(ll i = 1; i < (n); i++)
#define ALL(a)  (a).begin(),(a).end()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll fac[MAX], finv[MAX], inv[MAX];


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;
}
// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (ll i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }

}
ll Len(ll n) {
  ll s=0;
  while(n!=0) s++, n/=10;
  return s;
}


ll Sint(ll n) {
  ll m=0,s=0,a=n;
  while(a!=0) s++, a/=10;
  for(ll i=s-1;i>=0;i--) m+=n/((ll)pow(10,i))-(n/((ll)pow(10,i+1)))*10;
  return m;
}


ll Svec(vector<ll> v){
  ll n=0;
  for(ll i=0;i<v.size();i++) n+=v[i];
  return n;
}


ll GCD(ll a,ll b) {
  return b ? GCD(b,a%b) : a;
}


ll LCM(ll a,ll b){
  return a/GCD(a,b)*b;
}


ll Factorial(ll n){
  ll m=1;
  while(n>=1) m*=n,n--;
  return m;
}

void runlength(string s,vector<pair<char,ll>> &p){
  ll x=1;
  if(s.size()==1){
    p.push_back(pair<char,ll>(s[0],1));
  }
  for(ll i=0;i<s.size()-1;i++){
    if(s[i]==s[i+1]){
      x++;
      if(i==s.size()-2){
        p.push_back(pair<char,ll>(s[i],x));
      }
    }else{
      p.push_back(pair<char,ll>(s[i],x));
      x=1;
      if(i==s.size()-2){
        p.push_back(pair<char,ll>(s[s.size()-1],x));
      }
    }
  }
}




ll COM(ll n,ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

const int MAX_N=100010;
vector<bool> sieve_of_eratosthenes(){
  vector<bool> isPrime(MAX_N+1,true);
  for(int i=2;i<=MAX_N;i++){
    if(isPrime[i]){
      for(int j=2*i;j<=MAX_N;j+=i){
        isPrime[j]=false;
      }
    }
  }
  return isPrime;
}

vector<pint> prime_factorize(ll n){
  vector<pint> ans;
  for(ll p=2;p<=sqrt(n);p++){
    if(n%p!=0) continue;
    ll cnt=0;
    while(n%p==0){
      n/=p;
      cnt++;
    }
    ans.push_back(make_pair(p,cnt));
  }
  if(n!=1) ans.push_back(make_pair(n,1));
  return ans;
}
ll n,q;
vector<ll> graph[200010];
ll value[200010];
void dfs(ll v,ll p){
  if(p!=-1) value[v]+=value[p];
  for(auto tmp:graph[v]){
    if(p==tmp) continue;
    dfs(tmp,v);
  }
}


/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
int main() {
  IOS;
  
  cin>>n>>q;
  rep(i,n-1){
    ll a,b;cin>>a>>b;a--;b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  
  fill(value,value+200010,0);
  rep(i,q){
    ll a,b;cin>>a>>b;a--;
    value[a]+=b;
  }
  dfs(0,-1);
  rep(i,n) cout<<value[i]<<' ';
  cout<<endl;
}

Measured memory_bytes: 26240
#include <bits/stdc++.h>
#define rep(i,a,b) for(ll i=ll(a);i<ll(b);i++)
#define irep(i,a,b) for(ll i=ll(a);i>=ll(b);i--)
#define pb push_back
#define mp make_pair
#define pll pair<ll,ll>
#define endl "\n"
using ll=long long;
using ld=long double;
using namespace std;
ll mod= 1e9+7;
ll GCD(ll a, ll b) { return b ? GCD(b, a%b) : a; }

int main(){
    ll n,ans=0;
    cin>>n;
    rep(i,1,n+1){
        ans+=(i+(n/i)*i)*(n/i)/2;
    }
    cout<<ans;
}

Measured memory_bytes: 3620
//============================================================================
// Name        : AOJ.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;
long long P[100001];
long long A[100001],B[100001],C[100001];
int D[100001];
int main() {
	int N,M;
	long long R=0;

	cin>>N>>M;
	for(int i=1;i<M+1;i++)cin>>P[i];

	for(int i=1;i<N;i++)cin>>A[i]>>B[i]>>C[i];

	for(int i=1;i<M;i++){
		if(P[i]<P[i+1]){
			for(int j=P[i];j<P[i+1];j++)D[j]+=1;
		}else{
			for(int j=P[i+1];j<P[i];j++)D[j]+=1;
		}
	}

	for(int j=1;j<N;j++)R+=min(D[j]*A[j],D[j]*B[j]+C[j]);

	cout<<R<<endl;
	return 0;
}

Measured memory_bytes: 6600
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <climits>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

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

    int n, k;

    cin >> n >> k;

    if (k == 1)
        cout << 0 << endl;
    else
        cout << (n - k) << endl;

    return 0;
}

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

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

fn neighbor_indexes(dice_index: usize) -> Vec<usize> {
    match dice_index {
        0 => return vec![1, 2, 4, 3],
        1 => return vec![0, 3, 5, 2],
        2 => return vec![0, 1, 5, 4],
        3 => return vec![0, 4, 5, 1],
        4 => return vec![0, 2, 5, 3],
        5 => return vec![1, 3, 4, 2],
        _ => return vec![],
    }
}

fn right_index(dice_top: usize, dice_front: usize) -> usize {
    let neighbor_indexes: Vec<usize> = neighbor_indexes(dice_top);
    let position: usize = neighbor_indexes
        .iter()
        .position(|&i| i == dice_front)
        .unwrap();
    return neighbor_indexes[(position + 1) % 4];
}

fn main() {
    let mut dice: Vec<u8> = vec![];

    for _ in 0..6 {
        let number: u8 = read();
        dice.push(number);
    }

    let q: u8 = read();
    for _ in 0..q {
        let top_number: u8 = read();
        let front_number: u8 = read();

        let top_index: usize = dice.iter().position(|&n| n == top_number).unwrap();
        let front_index: usize = dice.iter().position(|&n| n == front_number).unwrap();
        println!("{}", dice[right_index(top_index, front_index)]);
    }
}



Measured memory_bytes: 5028
#include <iostream>  
#include <string.h> 
using namespace std;
int main() {
    int a, b;
    cin >> a >> b;
    if ((a + b) % 3 == 0||a%3==0||b%3==0)
        cout << "Possible";
    else
        cout << "Impossible";

    return 0;
}


Measured memory_bytes: 3592
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <algorithm>
#include <cmath>
#include <climits>
#include <iomanip>

using namespace std;

typedef long long ll;

int main() {
    set<int> s;

    int n;  cin >> n;
    for (int i = 0; i < n; i++) {
        int c;  cin >> c;
        s.insert(c);
    }

    int ans = 0;
    for (auto ite = s.begin(); ite != s.end(); ite++) {
        ans++;
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 256