akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(var, begin, end) for (int var = (begin); var <= (end); var++)
#define RFOR(var, begin, end) for (int var = (begin); var >= (end); var--)
#define REP(var, length) FOR(var, 0, length - 1)
#define RREP(var, length) RFOR(var, length - 1, 0)
#define EACH(value, var) for (auto value : var)
#define SORT(var) sort(var.begin(), var.end())
#define REVERSE(var) reverse(var.begin(), var.end())
#define RSORT(var) \
  SORT(var);       \
  REVERSE(var)
#define OPTIMIZE_STDIO         \
  ios::sync_with_stdio(false); \
  cin.tie(0);                  \
  cout.precision(10);          \
  cout << fixed
#define endl '\n'

const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
struct mint {
  ll x;
  mint() : x(0) {
  }
  mint(ll x) : x((x % MOD + MOD) % MOD) {
  }
  mint& operator+=(const mint& r) {
    if ((x += r.x) >= MOD) {
      x -= MOD;
    }
    return *this;
  }
  mint& operator-=(const mint& r) {
    if ((x -= r.x) < 0) {
      x += MOD;
    }
    return *this;
  }
  mint& operator*=(const mint& r) {
    if ((x *= r.x) > MOD) {
      x %= MOD;
    }
    return *this;
  }
  mint& operator++() {
    if ((++x) >= MOD) {
      x -= MOD;
    }
    return *this;
  }
  mint operator++(int) {
    mint ret = x;
    if ((++x) >= MOD) {
      x -= MOD;
    }
    return ret;
  }
  mint& operator--() {
    if ((--x) < 0) {
      x += MOD;
    }
    return *this;
  }
  mint operator--(int) {
    mint ret = x;
    if ((--x) < 0) {
      x += MOD;
    }
    return ret;
  }
  mint operator+(const mint& r) {
    mint ret;
    return ret = x + r.x;
  }
  mint operator-(const mint& r) {
    mint ret;
    return ret = x - r.x;
  }
  mint operator*(const mint& r) {
    mint ret;
    return ret = x * r.x;
  }
  mint operator-() {
    return mint() - *this;
  }
  bool operator<(const mint& a) {
    return x < a.x;
  }
  bool operator>(const mint& a) {
    return x > a.x;
  }
  bool operator<=(const mint& a) {
    return x <= a.x;
  }
  bool operator>=(const mint& a) {
    return x >= a.x;
  }
  bool operator==(const mint& a) {
    return x == a.x;
  }
  bool operator!=(const mint& a) {
    return x != a.x;
  }
};
ostream& operator<<(ostream& os, const mint& r) {
  os << r.x;
  return os;
}
ostream& operator>>(ostream& os, const mint& r) {
  os >> r.x;
  return os;
}

void solve(istream& cin, ostream& cout) {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> G(n);
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].emplace_back(b);
    G[b].emplace_back(a);
  }
  vector<int> v(n);
  REP(i, n) v[i] = i;
  int ans = 0;
  do {
    if (v[0] != 0) continue;
    bool visited_all = true;
    REP(i, n - 1) {
      bool visited = false;
      int u = v[i];
      for (int nu : G[u]) {
        if (nu == v[i + 1]) visited = true;
      }
      if (!visited) visited_all = false;
    }
    if (visited_all) ans++;
  } while (next_permutation(v.begin(), v.end()));
  cout << ans << endl;
}

#ifndef TEST
int main() {
  OPTIMIZE_STDIO;
  solve(cin, cout);
  return 0;
}
#endif


Measured memory_bytes: 256
import java.util.Scanner;
class Main{
    public static void main(String arg[]){
        Scanner scn=new Scanner(System.in);
        String text =scn.next();
        String key = scn.next();
        text+=text;
        String answer = "No";
        for(int i=0; i<(text.length() - key.length()) ; i++){
            String buf = text.substring(i, i+key.length());
            if(buf.equals(key)){
                answer="Yes";
                break;
            }
        }
        System.out.println(answer);
    }
}

Measured memory_bytes: 22996
def f(n):
    ans = 0
    while n > 0:
        ans += 1
        n = n % bin(n).count('1')
    return ans

def main():
    n = int(input())
    x = input()

    c1 = x.count('1')
    cc2 = int('0b' + x, 0) % (c1 + 1)

    if c1 == 1:
        for i in range(n):
            if x[i] == '1':
                print(0)
            else:
                print(f((pow(2, n-i-1, c1+1) + cc2) % (c1 + 1)) + 1)
        exit()

    cc1 = int('0b' + x, 0) % (c1 - 1)

    for i in range(n):
        if x[i] == '1':
            print(f((-pow(2, n-i-1, c1-1) + cc1) % (c1 - 1)) + 1)
        else:
            print(f((pow(2, n-i-1, c1+1) + cc2) % (c1 + 1)) + 1)

main()

Measured memory_bytes: 9460
K,N = map(int,input().split())
A_list = list(map(int,input().split()))
A_list.append(K + A_list[0])
A_split = []
for i in range(N):
    A_split.append(A_list[i+1]-A_list[i])

A_split.pop(A_split.index(max(A_split)))
print(sum(A_split))

Measured memory_bytes: 26444
//  Copyright © 2020 Diego Garcia Rodriguez del Campo. All rights reserved.
#include<bits/stdc++.h>
#define MAX 105
#define INF 1e18
#define optimiza_io cin.tie(0); ios_base::sync_with_stdio(0);
#define what_is(x) cerr << #x << " is " << x << endl;
using namespace std;
typedef long long ll;

ll N, M, X, c[MAX], a[MAX][MAX];

int main()
{
    optimiza_io
    cin >> N >> M >> X;
    for( ll i = 0; i < N; i ++ ) {
        cin >> c[i];
        for( ll j = 0; j < M; j ++ )
            cin >> a[i][j];
    }
    
    ll ans = INF;
    for( ll msk = 0; msk < ( 1 << N ); msk ++ ) {
        vector < ll > skill(M,0);
        ll tmp = 0;
        for( ll i = 0; i < N; i ++ )
            if( msk & ( 1 << i ) ) {
                tmp += c[i];
                for( ll j = 0; j < M; j ++ )
                    skill[j] += a[i][j];
            }
        bool ok = 1;
        for( ll j = 0; j < M; j ++ )
            ok &= skill[j] >= X;
        if( ok )
            ans = min( ans , tmp ); 
    }
    ans = ans == INF ? -1 : ans;
    cout << ans << "\n";
    
    return 0;
}

// CHECAR LIMITES 

Measured memory_bytes: 3664
#include <bits/stdc++.h>
#define REP(i, n) for (long long i = 0; i < (long long)n; i++)
//#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Vl = vector<ll>;
using vi = vector<int>;
using Graph = vector<vi>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(15); //"for float-base
  // problem"
  int N, K;
  cin >> N >> K;
  vi A(N);
  REP(i, N) cin >> A.at(i);
  if (N == K) {
    cout << 1 << endl;
    return 0;
  }
  int count = 0;
  for (auto a : A) {
    if (a == 1)
      break;
    else
      count++;
  }
  bool flag = (count < N - count - 1);
  int mod1 = count % (K - 1);
  int mod2 = (N - count - 1) % (K - 1);
  if (mod1 != 0 && mod1 < mod2) {
    cout << (count + (K - 1 - 1)) / (K - 1) +
                (N - count - 1 - (K - mod1) + (K - 1 - 1)) / (K - 1)
         << endl;
  } else if (mod2 != 0 && mod1 >= mod2) {
    cout << (count - (K - mod2) + (K - 1 - 1)) / (K - 1) +
                (N - count - 1 + (K - 1 - 1)) / (K - 1)
         << endl;
  } else {
    cout << (count + (K - 1 - 1)) / (K - 1) +
                (N - count - 1 + (K - 1 - 1)) / (K - 1)
         << endl;
  }
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1e9
#define PI 3.14159265359
#define MOD 1000000007
#define ALL(v) v.begin(),v.end()
#define ALLR(v) v.rbegin(),v.rend()
#define F first
#define S second
typedef long long ll;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
//isPrime
//modpow modinv
//getDigit
template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i,n) cin >> a[i];
  ll ans = 0;
  rep(i,n-1){
    if(a[i]>a[i+1]){
      ans += a[i]-a[i+1];
      a[i+1]=a[i];
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 3992
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    string a;
    cin >> a;
    int n = a.size();
    int r = 1000, con = 753;
    for (int i = 0; i + 2 < n; ++i)
    {
        int l = (a[i] - '0') * 100 + (a[i + 1] - '0') * 10 + (a[i + 2] - '0');
        r = min(r, (int) abs(con - l));
    }
    cout << r;
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

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

    ll n, t;
    cin >> n >> t;

    vector<ll> a(n);

    ll mn_val = 1001001001;
    ll mx_range = 0;
    bool flg = false;
    for(int i = 0; i < n; ++i){
        cin >> a[i];
        
        if(a[i] < mn_val){
            mn_val = a[i];
            flg = true;
        }

        else if(flg){
            mx_range = max(mx_range, a[i]-mn_val);
        }
    }

    // ll gain = t/2*mx_range;
    // cout << gain << endl;

    mn_val = 1001001001;
    ll res = 0;
    for(int i = 0; i < n; ++i){
        if(a[i] < mn_val){
            mn_val = a[i];
            flg = true;
        }

        else if(flg){
            if(a[i]-mn_val == mx_range) ++res;
        }
    }

    cout << res << endl;

    return 0;
}

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

int main()
{
    int n, sum = 0;
    cin >> n;
    int a1[n], a2[n], dif[n];

    for(int i=0; i<n; i++){
        cin >> a1[i];
    }

    for(int i=0; i<n; i++){
        cin >> a2[i];
    }

    for(int i=0; i<n; i++){
        dif[i] = a1[i] - a2[i];
    }

    for(int i=0; i<n; i++){
        if(dif[i] > 0) sum += dif[i];
    }

    cout << sum << endl;

    return 0;
}

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

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	ll n, i = 100;
	cin >> n;
	ll ans = 0;
	while (i < n) {
		i *= 1.01;
		ans++;
	}
	cout << ans;
}

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

#define MAXSIZE 1000000001
#define ll long long int

using namespace std;

bool findcut(vector<ll>& logs, ll K, ll mid)
{
  for (int i = 0; i < logs.size(); i++){
    K -= (ceil((double)logs[i] / (double)mid) - 1);
    if (K < 0)
      return false;
  }
  return true;
}

ll custom_binary_search(vector<ll>& logs, ll K){
  ll start = 1, end = MAXSIZE, mid;
  while (start < end){
    mid = (start + end) / 2;
    if (findcut(logs, K, mid))
      end = mid;
    else
      start = mid + 1;
  }
  return end;
}

int main(void)
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll N, K;
  cin >> N >> K;
  vector<ll> logs(N);
  for (ll i = 0; i < N; i++)
    cin >> logs[i];
  cout << custom_binary_search(logs, K) << '\n';
  return 0;
}


Measured memory_bytes: 4744
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
using namespace std;

int main(void){
	string ohn,mezikara;
	int p;
	cin>>mezikara;
	p=0;
	while(1){
		ohn="\0";
		cin>>ohn;
		if(ohn=="END_OF_TEXT"){
			break;
		}
		for(int i=0;i<9;i++){
			ohn[i]=tolower(ohn[i]);
		}
		if(ohn==mezikara){
			p++;
		}
	};
	cout<<p<<endl;
	return 0;
}



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

signed main(void)
{
	long long a, b, x, ans;
	cin >> a >> b >> x;

	ans = 0;
	ans += b / x;
	if (a > 0)
		ans -= (a - 1) / x;
	else
		ans++;
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 256
n = int(input())
tmp = []
flag = 0
for i in range(n):
    a = input()
    if a in tmp:
        flag = 1
    else:
        if len(tmp) == 0:
            tmp.append(a)
        else:
            last = tmp[-1][-1]
            tmp.append(a)
            if a[0] != last:
                flag = 1
if flag == 1:
    print('No')
else:
    print('Yes')

Measured memory_bytes: 3064
#include <bits/stdc++.h>
typedef unsigned long long ull;
typedef long long ll;
#define __TRUE "Yes"
#define __FALSE "No"
#define cout(x) cout << (x) << endl
#define MAX 100001
using namespace std;

int main() {
  string s;
  cin >> s;
  ll ans=stoll(s);
  for(ll i=1;i<(1<<(s.size()-1));i++) {
    vector<string> a;
    ll j=0,num=0,count=1,tmp=0;
    while(num+count<=s.size()+1) {
      if( i&(1<<j)) {
        a.push_back(s.substr(num,count));
        num=num+count;
        count=1;
      }else {
        count++;
      }
      j++;
    }
    a.push_back(s.substr(num,count));

    for(int j=0;j<a.size();j++) {
      tmp += stoll(a[j]);
    }
    ans += tmp;
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
N = gets.to_i
K = gets.to_i

min_v = Float::INFINITY

0.upto(N) do |n|
  v = 1

  n.times do
    v *= 2
  end
  (N-n).times do
    v += K
  end

  if v < min_v
    min_v = v
  end
end

puts min_v


Measured memory_bytes: 1788
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections.Generic;

class Program
{
    const string Yes = "Yes";
    const string No = "No";
    const long Mod = 1000000007;
    static void Main(string[] args) {
#if DEBUG
        MyDebugger.Test(process, "../TextFile1.txt");
        //MyDebugger.Test(process, "../TextFile2.txt");
        //MyDebugger.MakeTestCase("../TextFile2.txt");
#else
        object ret = process(new StreamReader(Console.OpenStandardInput()));
        Console.WriteLine(ret);
#endif
    }

    static object process(TextReader input) {
        var n = int.Parse(input.ReadLine());

        long c = 0;
        for(int i=1; i<=n; i++) {
            if (i % 3 != 0 && i % 5 != 0) c+=i;
        }

        return c;
    }
}


Measured memory_bytes: 27448
import sys

def print_data(data):
    last_index = len(data) - 1
    
    for idx, item in enumerate(data):
        print(item, end='')
        if idx != last_index:
            print(' ', end='')

    print('')
        
def main():
    N = int(input())
    data = list(map(int, input().split()))
    counter = 0
    
    for i in range(N):
        j = N -1
        while j >= i+1:
            if data[j] < data[j-1]:
                hoge = data[j]
                data[j] = data[j-1]
                data[j-1] = hoge
                counter += 1
            j -= 1
        
    print_data(data)
    print(counter)
    
            
if __name__ == '__main__':
    main()


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

using ll=long long;

int main() {
    ll i,N,x;
    cin >> N;
    x = N;

    for(i=1; i*i<=N; i++)
        if(N%i == 0)
            x = min(x, i+N/i);
  
  	if(x == N)
      x = N+1;

    cout << x-2 << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<utility>
#include<algorithm>
using namespace std;
int n;
pair<int, int> island[30];
int main() {
	while(cin >> n,n!=0) {
		for (int i = 0; i < n;i++) {
			cin >> island[i].second >> island[i].first;
		}
		sort(island, island + n);
		//1,3,4,5
		int weight = 0;
		bool f = true;
		for (int i = 0; i < n;i++) {
			weight += island[i].second;
			if (weight > island[i].first){
				f = false;
				break;
			}
		}
		if (f) {
			cout << "Yes" << endl;
		}
		else {
			cout << "No" << endl;
		}
	}
	return 0;
}


Measured memory_bytes: 3096
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define fo(i,s,e) for( i=s;i<e;i++)
#define rfo(i,s,e) for(i=s;i>e;i--)
#define LL long long int
const LL MOD=1e9+7;
#define pb push_back
#define pob pop_back()
#define sp " "
#define ff first
#define ss second
//            When something is important enough, you do it even if the odds are not in your favor.

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    // code goes here
    LL t=1;//cin>>t;
    while(t--){
      LL h,w,m,i,j;cin>>h>>w>>m;
      std::vector<pair<LL,LL>> v(m);
      for(auto &x:v) cin>>x.ff>>x.ss;
      std::map<LL,set<LL> > mp;
      std::map<LL, LL> mp1;
      for(auto x:v){
        mp1[x.ss]++;
        mp[x.ff].insert(x.ss);
      }
      LL maxa=-1;
      for(auto x:mp1) maxa=max(maxa,x.ss);
      set<LL> st;
      for(auto x:mp1) if(x.ss==maxa) st.insert(x.ff);
      LL mx=-1;
      for(auto x:mp){
        LL num=0;
        for(auto x1:x.ss){
          if(st.count(x1)){
            num++;
          }
          //mx=max(mx,(LL)x.ss.size()+mp1[x1]-1);cout<<mx<<sp<<x.ff<<sp<<x1<<endl;
        }
        if(num==st.size()) mx=max(mx,(LL)x.ss.size()+maxa-1);
        else mx=max(mx,(LL)x.ss.size()+maxa);
      }
      cout<<mx;
    }
    return 0;
}

Measured memory_bytes: 82864
H, W, K = map(int,input().split())
c = [[0]*W for i in range(H)]
for i in range(H):
    s = input()
    for j in range(W):
        if s[j]=='#':
            c[i][j] = 1
ans=0
from itertools import product
for i in product([0, 1], repeat = H):
    for j in product([0, 1], repeat =W):
        cnt = 0
        for x in range(H):
            for y in range(W):
                if c[x][y] == 1:
                    if i[x] == 0 and j[y] == 0:
                        cnt+=1
        if cnt == K:
            ans+=1
print(ans)

Measured memory_bytes: 9216
import java.util.*;

public class Main {
	 
	public static void main(String[] args) {
		Scanner cin=new Scanner(System.in);
		
		while(true) {
			String str=cin.next();
			if(str.charAt(0)=='-')break;
			//System.out.println(str);
			int m=cin.nextInt();
			for(int i=0;i<m;i++) {
				int h=cin.nextInt();
				String h1=str.substring(h,str.length());
				String h2=str.substring(0, h);
				//System.out.println(h1+" "+h2);
				str=h1+h2;
			}
			
			System.out.println(str);
		}
	}
	
	
}


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

#ifdef _PRINTDEBUG
  #include "lib/printdebug.hpp"
#else
  #define printdebug(...) 1
#endif
#define MOD_1_000_000_007 (1000000007)
#define IINF (1<<28)
#define LINF (1LL<<60)
#define rep(i, n)      REP(i, 0, (n))
#define repc(i, n)     REPC(i, 0, (n))
#define REP(i, n, m)   for (int i = (int)(n); i <  (int)(m); i++)
#define REPC(i, n, m)  for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define yesno(cond)       YESNO((cond), "Yes", "No")
#define YESNO(cond, a, b) cout << ((cond) ? (a) : (b)) << "\n"

namespace solver {
  using namespace std;
  typedef long long ll;

  string s;

  void init() {
    cin >> s;
  }

  void solve() {
    ll a = 0;
    char past2 = 'D', past1 = 'D';
    ll ans = 0;
    rep(i, s.size()) {
      char cur = s[i];
      if(past1 == 'A' && cur == 'A') {
        a++;
        past2 = past1;
        past1 = cur;
      } else if(cur == 'A') {
        a = 1;
        past2 = past1;
        past1 = cur;
      } else if(past1 == 'A' && cur == 'B') {
        past2 = past1;
        past1 = cur;
      } else if(past2 == 'A' && past1 == 'B' && cur == 'C') {
        ans += a;
        past2 = 'C';
        past1 = 'A';
      } else {
        a = 0;
        past2 = past1;
        past1 = cur;
      }
    }
    cout << ans << "\n";
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  solver::init();
  solver::solve();
  return 0;
}


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

""" def nCr(n, r, mod=10**9+7):
    r = min(r, n-r)
    numer = denom = 1
    for i in range(1, r+1):
        numer = numer * (n+1-i) % mod
        denom = denom * i % mod
    return numer * pow(denom, mod-2, mod) % mod """
def cmb(n, k, mod, fac, ifac):
    """
    nCkを計算する
    """
    k = min(k, n-k)
    return fac[n] * ifac[k] * ifac[n-k] % mod


def make_tables(mod, n):
    """
    階乗テーブル、逆元の階乗テーブルを作成する
    """
    fac = [1, 1] # 階乗テーブル・・・(1)
    ifac = [1, 1] #逆元の階乗テーブル・・・(2)
    inverse = [0, 1] #逆元テーブル・・・(3)

    for i in range(2, n+1):
        fac.append((fac[-1] * i) % mod)
        inverse.append((-inverse[mod % i] * (mod//i)) % mod)
        ifac.append((ifac[-1] * inverse[-1]) % mod)
    return fac, ifac

ans = 0
fac,ifac = make_tables(M,N)
for i in range(K-1,N):
    t = cmb(i,K-1,M,fac,ifac)
    ans += t*A[i]
A.sort(reverse=True)
for i in range(K-1,N):
    t = cmb(i,K-1,M,fac,ifac)
    ans -= t*A[i]
print(ans%M)


Measured memory_bytes: 76840
let rec squash tList fList =
    match fList with
    | [] -> tList
    | x :: [ y ] -> if x = y then squash (x :: tList) [] else squash (y :: x :: tList) ([])
    | x :: y :: xx -> if x = y then squash tList (y :: xx) else squash (x :: tList) (y :: xx)

stdin.ReadLine().ToCharArray()
|> Array.toList
|> fun x ->
    let l = x |> squash [] |> List.length
    if l = List.length x then printfn "Good" else printfn "Bad"


Measured memory_bytes: 28996
#include <bits/stdc++.h>
using namespace std;
#define vec(type) vector<type>

int main() {
	int n;
	cin >> n;
	vec(int) a(n);
	for (int i = 0; i < n; i++) cin >> a[i];

	int sum = accumulate(a.begin(), a.end(), 0);
	int ans = -1;
	int diff = INT_MAX;
	for (int i = 0; i < n; i++) {
		if (abs(n*a[i]-sum) < diff) {
			ans = i;
			diff = abs(n*a[i]-sum);
		}
	}
	cout << ans << endl;
}

Measured memory_bytes: 3540
'''
Created on 2016/08/28

@author: diyosko7
'''

if __name__ == '__main__':
    n = 4 
    input_lines = [int(input()) for i in range(n)] 
    N = int(input_lines[0])
    K = int(input_lines[1])
    X = int(input_lines[2])
    Y = int(input_lines[3])
    

    SUM = 0
    for i in range(0,N):
        if i < K:
            SUM += X
        else:
            SUM += Y
    print(SUM)

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

int main(int argc, char const *argv[])
{
	int n;
	int arr[]={105,104,103,102,101,100};
	while(~scanf("%d",&n)){
		while(n%100){
			bool f=false;
			for (int i = 0; i < 6; ++i)
			{
				if (n%100<arr[i]%100 || n<100)
				{
					continue;
				}
				n-=arr[i];
				f=true;
				break;
			}
			if (f==false)
			{
				break;
			}
		}
		if (n%100)
		{
			printf("0\n");
		}
		else{
			printf("1\n");
		}

	}
	return 0;
}

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

#include <cstdlib>  // abs() for integer 絶対値求めやつ

#include <cmath>    // abs() for float, and fabs()

#include <algorithm>
#include <vector>
#define rep(i,n) for(int i = 0; i< (n);i++)

#define sort(a) sort((a).begin(),(a).end());

#define che(a,string) cout<<string<<":"<<(a)<<endl;

#define ch(a,string) cout<<string<<":"<<(a)<<" ";

using namespace std;

typedef pair<int,int> p;

int main(){
	int n;cin>>n;
	int a,b;
	a= n/10;
	b=n%10;
	if(a==9||b==9){
		cout<<"Yes"<<endl;
	  }
	  else{
		  cout<<"No"<<endl;
		}
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pip;
const ll INF = 1e9+100;
const ll MOD = 1e9;
const double EPS = 1e-10;
const bool debug = 0;
//------------------------------//

int N;
int P[1000], X[1000];
int dp[1000][5001]; // 頂点iの部分木で黒の重みがjであるときの白の重みの最小値
vector<int> g[1000];

void dfs(int u) {
	bool f = false;
	REP(i, g[u].size()) {
		dfs(g[u][i]);
		
		if (!f) {
			REP(j, 5001) dp[u][j] = dp[g[u][i]][j];
			f = true;
		}
		else {
			vector<pii> v1, v2;
			REP(j, 5001) if (dp[u][j] != INF) v1.push_back(pii(j, dp[u][j]));
			REP(j, 5001) if (dp[g[u][i]][j] != INF) v2.push_back(pii(j, dp[g[u][i]][j]));
			
			fill(dp[u], dp[u + 1], INF);
			REP(j, v1.size()) REP(k, v2.size()) {
				int sum1 = v1[j].fi + v2[k].fi;
				int sum2 = v1[j].se + v2[k].se;
				if (sum1 <= 5000) chmin(dp[u][sum1], sum2);
			}
		}
	}
	if (g[u].size() == 0) {
		dp[u][X[u]] = 0;
		dp[u][0] = X[u];
		return;
	}
	
	int mi = INF;
	REP(i, X[u] + 1) chmin(mi, dp[u][i]);
	
	// 白
	REP(i, 5001) {
		if (dp[u][i] <= X[u]) dp[u][i] = X[u];
		else dp[u][i] = INF;
	}
	
	// 黒
	chmin(dp[u][X[u]], mi);
}

int main() {
	cin >> N;
	FOR(i, 1, N) scanf("%d", P + i);
	REP(i, N) scanf("%d", X + i);
	
	FOR(i, 1, N) {
		P[i]--;
		g[P[i]].push_back(i);
	}
	
	fill(dp[0], dp[N], INF);
	dfs(0);
	bool ans = false;
	REP(i, 5001) if (dp[0][i] != INF) ans = true;
	puts(ans ? "POSSIBLE" : "IMPOSSIBLE");
	
	return 0;
}


Measured memory_bytes: 19968
from collections import Counter
n=int(input())
a=list(map(int,input().split()))

cnt=Counter(a)
numofcard=len(cnt)
#print(cnt,numofcard)

count=0
for i in cnt.values():
    if i==2:
        count+=1
    elif i>3 and i%2==0:
        count+=1
print(numofcard if count%2==0 else numofcard-1)

Measured memory_bytes: 18656
#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
using namespace std;
using ll = long long;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 3000000000000000000
#define rep(i,n) for(long long i = 0;i < n;++i)    
#define seg_size 524288

int main(){
    int a,b;
    vector<int>n(3);
    rep(i,3) cin>>n[i];
    if(n[0]==n[1]&&n[1]==n[2]){
        cout<<"No"<<endl;
        return 0;
    }
    if(n[0]==n[1]||n[0]==n[2]||n[1]==n[2])cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define sci1(a) scanf("%d",&a)
#define sci2(a,b) scanf("%d %d",&a,&b)
#define sci3(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define scs(s) scanf("%s",s)
#ifndef GET_MACRO
#define GET_MACRO(__1,__2,__3,NAME,...) NAME
#endif // GET_MACRO
#define sci(...) GET_MACRO(__VA_ARGS__,sci3,sci2,sci1)(__VA_ARGS__)
#define ll long long
#define pii pair<int,int>
#define mp(a,b) make_pair(a,b)
#define LSB(i) ((i)&-(i))
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
#define infl  0x7fffffffffffffff
#define infi  0x7fffffff
ll dp[100001][101];
int main(){
    int n,v,W,w,i,j,k,sum=0;
    memset(dp,-1,sizeof(dp));
    vector<pii> vct;
    sci(n,W);
    for(i=0;i<n;i++){
        sci(w,v);
        vct.push_back(mp(v,w));
        sum+=v;
    }
    for(i=0;i<=n;i++){
        dp[0][i]=0;
    }
    for(i=1;i<=sum;i++){
        for(j=1;j<=n;j++){
            dp[i][j]=dp[i][j-1]==-1?-1:dp[i][j-1];
            if((i-vct[j-1].first)<0){
                if(dp[i][j]!=-1)dp[i][j]=min(dp[i][j],(ll)vct[j-1].second);
                else dp[i][j]=vct[j-1].second;
            }
            else{
                if(dp[i-vct[j-1].first][j-1]!=-1){
                    if(dp[i][j]==-1)
                        dp[i][j]=dp[i-vct[j-1].first][j-1]+vct[j-1].second;
                    else   dp[i][j]=min(dp[i][j],dp[i-vct[j-1].first][j-1]+vct[j-1].second);
                }
            }
        }
    }
    int sol=0;
    for(i=1;i<=sum;i++){
        if(dp[i][n]<=(ll)W)sol=i;
    }
    cout<<sol<<endl;
    return 0;
}


Measured memory_bytes: 79232
X,Y = map(int, input().split())
m = (-X+2*Y)//3
n = (2*X-Y)//3

def combination(n, r, mod=10**9+7):
    n1, r = n+1, min(r, n-r)
    numer = denom = 1
    for i in range(1, r+1):
        numer = (numer*(n1-i)) % mod
        denom = (denom*i) % mod
    return numer * pow(denom, mod-2, mod) % mod

if (X+Y)%3 != 0 or m*n<0:
  print(0)
else:
  print(combination(m+n,n))

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

#define REP(i, s, e) for(int i = (int)s; i < (int) e; i++)
#define rep(i, n) REP(i, 0 ,n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define fi first
#define se second
#define pb push_back
#define mp nake_pair

typedef long long ll;

ll d[(1<<13)+10];

int main(){
    int m, w;
    while(~scanf("%d%d", &m, &w)){
        if(m == 0 && w == 0) break;
        vector<int> a(m), b(w);
        rep(i, m) scanf("%d", &a[i]);
        rep(i, w) scanf("%d", &b[i]);
        if(m > w){
            swap(a, b);
            swap(m, w);
        }
        rep(i, (1<<13)+10) d[i] = 0LL;
        ll ans = 0LL;
        rep(mask, 1<<w){
            int k = 0;
            rep(j, w) if(mask & (1<<j)) k++;
            if(k >= m) continue;
            rep(j, w) {
                if(mask & (1<<j)) continue;
                int nxt = mask | (1<<j);
                ll c = abs(a[k]-b[j]);
                d[nxt] = max(d[nxt], d[mask]+c*(c-30)*(c-30));
                ans = max(ans, d[nxt]);
            }
        }
        printf("%lld\n", ans);
    }
}

Measured memory_bytes: 3276
lilis = []
count = 0
try:
  while True:
    lis = list(map(int,input().split()))
    if lis[0]==lis[1]==lis[2]==-1:
      break
    lilis.append(lis)
    count +=1
  for i in range(count):
    if lilis[i][0]+lilis[i][1]>=80:
      print("A")
    
    elif 80>lilis[i][0]+lilis[i][1]>=65:
      print("B")
      
    elif 65>lilis[i][0]+lilis[i][1]>=50:
      print("C")
      
    elif 50>lilis[i][0]+lilis[i][1]>=30:
      if lilis[i][0]==-1 or lilis[i][1]==-1:
        print("F")
      else:
        if lilis[i][2]>=50:
          print("C")
        else:
          print("D")
    elif 30>lilis[i][0]+lilis[i][1]:
      print("F")
      
except EOFError:
  pass


Measured memory_bytes: 5608
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <stack>
#include <deque>
#include <cmath>
#include <map>
using ll = long long;
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<string> a(n), b(m);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < m; i++) cin >> b[i];

  bool flg = false;
  for(int i = 0; i <= n-m; i++) {
    for (int j = 0; j <= n-m; j++) {
      bool f = true;
      for (int k = 0; k < m; k++) {
        for (int l = 0; l < m; l++) {
          f &= a[i+k][j+l] == b[k][l];
        }
      }
      flg |= f;
    }
  }
  if (flg) cout << "Yes" << endl;
  else cout << "No" << endl;
}


Measured memory_bytes: 256
import string

alp = string.ascii_uppercase

def main():
    S = input()
    len_s = len(S)

    if S[0] != 'A':
        print ('WA')
        return

    for i in range(2, len_s-1):
        if S[i] == 'C':
            c_index = i
            break
        if i == len_s-2:
            print ('WA')
            return

    for i in range(1, len_s):
        if i == c_index:
            continue
        if S[i] in alp:
            print ('WA')
            return

    print ('AC')

main()



Measured memory_bytes: 3768
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using bint = boost::multiprecision::cpp_int;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i,n) for (int i = 0; i < (n); i++)
#define repSE(i,s,n) for (int i = (s); i < (n); i++)
#define rrepSE(i,s,e) for (int i = (s); i > (e); i--)
#define ssort(v) sort(v.begin(), v.end())
#define gsort(v) sort(v.rbegin(), v.rend())
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 << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T> bool chmin(T &m, const T q) { if (q < m) {m = q; return true;} else return false; }
#define DEBUG_PRINT(var) std::cout << #var << " = " << var << "\n";

int main()
{
    int h, w, k;
    cin >> h >> w >> k;
    vector<string> c(h);
    rep(i,h) cin >> c[i];
    int ans = 0;
    rep(sel,1<<(h+w)) {
        vector<string> tmp(c);
        rep(i,h) {
            if (sel>>i&1) {
                rep(j,w) tmp[i][j] = 'r';
            }
        }
        rep(j,w) {
            if (sel>>(h+j)&1) {
                rep(i,h) tmp[i][j] = 'r';
            }
        }
        int orgb = 0;
        rep(i,h) rep(j,w) {
            if (tmp[i][j] == '#') orgb ++;
        }
        if (orgb == k) ans ++;
    }
    cout << ans << "\n";
    return 0;
}


Measured memory_bytes: 3616
a=input()
flag=False
ans=0
hold=0
for i in range(3):
    if a[i]=='R' and not flag:
        ans=1
        flag=not flag
        hold=max(hold,ans)

    elif a[i]=='R' and flag:
        ans+=1
        hold=max(hold,ans)

    else:
        ans=0
        flag=False


print(hold)



Measured memory_bytes: 9116
#include<cstdio>

int main()
{
	int n,t,s=0,p=0,x;
	scanf("%d%d",&n,&t);
	while(n--)
	{
		scanf("%d",&x);
		if(p>x)s-=p-x;
		p=x+t,s+=t;
	}
	printf("%d\n",s);
}

Measured memory_bytes: 128
#include<iostream>

using namespace std;

int main()
{
    char a, b;
    cin>>a>>b;
    if(a == 'H') cout<<b<<endl;
    else
    {
        if(b == 'H') cout<<"D"<<endl;
        else cout<<"H"<<endl;
    }
    return 0;
}


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

int main()
{
    int n, tmp;
    cin >> n;
    int array[n] = {0};
    for(int i = 0; i < n - 1; i++){
        cin >> tmp;
        array[tmp - 1]++;
    }
    for(int i = 0; i < n; i++)
        cout << array[i] << endl;

    return 0;
}

Measured memory_bytes: 4408
n,m=map(int,input().split())
a=[[int(i) for i in input().split()] for j in range(n)]
c=[[int(i) for i in input().split()] for j in range(m)]
for x1,y1 in a:
  d=10**9
  n=1
  s=0
  for x2,y2 in c:
    if abs(x1-x2)+abs(y1-y2)<d:
      s=n
      d=abs(x1-x2)+abs(y1-y2)
    n+=1
  print(s)

Measured memory_bytes: 3064
import string
N=int(input())
A=string.ascii_letters
ans=["a"]*N
Ans=[1]*N

def search(n):
    if n!=N-1:
        for i in range(max(Ans)+1):
            Ans[n]=i+1
            ans[n]=A[i]
            search(n+1)
            Ans[n+1:]=[1]*(N-n-1)
    else:
        for i in range(max(Ans)+1):
            Ans[n]=i+1
            ans[n]=A[i]
            print("".join(ans))

if N==1:
    print("a")
else:
    search(1)

Measured memory_bytes: 4984
#include<stdio.h>
#include<math.h>

char lower(char c);

int main(){
  double a,b,C,c2,cr;
  double S,L,h;

  scanf("%lf %lf %lf", &a,&b,&C);
  cr = 2.0*M_PI*C/360.0;
  S = sin(cr)*a*b/2.0;
  c2 = a*a+b*b-2.0*a*b*cos(cr);
  L = a + b + sqrt(c2);
  h = S*2.0/a;
  printf("%f\n%f\n%f\n",S,L,h);

  return 0;
}


Measured memory_bytes: 2072
#include<bits/stdc++.h>
#define R(a) int a;cin>>a;
#define deb(x) cout<<#x<<" "<<x<<endl;
#define mod 1000000007
#define f_arr(n) for(int i=0;i<n;i++)
#define r_arr(n) for(int i=n-1;i>=0;i--)
#define pb push_back
#define all(x) x.begin(),x.end()
#define inf 1e18
#warning uncomment the #define int line before submitting
#define int long long int
using namespace std;

#ifndef ONLINE_JUDGE
template<typename T>    
void __p(T a) {
  cerr<<a;
}
template<typename T, typename F>
void __p(pair<T, F> a) {
  cerr<<"[";
  __p(a.first);
  cerr<<",";
  __p(a.second);
  cerr<<"]";
}
template<typename T>
void __p(std::vector<T> a) {
  cerr<<"[";
  for(auto it=a.begin(); it<a.end(); it++)
    __p(*it),cerr<<",]"[it+1==a.end()];
}
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) {
  __p(a1);
  __p(a...);
}
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
  cerr<<name<<" : ";
  __p(arg1);
  cerr<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
  int bracket=0,i=0;
  for(;; i++)
    if(names[i]==','&&bracket==0)
      break;
    else if(names[i]=='(')
      bracket++;
    else if(names[i]==')')
      bracket--;
  const char *comma=names+i;
  cerr.write(names,comma-names)<<" : ";
  __p(arg1);
  cerr<<" | ";
  __f(comma+1,args...);
}
#define trace(...) cerr<<"Line:"<<__LINE__<<" ", __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...)
#endif

void test_case()
{
  R(n);
  vector<pair<string,int>> cnt{{"AC",0},{"WA",0},{"TLE",0},{"RE",0}};
  for(int i=0;i<n;i++){
    string s;
    cin>>s;
    if(s=="AC"){
      cnt[0].second++;
    }
    if(s=="WA"){
      cnt[1].second++;
    }
    if(s=="TLE"){
      cnt[2].second++;
    }
    if(s=="RE"){
      cnt[3].second++;
    }
  }
  for(int i=0;i<4;i++){
    cout<<cnt[i].first<<" x "<<cnt[i].second<<endl;
  }
}
int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    int t=1;
    // cin>>t;
    while(t--)
    {
        test_case();
    }

    cerr<<endl<<"Time : "<<1000*((double)clock())/(double)CLOCKS_PER_SEC<<"ms\n";

    return 0;
}


Measured memory_bytes: 3728
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[] s = sc.next().toCharArray();
        int n = s.length;
        int cnt = 0;
        for (int i = 0; i < n; i++) {
            if(s[i] != '0'){
                cnt++;
            }
        }

        System.out.println(Math.min(cnt, n-cnt) *2);
        sc.close();
    }

}


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

#define MOD_1000000007 (1000000007LL)
#define rep(i, n)   for (long long i = 0; i <  (long long)(n); i++)
#define rep1(i, n)  for (long long i = 1; i <  (long long)(n); i++)
#define rep0c(i, n) for (long long i = 0; i <= (long long)(n); i++)
#define rep1c(i, n) for (long long i = 1; i <= (long long)(n); i++)
#define rep2c(i, n) for (long long i = 2; i <= (long long)(n); i++)
#define repc0(n, i) for (long long i = (long long)(n); i >= 0; i--)
#define repc1(n, i) for (long long i = (long long)(n); i >= 1; i--)

using std::cerr;
using std::cout;
using std::endl;
using std::map;
using std::set;
using std::string;
using std::vector;
typedef long long ll;

char S[100010];
set<int> rls, lrs;
map<int, int> result;

void solve() {
  int n = strlen(S);
  rep(i, n-1) {
    if(S[i] == 'R' && S[i+1] == 'L') rls.insert(i+1);
    else if(S[i] == 'L' && S[i+1] == 'R') lrs.insert(i+1);
  }
  lrs.insert(n);
  int past = -1;
  set<int>::iterator lrs_itr = lrs.begin();
  for(const int& rl : rls) {
    int rb = *lrs_itr - 1;
    lrs_itr++;
    for(int i = past+1; i <= rb; i++) {
      if(i < rl-1 || rl < i) {
        result[i] = 0;
      }
      if((abs(rl-i))%2) result[rl-1]++;
      else result[rl]++;
    }
    past = rb;
  }
  rep(i, n) {
    if(i) printf(" ");
    printf("%d", result[i]);
  }
  printf("\n");
}

int main() {
  scanf("%s", S);
  solve();
  return 0;
}

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

   typedef long long ll;
   typedef pair<ll, ll> Pii;
   typedef vector<ll> Vi;
   typedef vector<Vi> VVi;

   const double EPS = (1e-7);
   const ll INF =(1e13);
   const double PI = (acos(-1));
   const ll MOD = ll(1e9) + 7;

   #define REP(i, n) for(ll i = 0; i < (ll)(n); i++)
   #define REPR(i, n) for(ll i = n; i > -1; i--)
   #define FOR(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
   #define ALL(x) (x).begin(),(x).end()
   #define SORT(x) sort((x).begin(), (x).end())
   #define RSORT(x) sort((x).rbegin(), (x).rend())
   #define REVERSE(x) reverse((x).begin(), (x).end())
   #define SZ(x) ((ll)(x).size())
   #define pb push_back
   #define mp make_pair

   //chmax(a, b): a>bならaをbで更新　更新したときにtrueを返す
   //chmin(a, b): a<bならaをbで更新　更新したときにtrueを返す
   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; }

   #define dump(x) cerr<< #x << "= " << (x) << endl;

   ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
   ll pow(ll a, ll b){if (b == 0) return 1; else if (b % 2 == 0) return pow(a * a, b / 2); else return pow(a * a, b / 2) * a;}
   ll pow(ll a, ll b, ll m){if (b == 0) return 1; else if (a == 0) return 0; else if (b % 2 == 0) return (pow((a * a) % m, b / 2, m) % m); else return (pow((a * a) % m, b / 2, m) * a) % m;}
   ll residue(ll a, ll m){return ((a % m) + m) % m;};

   ll dx[4]={1,0,-1,0};
   ll dy[4]={0,1,0,-1};
struct UnionFind {
  vector<long long> data;
  UnionFind(long long size) : data(size, -1) { }
  bool unionSet(long long x, long long y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool findSet(long long x, long long y) {
    return root(x) == root(y);
  }
  long long root(long long x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  long long size(long long x) {
    return -data[root(x)];
  }
};

int main(){
   ll n, m; cin >> n >> m;
   vector<Pii> v(m); 
   REP(i, m) {
      ll a, b; cin >> a >> b; a--; b--;
      v[m-1-i] = mp(a, b);
   }
   UnionFind s(n);
   Vi ans(m);
   ans[0] = n * (n-1) / 2;
   REP(i, m-1){
      if(s.findSet(v[i].first, v[i].second)) ans[i + 1] = ans[i];
      else{
         ans[i+1] = ans[i] - s.size(v[i].first) * s.size(v[i].second);
         s.unionSet(v[i].first, v[i].second);
      }
   }
   REP(i, m){
      cout << ans[m-1-i] << endl;
   }
}


Measured memory_bytes: 4480
using System;
using System.Collections.Generic;
using System.Linq;

namespace ABC160D
{
    class Program
    {
        static void Main(string[] args)
        {
            var NXY = ReadInt();
            int N = NXY[0];
            int X = NXY[1] - 1;
            int Y = NXY[2] - 1;

            int[] s = new int[N];
            int[] t = new int[N];
            for (int i = 0; i < N - 1; i++)
            {
                s[i] = i; t[i] = i + 1;
            }
            s[N - 1] = X;
            t[N - 1] = Y;

            int[] ans = new int[N];
            for (int i = 0; i < N; i++)
            {
                int[] d = new int[N];
                for (int j = 0; j < N; j++)
                {
                    d[j] = int.MaxValue / 2;
                }
                d[i] = 0;

                Queue<edge> que = new Queue<edge>();
                que.Enqueue(new edge() { point = i, cost = 0 });

                while (que.Count > 0)
                {
                    edge now = que.Dequeue();
                    if (now.cost > d[now.point]) continue;
                    d[now.point] = now.cost;

                    int next = now.point + 1;
                    int prev = now.point - 1;
                    int edge = -1;
                    if (now.point == X) edge = Y;
                    else if (now.point == Y) edge = X;

                    if (next < N) que.Enqueue(new edge() { point = next, cost = now.cost + 1 });
                    if (prev >= 0) que.Enqueue(new edge() { point = prev, cost = now.cost + 1 });
                    if (edge != -1) que.Enqueue(new edge() { point = edge, cost = now.cost + 1 });
                }

                for (int j = 0; j < N; j++)
                {
                    ans[d[j]]++;
                }
            }

            for (int i = 1; i < N; i++)
            {
                Console.WriteLine(ans[i] / 2);
            }

            Console.ReadKey();
        }

        struct edge
        {
            public int point;
            public int cost;
        }

        static int[] ReadInt()
        {
            int[] ret = Console.ReadLine().Split().Select(int.Parse).ToArray();
            return ret;
        }
        static long[] ReadLong()
        {
            long[] ret = Console.ReadLine().Split().Select(long.Parse).ToArray();
            return ret;
        }
    }
}


Measured memory_bytes: 29280
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=1e6+23;

vector<int> g[maxn];
int n;

int main(){
	cin>>n;
	for(int i=0;i<n-1;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for(int i=1;i<=n;i++) sort(g[i].begin(),g[i].end());
	ll ans=0,now=0;
	for(int i=n;i;i--){
		now+=n-i+1;
		auto it=lower_bound(g[i].begin(),g[i].end(),i);
		while(it!=g[i].end()){
			now-=n-*it+1;	
			it++;
		}
		ans+=now;
	}
	printf("%lld",ans);
}

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

using namespace std;

#define FOR(i, N) for (int i = 0; i < (int)N; i++)
#define FORIN(i, a, b) for (int i = a; i < (int)b; i++)
#define ALL(x) (x).begin(), (x).end()
#define MOD 1000000007
#define INF (1 << 29)
#define LLINF (1LL << 60)
#define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__)

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

string to_string(string s) { return s; }
template <class S>
string to_string(unordered_set<S> s);
template <class S>
string to_string(set<S> s);
template <class S, class T>
string to_string(map<S, T> m);
template <class T>
string to_string(vector<T> v);

template <class S, class T>
string to_string(pair<S, T> p) {
  return "{" + to_string(p.first) + "," + to_string(p.second) + "}";
}

template <class S>
string to_string(unordered_set<S> s) {
  string ret = "{";
  for (S x : s) ret += to_string(x) + ",";
  return ret + "}";
}

template <class S>
string to_string(set<S> s) {
  string ret = "{";
  for (S x : s) ret += to_string(x) + ",";
  return ret + "}";
}

template <class S, class T>
string to_string(map<S, T> m) {
  string ret = "{";
  for (pair<S, T> x : m) ret += to_string(x) + ",";
  return ret + "}";
}

template <class T>
string to_string(vector<T> v) {
  string ret = "{";
  for (int i = 0; i < (int)v.size() - 1; ++i) {
    ret += to_string(v[i]) + ",";
  }
  if (v.size() > 0) {
    ret += to_string(v.back());
  }
  ret += "}";
  return ret;
}

void debug() { cerr << endl; }

template <class Head, class... Tail>
void debug(Head head, Tail... tail) {
  cerr << to_string(head) << " ";
  debug(tail...);
}

void print() { cout << endl; }

template <class Head, class... Tail>
void print(Head head, Tail... tail) {
  cout << to_string(head);
  print(tail...);
}

int main() {
  cin.tie(0);
  int N;
  cin >> N;
  vector<ll> A(N);
  FOR(i, N) cin >> A[i];
  vector<vector<int>> cnt(60, vector<int>(2));
  ll ans = 0;
  FOR(i, 60) {
    FOR(j, N) { cnt[i][(A[j] >> i) & 1]++; }
    ans =
        (ans + (((1LL << i) % MOD) * cnt[i][0] % MOD) * cnt[i][1] % MOD) % MOD;
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 2560