akhauriyash/Code-Regression huggingface dataset:
/*
memo for solution
*/
#include <cstdlib> 
#include <numeric>
#include <cstring>
#include <cstdio> 
#include <cfloat> 
#include <map> 
#include <cassert>
#include <cmath> 
#include <climits> 
#include <utility> 
#include <set> 
#include <iostream> 
#include <memory> 
#include <string> 
#include <vector> 
#include <algorithm> 
#include <functional> 
#include <sstream> 
#include <complex> 
#include <stack> 
#include <queue> 
#include <iomanip>
#include<sys/time.h>
using namespace std; 
double e_time(void){static struct timeval now;gettimeofday(&now, NULL);return (double)(now.tv_sec  + now.tv_usec/1000000.0);}
#define REP(i,b,n) for(int i=b;i<n;i++) 
#define rep(i,n)      REP(i,0,n) 
#define pb push_back  
#define mp make_pair 
#define ALL(C)   (C).begin(),(C).end() 
#define fe(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr != (c).end();itr++)
#define BITSHIFT(X)     ( (1<<(X)) )
#define CONTAIN(S,X) ( ((S)&BITSHIFT(X)) != 0)
typedef complex<double>P; 
typedef long long ll; 
typedef unsigned long long ull; 
typedef pair<int,int> pii; 
typedef vector<int> vint;
template<class T> void vp(T &a,int p){rep(i,p)cout << a[i]<<" ";cout << endl;}  
template<class T> T ceilUp(const T& a,const T& b){return (a+b-1)/b;}
const ll mod =   1000000009;
inline ll modadd(ll a,ll b){a %= mod;b %= mod;return (a+b)%mod;}
inline ll modsub(ll a,ll b){a %= mod;b %= mod;a-=b;a%=mod;if (a<0)a+=mod;return a;}
inline ll modmul(ll a,ll b){a %= mod;b %= mod;return (a*b)%mod;}
inline ll modeq(ll a,ll b,ll m){a = (a%m+m)%m; b = (b%m+m)%m;return a == b;}
//ll modpow(ll n,ll p){if (p == 0)return 1;if (p == 1)return n;ll ret=mypow(n,p/2);ret=ret*ret%mod;if (p&1)ret=ret*n%mod;return ret;}
//ll getinv(ll a){return mypow(a,mod-2);}


ll solve(const int n, vector<ll> & a){
  map<ll,int> M;
  rep(i, n){
    ll b = a[i] - i;
    M[b]++;
  }

  vector<ll> cnt;
  for(auto itr = M.begin(); itr != M.end();itr++){
    cnt.emplace_back((*itr).first);
  }

  ll left = 0, right = n;
  ll mini = 0, sum = 0;
  for(int i = 0;i < cnt.size();i++){
    mini += M[cnt[i]] * (cnt[i] - cnt[0]);
  }
  sum = mini;
  //cout << cnt.size() <<" " << mini << endl;

  for(int i = 1;i < (int)cnt.size();i++){
    ll prev = cnt[i-1];
    left += M[cnt[i-1]];
    right -= M[cnt[i-1]];
    sum = sum + left * (cnt[i] - cnt[i-1]) - right * (cnt[i] - cnt[i-1]);
    //cout << left <<" " << (cnt[i] - cnt[i-1]) <<" " << right <<" " << (cnt[i] - cnt[i-1]) << endl;
    mini = min(mini, sum);
  }

  return mini;
}

int main(){
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i,n){
    cin >> a[i];
  }
  cout << solve(n, a) << endl;
  return 0;
}
 
 


Measured memory_bytes: 16500
import java.util.Arrays;
import java.util.Scanner;


public class Main {
	static boolean[] isDist;
	static int N;
	static int M;
	static int p;
	public static int INF = 1 << 24;
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		N = stdIn.nextInt();
		M = stdIn.nextInt();
		p = stdIn.nextInt();
		Limit = N*50;
		isDist = new boolean[N];
		for(int i = 0; i < M ;i++) {
			int d = stdIn.nextInt();
			isDist[d] = true;
		}
		boolean[] isDist2 = Arrays.copyOf(isDist,N);
		
		solv(1,p,0,0,false,isDist);
		solv(-1,p,0,0,false,isDist2);
		System.out.println(min);
		
	}
	static int min = 1 << 24; 
	static int Limit;
	public static void solv(int vec, int now, int coun, int cos, boolean alC,boolean[] isDist) {
		
		int nowp = now;
		int cost = cos;
		int count = coun;
		while(true) {
			if(min <= cost)return;
			if(nowp == N) nowp = 0;
			else if(nowp == -1) nowp = N-1;
			if(count == M ) {
				if(min > cost)min = cost;
				return;
			}
			if(isDist[nowp]) {
				
				isDist[nowp] = false;
				count++;
				if(!alC && cost < Limit) {
					boolean[] isDist2 = Arrays.copyOf(isDist, N);
					
					solv(vec*-1,nowp+vec*-1,count,cost+100,true,isDist2);
				}
			}
			if(count == M ) {
				if(min > cost)min = cost;
				return;
			}
			while(true) {
				if(nowp == N) nowp = 0;
				else if(nowp == -1) nowp = N-1;
				if(!isDist[nowp]) {
					nowp += vec;
					cost += 100;
				}
				else {
					break;
				}
			}
		}
	}
}

  

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

int main(){
  string s; cin >> s;
  if (s.at(0)==s.at(1) || s.at(1)==s.at(2) || s.at(2)==s.at(3)){
    cout << "Bad" << endl;
  }else cout << "Good" << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int MOD=1e9+7;
const long double Pi=acos(-1);
int main(){
  ll N,ans=1;
  cin>>N;
  vector<ll> vec(N);
  for(ll i=0;i<N;i++){
    cin>>vec.at(i);
  }
  sort(vec.begin(),vec.end());
  if(vec.at(0)==0){cout<<0<<endl;return 0;}
  else{
    for(ll i=0;i<N;i++){
      //cout<<vec.at(i)<<endl;
      if(1000000000000000000/ans<vec.at(i)){cout<<-1<<endl;return 0;}
      ans*=vec.at(i);
      //if(ans>1000000000000000000){cout<<-1<<endl;return 0;}
    }
  }
  cout<<ans<<endl;
}


Measured memory_bytes: 3984
N = int(input())
dlist = list(map(int,input().split()))

dlist.sort()

mannaka = dlist[N//2-1] + 1
tugi = dlist[N//2]

#print(dlist)                                                                  

#print(mannaka,tugi)                                                           
print(tugi - mannaka + 1)

Measured memory_bytes: 14428
#include<bits/stdc++.h>
using namespace std;
int a[200009];
int main()
{
    int n,x,i,j,k;
    cin >> n >> x;
    if(n==1)
    {
        if(x==1) cout << "Yes" << endl << 1 << endl;
        else cout << "No" << endl;
    }
    else if(n==2)
    {
        if(x==2) cout << "Yes" << endl << 1 << endl << 2 << endl << 3 << endl;
        else cout << "No" << endl;
    }
    else
    {
        if(x==1 || x==(2*n-1))
            cout << "No" << endl;
        else
        {
            cout << "Yes" << endl;
            a[n] = x;
            if(x==(2*n-2))
            {
                a[n-1]=x+1;
                a[n-2] = x-1;
                a[n+1] = x-2;
                j=x-3;
                k=x+2;
            }
            else
            {
                a[n-1]=x-1;
                a[n-2]=x+1;
                a[n+1]=x+2;
                j=x-2;
                k=x+3;
            }
            for(i=1;i<=2*n-1;i++)
            {
                if(a[i]==0)
                {
                    if(j>=1)
                    {
                        a[i]=j;
                        j--;
                    }
                    else if(k<=2*n-1)
                    {
                        a[i]=k;
                        k++;
                    }
                }

            }
            for(i=1;i<=2*n-1;i++)
            {

                cout << a[i] << endl;
            }

        }

    }

}


Measured memory_bytes: 2304
// Atcoder150 - B
function main(input) {
  var inputs = input.split('\n');
  var n = parseInt(inputs[0], 10);
  var s = inputs[1];

  s += 'X' + 'X';
  var sArr = s.split('');

  var counter = 0;
  for (var i = 0; i < sArr.length - 2; i++) {
    if (sArr[i] === 'A') {
      counter += sArr[i + 1] === 'B' && sArr[i + 2] === 'C' ? 1 : 0;
    }
  };
  console.log(counter);
}

main(require('fs').readFileSync('/dev/stdin', 'utf8'));


Measured memory_bytes: 21064
package main

import (
	"fmt"
	"sort"
)

func main() {
	var N, H int
	if _, err := fmt.Scan(&N, &H); err != nil {
		return
	}
	var A []int
	var B []int
	count := 0

	for i := 0; i < N; i++ {
		var tmpA, tmpB int
		if _, err := fmt.Scan(&tmpA, &tmpB); err != nil {
			return
		}
		A = append(A, tmpA)
		B = append(B, tmpB)
	}

	sort.Ints(A)
	sort.Ints(B)
	maximumVal := A[N-1]

	for j:=N-1;j>=0;j--{
		if maximumVal > B[j]{
			break
		}
		H-= B[j]
		count++
		if H <= 0{
			fmt.Println(count)
			return
		}
	}

	if H%maximumVal == 0 {
		fmt.Println(count + H/maximumVal)
	} else {
		fmt.Println(count + H/maximumVal + 1)
	}
}


Measured memory_bytes: 7552
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

//4方向ベクトル→↑←↓
int dx[] ={1,0,-1,0};
int dy[] ={0,-1,0,1};
double rad;
double a,b,L,h,S;
double c;
int main()
{
  cin >>a>>b>>rad;
  rad = M_PI*rad/180.0;
  h =b*sin(rad);
  S =a*h/2.0;
  c =sqrt(a*a+b*b-2*a*b*cos(rad));
  L = a+b+c;
  cout <<fixed<<setprecision(10)<<S<<endl<<L<<endl<<h<<endl;
  return 0;
}

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

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
// #define int long long
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000               // 2e9
#define LLINF 2000000000000000000ll  // 2e18 (llmax:9e18)
#define fi first
#define sec second
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))
#define dmp(x) cerr << #x << ": " << x << endl;

template <class T>
void chmin(T &a, const T &b) {
  if (a > b) a = b;
}
template <class T>
void chmax(T &a, const T &b) {
  if (a < b) a = b;
}

template <class T>
using MaxHeap = priority_queue<T>;
template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <class T>
vector<T> vect(int len, T elem) {
  return vector<T>(len, elem);
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << p.fi << ',' << p.sec;
  return os;
}
template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.fi >> p.sec;
  return is;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i];
    if (i + 1 < vec.size()) os << ' ';
  }
  return os;
}
template <class T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) is >> vec[i];
  return is;
}
void fastio() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
}

#define endl "\n"

vector<pair<ll, int>> factorize(ll x) {
  ll tmp = x;
  vector<pair<ll, int>> ret;
  for (ll i = 2ll; i * i <= x; i++) {
    int cnt = 0;
    while (tmp % i == 0ll) {
      tmp /= i;
      cnt++;
    }
    if (cnt > 0) ret.push_back(pair<ll, int>(i, cnt));
  }
  if (tmp != 1ll) ret.push_back(pair<ll, int>(tmp, 1));
  return ret;
}

void solve() {
  int N;
  cin >> N;
  map<ll, int> m;
  for (int i = 2; i <= N; i++) {
    auto f = factorize(i);
    for (auto p : f) {
      if (m.find(p.first) == m.end()) m[p.first] = 0;
      m[p.first] += p.second;
    }
  }
  ll ans = 1ll;
  for (auto p : m) { ans = ans * (1 + p.second) % 1000000007; }
  cout << ans << endl;
  return;
}

signed main() {
  fastio();
  solve();
  // int t; cin >> t; while(t--)solve();
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <iomanip>
#define ll long long

using namespace std;

long long MOD = 998244353;

template<typename T>
void cout_vec(vector<T> &vec){
    for(int i=0; i<vec.size(); i++){
        if (i!=0){
            cout << ' ';
        }
        cout << vec[i];
    }
    cout << endl;
}
template<typename T>
void cout_mat(vector<vector<T>  > &mat){
    for(int i=0; i<mat.size(); i++){
        vector<T> vec = mat[i];
        cout_vec(mat[i]);
    }
}

int main(){
    int N, S;
    cin >> N >> S;
    vector<ll> A(N);
    for(auto &a:A){
        cin >> a;
    }
    sort(A.begin(),A.end());

    vector<ll> dp(S+1);
    dp[0]=1;
    for(int i=0; i<N; i++){
        for(int k=S; k>=0; k--){
            if(k+A[i]<=S){
                dp[k+A[i]]+=dp[k];
                dp[k+A[i]]%=MOD;
            }
            dp[k]=dp[k]*2%MOD;
        }
    }
    //cout_vec(dp);
    /*
    vector<ll> p2(3001,1);
    for(int i=0; i<3000; i++){
        p2[i+1]=p2[i]*2%MOD;
    }

    ll ans=0;
    for(int i=0; i<=N; i++){
        //cout << dp[i][S] << endl;
        ans+=dp[i][S]*p2[N-i]%MOD;
        ans%=MOD;
    }
    */
    cout << dp[S] << endl;
}

Measured memory_bytes: 3672
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#pragma GCC diagnostic error "-std=c++11"
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define esp 1e-9
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
typedef long long ll;
ll n,k,l,r;
ll ans=0;
int main()
{
    scanf("%lld%lld",&n,&k);
    for(int i=1;i<=n;i++)
    {
        l=n/k+(n%k>=(k-(i%k)));
        r=0;
        if((i%k)==((k-(i%k))%k)){
            r=l;
        }
        ans+=l*r;
    }
    printf("%lld\n",ans);
    return 0;
}

Measured memory_bytes: 256
package main

import "fmt"

func main() {
  var N, M int
  fmt.Scanf("%d %d\n", &N, &M)
  H := make([]int, N)
  good := make([]int, N)
  for i := 0; i < N; i++ {
    fmt.Scanf("%d", &H[i])
    good[i] = 1
  }
  
  for i := 0; i < M; i++ {
    var a, b int
    fmt.Scanf("%d %d\n", &a, &b)
    if H[a-1] <= H[b-1] {
      good[a-1] = 0
    }
    if H[a-1] >= H[b-1] {
      good[b-1] = 0
    }
  }
  
  cnt := 0
  for i := 0; i < N; i++ {
    if good[i] == 1 {
      cnt++
    }
  }
  fmt.Println(cnt)
}

Measured memory_bytes: 6504
#include<bits/stdc++.h>
#define ll long long
#define N 100005
using namespace std;
int n,ans;
vector<int>s[N];
int root,h[N],f[N],g[N];
void dfs(int x,int fa)
{
	for(auto i:s[x])
	{
		if(i!=fa)
		{
			dfs(i,x);
			f[x]|=f[i];
			if(!f[i])g[x]++;
		}
	}
	if(g[x]>1)ans+=g[x]-1,f[x]=1;
	return ;
}
void dffs(int x,int fa)
{
	for(auto i:s[x])
	{
		if(i!=fa)
		{
			h[i]=h[x]+1;
			dffs(i,x);
		}
	}
	if(h[x]>h[root])root=x;
}
int main()
{
	scanf("%d",&n);
	for(int i=0;i<=n-2;i++)
	{
		int t1,t2;scanf("%d%d",&t1,&t2);
		t1++;t2++;
		s[t1].push_back(t2);
		s[t2].push_back(t1);
	}
	dffs(1,-1);
	dfs(root,-1);
	cout<<ans+1<<endl;
	return 0;
}

Measured memory_bytes: 13184
#include<bits/stdc++.h>
using namespace std;
#define db(x){if(cond)cerr<<__LINE__<<" "<<#x<<" " <<x<<endl;}
#define rep(i,b)for(auto i=0;i<(b);++i)
#define fo(i,a,b)for(auto i=(a);i<=(b);++i)
#define ford(i,a,b)for(auto i=(a);i>=(b);--i)
#define vv vector
#define pb push_back
template<typename X>void MA(X&a,X b){a=max(a,b);}template<typename X>void MI(X&a,X b){a=min(a,b);}
template<typename X>void clr(X&x,int a){memset(x,a,sizeof(a));};typedef long long ll;typedef long double ld;
int cond=0,multi=0,gcj=0;

const int MOD=(int)1e9+7;
const int MAXN=(int)1e5+11;
ll A[MAXN];
ll POS[MAXN];

ll SILNIA[MAXN],REV[MAXN];

ll pod(ll a,ll b){
  ll pot=a;
  ll res=1;
  while(b){
    if(b&1)res=(res*pot)%MOD;
    pot=(pot*pot)%MOD;
    b/=2;
  }
  return res;
}

void solve(){
  SILNIA[0]=1;
  fo(i,1,MAXN-1)SILNIA[i]=(i)%MOD;
  fo(i,0,MAXN-1)REV[i]=pod(SILNIA[i],MOD-2);
  fo(i,0,6)db(i<<" "<<SILNIA[i]<<" "<<REV[i]<<" "<<(SILNIA[i]*REV[i])%MOD);

  int N;
  cin>>N;
  N++;
  fo(i,1,N)cin>>A[i];
  int a=0,b=0;
  fo(i,1,N){
    if(POS[A[i]]==0)POS[A[i]]=i;
    else {
      int p1=POS[A[i]];
      int p2=i;

      a=p1-1;
      b=N-p2;
    }
  }
  ll nk=1;
  ll nk2=1;
  db(a<<" "<<b);
  fo(k,1,N){
    nk=((nk*(SILNIA[N-k+1]))%MOD*REV[k])%MOD;
    ll ans=(nk-nk2+MOD)%MOD;
    db(k<<" "<<nk<<" "<<nk2);

    cout<<ans<<"\n";
    if(a+b+1<=k)nk2=0;
    else
      nk2=((nk2*(SILNIA[a+b-k+1]))%MOD*REV[k])%MOD;
  }
  db(N<<" "<<a<<" "<<b);

}

























































int main(int argc,char** argv){
  ios::sync_with_stdio(false),cin.tie(0),cond=argc>=2&&argv[1][0]=='q'?1<<30:0;
  cout.setf(ios::fixed),cout.precision(10);int t;if(multi||gcj)cin>>t;else t=1;
  fo(i,1,t){if(cond)cerr<<__LINE__<<" "<<i<<endl;if(gcj)cout<<"Case #"<<i<<": ";
    solve();
  }return 0;}


/*
stringstream io;
#define cin io
int init(){
  io<<1<<endl;
  int N=500000;
  io<<N<<" "<<N<<endl;
  fo(i,1,N-1)io<<i-1<<" ";io<<endl;
  fo(i,0,N-1)io<<i%2<<" ";io<<endl;
  fo(i,0,N-1)io<<(N-1-i)/2<<" ";io<<endl;

  return 0;
}
int xx=init();
*/


Measured memory_bytes: 4352
from functools import lru_cache


@lru_cache(maxsize=None)
def fib(n):
    if n < 2:
        return n
    return fib(n-1) + fib(n-2)


class Fibonacci(object):
    memo = [1, 1]

    def get_nth(self, n):
        if n < len(Fibonacci.memo):
            return Fibonacci.memo[n]
        # print('fib({0}) not found'.format(n))
        for i in range(len(Fibonacci.memo), n+1):
            result = Fibonacci.memo[i-1] + Fibonacci.memo[i-2]
            Fibonacci.memo.append(result)
            # print('fib({0})={1} append'.format(i, result))
        return Fibonacci.memo[n]


if __name__ == '__main__':
    # ??????????????\???
    num = int(input())

    # ?????£??????????????°????¨????
    #f = Fibonacci()
    #result = f.get_nth(num)
    result = fib(num+1)

    # ???????????????
    print('{0}'.format(result))

Measured memory_bytes: 8300
#include<stdio.h>
int f[44];
void fibonacci(int n) 
{
  int i;
  f[0]=f[1]=1;
  for(i=2; i<=n; i++) f[i] = f[i-2] + f[i-1];
}

int main() 
{
  int n;

  scanf("%d",&n);
  fibonacci(n);
  printf("%d\n",f[n]);
  return 0;
}

Measured memory_bytes: 596
n = int(input())
A = list(map(int, input().split()))
ans = 1
flag = True

while True:
    for i in range(n):
        if A[i] % (2 ** ans) != 0:
            flag = False
            break

    if flag == False:
        break
    ans = ans + 1

print(ans - 1)

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

int main() {
    int k, n;
    cin >> k >> n;
    int a[n];
    rep(i, n) cin >> a[i];

    int ans = a[n - 1] - a[0];
    for (int i = 1; i < n; i++) {
        ans = min(ans, k - a[i] + a[i - 1]);
    }

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


Measured memory_bytes: 1024
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
#define itn int
#define endl '\n'
#define co(ans) cout<<ans<<endl
#define COYE cout<<"YES"<<endl
#define COYe cout<<"Yes"<<endl
#define COye cout<<"yes"<<endl
#define CONO cout<<"NO"<<endl
#define CONo cout<<"No"<<endl
#define COno cout<<"no"<<endl
#define FORE(i,a) for(auto &i:a)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RREP(i,n)  FFOR(i,1,n)
#define PB push_back
#define MP make_pair
#define ALL(V) (V).begin(),(V).end()
#define SORT(V) sort((V).begin(),(V).end())
#define REVERSE(V) reverse((V).begin(),(V).end())
#define EACH(V,i) for(typeof((V).begin()) i=(V).begin();i!=(V).end();++i)
#define equals(a,b) (fabs((a)-(b))<EPS)
#define INF ((1LL<<62)-(1LL<<31))
#define EPS 1e-10
#define PI 3.141592653589793238
#define MOD 1000000007
#define MAX 5100000
using namespace std;
using Edge=pair<int,int>;
using Graph=vector<vector<int>>;
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();}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
const int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

////////// 二項係数(N<=10^7) //////////

ll fac[MAX],finv[MAX],inv[MAX];
// fac[i] i!
// finv[i] i!の逆元
// inv[i] iの逆元

// テーブルを作る前処理
void COMinit(){
  fac[0]=fac[1]=1;
  finv[0]=finv[1]=1;
  inv[1]=1;
  for(ll i=2; i<MAX; ++i){
    fac[i]=fac[i-1]*i%MOD;
    inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;
    finv[i]=finv[i-1]*inv[i]%MOD;
  }
}

// 二項係数計算
ll COM(ll n,ll k){
  if(n<k) return 0;
  if(n<0 || k<0) return 0;
  return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD;
}

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

int main(){
  COMinit();
  ll N,M,ans=0;
  cin>>N>>M;
  
  ll DP[N+1];
  REP(i,N+1){
    DP[i]=COM(N,i)*COM(M-i,N-i)%MOD*fac[N-i]%MOD;
    if(i%2==0) ans+=DP[i];
    else ans-=DP[i];
    ans%=MOD;
  }
  
  for(int i=M; i>M-N; --i){
    ans*=i;
    ans%=MOD;
  }
  if(ans<0) ans+=MOD;
  co(ans);
  return 0;
}

Measured memory_bytes: 126964
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();
        int[] c = new int[M];
        for (int i = 0; i < M; i++) {
            c[i] = sc.nextInt();
        }
        Arrays.sort(c);

        int[] dp = new int[N+1];
        for (int i = 0; i < N+1; i++) {
            dp[i] = Integer.MAX_VALUE;
        }
        dp[0] = 0;

        for (int coin : c) {
            for (int i = 0; i < N; i++) {
                if (i + coin > N) continue;
                dp[i + coin] = Math.min(dp[i + coin], dp[i] + 1);
            }
        }

        System.out.println(dp[N]);
    }
}


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

#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)

using ll = long long;
#define INF 1e9




int main(){ 
  string S, T;
  cin >> S >> T;
  vector<int> cvt(26, -1), flag(26, 0);
  bool ans = true;
  REP(i, S.size()){
    int s = S[i] - 'a', t = T[i] - 'a';
    if(cvt[s] != -1){
      if(cvt[s] != t){
        ans = false;
      }
    }
    else if(flag[t] == 0){
        flag[t] = 1;
        cvt[s] = t;
    }
    else if(flag[t] == 1){
      ans = false;
    }
  }
  if(ans){
    cout << "Yes" << endl;
  }
  else{
    cout << "No" << endl;
  }
  return 0;
}



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

int main() {
  int a,b,c,d;
  cin >> a >> b >> c >> d;
  
  if(max(a,c) - min(a,c) <= d){
    cout << "Yes" << endl;
  }
  
  else if(max(a,b) - min(a,b) > d || max(b,c) - min(b,c) > d){
    cout << "No" << endl;
  }
  
  else{
   cout << "Yes" << endl;
  }
}


Measured memory_bytes: 256
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
#define ull unsigned long long
using namespace std;
void io() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

const int MAXN = 1e5 + 5;
const ll INF = 0x3f3f3f3f3f3f3f;
const ll prim = 1e9 + 7;

struct Node {
    int t;
    int id;

    bool operator<(const Node& oth) { return t < oth.t; }
} s[MAXN];

int N;

int main() {
    io();
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> s[i].t;
        s[i].id = i + 1;
    }
    sort(s, s + N);
    for (int i = 0; i < N; i++) {
        cout << s[i].id << ' ';
    }
}

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

using namespace std;

template <class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')
    cerr<<*sdbg++;
  cerr<<"="<<h<<","; 
  _dbg(sdbg+1, a...);
}

template<class T> ostream & operator<<(ostream & os, vector<T> V){
  os<<"[";
  for(auto vv: V) os << vv <<",";
  return os << "]";
}
template<class L, class R> ostream & operator <<(ostream & os, pair<L,R> P){
  return os <<"("<<P.first <<","<<P.second <<")";
}

#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

const int N = 100005;

int x, y, a, b, c, red[N], green[N], colorless[N];


int main() {
    
    scanf("%d %d %d %d %d", &x, &y, &a, &b, &c);
    
    for (int i = 1; i <= a; i++) {
        scanf("%d", &red[i]);
    }
    for (int i = 1; i <= b; i++) {
        scanf("%d", &green[i]);
    }
    for (int i = 1; i <= c; i++) {
        scanf("%d", &colorless[i]);
    }
    
    sort(red + 1, red + 1 + a);
    reverse(red + 1, red + 1 + a);
    
    sort(green + 1, green + 1 + b);
    reverse(green + 1, green + 1 + b);
    
    sort(colorless + 1, colorless + 1 + c);
    reverse(colorless + 1, colorless + 1 + c);
    
    long long currentCost = 0;
    
    for (int i = 1; i <= x; i++) {
        currentCost += red[i];
    }
    
    for (int i = 1; i <= y; i++) {
        currentCost += green[i];
    }
    
    long long ans = currentCost;
    
    int ptrA = x;
    int ptrB = y;
    for (int i = 1; i <= min(c, x + y); i++) {
        currentCost += colorless[i];
        if (ptrB == 0 || (ptrA > 0 && red[ptrA] < green[ptrB])) {
            currentCost -= red[ptrA];
            ptrA--;
        } else {
            currentCost -= green[ptrB];
            ptrB--;
        }
        ans = max(ans, currentCost);
    }
    
    printf("%lld\n", ans);
    
    return 0;
}


Measured memory_bytes: 1408
from collections import deque

N = int(input())
dic = {}
lnum = [0] * N
q = deque([])
alis = [0] * N


for i in range(N-1):

    a,b = map(int,input().split())
    a -= 1
    b -= 1

    lnum[a] += 1
    lnum[b] += 1

    if a not in dic:
        dic[a] = []

    if b not in dic:
        dic[b] = []

    dic[a].append(b)
    dic[b].append(a)

for i in range(N):

    if lnum[i] == 1:
        q.append(i)

c = list(map(int,input().split()))
c.sort()
#print (c)
for i in range(N):

    now = q.popleft()
    lnum[now] = 0

    alis[now] = c[i]
    #print (q,now,alis)

    for nex in dic[now]:

        lnum[nex] -= 1
        if lnum[nex] == 1:
            q.append(nex)

print (sum(c) - c[-1])
print (" ".join(map(str,alis)))


Measured memory_bytes: 51544
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
 
int main() {
  long long n;
  cin>>n;
  vector<long long>a(n);
  for(long long i=0;i<n;i++)cin>>a[i];
  long long kotae=0;
  long long count=0;
  for(long long i=0;i<n-1;i++){
    if(a[i+1]<=a[i]){
      count++;
      kotae=max(kotae,count);
    }else{
      count=0;
    }
  }
  cout<<kotae<<endl;
  return 0;
}

Measured memory_bytes: 1024
#include <iostream>
#include <vector>
#include<algorithm>
#include<string>
#include<stdio.h>
#include<map>
#include<math.h>
#include<queue>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
const int mod = 1000000007;
const int INF = 1000000001;
const long long LINF = 1e18;
const int MAX = 510000;

int main() {
	int n, m;
	cin >> n >> m;
	vector<string>s(n);
	for (int i = 0; i < n; i++) {
		cin >> s.at(i);
	}
	string su;
	sort(s.begin(), s.end());
	for (int i = 0; i < n; i++) {
		su += s.at(i);
	}
	cout << su << endl;
	return 0;
}

Measured memory_bytes: 3636
require 'set'; require 'prime'
INF=Float::INFINITY; MOD=10**9+7
n = gets.chomp.to_i
orig_n = n

ks = [1]
k = 1
while true
  ksum = k * (k+1) / 2
  ks[k] = ksum
  if ksum > n
    break
  end
  k += 1
end
# p ks

size = ks.size
ans = []
ks.reverse.each_with_index do |k, i|
  index = size - i
  # p [n,index,k]
  if n > k
    n -= index
    ans.push index
  end
end
if ans.inject(&:+) != orig_n
  ans.push 1
end

ans.reverse_each do |e|
  puts e
end

Measured memory_bytes: 2556
#include<iostream>
#include <algorithm>
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#define MAX 2147483647;
using namespace std;
int n;
int arr[100005];
struct Node {
	int min;
	int lowerLimit;
	int upperLimit;
	struct Node *left;
	struct Node *right;
};
Node* makeSegmentTree(int lower, int upper) {


	Node* node = (Node*)malloc(sizeof(Node));
	node->lowerLimit = lower;
	node->upperLimit = upper;

	if (lower == upper) {
		node->left = NULL;
		node->right = NULL;
		node->min = arr[lower];
		return node;
	}

	int middle = (lower + upper) / 2;
	node->left = makeSegmentTree(lower,middle);
	node->right = makeSegmentTree(middle+1,upper);
	node->min = min(node->left->min, node->right->min);

	return node;
}
int findMin(Node *node,int lower, int upper) {
	if (lower <= node->lowerLimit && upper >= node->upperLimit) {
		//printf("[%d,%d]:%d", node->lowerLimit, node->upperLimit,node->min);
		return node->min;
	}
	int middle = (node->lowerLimit + node->upperLimit) / 2;
	int minLeft = MAX;
	int minRight = MAX;

	//rintf("middle:%d\n", middle);

	if (lower <= middle) {
		minLeft = findMin(node->left, lower, upper);
	}
	if (upper >= (middle + 1)) {
		minRight = findMin(node->right,lower, upper);
	}
	return min(minLeft, minRight);
}
void update(Node *node,int position,int value) {
	if (node->lowerLimit == node->upperLimit) {
		node->min = value;
		
		return;
	}
	int middle = (node->upperLimit + node->lowerLimit) / 2;
	if (position <= middle) {
		update(node->left, position, value);
	}
	else {
		update(node->right, position, value);
	}
	int leftMin = node->left->min;
	int rightMin = node->right->min;
	node->min = min(leftMin, rightMin);
}
void preorder(Node *node) {
	if (node == NULL) {
		return;
	}
	printf("[%d,%d]:%d\n", node->lowerLimit, node->upperLimit,node->min);
	preorder(node->left);
	preorder(node->right);
}
void inorder(Node* node) {
	if (node == NULL) {
		return;
	}
	inorder(node->left);
	printf("[%d,%d]:%d\n", node->lowerLimit, node->upperLimit, node->min);
	inorder(node->right);
}
int main() {
	int q,op,l,r;
	//freopen("D:\\in.txt", "r", stdin);
	scanf("%d %d", &n, &q);

	for (int i = 0; i < n; i++) {
		arr[i] = MAX;
	}
	
	Node* root;
	root = makeSegmentTree(0,n-1);

	while (q--) {
		scanf("%d %d %d", &op, &l, &r);
		if (op == 0) {
			arr[l] = r;
			update(root,l, r);
		}
		else if(op == 1){
			int minQuery = findMin(root,l, r);
			printf("%d\n", minQuery);
		}
		else if (op == 2) {
			preorder(root);
			printf("\n");
		}
		else if (op == 3) {
			inorder(root);
			printf("\n");
		}
	}
}


Measured memory_bytes: 12924
#include<stdio.h>
int main()
{
	int length,n,i,a[25];
	while(scanf("%d",&length),length)
	{
		for(i = 0; i < length; ++i)
			scanf("%d",&a[i]);
		bool isJiro=false;
		i = 0;
		n = 32;
		do
		{
			if(isJiro)
			{
				n -= a[i];
				i = (i+1)%length;
			}
			else
			{
				n -= (n-1)%5;
			}
			printf("%d\n",n>0?n:0);
			isJiro = !isJiro;
		}while(n>0);
	}
	return 0;
}

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

using static Input;
using static Util;
class Program
{
    static int L;
    
    static void INPUT()
    {
        L = NextInt();
    }
    static void OUT()
    {
        var f = (float)L;
        Console.WriteLine(Math.Pow(f / 3, 3));
    }
    static void Main(string[] args)
    {
#if DEBUG  
        TESTINPUT();
        //INPUT();

#else
        INPUT();
#endif
        OUT();
    }

    static void TESTINPUT()
    {
        L = 999;
    }

}

public class Input
{
    public const long MOD = 1000000007;
    public const int INF = 1000000007;
    private static Queue<string> q = new Queue<string>();
    private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); }
    public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); }
    public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); }
    public static string NextString() { Confirm(); return q.Dequeue(); }
    public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); }
    public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
    public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
    public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); }
    public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); }
}

class Util
{
}




Measured memory_bytes: 11220
import itertools
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
lis = [i for i in range(1,n+1)]
zisyo = list(itertools.permutations(lis))
print(abs(zisyo.index(tuple(p)) - zisyo.index(tuple(q))))

Measured memory_bytes: 13856
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <numeric>
#include <queue>
using namespace std;

int64_t min(int64_t a,int64_t b) {
    if (a > b)
    {
        return b ;
    }else
    {
        return a ;
    }
}

int64_t solve(vector<int> a,bool next){
    bool nextposi = next ;
    int64_t ans = 0 ;
    int64_t sum = 0 ;
    for (int i = 0; i < a.size(); i++)
    {
        sum += a.at(i) ;
        if (nextposi != (sum > 0))
        {
            if (nextposi == 1)
            {
                ans += abs(sum - 1) ;
                sum = 1 ;
            }else
            {
                ans += abs(sum + 1) ;
                sum = -1 ;
            }
            
            
        }
        
        nextposi = !nextposi ;
    }
    return ans ;
    
}

int main(){
    int n ;
    cin >> n ;
    vector<int> a(n) ;
    for (int i = 0; i < n; i++)
    {
        cin >> a.at(i) ;
    }
    int64_t ans = 0 ;
    if (a.at(0) == 0)
    {
        a.at(0) = 1 ;
        ans = min(solve(a,0),solve(a,1)) ;
        a.at(0) = -1 ;
        int64_t ansp = min(solve(a,0),solve(a,1)) ;
        ans = min(ans,ansp) + 1;
    }else
    {
        ans = min( solve(a,0), solve(a,1) ) ;
    }
    
    cout << ans << endl ;
}


Measured memory_bytes: 4156
#include <bits/stdc++.h>
#define rep(i ,n) for(int i=0;i<(int)(n);++i)
using namespace std;
typedef long long int int64;
typedef unsigned long long uint64;

int main(){
    int n,q;
    cin >> n >> q;
    vector<vector<int>> a(n);
    rep(i ,q){
        int s , t , x;
        cin >> s;
        if ( s == 0 ){
            cin >> t >> x;
            a[t].push_back(x);
        } else if ( s == 1 ){
            cin >> t;
            int j = 0;
            if ( a[t].size() == 0 ) cout << endl;
            else {
                while( j <=(signed int)(a[t].size()-2) )
                    { cout << a[t].at(j) << " "; ++j; }
                if( (signed int)a[t].size() >= 1 ) cout << a[t].at(a[t].size()-1) << endl;
            } 
        } else {
            cin >> t;
            a[t].clear();
        }
    }
    return 0;
}


Measured memory_bytes: 5816
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<cstring>
#include<math.h>
#include<bitset>
#include<queue>
#include<set>
#include<iomanip>
#include<math.h>
#include<assert.h>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr long long int INFLL = 1001001001001001LL;
constexpr int INF = 1000000007;
const int mod = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};


ll dp[100050][4];
int main(){
  memset(dp,0,sizeof(dp));
  int N;
  cin >> N;
  vector<int> A(N+1),B(N+1),C(N+1);
  for(int i=1; i<=N; i++){
    cin >> A[i] >> B[i] >> C[i];
  }
  for(int i=1; i<=N; i++){
    dp[i][1] = max(dp[i-1][2]+A[i],dp[i-1][3]+A[i]);
    dp[i][2] = max(dp[i-1][1]+B[i],dp[i-1][3]+B[i]);
    dp[i][3] = max(dp[i-1][1]+C[i],dp[i-1][2]+C[i]);
  }
  cout << max({dp[N][1],dp[N][2],dp[N][3]}) << endl;
  return 0;
}


Measured memory_bytes: 4608
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
using ll = long long int;
using namespace std;

int main()
{
    int N;
    cin >> N;
    vector<ll> A(N);
    cin >> A[0];
    ll total = A[0];
    for (int i = 1; i < N; i++)
    {
        cin >> A[i];
        total += A[i];
        A[i] += A[i - 1];
    }
    ll ans = total;
    for (int i = 0; i < N - 1; i++)
    {
        ll tmp = total - A[i];
        if (abs(tmp - A[i]) < ans)
        {
            ans = abs(tmp - A[i]);
        }
    }
    cout << ans << endl;

    return 0;
}

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

#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define debug(x) cerr << #x << ": " << x << ", "
#define debugln(x) cerr << #x << ": " << x << '\n'

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

using namespace std;
using ll = long long;
using P = pair<ll, ll>;

//constexpr ll mod = 998244353;
constexpr ll mod = 1e9+7;
const double PI = acos(-1.0);
mt19937_64 engine(chrono::steady_clock::now().time_since_epoch().count());

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int a, b; cin >> a >> b;
  if (a <= 5) {
    b = 0;
  } else if (a <= 12) {
    b /= 2;
  }
  cout << b << endl;
}

Measured memory_bytes: 256
use std::cmp::{max, min};
use std::io::{stdin, Read};

macro_rules! parse {
    ($iter: ident) => {
        $iter.next().unwrap().parse().unwrap()
    };
    ($iter: ident,2) => {
        (parse!($iter), parse!($iter))
    };
    ($iter: ident,3) => {
        (parse!($iter), parse!($iter), parse!($iter))
    };
    ($iter: ident,4) => {
        (parse!($iter), parse!($iter), parse!($iter), parse!($iter))
    };
    ($iter: ident, $n: ident) => {
        $iter.take($n).map(|e| e.parse().unwrap()).collect();
    };
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    let mut iter = s.split_whitespace();
    let mut val: Vec<u32> = iter
        .next()
        .unwrap()
        .chars()
        .map(|x| x.to_digit(10).unwrap())
        .collect();
    let n = val.len();
    val.reverse();
    let mut dp0 = vec![std::u32::MAX / 2; n + 1];
    let mut dp1 = vec![std::u32::MAX / 2; n + 1];
    dp0[0] = 0;
    for (i, &v) in val.iter().enumerate() {
        dp0[i + 1] = min(dp0[i] + v, dp1[i] + v + 1);
        dp1[i + 1] = min(dp0[i], dp1[i] - 1) + 10 - v;
    }
    println!("{}", min(dp0[n], dp1[n] + 1));
}


Measured memory_bytes: 19704
//https://atcoder.jp/contests/abc120/tasks/abc120_d
#include <bits/stdc++.h>
using namespace std;
using ll=int64_t;
#define FOR(i,a,b) for(int64_t i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#if LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif

struct uf_tree {
    std::vector<int> parent;
    int __size;
    uf_tree(int size_) : parent(size_, -1), __size(size_) {}
    void unite(int x, int y) {
        if ((x = find(x)) != (y = find(y))) {
            if (parent[y] < parent[x]) std::swap(x, y);
            parent[x] += parent[y];
            parent[y] = x;
            __size--;
        }
    }
    bool is_same(int x, int y) { return find(x) == find(y); }
    int find(int x) { return parent[x] < 0 ? x : parent[x] = find(parent[x]); }
    int size(int x) { return -parent[find(x)]; }
    int size() { return __size; }
};

int main() {
    ll N,M; cin>>N>>M;
    vector<pair<int,int>> abm(M);
    for(auto&& ab:abm){
        cin>>ab.first>>ab.second;
    }

    uf_tree ut(N+1);
    //ll a=0;
    vector<ll> ans;
    for(ll i=M-1;i>=0;--i){ //逆から
        //ll a=0;
        // FOR(sima,1,N+1){
        //     dump(sima,ut.size(sima));
        //     a+=ut.size(sima)-1;
        // }
        // ans.push_back(a/2);

        const auto& dec=abm.at(i);

        ll pa=ut.size(dec.first);
        ll pb=ut.size(dec.second);

        ut.unite(dec.first,dec.second);
        //dump(ut.size(dec.first),ut.size());
        ll aa=ut.size(dec.first);
        ll ab=ut.size(dec.second);
        dump(pa,pb,aa,ab);
        ll a = (aa-pa)*(ab-pb);
        ans.push_back(a);
    }

    //ll nc2=N*(N-1)/2;
    ll a=0;
    reverse(ans.begin(),ans.end());
    for(const auto& o:ans){
        //cout<<nc2-o<<endl;
        a+=o;
        cout<<a<<endl;
    }
    return 0;
}

Measured memory_bytes: 3444
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); i++)
using namespace std;
using ll = long long;
int main()
{
    int H, W, K;
    cin >> H >> W >> K;
    vector<int> c(H*W);
    rep(i, H){
        rep(j, W){
            char s;
            cin >> s;
            if(s=='.') c[i*W+j] = 0;
            else c[i*W+j] = 1;  
            // cout << c[i*W+j];
        }
        // cout << endl;
    }
    int ans = 0;
    rep(maskR, 1<<H){
        rep(maskC, 1<<W){
            int cnt = 0;
            rep(i, H){
                rep(j, W){
                    if((maskR>>i) & 1 ==1 && ((maskC>>j)&1) ==1 && c[i*W+j]==1){
                        cnt++;
                    }
                }
            }
            if (cnt == K) ans++;
        }
    }

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

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

int main()
{
	int a,b,c,d,e,f;
	scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
	int maxw = 0,maxs = 0;
	for(int aa = 0;aa * a * 100 <= f;++aa)
		for(int bb = 0;aa * a * 100 + bb * b * 100 <= f;++bb) {
			int water = 100 * (a * aa + b * bb);
			int maxsugar = min(f - water,(a * aa + b * bb) * e);
			for(int cc = 0;c * cc <= maxsugar;++cc)
				for(int dd = 0;c * cc + d * dd <= maxsugar;++dd)
					if(maxs == 0 || maxs * water < (c * cc + d * dd) * maxw) {
						maxs = c * cc + d * dd,maxw = water;
					}
		}
	printf("%d %d\n",maxs+maxw,maxs);
}

Measured memory_bytes: 256
#include<stdio.h>
#define MAX 100005
#define NIL -1
struct Node_struct{int p, l, r;}; /*p: 親(parent), l: 一番左の子(left child), r:右の兄弟(right sibling)*/
typedef struct Node_struct Node;
Node T[MAX];
int D[MAX];
void print(int u){
 int i, c;
 printf("node %d: parent = %d, depth = %d, ", u, T[u].p, D[u]);
 if(T[u].p==-1) printf("root, ");
 else if(T[u].l==NIL /*&& T[T[u].l].r==NIL*/) printf("leaf, ");
 else printf("internal node, ");
 printf("[");
 for (i=0, c = T[u].l; c != NIL; i++, c = T[c].r){
   if(i) printf(", ");
   printf("%d", c);
 }
 printf("]\n");
}
void calcDepth(int u, int p){
 D[u] = p;
 if(T[u].r!=NIL) calcDepth(T[u].r,p);
 if(T[u].l!=NIL) calcDepth(T[u].l,++p);
}
int main(){
 int i, j, k;
 int n;
 int id, sib, child;
 int root = NIL;
 scanf("%d", &n);
 for(i=0; i<n; i++) T[i].p = T[i].l = T[i].r = NIL;
 for(i=0; i<n; i++){
   scanf("%d %d", &id, &k);
   for(j = 0, sib = NIL; j<k; j++){ //子を入力する (input children)
     scanf("%d", &child);
     T[child].p = id;
     if(j == 0) T[id].l = child; //idの一番左の子 (the most left child of id)
     else T[sib].r = child; //左の兄弟に繋げる (connect to left sibling)
     sib = child;//次の子供の左の兄弟 (the left sibling of the next child)
   }
 }
 //根を求める (find the root)
 for(i=0; i<n; i++){
   if(T[i].p == -1) root = i;
 }
 //それぞれのノードの深さを求める (find depth of each node)
 calcDepth(root, 0);
 //出力 (output)
 /*s
 for(i=0; i<n; i++)
 printf("lc:%d, r:%d\n",T[i].l,T[i].r);
 e*/
 for(i=0; i<n; i++) print(i);
 return 0;
}



Measured memory_bytes: 6468
#include <bits/stdc++.h>
// debug
#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl;
#define ddd(x, y, z) cerr << "(" #x "," #y "," #z "):(" << x << "," << y << "," << z << ")" << endl;
#define dump(v)              \
  cerr << #v ":[ ";          \
  for (auto macro_vi : v)    \
  {                          \
    cerr << macro_vi << " "; \
  }                          \
  cerr << "]" << endl;
#define ddump(v)                     \
  cerr << #v ":" << endl;            \
  for (auto macro_row : v)           \
  {                                  \
    cerr << "[";                     \
    for (auto macro__vi : macro_row) \
    {                                \
      cerr << macro__vi << " ";      \
    }                                \
    cerr << "]" << endl;             \
  }
#define damp(m)                                                   \
  for (auto macro_pair : m)                                       \
  {                                                               \
    cerr << macro_pair.first << ":" << macro_pair.second << endl; \
  }
#define dpair(p) cerr << #p ":" \
                      << "(" << p.first << "," << p.second << ")" << endl;
// iterate
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define itr(macro_v, i) macro_v.begin() + i
// etc
#define chmin(mi, val) mi = min(mi, val);
#define chmax(ma, val) ma = max(ma, val);
using ll = long long;

using namespace std;

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;
  for (int x = 0; x <= 999; x++)
  {
    int c1 = x / 100, c2 = (x / 10) % 10, c3 = x % 10;
    int pos = 0;
    while (pos < s.size() and (s[pos] - '0') != c1)
    {
      pos++;
    }
    if (pos < s.size())
    {
      pos++;
    }
    while (pos < s.size() and (s[pos] - '0') != c2)
    {
      pos++;
    }
    if (pos < s.size())
    {
      pos++;
    }
    while (pos < s.size() and (s[pos] - '0') != c3)
    {
      pos++;
    }
    if (pos == s.size())
    {
      continue;
    }
    else
    {
      // cout << pos << endl;
      cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}

Measured memory_bytes: 512
#include <bits/stdc++.h>
#define rep(i, n) for(ll i = 0; i < n; i++)
#define repr(i, n) for(ll i = n; i >= 0; i--)
#define reprr(i, m, n) for (ll i = m; i < n; i++)
#define inf 2e9
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;


int main()
{
    ll N, M; cin >> N >> M;
    ll maxn = 1;
    for (ll i = 1; i <= sqrt(M); i++) {
        if (M % i) continue;
        ll x = i, y = M / i;
        if (N <= x && x <= M) maxn = max(maxn, y);
        if (N <= y && y <= M) maxn = max(maxn, x);
    }
    cout << maxn << endl;
    return 0;
}

Measured memory_bytes: 256
N = int(input()) #入力する整数
A = list(map(int,input().split())) #入力する数列A
SUMA = sum(A) #数列の和
MOD = 10**9 + 7 # mod
C = [0] * (N-1) #累積和数列
for i in range(N-1): #\sum_{j = i+1}^{N}を求めて数列に代入する
  SUMA -= A[i]
  C[i] = SUMA
ans = 0 #求める答え
for i in range(N-1):
  ans += A[i]*C[i]
  ans %= MOD #その都度modで割った余りにする
print(ans) #答えを出力する

#計算量はO(N)です。


Measured memory_bytes: 31452
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int> >;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reprep(i,j,h,w) rep(i,h)rep(j,w)
#define rrep(i,m,n) for(int i = m; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define aall(x,n) (x).begin(),(x).begin()+(n)
#define VEC(type,name,n) std::vector<type> name(n);rep(i,n)std::cin >> name[i];
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define mp make_pair
#define sum accumulate
#define keta fixed<<setprecision
#define vvector(name,typ,m,n,a)vector<vector<typ> > name(m,vector<typ> (n,a))
#define vvvector(name,t,l,m,n,a) vector<vector<vector<t> > > name(l, vector<vector<t> >(m, vector<t>(n,a)));
#define vvvvector(name,t,k,l,m,n,a) vector<vector<vector<vector<t> > > > name(k,vector<vector<vector<t> > >(l, vector<vector<t> >(m, vector<t>(n,a)) ));
typedef long long ll;
const int INF = 2000000000;
const ll INF64 = 1000000000000000ll;
const ll MOD = 1000000007LL;


int main(){
    string s;
    std::cin >> s;
    if(s.size()==2)std::cout << s << std::endl;
    else{
        std::cout << s[2]<<s[1]<<s[0] << std::endl;
    }
}

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

int main()
{
	int N, M;
	cin >> N >> M;
	vector<ll> a(N), b(N), c(M), d(M);
	for (int i = 0; i < N; i++) {
		cin >> a[i] >> b[i];
	}
	for (int i = 0; i < M; i++) {
		cin >> c[i] >> d[i];
	}
	for (int i = 0; i < N; i++) {
		ll dis = 1000000000000000000, res;
		for (int j = 0; j < M; j++) {
			ll dd = abs(a[i] - c[j]) + abs(b[i] - d[j]);
			if (dd < dis) {
				res = j + 1;
				dis = dd;
			}
		}
		cout << res << endl;
	}
	return 0;
}


Measured memory_bytes: 256