akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,o,n) for(long long i = o;i<n;i++)
#define oneforall ios::sync_with_stdio(false);cin.tie(0);
#define all(v) (v).begin(),(v).end()
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
const long long INF=1e18;
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
typedef vector<int> vi;
typedef vector<long long> vl;
typedef long long ll;
typedef vector<pair<long,long>> vpll;
typedef vector<pair<int,int>> vpii;

template<class T>
struct BIT
{
	int size;
	vector<T> bit;

	void init(int n){
		size = n;
		bit.assign(size,0);
	}

	T total(ll pos){
		ll tmp = 0;
			for(;pos>=0;pos = (pos&(pos+1))-1){
				tmp+=bit[pos];
			}
	return tmp;
	}
	void upd(ll pos,T val){
		for(;pos<size;pos = pos|(pos+1)){
			bit[pos]+=val;
		}
	}
	T total(ll pos_,ll _pos){
		return total(_pos) - total(pos_ - 1);
	}
};
int main() {
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	ini(n);
	ins(a);
	ini(n1);
	BIT<int> bit[26];
	FOR(i,0,26){
		bit[i].init(n);
	}
	FOR(i,0,n){
		bit[a[i]-'a'].upd(i,1);
	}
	FOR(i,0,n1){
		ini(x);
		if(x == 1){
			ini(x1);
			x1--;
			char x_;
			in(x_);
			bit[a[x1]-'a'].upd(x1,-1);
			a[x1] = x_;
			bit[a[x1]-'a'].upd(x1,1);
		}
		else{
			ini(x1);
			x1--;
			ini(_x);
			_x--;
			int tmp1 = 0;
			FOR(i1,0,26){
				tmp1+= bit[i1].total(x1,_x)>0;
			}
			out(tmp1);
		}
	}
	return 0;
}

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

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

struct UnionFindTree{
    vector<int>par,sz;
    UnionFindTree(int n){
        par.resize(n);
        sz.resize(n);
        for(int i=0;i<n;i++){
            par[i]=i;
            sz[i]=1;
        }
    }
    int find(int x){
        return x==par[x]?x:par[x]=find(par[x]);
    }
    void unite(int x,int y){
        x=find(x);y=find(y);
        if(x==y)return;
        if(sz[x]<sz[y])swap(x,y);
        sz[x]+=sz[y];
        par[y]=x;
    }
    bool areSame(int x,int y){
        return find(x)==find(y);
    }
    int size(int x){
        return sz[find(x)];
    }
};

int N,M;
int A[2222],B[2222],D[2222];

int ei[2222];

void calc(int id){
    vpint es;
    rep(i,M)if(i!=id)es.pb({D[i],i});
    sort(all(es));

    int val=0;
    val+=D[id];
    UnionFindTree uf(N);
    uf.unite(A[id],B[id]);
    rep(w,es.size()){
        int k=es[w].se;
        if(uf.areSame(A[k],B[k]))continue;
        uf.unite(A[k],B[k]);
        val+=D[k];
    }
    ei[id]=val;
}

const int mod=1000000007;

int po[222222];

signed main(){
    po[0]=1;
    for(int i=1;i<222222;i++)po[i]=po[i-1]*2%mod;

    cin>>N>>M;
    int X;cin>>X;
    rep(i,M)cin>>A[i]>>B[i]>>D[i],A[i]--,B[i]--;

    rep(i,M)calc(i);


    int mi=*min_element(ei,ei+M);

    int cnt1=0,cnt2=0;
    rep(i,M){
        if(ei[i]<X)cnt1++;
        else if(ei[i]==X)cnt2++;
    }

    if(cnt2==0){
        cout<<0<<endl;
        return 0;
    }

    int ans;
    if(cnt1==0){
        ans=(po[cnt2]-2+mod)%mod;
    }
    else{
        ans=2*(po[cnt2]-1+mod)%mod;
    }

    ans=ans*po[M-cnt1-cnt2]%mod;
    cout<<ans<<endl;
    return 0;
}


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

#include <iostream>
//#include <algorithm>
// #include <iomanip>
#define ll long long
#define map unordered_map
#define set unordered_set
#define pll pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>

using namespace std;

const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);

int main() {
  // std::cout << std::fixed << std::setprecision(10);

  ll N, X, T;
  scanf("%lld %lld %lld", &N, &X, &T);
  ll ans = (N + X - 1) / X * T;
  cout << ans << endl;
}


Measured memory_bytes: 3636
import java.util.*;

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

        for(int i = 0; i < N; i++){
            A[i] = sc.nextInt();
        }
        sc.close();
        Arrays.sort(A);
        int Re = A[N - 1] - A[0];

        System.out.println(Re);
    }
}

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

int main() {
  long long  a, b, c, k;
  cin >> a >> b >> c >> k;
  
  if (k <= a) cout << k << endl;
  else if (k <= a + b) cout << a << endl;
  else if (k <= a + b + c) cout << a - (k - a - b) << endl;
  
  return 0;
}

Measured memory_bytes: 3636
from collections import deque
h,w=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))
b=[]
for i in range(n):
    for j in range(a[i]):
      b.append(i+1)
b=deque(b)
ans=[]
for hi in range(h):
  c=[0]*w
  for wi in range(w):
    if hi%2==0:
      j=wi
    else:
      j=w-wi-1
    c[j]=b.popleft()
  ans.append(c)
for ci in ans:
  print(' '.join(map(str,ci)))



Measured memory_bytes: 41456
#include <iostream>
#include <vector>

using namespace std;

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

    vector<int> U(N);
    cin >> U[0];
    for (int i = 1; i < N; i++) {
        int x;
        cin >> x;
        U[i] = x + U[i - 1];
    }

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

    int result = 0;
    int d_sum = 0;
    for (int i = N - 1; i >= 0; i--) {
        d_sum += D[i];
        result = max(U[i] + d_sum, result);
    }

    cout << result << endl;
    return 0;
}


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

// 最小公倍数を求める
int euqlid (int a, int b){
  int temp;
  temp = a % b;
  if(temp == 0)return b;
  return euqlid(b,temp);
}
ll conbination(ll a,ll b){
  ll u = 1;
  ll d = 1;
  while(b != 0){
    u *= a;
    d *= b;
    a--;
    b--;
  }
  return u / d;
}
int fact(int n){
  if(n == 1)return 1;
  return n * fact(n-1);
  }

int main(){
  int n;
  cin >> n;
  cout << int((n+1)/2) << endl;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<algorithm>
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); i++)
#define rep1(i,n) for(int i = 1; i < (n); i++)

int main(){
    int n, k;
    cin >> n >> k;
    int a = 1;
    int b = k;
    while(n-b >= 0){
        a++;
        b *= k;
    }
    cout << a << endl;
}


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

int main(){
  int N,M,X;
  cin>>N>>M;
  vector<int> toi(M);
  vector<string> jg(M);
  for(int i=0;i<M;i++)
    cin>>toi.at(i)>>jg.at(i);
  vector<int> check(N),count(N);
  
  int AC=0;
  int WA=0;
  for(int i=0;i<M;i++){
    X=toi.at(i)-1;
    if(check.at(X)==0){
  	  if(jg.at(i)=="WA")
        count.at(X)++;
   	  else{
      	AC++;
        check.at(X)=-1;
        WA+=count.at(X);
      }
    }
  }
  cout<<AC<<" "<<WA<<endl;
}

Measured memory_bytes: 6912
#include <bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
#define rep(X,N) for(ll X = 0; X < (N); X++)
#define PI (acos(-1.0))
#define pback push_back
#define mpair make_pair
#define MODN 1000000007
#define ALL(V) (V).begin(),(V).end()
#define CERR if(false) cerr

typedef long long ll;

using namespace std;
//using namespace boost::multiprec

int main(){

    string s;
    cin >> s;

    int a = 0;
    int b = 0;

    if(s[0] == s[1]){
        a = 1;
        b = 2;
    }else{

        for(int i = 2; i < s.size(); i++){
            if(s[i] == s[i - 1]){
                b = i + 1;
                a = i;
                break;
            }else if(s[i] == s[i - 2]){
                b = i + 1;
                a = i - 1;
                break;
            }
        }

    }

    if(a != 0){
        cout << a << " " << b << endl;
    }else{
        cout << -1 << " " << -1 << endl;
    }
    
    return 0;
}

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

#define ll long long

const ll mod = 1e9 + 7;
const ll N = 2e3 + 1;

int s, fact[N];

ll fastpow(ll a, ll n, ll m)
{
    ll res = 1;
    while (n)
    {
        if (n & 1)
            res = (res * a) % m;
        n >>= 1;
        a = (a * a) % m;
    }
    return res;
}

ll add(ll a, ll b, ll m)
{
    return (a % m + b % m) % m;
}

ll mul(ll a, ll b, ll m)
{
    return (a % m * b % m) % m;
}

ll div(ll a, ll b, ll m)
{
    return mul(a, fastpow(b, m - 2, m), m);
}

int ncr(int n, int r)
{
    int x = mul(fact[r], fact[n - r], mod);
    return div(fact[n], x, mod);
}

int main()
{
    cin >> s;

    ll ans = 0;

    fact[0] = fact[1] = 1;
    for (int i = 2; i < N; i++)
        fact[i] = mul(fact[i - 1], i, mod);

    for (int i = 1; i < s / 3 + 1; i++)
    {
        int tmp = s - 3 * i;
        int a = ncr(tmp + i - 1, i - 1);
        ans = add(ans, a, mod);
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 3644
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 0;
const ll mo = 998244353;
ll mypow(ll a, ll b)
{
  ll res = 1;
  a %= mo;
  while (b)
  {
    if (b & 1)
      res = res * a % mo;
    a = a * a % mo;
    b >>= 1;
  }
  return res;
}
int main()
{
ll n;
cin >> n;
vector<ll> a(n);
ll maxv = 1;
rep(i, n)
{
  cin >> a[i];
  if (maxv < a[i])
    maxv = a[i];
}
vector<ll> r(maxv + 1);
rep(i, n) r[a[i]]++;

auto mod = [](ll modx) {modx%=mo;modx+=mo;modx%=mo;return modx; };
vector<ll> v(maxv + 1);
rep(i, maxv) v[i + 1] = mypow(i + 1, mo - 2);
for (ll i = 1; i <= maxv; i++)
{
  for (ll j = i * 2; j <= maxv; j += i)
  {
    v[j] -= v[i];
    //v[j] = mod(v[j]);
  }
}

vector<ll> sum(maxv + 1);
vector<ll> sum2(maxv + 1);
for (ll i = 1; i <= maxv; i++)
{
  for (ll j = i; j <= maxv; j += i)
  {
    sum[i] += r[j] * j;
    sum[i] = mod(sum[i]);
    sum2[i] += r[j] * mod(j * j);
    sum2[i] = mod(sum2[i]);
  }
}
rep(i, maxv)
{
  ll an = 0;
  an = v[i + 1] * (mod(sum[i + 1] * sum[i + 1]) - sum2[i + 1]);
  an = mod(an);
  an *= mypow(2, mo - 2);
  an = mod(an);
  ans += an;
  ans = mod(ans);
}
cout << ans << endl;
return 0;
}

Measured memory_bytes: 33024
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>

typedef long long ll;
using namespace std;

int main()
{
  int n, sum = 0;
  int a[100001]={0}, tmp, i;

  cin >> n;

  for( i=0; i<n; i++){
    cin >> tmp;
    if(tmp > n) sum++;
    else a[tmp]++;
  }

  for( i=1; i<=n; i++){
    if(a[i] >= i) sum += a[i] - i;
    else sum += a[i];
  }

  cout << sum << endl;

  return 0;
}



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

int main() {
  int N,A,B;
  cin>>N>>A>>B;
  
  int train = N*A;
  int taxi = B;
  if(train<taxi){
    cout<<train<<endl;
  }
  else{
    cout<<taxi<<endl;
  }

}


Measured memory_bytes: 256
S=int(input())

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
N = 10**4
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

if S<3:
    ans=0
n=S//3
ans=0
for i in range(1,n+1):
    ans+=cmb(S-2*i-1,i-1,mod)
    ans%=mod
print(ans)


Measured memory_bytes: 10180
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

const int maxn = 2e5 + 5;
vector<int> adj[maxn];
unordered_map<int, unordered_map<int, int>> has_edge;
bool vis[maxn];
int cnt, ans;

void dfs(int u){
    vis[u] = 1;
    cnt++;
    for(auto& v: adj[u]){
        if(!vis[v]){
            dfs(v);
        }
    }
}

int main() {
    int n, m; cin>>n>>m;
    for(int i = 0; i < m; i++){
        int u, v; cin>>u>>v;
        if(u > v) swap(u, v);
        if(has_edge[u][v]) continue;
        adj[u].push_back(v);
        adj[v].push_back(u);
        has_edge[u][v] = 1;
    }
    for(int i = 1; i <= n; i++){
        if(!vis[i]){
            cnt = 0;
            dfs(i);
            ans = max(ans, cnt);
        }
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 50768
import java.util.Scanner

import scala.collection.mutable

object Main extends App {
  val sc = new Scanner(System.in)
  val n = sc.nextInt()
  val heights = (1 to n).map { _ => sc.nextInt() }
  val ls = mutable.Map.empty[Int, Int].withDefaultValue(0)
  val rs = mutable.Map.empty[Int, Int].withDefaultValue(0)
  heights.zipWithIndex.foreach { case (h, i) =>
    val l = (i + 1) + h
    ls.put(l, ls(l) + 1)
    val r = (i + 1) - h
    if(r > 0) rs.put(r, rs(r) + 1)
  }
  val result = rs.map { case (r, num) =>
    ls.getOrElse(r, 0).toLong * num.toLong
  }.sum
  println(result)
}


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

using namespace std;
int a[200050],n,k;
bool check(int mid)
{
	int ans=0;
	for(int i=1;i<=n;i++)
	{
      if(a[i]>mid)
		 ans+=a[i]/mid;
		
	}
  if(ans>k) return 0;
	return 1;
}
int main()
{
	
	cin>>n>>k;
	int l=1,r=-1;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		r=max(a[i],r);
	}
	int ans=0;
	while(l<=r)
	{
		int mid=(l+r)/2;
		if(check(mid))
		{
			r=mid-1;
			
		}
		else l=mid+1;
	}
  
  if(check(l))
	cout<<l;
  else
    cout<<l-1;
	return 0;
} 


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

using i64 = long long;
using u64 = unsigned long long;

const int mod = (int)1e9+7;

void __print() {cerr << "]\n";}

template<typename T, typename... V>
void __print(T t, V... v)
{ cerr << t; if(sizeof...(v)) cerr << ", "; __print(v...);}

#define dbg(x...) cerr << "[" << #x << "] = ["; __print(x);


int main()
{
    i64 n;
    scanf("%lld", &n);
    printf("%lld\n", (n*n-n)/2);
}


Measured memory_bytes: 3808
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
#define cinf(n,x) for(int i=0;i<(n);i++) cin >> x[i];

int main(){
  string S ;
  cin >> S ;
  int c1=0; 
  int c2=0;
  for(int i=0;i< S.size();i++){
    if(S[i] == '0')
      c1 ++ ;
    else
      c2 ++ ;
  }
  cout << 2*min(c1,c2) << endl;
}

Measured memory_bytes: 512
# Problem D - Remainder Reminder

# input
N, K = map(int, input().split())

# initialization
class_count = 0

for b in range(1, N+1):
    count_1 = (N//b) * max(0, b-K)
    count_2 = max(0, (N % b + 1) - K)
    class_count += count_1 + count_2

# output
if K==0:
    class_count -= N
print(class_count)


Measured memory_bytes: 40864
#include <bits/stdc++.h>
#define ll long long
int main()
{
    char a;
    char b;
    char c;
    ll k = 0;
    std::cin >> a >> b >> c;

    if (a == 'R') {
        if (b == 'R') {
            if (c == 'R') {
                k = 3;
            } else {
                k = 2;
            }
        } else {
            k = 1;
        }
    } else {
        if (b == 'R') {
            if (c == 'R') {
                k = 2;
            } else {
                k = 1;
            }
        } else {
            if (c == 'R') {
                k = 1;
            } else {
                k = 0;
            }
        }
    }
    std::cout << k << std::endl;
    return 0;
}

Measured memory_bytes: 3628
import java.util.*;

class Main {
	public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
      	int n = Integer.parseInt(sc.next());
      	int brokenCnt = 0;

      	int i = 0;
      	int num = n;
      	while (i != n) {
          	int a = Integer.parseInt(sc.next());
          	if ((i + 1) != a) { 
              brokenCnt++; 
              n--;
            } else {
              i++;
            }
          	
        }
      
      	if (brokenCnt == num) { System.out.println(-1); return; }

      	System.out.println(brokenCnt);
    }
}

Measured memory_bytes: 49200
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<math.h>
#include<iomanip>
#define rep(i,n) for(int i=0;i<n;++i)
#define mod 1000000007
using namespace std;
using ll = long long;
int main() {
	int A, B, K;	cin >> A >> B >> K;
	rep(i, K) {
		if (i % 2 == 0) {
			if (A % 2 == 0) {
				B += A / 2;
				A /= 2;
			}
			else {
				A -= 1;
				B += A / 2;
				A -= A / 2;
			}
		}
		else {
			if (B % 2 == 0) {
				A += B / 2;
				B /= 2;
			}
			else {
				B -= 1;
				A += B / 2;
				B -= B / 2;
			}
		}
	}
	cout << A << " " << B << endl;
	cin >> A;
}

Measured memory_bytes: 256
Num = []
Num = list(map(int,input().split(" "))) #0-all 1-pick
lists = []
for i in range(0,Num[0]):
    lists.append(int(input()))
lists.sort()
lists.reverse()
dist = lists[0] - lists[0+Num[1]-1]
for j in range(1,Num[0] - Num[1] + 1):
    tmp = lists[j] - lists[j+Num[1]-1]
    if (dist > tmp):
        dist = tmp
print(dist)

Measured memory_bytes: 7384
import java.util.*;

class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int len = sc.nextInt();
		int[] nums = new int[len];
		
		for(int i = 0; i < len; i++){
			nums[len - (i + 1)] = sc.nextInt();
		}
		
		for(int i = 0; i < len; i++){
			System.out.print(nums[i]);
			if(i < len - 1) System.out.print(" ");
		}

		System.out.println();
	}
}

Measured memory_bytes: 19600
#include <iostream>
#include <array>
#include <vector>
#include <map>

using ll = long long;
using namespace std;

int main() {
    map<int, int> M;
    int n, m, k, p = 0;
    ll c = 0;
    cin >> n >> m;
    M[0] = 1;
    for (int i = 0; i < n; ++i) {
        scanf("%d", &k);
        p = (p + k)%m;
        c += M[p];
        M[p]++;
    }
    cout << c << "\n";
    return 0;
}

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

using namespace std;

int main()
{
    int a,b;
    cin >>a>>b;

    if(a>=1 && a<=9 && b>=1 && b<=9)
    {
        cout<<a*b<<endl;
        return 0; 
    }
    cout<<-1<<endl; 

    return 0; 
}



Measured memory_bytes: 256
N,Q=map(int,input().split())
L=[[]for i in range(N+1)]
for i in range(N-1):
    a,b=map(int,input().split())
    L[a].append(b)
    L[b].append(a)
#print(L)

P=[[0,0] for i in range(N+1)]
for i in range(Q):
    p,x=map(int,input().split())
    P[p][1]+=x
#print(P)
P[1][0]=1
A=[]
for i in L[1]:
    A.append([1,i])
    P[i][0]=1
    P[i][1]+=P[1][1]
#print(A)
#print(P)
for i in range(10**7):
    if i==len(A):
        break
    s,g=A[i]
    for j in L[g]:
        if P[j][0]==0:
            P[j][0]=g
            P[j][1]+=P[g][1]
            A.append([g,j])
    #print(A)
ans=[]
for i in range(1,N+1):
    ans.append(P[i][1])
print(*ans)

Measured memory_bytes: 169272
import java.io.*;
import java.util.*;
 
public class Main{
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){br = new BufferedReader(new InputStreamReader(System.in));}
        public FastReader(String fileName) throws FileNotFoundException{br = new BufferedReader(new FileReader(fileName));}
        String next(){while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());}catch (IOException  e){e.printStackTrace();}}return st.nextToken();}
        int nextInt(){return Integer.parseInt(next());}
        long nextLong(){return Long.parseLong(next());}
        double nextDouble(){return Double.parseDouble(next());}
        char[][] readCharArray(int n,int m) {
            String str;
            char[][] arr = new char[n][m];
            for(int i=0;i<n;i++) {
                str=nextLine();
                for(int j=0;j<n;j++) {
                    arr[i][j]=str.charAt(j);
                }
            }
            return arr;
        }
        char[][] readCharArray(int n){
            return readCharArray(n,n);
        }
        int[] readArray(int n){int[] a=new int[n];for (int i=0; i<n; i++) a[i]=nextInt();return a;}
        long[] readLongArray(int n){long[] a=new long[n];for (int i=0; i<n; i++) a[i]=nextLong();return a;}
        String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}
    }
    static void printArray(int[] arr){
        for(int x:arr){
            System.out.print(x+" ");
        }
        System.out.println();
    }
    static final Random random=new Random();
    static void ruffleSort(int[] a) {
        int n=a.length;//shuffle, then sort
        for (int i=0; i<n; i++) {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        }
        Arrays.sort(a);
    }
    public static void main(String[] args) throws NumberFormatException, IOException {
        FastReader st = new FastReader();
        int ans=0;
        int n = st.nextInt();
        int m = st.nextInt();
        Node[] node = new Node[n];
        for(int i=0;i<n;i++) node[i] = new Node();
        for(int i=0;i<m;i++) {
        	int a = st.nextInt(),b = st.nextInt();
        	node[a-1].adj.add(node[b-1]);
        	node[b-1].adj.add(node[a-1]);
        }
        dfs(node[0]);
        for(int i=0;i<n;i++) {
        	if(!node[i].vis) {
        		dfs(node[i]);
        		ans++;
        	}
        }
        System.out.println(ans);
    }
    static void dfs(Node from) {
    	from.vis = true;
    	for(Node nn:from.adj) {
    		if(!nn.vis) {
    			nn.vis = true;
    			dfs(nn);
    		}
    	}
    }
    static class Node{
    	boolean vis = false;
    	ArrayList<Node> adj = new ArrayList<>();
    }
}

Measured memory_bytes: 68728
N,H=map(int,input().split())
c=[list(map(int,input().split()))for _ in[0]*N]
a=max(r[0]for r in c)
b=sorted(r[1]for r in c if r[1]>a)
n=0
while H>0 and b:
    H-=b.pop()
    n+=1
print(n+max(H-1,-1)//a+1)

Measured memory_bytes: 28456
from collections import defaultdict
import sys,heapq,bisect,math,itertools,string
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
AtoZ = [chr(i) for i in range(65,65+26)]
atoz = [chr(i) for i in range(97,97+26)]

N,M = map(int,input().split())

if abs(N-M) > 1:
	print(0)
else:
	if N == M:
		ans = 1
		for i in range(2,N+1):
			ans *= i
			ans %= mod
		print((ans**2*2)%mod)
	else:
		MIN = min(N,M)
		ans1 = ans2 =1
		for i in range(2,MIN+1):
			ans1 *= i
			ans1 %= mod
		ans2 = ans1*max(N,M)%mod
		print(ans1*ans2%mod)

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

int main(){
    string a,b,c; cin >> a >> b >> c;
    string ans="";
    ans+=(char)toupper(a[0]);
    ans+=(char)toupper(b[0]);
    ans+=(char)toupper(c[0]);
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(t) t.begin(), t.end()
#define inrange(i, a, b) (((i)>= min((a), (b))) && ((i) <= max((a), (b))))
typedef vector<ll> vi;
#define fi first
#define se second
const ll inf = 9e18;
const ll mod = 1e9 + 7;

void print(ll a[], ll n){for(ll i=0;i<n;i++){cout<<a[i]<<" ";}cout<<"\n";}
ll power(ll x, ll y, ll M = inf){
    ll ans = 1;
    x %= M;
    while(y){
        if(y&1)
            ans = (x * ans) % M;

        x = (x * x) % M;
        y >>= 1;
    }
    return ans;
}
ll modInverse(ll n) {return power(n, mod-2, mod);}
inline ll mul(ll a, ll b){ return (a * b) % mod; }
inline ll sub(ll a, ll b){ ll c = a - b; if(c < 0) c += mod; return c; }
inline ll add(ll a, ll b){ ll c = a + b; if(c > mod) c -= mod; return c; }
inline ll divi(ll a, ll b){ return mul(a, modInverse(b)); }

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

	ll n, r;
	cin>>n>>r;
	if(n>=10){
		cout<<r;
	}
	else{
		cout<<r+100*(10 - n);
	}
	return 0;
}


Measured memory_bytes: 256
Input = list(map(int,input().split()))

point =[]
for i in range(Input[0]):
    point.append(0)

for i in range(Input[2]):
    aa = int(input())
    point[aa-1] +=1
gouhi =[]
for i in range(Input[0]):
    bb = 0
    if Input[1]-Input[2]+point[i] > 0 :
        bb = "Yes"
    else:
        bb = "No"
    gouhi.append(bb)
print("\n".join(gouhi))

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

ll knapsack(int W, vector<ll> weights, vector<ll> values) {
    int n = weights.size();
    vector<vector<ll>> dp(n+1, vector<ll>(W+1, 0));
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= W; j++) {
            dp[i][j] = dp[i-1][j];
            dp[i][j] = max(dp[i][j], ((j >= weights[i-1]) ? values[i-1] + dp[i-1][j-weights[i-1]] : 0));
        }
    }
    return dp[n][W];
}

int main() {
    int n, w;
    cin >> n >> w;
    vector<ll> weights(n);
    vector<ll> values(n);
    for (int i = 0; i < n; i++) cin >> weights[i] >> values[i];
    cout << knapsack(w, weights, values) << endl;
    return 0;
}

Measured memory_bytes: 83144
h,w=map(int,input().split()) 
l = [input() for _ in range(h)]
dx = [1,-1,0,0]
dy = [0,0,1,-1]
for i in range(h):
    for j in range(w):
        if l[i][j] =='#':
            cnt = 0
            for di in range(4):
                y = i+dx[di]
                x = j+dy[di]
                if x<0 or y<0 or x>w-1 or y>h-1:
                    continue
                else:
                    if l[y][x] =='#':
                        cnt +=1
            if cnt == 0:
                print('No')
                exit()

print('Yes')

Measured memory_bytes: 9160
// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever for (;;)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
template<typename A> using V = std::vector<A>;
template<typename A> using F = std::function<A>;
template<typename A, typename B> using P = std::pair<A, B>;
using pii = P<int, int>;
using vi = V<int>;
using vd = V<double>;
using vs = V<std::string>;
using vpii = V<pii>;
using vvi = V<vi>;
using vvpii = V<vpii>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> std::istream &operator>>(std::istream &is, std::pair<A, B> &p) { is >> p.first; is >> p.second; return is; }
using namespace std;
// clang-format on

using Weight = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(const int &s = 0, const int &d = 0, const Weight &w = 0) : src(s), dst(d), weight(w) {}
};

using Edges = std::vector<Edge>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;

class Graph {
  std::vector<Edges> g;
  using iterator = std::vector<Edges>::iterator;
  using const_iterator = std::vector<Edges>::const_iterator;

public:
  Graph(const int &size = 0) : g(size) {}
  int size() const { return g.size(); }
  const Edges &operator[](const int &i) const { return g[i]; }
  void addArc(const int &src, const int &dst, const Weight &w = 1) { g[src].emplace_back(src, dst, w); }
  void addEdge(const int &node1, const int &node2, const Weight &w = 1) {
    addArc(node1, node2, w);
    addArc(node2, node1, w);
  }
  iterator begin() { return g.begin(); }
  const_iterator begin() const { return g.begin(); }
  iterator end() { return g.end(); }
  const_iterator end() const { return g.end(); }
};

template<int inf = std::numeric_limits<Weight>::max() / 8> Matrix warshallFloyd(const Graph &g) {
  int n = g.size();
  Matrix d(n, Array(n, inf));
  rep(i, n) d[i][i] = 0;
  for (auto &adj : g) {
    for (auto &e : adj) cmin(d[e.src][e.dst], e.weight);
  }
  rep(k, n) rep(i, n) rep(j, n) {
    if (d[i][k] != inf && d[k][j] != inf) cmin(d[i][j], d[i][k] + d[k][j]);
  }
  return d;
}

main {
  int n, m, s, g1, g2;
  while (cin >> n >> m >> s >> g1 >> g2, n) {
    --s, --g1, --g2;
    Graph g(n);
    while (m--) {
      int b1, b2, c;
      cin >> b1 >> b2 >> c;
      --b1, --b2;
      g.addArc(b1, b2, c);
    }
    Matrix d = warshallFloyd<INF>(g);
    int mini = INF;
    rep(i, n) cmin(mini, d[s][i] + d[i][g1] + d[i][g2]);
    cout << mini << endl;
  }
}

Measured memory_bytes: 3160
import heapq

h = []
n, m = list(map(int, input().split()))
for _ in range(n):
  a, b = list(map(int, input().split()))
  heapq.heappush(h, (a, b))

cost = 0
while True:
  (a, b) = heapq.heappop(h)
  if b < m:
    cost += a * b
    m -= b
  else:
    cost += a * m
    break

print(cost)

Measured memory_bytes: 16716
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <array>
#include <tuple>
#include <random>

using namespace std;
typedef long long int ll;

#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for(int i = int(a); i < int(b); i++)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep,)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()

#define INF 10000000000

ll a, b, c, n, x, y, z, w, h, ans = 0, cnt = 0, max = 0;
string s;

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

    vector<ll> list = {1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049};
    cin >> n;
    ll dp[n + 1];
    dp[0] = 0;
    rep(i, 1, n + 1){
        dp[i] = INF;
        rep(j, list.size()){
            if(i < list[j]){
                break;
            }
            dp[i] = min(dp[i], dp[i - list[j]] + 1);
        }
    }
    cout << dp[n] << endl;
}



Measured memory_bytes: 1024
N,M,X = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
ans = float('inf')
for mask in range(1,1<<N):
    tmp = [0]*M
    money = 0
    for i in range(N):
        if mask>>i & 1:
            for j in range(M):
                tmp[j]+=A[i][j+1]
            money+=A[i][0]
    if min(tmp)>=X:
        ans = min(ans,money)
if ans == float('inf'):
    print(-1)
else:
    print(ans)

Measured memory_bytes: 9220
#include <iostream>
#include <vector>

int main()
{
	long long N; std::cin >> N;
	long long A; std::cin >> A;
	long long B; std::cin >> B;
	std::vector<int> X(N);
	for(auto &x: X) std::cin >> x;
	long long ans = 0;
	for(int n=0; n<N-1; n++)
	{
		ans += std::min(A*(X[n+1]-X[n]), B);
	}
	std::cout << ans << std::endl;
	return 0;
}

Measured memory_bytes: 640
from collections import deque
n=int(input())
e=[[]for _ in range(n+1)]
c=[-1]*(n+1)
for _ in range(n-1):
  a,b,w=map(int,input().split())
  e[a]+=[(b,w)]
  e[b]+=[(a,w)]
q=deque([1])
c[1]=0
while q:
  now=q.popleft()
  for to,w in e[now]:
    if c[to]==-1:
      c[to]=(c[now]+w)%2
      q.append(to)
print(*c[1:],sep='\n')

Measured memory_bytes: 99332
#include <stdio.h>

int main(void) {
	// your code goes here
	int n;
	scanf("%d",&n);
	int a;
	int i;
	int notBreak = 1;
	for(i=0;i<n;i++)
	{
		scanf("%d",&a);
		if(a==notBreak){
			notBreak++;
		}
	}
	if(notBreak == 1)
		printf("-1\n");
	else
		printf("%d\n",n-notBreak+1);
	return 0;
}


Measured memory_bytes: 128
#include<bits/stdc++.h>
  using namespace std;
  #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #define endl "\n"
  #define ll long long
  #define int long long
  #define double long double
  #define max(a,b) (a>b?a:b)
  #define min(a,b) (a<b?a:b)
  #define mp make_pair
  #define pb push_back
  #define inf INT_MAX
  #define f first
  #define s second
  #define trace1(x)                cerr<<#x<<": "<<x<<endl
  #define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
  #define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
  #define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
  #define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<< ": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
  #define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
  #define cases int testcases;cin>>testcases; while(testcases--)
  #define all(v) v.begin(), v.end()
  #define mod INT_MAX 
  const int MOD=1e9+7;
  const int N=2e5+2;

 int divisors(int x)
 {
     int cnt=0;
     for(int i = 1; i*i <= x; ++i)
         if(x % i == 0)
           { if(x/i != i) cnt++;
            cnt++;}
     return cnt%MOD;
 }
 
   
  int32_t main()
  {/*
    #ifndef ONLINE_JUDGE
       freopen("in.txt","r",stdin);
       freopen("out.txt","w",stdout);
    #endif*/
       IOS
       int n,sum=0;
       cin>>n;
       // m*(m+floor(n/m))*(floor(n/m)+1-m)
       int lim=(int)sqrt(n);
       for(int i=1;i <=lim;i++)
       {
       	 sum+=(i * (i+ (n/i)) * ((n/i)+1-i) );
       	 
       }
       cout<<sum - (lim * (lim+1) * (2*lim+1)/6);
       return 0;
    }

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

int main()
{
        ll N; cin >> N;
        vector<ll> a(N, 0);
        map<ll, ll> m;
        for (int i = 0; i < N; ++i) {
                cin >> a[i];
                m[a[i]]++;
        }
        vector<ll> T;
        for (auto i : m) {
                T.push_back(i.first);
        }

        ll ans = 0;
        for (int i = 0; i < T.size(); ++i) {
                ll tmp = T[i];
                ll sum = m[tmp] + m[tmp+1] + m[tmp+2];
                if (ans < sum) ans = sum;
        }
        cout << ans << endl;

}


Measured memory_bytes: 7416
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const int INF = 1000000005;
const long double EPS = 1e-10;

int main() {
  int n;
  cin >> n;
  vector<int> d(n);
  rep(i, n) cin >> d[i];

  if (d[0] != 0) {
    cout << 0 << endl;
    return 0;
  }
  sort(d.begin(), d.end());
  if (d[1] == 0) {
    cout << 0 << endl;
    return 0;
  }

  ll ans = 1, cnt = 1, cnt2 = 1;
  FOR(i, 1, n) {
    if (d[i] == d[i - 1]) cnt2++;
    else if (d[i] - d[i - 1] == 1) {
      cnt = cnt2;
      cnt2 = 1;
    }
    else {
      ans = 0;
      break;
    }

    ans *= cnt;
    ans %= mod2;
  }
  
  cout << ans << endl;


  
  return 0;
}

Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;

//探索
vector<bool> seen,finished;

//サイクル復元のための情報
int pos = -1;//サイクル中に含まれる頂点pos
stack<int> hist;//訪問履歴

void dfs(const Graph & G,int v,int p){
  seen[v] = true;
  hist.push(v);
  for(auto nv:G[v]){
    if(nv==p) continue;//逆流を禁止する
    //完全終了した頂点はスルー
    if(finished[nv]) continue;
    //
    if(seen[nv] && !finished[nv]){
      pos = nv;
      return;
    }
    //再帰的に探索
    dfs(G,nv,v);
    //サイクル検出したならばまっすぐに抜けていく
    if(pos!=-1) return;
  }
  hist.pop();
  finished[v] = true;
}

int main(){
  //頂点数(サイクルを一つ含むグラフなので辺数はNで確定)
  int n;cin >> n;
  //グラフ入力受け取り
  Graph G(n);
  for(int i=0;i<n;i++){
    int a,b;cin >> a >> b;
    --a,--b;//頂点番号が1-indexedで与えられるので、0-indexedにする
    G[a].push_back(b);
    G[b].push_back(a);
  }
  seen.assign(n,false),finished.assign(n,false);
  pos = -1;
  dfs(G,0,-1);
  //サイクルを復元
  set<int> cycle;
  while(!hist.empty()){
    int t = hist.top();
    cycle.insert(t);
    hist.pop();
    if(t==pos) break;
  }
  //クエリに答える
  int Q;cin >> Q;
  for(int k=0;k<Q;k++){
    int a,b;cin >> a >> b;
    --a,--b;
    if(cycle.count(a)&&cycle.count(b)) cout << 2 << endl;
    else cout << 1 << endl;
  }
}



Measured memory_bytes: 19160
#pragma region

#include "bits/stdc++.h"
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define _rep3(i, begin, end) for (register int i = (begin), i##_end = (end); i < i##_end; i++)
#define _irep3(i, begin, end) for (register int i = (end)-1, i##_begin = (begin); i >= i##_begin; i--)
#define _rep2(i, n) _rep3(i, 0, n)
#define _irep2(i, n) _irep3(i, 0, n)
#define _overload3(_1, _2, _3, name, ...) name
#define rep(...) _overload3(__VA_ARGS__, _rep3, _rep2, )(__VA_ARGS__)
#define irep(...) _overload3(__VA_ARGS__, _irep3, _irep2, )(__VA_ARGS__)
#define out(x) cout << (x) << '\n';
#define debug(x) cerr << #x << " = " << (x) << " (line: " << __LINE__ << ") " << endl;
typedef long long ll;

template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec) is >> v;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "vec[ ";
  rep(i, vec.size()) i == 0 ? os << vec[i] : os << ", " << vec[i];
  os << " ]";
  return os;
}

#pragma endregion

#define MOD 1000000007
#define INF 2147483647

ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0) u += m;
  return u;
}

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

  ll N;
  cin >> N;
  vector<ll> x(N);
  cin >> x;

  vector<ll> frac(100000);
  frac[0] = 1;
  rep(i, 99999) frac[i + 1] = (frac[i] * (i + 1)) % MOD;

  vector<ll> p(N - 1);
  p[0] = 1;
  rep(i, N - 2) {
    p[i + 1] = (p[i] * (i + 2) + frac[i + 1]) % MOD;
  }
  rep(i, N - 1) {
    p[i] = p[i] * frac[N - 1] % MOD * modinv(frac[i + 1], MOD) % MOD;
  }

  ll ans = 0;
  rep(i, N - 1) {
    ans = (ans + (x[i + 1] - x[i]) * p[i]) % MOD;
  }
  out(ans);
}


Measured memory_bytes: 2560
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main(){
	int a,b,ans=0;
	string s1[100],s2[100];
	cin>>a;
	for(int i=0;i<a;i++){
		cin>>s1[i];
	}
	cin>>b;
	for(int i=0;i<b;i++){
		cin>>s2[i];
	}
	for(int i=0;i<a;i++){
		int sum=0;
		for(int j=0;j<a;j++){
			if(s1[i]==s1[j])
				sum++;
		}
		for(int j=0;j<b;j++){
			if(s1[i]==s2[j])
				sum--;
		}
		if(sum>ans)
			ans=sum;
	}
	cout<<ans<<endl;
  return 0;
}


Measured memory_bytes: 256
#include <map>
#include <set>
#include <list>
#include <cstdio>
#include <cmath>
#include <deque>
#include <stack>
#include <bitset>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
 
using namespace std;
 
#define fr first
#define sc second
#define mp make_pair
#define foreach(c, it) for(typeof(c) it = (c).begin(); it != (c).end(); it++)
 
typedef long long int64;
typedef pair< int, int > iP;
typedef pair< iP, int > iiP;
 
const int MAX = 1000000;
const int INF = 2 << 28;
const double EPS = 1e-10;
 
int p[4 * MAX + 1], op[4  *MAX + 1];
vector < int > num;
int main() {
     
    for(int i = 0; i < 2 + MAX + 1; i++)
        p[i] = op[i] = INF;
     
    for(int i = 1; i < 200; i++) {
        num.push_back(i * (i + 1) * (i + 2) / 6);
        p[i * (i + 1) * (i + 2) / 6] = 1;
        if((i * (i + 1) * (i + 2) / 6) % 2) op[i * (i + 1) * (i + 2) / 6] = 1;
    }
 
    for(int i = 1; i <= MAX; i++) {
        for(int j = 0; j <num.size(); j++) {
            if(p[i] != INF) {
                p[i + num[j]] = min(p[i] + 1, p[i + num[j]]);
            }
            if(op[i]  != INF && num[j] % 2) {
                op[i + num[j]] = min(op[i] + 1, op[i + num[j]]);
            }
        }
    }
     
    int N;
    while(cin >> N, N) {
        cout << p[N] << " " << op[N] << endl;
    }
}

Measured memory_bytes: 23736
N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10**9+7

def solve():
    A.sort(key=lambda x: abs(x), reverse=True)

    ans = 1
    nneg = 0
    a, b, c, d = -1, -1, -1, -1
    for k in range(K):
        ans = (ans * A[k])%MOD
        if A[k] < 0:
            nneg += 1
            b = k
        else:
            a = k
    
    if K == N or nneg%2 == 0:
        return ans
    
    for k in range(N-1, K-1, -1):
        if A[k] < 0:
            d = k
        else:
            c = k

    # b must be >= 0
    if a == -1 and c == -1: # all minus
        ans = 1
        for k in range(K):
            ans = (ans * A[-1-k])%MOD
        return ans
    
    if a == -1 or d == -1:
        outn = A[b]
        inn = A[c]
    elif c == -1:
        outn = A[a]
        inn = A[d]
    else:
        if A[a]*A[c] > A[b]*A[d]:
            outn = A[b]
            inn = A[c]
        else:
            outn = A[a]
            inn = A[d]

    ans = (ans * pow(outn, MOD-2, MOD))%MOD
    ans = (ans * inn)%MOD
    return ans

if __name__ == "__main__":
    print(solve())


Measured memory_bytes: 33304
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+50;
int t,n,m;
int arr[maxn];
struct node
{
	ll x;int id;
}poi[2050];
bool comp(node a,node b)
{
	if(a.x!=b.x) return a.x>b.x;
	return a.id>b.id;
}
ll dp[2050][2050];
int main()
{
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>poi[i].x;
		poi[i].id=i;
	}
	sort(poi+1,poi+1+n,comp);
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<=i;j++)
		{
			ll id=poi[i].id;
			ll x=poi[i].x;
			if(i-1>=j) dp[i][j]=max(dp[i][j],dp[i-1][j]+abs(id-(n-i+1+j))*x);
			if(j>0) dp[i][j]=max(dp[i][j],dp[i-1][j-1]+abs(id-j)*x);
		}
	}
	ll fin=0;
	for(int i=0;i<=n;i++)
	{
		fin=max(fin,dp[n][i]);
	}
	cout<<fin<<endl;
	return 0;
}

Measured memory_bytes: 26480
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;

int n;
vector<int> G[MAX];
int color[MAX];

void dfs(int r, int c){
	stack<int> S;
	S.push(r);
	color[r] = c;
	while( !S.empty()){
		int u = S.top(); S.pop();
		for(int i = 0; i < G[u].size(); i++){
			int v = G[u][i];
			if(color[v] == NIL){
				color[v] = c;
				S.push(v);
			}
		}
	}
}

void assignColor(){
	int id = 1;
	for(int i = 0; i < n; i++) 
		color[i] = NIL;
	for(int u = 0; u < n; u++){
		if(color[u] == NIL)
			dfs(u, id++);
	}
}

int main(){
	int s, t, m, q;

	cin >> n >> m;
	for(int i = 0; i < m; i++){
		cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}

	assignColor();

	cin >> q;

	for(int i = 0; i < q; i++){
		cin >> s >> t;
		if(color[s] == color[t]){
			cout << "yes" << endl;
		} else {
			cout << "no" << endl;
		}
	}

	return 0;
}

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

int main()
{
    string a;
    int ans = 0;
    cin >> a;
    
    if (a.at(0) == 'S') {
        ans = 0;
        if (a.at(1) == 'R') { 
            ans = 1;
            if (a.at(2) == 'R') ans = 2;
        } else {
            if (a.at(2) == 'R') ans = 1;
        }
    } else {
        ans = 1;
        if (a.at(1) == 'R') { 
            ans = 2;
            if (a.at(2) == 'R') ans = 3;
       }
    }
    
    cout << ans << endl;
}

Measured memory_bytes: 3600
/*
 * main.c
 *
 *  Created on: 2019/07/21
 *      Author: family
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

typedef unsigned long long int uint64_t;

int main()
{
	uint64_t N = 0, ans = 0;
	scanf("%llu", &N);
	if (N%2 == 1) {
		ans = N * ((N-1)/2);
	} else {
		ans = (N+1) * N/2;
		ans -= N;
	}
	printf("%llu\n", ans);
    return 0;
}


Measured memory_bytes: 128
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstdio>
#include<map>
#define  For(a,b)    for(int a=0;a<b;a++)
#define  si(a)       scanf("%d",&a);
#define  sl(a)       scanf("%lld",&a);
#define  sii(a,b)    scanf("%d%d",&a,&b);
#define  sll(a,b)    scanf("%lld%lld",&a,&b);
#define  queues      priority_queue
#define mod 1000000007
#define mem(a)  memset(a,0,sizeof(a));
typedef long long ll;
const int INF=0x3f3f3f3f;
//queues<int,vector<int >,greater<int > >q;
using namespace std;

int main()
{
    int a,b;
    cin>>a>>b;
    if(b%a)cout<<b-a;
    else cout<<a+b;
}

/******************************************
**        **             ********       **
**       ** **           **             **
**      **   **          **             **
**     **     **         **             **
**    ***********        **             **
**   **         **       ********       **
******************************************/


Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string>
#include <tuple>
#include <vector>
#include <map>
#include <unordered_map>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <cassert>
using namespace std;
#define LL long long
#define MP(a, b) make_pair(a, b)
#define POWER9 1000000000
#define MOD POWER9+7
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483647
#define INT_MAX 2147483647
#define LL_MIN (LL)-9223372036854775807
#define LL_MAX (LL)9223372036854775807
#define PI 3.14159265359

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

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

	if(sum % (N*(N+1)/2) != 0){
		cout << "NO" << endl;
		return 0;
	}
	LL t = sum / (N*(N+1)/2);

	multiset<LL> st;
	for(int i=1; i<N; i++) st.insert(A[i]-A[i-1]-t);
	st.insert(A[0]-A[N-1]-t);

	auto itr = st.begin();
	LL bef = *itr;
	t -= (-*itr)/N;
	itr++;
	while(itr != st.end()){
		if(abs(*itr-bef)%N != 0){
			cout << "NO" << endl;
			return 0;
		}
		t -= (-*itr)/N;
		if(t < 0){
			cout << "NO" << endl;
			return 0;
		}
		bef = *itr;
		itr++;
	}
	if(t > 0){
		cout << "NO" << endl;
		return 0;
	}
	cout << "YES" << endl;

	return 0;
}


Measured memory_bytes: 5760
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <sstream>
#include <cmath>
#include <math.h>
#include <string>
using namespace std;
typedef long long ll;
typedef double d;
typedef int i;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n , a[100000] = {} , b , x = 0 , y = 0;
    cin >> n;
    for ( int i = 0 ; i < n ; i++ ) {
        cin >> b;
        a[b]++;
    }
    for ( int i = 0 ; i < 100000 ; i++ ) {
        if ( a[i] % 2 == 1 ) x++;
        else if ( a[i] % 2 == 0 && a[i] > 0 ) y++;
    }
    if ( y % 2 == 0 ) cout << x + y;
    else cout << x + y - 1;
}


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

n = 3
e = [[int(i) for i in input().split()] for i in range(n)]

for path in e:
    if 1 in path:
        path.sort()
        next_city = path[1]

        e.remove(path)
        break



for path in e:
    if next_city in path:
        if next_city == path[0]:
            next_city = path[1]
        else:
            next_city = path[0]

        e.remove(path)
        break


for path in e:
    if next_city in path:
        if next_city == path[0]:
            next_city = path[1]
        else:
            next_city = path[0]
        e.remove(path)
        break


if len(e) == 0:
    print('YES')
else:
    print('NO')

Measured memory_bytes: 3064
import collections

s = list(input())
counter_s = collections.Counter(s)
if counter_s["N"] != 0 and counter_s["S"] == 0:
    print("No")
elif counter_s["S"] != 0 and counter_s["N"] == 0:
    print("No")
elif counter_s["W"] != 0 and counter_s["E"] == 0:
    print("No")
elif counter_s["E"] != 0 and counter_s["W"] == 0:
    print("No")
else:
    print("Yes")


Measured memory_bytes: 9416