akhauriyash/Code-Regression huggingface dataset:
def main():
    N, K = map(int,input().split())
    S = input()

    #幸せな人の数
    h = 0 
    s1 = 'S'
    for s in S:
        if s == s1:
            h += 1
        s1 = s

    print(min(h+(K*2) ,N-1))

if __name__ == '__main__':
    main()

Measured memory_bytes: 3316
import bisect
n=int(input())
a=sorted(list(map(int,input().split())))
b=sorted(list(map(int,input().split())))
c=sorted(list(map(int,input().split())))
ans=0
for i in b:
    x=bisect.bisect_left(a,i)
    y=n-bisect.bisect_right(c,i)
    ans+=x*y
print(ans)

Measured memory_bytes: 23092
N,A,B,C,D=map(int, input().split())
A-=1
B-=1
C-=1
D-=1
S=input()
s=[0]*N
stop=[0]*N
space=[0]*N
for i in range(N):
    if S[i]=='#':
        s[i]=1
        if i>0 and s[i-1]==1:
            stop[i]=1
    elif i>1 and s[i-1]==0 and s[i-2]==0:
        space[i]=1
    if i>0:
        stop[i]+=stop[i-1]
        space[i]+=space[i-1]
if stop[C]-stop[A]>0 or stop[D]-stop[B]>0:
    ans=0
elif C<D:
    ans=1
elif space[D+1]-space[B]>0:
    ans=1
else:
    ans=0
print(['No','Yes'][ans])

Measured memory_bytes: 7928
#include <stdio.h>
#include <bits/stdc++.h>
#define mod 1000000007
typedef long long ll;
using namespace std;
ll n,a,b,c,d;
int pd(int x){
	ll t1=x*d;
	ll t2=(n-x-1)*c;
	ll t3=b-a;
	ll t4=x*c;
	ll t5=(n-x-1)*d;
	ll minn=t4-t5;
	ll maxn=t1-t2;
	if(t3<=maxn && t3>=minn) return 0;
	if(t3>maxn) return 1;
	return -1;
}
int main()
{
    cin>>n>>a>>b>>c>>d;
    if(a>b) swap(a,b);
    ll l=0,r=n-1;
    while(l<=r){
    	ll mid=(l+r+1)/2;
    	ll k=pd(mid);
		if(k==0){
    		cout<<"YES";return 0;
    	}
    	if(k==1) l=mid+1;
    	else r=mid-1;
	}
	cout<<"NO";
    return 0;
}


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

using ll = long long;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define ssrep(i, s, t) for (int i = s; i <= t; ++i)

#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))

using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using ld = long double;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "(" << p.first << "," << p.second << ")";
    return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << "{";
    rep(i, (int)v.size()) {
        if (i)
            os << ",";
        os << v[i];
    }
    os << "}";
    return os;
}

template <typename T, size_t S> void printArray(const T (&array)[S]) {
    for (auto val : array)
        std::cout << val << ", ";
    std::cout << "\n";
}

const int mod = 1e9 + 7;
const int inf = 1e9 + 5;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << std::setprecision(10);

    string s, t; std::cin >> s >> t;
    int idx = -1, slen = s.size(), tlen = t.size();
    rep(i, slen) {
        if (slen-i < tlen) continue;
        bool ok = true;
        rep(j, tlen) {
            if (s[i+j] != '?' && s[i+j] != t[j]) ok = false;
        }
        if (ok) idx = i;
    }
    rep(i, tlen) {
        s[idx+i] = t[i];
    }
    rep(i, slen) if (s[i] == '?') s[i] = 'a';
    if (idx == -1)
        std::cout << "UNRESTORABLE" << "\n";
    else
        std::cout << s << "\n";
}


Measured memory_bytes: 256
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <assert.h>
using namespace std;
 
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef vector<VI> VVI;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;
typedef vector<double> VD;

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }

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

    ll ans = 1e18;
    for (ll z = 0; z / 2 <= max(x, y); z += 2){
        ll tmp = c * z;
        if (z / 2 < x) tmp += a * (x - z / 2);
        if (z / 2 < y) tmp += b * (y - z / 2);
        ans = min(ans, tmp);
    }
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using str = string;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
const double PI = 3.141592653589793238;
const ll mod = 1000000007;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define REP(i, m, n) for (int i = m; i < (int)n; i++)
#define all(a) (a).begin(), (a).end()
#define allg(a) (a).begin(), (a).end(), greator<>()
#define d20 std::setprecision(20)
#define veci vector<int>
#define vecll vector<long long int>
#define vecb vector<bool>
#define vecd vector<double>
template <typename T>
T lcm(T a, T b)
{
	return a / __gcd(a, b) * b;
}

int main()
{
	ll L, R;
	cin >> L >> R;
	if (R - L + 1 >= 2070)
		cout << 0 << endl;
	else
	{
		int ans = INT_MAX;
		for (ll i = L; i <= R - 1; i++)
			for (ll j = i + 1; j <= R; j++)
				ans = min<ll>(ans, (i * j) % 2019);
		cout << ans << endl;
	}

	return 0;
}

Measured memory_bytes: 3636
n, m = map(int, input().split())
city = [list(map(int, input().split())) for _ in range(m)]
pref = [[] for _ in range(n+1)]

ans = []

for i, c in enumerate(city):
    pref[c[0]].append([i, c[1]])

for i in range(n):
    pref[i + 1].sort(key=lambda x: x[1])
    for j in range(len(pref[i + 1])):
        ans.append([pref[i+1][j][0], str(i+1).zfill(6) + str(j+1).zfill(6)])

ans.sort(key=lambda x: x[0])

for c in ans:
    print(c[1])


Measured memory_bytes: 64964
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
 
class Program{
    static void Main(){
        char[] s = Console.ReadLine().ToCharArray();
        char[] a = "CODEFESTIVAL2016".ToCharArray();
        int cnt = 0;
        for(int i = 0; i < 16; i++){
            if(s[i] != a[i]) cnt++;
        }
        Console.WriteLine(cnt);
    }
}

Measured memory_bytes: 2900
#include<iostream>
#include<cstdio>
using namespace std;
const int N=2020,mod=1e9+7;
int f[N][N],g[N][N];
int main()
{
	int n; cin>>n;
	f[0][0]=1;
	for(int i=0;i<=n;i++)
		g[0][i]=g[0][i-1]+f[0][i];
	int ans=0;
	for(int i=1;i<=n;i++){
		for(int j=3;j<=n;j++)
			f[i][j]=g[i-1][j-3];
		for(int j=0;j<=n;j++)
			g[i][j]=(g[i][j-1]+f[i][j])%mod;
		(ans+=f[i][n])%=mod;
	} cout<<ans<<endl;
	return 0;
}

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

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define show(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define show(x)
#endif

typedef long long int ll;
typedef pair<int,int> pii;
template<typename T> using vec=std::vector<T>;

const int inf=1<<30;
const long long int infll=1LL<<62;
const double eps=1e-9;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
    os << "[";
    for (const auto &v : vec) {
    	os << v << ",";
    }
    os << "]";
    return os;
}

struct topological_sort{
    vector<vector<int>> graph;
    vector<bool> done,open;
    vector<int> res;
    bool success;
    topological_sort(vector<vector<int>> &graph):graph(graph),done(graph.size()),open(graph.size()),success(true){
        int size=graph.size();
        vector<int> in(size);
        for(int i=0; i<size; ++i) for(int j:graph[i]) ++in[j];
        priority_queue<int,vector<int>,greater<int>> que;
        for(int i=0; i<size; ++i) if(!in[i]) que.push(i);
        while(!que.empty()){
            int x=que.top();
            que.pop();
            res.push_back(x);
            for(int y:graph[x]){
                --in[y];
                if(!in[y]) que.push(y);
            }
        }
        if(res.size()!=size) success=false;
    }
};

void solve(){
    int v,e;
    cin >> v >> e;
    vector<vector<int>> graph(v);
    rep(i,0,e){
        int s,t;
        cin >> s >> t;
        graph[s].push_back(t);
    }
    topological_sort ts(graph);
    rep(i,0,v) cout << ts.res[i] << endl;
}

int main(){
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(10);
    solve();
    return 0;
}

Measured memory_bytes: 4308
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define REP(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) std::cout << x << '\n'
#define all(a) a.begin(), a.end()
#define pb push_back
#define sz(x) ((int)(x).size())
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; }
const int dx[4] = { 1, 0, -1,  0 };
const int dy[4] = { 0, 1,  0, -1 };
const int INF = (int)1e9;
const ll  inf = (ll)1e18;
const int mod = 1000000007;
const int MAX_N = 100010;
const double PI = acos(-1);

int n, m, q;
int a[50], b[50], c[50], d[50];

int ans = 0;
int A[10];

void dfs(int cu, int lst) {
	if (cu == n) {
		int tot = 0;
		rep(i, q) {
			if (A[b[i]] - A[a[i]] == c[i]) {
				tot += d[i];
			}
		}
		ans= max(ans, tot);
		return;
	}

	for (int nxt = lst; nxt <= m; nxt++) {
		A[cu] = nxt;
		dfs(cu + 1, nxt);
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(10);
	/*------------------------------*/

	cin >> n >> m >> q;
	rep(i, q) {
		cin >> a[i] >> b[i] >> c[i] >> d[i];
		a[i]--; b[i]--;
	}

	dfs(0, 1);

	out(ans);

	return 0;
}

Measured memory_bytes: 3604
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc =new Scanner(System.in);
		int N=sc.nextInt();
		long L0=2,L1=1;
		Long ans=0L;
		if(N==1) {
			System.out.println(L1);
		}else {
			for(int i=2;i<=N;i++) {
				ans=L0+L1;
				L0=L1;
				L1=ans;
			}
			System.out.println(ans);
		}
	}
}

Measured memory_bytes: 21844
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main(final String[] args) {
        final FastScanner scanner = new FastScanner(System.in);
        final int k = scanner.nextInt();
        final int a = scanner.nextInt();
        final int b = scanner.nextInt();

        for(int i = a;i<=b;i++){
            if(i%k ==0){
                System.out.println("OK");
                return;
            }
        }
        System.out.println("NG");
    }

    private static class FastScanner {
        private final BufferedReader reader;
        private StringTokenizer tokenizer;

        FastScanner(final InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
        }

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

        String nextLine() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    return reader.readLine();
                } catch (final IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken("\n");
        }

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

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

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


Measured memory_bytes: 32740
#include<iostream>
#include<algorithm>
#include<fstream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
long long a,b,c,x,h;
int main()
{
	cin>>a>>b>>c>>x;
	for(int i=0;i<=a;i++)
	{
	  for(int j=0;j<=b;j++)
	  {
	    for(int u=0;u<=c;u++)
		{
		  if((i*500+j*100+u*50)==x)
		  {
		    h++;	
		  }	
		}	
	  }	
	}
	cout<<h;
	return 0;
}

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

int main(){
    int n,cnt1=0, cnt2=0;
    cin>>n;
    rep(i,n){
        string st1, st2;
        cin>>st1>>st2;
        if(st1>st2){
            cnt1+=3; 
        }else if(st2>st1)  cnt2+=3;
        else{
            cnt1++; cnt2++;
        }
    }
    
        
    
    cout<<cnt1<<" "<<cnt2<<endl;
    
      
}


Measured memory_bytes: 3128
import java.util.*;
import java.io.*;
class Main{
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String in[]=br.readLine().split(" ");
		int arr[]=new int[n];
		for(int i=0;i<n;i++)
			arr[i]=Integer.parseInt(in[i]);
		int dp[]=new int[n];
		Arrays.fill(dp,1000000005);	
		dp[0]=0;	
		for(int i=0;i<n;i++){
			if(i+1 < n)
				dp[i+1] = Math.min(dp[i+1],dp[i]+Math.abs(arr[i]-arr[i+1]));
			if(i+2 < n)
				dp[i+2] = Math.min(dp[i+2],dp[i]+Math.abs(arr[i]-arr[i+2]));
		}
		System.out.println(dp[n-1]);
	}
}

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

int main() {
 int A , N , Y;
 string S;
 cin >> N;
 A = 0;

 for ( int i = 0; i < N; i++){
   cin >> S;
   if ( S == "Y" ){
     A = A + 1;
   }
 }

 if ( A == 0 ){
   cout << "Three" << endl;
 }

 else{
   cout << "Four" << endl;
 }

}

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

signed main() 
{
	int t;
	cin >> t;
	for(int i = 1; i <= t; i++){
		int cnt = 0;
		for(int x = 1; x * x <= i; x++){
			for(int y = 1; y <= x; y++){
				for(int z = 1; z <= y; z++){
					if(x*x+y*y+z*z+x*z+x*y+y*z == i){
						if(x!=y && y!=z && x!=z){
							cnt += 6;
						}
						if(x!=y && y!=z && x==z){
							cnt += 3;
						}
						if(x==y && y!=z && x!=z){
							cnt += 3;
						}
						if(x!=y && y==z && x!=z){
							cnt += 3;
						}
						if(x==y && y==z && x==z){
							cnt += 1;
						}
					}
				}
			}	
		}
		cout << cnt << '\n';
	}
}

Measured memory_bytes: 3604
#include <bits/stdc++.h>
#define FOR(i, a, n) for(int (i) = (int)(a); (i) < (int)(n); ++(i))
#define FORe(i, a, n) for(int (i) = (int)(a); (i) <= (int)(n); ++(i))
#define rFOR(i, a, n) for(int (i) = (int)(n)-1; (i) >= (int)(a); --(i))
#define rFORe(i, a, n) for(int (i) = (int)(n); (i) >= (int)(a); --(i))
#define REP(i, n) FOR((i), 0, (n))
#define REPe(i, n) FORe((i), 1, (n))
#define REPo(i, n) FORe((i), 0, (n))
#define rREP(i, n) rFOR((i), 0, (n))
#define rREPe(i, n) rFORe((i), 1, (n))
#define rREPo(i, n) rFORe((i), 0, (n))
#define rep(i, n) FOR((i), 0, (n))
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define fi first
#define se second
#define dup(x, y) (((x)+(y)-1)/(y))
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
using ll = long long;
using P = pair<int,int>;
using T = tuple<int,int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vs = vector<string>;
using vp = vector<P>;
using vt = vector<T>;

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

// auto mod int
const int mod = 2019;
// const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  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 { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= 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 { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

ll comb(ll a) {
  return a*(a-1)/2;
}

int main() {
  string s;
  cin >> s;
  int n = s.size();
  vl cnt(2019);
  cnt[0]++;
  mint tmp = 0;
  mint digit = 1;
  reverse(all(s));
  REP(i, n) {
    tmp += mint(s[i]-'0')*digit;
    cnt[tmp.x]++;
    digit *= 10;
  }
  ll ans = 0;
  REP(i, 2019) {
    ans += comb(cnt[i]);
  }
  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 3752
#include<bits/stdc++.h>
#define endl "\n"
#define FASTIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fi first
#define se second
typedef long long int ll;
const int INF = 1e9;

using namespace std;

void solve()
{
  string a;
  cin>>a;
  if(a=="ABC") cout<<"ARC\n";
  else cout<<"ABC\n";
}
 
int main()
{
    FASTIO
    solve();
    return 0;
}

Measured memory_bytes: 3584
require 'enumerator'
N = gets.to_i
AS=gets.split.map &:to_i
ans=[0]*(N+1)
AS.unshift(0)

for i in 1+N/2..N
  ans[i]=AS[i]
end

for ic in 1...1+N/2
  i=1+N/2-ic
  a_i=[0]
  j=2
  until j*i>N
    a_i << ans[j*i]
    j+=1
  end
  ans[i]=a_i.inject(:+)%2 == AS[i] ? 0 : 1
end

out=[]
for i in 1..N
  out << i if ans[i]==1
end
puts ans.inject(:+)
puts out.join(" ")


Measured memory_bytes: 41480
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
s = sum(a)
m = inf
for i in range(n):
    if m > abs(s / n - a[i]):
        m = abs(s / n - a[i])
        idx = i
print(idx)

Measured memory_bytes: 65636
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>

using namespace std;

int main() {

	int64_t A, B, C, K;

	cin >> A >> B >> C >> K;

	

	if (A >= K) {
		cout << K;
		return 0;
	}
	else if (K <= A + B) {
		cout << A;
		return 0;
	}
	else if (K <= A + B + C) {
		cout << A - (K - (A + B));
		return 0;
	}


}

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

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

  vector<double> v(n);
  for (int i = 0; i < n; i++) cin >> v[i];

  sort(v.begin(), v.end());
  double ans = v[0];

  for (int i = 1; i < n; i++) ans = (ans + v[i]) / 2.0;

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

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
using namespace std;
int main()

{
    int a, b, k;
    cin >> k >> a >> b;
    bool flag = false;
    for(int i = a; i <= b; i++)
    {
        if(i % k == 0)

        {
            flag = 1;
            break;
        }
    }
    if(flag)
        printf("OK\n");
    else
        printf("NG\n");
}

Measured memory_bytes: 3128
#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>;

int main() {
	int a, b, c, d;
	cin >> a >> b >> c >> d;
	string ans = "No";
	if (abs(a-c) <= d || (abs(a-b) <= d && abs(b-c) <= d)) ans = "Yes";
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 3636
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
using int64=long long;
const int INF=1145141919,MOD=1e9+7;
const int64 LINF=8931145141919364364,LMOD=998244353;
const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

int main(){
    int h,w; cin>>h>>w;
    char a[h][w];
    queue<pair<pair<int,int>,int>> q;
    rep(i,h)rep(j,w){
        cin>>a[i][j];
        if(a[i][j]=='#') q.push({{i,j},0});
    }
    int ans=0;
    while(q.size()){
        auto z=q.front(); q.pop();
        int y=z.first.first,x=z.first.second;
        // cout<<y<<" "<<x<<endl;
        a[y][x]='#';
        ans=max(ans,z.second);
        rep(i,4){
            int ny=y+dy[i],nx=x+dx[i];
            if(0<=ny&&ny<h&&0<=nx&&nx<w){
                if(a[ny][nx]=='.'){
                    a[ny][nx]='#';
                    q.push({{ny,nx},z.second+1});
                }
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 13328
#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;

typedef long long LL;

const int maxn=1e5+5;

int n;

int tot,go[2*maxn],nxt[2*maxn],f1[maxn];
void ins(int x,int y)
{
	go[++tot]=y;
	nxt[tot]=f1[x];
	f1[x]=tot;
}

int sg[maxn];
void dfs(int k,int last)
{
	sg[k]=0;
	for(int p=f1[k]; p; p=nxt[p]) if (go[p]!=last)
	{
		dfs(go[p],k);
		sg[k]^=(sg[go[p]]+1);
	}
}

int main()
{
	scanf("%d",&n);
	fo(i,1,n-1)
	{
		int x,y;
		scanf("%d %d",&x,&y);
		ins(x,y), ins(y,x);
	}
	
	dfs(1,0);
	
	printf((sg[1]) ?"Alice\n" :"Bob\n");
}

Measured memory_bytes: 7168
from bisect import bisect_left, bisect_right; from collections import Counter, defaultdict, deque, OrderedDict; from copy import deepcopy; from fractions import gcd; from functools import lru_cache, reduce; from math import ceil, floor; from sys import stdin, setrecursionlimit
import heapq as hq; import itertools as its; import operator as op


# globals
inf = float('inf')
MOD = 1000000007
input = stdin.readline


def main():
    s = get_str()
    dp = [[0] * 13 for _ in range(len(s) + 1)]
    dp[0][0] = 1
    for i, c in enumerate(s):
        ds = [int(c)] if c != '?' else range(10)
        for d in ds:
            for j in range(13):
                dp[i+1][(j * 10 + d) % 13] += dp[i][j]
                dp[i+1][(j * 10 + d) % 13] %= MOD
    print(dp[len(s)][5])
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def gets(n, types, sep=None):
    if len(types) == 1:
        return [types[0](input().strip()) for _ in range(n)]
    return list(zip(*(
        [t(x) for t, x in zip(types, input().strip().split(sep=sep))]
        for _ in range(n)
    )))


if __name__ == '__main__':
    setrecursionlimit(4100000)
    main()


Measured memory_bytes: 88664
n = int(input())
li = []
for _ in range(n):
    a,b = map(int,input().split())
    li.append([a+b,a-b])
li.sort()
#print(li)
last = 0
count = 0
for i,l in enumerate(li):
    if i == 0:
        last = l[0]
        count += 1
        continue
    #print(l,l[1],last,'last')
    if l[1]>=last:
        count += 1
        last = l[0]
print(count)

Measured memory_bytes: 60544
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int cmp_str(char *x , char *y){
    return strcmp(x,y);
}

int main(void){
    char array[100][101];
    int n,l;    /* l：文字列の長さ、n：文字列の数 */
    int i;
    
    scanf("%d%d", &n,&l);  /* 文字列の数と */
    
    for(i = 0; i<n; ++i){
        scanf("%s",array[i]);    
    }
    
    qsort(array,n,sizeof(array[0]),cmp_str); /* クイックソート昇順 */
    
    for(i = 0; i<n; ++i){   /* 文字列の数まで出力 */
        printf("%s", array[i]);
    }
    printf("\n");
}

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

S =input()

total = 0
kaigai = 0
for i,s in enumerate(S):
    if s=='a':
        if total<A+B:
            print("Yes")
            total +=1
        else:
            print("No")
    elif s=="b":
        if total <A+B and kaigai <B:
            print("Yes")
            total+=1
            kaigai+=1
        else:
            print("No")
    else:
        print("No")

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

typedef long long ll;

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

const ll MAX = 110;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;

int N, F[MAX][10], P[MAX][11];

int main(){
    cin >> N;
    rep(i, N)rep(j, 10) cin >> F[i][j];
    rep(i, N)rep(j, 11) cin >> P[i][j];

    int ans = -INF;
    rep(i, 1<<10){
        if(i == 0) continue;
        int res = 0;
        rep(j, N){
            int cnt = 0;
            rep(k, 10){
                if( F[j][k] && i >> k & 1 ) cnt++;
            }
            res += P[j][cnt];
        }
        ans = max(ans, res);
    }

    cout << ans << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
#define forn(a,b,c) for(int a=b;a<=c;a++)
#define ford(a,b,c) for(int a=b;a>=c;a--)
#define f first
#define s second
typedef pair<int,int> pii;
const int N=21;
const int mod=1e9+7;
vector<int>g[N];
int dp[(1<<N)];
int add(int a,int b){
    a+=b;
    if(a>=mod){
        a-=mod;
    }
    return a;
}
int calc(int curr){
    if(dp[curr]!=-1){
        return dp[curr];
    }
    int n=__builtin_popcount(curr);
    if(n==0)
        return dp[curr]=1;
    n--;
    dp[curr]=0;
    for(auto p:g[n]){
        if(curr&(1<<p)){
            int next=curr^(1<<p);
            dp[curr]=add(dp[curr],calc(next));
        }
    }
    return dp[curr];
}
int main()
{
    int n;
    scanf("%d",&n);
    fill(&dp[0],&dp[0]+(1<<N),-1);
    forn(i,0,n-1){
        forn(j,0,n-1){
            int a;
            scanf("%d",&a);
            if(a){
                g[i].pb(j);
            }
        }
    }
    printf("%d\n",calc((1<<n)-1));
    return 0;
}

Measured memory_bytes: 8448
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a,b,x;
signed main()//singed=int
{
    scanf("%lld%lld%lld",&a,&b,&x);
    printf("%lld",!(a%x)?b/x-a/x+1:b/x-a/x);
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
char s[15],t[15];
int main(){
	int head=0,l;
	cin>>s;
	l=strlen(s);
	for(int i=0;i<l;i++)
		if(s[i]=='B'&&head!=0)
			head--;
		else if(s[i]!='B')
			t[head++]=s[i];
	for(int i=0;i<head;i++)
		cout<<t[i];
	cout<<endl;
	return 0;}

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

int main()
{
	int N, M;
	cout << fixed << setprecision(7);
	cin >> N >> M;
	vector<ld> L(N), A(N), B(N);
	for (int i = 0; i < N; i++) {
		cin >> L[i] >> A[i] >> B[i];
	}
	ld res = 0.0;
	for (int i = 0; i < (1 << N);i++) {
		int cnt = 0;
		for (int j = 0; j < N;j++) {
			if (i & (1 << j)) cnt++;
		}
		if (cnt == M) {
			ld val = 0.0;
			for (int j = 0; j < N;j++) {
				if (i & (1 << j)) {
					for (int k = 0; k < N; k++) {
						if (i & (1 << k)) {
							val += (L[j] - L[k]) * (L[j] - L[k]) + (A[j] - A[k]) * (A[j] - A[k]) + (B[j] - B[k]) * (B[j] - B[k]);
						}
					}
				}
			}
			res = max(res, val / 2.0);
		}
	}
	cout << res << endl;
	return 0;
}

Measured memory_bytes: 3272
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x) \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;

ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};

const ll INF = 1LL << 50;

static const long long mod = 1000000007;

int main() {
  ll n, k, l, r;
  cin >> n >> k >> l >> r;

  ll na = n / 2;
  ll nb = n - na;
  vll sa(na), sb(nb);

  rep(i, na) cin >> sa[i];
  rep(i, nb) cin >> sb[i];

  ll ans = 0;
  vvll lsta(na+1, vll()), lstb(nb+1, vll());
  rep(s, 1LL << na) {
    ll cur = 0;
    rep(ii, na) if ((s >> ii & 1) == 1) cur += sa[ii];
    lsta[__builtin_popcount(s)].push_back(cur);
  }
  rep(s, 1LL << nb) {
    ll cur = 0;
    rep(ii, nb) if ((s >> ii & 1) == 1) cur += sb[ii];
    lstb[__builtin_popcount(s)].push_back(cur);
  }
  rep(i, nb) Sort(lstb[i]);
  for (ll i = 0; i <= k; i++) {
    ll j = k - i;
    if (i > na || j > nb) continue;
    for (ll x : lsta[i]) {
      ll ir = Upper_bound(lstb[j], r - x);
      ll il = Lower_bound(lstb[j], l - x);
      ans += ir - il;
    } // i
  } // i

  cout << ans << endl;

  return 0;
}



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

typedef long long lli;
typedef vector<int> Vi;
typedef vector<lli> Vl;
typedef pair<int, int> Pii;
typedef pair<lli, lli> Pll;


//===

// コンテスト中に提出したのは嘘解法であった。。。


bool check(char cl, char cg, int init, int mg, string& S, string& T)
{
	int l = 1, g = mg;
	int N = S.size();
	for (int i = N - 1; i >= 0; --i) {
		if (T[i] == cl) {
			g = min(mg, g + 1);
		}
		else if (T[i] == cg) {
			l = max(1, l - 1);
		}

		if (S[i] == cl) {
			++l;
		}
		else if (S[i] == cg) {
			--g;
		}

		if (l > g) {
			return false;
		}
	}
	if (init < l || init > g) {
		return false;
	}
	else {
		return true;
	}
}


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

	int sr, sc;
	cin >> sr >> sc;

	string S, T;
	cin >> S >> T;

	bool ans = true;
	ans &= check('L', 'R', sc, W, S, T);
	ans &= check('U', 'D', sr, H, S, T);

	cout << (ans ? "YES" : "NO") << endl;
}


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

int counter(string S,int n){
    int s=S.size(),ans=0;
    map<char,int> m;
    for(int i=0;i<n;i++){
        char c=S[i];
        if(m[c]==0){
            m[c]=1;
        }
    }
    for(int i=n;i<s;i++){
        char d=S[i];
        if(m[d]!=0){
            ans++;
            m[d]=0;
        }
    }
    return ans;
}

int main(){
    int N,ans=0;
    string S;
    cin >> N >> S;
    for(int i=0;i<N;i++){
        ans=max(ans,counter(S,i));
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
### constant

MAX_B = 100

### main

loop do
  m, c, n = gets.split.map(&:to_i)
  break if (m | c | n) == 0

  q = []
  
  n.times do
    ki = gets.to_i
    q << gets.split.map(&:to_i)
  end
  
  books = MAX_B.times.map{m}
  desks = m.times.map{[]} + [(1...MAX_B).to_a]

  csum = 0

  while ! q.empty?
    reqs = q.shift
    next if reqs.empty?

    req = reqs.shift
    q << reqs if ! reqs.empty?

    pos = books[req]
    desks[pos].delete(req)
    csum += pos + 1

    if desks[0].length < c
      desks[0] << req
      books[req] = 0
      csum += 1
    else
      rpos = (1...m).find{|i| desks[i].length < c} || m
      desks[rpos] << req
      books[req] = rpos
      csum += rpos + 1
      
      tbook = desks[0].shift
      csum += 1

      tpos = (1...m).find{|i| desks[i].length < c} || m
      desks[tpos] << tbook
      books[tbook] = tpos
      csum += tpos + 1
      
      desks[rpos].delete(req)
      csum += rpos + 1

      desks[0] << req
      books[req] = 0
      csum += 1
    end
  end

  p csum
end

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

int dp[1001][MAX];

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

  int h, n;
  cin >> h >> n;

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

  for (int i = 0; i < 1001; ++i) {
    for (int j = 0; j < MAX; ++j) {
      dp[i][j] = 1000000000;
    }
  }
  dp[0][0] = 0;

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < MAX; ++j) {
      if (j - a[i] >= 0) dp[i+1][j] = min({dp[i+1][j], dp[i+1][j-a[i]] + b[i], dp[i][j-a[i]] + b[i]});
      dp[i+1][j] = min(dp[i+1][j], dp[i][j]);
    }
  }

  int ans = 1000000000;
  for (int i = h; i < MAX; ++i) ans = min(ans, dp[n][i]);

  cout << ans << '\n';

  return (0);
}


Measured memory_bytes: 394648
import static java.lang.System.out;
import java.io.BufferedReader;
import java.io.InputStreamReader;

class Main {
  public static void main(String...args) throws Exception {
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

    stdin.lines()
      .mapToInt(Integer::parseInt)
      .mapToDouble(v -> Math.pow(v, 3d))
      .mapToInt(v -> (int)v)
      .forEach(out::println);
  }
}

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

using lint = long long;
const lint INF = 1e18;

int N, K;
vector<lint> a, asum, bsum;

int main() {
    cin >> N >> K;
    a.resize(N + 1);
    asum.resize(N + 1);
    bsum.resize(N + 1);
    for (int i = 1; i <= N; i++) {
        cin >> a[i];
    }
    asum[0] = bsum[0] = 0;
    for (int i = 1; i <= N; i++) {
        asum[i] = asum[i - 1] + a[i];
        bsum[i] = bsum[i - 1] + max((lint)0, a[i]);
    }

    lint ans = -INF;
    for (int i = K; i <= N; i++) {
        int left = i - K, right = i;
        lint cand = max((lint)0, asum[right] - asum[left])
                + bsum[N] - bsum[right] + bsum[left];
        ans = max(ans, cand);
    }

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

Measured memory_bytes: 2560
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int main()
{
	char s[100];
	scanf("%s",s);
	int n = strlen(s);
	bool flag= true;
	for (int i = 1; i< n;i++)
	{
		if (s[i]!='9') flag=false;
	}
	if (flag) printf("%d",(n-1)*9+s[0]-'0');
	else printf("%d",(n-1)*9+s[0]-'1');
	return 0;
}


Measured memory_bytes: 256
n=int(input())
v=list(map(int,input().split()))
c=list(map(int,input().split()))
count=0
saidaiti=0
while True:
    if v[count]>c[count]:
        saidaiti=saidaiti+v[count]-c[count]
    count=count+1
    if count==n:
        break
print(saidaiti)


Measured memory_bytes: 3060
n = gets.to_i
points = Array.new(n, 0)
while line = gets
    a, b, c, d = line.chomp.split.map(&:to_i)
    if c > d
        points[a - 1] += 3
    elsif c < d
        points[b - 1] += 3
    else
        points[a - 1] += 1
        points[b - 1] += 1
    end
end

team = Hash.new{|h, k| h[k] = []}
points.each_with_index{|point, i|
    team[point] << i
}

result = []
rank = 1
points.sort.reverse.uniq.each{|point|
    team[point].each{|t| result[t] = rank}
    rank += team[point].length
}
puts result


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

typedef long long ll;
typedef unsigned long long ull;

int main()
{
    ll N, M ,C;
    cin >> N >> M >> C;
    ll Ai, Bi;
    vector<ll> B;
    ll res = 0;
    for(int i=0;i<M;i++){
        cin >> Bi;
        B.push_back(Bi);
    }
    for(int i=0;i<N;i++){
        auto sum = C;
        for(int j=0;j<M;j++){
            cin >> Ai;
            sum += Ai * B[j];
        }
        if(sum>0) res++;
    }
    cout<<res<<endl;

    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,i,k,x,y,j,s=0;
    cin>>n;
    long long A[n];
    for(i=0;i<n;i++)
      cin>>A[i];
    sort(A,A+n);
    x=A[0];
    y=A[n-1];
    k=10000000000000000;
  for(i=x;i<=y;i++)
  {
    s=0;
    for(j=0;j<n;j++)
    {
      s+=(i-A[j])*(i-A[j]);
    }
    k=min(s,k);
  }
  cout<<k<<endl;
return 0;
}


Measured memory_bytes: 3628
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <tuple>
#include <cassert>
#include <exception>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<P> vp;
#define rep(i,a,n) for(ll i = (a);i < (n);i++)
#define per(i,a,n) for(ll i = (a);i > (n);i--)
#define lep(i,a,n) for(ll i = (a);i <= (n);i++)
#define pel(i,a,n) for(ll i = (a);i >= (n);i--)
#define clr(a,b) memset((a),(b),sizeof(a))
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define sz size()
#define print(X) cout << (X) << endl
static const int INF = 1e+9+7;
ll n,m,l;
string s,t;
int d[200010],dp[1010][1010];
double w[1000],v[1000];
double box[200010];
char field[200][200];

int main(){
  while(1){
    cin >> n;
    if(!n)break;
    ll ans = 0;
    lep(i,1,96){
      l = pow(i,3);
      if(l <= n)ans = max(ans,l);
    }
    lep(i,1,96){
      l = i * (i+1)*(i+2)/6;
      if(l <= n)ans = max(ans,l);
    }
    lep(i,1,96){
      l = pow(i,3);
      lep(j,1,96){
        m = j*(j+1)*(j+2)/6;
        if(l + m <= n)ans = max(ans,l+m);
      }
    }
    print(ans);
  }
  return 0;
}


Measured memory_bytes: 3288
#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(),(c).end())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

using namespace std;
int main(void)
{
  int d,n;
  cin >> d >> n;
  vector<int> hot;
  hot.resize(d);
  REP(i,d) cin >> hot[i];
  vector<pair<pair<int,int>,int> > cloth;
  // cold,hot , color
  cloth.resize(n);
  REP(i,n) cin >> cloth[i].first.first >> cloth[i].first.second >> cloth[i].second;
  vector<vector<int> > dp;
  // dp[day][lastwear] = max score
  dp.resize(d+1);
  REP(i,d) dp[i].assign(n,-1048576);
  REP(j,n) if(cloth[j].first.first <= hot[0] && hot[0] <= cloth[j].first.second) dp[0][j]=0;
  FOR(i,1,d)
    REP(j,n)
    if(cloth[j].first.first <= hot[i] && hot[i] <= cloth[j].first.second)
	REP(k,n) dp[i][j]=max(dp[i][j],dp[i-1][k]+abs(cloth[j].second-cloth[k].second));
  int answer=0;
  REP(j,n) answer=max(answer,dp[d-1][j]);
  cout << answer << endl;
  return 0;
}

Measured memory_bytes: 1332
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+5,maxm=3e6+5;
inline int read(){
	char c=getchar();int t=0,f=1;
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){t=(t<<3)+(t<<1)+(c^48);c=getchar();}
	return t*f;
}
int n,mp[maxn][maxn];
struct edge{
	int v,p;
}e[maxm];
int val[maxn][maxn],tot,cnt;
int h[maxm];
inline void add(int a,int b){
	e[++cnt].p=h[a];
	e[cnt].v=b;
	h[a]=cnt;
}
int du[maxm];
int ans=-1,dis[maxm];
void topo(){
	queue<int> q;
	for(int i=1;i<=tot;i++){
		if(du[i]==0){q.push(i);dis[i]=1;ans=1;}
	}
	while(!q.empty()){
		int u=q.front();q.pop();
		for(int i=h[u];i;i=e[i].p){
			int v=e[i].v;
			du[v]--;
			if(du[v]==0){
				dis[v]=dis[u]+1;
				ans=max(ans,dis[v]);
				q.push(v);
			}
		}
	}
}
int main(){
	//freopen("5.in","r",stdin);
	//freopen("5.out","w",stdout);
	n=read();
	for(int i=1;i<=n;i++){
		for(int j=1;j<n;j++){
			mp[i][j]=read();
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=i+1;j<=n;j++){tot++;
			val[i][j]=val[j][i]=tot;
		}
	}
	//printf("%d\n",tot);
	for(int i=1;i<=n;i++){
		for(int j=2;j<n;j++){
			add(val[i][mp[i][j-1]],val[i][mp[i][j]]);
			du[val[i][mp[i][j]]]++;
		}
	}
	topo();
	for(int i=1;i<=tot;i++)
	if(du[i]!=0){ans=-1;break;}
	printf("%d\n",ans);
	return 0;
}


Measured memory_bytes: 27776
#include "bits/stdc++.h"
 
using namespace std;

using ll = long long;
using vll = vector<ll>;
using pl4 = pair<ll,ll>;
using str = string;
using vpl4 = vector<pair<ll,ll>>;

#define sz size()
#define be begin()
#define en end()
#define fi first
#define se second
#define pb push_back
#define mp make_pair

#define llin(x) ll (x);cin >>(x);
#define stin(x) str (x);cin >>(x);
#define FOR(i,a,b) for(ll i = a ; i <= b ; i++)
#define rFOR(i,b,a) for(ll i = a; i >= b ; i--)
#define SORT(x) sort(x.be, x.en)
#define rSORT(x) sort(x.rbegin(), x.rend())
#define say(x) cout<<(x);
#define sal(x) cout<<(x)<<endl;
#define yn(a) cout <<((a)?"yes":"no")<<endl;
#define Yn(a) cout <<((a)?"Yes":"No")<<endl;
#define YN(a) cout <<((a)?"YES":"NO")<<endl;
#define Imp(a) cout <<((a)?"Possible":"Impossible")<<endl;
#define IMP(a) cout <<((a)?"POSSIBLE":"IMPOSSIBLE")<<endl;
#define pow(a,b) ll(pow(a,b))

ll MOD=1000000007;

signed main(){
	llin(n);
	llin(k);
	YN(n>(k-1)*2);
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
const int N=1005;
int n,d1,d2;
bool v[N][N];
void solve(int d)
{
	int x=1;
	while(d%4==0) d>>=2,x<<=1;
	if(d&1)
	{
		for(int i=0;i<2*n;++i)
			for(int j=0;j<2*n;++j)
				if((i/x)+(j/x)&1) v[i][j]=true;
	}
	else
	{
		for(int i=0;i<2*n;++i)
			for(int j=0;j<2*n;++j)
				if(i/x&1) v[i][j]=true;
	}
}
int main()
{
	scanf("%d%d%d",&n,&d1,&d2);
	solve(d1),solve(d2);
	for(int i=0,k=1;i<2*n;++i)
		for(int j=0;j<2*n&&k<=n*n;++j)
			if(!v[i][j]) ++k,printf("%d %d\n",i,j);
}


Measured memory_bytes: 1536
#include<iostream>
using namespace std;
int kan(int i)
{
if(i==0)
{
  return 1;
}
else if(i==1)
{
  return kan(i-1);
}
else if(i==2)
{
  return kan(i-1)+kan(i-2);
}
else
{
  return kan(i-1)+kan(i-2)+kan(i-3);
}
};
int main()
{
 int i,days,years;
 while(cin>>i&&i)
 {
  if(kan(i)%10!=0)
    days=(kan(i)/10)+1;
  else
    days=kan(i)/10;

  if(days%365!=0)
    years=(days/365)+1;
  else
    years=days/365;
  cout<<years<<endl;
 }
}

Measured memory_bytes: 3008
#include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <numeric>
#include <sstream>
#include <queue>
#include <iomanip>
using namespace std;

#define REP(i,n) for(int (i)=0; (i)<(n); (i)++)
#define FOR(i,a,b) for(int (i)=(a); (i)<(b); (i)++)
#define PUSH(n,v) for(int i=0; i<(n); i++) {int j; cin >> j; v.push_back(j);}
#define ALL(v) v.begin(), v.end()
#define print(s) cout << (s) << endl

int priceAtA(int n) {
    int bags = n / 200;
    int lp = bags % 5;
    return 380 * lp + (380 * (bags - lp) * 0.8);
}

int priceAtB(int n) {
    int bags = n / 300;
    int lp = bags % 4;
    return 550 * lp + (550 * (bags - lp) * 0.85);
}

int priceAtC(int n) {
    int bags = n / 500;
    int lp = bags % 3;
    return 850 * lp + (850 * (bags - lp) * 0.88);
}

void solve107(int n) {
    int price = 1e9;
    for (int i = 0; i <= n; i+=200) {
        for (int j = 0; i + j <= n; j+=300) {
            for (int k = 0; i + j + k <= n; k+=500) {
                if (i + j + k == n) {
                    price = min(price, priceAtA(i) + priceAtB(j) + priceAtC(k));
                }
            }
        }
    }
    cout << price << endl;
}

int main() {
    int n;
    while(cin >> n, n) {
        solve107(n);
    }
    return 0;
}

Measured memory_bytes: 1164
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <x86intrin.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define V vector
template <typename T> bool chmin(T &a, const T &b) {if(a > b){a = b; return true;} return false;}
template <typename T> bool chmax(T &a, const T &b) {if(a < b){a = b; return true;} return false;}
template<typename A, size_t N, typename T> void Fill (A (&array)[N], const T & val) {fill ((T*)array, (T*)(array+N), val);}
V<int> dx = {-1, 1,  0, 0, -1, -1,  1, 1};
V<int> dy = { 0, 0, -1, 1, -1,  1, -1, 1};

V<int> color(1e5+1, 0);
V<V<P>> to(1e5+1);

void dfs (int v, int c) {
    color[v] = c;
    for (P p : to[v]) {
        int nv = p.first, a = p.second;
        if (color[nv] == 0) dfs(nv, c*a);
    }
}

int main () {
    int n; cin >> n;
    rep(i, n-1) {
        int u, v, w; cin >> u >> v >> w; u--; v--;
        if (w % 2 == 0) {
            to[u].emplace_back(v, 1); to[v].emplace_back(u, 1);
        }
        else {
            to[u].emplace_back(v, -1); to[v].emplace_back(u, -1);
        }
    }
    dfs(0, 1);
    rep(i, n) {
        if (color[i] == 1) cout << 1 << endl;
        else cout << 0 << endl;
    }
    return 0;
}

Measured memory_bytes: 10336
#include <bits/stdc++.h>   
#define rep(i, n) for(long long int i = 0; i < n; i++)
#define _rep(i, m, n) for(long long int i = m; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll mod = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
     
ll gcd(ll A, ll B){
    if(B == 0)return A;
    return gcd(B, A % B);
}
ll lcm(ll A, ll B){
    return A * B / gcd(A, B);
}
template <typename T>
T pow(T a, ll n, T e = 1) {
    T ret = e;
    while (n) {
        if (n & 1) ret *= a;
        a *= a;
        n >>= 1;
    }
    return ret;
}
 
template <int mod>
struct ModInt {
    int x;
    ModInt() : x(0) {}
    ModInt(long long x_) {
        if ((x = x_ % mod + mod) >= mod) x -= mod;
    }
    ModInt& operator+=(ModInt rhs) {
        if ((x += rhs.x) >= mod) x -= mod;
        return *this;
    }
    ModInt& operator-=(ModInt rhs) {
        if ((x -= rhs.x) < 0) x += mod;
        return *this;
    }
    ModInt& operator*=(ModInt rhs) {
        x = (unsigned long long)x * rhs.x % mod;
        return *this;
    }
    ModInt& operator/=(ModInt rhs) {
        x = (unsigned long long)x * rhs.inv().x % mod;
        return *this;
    }
 
    ModInt operator-() const { return -x < 0 ? mod - x : -x; }
    ModInt operator+(ModInt rhs) const { return ModInt(*this) += rhs; }
    ModInt operator-(ModInt rhs) const { return ModInt(*this) -= rhs; }
    ModInt operator*(ModInt rhs) const { return ModInt(*this) *= rhs; }
    ModInt operator/(ModInt rhs) const { return ModInt(*this) /= rhs; }
    bool operator==(ModInt rhs) const { return x == rhs.x; }
    bool operator!=(ModInt rhs) const { return x != rhs.x; }
    ModInt inv() const { return pow(*this, mod - 2); }
 
    friend ostream& operator<<(ostream& s, ModInt<mod> a) {
        s << a.x;
        return s;
    }
    friend istream& operator>>(istream& s, ModInt<mod>& a) {
        s >> a.x;
        return s;
    }
};
 
using mint = ModInt<1000000007>;
using Graph = vector<vector<int>>;
Graph G;
/*------------------------------------------------------------------*/

int main(){
    int n, k; cin >> n >> k;
    string s; cin >> s;
    vector<pair<int,int>> pos;

    int i = 0;
    while(i < n){
        int j;
        if(s[i] == '0'){
            j = i;
            while(i < n and s[i] == '0') i++;
        }else if(s[i] == '1'){
            j = i;
            while(i < n and s[i] == '1') i++;
        }
        pos.push_back({s[j] - '0', i - j});   
    }
    int m = pos.size();
    vector<int> T(m + 1);
    rep(i, m) T[i + 1] = T[i] + pos[i].second;

    int ans = 0;
    rep(i, m){
        if(pos[i].first == 0){
            int j = min(m, (int)i + 2 * k);
            ans = max(ans, T[j] - T[i]);
        }else{
            int j = min(m, (int)i + 2 * k + 1);
            ans = max(ans, T[j] - T[i]);
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 4640
#include <iostream>
using namespace std;
int main()
{
    int n,k;
    cin>>n>>k;
    int A[n]={};
    for(int i=0;i<k;i++)
    {
        int d;
        cin>>d;
        int b[d];
        for(int i=0;i<d;i++)
        {
        cin>>b[i];
        A[b[i]-1]=1;
        }
    }
    int count=0;
    for(int i=0;i<n;i++)
    {
        if(A[i]==0)
        count++;
    }
    cout<<count<<endl;
    return 0;
}


Measured memory_bytes: 3628