akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#define rep(i,s,n) for (int i=s;i<n;++i)
#define drep(i,s,n) for (int i=n;i>=s;--i)
#define all(x) (x).begin(),(x).end()
#define rall(a) (a).rbegin(),(a).rend()
#define print(x) cout<<(x)<<endl
using namespace std;
typedef long long ll;
int gcd(int a,int b)
{if (a%b == 0) return(b);
 else          return(gcd(b,a%b));}
int lcm(int a,int b)
{return a*b/gcd(a,b);}
void printv(vector<auto> v) {cout<<'[';rep(i,0,v.size()){cout<<v[i]<<',';}cout<<']'<<endl;}
void printvv(vector<vector<auto>> vv)
{
  cout<<'['<<endl;
  rep(q,0,vv.size()){cout<<'[';rep(p,0,vv.at(q).size())cout<<vv.at(q).at(p)<<',';cout <<']'<<endl;}
  cout<<']'<<endl;
}
void printpair(vector<pair<ll, ll>> vp)
{
  cout<<'['<<endl;
  rep(i,0,vp.size())
  {
    pair<ll, ll> p=vp[i];
    cout<<'{'<<p.first<<','<<p.second<<'}'<<endl;
  }
  cout<<'}'<<endl;
}
void solve(); int main(){cin.tie(0);ios::sync_with_stdio(0);solve();}
int step_x[] = {-1,0,0,1};
int step_y[] = {0,-1,1,0};
const long long INF = 1e12;
//------------------------------------------------------------
ll ans=0,n;
string s;
void solve()
{
    cin >> s;
    n=s.length();
    vector<int> dp(2019,0);

    dp[0]++;

    ll cur=0,coef=1;
    // 2019で割った余りは一巡だけ考えればよい(解説: Ti−1 == Tj (mod2019))←計算したものの再利用
    rep(i,0,n) {
      cur = (cur+coef*(s[n-1-i]-'0')%2019)%2019; // avoid overflow
      dp[cur]++;
      coef = coef*10%2019;
    }

    rep(i,0,2019) ans+=dp[i]*(dp[i]-1)/2; // 2の時だけ1追加する（dp[i]=0,1,2）

    print(ans);

    return;
}



Measured memory_bytes: 3732
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(int (i) = (s);(i) <= (e);(i)++)
#define all(x) x.begin(),x.end()


int main(){
  string s;
  cin >> s;
  int n = s.size();
  if(s.back() != '0' || s.front() != '1'){
    cout << "-1"<< endl;
    return 0;
  }
  s.pop_back();
  {
    string t =s ;
    reverse(all(t));
    if(s != t){
      cout << -1 << endl;
      return 0;
    }
  }

  int cnt = 3;
  vector<pair<int,int>> ans;
  vector<int> l,r;
  l.push_back(1);
  r.push_back(2);
  int lead = 0;
  for(int i = 2;i <= n - i;i++){
    if(s[i - 1] == '0'){
      lead++;
    }
    else{
      for(int j = 0;j < lead;j++){
        l.push_back(cnt++);
        r.push_back(cnt++);
      }
      int L = cnt++;
      int R = cnt++;
      for(int x : l){
        ans.push_back({x,L});
      }
      for(int x : r){
        ans.push_back({x,R});
      }
      l.clear();
      r.clear();
      l.push_back(L);
      r.push_back(R);
      lead = 0;
    }
  }
  if(cnt == n + 1){
    ans.push_back({l.front(),r.front()});
  }
  else{
    int CEN = cnt++;
    for(int x : l){
      ans.push_back({CEN,x});
    }
    for(int x : r){
      ans.push_back({CEN,x});
    }
    for(int i = cnt;i <= n;i++){
      ans.push_back({CEN,i});
    }
  }

  for(auto p : ans){
    cout << p.first << " " << p.second << endl;
  }
}


Measured memory_bytes: 2548
#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
using namespace std;
int dp[3005][3005];
int main(){
  string s,t;
  cin>>s>>t;
  
  int slen=s.size();
  int tlen=t.size();
  
  stack<char> st;
  
  for(int i=1;i<=slen;i++){
    for(int j=1;j<=tlen;j++){
      if(s[i-1]==t[j-1]){
	dp[i][j]=dp[i-1][j-1]+1;
      }else{
	dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
      }
    }
  }
  
  int i=slen,j=tlen;
  while(i>=1 && j>=1){
    if(s[i-1]==t[j-1]){
      st.push(s[i-1]);
      i--;
      j--;
    }else{
      if(dp[i-1][j]>=dp[i][j-1]){
	i--;
      }else{
	j--;
      }
    }
  }
  while(!st.empty()){
    cout<<st.top();
    st.pop();
  }
  cout<<"\n";
  
  return 0;
}


Measured memory_bytes: 35456
// D - Not Divisible

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

vector<int> enum_divisor(int n) {
	vector<int> result;
	for(int i=1; i*i <= n; i++){
		if(n % i == 0){
			result.push_back(i);
			if(i*i != n) result.push_back(n / i);
		}
	}

	return result;
}

int A[200000];
unordered_map<int, int> cnt;

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

	for (int i=0; i<N; i++) {
		cin >> A[i];
		cnt[A[i]]++;
	}

	int ans = 0;

	for (int i=0; i<N; i++) {
		cnt[A[i]]--;

		bool no_divisor = true;

		for(int k=1; k*k <= A[i]; k++){
			if(A[i] % k == 0) {
				int d1 = k;
				int d2 = A[i] / k;

				if (cnt[d1] > 0 || (cnt[d2] > 0)) {
					no_divisor = false;
					break;
				}
			}
		}

		if (no_divisor) ans++;

		cnt[A[i]]++;
	}

	cout << ans << endl;

	return 0;
}

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

int main()
{
	int n,a,i,b,j=0,k,s=0;
	cin>>n>>a>>b;
	for(i=a;i<=n;i++)
	{
		j=0;k=i;
		while(k!=0)
		{
			j+=k%10;
			k/=10;
		}
		if(j>=a&&j<=b) s+=i;
	}
	cout<<s;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <string.h>
using namespace std; using ll = long long; using pll = pair<ll, ll>;
using vl = vector<ll>; using vll = vector<vl>; using vpll = vector<pll>;
int ctoi(char c) {
	switch (c) {
	case '0': return 0; case '1': return 1; case '2': return 2;
	case '3': return 3; case '4': return 4; case '5': return 5;
	case '6': return 6; case '7': return 7; case '8': return 8;
	case '9': return 9;  default: return 0;
	}
}
bool pairCompare(const pll firstElof, pll secondElof)
{
	return firstElof.second < secondElof.second;
}
ll nod(ll F) { return log10(F) + 1; }
//**定義場所**//
//gcd, isPrime, eratos, modinv, bitSearch, dfs, bfs, dijkstra, UnionFind//
ll i, j, k, l, m, n; ll N, M, K, H, W, L; string S;
ll MOD = 1000000007; ll ans = 0; ll INF = 9999999999999;
vl flag, color; vll path;
//***********//
int main() {
	cin >> N >> K;
	while (N > 0) {
		N /= K;
		ans++;
	}
	cout << ans;
}


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

cnt = 0
for i in range(N):
	if A[i] >= sum(A)/(4*M):
		cnt += 1
	else:
		cnt = cnt

if cnt >= M:
	print('Yes')
else:
	print('No')


Measured memory_bytes: 3064
/*siddharth goyal*/
#include<bits/stdc++.h>
using namespace std;
#define   pb              push_back
#define   REP(i,n)        for(int i=1;i<=n;i++)
#define   FOR(i,a,b)      for(int i=a;i<=b;i++)
#define   all(v)          v.begin(),v.end()
#define   F               first
#define   S               second
#define   vl              vector<LL>
#define   itr             ::iterator it
#define   lb              lower_bound
#define   ub              upper_bound
#define   LL              long long
#define   ULL             unsigned long long
#define   ret             return 
#define   sz(x)           (LL)x.size()
LL n,i,j ; 
string ans ;
string s,t ;
LL dp[3000+4][3004] ;  
int main()
{   // Read the constraints,highlights,Time Limit,Test Cases
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>s>>t ; 
    LL n = (LL)s.length(),m = sz(t) ; 
    s = "X" + s ; 
    t = "T" + t ; 
    for(int i=1;i<=n;i++)
    {  for(int j=1;j<=m;j++)
         if(s[i]==t[j]) dp[i][j] = dp[i-1][j-1] + 1 ; 
          else dp[i][j] = max(dp[i-1][j],dp[i][j-1]) ;  
    }
    LL I = n,J = m ; 
    while(I>0 and J>0)
    {  if(s[I]==t[J]) ans.pb(s[I]),I--,J-- ; 
       else 
       {  if(dp[I-1][J]>=dp[I][J-1]) I-- ; 
          else J-- ;   
       }
    }
    for(int i=sz(ans)-1;i>=0;i--) cout<<ans[i] ; 
}
//Check for 0,1 and other corner cases.

Measured memory_bytes: 70656
#include<iostream>
#include<cmath>
#define LIMIT 50000
using namespace std;
int a, b , d, mx, my;

void update( int x, int y ){
    if ( y < 0 ) return;
    if ( x + y == mx + my && a*x + b*y < a*mx + b*my ){
	mx = x; my = y;
    } else if ( x + y < mx + my ){
	mx = x; my = y;
    }
}

main(){
    while( cin >> a >> b >> d, (a || b || d) ){
	mx = my = LIMIT;
	for ( int y, x = 0; x <= b + d; x++ ){
	    if ( (a*x - d)%b == 0 ){y = (a*x - d)/b; update( x, y );}
	    if ( (a*x + d)%b == 0 ){y = (a*x + d)/b; update( x, y );}
	    if ( (d - a*x)%b == 0 ){y = (d - a*x)/b; update( x, y );}
	}
	cout << mx << " " << my << endl;
    }
}

Measured memory_bytes: 868
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
int h,w;
char a[411][411];
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int white,black;
int ans=0;
bool used[411][411];
void dfs(int x,int y){
	used[x][y]=true;
	if(a[x][y]=='.')white++;
	else black++;
	rep(i,4)if(a[x][y]!=a[x+dx[i]][y+dy[i]]&&!used[x+dx[i]][y+dy[i]]&&(a[x+dx[i]][y+dy[i]]=='#'||a[x+dx[i]][y+dy[i]]=='.'))dfs(x+dx[i],y+dy[i]);
}
signed main(){
	cin>>h>>w;
	for(int i=1;i<=h;i++)for(int j=1;j<=w;j++)cin>>a[i][j];
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			if(a[i][j]=='.')continue;
			if(used[i][j])continue;
			white=0,black=0;
			dfs(i,j);
			ans+=white*black;
		}
	}
	cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 8064
#include <stdio.h>
int main(){
  int a[5]={0},b[5]={0},i;
  for(i=0;i<4;i++){
    scanf("%d",&a[i]);
  }
  for(i=0;i<4;i++){
    scanf("%d",&b[i]);
  }
  for(i=0;i<4;i++){
    a[4]+=a[i];
    b[4]+=b[i];
  }
  if(a[4]>b[4])printf("%d\n",a[4]);
  else printf("%d\n",b[4]);
  return 0;
}

Measured memory_bytes: 592
#include<stdio.h>

#define INITIALIZE_INT 0//整数型の初期値
#define END 0//入力の終了値
#define START_NUMBER 1//魔法陣に入る最初の数値
#define ROUND_UP 1//切り上げするときの値
#define HALF 2 //魔法陣のセンターの座標を求めるとき使う値

enum
{
	OFF,
	ON
};

int main(void)
{
	
	while(1)
	{
		unsigned short ix = INITIALIZE_INT;
		unsigned short iy = INITIALIZE_INT;
		
		short x = INITIALIZE_INT;//魔法陣のx座標
		short y = INITIALIZE_INT;//魔法陣のy座標
	
		unsigned short input_n = INITIALIZE_INT;//入力値n
		scanf("%hd", &input_n);
		if(END == input_n)
		{
			break;
		}
		
		unsigned short number = START_NUMBER;
		short magic_square[input_n][input_n];
		
		for(iy = 0; iy < input_n; iy++)
		{
			for(ix = 0; ix < input_n; ix++)
			{
				magic_square[iy][ix] = 0;
			}
		}
		
		unsigned short loop_flag = OFF;
		unsigned short center = INITIALIZE_INT;
		center = input_n / HALF;
		//printf("bb=%hd\n", center);
		magic_square[center + 1][center] = number;
		//printf("aa=%d\n",magic_square[center + 1][center]);
		number++;
		x = center;
		y = center + 1;
		/*
		for(iy = 0; iy < input_n; iy++)
		{
			for(ix = 0; ix < input_n; ix++)
			{
				printf("%d ",magic_square[iy][ix]);
			}
			printf("\n");
		}*/
		while(1)
		{
			x++;
			y++;
			//printf("x = %d y= %d\n",x,y);
			if(x >= input_n )
			{
				x = 0;
			}
			if(y >= input_n)
			{
				y = 0;
			}
			if( x < 0)
			{
				x = input_n - 1;
			}
			//printf("x = %d y= %d\n",x,y);
			//printf("mg = %d\n",magic_square[y][x]);
			if(0 == magic_square[y][x])
			{
				magic_square[y][x] = number;
				//printf("cccc\n");
				number++;
			}
			else if(0 != magic_square[y][x])
			{
				while(1)
				{
					y++;
					x--;
					
					if(x >= input_n )
					{
						x = 0;
					}
					if(y >= input_n)
					{
						y = 0;
					}
					if( x < 0)
					{
						x = input_n - 1;
					}
					//printf("ax = %d ay= %d\n",x,y);
					if(0 == magic_square[y][x])
					{
						magic_square[y][x] = number;
						number++;
						break;
					}
				}
			}
			loop_flag = OFF;
			for(iy = 0; iy < input_n; iy++)
			{
				for(ix = 0; ix < input_n; ix++)
				{
					if(0 == magic_square[iy][ix])
					{
						loop_flag = ON;
					}
				}
			}
			if(OFF == loop_flag )
			{
				break;
			}
			//printf("number=%d\n", number);
		}
		
		
		for(iy = 0; iy < input_n; iy++)
		{
			for(ix = 0; ix < input_n; ix++)
			{
				printf("%4d",magic_square[iy][ix]);
			}
			printf("\n");
		}
	}
	return 0;
}

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

int main() {
    int N;
    cin >> N;
    pair<int, int> BA[N];
    for (int i = 0; i < N; i++) {
        cin >> BA[i].second >> BA[i].first;
    }
    sort(BA, BA + N);
    string ans = "Yes";
    int now = 0;
    for (auto v : BA) {
        now += v.second;
        if (now > v.first) ans = "No";
    }
    cout << ans << endl;
}

Measured memory_bytes: 1792
#include<stdio.h>
#include<string.h>
#define MAX 1000

typedef struct dice{
 int num[7];
}dice;


int main(){
dice D;
int i;
int lenC;
int temp;
char com[MAX];

for(i=1;i<=6;i++)scanf("%d",&D.num[i]);

scanf("%s",com);
lenC=strlen(com);

for(i=0;i<lenC;i++){
 switch(com[i]){
  case 'N':temp=D.num[1];
           D.num[1]=D.num[2];
           D.num[2]=D.num[6];
           D.num[6]=D.num[5];
           D.num[5]=temp;
           break;

  case 'S':temp=D.num[1];
           D.num[1]=D.num[5];
           D.num[5]=D.num[6];
           D.num[6]=D.num[2];
           D.num[2]=temp;
           break;

  case 'E':temp=D.num[1];
           D.num[1]=D.num[4];
           D.num[4]=D.num[6];
           D.num[6]=D.num[3];
           D.num[3]=temp;
           break;

  case 'W':temp=D.num[1];
           D.num[1]=D.num[3];
           D.num[3]=D.num[6];
           D.num[6]=D.num[4];
           D.num[4]=temp;
           break;

 }

}

printf("%d\n",D.num[1]);

return 0;
}


Measured memory_bytes: 2104
#include<iostream>
#include<iomanip>
#include<math.h>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<string>
#define INF 1000000000ll
#define MOD 1000000007ll
#define EPS 1e-10
#define REP(i,m) for(long long i=0; i<m; ++i)
#define FOR(i,n,m) for(long long i=n; i<m; ++i)
#define DUMP(n,a) for(long long dump=0; dump<n; ++dump) { cout<<a[dump]; if(dump!=n-1) cout<<" "; else cout<<endl; }
#define ALL(v) v.begin(),v.end()
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef long double ld;

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	string s;
	cin>>s;
	int l=INF,r=-1;
	REP(i,s.size()) {
		if(s[i]=='C'&&l==INF) l=i;
		if(s[i]=='F') r=i;
	}
	if(l<r) cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
}


Measured memory_bytes: 256
import java.util.Scanner;
class Main{
    public static void main(String arg[]){
        Scanner scn=new Scanner(System.in);
        int rate = scn.nextInt();
        if(rate<1200){
            System.out.println("ABC");
        }else if(2800<=rate){
            System.out.println("AGC");
        }else{
            System.out.println("ARC");
        }
    }
}

Measured memory_bytes: 21716
/*@author Vipen Loka*/
#include <bits/stdc++.h>
#define endl '\n'
#define ff first
#define ss second
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define deb(x) cout << #x << ':' << x << '\n';

using namespace std;

void solve(){
	int i,j;
	string s;
	cin >> s;
	if(s[s.size()-1]=='s'){
		s.push_back('e');
		s.push_back('s');
	}
	else{
		s.push_back('s');
	}

	cout << s << endl;

}


int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(0);
    
	int T=1;
	// cin >> T;
	while (T--){
		solve();
	}
}


Measured memory_bytes: 3672
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <iomanip> // 文字幅の指定 setw(int), 詰めたい文字の指定 setfill(char)
#include <algorithm> // sort
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define p(x) cout << (x) << endl
#define FOR(i,r,n) for(ll i=(ll)(r); i < (ll)(n); i++)
#define RFOR(i,r,n) for(ll i=(ll)(n); i>=r; i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) REP(i,0,n)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define pb push_back

// 定義する？→左揃えで0埋め
// cout << setfill('0') << left << setw(4) << 12; // "0012"
// http://marycore.jp/prog/cpp/padding-left-right-zero/

const int inf = (int)1e9;
const ll INF = (ll)1e18;

// vectorの中身をcout << vs;で表示
template <class T>
ostream &operator<<(ostream &o, const vector<T> &v)
{
    o << "{";
    for (int i = 0; i < (int)v.size(); i++) o << (i > 0 ? ", " : "") << v[i];
    o << "}";
    return o;
}

ll gcd (ll a, ll b) {
	while (b > 0) {
		ll t = a % b;
		a = b, b = t;
	}
	return a;
}

ll lcm (ll a, ll b) {
	return (a / gcd (a, b)) * b;
}

bool is_prime (ll n) {
		if (n < 2) return false;
		else if (n == 2) return true;
		else if (n % 2) return false;
		
		double sqrtn = sqrt(n);
		for (ll i = 3; i < sqrtn; i += 2) {
			if (n % i == 0) return false; 
		}
		return true;
}

ll ctoi(char c) {
	return (ll)(c - 48);
}

ll n, k, cnt=0, ans=0;
string s,t;

// ==================== template ============================

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

	cin >> s;
	REP(i, s.size()) {
		if (s.at(i) == 'o') cnt += 1; 
	}
	cout << 700 + 100 * cnt << endl;
}



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

#define pb push_back
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,n,s) for(int i=(s);i<(n);i++)
#define rrep(i,n) for(int i=(n-1);i>=0;i--)
#define rreps(i,n,s) for(int i=s;i>=n;i--)
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

using ll = long long;
using namespace std;
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int MOD = 1000000007;

int main(){
cin.tie(0);
ios::sync_with_stdio(false);
string s;
cin>>s;
int n=s.size()+1;
vector<ll>a(n,0);

rep(i,n-1){
    if(s[i]=='<'){
        a[i+1]=a[i]+1;
    }
}
rreps(i,0,n-2){
    if(s[i]=='>'){
        a[i]=max(a[i+1]+1,a[i]);
    }
}
ll ans=0;
rep(i,n)ans+=a[i];
cout<<ans<<endl;
return 0;
}

Measured memory_bytes: 4764
program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: a,b,c,k

    read*, a,b,c,k

    if (a+b >= k) then
        print'(i0)', min(a,k)
    else
        print'(i0)', a-(k-(a+b))
    end if
    
end program main

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

using ll = long long;
using ld = long double;
using pii = pair<int, int>;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
#define m0(x) memset(x,0,sizeof(x))

int dx4[4] = {1,0,-1,0}, dy4[4] = {0,1,0,-1};


int main()
{
    int a[5];
    for(int i = 0; i < 5; i++)
    {
        cin>>a[i];
    }
    int mod=10;
    for(int i = 0; i < 5; i++)
    {
        int tmp = a[i]%10;
        if(tmp>0) mod=min(mod,tmp);
    }
    int ans = 0;
    for(int i = 0; i < 5; i++)
    {
        ans+=((a[i]+9)/10)*10;
    }
    ans=ans-10+mod;
    cout<<ans<<endl;
 
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <ccomplex>
#include <vector>
#include <cassert>
#include <utility>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <functional>
#include <cmath>
#include <iomanip>
#include <map>
#include <numeric>
#include <list>
#include <assert.h>
#include <math.h>
#include <valarray>
#include <stdio.h>
#include <algorithm>
#include <set>
#include <complex>
#include <list>
#include <time.h>
#include <stack>
#include <locale>
#include <clocale>
#include <ctype.h>
#include <wchar.h>
#include <random>
#include <vector>
#include <unordered_map>
#include <bitset>

using namespace std;
typedef long long int LL;
typedef long long int ll;
typedef pair<long long int, long long int> pii;
typedef pair<double, double> pdd;

#define SORT(c) sort((c).begin(),(c).end())
#define BACKSORT(c) sort((c).begin(),(c).end(),std::greater<LL>())
#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

LL mod = 1000000007;

class union_find {
	vector<int> par;
	int find(int a) {
		return par[a] < 0 ? a : par[a] = find(par[a]);
	}
public:
	union_find(int n) : par(n, -1) {}
	bool same(int a, int b) {
		return find(a) == find(b);
	}
	void unite(int a, int b) {
		a = find(a);
		b = find(b);
		if (a == b) return;
		if (par[a] < par[b]) {
			par[a] += par[b];
			par[b] = a;
		}
		else {
			par[b] += par[a];
			par[a] = b;
		}
	}
	int size(int a) {
		return -par[find(a)];
	}
};


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

  while(true){
    LL n;
    cin >> n;
    if(n==0){
      break;
    }


    vector<vector<LL>> v(n, vector<LL>(0));

    bool flag = false;

    REP(i,n){
      LL m;
      cin >> m;
      REP(j,m){
        LL a;
        cin >> a;
        v[i].push_back(a);
      }
      SORT(v[i]);
    }

    LL m = 9999;

    REP(ddd,30){
    REP(iii,n){
      union_find uf(n);
      REP(dd,30-ddd){
        LL d = (30-ddd) - dd;
        vector<LL> s(0);

        bool f = false;

        REP(i, n)
        {
          REP(j,v[i].size()){
            if(v[i][j]==d){
              s.push_back(i);
              if(uf.same(i,iii)){
                f = true;
              }
            }
          }
        }

        if(f){
          FOR(i,1,s.size()){
            uf.unite(s[0], s[i]);
          }
        }

        if(uf.size(0)==n){
          m = 30-ddd;
          break;
        }
      }
    }
    }

    if(m==9999){
      cout << -1 << endl;
    }else{
      cout << m << endl;
    }
  }
}



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

namespace abc062
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] str = Console.ReadLine().Split(' ');

            int x = Convert.ToInt32(str[0]);
            int y = Convert.ToInt32(str[1]);

            List<int> groupA =new List<int> {1,3,5,7,8,10,12};
            List<int> groupB = new List<int> {4,6,9,11}; 
            List<int> groupC = new List<int> {2};

            if (groupA.Contains(x) && groupA.Contains(y))
            {
                Console.WriteLine("Yes");
            }
            else if (groupB.Contains(x) && groupB.Contains(y))
            {
                Console.WriteLine("Yes");
            }
            else if (groupC.Contains(x) && groupC.Contains(y))
            {
                Console.WriteLine("Yes");
            }
            else
            {
                Console.WriteLine("No");
            }
        }
    }
}


Measured memory_bytes: 13152
x,k,d = map(int, input().split())

#正の処理
if x > 0:
    neededmove = x//d
    amari = x%d
    if abs(amari - d) < abs(amari):
        amari = abs(amari - d)
        neededmove += 1
    if k < neededmove:
        print(abs(x-k*d))
    elif k == neededmove:
        print(abs(amari))
    else:
        hoge = k - neededmove
        if hoge % 2 == 0:
            print(abs(amari))
        else:
            if amari <= 0:
                print(abs(amari + d))
            else:
                print(abs(amari-d))
            
#負の処理
elif x < 0:
    neededmove = abs(x//d)
    amari = x%d
    if abs(amari + d) < abs(amari):
        amari = abs(amari + d)
        neededmove += 1
    if k < neededmove:
        print(abs(x+k*d))
    elif k == neededmove:
        print(abs(amari))
    else:
        hoge = k - neededmove
        if hoge % 2 == 0:
            print(abs(amari))
        else:
            if amari <= 0:
                print(abs(amari + d))
            else:
                print(abs(amari-d))
#0の処理
else:
    if k % 2 == 0:
        print(0)
    else:
        print(d)

Measured memory_bytes: 61768
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<set>
#include<cstring>
 
 
using namespace std;
#define int long long int
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1001001001
#define LLINF 1001001001001001001
#define mp make_pair
#define pb push_back

int X,N;
int L[104];
int ans=0;
signed main(){
    scanf("%lld %lld",&N,&X);
    int basho=0;
    rep(i,N+1){
        scanf("%lld",&L[i]);
        if(i>0)basho+=L[i-1];
        if(basho<=X)ans++;
    }
    printf("%lld\n",ans);
}

Measured memory_bytes: 128
#!python3.8
# -*- coding: utf-8 -*-
# abc162/abc162_c
import sys
from math import gcd
from itertools import product

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    K = i2n()
    r = sum(gcd(gcd(i, j), k) for (i, j, k) in product(range(1, K+1), range(1, K+1), range(1, K+1)))
    print(r)
    return

main()


Measured memory_bytes: 9236
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
//#include<boost/multiprecision/cpp_int.hpp>
//#include<boost/multiprecision/cpp_dec_float.hpp>
//namespace mp=boost::multiprecision;
//#define mulint mp::cpp_int
//#define mulfloat mp::cpp_dec_float_100
using namespace std;
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
//constexpr int MOD=1000000007;
constexpr int MOD=998244353;
#define INF (1<<30)
#define LINF (lint)(1LL<<56)
#define endl "\n"
#define rep(i,n) for(lint (i)=0;(i)<(n);(i)++)
#define reprev(i,n) for(lint (i)=(n-1);(i)>=0;(i)--)
#define Flag(x) (1<<(x))
#define Flagcount(x) __builtin_popcountll(x)
#define pint pair<int,int>
#define pdouble pair<double,double>
#define plint pair<lint,lint>
#define fi first
#define se second
typedef long long lint;
int dx[8]={1,1,0,-1,-1,-1,0,1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
const int MAX_N=2e5+5;
//struct edge{lint to,num;};
//vector<int> bucket[MAX_N/1000];

int main(void){
    lint N,K;
    cin >> N >> K;
    int A[N];
    rep(i,N) cin >> A[i],A[i]--;
    int to[70][N];
    rep(i,N) to[0][i]=A[i];
    for(int i=1;i<70;i++) rep(j,N) to[i][j]=to[i-1][to[i-1][j]];
    int ans=0;
    rep(i,62) if((1LL<<i)&K) ans=to[i][ans];
    cout << ans+1 << endl;
}

Measured memory_bytes: 59120
double x[100],y[100],A,h,c,X,Y;i,j,k,s,m,M;main(n){for(;scanf("%d",&n),n;printf("%d\n",M+2)){M=-1;for(i=n;i--;)scanf("%lf,%lf",x+i,y+i);for(i=n;--i;)for(j=i;j--;)if(A=hypot(x[i]-x[j],y[i]-y[j])/2,A<=1)for(c=atan2(y[i]-y[j],x[i]-x[j]),h=sqrt(1-A*A),s=2;s--;h=-h)for(m=0,k=n;k--;M=m>M?m:M)m+=k-i&&k-j&&hypot(x[j]+A*cos(c)-h*sin(c)-x[k],y[j]+A*sin(c)+h*cos(c)-y[k])<=1;}exit(0);}

Measured memory_bytes: 0
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
from itertools import product
def resolve():
    L=input()
    dp=[0]*2
    dp[0]=1
    for l in L:
        l=int(l)
        ndp=[0]*2
        for a,b,lt in product(range(2),repeat=3):
            if(a*b): continue
            if((not lt) and (a+b>l)): continue
            ndp[max(lt,a+b<l)]+=dp[lt]
            ndp[max(lt,a+b<l)]%=MOD
        dp=ndp
    print(sum(dp)%MOD)
resolve()

Measured memory_bytes: 42332
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
//INSERT ABOVE HERE

signed main(){
  Int n,m;

  while(cin>>n>>m,n){
    auto encode=[n](auto &v){
      Int x=0;
      for(Int i=0;i<n;i++) x=x*3+v[i];
      return x;
    };
  
    auto decode=[n](Int x){
      vector<Int> v(n);
      for(Int i=0;i<n;i++){
	v[n-(i+1)]=x%3;
	x/=3;
      }
      return v;
    };

    vector<Int> dp(14348907,-1);
    
    queue<Int> q;
    for(Int i=0;i<2;i++){
      vector<Int> b(n,i*2);
      Int k=encode(b);
      dp[k]=0;
      q.emplace(k);
    }

    while(!q.empty()){
      Int k=q.front();
      vector<Int> v=decode(k);q.pop();
      vector<Int> x(3,-1);
      for(Int i=0;i<n;i++) x[v[i]]=i;
      for(Int i=0;i<3;i++){
	for(Int j=0;j<3;j++){
	  if(i==j||abs(i-j)==2) continue;
	  if(x[i]>x[j]){
	    v[x[i]]=j;
	    Int nk=encode(v);
	    if(dp[nk]<0){
	      dp[nk]=dp[k]+1;
	      q.emplace(nk);
	    }
	    v[x[i]]=i;
	  }
	}
      }	  
    }
  
    vector<Int> v(n);
    for(Int i=0;i<3;i++){
      Int k;
      cin>>k;
      for(Int j=0;j<k;j++){
	Int x;
	cin>>x;
	v[x-1]=i;
      }
    }
    Int k=encode(v);
    //cout<<k<<":"<<dp[k]<<endl;
    cout<<(dp[k]>m?-1:dp[k])<<endl;
  }
  return 0;
}



Measured memory_bytes: 114924
(let* ((a (read))
       (lst (loop :repeat a :collect (read-line)))
       (srt nil))
  (defun f (k)
      (cons (subseq k 0 (position #\Space k :test #'char=)) (parse-integer (subseq k (1+ (position #\Space k :test #'char=))))))
  (map-into lst #'f lst)
  (defun ff< (p q)
    (if (string= (car p) (car q))
        (> (cdr p) (cdr q))
        (string< (car p) (car q))))
  (setf srt (sort (copy-seq lst) #'ff<))
  (mapcar (lambda (k) (print (1+ (position k lst)))) srt))

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

#define rep(x,to) for(int x=0;x<to;x++)
#define rep2(x,from,to) for(x=from;x<to;x++)

using namespace std;

typedef pair<string,int> mypair;

int main(void){
vector< mypair> vvc(1,mypair("",0));
string wst, qst="", mst;

	while(cin >> wst){
		if(wst.size() > qst.size()) qst = wst;
		int sszz=vvc.size();
		bool addsw = true;
		rep(i,sszz){
			if(wst == vvc[i].first){
				vvc[i].second++;
				addsw=false;
				break;
			}
		}
		if(addsw) vvc.push_back(mypair(wst,1));
	}
	int mx = -999, sszz = vvc.size();
//	rep(i,sszz) cout << i << ": " << vvc[i].first << "- " << vvc[i].second << endl;
	rep(i,sszz){
		if(mx < vvc[i].second){
			mx = vvc[i].second;
			mst = vvc[i].first;
		}
	}
	cout <<  mst << " " << qst << endl;
	return 0;
}

Measured memory_bytes: 1196
#include<bits/stdc++.h> 
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 
#include <functional> 
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define MOD 1000000007
#define MOD9 1000000009
#define pi 3.1415926535
#define ms(s, n) memset(s, n, sizeof(s))
#define prec(n) fixed<<setprecision(n)
#define eps 0.000001
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define bolt ios::sync_with_stdio(0)
#define light cin.tie(0);cout.tie(0)
#define umll unordered_map<ll,ll>
#define mll map<ll,ll>
#define forr(i,p,n) for(ll i=p;i<(ll)n;i++)
#define test int t;cin>>t;while(t--)
#define MAXN 1000003
typedef long long ll;
using namespace std;
ll mult(ll a,ll b, ll p=MOD){return ((a%p)*(b%p))%p;}
ll add(ll a, ll b, ll p=MOD){return (a%p + b%p)%p;}
ll fpow(ll n, ll k, ll p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n%p; n = n * n%p;} return r;}
ll inv(ll a, ll p = MOD) {return fpow(a, p - 2, p);}
ll inv_euclid(ll a, ll m = MOD){ll m0 = m;ll y = 0, x = 1;if (m == 1)return 0;while (a > 1){ll q = a / m;ll t = m;m = a % m, a = t;t = y;y = x - q * y;x = t;}if (x < 0)x += m0;return x;}
using namespace __gnu_pbds;  
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>ordered_set; 
ll dp[100001];
vector<ll>adj[100001];
bool vis[100001];
void dfs(ll x) {
	vis[x] = true;
	for(auto i:adj[x]) {
		if(!vis[i]) {
			dfs(i);
		}
	dp[x] = max(dp[x],1+dp[i]);
	}
}
void solve() {
	ll n,m;
	cin>>n>>m;
	forr(i,0,m) {
		ll x,y;
		cin>>x>>y;
		adj[x].pb(y);
	}
	forr(i,1,n+1) {
		if(!vis[i]) {
			dfs(i);
		}
	}
	ll ans = 0;
	forr(i,1,n+1) ans = max(ans,dp[i]);
	cout<<ans<<"\n";
}
int main(){
	bolt;
	light;
		solve();
 	return 0;
 }

Measured memory_bytes: 8704
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<map>
#include<tuple>
#define rep(index,num) for(int index=0;index<num;index++)
#define rep1(index,num) for(int index=1;index<=num;index++)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-15
#define mp(a1,a2) make_pair(a1,a2)
typedef long long ll;
using namespace std;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl=1e+18+1;
int INF=1e+9+1;
int main(){
	int N;
	int a[100001];
	int pm1num[100001]={0};
	scan(N);
	rep(i,N){
		scan(a[i]);
		if(a[i]!=0) pm1num[a[i]-1]++;
		pm1num[a[i]]++;
		pm1num[a[i]+1]++;
	}
	int Xmax=0;
	rep(i,100001){
		Xmax=max(Xmax,pm1num[i]);
	}
	prin(Xmax);

	return 0;
}


Measured memory_bytes: 1024
#include<stdio.h>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<utility>
#include<memory>
#include<cmath>
#include<stack>
#include<tuple>
#include<numeric>
#include<cassert>

#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define pb push_back
#define DEBUG false

using namespace std;

using ll = long long;
using P = pair<int,int>;

const int mod=1e9+7,INF=1<<30;
const double EPS=1e-12,PI=3.1415926535897932384626;
const ll LINF=1LL<<60;
const int MAX_N=1003;
const ll lmod = 1e9+7;

int N;
vector<int> adj[MAX_N];
int par[MAX_N],x[MAX_N],m[MAX_N];
bool dp[2][5003];
bool ans=true;

void dfs(int v,int p){
  if(!ans) return;
  for(auto&& u : adj[v]){
    if(u==p) continue;
    dfs(u,par[u]);
  }
  if(adj[v].size()==1&&(v!=0)){
    m[v] = 0;
    return;
  }
  int a = 0;
  int b = 1;
  fill(dp[0],dp[0]+5000,false);
  fill(dp[1],dp[1]+5000,false);
  dp[a][0] = true;
  for(auto&& u : adj[v]){
    if(u==p) continue;
    fill(dp[b],dp[b]+5000,false);
    for(int i=5000;i>=0;i--){
      if(i-x[u]>=0){
        dp[b][i] |= dp[a][i-x[u]];
      }
      if(i-m[u]>=0){
        dp[b][i] |= dp[a][i-m[u]];
      }
    }
    swap(a,b);
  }
  int temp = x[v];
  for(;temp>=0;temp--)
    if(dp[a][temp]) break;

  if(temp<=0&&(!dp[a][0])){
    ans = false;
    return;
  }
  int _sum = 0;
  for(auto&& u : adj[v]){
    if(u==p) continue;
    _sum += m[u];
    _sum += x[u];
  }
  m[v] = _sum - temp;
  // cout << v << "," << m[v] << endl;
  return;
}

int main(){
  cin >> N ;
  REP(i,1,N){
    int a;
    cin >> a ;
    a--;
    par[i] = a;
    adj[i].pb(a);
    adj[a].pb(i);
  }
  rep(i,N) cin >> x[i] ;
  dfs(0,-1);
  if(!ans) cout << "IM";
  cout << "POSSIBLE" << endl;
  return 0;
}

Measured memory_bytes: 384
#include<bits/stdc++.h>
using namespace std;
int x,a,b;
int i,j;

int main( ){
	cin>>x>>a>>b;
	 i=abs(a-x);
	 j=abs(b-x);
	 if(i>j){
	 	cout<<"B"<<endl;
	 }
	 else{
	 	cout<<"A"<<endl;
	 }
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <cmath>
#include <tuple>
#include <iomanip>
#include <numeric>
#include <unordered_map>
#include <sstream>   
#include<limits.h>
using namespace std;
#define i64  long long
#define lint long long
 
#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807
#define MOD 1000000007

#define int long long

int N_global;


void solve()
{
  int D;
  cin>>D;
  
  if(D == 22)
  {
    cout<<"Christmas Eve Eve Eve"<<endl;
  }
  else if(D == 23)
  {
    cout<<"Christmas Eve Eve"<<endl;
  }
  else if(D == 24)
  {
    cout<<"Christmas Eve"<<endl;
  }
  else if(D == 25)
  {
    cout<<"Christmas"<<endl;
  }
  
}
 
int32_t main(int32_t argc, const char *argv[])
{
 
  solve();
 
  return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <fstream>

using namespace std;
#define SZ(x) (int)(x).size()
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REPR(i,n) for(int i=(n)-1;i>=0;i--)
#define ALL(s) (s).begin(), (s).end()
#define so(V) sort(ALL(V))
#define rev(V) reverse(ALL(V))
#define uni(v) v.erase( unique(ALL(v)) , v.end());

typedef long long unsigned int llu;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<bool> vb;
const double EPS = 1e-9;
const int MOD = 1e9 + 7;
const int INF = (1 << 29);
const ll LINF = 1e18;
const double PI = acos(-1);

template<typename T>vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T, typename... Ts>auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); }
template<typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value != 0>::type
fill_v(U & u, const V... v) { u = U(v...); }
template<typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value == 0>::type
fill_v(U & u, const V... v) { for (auto& e : u) fill_v<T>(e, v...); }
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T& a, const 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;
	vi W(N);
	REP(i, N) {
		cin >> W[i];
	}
	vi right(N), left(N);
	int sum = 0;
	REP(i, N) {
		sum += W[i];
		left[i] = sum;
	}
	sum = 0;
	REPR(i, N) {
		sum += W[i];
		right[i] = sum;
	}
	int ans = INF;
	REP(i, N-1) {
		chmin(ans, abs(right[i+1] - left[i]));
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 256
/**    epsileer 18 August 2019  17:41:02  **/    
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<LL,LL> PII;
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define TEST LL t = 1; while(t--) solve()
 
vector<LL> gp[300000], sz(300000, 1), pre(300000), pos(300000);
LL timer = 0;
void dfs(LL u, LL p){
    timer++;
    pos[u] = timer;
    for(auto v: gp[u]){
        if(v != p){
            dfs(v, u);
            sz[u] += sz[v];
        }
    }
}
 
void solve(){
    LL n,q; cin>>n>>q;
    for(LL i=0;i<n-1;i++){
        LL x,y; cin>>x>>y;
        gp[x].PB(y);
        gp[y].PB(x);
    }
    dfs(1, 1);
    for(LL i=0;i<q;i++){
        LL x,y; cin>>x>>y;
        pre[pos[x]] += y;
        pre[pos[x]+sz[x]] -= y;
    }
    for(LL i=1;i<=n;i++)
        pre[i] += pre[i-1];
    for(LL i=1;i<=n;i++)
        cout<<pre[pos[i]]<<" ";
}
 
int main(){
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL); TEST;
return 0;
}

Measured memory_bytes: 35200
#include<bits/stdc++.h>
#define V (100*100+2)
#define INF 1e9
#define Out 1
#define In 0
using namespace std;
typedef pair<int,int> P;
string mp[101];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int h,w;
struct edge{
  int to,cap,rev;
  edge(){}
  edge(int a,int b,int c){to=a,cap=b,rev=c;}
};
vector<edge> G[2*V];
bool used[2*V];

void add_edge(int from,int to,int cap){
  G[from].push_back(edge(to,cap,G[to].size()));
  G[to].push_back(edge(from,0,G[from].size()-1));
}

int dfs(int v,int t,int f){
  if(v==t)return f;
  used[v]=true;
  for(int i=0;i<G[v].size();i++){
    edge &e=G[v][i];
    if(!used[e.to]&&e.cap>0){
      int d=dfs(e.to,t,min(f,e.cap));
      if(d>0){
	e.cap-=d;
	G[e.to][e.rev].cap+=d;
	return d;
      }
    }
  }
  return 0;
}

int max_flow(int S,int T){
  int flow=0;
  while(1){
    memset(used,0,sizeof(used));
    int f=dfs(S,T,INF);
    if(f==0)return flow;
    flow+=f;
  }
}


//flg==0 -> In
//flg==1 -> Out
int Pos(int x,int y,int flg){return y*w+x+(flg? w*h:0);}

int solve(){
  int S=2*V-2,T=2*V-1;
  for(int i=0;i<h;i++)
    for(int j=0;j<w;j++){
      if(i==0||i==h-1||j==0||j==w-1){
	if(mp[i][j]=='X')return -1;
	add_edge(Pos(j,i,Out),T,1);
      }
      if(mp[i][j]=='X') add_edge(S,Pos(j,i,Out),INF);
      add_edge(Pos(j,i,In),Pos(j,i,Out),1);

      for(int k=0;k<4;k++){
	int nx=j+dx[k],ny=i+dy[k];
	if(nx<0||ny<0||nx>=w||ny>=h)continue;
	add_edge(Pos(j,i,Out),Pos(nx,ny,In),INF);
      }      
    }
  return max_flow(S,T);
}

int main(){
  cin>>h>>w;
  for(int i=0;i<h;i++)cin>>mp[i];
  cout<<solve()<<endl;  
}




Measured memory_bytes: 2816
using System;

	public class Class1
	{
		public static void Main()
		{   int h=0;
			int w=0;
			int k=0;
			while(true)
			{
				string dataSet=Console.ReadLine();
				string[] data=dataSet.Split();
				 h=int.Parse(data[0]);
				 w=int.Parse(data[1]);
				
				 if(h==0&&w==0)
				 {
				 	break;
				 }
				for(int i=0;i<h;i++)
				{   if(i==0||i==(h-1))
					{
						 k=1;
					}
					
					
					for(int j=0;j<w;j++)
					{
						if(k==1||j==0||j==(w-1))
						{
							Console.Write("#");						
							
						}
						else
						{
							Console.Write(".");
						}
						
					}
					k=0;
					Console.Write("\r\n");
						
				}
				Console.Write("\r\n");
				
			}
				
		}
	}

Measured memory_bytes: 8780
#include <iostream>
#include <iomanip>
#include <utility>
#include <cmath>
#include <random>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <algorithm>
using namespace std;

#define rep(i,n) for(int i = 0; i<n; ++i)
#define REP(i,n) for(int i = 1; i<=n; ++i)
#define all(x) begin(x),end(x)
typedef long long ll;
typedef pair<int,int> ip;
typedef pair<ll,ll> lp;
const int inf = 1001001000;
const ll INF = 1LL<<60;
const int MOD = (int)1e9 + 7;

int main(){
	int x;
	cin>>x;
	ll ans = 0, e1 = 0,e2  = 0;
	e1 += x/500;
	ans += e1*1000;
	x %= 500;
	e2 += x/5;
	ans += e2*5;
	x /= 50;
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>

using namespace std;

main(){

int n,number,S[13],H[13],C[13],D[13];
char input;

cin >> n;

for(int i=0;i<13;i++){
 S[i]=0; 
 H[i]=0;
 C[i]=0;
 D[i]=0;
}

for(int i=0;i<n;i++){
 cin >> input >> number;
 if(input == 'S'){
  S[number-1] = 1; 
 }else if(input == 'H'){
  H[number-1] = 1;
 }else if(input == 'C'){
  C[number-1] = 1;
 }else if(input == 'D'){
  D[number-1] = 1;
 }
}

for(int i=0;i<13;i++){
 if(S[i] == 0){
  cout << "S " << i+1 <<endl; 
 }
}
for(int i=0;i<13;i++){
 if(H[i] == 0){
  cout << "H " << i+1 <<endl; 
 }
}
for(int i=0;i<13;i++){
 if(C[i] == 0){
  cout << "C " << i+1 <<endl; 
 }
}
for(int i=0;i<13;i++){
 if(D[i] == 0){
  cout << "D " << i+1 <<endl; 
 }
}

}

Measured memory_bytes: 1160
#include<bits/stdc++.h>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)
#define R(i,a,n) for(int i=a;i<n;i++)
typedef complex<double> P;
struct seg{P p1,p2;};
double eps=1e-10;
bool eq(double a,double b){return (b-a<eps&&a-b<eps);}
double dot(P a,P b){return real(b*conj(a));}
double cross(P a,P b){return imag(b*conj(a));}
int ccw(P a,P b,P c){
  b-=a,c-=a,a=c*conj(b);
  if(a.imag()>eps)return 1;
  if(a.imag()<-eps)return -1;
  if(a.real()<-eps)return 2;
  if(abs(b)+eps<abs(c))return -2;
  return 0;
}
double dist(P a,P b,P c){
  if(dot(b-a,c-a)<0)return abs(c-a);
  if(dot(a-b,c-b)<0)return abs(c-b);
  return abs(cross(b-a,c-a))/abs(b-a);
}
bool isintersect(P a,P b,P c,P d){
  return ((ccw(a,b,c)*ccw(a,b,d)<=0)&&(ccw(c,d,a)*ccw(c,d,b)<=0));
}
double dist(P a,P b,P c,P d){
  if(isintersect(a,b,c,d))return 0;
  double ab=min(dist(a,b,c),dist(a,b,d));
  double cd=min(dist(c,d,a),dist(c,d,b));
  return min(ab,cd);
}
int main(){
  double a,b,c,d;
  int n,ve,al;
  while(cin>>n>>ve>>al,n){
    P p[n][5];
    seg D[n][5];
    r(i,n){
      scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
      r(j,5)p[i][j]=P(a-d*sin(M_PI/180*(72*j+c)),b+d*cos(M_PI/180*(72*j+c)));
      D[i][0].p1=p[i][0];D[i][0].p2=p[i][2];
      D[i][1].p1=p[i][1];D[i][1].p2=p[i][3];
      D[i][2].p1=p[i][2];D[i][2].p2=p[i][4];
      D[i][3].p1=p[i][3];D[i][3].p2=p[i][0];
      D[i][4].p1=p[i][4];D[i][4].p2=p[i][1];
    }
    double w[n][n];
    r(i,n)r(j,n)w[i][j]=i==j?0:1000000000;
    r(i,n)R(j,i+1,n){
      double t=1000000000;
      r(k,5)r(l,5)t=min(t,dist(D[i][k].p1,D[i][k].p2,D[j][l].p1,D[j][l].p2));
      w[i][j]=w[j][i]=t;
    }
    r(k,n)r(i,n)r(j,n)w[i][j]=min(w[i][j],w[i][k]+w[k][j]);
    printf("%.11f\n",w[ve-1][al-1]);
  }
}

Measured memory_bytes: 3668
//g++ -std=c++14 test.cpp -o test.out
//問題URL
//https://atcoder.jp/contests/abc154/tasks/abc154_e

#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <iomanip>
#include <vector>
#include <queue>
#include <map> 
#include <unordered_map> 
#include <utility>
#include <limits.h>
#include <bitset>
#include <set>
using namespace std;
 
#define LL long long int
const LL INF = (1LL<<60);
const int INF_INT = 2147483647-1e6-1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

LL N,M,X;
LL C[12];
LL A[12][12];

LL ans = INF;

void solve(){
	for(int bit=0;bit<(1<<(N));bit++){
		//j個目のアルゴリズムの理解度をunderstanding[j]とおく
		vector<LL> understanding(M,0);
		LL money = 0;
		for(int i=0;i<N;i++){
			if((bit>>i)&1){
				money += C[i];
				for(int j=0;j<M;j++){
					understanding[j] += A[i][j];
				}
			}
		}
		//どれか1つでもXに達していないならダメ
		bool flag = true;
		for(int j=0;j<M;j++){
			if(understanding[j]<X){
				flag = false;
				break;
			}
		}
		if(flag) ans = min(ans,money);
	}
	if(ans==INF) ans = -1;
}

int main(){
    cin >> N >> M >> X;
    for(int i=0;i<N;i++){
    	cin >> C[i];
    	for(int j=0;j<M;j++) cin >> A[i][j];
    }

    solve();

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

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

#define ld long double

using namespace std;

vector <int> g[200500];

int add[200500];

int sum[200500];

void dfs(int v, int pr, int cur)
{
    cur += add[v];
    sum[v] += cur;
    for(int j = 0; j < g[v].size(); j++)
    {
        int to = g[v][j];
        if(to != pr)
        {
            dfs(to, v, cur);
        }
    }
}

int main()
{
    ios_base :: sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,q,x,y,p;
    cin >> n >> q;
    for(int i = 0; i < n - 1; i++)
    {
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    while(q--)
    {
        cin >> p >> x;
        add[p] += x;
    }
    dfs(1, 1, 0);
    for(int i = 1; i <= n; i++) cout << sum[i] << ' ';
}


Measured memory_bytes: 27264
import fractions
import operator
from functools import reduce
N = int(input())
a_list = list(map(int, input().split(' ')))

def lcm_list(numbers):
    return reduce(operator.mul, numbers) // reduce(fractions.gcd, numbers)

m = lcm_list(a_list) - 1

print(sum([m % a for a in a_list]))


Measured memory_bytes: 5560
#include <stdio.h>

int main(int argc, const char * argv[]) {
    int r, x, y, H, W;
    
    scanf("%d %d %d %d %d", &W, &H, &x, &y, &r);//入力する順番
    if (x >= r && y >= r && (H - r) >= y && (W - r) >= x) {
        printf("Yes\n");
    }else{
        printf("No\n");
    }
    
    return 0;
}



Measured memory_bytes: 2100
#include <bits/stdc++.h>
//#pragma GCC optimize "-O3"
typedef unsigned long long ull;
typedef unsigned int ui;
typedef unsigned short us;
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
const ll infll=(1ull<<63)-1,mod=1e9+7;
const int inf=(1ull<<31)-1;
int n,d;

int main()
{
	//freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
	ios_base::sync_with_stdio(false); cin.tie(0);cout.precision(18);
	cin>>n>>d;
	d=d<<1|1;
	cout<<ceil((ld)n/d);
	return 0;
}


Measured memory_bytes: 256