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

template<class Monoid, class InputIterator> typename Monoid::value_type fold(InputIterator first, InputIterator last) {
  Monoid m = Monoid();
  using T = typename Monoid::value_type;
  T acc = m.id();
  std::for_each(first, last, [&](T &x) { acc = m(acc, x); });
  return acc;
}

constexpr int gcd(int a, int b) {
  while (b) b ^= a ^= b ^= a %= b;
  return a;
}

template<typename T = int> struct gcdMonoid {
  using value_type = T;
  constexpr T id() const { return 0; }
  T operator()(const T &a, const T &b) const { return gcd(a, b); }
};

template<typename T, T lowerInf = std::numeric_limits<T>::min()> struct maxMonoid {
  using value_type = T;
  constexpr T id() const { return lowerInf; }
  T operator()(const T &a, const T &b) const { return std::max(a, b); }
};

main {
  int n, k;
  cin >> n >> k;
  vi a(n);
  cin >> a;
  int m = fold<maxMonoid<int>>(all(a));
  if (m < k) {
    cout << "IMPOSSIBLE" << endl;
    return 0;
  }
  int g = fold<gcdMonoid<int>>(all(a));
  cout << (k % g == 0 ? "" : "IM") << "POSSIBLE" << endl;
}


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

const int MAXN = 200005;

int n, a[MAXN];

int main(){
	scanf("%d", &n);
	
	map <int, int> mp;
	for(int i = 0; i < n; i++){
		scanf("%d", &a[i]);
		mp[a[i]]++;
	}
	
	int res = 0;
	for(int i = (1 << 30); i >= 2; i >>= 1){
		for(auto &d : mp){
			if(d.first == i - d.first){
				res += d.second / 2;
				d.second %= 2;
			}else{
				if(mp.count(i - d.first)){
					int mn = min(d.second, mp[i - d.first]);
					res += mn;
					d.second -= mn;
					mp[i - d.first] -= mn;
				}
			}
		}
	}
	printf("%d\n", res);
	
	return 0;
}


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

int main(void) {
    int N;
  cin >> N;
    vector<int> vec(N);
    vector<int> A(N);
  vector<int> B(N, 2);
  int count = 1;
  for (int i = 0; i < N; i++){
    cin >> vec.at(i);
  }
  A.at(0) = vec.at(vec.at(0) - 1);
    if(vec.at(0) == 2){
     cout << 1; 
    }
  else{
  for(int j = 1; j < N ; j++){

    A.at(j) = vec.at(A.at(j - 1) - 1);
    count++;
          if (A.at(j - 1) == 2){
      break;
    }

  }
    if((count == N) && (A.at(N - 1) != 2) ){
      count = -1;
    }
  cout << count;
  }
}

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

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<pdd> vdd;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define mem(a,b) memset(a, b, sizeof(a) )
#define all(x) (x).begin(),(x).end()
#define INF 1000000000000
#define MOD 1000000007
#define PB push_back
#define MP make_pair
#define F first
#define S second

inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

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;}

int ctoi(char c) {
	if (c >= '0' && c <= '9') {
		return c - '0';
	}
	return 0;
}

int main(){
    int n;cin>>n;
    string s;cin>>s;
    int b[n+1],w[n+1];
    b[0]=0;w[0]=0;
    rep(i,n){
        if(s[i]=='#'){
            b[i+1]=b[i]+1;
            w[i+1]=w[i];
        }
        else{
            b[i+1]=b[i];
            w[i+1]=w[i]+1;
        }
    }
    int ans=1e9;
    for(int i=0;i<=n;++i){
        ans=min(ans,b[i]+w[n]-w[i]);
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 2180
using System;
class Program
{
	static void Main(string[] args)
	{
		string b = Console.ReadLine();
		string result = "";
		if(b=="A") result = "T";
		else if(b=="T") result = "A";
		else if(b=="G") result = "C";
		else if(b=="C") result = "G";

		Console.WriteLine(result);
	}
}

Measured memory_bytes: 11092
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#if DEBUG
using Console = Atcoder.Helper.HelperConsole;
using AtcoderHelper = Atcoder.Helper.AtcoderHelper;
#endif

namespace Atcoder
{
    public class Program
    {
        public static void Main(string[] args)
        {
#if DEBUG
            var url = @"
https://atcoder.jp/contests/abc142/tasks/abc142_b
";
            AtcoderHelper.RunTestCases(url);
        }
        public static void Code()
        {
#endif
            var line = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            var n = line[0];
            var k = line[1];
            var h = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            var count = h.Count(x => x >= k);
            Console.WriteLine(count);
        }
    }
}


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

int main(){
  string s;
  cin >> s;
  bool ans = false;
  if (s[0] == s[1] && s[2] == s[3] && s[0] != s[2]) ans =true;
  if (s[0] == s[2] && s[1] == s[3] && s[0] != s[1]) ans =true;
  if (s[0] == s[3] && s[2] == s[1] && s[0] != s[1]) ans =true;
  if (ans) puts("Yes");
  else puts("No");
  return 0;
}


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

using namespace std;

const int DIM = 25;
const int inf = 1e9 + 7;

int n, m;

bool v[DIM][DIM];

int dp[DIM][DIM];

bool viz[DIM][DIM];

queue < pair <int,int > > c;

void bfs(int x, int y,int &rez)
{
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            viz[i][j] = false;

    viz[x][y] = true;
    dp[x][y] = 0;

    c.push(make_pair(x,y));


    while(!c.empty())
    {

        int x = c.front().first;
        int y = c.front().second;
        c.pop();
        rez = max(rez,dp[x][y]);

        if(v[x + 1][y] == 0 && x + 1 <= n)
        if(viz[x + 1][y] == false)
         {c.push(make_pair(x + 1,y));
          viz[x + 1][y] = true;
         dp[x + 1][y] = dp[x][y] + 1;
         }

         if(v[x][y + 1] == 0 && y + 1 <= m)
        if(viz[x][y + 1] == false)
         {c.push(make_pair(x,y + 1));
         viz[x ][y + 1] = true;
         dp[x][y + 1] = dp[x][y] + 1;
         }

          if(v[x][y - 1] == 0 && y - 1 > 0)
        if(viz[x][y - 1] == false)
         {c.push(make_pair(x,y - 1));
         viz[x][y - 1] = true;
         dp[x][y - 1] = dp[x][y] + 1;
         }
          if(v[x - 1][y] == 0 && x - 1 > 0)
        if(viz[x - 1][y] == false)
         {c.push(make_pair(x - 1,y));
         viz[x - 1][y] = true;
         dp[x - 1][y] = dp[x][y] + 1;
         }


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

    cin >> n >> m;

    for(int i = 1; i <= n; i++)
        for(int j = 1;  j <= m ; j++)
        {
         char x;
         cin >> x;
         if(x == '.')
            v[i][j] = 0;
         else
            v[i][j] = 1;
        }
    int rez = 0;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m ;j++)
        {
        if(v[i][j] == 0)
         bfs(i,j,rez);
        }


        cout << rez;
    return 0;
}


Measured memory_bytes: 256
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 15 17:00:46 2020

@author: saito
"""

# %% import phase

# %% define phase

# %% input phase
N = int(input())
L = list(map(int, input().split()))

# %% process phase
cnt = 0
if N >= 3:
    for i in range(len(L)-2):
        for j in range(i+1, len(L)-1):
            for k in range(j+1, len(L)):
                a = [L[i], L[j], L[k]]
                if len(set(a)) < len(a):
                    continue
                ami = a.index(max(a))
                am = a.pop(ami)
                if am < sum(a):
                    cnt += 1


# %%output phase
print(cnt)

Measured memory_bytes: 9208
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int main(){
    while(true){
	int n;
	cin >> n;
	if(n == -1) break;
	vector<int> ans;
	if(n == 0){
	    cout << 0 << endl;
	    continue;
	}
	while(n > 0){
	    ans.PB(n%4);
	    n /= 4;
	}
	REP(i,ans.size()) cout << ans[ans.size()-1-i];
	cout << endl;
    }
    return 0;
}

Measured memory_bytes: 1212
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n,Ap=0,Bp=0;
  cin >> n;
  vector<int> point(n);
  for(int i=0;i<n;i++){
    cin >> point.at(i);
  }
  for(int i=0;i<n-1;i++){
    int memo;
    for(int j=0;j<n-i-1;j++){
      if(point.at(j)<point.at(j+1)){
        memo = point.at(j+1);
        point.at(j+1) = point.at(j);
        point.at(j)=memo;
      }
    }
  }
  for(int i=0;i<n;i++){
    if(i%2==0)
      Ap+=point.at(i);
    else
      Bp+=point.at(i);
  }
  cout << Ap-Bp << endl;
}

Measured memory_bytes: 3596
a, b = map(int, input().split())

_a = a/0.08

if _a % 1 == 0.5:
    _as = [int(_a+0.5+i) for i in range(12)]
else:
    _as = [int(_a+i) for i in range(12)]

ans = -1
for x in _as:
    if b == int(x * 0.10):
        ans = x
        break

print(ans)


Measured memory_bytes: 38384
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cmath>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()

typedef int Int;

// math
Int gcd(Int a, Int b) {
  return b != 0 ? gcd(b, a % b) : a;
}
Int lcm(Int a, Int b) {
  return a * b / gcd(a, b);
}
// a x + b y = gcd(a, b)
Int extgcd(Int a, Int b, Int &x, Int &y) {
  Int g = a; x = 1; y = 0;
  if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

// Graph
typedef Int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) :
    src(src), dst(dst), weight(weight) { }
};
bool operator < (const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
    e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

// bipartite matching
bool augment(const Graph& g, int u,
    vector<int>& matchTo, vector<bool>& visited) {
  if (u < 0) return true;
  FOR(e, g[u]) if (!visited[e->dst]) {
    visited[e->dst] = true;
    if (augment(g, matchTo[e->dst], matchTo, visited)) {
      matchTo[e->src] = e->dst;
      matchTo[e->dst] = e->src;
      return true;
    }
  }
  return false;
}
int bipartiteMatching(const Graph& g, int L, Edges& matching) {
  const int n = g.size();
  vector<int> matchTo(n, -1);
  int match = 0;
  REP(u, L) {
    vector<bool> visited(n);
    if (augment(g, u, matchTo, visited)) ++match;
  }
  REP(u, L) if (matchTo[u] >= 0) // make explicit matching
    matching.push_back( Edge(u, matchTo[u], 0) );
  return match;
}


int main() {
    int m, n;
    while ( cin >> m >> n ) {
        if ( m == 0 && n == 0 ) break;

        int B[501];
        int R[501];
        for ( int i = 0; i < m; i++ ) cin >> B[i];
        for ( int i = 0; i < n; i++ ) cin >> R[i];
        
        Graph g(m+n);
        for ( int i = 0; i < m; i++ ) {
            for ( int j = 0; j < n; j++ ) {
                if ( gcd( B[i], R[j] ) > 1 ) {
                    g[i].push_back( Edge( i, m+j, 0 ) );
                }
            }
        }

        Edges edges;
        cout << bipartiteMatching( g, m, edges ) << endl;
    }
    return 0;
}

Measured memory_bytes: 3056
def main():
    n = int(input())

    inlis = list()
    for _ in range(n):
        d1, d2 = map(int, input().split())
        inlis.append([d1, d2])
    
    for i in range(n):
        a, b = inlis[i][0], inlis[i][1]
        if a == b and i <= n - 3:
            if inlis[i+1][0] == inlis[i+1][1] and inlis[i+2][0] == inlis[i+2][1]:
                print('Yes')
                exit()
    print('No')




if __name__ == "__main__":
    main()

Measured memory_bytes: 62512
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, a, b) for (int i = a; i <= (b); ++i)
#define repr(i, a, b) for (int i = a; i >= (b); --i)
typedef long long ll;
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;

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() {
  int N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;

  vector<int> l(Q), r(Q);
  rep(i, Q) {
    cin >> l[i] >> r[i];
    l[i]--;
    r[i]--;
  }

  vector<int> L(N);
  L[0] = 0;
  repp(i, 1, N - 1) {
    L[i] = L[i - 1];
    if (S[i - 1] == 'A' && S[i] == 'C') L[i]++;
  }

  rep(i, Q) { cout << L[r[i]] - L[l[i]] << endl; }
}

Measured memory_bytes: 2176
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

r,g,b,n = iim()
ans = 0
for i in range(n//r+1):
    for j in range(n//g+1):
        num = n-i*r-j*g
        if num%b == 0 and num//b >= 0:
#            print(i,j,num//b)
            ans += 1
print(ans)

Measured memory_bytes: 40812
n=int(input())
def dfs(s):
  if int(s) > n:
    return 0
  if all(s.count(c) > 0 for c in '753'):
    ret=1
  else:
    ret=0 # s 自体が七五三数なら +1 
  for c in '753':
    ret += dfs(s + c)
  return ret
print(dfs('0'))

Measured memory_bytes: 2940
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string>
#define ios ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
const int maxn=1e4+100;
string a[maxn];
int main()
{
	ios;
	int n,A=0,B=0,AB=0; cin>>n;
	ll sum=0;
	for(int i=0;i<n;i++){
		cin>>a[i];
		int len=a[i].size()-1;
		for(int j=1;j<=len;j++){
			if(a[i][j]=='B'&&a[i][j-1]=='A') sum++;
		}
		if(a[i][0]=='B'&&a[i][len]=='A'){
			AB++; continue;
		}
		if(a[i][0]=='B'){
			B++; continue;
		}
		if(a[i][len]=='A'){
			A++; continue;
		}
	}
	ll ans=0;
	if(AB>0){
		if(A>0||B>0){
			A-=1; B-=1;
			ans+=2;AB-=1;			
		}
		else AB-=1;
	}
	ans+=min(A,B)+AB+sum;
	cout<<ans<<endl;
	return 0;
}

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

int main()
{
    string S;
    cin >> S;

    if (S == "Sunny")
    {
        cout << "Cloudy" << endl;
    }
    if (S == "Cloudy")
    {
        cout << "Rainy" << endl;
    }
    if (S == "Rainy")
    {
        cout << "Sunny" << endl;
    }
}


Measured memory_bytes: 256
import sys
import itertools
if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
    
    
cnt=0    
k=int(input())
a,b=map(int,input().split())    
for i in range(a,b+1):
	if i%k==0:
		cnt+=1
		
if cnt > 0:
	print("OK")
else:
	print("NG")

Measured memory_bytes: 61732
#include<stdio.h>
#include<string.h>
#define N 1001
int A[N][N];
int LCS(char x[N],char y[N]);
int max(int a,int b);
int main(){
  int q,i;
  char x[N],y[N];
  scanf("%d",&q);
  for(i=0;i<q;i++){
    scanf("%s",x);
    scanf("%s",y);
    printf("%d\n",LCS(x,y));
  }
  return 0;
}

int LCS(char x[N],char y[N]){
  int i,j,n1,n2;
  n1=strlen(x);
  n2=strlen(y);
  for(i=1;i<=n1;i++){
    for(j=1;j<=n2;j++){
      if(x[i-1]==y[j-1])  A[i][j]=A[i-1][j-1]+1;
      else  A[i][j]=max(A[i-1][j],A[i][j-1]);
    }
  }
  return A[n1][n2];
}

int max(int a,int b){
  if(a>=b) return a;
  else return b;
}

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

namespace ABC20190427
{
    class Program
    {
        static void Main(string[] args)
        {
            //Sample();
            Biscuit();
            //Console.ReadLine();
            //Biscuit();
            //Console.ReadLine();
            //Biscuit();
            //Console.ReadLine();
        }

        static void Biscuit()
        {
            // A B T
            string[] input = Console.ReadLine().Split(' ');
            int span = int.Parse(input[0]);
            int biscuitPerSpan = int.Parse(input[1]);
            int time = int.Parse(input[2]);

            int spanCount = time / span;
            int result = biscuitPerSpan * spanCount;
            
            //出力
            Console.WriteLine(result);
        }


        static void Sample()
        {
            // 整数の入力
            int a = int.Parse(Console.ReadLine());

            // スペース区切りの整数の入力
            string[] input = Console.ReadLine().Split(' ');
            int b = int.Parse(input[0]);
            int c = int.Parse(input[1]);

            // 文字列の入力
            string s = Console.ReadLine();

            //出力
            Console.WriteLine((a + b + c) + " " + s);
        }
    }
}


Measured memory_bytes: 11092
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
ll am, n;
pair<ll, ll> points[300];
bool colin(int a, int b, int c)
{
	if (points[a].first == points[b].first && points[b].first == points[c].first) return true;
	ld first = ((ld)points[a].second-points[b].second)/(points[a].first-points[b].first);
	ld second = ((ld)points[a].second-points[c].second)/(points[a].first-points[c].first);
	return first == second;
}
bool done[500][500];
int main()
{
	am = 1;
	ll mod = 998244353L*1000;
	ll mod2 = 998244353L;
	scanf("%lld", &n);
	for (int i = 0; i < n; i++) {
		scanf("%lld%lld", &points[i].first, &points[i].second);
		am*=2;
		am%=mod;
	}
	am-=n+1;
	for (int i = 0; i < n; i++)
	{
		for (int j = i+1; j < n; j++)
		{			
		//	printf("%d %d %Lf %Lf\n", i, j, m, c);
			ll am2 = 4;
			ll in = 2;
			int smallest = i;
			int smallest2 = j;
			for (int k = 0; k < n; k++)
			{
				if (colin(i, j, k) && i != k && j != k)
				{
					am2*=2;
					am2%=mod2*2;
					in++;
					if (k < smallest)
					{
						smallest2 = smallest;
						smallest = k;
					}
					else smallest2 = min(smallest2, k);
				}
			}
			if (done[smallest][smallest2]) continue;
			done[smallest][smallest2] = true;
			am2-=in+1;
			am-=am2%(mod2);
		}
	}
	printf("%lld\n", am%(mod2));
}

Measured memory_bytes: 384
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <complex>
#include <algorithm>
#include <numeric>

using namespace std;

typedef complex<double> Point;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define all(c) (c).begin(), (c).end()
#define mp(a, b) make_pair(a, b)
const double eps = 1e-8;
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};

template<typename T> inline void chmax(T& t, T f){if(t < f)t = f;}
template<typename T> inline void chmin(T& t, T f){if(t > f)t = f;}

int h, w;
vector<string> bldg;
vector<vector<bool> > vis;

inline bool inrect(int x, int y){
    return 0 <= x && x < h && 0 <= y && y < w;
}

pair<Point, int> dfs(int sx, int sy){
    vis[sx][sy] = true;
    vector<Point> piece;
    vector<pair<int, int> > on;
    int ly = w, ry = 0;
    queue<pair<int, int> > q;
    for(q.emplace(sx, sy); !q.empty();){
        int x = q.front().first, y = q.front().second; q.pop();
        piece.emplace_back(x + .5, y + .5);
        if(x && !vis[x-1][y] && bldg[x-1][y] != '.' && bldg[x-1][y] != bldg[sx][sy]){
            on.emplace_back(x-1, y);
        }
        if(x == h-1 || (bldg[x+1][y] != '.' && bldg[x+1][y] != bldg[sx][sy])){
            chmin(ly, y); chmax(ry, y+1);
        }
        rep(i, 4){
            int nx = x + dx[i], ny = y + dy[i];
            if(!inrect(nx, ny) || vis[nx][ny] || bldg[nx][ny] != bldg[sx][sy])continue;
            vis[nx][ny] = true;
            q.emplace(nx, ny);
        }
    }
    Point med = accumulate(all(piece), Point(0, 0));
    int n = (int)piece.size();
    for(auto p: on)if(!vis[p.first][p.second]){
        pair<Point, int> t = dfs(p.first, p.second);
        if(t.second == -1)return mp(med, -1);
        med += t.first; n += t.second;
    }
    double M = med.imag() / (1.*n);
    return ly < M - eps && M + eps < ry? mp(med, n): mp(med, -1);
}

const char* solve(){
    vis.assign(h, vector<bool>(w, false));
    int y = find_if_not(all(bldg[h-1]), [](const char& c){return c == '.';}) - bldg[h-1].begin();
    return dfs(h-1, y).second != -1? "STABLE": "UNSTABLE";
}

int main(){
    while(cin >> w >> h, w|h){
        bldg.assign(h, "");
        rep(i, h)cin >> bldg[i];
        cout << solve() << '\n';
    }
    return 0;
}

Measured memory_bytes: 1236
import itertools
N=int(input())
I=[str(i) for i in range(1,N+1)]
P=''.join(input().split())
Q=''.join(input().split())
li=list(itertools.permutations(I,N))

a=0
b=0
for i in range(len(li)):
    if(''.join(li[i])==P):
        a=i+1
    if(''.join(li[i])==Q):
        b=i+1
print(abs(a-b))

Measured memory_bytes: 8052
#include <cstdio>
using namespace std;
bool isPrime(int x) {
	if (x == 2) return true;
	if (x == 1 || (x & 1) == 0) return false;
	for (int i = 3; i * i <= x; i += 2) {
		if (x % i == 0) return false;
	}
	return true;
}
int main() {
    int n, ans = 0;
	scanf("%d", &n);
	while (n--) {
		int x;
		scanf("%d", &x);
		if (isPrime(x)) ans++;
	}
	printf("%d\n", ans);
	return 0;
}


Measured memory_bytes: 2692
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using namespace std;
#define a_macro(args...) sum(args)
#define mp make_pair
#define eb emplace_back
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

typedef long long ll;
const ll mod = 1e9+7;

typedef tuple<int,int,int> State; // operator< defined
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef double ld;

int main(int argc, const char** argv) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
	int n,k,i;
	cin>>n>>k;
	int a[n];
	for(i=0;i<n;++i)
		cin>>a[i];
	int dp[k+1],j;
	memset(dp,0,sizeof(dp));

	for(i=1;i<=k;++i){
		for(j=0;j<n;++j){
			if(a[j] > i)
				continue;
			if(dp[i-a[j]] == 0)
				dp[i] = 1;
		}
	}
	if(dp[k])
		cout<<"First\n";
	else
		cout<<"Second\n";
    
#ifndef LOCAL_DEFINE
    cerr << "Time elapsed: "<< 1e1 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif // LOCAL_DEFINE
    return 0;
}

Measured memory_bytes: 640
# 入力
N = int(input())
a = list(map(int, input().split()))

# 各整数を素因数分解したとき、因数2の個数に着目して判定する
X = sum(x % 2 == 1 for x in a)
Y = sum(x % 2 == 0 and x % 4 == 2 for x in a)
Z = sum(x % 4 == 0 for x in a)
ans = (
    'Yes' if X <= Z or X == Z + 1 and Y == 0 else
    'No'
)

# 出力
print(ans)


Measured memory_bytes: 15020
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <cstdint>
#include <numeric>
#include <bitset>
#include <functional>

using namespace std;

using ll =  long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {
    { 0, 1}, {-1, 0}, {0,-1}, {1, 0}
};
constexpr ll INF = 1LL << 60;

constexpr int MAX_N = 100001;
constexpr int MAX_M = 100001;

struct Edge {
    int l, r;
    ll c;
    Edge(int _l=0, int _r=0, ll _c=0): l(_l), r(_r), c(_c) {}
    bool operator < (const Edge &e) const {
        if(l != e.l) return l < e.l;
        if(c != e.c) return c < e.c;
        return r < e.r;
    }
};

template<class T> class SegmentTree {
    public:
    size_t n;          // 葉の数
    vector<T> data; // データ
    T def;          // 単位元
    function<T(T, T)> update_func;  // 点更新で使う処理
    function<T(T, T)> find_func;    // 区間クエリで使う処理

    SegmentTree(size_t _n, T _def, function<T(T, T)> _update_func, function<T(T, T)> _find_func)
        : def(_def), update_func(_update_func), find_func(_find_func) {
        // 2のべき乗にする
        n = 1;
        while(n < _n) n <<= 1;
        data = vector<T>(2*n-1, _def);
    }

    void update(size_t i, T x){
        i += n-1;
        data[i] = update_func(data[i], x);
        while(i){
            i = (i-1)/2;
            data[i] = find_func(data[2*i+1], data[2*i+2]);
        }
    }

    T _find(size_t s, size_t t, size_t k, size_t kl, size_t kr){
        if(kr <= s || t <= kl) return def;
        if(s <= kl && kr <= t) return data[k];
        size_t kc = (kl+kr)/2;
        T vl = _find(s, t, 2*k+1, kl, kc);
        T vr = _find(s, t, 2*k+2, kc, kr);
        return find_func(vl, vr);
    }
    
    T find(size_t s, size_t t) {
        return _find(s, t, 0, 0, n);
    }
};

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<Edge> edges(m);
    for(int i=0;i<m;++i){
        cin >> edges[i].l >> edges[i].r >> edges[i].c;
        --edges[i].l;
        --edges[i].r;
    }
    sort(edges.begin(), edges.end());
    
    SegmentTree<ll> tree(
        n, INF, 
        [](ll a, ll b) { return b; }, 
        [](ll a, ll b) { return min(a, b); }
    );

    tree.update(0, 0);

    int l, r; ll c;
    for(int i=0;i<m;++i){
        l = edges[i].l;
        r = edges[i].r;
        c = edges[i].c;
        if(tree.find(l, n) == INF) {
            continue;
        }
        tree.update(
            r, 
            min(tree.find(r, r+1), tree.find(l, n) + c)
        );
    }

    ll ans = tree.find(n-1, n);

    cout << ((ans == INF)?-1:ans) << endl;
}


Measured memory_bytes: 3840
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <set>
#include <map>
#include <stack>
#include <deque>
#include <iterator>
#include <iomanip>
#include <queue>
using namespace std;


int main(){
    long long n;
    cin >> n;
    vector<vector<long long> > a(n + 1, vector<long long>(n));
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j < n; ++j){
            cin >> a[i][j];
        }
    }
    vector<long long> round;
    vector<long long> cnt(n + 1, 1);
    for(int i = 1; i <= n; ++i){
        if(a[a[i][1]][1] == i){
            round.push_back(i);
        }
    }
    long long ans = 0;
    while(!round.empty()){
        ans++;
        vector<long long> nw;
        for(long long i: round){
            cnt[i]++;
            if(cnt[i] <= n - 1){
                long long x = a[i][cnt[i]];
                if(i == a[x][cnt[x]]){
                    nw.push_back(i);
                    nw.push_back(x);
                }
            }
        }
        round = nw;
    }
    for(int i = 1; i <= n; ++i){
        if(cnt[i] != n){
            cout << -1;
            return 0;
        }
    }
    cout << ans;
}

Measured memory_bytes: 8192
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
a = [LIST() for i in range(n)]
ans = 0

for x in a[::-1]:
        if ( x[0] + ans ) % x[1] != 0:
                ans += x[1] - ( x[0] + ans ) % x[1]
print(ans)

Measured memory_bytes: 28868
N,A,B=map(int,raw_input().split())
S=raw_input()
check=0
juni=0
for s in S:
    if s=='a':
        if check<A+B:
            check+=1
            print "Yes"
        else:
            print "No"
    elif s=='b':
        juni+=1
        if check<A+B and juni<=B:
            check+=1
            print "Yes"
        else:
            print "No"
    elif s=='c':
        print "No"


Measured memory_bytes: 3076
#[allow(unused_imports)]
use {
    proconio::{fastout, input, marker::*},
    rand::Rng,
    std::cmp::*,
    std::collections::*,
    std::ops::*,
};

const N: usize = 100000;

fn main() {
    input! {
        n: usize,
        xy: [(Usize1, Usize1); n]
    }
    let mut to = vec![vec![]; 2 * N];
    for (x, y) in xy {
        to[x].push(N + y);
        to[N + y].push(x);
    }
    let mut visit = vec![false; 2 * N];
    let mut ans = 0;
    for i in 0..N {
        if visit[i] {
            continue;
        }
        let (x, y) = dfs(i, &to, &mut visit);
        ans += x * y;
    }
    ans -= n;
    println!("{}", ans);
}

fn dfs(v: usize, to: &Vec<Vec<usize>>, visit: &mut Vec<bool>) -> (usize, usize) {
    let mut x = 0;
    let mut y = 0;
    if v / N == 0 {
        x += 1;
    } else {
        y += 1;
    }
    visit[v] = true;
    for &u in to[v].iter() {
        if visit[u] {
            continue;
        }
        let (dx, dy) = dfs(u, to, visit);
        x += dx;
        y += dy;
    }
    (x, y)
}


Measured memory_bytes: 14960
#include<bits/stdc++.h>
using namespace std;
int main(){
	string s;
	int n, len;
	cin >> n;
	cin >> s;
	len = s.size();
	if(len <= n){
		cout << s << endl;
	}else{
		for(int i=0; i<n; i++){
			cout << s[i];
		}	
		cout << "...";

	}
	
	return 0;
}

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

#define pb push_back
#define fi first
#define se second
#define eb emplace_back

using namespace std;
typedef pair<int, int> ii;
typedef long long ll;

const int M = 998244353;

ll memo[3007][3007][2];
int n, m;
int a, b, c, d; 

ll solve(int a, int b, int last = 0) {
	if(a == n and b == m) return 1;
	ll & st = memo[a][b][last];

	if(st == -1) {
		// tira 
		st = 0;

		if(a < n) {
			if(last == 0 and a + 1 > ::a) st += ll(b) * solve(a + 1, b, 0) % M;
			else st += solve(a + 1, b, 0);
		}
		st %= M;

		if(b < m and b + 1 > ::b) {
			st += ll(b + 1 <= ::b? 0 : a) * solve(a, b + 1, 1) % M;
		}
		st %= M;
	}
	return st;
}

int main() {
	memset(memo, -1, sizeof memo);
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> a >> b >> c >> d;

    if(a == c and b == d) {
    	cout << 1 << endl;
    	exit(0);
    }
    n = c, m = d;

    ll ans = 0;
	ans += ll(b) * solve(a + 1, b, 0);
    ans %= M;

    ans += (ll) a * solve(a, b + 1, 1)%M;
    ans %= M;

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

Measured memory_bytes: 145312
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main(void){
int n;
int p[55];
int wrong=0;
scanf("%d",&n);

for(int i=0;i<n;i++){
    scanf("%d",&p[i]);
}

for(int i=0;i<n;i++){
    if(p[i]!=i+1){wrong++;}
}
if(wrong>=3){printf("NO");}
else{printf("YES");}
}


Measured memory_bytes: 128
#pragma gcc optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#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 SORT(V) sort(ALL(V)) //小さい方からソート
#define REV(V) reverse(ALL(V)) //リバース
#define RSORT(V) SORT(V);REV(V) //大きい方からソート
#define NPN(V) next_permutation(ALL(V)) //順列
#define pb(n) push_back(n)
#define endl '\n'
#define Endl '\n'
#define DUMP(x)  cout << #x << " = " << (x) << endl
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define Yay(n) cout << ((n) ? "Yay!": ":(") << endl
#define RAPID cin.tie(0);ios::sync_with_stdio(false)
#define VSUM(V) accumulate(ALL(V), 0)
#define MID(a,b,c) (a) < (b) && (b) < (c)
#define MIDe(a,b,c) (a) <= (b) && (b) <= (c)
#define IN(n) cin >> n
#define IN2(a,b) cin >> a >> b
#define IN3(a,b,c) cin >> a >> b >> c 
#define IN4(a,b,c,d) cin >> a >> b >> c >> d
#define VIN(V) for(int i = 0; i < (V).size(); i++) {cin >> (V).at(i);}
#define OUT(n) cout << n << endl
#define VOUT(V) REP(i, (V).size()) {cout << (V)[i] << endl;}
#define VOUT2(V) REP(i, (V).size()) {cout << (V)[i] << " ";} cout<<endl;

// 型マクロ定義
#define int long long
#define P pair<ll,ll>
#define Vi vector<ll>
#define Vd vector<double>
#define Vs vector<string>
#define Vc vector<char>
#define M map<ll,ll>
#define S set<ll>
#define PQ priority_queue<ll>
#define PQG priority_queue<ll,V,greater<ll>
//

const int MOD = 1000000007;
const int INF = 1061109567;
const double EPS = 1e-10;
const double PI  = acos(-1.0);

int gcd(int a,int b){return b != 0 ? gcd(b, a % b) : a;} //最大公約数
int lcm(int a, int b){return a * b / gcd(a, b);}
string toStrUp(string str){char diff = 'A'-'a';REP(i,str.size()) str[i] += diff;return str;}
string gCh(string str, int key){return str.substr(key,1);}

signed main() {
  RAPID;
  // デフォルト変数定義
  int n=0,m=0,a=0,b=0,c=0,d=0,x=0,y=0,z=0;
  string s="",t="";
  //

  // ここから
  IN(n);
  Vi A(n);
  VIN(A)
  SORT(A);
  REP(i,n-1){
    z += A[i+1] - A[i];
  }
  OUT(z);
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 1145141919
#define MOD 1000000007
#define EPS 1e-6
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int main()
{
	int n;
	while (cin >> n, n)
	{
		tuple<double, int, int, int> ans;
		vi l(n);
		REP(i, n) cin >> l[i];
		ans = make_tuple(INF, -1, -1, -1);
		REP(s, 16)REP(a, 16)REP(c, 16)
		{
			vi r(n+1);
			r[0] = s;
			REP(i, n) r[i + 1] = ((a*r[i]) + c) % 256;
			vi o(n);
			double cnt[256] = {};
			REP(i, n)
			{
				o[i] = (l[i] + r[i + 1]) % 256;
				cnt[o[i]]++;
			}
			double h = 0;
			REP(i, 256)
			{
				if (cnt[i] > 0) h -= (cnt[i] / n)*log(cnt[i] / n);
			}
			if (h + EPS < get<0>(ans)) ans = make_tuple(h, s, a, c);
		}
		cout << get<1>(ans) << " " << get<2>(ans) << " " << get<3>(ans) << endl;
	}
	return 0;
}

Measured memory_bytes: 1240
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>

using namespace std;

int main(){

	int N;
	while(cin>>N,N){
		vector<int> vi(N*N,0),vi2(N*2+1,0);
		for(int i=0;i<N;i++){
			int sum=0;
			for(int j=0;j<N;j++){
				int num;
				cin>>num;
				vi[i*N+j]=num;
				sum+=num;
			}
			vi2[i]=sum;
		}
		for(int i=0;i<N;i++){
			int sum=0;
			for(int j=0;j<N;j++){
				sum+=vi[i+j*N];
				printf("%5d",vi[i*N+j]);
			}
			printf("%5d\n",vi2[i]);
			vi2[N*2]+=vi2[i];
			vi2[N+i]=sum;
		}
		for(int i=0;i<=N;i++){
			printf("%5d",vi2[N+i]);
		}
		cout<<endl;
	}

	return 0;
}

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

#define PI 3.14159265358979323846264338327950L

int main() {
  int a, b, h, m;

  cin >> a >> b >> h >> m;
  long double thetaa = 2 * PI * ((long double)h * 60 + m) / 720;
  long double thetab = 2 * PI * (long double)m / 60;

  long double theta = thetab - thetaa;

  // double theta = thetaa - thetab;
  long double c = a * a + b * b - 2 * a * b * cosl(theta);
  c = sqrtl(c);

  cout << setprecision(20) << c << endl;
  return 0;
}

Measured memory_bytes: 4092
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Stream;

public class Main {
	private static Scanner sc;

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

		restaurant[] ary = new restaurant[N];

		for (int i = 0; i < N; i++) {
			ary[i] = new restaurant(i + 1, sc.next(), sc.nextInt());
		}

		Arrays.sort(ary);

		Stream.of(ary).forEach(s -> System.out.println(s.number));

	}
}

class restaurant implements Comparable<restaurant> {
	int number;
	String city;
	int point;

	public restaurant(int numer, String city, int point) {
		super();
		this.number = numer;
		this.city = city;
		this.point = point;
	}

	@Override
	public String toString() {
		return "restaunrant [numer=" + number + ", city=" + city + ", point=" + point + "]";
	}

	@Override
	public int compareTo(restaurant r) {
		int ret = this.city.compareTo(r.city);
		if (ret != 0) {
			return ret;
		}

		return Integer.compare(r.point, this.point);
	}

}

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

using namespace std;

#define _ << " " <<
#define pb push_back
#define ff first
#define ss second


typedef long long ll;
typedef long double ld;

#define int ll

const int MAXN = 1e5 + 5;

vector<int> used(MAXN);

vector<pair<int, int> > g[MAXN];

bool bfs(int v) {
    vector<int> q = {v};
    int ind = 0;
    map<int, int> dst;
    dst[v] = 0;
    used[v] = 1;
    while (q.size() > ind) {
        int tmp = q[ind];
        ++ind;
        for (auto to : g[tmp]) {
            if (used[to.ff] == 1) {
                if (dst[to.ff] != dst[tmp] + to.ss) return false;
            } else {
                dst[to.ff] = dst[tmp] + to.ss;
                used[to.ff] = 1;
                q.pb(to.ff);
            }
        }
    }
    return true;
}

void solve() {
    int n, m;
    cin >> n >> m;
    int l, r, d;
    for (int i = 0; i < m; ++i) {
        cin >> l >> r >> d;
        g[l].pb({r, d});
        g[r].pb({l, -d});
    }
    for (int i = 0; i <= n; ++i) {
        if (used[i] == 0 && !bfs(i)) {
            cout << "No";
            return;
        }
    }
    cout << "Yes";
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    //freopen("dominoes.in", "r", stdin);
    //freopen("dominoes.out", "w", stdout);
    int test = 1;
    //cin >> test;
    while (test--) {
        solve();
    }
}


Measured memory_bytes: 20340
import sys
input = sys.stdin.readline

class UnionFind:
    def __init__(self, size):
        self.parent = [-1 for i in range(size)]#非負なら親ノード，負ならグループの要素数

    def root(self, x): #root(x): xの根ノードを返す．
        if self.parent[x] < 0:
            return x
        else:
            self.parent[x] = self.root(self.parent[x]) #xをxの根に直接つなぐ
            return self.parent[x]

    def merge(self, x, y): #merge(x,y): xのいるグループと$y$のいるグループをまとめる
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return False
        if self.parent[x] > self.parent[y]: #xの要素数がyの要素数より「小さい」とき入れ替える
            x,y=y,x
        self.parent[x] += self.parent[y] #xの要素数を更新
        self.parent[y] = x #yをxにつなぐ
        return True

    def issame(self, x, y): #same(x,y): xとyが同じグループにあるならTrue
        return self.root(x) == self.root(y)
        
    def size(self,x): #size(x): xのいるグループの要素数を返す
        return -self.parent[self.root(x)]


n,m=[int(i) for i in input().split()]

A=[[int(i)-1 for i in input().split()] for _ in range(m)]

ans = [0]*(m+1)
a = n*(n-1)//2
ans[0] = a

uf = UnionFind(n)
for i in range(m):
    j = m-i-1
    v1 = A[j][0]
    v2 = A[j][1]
#    print(v1,v2)
    if not uf.issame(v1,v2):
#        print(uf.parent)
        a -= uf.size(v1) * uf.size(v2)
        uf.merge(v1,v2)

    ans[i+1]=a

for i in range(m):
    print(ans[m-i-1])
    
    
    
    
    
    





Measured memory_bytes: 27192
/*  -*- coding: utf-8 -*-
 *
 * a.cc: A: ABCxxx
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

/* typedef */

/* global variables */

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);
  printf("ABC%03d\n", n);
  return 0;
}


Measured memory_bytes: 256
// 2016-12-10
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;

int main() {
	int t, p, r, ti, pi, tt;
	int c[50], w[50][10], a[50];
	string m;
	while (cin >> t >> p >> r, t) {
		for (int i = 0; i < t; i++) {
			c[i] = 0;
			a[i] = 0;
			for (int j = 0; j < p; j++) {
				w[i][j] = 0;
			}
		}
		for (int i = 0; i < r; i++) {
			cin >> ti >> pi >> tt >> m;
			if (m[0] == 'C') {
				c[ti - 1]++;
				a[ti - 1] += w[ti - 1][pi - 1] * 1200 + tt;
			} else {
				w[ti - 1][pi - 1]++;
			}
		}
		vector<pair<pair<int, int>, int>> v;
		for (int i = 0; i < t; i++) {
			v.push_back(make_pair(make_pair(p - c[i], a[i]), i));
		}
		sort(begin(v), end(v));
		for (int i = 0; i < t; i++) {
			cout << v[i].second + 1 << ' ' << p - v[i].first.first << ' ' << v[i].first.second << endl;
		}
	}
	return 0;
}

Measured memory_bytes: 3164
#include<bits/stdc++.h>
constexpr long long INFL = 1LL << 60;
constexpr int INF = 1e9;

using namespace std;

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

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

    for(int i = 1; i <= N; ++i){
        cout << count(a.begin(), a.end(), i) + count(b.begin(), b.end(), i) << endl;
    }
    return 0;
}

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

#define int long long

int32_t main()
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int n;string s;
    cin>>n>>s;
    int r=0,w=0;
    for(char x : s)
    {
        if(x=='R') r++;
        else w++;
    }
    int cnt=0;
    for(int i=r;i<n;i++) if(s[i]=='R') cnt++;
    cout<<cnt<<endl;
    return 0;
}
/*



*/



Measured memory_bytes: 3776
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using edge = struct {int to; ll cost;};

#define mod 1000000007
#define REP(i, n) for (int i = 0; i < n; i++)
#define INF (1 << 29)


void solve() {
    string s, t;
    cin >> s >> t;
    cout << t+s << "\n";
    
}

int main() {
    solve();
    return 0;
}


Measured memory_bytes: 256
from collections import Counter

n = int(input())
A = list(map(int, input().split()))
cnt_A = Counter(A)

if not all([v == 2 for k, v in cnt_A.items() if k != 0]):
    print(0)
else:
    print(pow(2, n // 2, 10**9 + 7))

Measured memory_bytes: 20780
x, k, d = map(int, input().split())
x = abs(x)
if x >= k*d:
    print(x-k*d)
else:
    mv = int(x/d)
    x -= mv*d
    k -= mv
    if k % 2 == 0:
        print(x)
    else:
        print(abs(x-d))


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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N;
int A[101010];
int B[101010];
map<int,int> M[2];
map<int,int> T;
int is[101010];

template<int um> class UF {
	public:
	vector<int> par,rank;
	UF() {rank=vector<int>(um,0); for(int i=0;i<um;i++) par.push_back(i);}
	int operator[](int x) {return (par[x]==x)?(x):(par[x] = operator[](par[x]));}
	int operator()(int x,int y) {
		if((x=operator[](x))==(y=operator[](y))) return x;
		if(rank[x]>rank[y]) return par[x]=y;
		rank[x]+=rank[x]==rank[y]; return par[y]=x;
	}
};
UF<500000> uf;

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	
	FOR(i,N) cin>>A[i];
	FOR(i,N) cin>>B[i];
	int cur[2]={};
	FOR(i,N) {
		cur[0] ^= A[i];
		cur[1] ^= B[i];
		M[0][A[i]]++;
		M[1][B[i]]++;
		T[A[i]]=T[B[i]]=0;
	}
	M[0][cur[0]]++;
	M[1][cur[1]]++;
	if(M[0]!=M[1]) return _P("-1\n");
	T[cur[0]]=T[cur[1]]=0;
	x=0;
	FORR(r,T) r.second=x++;
	
	is[T[cur[0]]]=is[T[cur[1]]]=1;
	uf(T[cur[0]],T[cur[1]]);
	int ns=0;
	FOR(i,N) if(A[i]!=B[i]) {
		ns++;
		is[T[A[i]]]=is[T[B[i]]]=1;
		uf(T[A[i]],T[B[i]]);
	}
	
	FOR(i,T.size()) if(is[i]&&uf[i]==i) ns++;
	ns--;
	cout<<ns<<endl;
	
	
	
	
	
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}


Measured memory_bytes: 19952
#pragma GCC optimize("O3")
#pragma GCC target("avx")
#include <bits/stdc++.h>
#define rep(i ,n) for(int i=0;i<(int)(n);++i)
using namespace std;

signed main(){
    int s , w;
    cin >> s >> w;
    cout << (s <= w ? "unsafe" : "safe" ) << endl;
}

Measured memory_bytes: 3612
N, M = map(int, input().split())
problem_right = {}
problem_pen = {}
score, penalty = 0, 0
for i in range(M):
	p_i, S_i = input().split()
	if S_i == 'WA' and p_i not in problem_right:
		if p_i not in problem_pen:
			problem_pen[p_i] = 1
		else:
			problem_pen[p_i] += 1
	elif S_i == 'AC' and p_i not in problem_right:
		score += 1
		problem_right[p_i] = True
		if p_i in problem_pen:
			penalty += problem_pen[p_i]
print(str(score)+' '+str(penalty))

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

#define FASTIO  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define ll long long int
#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define vl(p) vector<p>
#define w while
#define For(i,s,x) for(i=s;i<x;i++)
#define Fore(i,s,x) for(i=s;i<=x;i++)
#define FOR(i,x) for(i=0;i<x;i++)
#define Rof(i,s,x) for(i=s;i>=x;i--)
#define all(v) v.begin(),v.end()
#define it(r,v) for(auto r=v.begin();r!=v.end();r++)
#define pb push_back
#define in insert
#define sz size()
#define F first
#define S second
#define nl cout<<"\n"
#define pr(a) cout<<a<<" "
#define pr2(a,b) cout<<a<<" "<<b<<"\n"
#define pr3(a,b,c) cout<<a<<" "<<b<<" "<<c<<"\n"
#define mod 1000000007
const double PI = 3.141592653589793238;

int main()
{
	FASTIO;


        ll i,j,k,x,y,z,n,m,a,b,c,c1=1,c2=0,flag=0,sum=0;
        char ch;
        string s;

        cin>>n;
        vector<string>ss;
        while(n>0)
        {

            m=n%26;
            if(m==0)
                m=26;
            n=n-1;
            n=n/26;

            m=m+96;
            ch=m;
            s=ch;
            ss.push_back(s);

        }

        x=ss.size();
        for(i=x-1;i>=0;i--)
            cout<<ss[i];



}


Measured memory_bytes: 3656