akhauriyash/Code-Regression huggingface dataset:
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


n,m,k=map(int,input().split())
uf=UnionFind(n)
block={}
for i in range(n):
    block[i]=[i]
for i in range(m):
    a,b=map(int,input().split())
    uf.union(a-1,b-1)
    block[a-1].append(b-1)
    block[b-1].append(a-1)
    
for i in range(k):
    a,b=map(int,input().split())
    block[a-1].append(b-1)
    block[b-1].append(a-1)
ans=[]
#print(block)
for i in range(n):
    res=uf.size(i)
 #   print(res)
    for value in block[i]:
        if uf.same(value,i):
            res-=1
    ans.append(res)
print(*ans)
    
    

Measured memory_bytes: 49084
(n,m,x),*t=[[*map(int,t.split())]for t in open(0)]
m=M=9**9
for i in range(1,2**n):c,*a=map(sum,zip(*[t[j]for j in range(n)if i>>j&1]));m=min(m,M*(min(a)<x)or-~c)
print(m%M-1)

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

#define ll long long
#define ff first
#define ss second
#define pb push_back
#define rep(i,a,b) for(i=a;i<b;i++)
#define pii pair<ll , ll>
#define all(x) x.begin(),x.end()
#define nl "\n"
#define ump unordered_map
#define mtv(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)

vector<string> vec_splitter(string s) {
    s += ',';
    vector<string> res;
    while(!s.empty()) {
        res.push_back(s.substr(0, s.find(',')));
        s = s.substr(s.find(',') + 1);
    }
    return res;
}
void debug_out( 
vector<string> __attribute__ ((unused)) args,
__attribute__ ((unused)) int idx, 
__attribute__ ((unused)) int LINE_NUM) { cout << endl; } 
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
    stringstream ss; ss << H;
    cout << args[idx] << "- " << ss.str() << ", ";
    debug_out(args, idx + 1, LINE_NUM, T...);
}

bool isvalid(ll k, ll n, vector<ll>&v,ll cur){
	ll i,cou = 0;
	if(cur == 0){
		return false;
	}
	rep(i,0,n){
		if(v[i] > cur){
		cou += (v[i]/cur - 1);
		if(v[i] % cur)cou++;}
	}
	return cou <= k ;
}


void doit(){
	ll n,k,i,x;
	cin >> n >> k;
	vector< ll >v(n);
	for(i = 0; i < n; i++)cin>>v[i];
	ll low = 0LL ,high = *max_element(all(v)),mid,ans = 1e18;
	while(low <= high){
		mid = low + (high - low)/2;
		if(isvalid(k,n,v,mid)){
			ans = mid;
			high = mid - 1;
		}
		else{
			low = mid + 1;
		}
	}
	//mtv(isvalid(k,n,v,292638192));
	cout << ans << nl;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input1.txt","r",stdin);
    freopen("output1.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t = 1;
    // cin >> t;
    for(ll i = 1; i <= t; i++){
        doit();
    }
}


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

# define ll long long
# define mod 1000000007
# define MAX 100005
# define PI 3.141592653589793238                                                                                  //shortcut keys

# define pb push_back
# define mp make_pair
# define all(x) x.begin(),x.end()
# define fr(i,a,b) for(int i=a;i<=b;i++)

# define decimal(k) fixed<<setprecision(k)
# define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);        //---------------------------------------------------------------
using namespace std;

int main()
{
	fast;
	int h,w,m;cin>>h>>w>>m;
	unordered_map<int,int> m1;
	unordered_map<int,int> m2;
	map<pair<int,int>,int> m3;
	fr(i,1,m){
		int x,y;cin>>x>>y;
		m1[x]++;m2[y]++;
		m3[{x,y}]++;
	}
	int x=0,y=0;
	vector<pair<int,int> > v1,v2;
	for(auto it=m1.begin();it!=m1.end();it++){
		if(it->second>x){
			x=it->second;
		}
	}
	for(auto it=m1.begin();it!=m1.end();it++){
		if(it->second==x){
			v1.pb({x,it->first});
		}
	}
	
	//cout<<x<<' ';
		for(auto it=m2.begin();it!=m2.end();it++){
		if(it->second>y){
			y=it->second;
		}
	}
	for(auto it=m2.begin();it!=m2.end();it++){
		if(it->second==y){
			v2.pb({y,it->first});
		}
	}
	//cout<<y<<' ';
	int n1=v1.size(),n2=v2.size();
	int ans=x+y-1;
	fr(i,0,n1-1){
		fr(j,0,n2-1){
			if(m3[{v1[i].second,v2[j].second}]==0){
				ans++;break;
			}
		}
		if(ans==x+y)break;
	}

		cout<<ans<<'\n';
		
	
	
	return 0;
}



Measured memory_bytes: 57328
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {

            int N = int.Parse(Console.ReadLine());
            var H = Array.ConvertAll(Console.ReadLine().Split(' '), long.Parse);
            int[] count = new int[N];

            Array.Reverse(H);

            count[0] = 0;
            
                for (int i = 1; i < N; i++)
                {
                if (H[i] >= H[i - 1])
                {
                    count[i] = count[i - 1] + 1;
                   // Console.WriteLine(H[i] + " " + count[i]);
                }
                else
                {
                    count[i] = 0;
                    //Console.WriteLine(H[i] + " "+count[i]);
                }
                
                }
                
                Console.WriteLine(count.Max());
        }
    }
}


Measured memory_bytes: 27996
n = int(input())
a = list(map(int, input().split()))

cnt = 0
ret = 0
for i in a:
    if i % 2 == 0:
        cnt += 1
        if i % 3 == 0 or i % 5 == 0:
            ret += 1
if cnt == ret:
    print('APPROVED')
else:
    print('DENIED')


Measured memory_bytes: 2940
#include <algorithm>
#include <array>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <queue>

using namespace std;

struct BoolName : numpunct<char> {
  string t, f;
  BoolName (string t = "Yes", string f = "No") : t(t), f(f) {}
  string do_truename() const {return t;}
  string do_falsename() const {return f;}
};

struct Initializer {
  Initializer() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(15) << boolalpha;
    cout.imbue(locale(cout.getloc(), new BoolName));
  }
} initializer;

template<typename T> istream& operator>>(istream &s, vector<T> &v) {
  for (T &t : v) s >> t;
  return s;
}

template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
  for (const T &t : v) s << t << endl;
  return s;
}

void set_bool_name(string t, string f) {
  cout.imbue(locale(cout.getloc(), new BoolName(t, f)));
}

int main() {
  int a, b, c;
  cin >> a >> b >> c;
  for (int i = 0; ; ++i) {
    if (a % 2 || b % 2 || c % 2) {
      cout << i << endl;
      return 0;
    }
    if (a == b && b == c) {
      cout << -1 << endl;
      return 0;
    }
    int aa = (b + c) / 2;
    int bb = (a + c) / 2;
    int cc = (a + b) / 2;
    a = aa;
    b = bb;
    c = cc;
  }
}



Measured memory_bytes: 256
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func readf(sc *bufio.Scanner) float64 {
	f, err := strconv.ParseFloat(sc.Text(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	d := make([]int, n)
	for i := range d {
		sc.Scan()
		d[i], _ = strconv.Atoi(sc.Text())
	}

	sort.Ints(d)

	half := len(d) / 2
	fmt.Println(d[half] - d[half-1])
}


Measured memory_bytes: 2048
// glory be to the sky
#include<cstdio>
#include<bits/stdc++.h> 
#define pb push_back
using namespace std;
	
typedef long long ll; 

int main(){

	ios::sync_with_stdio(false);
	cin.tie(0);

	int N, arr[105];
	ll ans = (ll) 1e9;
	cin >> N;
	for (int i = 0; i < N; i++) cin >> arr[i];

	for (int target = -100; target <= 100; target++) { 
		ll t = 0;
		for (int j = 0; j < N; j++) {
			t += ((target - arr[j]) * (target - arr[j]));
		}
		ans = min(ans ,t);
	}
	cout << ans << endl;
	// string input;
	// vector<char> output;
	// cin >> input;
	// for (int i = 0; i < input.length(); i++) {
	// 	if (input[i] == '0') output.pb('0');
	// 	else if (input[i] == '1') output.pb('1');
	// 	else if (output.size()) output.pop_back(); 
	// }
	// for (int i = 0; i < output.size(); i++) cout << output[i];
	return 0;	
}

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws InterruptedException {

        // 入力を取得
        Scanner sc = new Scanner(System.in);

        // 人数
        int N = sc.nextInt();

        // 合計数
        int count = 0;

        // 合計計算
        for (int i = 1; i <= N; i++) {
            // メニューの組み合わせ
            count += i;
        }

        // スキャナーを閉じる
        sc.close();

        // メニューの組み合わせを出力
        System.out.println(count);
    }
}


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

using namespace std;
using ll = long long;

int main() {
	auto f = [](ll a, ll b) {
		return a - 1 + (b - (a - 1) - 1) / 2 + 1;
	};
	
	ll n, a, b;
	cin >> n >> a >> b;


	if((b - a) % 2 == 0) {
		cout << (b - a) / 2 << endl;
	}
	else {
		cout << min(f(a, b), f(n - b + 1, n - a + 1)) << endl;
	}
}


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


int main() {
    ll n,sum=0,le=0;
    bool check=true;
    cin>>n;
    for(int i=0; i<n; i++){ll tmp; cin>>tmp; tmp%2!=0?check=false:check=check;}
    if(!check) cout << "first";
    else cout << "second";
    return 0;
}

/*
3
100000
30000
20000
*/

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    string s;
    getline(cin,s);
    if(*s.rbegin()=='s') s+="es";
    else s+='s';
    cout<<s<<endl;
    return 0;
}

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

using namespace std;

vector<long long> enum_divisors(long long N)
{
    vector<long long> res(N);
    for (long long i = 1; i <= N; ++i) 
    {
        for (long long j = 1; j*i <= N; j++)
        {
            res.at(i*j-1)++;
        }
    }
    return res;
}

int main()
{
    int N;
    cin >> N;
    long long answer = 0;
    const auto & res = enum_divisors(N);
    for (int i = 1; i < N+1; i++)
    {
        answer += res.at(i-1)*(i);
    }
    cout << answer;
}


Measured memory_bytes: 81400
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define rep(i,m,n) for(int i=m;i<n;i++)
ll mod=1e9+7;

int main(){
  ll n,m;
  cin>>n>>m;
  ll a=1;
  rep(i,0,n){
    a*=m-i;
    a%=mod;
  }
  ll b[n+1];
  b[1]=m-n;
  b[2]=(m-n+1+(m-n)*(m-n)%mod)%mod;
  rep(i,3,n+1){
    b[i]=((i-1)*b[i-2]%mod+(i-1+m-n)*b[i-1]%mod)%mod;
  }
  //cout<<a<<endl;
  //cout<<b[n]<<endl;
  cout<<a*b[n]%mod<<endl;
}

Measured memory_bytes: 7496
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define ull unsigned long long

int main(void) {
  ull n, b, r;
  cin >> n>> b >> r;
  
  ull quotient = n / (b + r);
  ull remainder = n % (b + r);
  ull ans = b * quotient + min(remainder, b);
  
  printf("%lld\n", ans);
  
  return 0;
  
  
  }
  
  
  


Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main(){
  int N,M,X;

  cin >> N >> M >> X;

  int A[M];
  int R[N];
  int i,j;

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

  int count1=0,count2=0;

  for(i= X;i < N+1;i++){
    for(j = 0;j < M;j++){
      if(i == A[j]) count1++;
    }
  }
  for(i = X;i > 0;i--){
    for(j = 0;j < M;j++){
      if(i ==A[j]) count2++;
    }
  }

  if(count1 >= count2) cout << count2 << endl;
  else cout << count1 << endl;

  return 0 ;
}


Measured memory_bytes: 256
import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] x = new int[5];
        for (int i = 0; i < x.length; i++) {
            x[i] = sc.nextInt();
        }

        for (int i = 0; i < x.length; i++) {
            if (x[i] == 0) {
                System.out.println(i + 1);
                break;
            }
        }
    }
}


Measured memory_bytes: 35552
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i,n) for(int i = 0; i < ((int)(n)); i++)   // 0-indexed昇順

int main() {
    int N; cin >> N;
    vector<int> P(N);
    rep(i,N) cin >> P.at(i);
    vector<int> Q(N);
    Q=P;
    sort(begin(P), end(P));
    int cnt = 0;
    rep(i,N) if (P[i] != Q[i]) cnt++;
    if (cnt == 0 || cnt == 2) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long  ll ;
typedef double db;
const double pi = 3.141592654;
#define pb         push_back
#define forab(i,a,b)       for(int i=(a);i<=(b);i++)
#define CIN	ios_base::sync_with_stdio(0); cin.tie(0)
#define pcase(z,x)     printf("Case %ld: %lld\n",z,x)
#define nw				"\n"


int main(void)
{
	CIN;
	ll tc,l,k,sum=0,x=0,y,z=0,m=1,n=0,ans=0,cnt=0;
	string s="",t="" ;
	cin>>n;
	tc=n;
	char c;
	while(tc--)cin>>c,s+=c;
	while(n--)cin>>c,t+=c;
	x=stoi(s),y=stoi(t);
	if(y<x){
		string tmp=t;
		t=s;
		s=tmp;
	}
	while(next_permutation(s.begin(),s.end()))
	{
		if(s==t)return cout<<cnt+1,0;
		cnt++;
	}
	cout<<0<<nw;

}


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

#define boost ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)


using namespace std;
using LL = long long;

constexpr int MAXN = 2e5 + 5;

template <typename T>
inline void read(T &x) {
    int f = 1;
    x = 0;
    char ch = getchar();
    while (!isdigit(ch)) f = (ch == '-') ? -1 : 1, ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
    x *= f;
}

int main() {
    // freopen("test.in", "r", stdin);
    // freopen("test.out", "w", stdout);
    boost;
    int k, a, b;
    cin >> k >> a >> b;
    for (int i = a; i <= b; i++) {
        if (i % k == 0) {
            cout << "OK" << endl;
            return 0;
        }
    }
    cout << "NG" << endl;
    return 0;
}

Measured memory_bytes: 3652
#include <iostream>
#include <algorithm>

using namespace std;

const int MAX_N = 1e5 + 10;

int n, A[MAX_N], L[MAX_N];

int LIS() {
	L[0] = A[0];
	int length = 1;

	for (int i = 1; i < n; ++i)
	{
		if (L[length-1] < A[i]) L[length++] = A[i];
		else *lower_bound(L, L + length, A[i]) = A[i];
	}

	return length;
}

int main(int argc, char const *argv[])
{
	cin >> n;
	for (int i = 0; i < n; ++i) cin >> A[i];

	cout << LIS() << endl;
	return 0;
}


Measured memory_bytes: 3860
#include <bits/stdc++.h> 
using namespace std; 
#define io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define rep(i,n) for (int i = 0; i < (n); ++i)

int main(void) {
    io;
    char a; cin >> a;
    if (a != 'z') a = a + 1;
    cout << a;
    return 0;
}



Measured memory_bytes: 3660
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pi M_PI
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;

void Main() {
  ll h,w,n;
  cin >> h >> w >> n;
  P a[n][2];
  vector<P> v;
  rep(i,n){
    vector<P> a;
    rep(j,2) {
      ll x,y;
      cin >> x >> y;
      if(x==0) a.pb(P(x+y,i));
      else if(y==w) a.pb(P(x+y,i));
      else if(x==h) a.pb(P(h+w+w-y,i));
      else if(y==0) a.pb(P(h+w+w+h-x,i));
    }
    if(a.size()==2) rep(j,2) v.pb(a[j]);
  }
  sort(all(v));
  n=v.size();
  stack<int> s;
  rep(i,n) {
    if(!s.empty()&&s.top()==v[i].S) {
      s.pop();
      continue;
    }
    s.push(v[i].S);
  }
  if(s.size()) cout << "NO" << endl;
  else cout << "YES" << endl;
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}

Measured memory_bytes: 3956
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <bitset>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef long long LL;

typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

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

//constant
//--------------------------------------------
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
const LL MOD = 1e9+7; 
//vector <vector<int>> x(20, vector<int> (20)), y(20,vector<int> (20));
/*int counter_1 (int x) {
    if(x == 0) return 0;
    return counter_1(x >> 1) + (x & 1);
}*/

//int Counter(int x)
/*int factorial(int n) {
    if (n > 0) {
        return n * factorial(n - 1);
    } else {
        return 1;
    }
}*/
int main(){
    LL n;
    cin >> n;
    VL p(n);
    bool ans=true;
    REP(i, n){
        cin >> p[i];
    }
    sort(p.begin(), p.end());
    REP(i, n-1){
       if(p[i]==p[i+1]) ans=false;
    }
    if(ans==false) cout << "NO" << endl;
    else cout << "YES"  << endl;
}

    



Measured memory_bytes: 1792
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define show(x) cout << #x << " = " << x << endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
// O(n^2m)
// bipartite graph O(\sqrt{n}m)
// capacityが同じ O(min{m^1/2,n^2/3}*m)
#include <cstring>  //for memset
#define MAX_V 100000
#define INF 10000000
struct edge {
    int to, cap, rev;
};
vector<edge> G[MAX_V];
int level[MAX_V];  // sからの距離
int iter[MAX_V];   //どこまで調べ終わったか

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

void bfs(int s) {
    memset(level, -1, sizeof(level));
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while (!que.empty()) {
        int v = que.front();
        que.pop();
        for (int i = 0; i < G[v].size(); ++i) {
            edge &e = G[v][i];
            if (e.cap > 0 && level[e.to] < 0) {
                level[e.to] = level[v] + 1;
                que.push(e.to);
            }
        }
    }
}

int dfs(int v, int t, int f) {
    if (v == t) return f;
    for (int &i = iter[v]; i < G[v].size(); ++i) {
        edge &e = G[v][i];
        if (e.cap > 0 && level[v] < level[e.to]) {
            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;
    for (;;) {
        bfs(s);
        if (level[t] < 0) return flow;
        memset(iter, 0, sizeof(iter));
        int f;
        while ((f = dfs(s, t, INF)) > 0) {
            flow += f;
        }
    }
}
int main(){
    ll n,y;
    cin >> n >> y;
    y/=1000;
    for(int i = 0; i <= n; i++){
        for(int j = 0; i+j <= n; j++){
            if(10*i+5*j+1*(n-i-j)==y){
                cout << i << " " << j  << " "<< n-i-j << endl;
                return 0;
            }
        }
    }
    cout << "-1 -1 -1" << endl;
}

Measured memory_bytes: 2560
n = int(input())
tree = [[] for _ in range(n)]
for i in range(n-1):
  v, w = map(int, input().split())
  if v > w:
    v, w = w, v
  tree[w-1].append(v-1)
  
ans = 0
count = 0
for i in range(n):
  count += i+1
  for v in tree[i]:
    count -= v+1 
  ans += count
  
print(ans)

Measured memory_bytes: 35624
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ull B = 100000007;
ll mod = 1000000007;
struct P {
    int to;
    int t;
    int c;
};
struct L {
    int id;
    int s;
    ll dis;
    bool operator<(const L& b) const {
        return b.dis < dis;
    }
};
ll dp[55][2600];

int main(){
    memset(dp, 0x3f3f3f3f, sizeof(dp));
    int n, m, s;
    cin >> n >> m >> s;
    vector<vector<P> > G(n + 1);
    vector<int> C(n + 1);
    vector<int> D(n + 1);
    for(int i = 1; i <= m; ++i) {
        int from, to, time, cost;
        cin >> from >> to >> cost >> time;
        G[from].push_back({to, time, cost});
        G[to].push_back({from, time, cost});
    }
    for(int i = 1; i <= n; ++i) {
        cin >> C[i] >> D[i];
    }
    dp[1][min(2505, s)] = 0;
    priority_queue<L> que;
    que.push({1, min(2505, s), 0});
    while(!que.empty()) {
        L cur = que.top();
        que.pop();
        for (auto v : G[cur.id]) {
            int n_id = v.to;
            int n_s = cur.s - v.c;
            if (n_s >= 0) {
                //cout <<dp[n_id][n_s] << " " << dp[cur.id][cur.s] + cur.dis << endl;
                if (dp[n_id][n_s] > dp[cur.id][cur.s] + v.t) {
                    dp[n_id][n_s] = dp[cur.id][cur.s] + v.t;
                    que.push({n_id, n_s, dp[n_id][n_s]});
                }
            }
        }
        int need = min(2505, cur.s + C[cur.id]);
        if (dp[cur.id][need] > dp[cur.id][cur.s] + D[cur.id]) {
            dp[cur.id][need] = dp[cur.id][cur.s] + D[cur.id];
            que.push({cur.id, need, dp[cur.id][need]});
        }
    }

    for(int i = 2; i <= n; ++i) {
        ll minm = INT64_MAX;
        for(int j = 0; j <= 2505; ++j) {
            minm = min(minm, dp[i][j]);
        }
        cout << minm << endl;
    }

    return 0;
}

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

int main(void){
	int i,*a,n;
	
	scanf("%d",&n);
	a = (int*)malloc(sizeof(int)*n);
	for(i=0; i<n; i++)
		scanf("%d",&a[i]);
	for(i=n-1; i>=0; i--){
		printf("%d",a[i]);
		if(i!=0)printf(" ");
		else printf("\n");
	}		
	
	return 0;
}

Measured memory_bytes: 600
X , Y = map(int,input().split())
if X == Y == 1:
    print("1000000")
    exit()
point = 0
if X == 1:
    point += 300000
elif X == 2:
    point += 200000
elif X == 3:
    point += 100000

if Y == 1:
    point += 300000
elif Y == 2:
    point += 200000
elif Y == 3:
    point += 100000
print(point)

Measured memory_bytes: 3316
def abc104_b():
    s = input()
    num = s.find("C")
    if s[0] == "A" and s[2:-1].count("C") == 1 and (s[1:num] + s[num + 1:]) == (s[1:num] + s[num + 1:]).lower():
        print("AC")
    else:
        print("WA")


abc104_b()

Measured memory_bytes: 3060
n = int(input())
ans = 0
cnt = 0
num = 0
for i in range(1, n + 1):
    if i == 1:
        num = 1
        continue
    v = i
    while True:
        if v % 2 == 0:
            v = v // 2
            cnt += 1
        else:
            break
    if ans < cnt:
        ans = cnt
        num = i
    cnt = 0
print(num)


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

import sys
import itertools
import math

def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    c = a % b
    return gcd(b, c)

def main():
    N, M = map(int, input().split())
    S = input()
    T = input()

    if N < M:
        t = M
        M = N
        N = t
        q = S
        S = T
        T = q

    d = gcd(N,M)
    c = N * M // d

    ret = -1
    slen = c
    nstep = slen // N
    mstep = slen // M

    dd = gcd(nstep, mstep)
    cc = nstep * mstep // dd

    good = True
    for k in range(0, slen, cc):
        u = (k // nstep) % N
        j = (k // mstep) % M
        if S[u] != T[j]:
            good = False
            break
    if good:
        ret = slen

    # 出力
    print("{}".format(ret))


if __name__ == '__main__':
    main()



Measured memory_bytes: 3444
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <math.h>
#include <tuple>

#include <set>
#include <unordered_map>
#include <stack>
#include <map>

using namespace std;

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define all(x) (x).begin(),(x).end()
using ll = long long;
template<class T>bool chmaxj(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 (b < a) { a = b; return 1; } return 0; }
void YesNo(bool y) { cout << (y ? "Yes" : "No") << endl; } //true==yes


#define INF 21474836

//////////////////////////////////////////////////////////////



int main() {

	int N;
	std::unordered_map<std::string, int> mp;    //  文字列 → 整数 のハッシュ連想配列	
	
	cin >> N;

	int ans = 0;
	rep(i, N) {
		string S;
		cin >> S;
		auto itr = mp.find(S);        // "xyz" が設定されているか？
		if (itr != mp.end()) {
		}
		else {
			mp[S] = 1;
			ans++;
			
		}
	}

	cout << ans << endl;
}


Measured memory_bytes: 17060
def special_trains(N: int, CSF: list) -> list:
    res = []
    for i in range(N):
        t = 0
        for c, s, f in CSF[i:]:
            if t < s:
                t = s
            if t % f != 0:
                t = (t + f) - (t % f)
            t = t + c
        res.append(t)
    return res


if __name__ == "__main__":
    N = int(input())
    CSF = [tuple(map(int, input().split())) for _ in range(N-1)]
    ans = special_trains(N, CSF)
    for a in ans:
        print(a)


Measured memory_bytes: 3316
#include <bits/stdc++.h>
using namespace std;
 
using ll=long long;
 
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
const ll MOD = 1e9 + 7;

bool check(int k, vector<bool>& ds) {
    while (k) {
        if (ds[k%10]) return false;
        k /= 10; 
    }
    return true;
}

int main() {
    int n, k;
    cin >> n >> k;
    vector<bool> ds(10, false);
    rep(i, k) {
        int d; cin >> d;
        ds[d] = true;
    }  
    int l = n;
    while (!check(l, ds)) ++l;
    cout << l << endl; 
    return 0;
}

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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = (n) - 1; i >= 0; i--)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x, n) bitset<n>(x)
#define PI 3.14159265358979323846

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

//-------------------------------------------------


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

    int a, b;
	cin >> a >> b;
	cout << max(a + b, max(a - b, a * b)) << endl;

	return 0;
}



Measured memory_bytes: 256
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import deque
import pprint

inf = 10**18
mod = 10**9+7

n,m = map(int, input().split())
s = input()
t = input()
f=fractions.gcd(n,m)           #最大公約数
f2=n*m//f                          #最小公倍数


x = f2//n
y = f2//m

for i in range(n):
    if (x*i)%y==0:
        if s[i] != t[x*i//y]:
            print(-1)
            sys.exit()
print(f2)

Measured memory_bytes: 65516
n = int(input())
cost = []
for i in range(n):
    a,b,c = map(int , input().split())
    cost.append([a,b,c])
dp = [[0,0,0] for _ in range(n+1)]
for i in range(1,n+1):
    dp[i][0] = max( dp[i][0] , dp[i-1][1] + cost[i-1][0])
    dp[i][0] = max( dp[i][0] , dp[i-1][2] + cost[i-1][0])
    dp[i][1] = max(dp[i][1] , dp[i-1][0]  + cost[i-1][1])
    dp[i][1] = max(dp[i][1] , dp[i-1][2]  + cost[i-1][1])
    dp[i][2] = max(dp[i][2] , dp[i-1][0]  + cost[i-1][2])
    dp[i][2] = max(dp[i][2] , dp[i-1][1]  + cost[i-1][2])
print( max(dp[n]))

Measured memory_bytes: 42552
#include <iostream>
#include <algorithm>
using namespace std;
int main(void){
    long a,b,k;
    cin >> a >> b >> k;
    
    for(int i = 0;i < k;i++){
        if(i % 2 == 0){
            b += a / 2;
            a /= 2;
        }else{
            a += b / 2;
            b /= 2;
        }
    }
    cout << a << " " << b << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <climits>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <deque>
#define INF_INT (INT_MAX / 2)
#define INF_LONG (LONG_MAX / 2)
//#define DEBUG true
#define DEBUG false
using namespace std;

const int MAX = 100001;
const int MOD = 1000000007;

typedef long long ll;
typedef pair< int, int > pii;
typedef pair< ll, ll > pll;
typedef pair< ll, int > pli;
typedef pair< int, ll > pil;

int ceil(int x, int y){
  return (x % y == 0) ? x / y : x / y + 1;
}

int gcd(int x, int y){
  return y ? gcd(y, x % y) : x;
}

int lcm(int x, int y){
  return x / gcd(x, y) * y;
}

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

  ll w, h;
  cin >> w >> h;
  ll p[w], q[h];
  for(int i = 0; i < w; i++) cin >> p[i];
  for(int i = 0; i < h; i++) cin >> q[i];

  pll r[2 * MAX];
  for(int i = 0; i < w; i++){
    r[i] = make_pair(p[i], true);
  }
  for(int i = 0; i < h; i++){
    r[i + w] = make_pair(q[i], false);
  }
  sort(r, r + w + h);

  ll x = 0, y = 0;
  ll sum = 0;
  for(int i = 0; i < w + h; i++){
    if(r[i].second){
      sum += (h + 1 - y) * r[i].first;
      ++x;
    }
    else{
      sum += (w + 1 - x) * r[i].first;
      ++y;
    }
  }

  cout << sum << endl;

  return 0;
}


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

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

  int ans = 1e9;
  for(int i=0; i<max(X, Y)*2; i++){
    int ab = i*2;
    int a = max(0, X-i);
    int b = max(0, Y-i);
    ans = min(a*A+b*B+ab*C, ans);
  }

  cout << ans << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main() {
	int n, ans = 1000;
	cin >> n;

	vector<int> w(n);
	for(int i=0;i<n;i++)
		cin >> w.at(i);

	for(int i=1;i<n;i++){
		int l = 0, r = 0;
		for(int j=0;j<n;j++){
			if(j<i) l += w.at(j);
			else r += w.at(j);
		}
		if(abs(l-r)<ans)
			ans = abs(l-r);
	}
	cout << ans << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<cmath>
#include<string>
#include<cstdlib>
#include<stdio.h>
#include<cctype>
#include<algorithm>
#include<string.h>


using namespace std;

int main()
{
	int a,b;
	while(cin>>a>>b)
	{
		if(a>b)
			cout<<"a > b"<<endl;
		if(a<b)
			cout<<"a < b"<<endl;
		if(a==b)
			cout<<"a == b"<<endl;

	
	
	
	}




}

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

int main() {
  int N;
  cin >> N;
  int A;
  bool b=1;
  for(int i=0; i<N; i++){
    cin >> A;
    if(A % 2){
    }else{
      if(A%3!=0 && A%5!=0){
        b=0;
        break;
      }
    }
  }
  if(b){
      cout << "APPROVED" <<endl;
    }else{
      cout << "DENIED" <<endl;
    }
  
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<cassert>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1000005;
int n;
char str[N],s[N];
int cnt[4];
int id[4];
int pre[N],nxt[N];
void del(int i)
{
	pre[nxt[i]]=pre[i];
	nxt[pre[i]]=nxt[i];
	return;
}
int main()
{
	scanf("%s",str+1);
	for(size_t i=1;i<=strlen(str+1);i++)
		if(str[i]!=str[i-1]) s[++n]=str[i];
	for(int i=1;i<=n;i++)
		cnt[s[i]-'A'+1]++;
	id[1]=1,id[2]=2,id[3]=3;
	sort(id+1,id+3+1,[=](const int &x,const int &y){return cnt[x]<cnt[y];});
	sort(cnt+1,cnt+3+1);
	for(int i=1;i<=n;i++)
		nxt[i]=i+1,pre[i]=i-1;
	nxt[0]=1,pre[n+1]=n;
	int head=0,tail=n+1;
	for(int i=head;i!=tail&&cnt[2]<cnt[3];i=nxt[i])
		if(i&&nxt[i]&&s[i]==id[3]+'A'-1&&s[pre[i]]!=s[nxt[i]]) cnt[3]--,del(i);
	if(cnt[2]<cnt[3])
	{
		for(int i=head;i!=tail&&cnt[2]<cnt[3];i=nxt[i])
			if(i&&pre[i]&&nxt[i]&&s[i]==id[3]+'A'-1&&s[pre[i]]==s[nxt[i]]&&s[pre[i]]==id[1]+'A'-1)
			{
				cnt[1]--,cnt[3]--;
				del(pre[i]),del(i);
			}
	}
	if(cnt[2]!=cnt[3]) return 0;
	if(cnt[1]<cnt[2])
	{
		for(int i=head;i!=tail&&cnt[1]<cnt[2];i=nxt[i])
		{
			if(i&&pre[i]&&s[pre[i]]!=id[1]+'A'-1&&s[i]!=id[1]+'A'-1)
			{
				if(s[pre[pre[i]]]==id[1]+'A'-1&&s[nxt[i]]==id[1]+'A'-1) continue;
				cnt[2]--,cnt[3]--;
				del(pre[i]),del(i);
			}
		}
	}
	if(cnt[1]!=cnt[2]||cnt[2]!=cnt[3]) return 0;
	for(int i=head;i!=tail;i=nxt[i])
		if(i) printf("%c",s[i]);
	return 0;
}

Measured memory_bytes: 13512
import java.util.*;
public class Main {
	public static void main(String[] args) { 
		Scanner scn = new Scanner(System.in);
		int n =scn.nextInt();
		ArrayList<Integer> arr=new ArrayList<Integer>();
		for(int j=0;j<n;j++) {
			int h=scn.nextInt();
			arr.add(h);
		}int ans=0;
		for(int i=0;i<arr.size()-1;i++) {
			for(int j=i+1;j<arr.size();j++) {
				ans+= arr.get(i)*arr.get(j);
			}
		}System.out.println(ans);
    }
}

Measured memory_bytes: 27008
#include<stdio.h>
#include<algorithm>

int main(){
	int a[5];
	while(~scanf("%d,%d,%d,%d,%d",a,a+1,a+2,a+3,a+4)){
		std::sort(a,a+5);
		if((a[0]==a[1])&&(a[2]==a[3])&&(a[2]==a[1]))printf("four card\n");
		else if((a[1]==a[2])&&(a[3]==a[4])&&(a[3]==a[2]))printf("four card\n");
		else if((a[0]==a[1])&&(a[1]==a[2])&&(a[3]==a[4]))printf("full house\n");
		else if((a[0]==a[1])&&(a[2]==a[3])&&(a[3]==a[4]))printf("full house\n");
		else if((a[0]==1)&&(a[1]==10)&&(a[2]==11)&&(a[3]==12)&&(a[4]==13))printf("straight\n");
		else if((a[0]+1==a[1])&&(a[1]+1==a[2])&&(a[2]+1==a[3])&&(a[3]+1==a[4]))printf("straight\n");
		else if(
			((a[0]==a[1])&&(a[1]==a[2]))||
			((a[1]==a[2])&&(a[2]==a[3]))||
			((a[2]==a[3])&&(a[3]==a[4])))printf("three card\n");
		else if(
			((a[0]==a[1])&&((a[2]==a[3])||(a[3]==a[4])))||
			((a[1]==a[2])&&(a[3]==a[4])))printf("two pair\n");
		else if((a[0]==a[1])||(a[1]==a[2])||(a[2]==a[3])||(a[3]==a[4]))printf("one pair\n");
		else printf("null\n");
	}
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(n) begin(n), end(n)
struct cww
{
    cww()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
    }
} star;
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<char> vchar;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<char>> vvchar;
int H, W, K;
int count(vvchar s)
{
    int x = s[0].size();
    int y = s.size();
    int ans = 0;
    rep(i, y)
    {
        rep(j, x)
        {
            if (s[i][j] == '#')
            {
                ans++;
            }
        }
    }
    return ans;
}

void paint(vvchar row, vvint &ans, int &c, int Y) //colは切った後
{
    int y = row.size();
    int k = c;
    int pos = 0;
    int p = 0;
    while (count({row[p]}) == 0)
    {
        p++;
    }
    pos = p;
    int N = 0;
    rep(j, W)
    {
        if (row[pos][j] == '#')
        {
            N++;
            if (N == 2)
            {
                c++;
                N = 1;
            }
        }
        for (int i = Y; i < Y + y; i++)
        {
            ans.at(i).at(j) = c;
        }
        
    }
    c = k + count(row);
}

int main()
{

    cin >> H >> W >> K;
    vvchar s(H, vchar(W));
    vvint ans(H, vint(W));
    rep(y, H)
    {
        rep(x, W)
        {
            cin >> s[y][x];
        }
    }
    int c = 1;
    rep(y, H)
    {
        vvchar row(1, vchar(W));
        row = {s[y]};
        vvchar row2(1, vchar(W));
        if (y + 1 < H)
        {
            row2 = {s[y + 1]};
        }
        int j = y;
        while (count(row) == 0 and j + 1 < H)
        {
            row.push_back(s[j + 1]);
            if(j+2<H)
            {
                row2 = {s[j + 2]};
            }
            j++;
        }
        while (count(row2) == 0 and j + 1 < H)
        {
            row.push_back(s[j + 1]);
            if (j + 2 < H)
            {
                row2 = {s[j + 2]};
            }
            j++;
        }
        paint(row, ans, c, y);
        y = j;
    }
    rep(y, H)
    {
        rep(x, W)
        {
            cout << ans[y][x] << " ";
        }
        cout << endl;
    }
    return 0;
}

Measured memory_bytes: 1280
#include <bits/stdc++.h>  // clang-format off
using namespace std;
using Int = long long;
#define REP2(i, n) for (Int i = 0, max_i = (n); i < max_i; i++)
#define REP3(i, a, b) for (Int i = (a), max_i = (b); i < max_i; i++)
#define OVERLOAD2(_1, _2, _3, name, ...) name
#define REP(...) OVERLOAD2(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
struct SetupIO { SetupIO() { cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(13); } } setup_io;
#ifndef _MY_DEBUG
#define dump(...)
#endif  // clang-format on

/**
 *    author:  knshnb
 *    created: Tue Feb 11 19:59:55 JST 2020
 **/

template <class T> inline bool chmin(T& a, const T& b) {
    if (a <= b) return false;
    a = b;
    return true;
}
template <class T> inline bool chmax(T& a, const T& b) {
    if (a >= b) return false;
    a = b;
    return true;
}

signed main() {
    Int n;
    cin >> n;
    Int mi = 1e18, ans = 0;
    REP(i, n) {
        Int x;
        cin >> x;
        ans += mi > x;
        chmin(mi, x);
    }
    cout << ans << endl;
}


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

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

using namespace std;
const int INF = 1 << 30;
const long long MOD = 1000000000 + 7;
const double PI = acos(-1);

// n乗を求める
long long modPow(int n, int num) {
	if (num == 0) return 1;
	long long res = modPow(n, num / 2);
	if (num % 2 == 0) res = (res * res) % MOD;
	else res = (((res * res) % MOD) * n) % MOD;
	return res;
}

void setFactorial(int n, vector<long long> &factorials) {
	long long ans = 1;
	factorials.push_back(ans);
	rep(i, n) {
		ans *= ((long long)i + 1);
		ans %= MOD;
		factorials.push_back(ans);
	}
}

int main() {
	int N, K; cin >> N >> K;
	vector<int> A(N);
	rep(i, N)cin >> A[i];
	sort(A.begin(), A.end());
	vector<long long> factorials;
	setFactorial(N, factorials);
	
	long long maxSum = 0;
	long long minSum = 0;
	rep(i, N - K + 1) {
		long long num = 1;
		num *= (factorials[N - i - 1] * modPow(factorials[K - 1], MOD - 2)) % MOD;
		num %= MOD;
		num *= modPow(factorials[N - i - K], MOD - 2);
		num %= MOD;
		maxSum += ((long long)A[N - i - 1] * num) % MOD;
		maxSum %= MOD;
		minSum += ((long long)A[i] * num) % MOD;
		minSum %= MOD;
	}
	long long ans = (maxSum - minSum) % MOD;
	if (ans < 0)cout << ans + MOD;
	else cout << ans;
}


Measured memory_bytes: 1784
n = int(input())
A = [int(i) for i in input().split()]

cnt = 0
flag = 1
while flag:
    for i in range(n):
        a = A.pop(0)
        if a % 2 != 0:
            flag = 0
            break
        else:
            A.append(a // 2)
    else:
        cnt += 1
    
print(cnt)

Measured memory_bytes: 3060
char a[500][501];m[500][500];main(i,j,k,x,y,r,M,z){
for(;scanf("%d%d",&y,&x),x;printf("%d\n",r)){
	for(j=0;j<y;j++)scanf("%s",a[j]);
	for(j=0;j<y;j++)for(r=i=0;i<x;m[j][i]=r,i++)if(a[j][i]=='.')r++;else r=0;
	for(r=i=0;i<x;i++)for(j=0;j<y;j++)for(M=99,k=j;k<y;k++){
		if(M>m[k][i])M=m[k][i];
		if(r<M*(z=k-j+1))r=M*z;
	}
}exit(0);}

Measured memory_bytes: 1824
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
import java.util.stream.IntStream;

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

        int[] tastes = IntStream.rangeClosed(1, n).map(i -> l + i - 1).toArray();
        int sum = Arrays.stream(tastes).sum();
        int ans = Arrays.stream(tastes)
            .map(taste -> sum - taste)
            .boxed()
            .min(Comparator.comparing(taste -> Math.abs(sum - taste)))
            .get()
            .intValue();
        System.out.println(ans);
    }
}

Measured memory_bytes: 28748
#include <iostream>

int main(int argc, char **argv)
{

  int k, a, b;
  bool isOK = false;
  std::cin >> k;
  std::cin >> a >> b;

  for (int i = a; i <= b; i++)
  {
    if (i % k == 0)
    {
      isOK = true;
      break;
    }
  }
  if (isOK)
  {
    std::cout << "OK" << std::endl;
  }
  else
  {
    std::cout << "NG" << std::endl;
  }
  return 0;
}

Measured memory_bytes: 3676
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef pair<int,int> pii;
typedef pair<lli,lli> pll;
template<typename T>
int sz(const T &a){return (int)a.size();}
const int MAXN=1e5+1;
lli ree[MAXN];
lli fri[MAXN];
int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    int a,b,m;
    cin>>a>>b>>m;
    for(int i=0;i<a;i++){
        cin>>ree[i];
    }
    for(int i=0;i<b;i++)cin>>fri[i];
    int x,y;
    lli c;
    lli best=LLONG_MAX;
    for(int i=0;i<m;i++){
        cin>>x>>y>>c;
        x-=1,y-=1;
        best=min(best,ree[x]+fri[y]-c);
    }
    sort(ree,ree+a),sort(fri,fri+b);
    best=min(best,ree[0]+fri[0]);
    printf("%lli\n",best);
    return 0;
}

Measured memory_bytes: 1792
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>

using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;

template<typename T1,typename T2>inline void chmin(T1 &a,const T2 &b){if(a>b) a=b;}
template<typename T1,typename T2>inline void chmax(T1 &a,const T2 &b){if(a<b) a=b;}

#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()

/* do your best */

int main() {
   
  int h, w, a, b; cin >> h >> w >> b >> a;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cout << ((i < a) ^ (j < b));
    }
    cout << endl;
  }
  return 0;
}


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

fn main() {
    let n: usize = read();

    let mut ans: Vec<usize> = 
        match n {
            3 => vec![2, 5, 63],
            4 => vec![2, 5, 20, 63],
            5 => vec![2, 5, 20, 30, 63],
            _ => Vec::new(),
        };

    if n > 5 {
        let po = [2, 3, 4, 6];
        let mut cnt = 0;
        let mut bai = 0;
        while cnt < n {
            let kiso = 6 * bai;
            for i in 0..4 {
                ans.push(kiso + po[i]);
                cnt += 1;
                if cnt == n {
                    break;
                }
            }
            bai += 1;
        }
        let mut sum = 0;
        for i in 0..n {
            sum += ans[i];
        }
        match sum % 6 {
            2 => {
                for i in 0..n {
                    if ans[i] == 8 {
                        ans[i] = (bai-1) * 6 + 6;
                    }
                }
            },
            3 => {
                for i in 0..n {
                    if ans[i] == 9 {
                        ans[i] = bai * 6 + 6;
                    }
                }
            },
            5 => {
                for i in 0..n {
                    if ans[i] == 9 {
                        ans[i] = (bai-1) * 6 + 4;
                    }
                }
            },
            _ => {},
        }
    }

    for i in 0..n {
        if i != 0 { print!(" "); }
        print!("{}", ans[i]);
    }
    println!();
}

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

Measured memory_bytes: 4476
(let ((n (read))
      (lst (reverse (loop for i from 0 to 6
                          collect (expt 2 i)))))

  (defun judge (x l)
    (if (>= x (car l))
      (car l)
      (judge x (cdr l))))

  (format t "~A~%"
          (judge n lst)))


Measured memory_bytes: 24368
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define int long long
using namespace std;
int n,m,cnt=0;
int head[410000],nxt[410000],to[410000],val[410000];
int ma=0,mi=0;
int q[1000000],dis[100100],f[100100],vis[100100];
void addedge(int x,int y,int z)
{
    cnt++;
    nxt[cnt]=head[x];
    head[x]=cnt;
    to[cnt]=y;
    val[cnt]=z;
}
bool spfa(int x)
{
    int l=0,r=1;
    q[1]=x;vis[x]=1;dis[x]=0;
    while(l<r)
    {
        int u=q[++l];
        for(int i=head[u];i!=-1;i=nxt[i])
        {
            int v=to[i];
            if(dis[v]==12345678987654321)
            {
                dis[v]=dis[u]+val[i];
                ma=max(ma,dis[v]);
                mi=min(mi,dis[v]);
                if(ma-mi>1e9) return 0;
                vis[v]=1;
                if(!f[v]) q[++r]=v,f[v]=1;
            }
            else if(dis[v]!=dis[u]+val[i]) return 0;
        }
        f[u]=0;
    }
    return 1;
}
signed main()
{
//	freopen("line.in","r",stdin);
//	freopen("line.out","w",stdout);
    memset(head,-1,sizeof(head));
    for(int i=1;i<=100000;i++)
        dis[i]=12345678987654321;
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int x,y,z;
        scanf("%lld%lld%lld",&x,&y,&z);
        addedge(x,y,z);
        addedge(y,x,-z);
    }
    for(int i=1;i<=n;i++)
        if(!vis[i]) {if(!spfa(i)) {printf("No");return 0;}}
    printf("Yes");
    return 0;
}

Measured memory_bytes: 19072