akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <bitset>
#include <vector>
#include <complex>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << x << endl
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int N = 202;
int n,a[N],b[N];

bool check(){
    int L = a[0] , R = a[n-1];
    if(L != (R+1)/2) return false;
    if(b[L] > 2) return false;
    if(b[L] != (R&1) + 1) return false;
    rep(i,L+1,R+1) if(b[i] < 2) return false;
    return true;
}

int main(){
    scanf("%d",&n);
    rep(i,0,n) scanf("%d",a + i),b[a[i]]++;
    sort(a,a+n);
    puts(check() ? "Possible" : "Impossible");
    return 0;
}


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

#define TRACE(x) x
#define WATCH(x) TRACE(cout << #x" = " << x << endl)
#define WATCHR(a, b) TRACE(for (auto it=a; it!=b;) cout << *(it++) << " "; cout << endl)
#define WATCHC(V) TRACE({cout << #V" = "; WATCHR(V.begin(), V.end());})

#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

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

    int N;
    cin >> N;

    vi a(N), b(N), c(N);

    for (int i = 0; i < N; i++)
        cin >> a[i];
    sort(all(a));

    for (int i = 0; i < N; i++)
        cin >> b[i];
    sort(all(b));

    vi wB(N);
    for (int i = 0, j = 0; i < N; i++) {
        while (j < N && a[j] < b[i])
            j++;
        wB[i] = j;
    }

    for (int i = 0; i < N; i++)
        cin >> c[i];
    sort(all(c));

    ll ans = 0, pref = 0;
    for (int i = 0, j = 0; i < N; i++) {
        while (j < N && b[j] < c[i])
            pref += wB[j++];
        ans += pref;
    }

    cout << ans << endl;

    return 0;
}



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

using namespace std;

using ll = long long int;

int main() {
    // Input
    int N, K;
    vector<short> D;

    cin >> N >> K;
    D = vector<short>(K);
    rep(i, K) cin >> D[i];

    // Process
    int idx = 0;
    vector<int> allow;
    rep(i, 10) {
        if (i == D[idx])
            idx++;
        else
            allow.emplace_back(i);
    }

    vector<int> allow_nums;
    int before_size = 0;
    rep(i, 5) {
        if (i == 0) {
            for (int n : allow) allow_nums.emplace_back(n);
        } else {
            const int size = allow_nums.size();
            for (int j = before_size; j < size; j++) {
                if (allow_nums[j] == 0) continue;

                for (int n : allow)
                    allow_nums.emplace_back(allow_nums[j] * 10 + n);
            }
            before_size = size;
        }
    }

    int ans = *lower_bound(allow_nums.begin(), allow_nums.end(), N);

    // Output
    cout << ans << endl;

    return 0;
}


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

using namespace std;

int main() {
  string s, t;
  
  cin >> s >> t;
  sort(s.begin(), s.end());
  sort(t.begin(), t.end(), greater<char>());
  
  if(s < t) cout << "Yes" << endl;
  else cout << "No" << endl;
  
  return 0;
}

Measured memory_bytes: 384
#include "bits/stdc++.h"
#include <sys/timeb.h>
#include <fstream>

using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define replrev(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define reprev(i,n) replrev(i,0,n)
#define repi(itr,ds) for(auto itr = ds.begin(); itr != ds.end(); ++itr)
#define mp make_pair
#define INF 2000000000
#define INFL 9000000000000000000LL
#define EPS 1e-9
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int> > vvi;
typedef vector<vector<bool> > vvb;
typedef vector<vector<ll> > vvll;
typedef vector<vector<char> > vvc;
typedef vector<vector<double> > vvd;
typedef vector<vector<P> > vvP;
typedef priority_queue<int, vector<int>, greater<int> > pqli;
typedef priority_queue<ll, vector<ll>, greater<ll> > pqlll;
typedef priority_queue<P, vector<P>, greater<P> > pqlP;
typedef pair<int, pair<int, int> > Edge;
typedef vector<Edge> vE;
typedef priority_queue<Edge, vector<Edge>, greater<Edge> > pqlE;


int main() {
	int a, b, c;
	cin >> a >> b >> c;
	if (a % 2 == 1 || b % 2 == 1 || c % 2 == 1) {
		cout << 0 << endl;
	}
	else if (a == b && b == c) {
		cout << -1 << endl;
	}
	else {
		repl(i, 1, 32) {
			int n = 1 << i;
			if (a%n != b%n || b%n != c%n) {
				cout << i - 1 << endl;
				return 0;
			}
		}
	}

	return 0;
}

Measured memory_bytes: 256
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

import javax.swing.text.Segment;

public class Main {
	
	public static void main(String[] args) throws IOException {
		try(Scanner sc = new Scanner(System.in)){
			final String s = sc.next();
			StringBuilder sb = new StringBuilder(s);
			
			System.out.println((sb.reverse().toString().equals(s)) ? "Yes" : "No");
			
			
		}
	}
	
	public static class Scanner implements Closeable {
		private BufferedReader br;
		private StringTokenizer tok;
 
		public Scanner(InputStream is) throws IOException {
			br = new BufferedReader(new InputStreamReader(is));
		}
 
		private void getLine() throws IOException {
			while (!hasNext()) {
				tok = new StringTokenizer(br.readLine());
			}
		}
 
		private boolean hasNext() {
			return tok != null && tok.hasMoreTokens();
		}
 
		public String next() throws IOException {
			getLine();
			return tok.nextToken();
		}
 
		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}
 
		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}
 
		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}
		
		public int[] nextIntArray(int n) throws IOException {
			final int[] ret = new int[n];
			for(int i = 0; i < n; i++){
				ret[i] = this.nextInt();
			}
			return ret;
		}
		
		public long[] nextLongArray(int n) throws IOException {
			final long[] ret = new long[n];
			for(int i = 0; i < n; i++){
				ret[i] = this.nextLong();
			}
			return ret;
		}
 
		public void close() throws IOException {
			br.close();
		}
	}
}

Measured memory_bytes: 21332
#include "bits/stdc++.h"
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    rep(i, n){
    	int a;
    	cin >> a;
    	if(a%2 == 1) continue;
    	if(a%3 == 0) continue;
    	if(a%5 == 0) continue;
    	cout << "DENIED" << endl;
    	return 0;
    }
    cout << "APPROVED" << endl;
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long
#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }


using namespace std;

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

    int n;
    int d[10];
    ll s;
    cin >> s;
    while(s > 0){
        d[n] = s % 10;
        s /= 10;
        n++;
    }

    reverse(d, d+n);

    rep(i, n){
        d[i] = (int)d[i];
    }

    ll ans = 0;

    rep(i, 1 << (n-1)){
        ll tmp = 0;
        ll res = d[0];
        rep(j, n-1){
          if (i & (1 << j)){
              tmp += res;
              res = d[j+1];
          } else {
              res = res*10 + d[j+1];
          }
        }
        ans += tmp + res;
    }

    cout << ans << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <sstream>
#include <cmath>
#include <math.h>
#include <string>
using namespace std;
typedef long long ll;
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n , a , b;
    cin >> n >> a >> b;
    int x= 0;
    for (int i = 1; i <= n ; i++ ) {
        int y = i;
        int t = 0;
        while ( y >= 1 ) {
             t+= y % 10;
            y /= 10;
        }
        if ( t >= a && t <= b ) x += i;
    }
    cout << x;
}

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

int main() {
  int N;
  scanf("%d", &N);
  char S[N+1], T[N+1];
  scanf("%s %s", S, T);
  char ans[2*N+1];
  for (int i = 0; i < N; i++) {
    ans[2*i] = S[i];
    ans[2*i+1] = T[i];
  }
  ans[2*N] = '\0';
  printf("%s\n", ans);
  return 0;
}


Measured memory_bytes: 128
#include <stdio.h>

void display(int n, int m) {
    int i, j;
    for(i = 0; i < n; i++) {
        if(i%2 == 1) {
            for(j = 0; j < m; j++) {
                if(j%2 == 1) {
                    printf("#");
                } else {
                    printf(".");
                }
            }
        } else {
            for(j = 0; j < m; j++) {
                if(j%2 == 1) {
                  printf(".");  
                } else {
                    printf("#");
                }
            }
        }
        printf("\n");
    }
}

int main() {
    int i, j, k;
    int num[300][300] = {0};
    
    i = 0;
    while(1) {
        j = 0;
        scanf("%d %d", &num[i][j], &num[i][j+1]);
        if(num[i][j] == 0 && num[i][j] == 0) break;
        i++;
    }
    
    for(k = 0; k < i; k++) {
        j = 0;
        display(num[k][j], num[k][j+1]);
        printf("\n");
    }
    
    return 0;
}




Measured memory_bytes: 2088
#include <stdio.h>

int main(void) {
	int div[100];
	int a, b, n;
	int i, j;
	int ans;
	while (scanf("%d %d %d", &a, &b, &n) != EOF) {
		for (i = 0; i < 100; i++) {
			if (a / b) {
				a %= b;
				i--;
			}
			else {
				a *= 10;
				div[i] = a / b;
			}
		}
		ans = 0;
		for (i = 0; i < n; i++) {
			ans += div[i];
		}
		printf("%d\n", ans);
	}
	return 0;
}

Measured memory_bytes: 596
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<cstring>
#include<math.h>
#include<bitset>
#include<queue>
#include<set>
#include<iomanip>
#include<assert.h>
#include<string>
#include<cstdlib>
#include<cctype>
#include<unordered_map>
#include<sstream>
#include<numeric>
#include<stack>
// #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
constexpr ll INF = 9e18;
constexpr int inf = 1e9;
const double INFD = 1e100;
const ll mod = 1000000007;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
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やllをstringに変換
template<typename T> inline string toString(const T &a){
  ostringstream oss;
  oss << a;
  return oss.str();
};
// ----------------------------------------------------------------------------
// snukeさんのmodint
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
};

// mint N;

ll RepeatSquaring(ll N, ll P, ll M){
    if(P==0) return 1;
    if(P%2==0){
        ll t = RepeatSquaring(N, P/2, M);
        return t*t % M;
    }
    return N * RepeatSquaring(N, P-1, M);
}

int main(){
  ll N,a,b;
  cin >> N >> a >> b;
  mint ans = 0;
  ans = RepeatSquaring(2,N,mod);
  ans -= 1;
  mint A=1,B=1;
  for(int i=1; i<=a; i++){
    A *= N-i+1;
    A /= i;
  }
  for(int i=1; i<=b; i++){
    B *= N-i+1;
    B /= i;
  }
  ans -= A+B;
  cout << ans.x << endl;
  return 0;
}


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

using namespace std;

int a[200010];

int main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    for(int i=0;i<n;++i){
        cin >> a[i];
    }

    int brick = 0;
    int counter = 0;

    for(int i=0;i<n;++i){
        if(a[i] == brick+1){
            brick++;
        }else{
            counter++;
        }
    }
    
    if(brick){
        cout << counter << '\n';
    }else cout << -1 << '\n';

    return 0;
}

Measured memory_bytes: 1024
#include<bits/stdc++.h>
#define ll long long
#define pii pair<ll,ll>
#define debug(a) cout<<a<<'\n'
#define maxn 300009 /// I wanna be the wind
#define MOD 1000000007
#define F first
#define S second
#define rep(i, a, b) for(ll i = a; i < (b); ++i)
#define per(i, b, a) for(ll i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define all(x) begin(x), end(x)
#define sz(x) (ll)(x).size()
using namespace std;
const ll INF = 1e14 + 9;
ll mult(ll a,ll b){
    return a*b%MOD;
}
ll fastpow(ll a, ll b){
    ll calc=1;
    for(;b>=1LL;b/=2LL){
        if((b&1LL))calc=mult(calc,a);
        a=mult(a,a);
    }
    return calc;
}
ll soma(ll a,ll b){
    if(a+b>MOD)return a+b-MOD;
    else return a+b;
}
ll subt(ll a,ll b){
    if(a-b<0)return a-b+MOD;
    else return a-b;
}
ll dp[2002][2002];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n;
    cin>>n;
    dp[0][0]=1;
    rep(i,1,2000){
        vector<ll> prefix_dp(n+2,0);
        prefix_dp[0] = dp[i-1][0];
        rep(sum,1,n+1){
            prefix_dp[sum] = soma(dp[i-1][sum] , prefix_dp[sum-1]);
        }
        rep(sum,3*i,n+1){
            dp[i][sum] = soma(dp[i][sum],prefix_dp[sum-3]); 
        }
    }
    ll ans=0;
    rep(i,1,2000){
        ans = soma(ans,dp[i][n]);
    }
    cout<<ans<<"\n";
    return 0;  
}   

Measured memory_bytes: 11092
#include<string>
#include<iostream>
#include <algorithm>
#include<vector>
#include<functional>
using namespace std;



int main(){
    int N,M,sum=0;
  double M_s;
    cin>>N>>M;
  
    int A[N];
    for(int i=0;i<N;i++){
        cin>>A[i];
    }
  for(int k=0;k<N;k++){
    sum=sum+A[k];
  }
  M_s=(double)sum/(double)(4*M);

    sort(A,A+N,greater<int>());

    if((double)A[M-1]>=M_s) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;


    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
#define SortAsc(c) std::sort(c.begin(), c.end())
#define SortDesc(c) std::sort(c.rbegin(), c.rend())
int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};

int main() {
  ll a, b, c, k;
  cin >> a >> b >> c >> k;

  if (k <= a) {
    cout << k << endl;
  } else if (k <= a + b) {
    cout << a << endl;
  } else {
    cout << a - (k - a - b) << endl;
  }
}

Measured memory_bytes: 3636
n,k=map(int,input().split())
A=[int(i) for i in input().split()]
mod=10**9+7
import math
import bisect

n_max=2000
nn=int(math.log2(n_max))+1
BIT=[0]*(2**nn)
BIT.insert(0,0)

def bitsum(BIT,i):
  s=0
  while i>0:
    s+=BIT[i]
    i-=i&(-i)
  return s

def bitadd(BIT,i,x):
  while i<=n_max:
    BIT[i]+=x
    i+=i&(-i)
  return BIT

ans1=0
for i in range(n):
  ans1+=i-bitsum(BIT,A[i])
  bitadd(BIT,A[i],1)

ans2=0
A.sort()
for i in range(n-1):
  ans2+=n-bisect.bisect_right(A,A[i])

ans=(ans1*k)%mod
ans=(ans+ans2*k*(k-1)//2)%mod
print(ans)

Measured memory_bytes: 3188
#include<bits/stdc++.h>
#define int long long
#define x first
#define y second
#define pb push_back

#define speed ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

const int INF=1000000000000000;
struct edge
{
    int a;
    int b;
    int cost;
};
std::vector<edge> e,te;
std::vector<std::vector<int>> g;
std::vector<bool> pathf,vis;
void fun(int n)
{
    pathf.resize(n,0);
    stack<int> st;
    st.push(0);
    while(!st.empty())
    {
        int pres=st.top();
        st.pop();
        pathf[pres]=1;
        for(auto it:g[pres])
        {
            if(!pathf[it])
            {
                st.push(it);
            }
        }
    }
    for(int i=1;i<n;++i)
    {
        if(pathf[i]){//pathf[i]=1 ie reachable
            st.empty();st.push(i);
            vis.clear();vis.resize(n,0);
            while(!st.empty())
            {
                int pres=st.top();
                st.pop();
                vis[pres]=1;
                for(auto it:g[pres])
                {
                    if(!vis[it])
                    {
                        st.push(it);
                    }
                }
            }
            if(!vis[n-1]){pathf[i]=0;}
        }
    }
}
signed main()
{
    int n,m,p;cin>>n>>m>>p;
    e.resize(m);
    g.resize(n);
    for(int i=0;i<m;++i)
    {
        cin>>e[i].a>>e[i].b>>e[i].cost;
        (e[i].a)--;(e[i].b)--;
        e[i].cost=(p-e[i].cost);
        g[e[i].a].pb(e[i].b);
    }
    fun(n);
    te=e;e.clear();
    for(int i=0;i<m;++i)
    {
        if(pathf[te[i].a]&&pathf[te[i].b])
        {
            e.pb(te[i]);
        }
    }
    m=e.size();
    std::vector<int> d(n,INF);
    d[0]=0;
    bool b=0;
    for(int i=0;i<n;++i)
    {
        b=0;
        for(int i=0;i<m;++i)
        {
            if(d[e[i].a]!=INF)
            {
                if((d[e[i].a]+e[i].cost)<d[e[i].b])
                {
                    d[e[i].b]=(d[e[i].a]+e[i].cost);
                    b=1;
                }
            }
        }
        if(!b){break;}
    }
    if(b)
    {
        cout<<-1<<"\n";
    }
    else
    {
        cout<<max(d[n-1]*(-1),0ll)<<"\n";
    }



}

Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> mp;
#define inf 1e9


int main(){
	int n;
	cin>>n;
	int cnt=0;
	int ab=0,sb=0,ea=0;
	for(int i=0;i<n;i++){
		string s;
		cin>>s;
		for(int i=0;i<s.size();i++){
			if(s[i]=='A'&&s[i+1]=='B')cnt++;
		}
		if(s[0]=='B'&&s[s.size()-1] == 'A')ab++;
		else if(s[0]=='B')sb++;
		else if(s[s.size()-1] == 'A') ea++;
	}
	int res = abs(sb-ea);
	cnt+= min(sb,ea);
	if(ab!=0){
		cnt+=ab-1;
		if(ea!=0||sb!=0)cnt++;
	}
	cout<<cnt<<endl;


	return 0;
}


Measured memory_bytes: 256
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
#include <random>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;;
#define MEM(a,b) memset((a),(b),sizeof(a))
const LL INF = 1e9 + 7;
const int N = 2e5 + 10;
vector<pair<int, int>> vp[N];
LL dis[N];
void dfs(int x, int fa = 0)
{
	for (auto &p : vp[x])
	{
		int y, w;
		tie(y, w) = p;
		if (y == fa) continue;
		dis[y] = dis[x] + w;
		dfs(y, x);
	}
}
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n;
	cin >> n;
	for (int i = 1; i < n; i++)
	{
		int x, y, w;
		cin >> x >> y >> w;
		vp[x].emplace_back(y, w);
		vp[y].emplace_back(x, w);
	}
	int q, k;
	cin >> q >> k;
	dfs(k);
	while (q--)
	{
		int x, y;
		cin >> x >> y;
		printf("%lld\n", dis[x] + dis[y]);
	}
	return 0;
}

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

int main(){
  int n;
  cin >> n;
  vector<int> data(n);
  for(int i=0; i<n; i++){
    cin >> data.at(i);
  }
  
  int min,sum1=0,sum2=0;
  
  sum1 = data.at(0);
  for(int i=1; i<n; i++){
    sum2 += data.at(i);
  }
  
  min = abs(sum1 - sum2);
  
  for(int i=1; i<n; i++){
    sum1 += data.at(i);
    sum2 -= data.at(i);
    if(min > abs(sum1 - sum2)){
      min = abs(sum1 - sum2);
    }
  }
  
  cout << min << endl;
  
}

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

sumA = [0]
sumB = [0]
tmp = 0
answer = 0
st = 0

for i in range(N):
    tmp = tmp + A[i]
    sumA.append(tmp)

tmp = 0
for i in range(M):
    tmp = tmp + B[i]
    sumB.append(tmp)

for i in range(N, -1, -1):
    booktmp = 0

    for j in range(st, M + 1):

        if sumA[i] + sumB[j] <= K:
            booktmp = i + j
        else:
            st = j
            break
    answer = max(answer, booktmp)
    if j == M:
        break


print(answer)




Measured memory_bytes: 48488
n=int(input())
if n==1:
  print("Yes")
  print(2)
  print(1,1)
  print(1,1)
  exit()
i=1
k=-1
while i<=n:
  if n==i*(i-1)//2:
    k=i
    break
  i+=1
if k==-1:
  print("No")
  exit()
ans=[[]for _ in range(k)]
anss=1
for i in range(k-1):
  for j in range(k-i-1):
    ans[i].append(anss)
    ans[i+j+1].append(anss)
    anss+=1
print("Yes")
print(k)
for i in ans:
  print(k-1,end=" ")
  print(*i)

Measured memory_bytes: 9496
H,W,K=map(int,input().split())

dp=[[0 for _ in range(W)] for _ in range(H+1)] #0~H,0~W-1
dp[0][0]=1
P=10**9+7


def check(l):
    for i in range(len(l)-1):
        if l[i+1]+l[i]==2:
            return False
    return True

for i in range(1,H+1):
    for w in range(W):
        X,Y,Z=0,0,0
        for j in range(2**(W-1)):
            tmp=[]
            for k in range(W-1):
                tmp.append(j>>k&1)
            if check(tmp):
                if w-1>=0 and (j>>(w-1))&1==1:
                    X+=1
                elif w<=W-2 and j>>w&1==1:
                    Z+=1
                else:
                    Y+=1
            else:
                continue

        if w-1>=0: dp[i][w-1]=(dp[i][w-1]+dp[i-1][w]*X)%P
        dp[i][w]=(dp[i][w]+dp[i-1][w]*Y)%P
        if w+1<=W-1: dp[i][w+1]=(dp[i][w+1]+dp[i-1][w]*Z)%P

print(dp[H][K-1])

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

using namespace std;

inline int ctoi(char c) { if(c < '0' || '9' < c) throw invalid_argument("ctoi error"); return c - '0'; }
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define REP(i, k, n) for(int i = (int)(k); i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

template <typename T>
inline T gcd(T x, T y){
	if (x <= 0 || y <= 0) throw invalid_argument("gcd error: x <= 0 or y <= 0");
	
	if(x < y) swap(x, y);
	T r = x % y;

	while(r != 0){
		x = y;
		y = r;
		r = x % y;
	}

	return y;
}
template <typename T>
inline T lcm(T x, T y){
	if (x <= 0 || y <= 0) throw invalid_argument("lcm error: x <= 0 or y <= 0");

	return x * y / gcd(x, y);
}

int main(){
	long long int n, m, k;
	cin >> n >> m >> k;

	vector<long long int> asum;
	vector<long long int> bsum;

	asum.push_back(0);
	bsum.push_back(0);

	rep(i, n){
		int a;
		cin >> a;

		asum.push_back(asum[i] + a);
	}

	rep(i, m){
		int a;
		cin >> a;

		bsum.push_back(bsum[i] + a);
	}

	int ans = 0;

	rep(i, n + 1){
		int left = 0;
		int right = m;
		int p = (left + right) / 2;

		while(left + 1 != right){
			if(asum[i] + bsum[p] <= k){
				ans = max(ans, i + p);
				left = p;
			}
			else{
				right = p;
			}

			p = (left + right) / 2;
		}

		if(asum[i] + bsum[left] <= k) ans = max(ans, i + left);
		if(asum[i] + bsum[right] <= k) ans = max(ans, i + right);

		// cout << left << " " << p << " " << right << endl;
	}

	cout << ans;

	return 0;
}

Measured memory_bytes: 7908
n, w = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]

dp = [[0]*(w+1) for i in range(n+1)]
for i in range(n):
    vi, wi = a[i]
    for j in range(w+1):
        if j + wi <= w:
            dp[i+1][j+wi] = max(dp[i][j]+vi, dp[i+1][j+wi])
        dp[i+1][j] = max(dp[i][j], dp[i+1][j])

print(max(dp[i+1]))



Measured memory_bytes: 25536
N = int(input())

def primes(n):
    from math import sqrt
    p = [1] * (n + 1)
    for x in range(2, int(sqrt(n)) + 1):
        if p[x]:
            p[x * 2:n + 1:x] = [0] * ((n - x) // x)
    return (i for i in range(2, n + 1) if p[i])

i = 0
for n in primes(55555):
    if n % 5 == 1:
        print(n)
        i += 1
        if i == N:
            break


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define rs(v,n)     v.resize(n)
#define hell        1000000007
#define peak        9123372036854775807
#define pii         acos(-1)
#define clr(a,x)    memset(a,x,sizeof(a))
#define ios         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class x,class y> x sum(x a,y b){return a+b;}
template<class x,class y> x mul(x a,y b){return a*b;}
template<class x,class y> x sub(x a,y b){return a-b;}
template<class x,class y> x divi(x a,y b){return a/b;}

template<class x,class y>
istream &operator>>(istream &in,pair<x,y> &p) 
{ 
    in>>p.F>>p.S;
    return in;
}
template<class x>
istream &operator>>(istream &in,vector<x> &v) 
{
    for(auto& i:v)
        in>>i;
    return in;
}
template<class x,class y>
ostream &operator<<(ostream &out,pair<x,y> &p) 
{ 
    out<<"("<<p.F<<","<<p.S<<") ";
    return out; 
}
template<class x>
ostream &operator<<(ostream &out,vector<x> &v) 
{ 
    out<<v.size()<<endl;
    for(auto i:v)
        out<<i<<" ";
    out<<endl;
    return out; 
}
#define N  1000005

int main()
{
    ios;
    ll tt=1;
    // cin>>tt;
    while(tt--)
    {
    	ll i,j,k,l,m,n;
    	cin>>n>>l;
    	vi v;
    	ll ans=0,maxi=peak;
    	map<ll,ll> ma;
    	for(i=l,j=0;j<n;j++,i++)
    	{
    		v.pb(i);
    		ans+=i;
    		ll x=abs(i);
    		maxi=min(maxi,x);
    		ma[i]=1;
    	}
    	if(ma[maxi]==1)
    		cout<<ans-maxi;
    	else
    		cout<<ans+maxi;
    }
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, from, to) for (int i = from; i < (to); ++i)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
using ll = long long;
using vin=vector<int>;
using vll=vector<ll>;
using P = pair<int, int>;
const int inf=1e9+7;
const ll INF=1e18;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

int main() {
  int a,b;
  cin>>a>>b;
  cout<<(a-1)*(b-1)<<endl;
}

Measured memory_bytes: 3600
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,l,r) for(long long i=(l);i<(r);++i)
#define REP(i,n) FOR(i,0,n)
#define REPS(i,n) FOR(i,1,n+1)
#define RFOR(i,l,r) for(long long i=(l);i>=(r);--i)
#define RREP(i,n) RFOR(i,n-1,0)
#define RREPS(i,n) RFOR(i,n,1)
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
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; }
const int INF = 1e18;

const int NAX = 200000;
vector<int> G[NAX], vs[NAX];
int in[NAX], out[NAX], k;
void dfs(int v, int p = -1){
    in[v] = k++;
    for(int u: G[v]){
        if(u == p) continue;
        dfs(u,v);
    }
    out[v] = k;
	return;
}

// Binary Indexed Tree (Fenwick Tree)
template <typename T>
struct BIT {
    int n,p;
    vector<T> d;
    BIT(int n=0) :n(n), d(n+1) {
        p = 1;
        while(p < n)p *= 2;
    }
    //更新
    void add(int i,T x=1){
        for(i++;i <= n;i += i&-i) d[i] += x;
    }
    //[0,i)の和
    T sum(int i){
        T res = 0;
        for(;i;i -= i&-i) res += d[i];
        return res;
    }
    //[l,r)の和
    T sum(int l,int r){ return sum(r) - sum(l);}
    //v0+v1+…vx>=mを満たすmin(x)を返す
    int lower_bound(T w){
        if(w <= 0) return 0;
        T x = 0;
        for ( int i=p;i;i/=2){
            if(i+x <=n &&d[i+x] < w){
                w -= d[i+x];
                x += i;
            }
        }
        return x;
    }
};

int f(int x){return x * (x+1) /2;};

signed main(){
    int n; cin >> n;
    int c[n];
    REP(i,n) cin >> c[i];
    REP(i,n-1){
        int a, b; cin >> a >> b; a--; b--;
        G[a].pb(b);
        G[b].pb(a);
    }
    dfs(0);
    REP(i,n) vs[c[i]-1].pb(i);
    BIT<int> d(n);
    REP(i,n) d.add(i,1);
    REP(i,n){
        int ans = f(n);
        vector<int>& nvs = vs[i];
        sort(all(nvs),[&](int a,int b){return in[a] > in[b];});
        vector<pair<int,int>> history;
        for(int v : nvs){
            int cnt = 1;
            for(int u : G[v]){
                if(in[u] < in[v]) continue;
                int num = d.sum(in[u],out[u]);
                ans -= f(num);
                cnt += num;
            }
            d.add(in[v],-cnt);
            history.eb(in[v],cnt);
        }
        ans -= f(d.sum(0,n));
        for(auto p: history) d.add(p.first, p.second);
        cout << ans << '\n';
    }
}

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

#define rep(i,n) for(int i=0;i<(n);++i)
#define reps(i,n) for(int i=1;i<=(n);++i)
#define all(x) (x).begin(),(x).end()
#define int long long
using pii = pair<int,int>;
constexpr int INF  = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;

signed main(){
    int x,y;
    cin >> x >> y;
    if (x + y >= 10) cout << "error\n";
    else cout << x + y << '\n';
    return 0;
}


Measured memory_bytes: 256
#define _USE_MATH_DEFINES

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <list>

using namespace std;

typedef pair<long long int, long long int> P;

long long int INF = 1e18;
long long int MOD = 1e9 + 7;

long long int cnt[100000];

int main(){
	
	int N, M;
	cin >> N >> M;
	
	int num = 0;
	
	for(int i = 0; i < N; i++){
		int p;
		cin >> p;
		num *= 2;
		num += p;
	}
	queue<int> que;
	que.push(num);
	
	for(int i = 0; i < (1 << N); i++){
		cnt[i] = INF;
	}
	
	for(int loop = 0; loop < (1 << N); loop++){
		int sz = que.size();
		if(sz == 0){
			break;
		}
		for(int i = 0; i < sz; i++){
			num = que.front();
			que.pop();
			if(cnt[num] != INF){
				continue;
			}
			cnt[num] = loop;
			for(int j = 0; j < N - 1; j++){
				int d1 = (num & (1 << j));
				int d2 = (num & (1 << (j + 1)));
				que.push(num + d1 - d2 / 2);
			}
		}
	}
	
	num = 0;
	int bin = 0;
	for(int i = 0; i < M; i++){
		int p;
		cin >> p;
		for(int j = 0; j < p; j++){
			num *= 2;
			num += bin;
		}
		bin++;
		bin %= 2;
	}
	
	long long int ans = cnt[num];
	
	for(int i = 0; i < N; i++){
		if(num & (1 << i)){
			num -= (1 << i);
		}else{
			num += (1 << i);
		}
	}
	
	cout << min(ans, cnt[num]) << endl;
	
	return 0;
}


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

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	ll n,a,b,c,x,y,z;
	cin>>n;
	vector<pair<int,pair<int,int>>>v(n);
	for(int i=0;i<n;i++){
		cin>>v.at(i).first;
		cin>>v.at(i).second.first;
		cin>>v.at(i).second.second;
		if(i==0){
			a=v.at(0).first;b=v.at(0).second.first;c=v.at(0).second.second;
		}else{
			x=v.at(i).first+max(b,c);
			y=v.at(i).second.first+max(a,c);
			z=v.at(i).second.second+max(a,b);
			a=x;b=y;c=z;
		}
	}
	cout<<max({a,b,c})<<endl;
}


Measured memory_bytes: 1408
#include<bits/stdc++.h>
//#include<boost/multiprecision/cpp_int.hpp>
#define fl(i,a,n) for(int i=a;i<=n;i++)
#define pb push_back
#define pob pop_back
#define int long long int
#define ff first
#define ss second
#define pi 3.1415926535898
#define vi vector<int>
#define mi map<int,int>
#define umi unordered_map<int,int>
#define endl "\n"
#define w(x) int x; cin>>x; while(x--)
#define db(x) cout<<(#x)<<" "<<(x)<<"\n";
#define ios ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
using namespace std;
//using namespace boost::multiprecision;
int mod=1000000007;
int gcd(int a,int b){
	return (b == 0) ? a :gcd(b, a % b);
}
int lcm(int a,int b){
	return (a*b)/gcd(a,b);
}
void c_p_c(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
    #endif
}
int32_t main(){

	ios
	//c_p_c();
	//w(t)
	{
int n;cin>>n;
fl(i,0,n-1){
	int d;cin>>d;
	if(d%2==0){
		if(d%3==0 or d%5==0){
			continue;
		}
		else{
			cout<<"DENIED\n";
			return 0;
		}
	}
	
}
cout<<"APPROVED\n";
	}

	return 0;
}                


Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#define MAXN 200005
#define PLI pair<long long,int>
#define fi first
#define se second
#define mp make_pair
#define ha 99994711
#define ba 823
//#define ivorysi
using namespace std;
typedef long long int64;
int N;
char s[305][305],b[305][305];
int64 hc[305],hr[305],e;
inline int C(int x) {
	return x > N ? x - N : x;
}
bool check(int y) {
	int T = C(N + y);
	for(int i = 1 ; i <= N ; ++i) 
		hr[i] = (hr[i] * ba + (b[i][T] - 'a' + 1)) % ha;
	for(int i = 1 ; i <= N ; ++i) {
		if(hr[i] != hc[C(i + y)]) {
			T = C(N + y + 1);
			for(int j = 1 ; j <= N ; ++j) 
				hr[j] = (hr[j] - e * (b[j][T] - 'a' + 1) % ha + ha) % ha;
			return false;
		}
	}
	T = C(N + y + 1);
	for(int i = 1 ; i <= N ; ++i) 
		hr[i] = (hr[i] - e * (b[i][T] - 'a' + 1) % ha + ha) % ha;
	return true;
}
void Solve() {
	scanf("%d",&N);
	if(N == 1) {
		puts("1");return;
	}
	e = 1;
	for(int i = 1 ; i < N ; ++i) e = e * ba % ha;
	for(int i = 1 ; i <= N ; ++i) {
		scanf("%s",s[i] + 1);
	}
	int cnt = 0;
	for(int A = 0 ; A < N ; ++A) {
		memset(hc,0,sizeof(hc));
		memset(hr,0,sizeof(hr));
		for(int i = 1 ; i <= N ; ++i) {
			for(int j = 1 ; j <= N ; ++j) {
				b[i][j] = s[C(i + A)][j];
				hc[j] = (hc[j] * ba + (b[i][j] - 'a' + 1)) % ha;
				if(j != N) {
					hr[i] = (hr[i] * ba + (b[i][j] - 'a' + 1)) % ha;
				}
			}
		}
		for(int B = 0 ; B < N ; ++B) {
			if(check(B)) ++cnt;
		}
	}
	printf("%d\n",cnt);
}
int main() {
#ifdef ivorysi
	freopen("f1.in","r",stdin);
#endif
	Solve();
	return 0;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a) for(int i = 0; i < (a); i++)
#define ALL(a) (a).begin(),(a).end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;

signed main(){
    int n;
    cin >> n;
    cout << (n - 1) / 2 << endl;
    return 0;
}

Measured memory_bytes: 256
/*Function Template*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int mod = 1000000007;
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define ALL(a)  (a).begin(),(a).end()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);


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){
  ll r,tmp;
/*自然数 a > b を確認・入替*/
  if(a<b){
    tmp=a,a=b,b=tmp;
  }
/*ユークリッドの互除法*/
  r=a%b;
  while(r!=0){
    a=b,b=r,r=a%b;
  }
  return b;
}


ll LCM(ll a,ll b){
  ll c=a,d=b,r,tmp;
/*自然数 a > b を確認・入替*/
  if(a<b){
    tmp=a,a=b,b=tmp;
  }
/*ユークリッドの互除法*/
  r=a%b;
  while(r!=0){
    a=b,b=r,r=a%b;
  }
  return c/b*d;
}


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

vector<pair<char,ll>> runlength(string s,vector<pair<char,ll>> p){
  ll x=1;
  if(s.size()==1){
    p.push_back(pair<char,ll>(s[0],1));
    return p;
  }
  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));
      }
    }
  }
  return p;
}



/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
int main() {
  IOS;
  string s;
  cin>>s;
  ll ans=0,k=1<<(s.size()-1),l=k;
  for(ll i=s.size()-1;i>=0;i--){
    ans+=(s[i]-'0')*k;
    k=k*5+l/2;
  }
  cout<<ans<<endl;
}

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

int main() {
  double A, B, X;
  cin >> A >> B >> X;

  double a, x = X / A, ans;

  if (x > A * B / 2) {
    double top = x * 2 / A - B;
    x -= A * top;
    B -= top;
    a = (B == 0) ? A : x * 2 / B;
  }
  else a = x * 2 / B;

  ans = atan(B / a) * (180.0 / M_PI); 

  cout << fixed << setprecision(10) << ans << endl;
}

Measured memory_bytes: 384
import java.io.BufferedReader;
import java.io.*;
import java.util.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Collections;
import java.io.Serializable;

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

        String x = sc.next();
        int num = 0;
        int min =1000;

        char[] work = new char[x.length()];

        for(int i = 0; i < x.length(); i++){
            work[i] = x.charAt(i);
            //System.out.println((int)work[i]);
            }
        
        for(int i = 0; i < x.length()-2 ; i++){
            num = 100*((int)work[i]-48) + 10*((int)work[i+1]-48) + ((int)work[i+2]-48);
            num = num - 753;
            if(Math.abs(num) < min){
                min = Math.abs(num);
            }
            num = 0;
        }

        System.out.println(min);


        


    }
}

Measured memory_bytes: 25300
#include<iostream>
#include<queue>

using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n,M[N][N];
int d[N];

void bfs(int s){
  queue<int> q;
  q.push(s);
  for(int i = 0; i < n; i++) d[i] = INFTY;
  d[s] = 0;
  int u;
  while(!q.empty()){
    u = q.front();
    q.pop();
    for(int v=0;v<n;v++){
      if(M[u][v] == 0) continue;
      if(d[v]!=INFTY) continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for(int i = 0; i<n; i++){
    cout << i+1 << " " << ( (d[i] == INFTY) ? (-1) : d[i] ) << endl;
  }
}

int main(){
  int u,k,v;

  cin >> n;
  for(int i=0;i<n;i++){
    for(int j = 0; j < n; j++) M[i][j] = 0;
  }

  for(int i = 0; i < n; i++){
    cin >> u >> k;
    u--;
    for(int j = 0; j<k; j++){
      cin>>v;
      v--;
      M[u][v] = 1;
    }
  }
  bfs(0);

  return 0;
}


Measured memory_bytes: 3128
// Copyright 2020 yaito3014
#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int A, B, M;
  cin >> A >> B >> M;
  vector<int> a(A), b(B);
  for (auto& e : a)cin >> e;
  for (auto& e : b)cin >> e;
  vector<int> x(M), y(M), c(M);
  for (int i = 0; i < M; ++i)
    cin >> x[i] >> y[i] >> c[i], --x[i], --y[i];
  int ma = INT_MAX, mb = INT_MAX;
  for (int i = 0; i < A; ++i)ma = min(ma, a[i]);
  for (int i = 0; i < B; ++i)mb = min(mb, b[i]);
  int ans = ma + mb;
  for (int i = 0; i < M; ++i) {
    ans = min(ans, a[x[i]] + b[y[i]] - c[i]);
  }
  cout << ans << endl;
}


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

int main(void) {
    long long N;
    cin >> N;
    long long n = N;
    vector< int > p;
    int count = 0;
    while (n % 2L == 0) {
        ++count;
        n /= 2L;
    }
    if (count > 0) {
        p.push_back(count);
    }
    for (long long i = 3; n > 1 && i * i <= N; i += 2) {
        count = 0;
        while (n % i == 0) {
            ++count;
            n /= i;
        }
        if (count > 0) {
            p.push_back(count);
        }
    }
    if (n > 1) {
        p.push_back(1);
    }

    long long ans = 0;
    for (auto &i : p) {
        for (int j = 1; j < 65; ++j) {
            if (i < j * (j + 1) / 2) {
                ans += j - 1;
                break;
            }
        }
    }
    printf("%lld\n", ans);
    return 0;
}


Measured memory_bytes: 3724
import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		while(true){
			int n = sc.nextInt();
			int r = sc.nextInt();
			if(n == 0 && r == 0){
				break;
			}
			int[] array = new int[n];
			for(int i=0;i<n;i++){
				array[i] = n - i;
			}
			for(int i=0;i<r;i++){
				int p = sc.nextInt();
				int c = sc.nextInt();
				arrayswap(array,p,c);
			}
			System.out.println(array[0]);
		}
	}
	
	public static void arrayswap(int[] array,int p,int c){
		int[] tmp1 = new int[p-1];
		int[] tmp2 = new int[c];
		for(int i=0;i<p-1;i++){
			tmp1[i] = array[i];
		}
		for(int i=0;i<c;i++){
			tmp2[i] = array[p-1+i];
		}
		for(int i=0;i<c;i++){
			array[i] = tmp2[i];
		}
		for(int i=0;i<p-1;i++){
			array[c+i] = tmp1[i];
		}		
	}

}

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

#define int long long
#define rep(i,l,r) for(int i=(int)(l);i<(int)(r);i++)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)x.size())
template<class T>bool chmax(T &a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,T b){if(a>b){a=b;return 1;}return 0;}

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int inf = 1LL<<60;
const int mod = 1e9 + 7;
const double eps = 1e-9;

/*{
  }*/

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

  vi h(n);
  rep(i, 0, n) cin >> h[i];

  reverse(all(h));
  int ans = 0, cnt = 0;
  rep(i, 1, n){
    if(h[i-1] <= h[i]) cnt++;
    else cnt = 0;
    chmax(ans, cnt);
  }

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


Measured memory_bytes: 1024
n = int(input())
a = input()
b = input()
c = input()
cnt = 0
for i in range(n):
    d, e, f = a[i], b[i], c[i]
    if d == e == f:
        pass
    elif d == e or d == f or e == f:
        cnt += 1
    else:
        cnt += 2
print(cnt)

Measured memory_bytes: 3060
#include<iostream>
#include<cstdio>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<list>
#include<vector>
#include<functional>
#include<string.h>
#include<limits.h>
#include<queue>
#include<deque>
#include<stack>
#include<stdlib.h>
#include<algorithm>
#include<cmath>
#define P pair<int,int>
using namespace std;

int main()
{
	int n;
	int a[100000 + 100];
	int b[100000 + 100];
	while (cin >> n) {
		memset(b, -1, sizeof(b));
		for (int i = 1; i <= n; i++) {
			cin >> a[i]; b[i] = 0;
		}
		int flag = 0; int k = 0;
		int i = 1;
		while (1) {
			if (b[i] == 0) {
				b[i] = 1; k++;
				i = a[i];
				if (i == 2) {
					flag = 1; break;
				}
			}
			else if (b[i] == 1) {
				flag = 0; break;
			}
		}
		if (flag == 0)
			cout << -1 << endl;
		else
			cout << k << endl;
	}
	return 0;
}

Measured memory_bytes: 1024
<?php
list($n, $k) = ints();
$ans = 0;
for ($b = $k + 1; $b <= $n; $b++) {
    $ans += intdiv($n, $b) * ($b - $k);
    $ans += max(0, $n % $b - max(0, $k - 1));
}
echo $ans;

function ints()
{
    return array_map('intval', explode(' ', trim(fgets(STDIN))));
}

Measured memory_bytes: 4728
import java.util.Scanner;

//AtCoder Grand Contest 034
//B	ABC
public class Main {

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

		sc.close();

		S = S.replaceAll("BC", "D");

		long ans = 0;
		long countA = 0;

		for (int i = 0; i < S.length(); i++) {
			switch (S.charAt(i)) {
			case 'A':
				countA++;
				break;

			case 'D':
				ans += countA;
				break;

			default:
				countA = 0;
				break;
			}
		}

		System.out.println(ans);
	}
}


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

int main() {
  int H, W;
  cin >> H >> W;

  int cnt = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char a;
      cin >> a;
      if (a == '#') cnt++;
    }
  }

  if (H + W - 1 == cnt)
    cout << "Possible" << endl;
  else
    cout << "Impossible" << endl;
}

Measured memory_bytes: 256
package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	ar := make([]int, n)
	for i := range ar {
		fmt.Scan(&ar[i])
	}
	count := 0
	for i := 0; i < n-1; i++ {
		minv := ar[i]
		mini := i
		for j := i + 1; j < n; j++ {
			if minv > ar[j] {
				minv = ar[j]
				mini = j
			}
		}
		if i != mini {
			count++
			if count >= 2 {
				fmt.Println("NO")
				return
			}
			ar[i], ar[mini] = ar[mini], ar[i]
		}
	}
	fmt.Println("YES")
}


Measured memory_bytes: 512
A,B = map(int,input().split())
S = input()
flag = 0
for i in range(A):
    if S[i] == "-":
        flag = 1
i += 1
if S[i] != "-":
    flag = 1
for i in range(A+1,A+B+1):
    if S[i] == "-":
        flag = 1
if flag == 1:
    print("No")
else:
    print("Yes")

Measured memory_bytes: 38384