akhauriyash/Code-Regression huggingface dataset:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B, C = [], []
for i in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)
data = {}
SUM = 0
for i in range(N):
    SUM += A[i]
    if A[i] in data:
        data[A[i]] += 1
    else:
        data[A[i]] = 1
for i in range(Q):
    if B[i] in data:
        SUM += data[B[i]]*(C[i]-B[i])
        if C[i] in data:
            data[C[i]] += data[B[i]]
        else:
            data[C[i]] = data[B[i]]
        data[B[i]] = 0
    print(SUM)

Measured memory_bytes: 100060
#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 debug(x)  cerr << #x << " = " << (x) << endl;
#define leftunique(a) {sort((a).begin(),(a).end());(a).erase(unique((a).begin(),(a).end()),(a).end());}
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; }
static const ll INF = 1LL << 60;
//Write From this Line

int main()
{
	double l;
	cin >> l;
	double ans = pow(l,3) / 27;
	printf("%.10f\n",ans);
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vi2 = vector<vector<int>>;
using vb = vector<bool>;
using vb2 = vector<vector<bool>>;
using vc = vector<char>;
using vc2 = vector<vector<char>>;
using vs = vector<string>;
using si = set<int>;
using vs2 = vector<vector<string>>;
using pii = pair<int, int>;
using mii = map<int, int>;
const int INF = 1e9;
const ll LINF = 1e18;
#define MP(a,b) make_pair((a),(b))
#define MT(...) make_tuple(__VA_ARGS__)
#define sz(x) (int)(x).size()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)

#define ALL(x) (x).begin(),(x).end()
#define IN(type,a) type a;cin >> a
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define COUT(x) cout << (x) << endl
#define DCOUT(x,n) cout << fixed << setprecision(n) << (x) << endl
template<typename T>inline istream& operator>>(istream&i,vector<T>&v)
{REP(j,sz(v))i>>v[j];return i;}
template<typename T>
T gcd(T x, T y){
    if ((x==1)||(y==1)) return 1;
    if (x>y) {
        if (x%y==0) {
            return y;
        }else{
            return gcd(y, x%y);
        }
    }else{
        if (y%x==0) {
            return x;
        }else{
            return gcd(x, y%x);
        }
    }
}

int main() {
    IN(int, n);
    IN(int, m);
    vector<bool> check(n,true);
    vi h(n);
    cin >> h;
    REP(i, m){
        IN(int, a);
        IN(int, b);
        if (h[a-1]==h[b-1]) {
            check[a-1]=false;
            check[b-1]=false;
        }else if (h[a-1]>h[b-1]){
            check[b-1]=false;
        }else{
            check[a-1]=false;
        }
    }
    int ans = 0;
    REP(i, n){
        if (check[i]) {
            ans++;
        }
    }
    COUT(ans);
}


Measured memory_bytes: 3784
#include<iostream>
#include<vector>
#include<bitset>
using namespace std;

int main(void)
{
	
	int n, m;
	cin >> n >> m;
	vector<int> h(n);
	for (auto &&i : h)
	{
		cin >> i;
	}

	int a, b;
	vector<bool> ans(n, true);
	for (int i = 0; i < m; i++)
	{
		cin >> a >> b;
		ans[a-1] = ans[a-1] && (h[a-1] > h[b-1]);
		ans[b-1] = ans[b-1] && (h[a-1] < h[b-1]);
	}
	
	int count = 0;
	for (auto &&i : ans)
	{
		if (i) count++;
	}
	cout << count << endl;
	
	
	
	return 0;
	
}

Measured memory_bytes: 3772
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#define int long long
#define endl '\n'
#define INF 1000000000000000000
#define EPS 1e-10
#define PI 3.141592653589793238
#define all(a) a.begin(),a.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define double long double
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<P, ll> PP;
typedef complex<double> com;
template<class T, class S> bool chmax(T &a, const S &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T, class S> bool chmin(T &a, const S &b) { if (a > b) { a = b; return 1; } return 0; }
ll gcd(ll n, ll m) { return (m ? gcd(m, n%m) : n); }
ll lcm(ll n, ll m) { return n / gcd(n, m)*m; }


signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(20);
	//modcalc();

	int n;
	cin >> n;
	vector<int> a(n);
	int ans = 1, cnt = 1;
	rep(i, n) {
		cin >> a[i];
		ans *= 3;
		if (a[i] % 2 == 0) cnt *= 2;
	}
	cout << ans - cnt << endl;
}

Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Console;
using static System.Math;

namespace At
{
    class Program
    {
        static void Main(string[] args)
        {
            var r = ReadInts();

            if (r[0] % r[1] == 0)
            {
                WriteLine(0);
            }
            else
            {
                WriteLine(1);
            }

        }

        private static string Read() { return ReadLine(); }
        private static string[] Reads() { return (Read().Split()); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}


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

int main() {
  int N;
  cin>>N;
  set<int> st;
  for(int i=1;i<=9;i++){
    for(int j=1;j<=9;j++){
      st.insert(i*j);
    }
  }
  if (st.count(N)) puts("Yes");
  else puts("No");
  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll MOD = (1e+9)+7;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-11;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
typedef pair<P, int> PP;
int main(){
	ll a, b, c, d, x;
	cin >> a >> b >> x >> c >> d >> x >> x;
	ll out = a + b + c + d;
	if (a % 2)out--; if (c % 2)out--; if (d % 2)out--;
	if (a > 0 && c > 0 && d > 0) {
		ll sum = (ll)3+b; a--; c--; d--;
		sum += a + c + d;
		if (a % 2)sum--; if (c % 2)sum--; if (d % 2)sum--;
		out = max(out, sum);
	}
	cout << out << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
const int INF = 1<<30; 
const int MOD = 1e9 + 7;
const int dy[] = {1, 0, -1, 0};
const int dx[] = {0, 1, 0, -1};
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int q;
    cin >> q;
    set<int> st;
    for(int i = 0; i < q; i++)
    {
        int com, x;
        cin >> com >> x;
        if(com == 0)
        {
            st.insert(x);
            cout << st.size() << endl;
        }
        else if(com == 1)
        {
            cout << (st.find(x) != st.end() ? 1 : 0) << endl;
        }
        else if(com == 2)
        {
            st.erase(x);
        }
        else if(com == 3)
        {
            int y;
            cin >> y;
            auto it = st.lower_bound(x);
            auto last = st.upper_bound(y);
            while(it != last)
            {
                cout << *it << endl;
                it++;
            }
        }
    }
    return 0;
}




Measured memory_bytes: 12424
N = int(raw_input())
num_list = map(int, raw_input().split())

c = 0
flag = 1
while flag==1:
    flag =0
    for j in range(N-1,0,-1):
        if num_list[j] < num_list[j-1]:
            a = num_list[j]
            num_list[j] = num_list[j-1]
            num_list[j-1] = a
            c += 1
            flag =1

print " ".join(map(str,num_list))
print c

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

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    double a, b, h, m;
    cin >> a >> b >> h >> m;
    double ji = h * 30. + m * 30. / 60.;
    double hu = m * 6.;
    double ka = fabs(ji - hu);
    ka = min(ka, 360-ka);
    double le = sqrt(a*a + b*b - 2. * a * b * cos(ka * M_PI / 180.));
    cout << fixed << setprecision(10) << le << '\n';

    return 0;
}


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

using namespace std;

int gcd(int a, int b) {
	if (b == 0) return a;
	return gcd(b, a%b);
}

void Solve() {
	int n, k;
	cin >> n >> k;
	vector<int> a(n+1);

	int g = 0, m= 0;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
		g = gcd(g, a[i]);
		m = max(m, a[i]);
	}
	
	if (k <= m && k % g == 0) {
		cout << "POSSIBLE\n";
	} else {
		cout << "IMPOSSIBLE\n";
	}
}

int main() {

	int tests = 1;
	for (;tests; --tests) {
		Solve();
	}

}

Measured memory_bytes: 640
#!/usr/bin/env python3


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

    ls = []
    for i in range(n):
        p = list(map(int, input().split()))
        np = (p[0] + p[1], p[0] - p[1])
        ls.append(np)

    minx = float("inf")
    miny = float("inf")
    maxx = -float("inf")
    maxy = -float("inf")

    for i in range(len(ls)):
        x, y = ls[i]
        minx = min(minx, x)
        miny = min(miny, y)
        maxx = max(maxx, x)
        maxy = max(maxy, y)

    print(max(maxx - minx, maxy - miny))


main()



Measured memory_bytes: 35960
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri May 26 01:33:48 2017

@author: kenta
"""

N = int(input())
a = [int(i) for i in input().split()]

hai = 0;cha = 0;midori = 0; mizu = 0;ao = 0;ki = 0;dai = 0;aka = 0;ue=0
for i in range(N):
    if 1<= a[i] < 400:
        hai +=1
    elif 400<= a[i] < 800:
        cha += 1
    elif 800<= a[i] < 1200:
        midori += 1
    elif 1200<= a[i] < 1600:
        mizu += 1
    elif 1600<= a[i] < 2000:
        ao += 1    
    elif 2000<= a[i] < 2400:
        ki += 1 
    elif 2400<= a[i] < 2800:
        dai += 1
    elif 2800<= a[i] < 3200:
        aka += 1
    else:
        ue += 1

count1 = 0
if hai > 0:
    count1 += 1
if cha > 0:
    count1 += 1
if midori> 0:
    count1 += 1
if mizu > 0:
    count1 += 1
if ao > 0:
    count1 += 1
if ki > 0:
    count1 += 1
if dai > 0:
    count1 += 1
if aka > 0:
    count1 += 1

countmin = count1
if countmin == 0:
    if ue >0:
        countmin += 1
countmax = count1+ue
print("{} {}".format(countmin, countmax))


Measured memory_bytes: 3064
import math

a = list([int(input()) for _ in range(5)])



diff = [0 if (ai%10)==0 else 10-(ai%10) for ai in a]
max_diff=max(diff)

a10 = [math.ceil(ai/10)*10 for ai in a]

ret=sum(a10)-max_diff

print(ret)

Measured memory_bytes: 3060
#include <bits/stdc++.h>
#include <limits>
#include <math.h>
#include <cmath>
#include <cctype>
#include <cstdio>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pll = pair<ll, ll> ;
using vs = vector<string>;
using vb = vector<bool>;
using vll = vector<long long>;
using vvll = vector<vector<long long >>;
#define PI  3.1415926535
#define rep(i, k, n) for(ll i = k; i < n; i++)

int main(){
  ll n, m, q; cin >> n >> m >> q;
  
  
  vvll per (100000,vll(10));
  ll num = 0;
  rep(i1, 1, m+1){
    rep(i2, i1, m+1){
      rep(i3, i2, m+1){
        rep(i4, i3, m+1){
          rep(i5, i4, m+1){
            rep(i6, i5, m+1){
              rep(i7, i6, m+1){
                rep(i8, i7, m+1){
                  rep(i9, i8, m+1){
                    rep(i10, i9, m+1){
                      per[num][0] = i1;
                      per[num][1] = i2;
                      per[num][2] = i3;
                      per[num][3] = i4;
                      per[num][4] = i5;
                      per[num][5] = i6;
                      per[num][6] = i7;
                      per[num][7] = i8;
                      per[num][8] = i9;
                      per[num][9] = i10;
                      num++;
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  
  vvll query (q,vll(4));
  rep(i, 0, q){
    cin >> query[i][0] >> query[i][1] >> query[i][2] >> query[i][3];
  }
  
  ll ans = 0;
  rep(i, 1, num+1){
    ll score = 0;
    rep(j, 0, q){
      if(per[i][query[j][1]-1] - per[i][query[j][0]-1] == query[j][2]){
        score += query[j][3];
      }
    }
    ans = max(ans, score);
  }
  cout << ans << endl;
}
      


Measured memory_bytes: 14860
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

class UnionFindTree{

private:
  // indexÌÌem[h
  vector<int> par;
  // indexðªÆ·éØÌN
  vector<int> rank;
  // ØÌÅål
  int treeSize;
  
public:
  UnionFindTree(int initTreeSize = 1000){
     // øÅ^¦çê½lðÅåi[Æ·éUnionFindTreeÌì¬
    treeSize = initTreeSize;
    init();
  }
  
  
  void init(){
    
    for(int i = 0; i < treeSize; i++){
      par.push_back(i);
      rank.push_back(0);
    }
    
  }

  // ^¦çê½ªi[³êÄ¢éØÌ[gðTõ
  int find(int x){
    
    if(par[x] == x){
      return x;
    }
    else{
      return par[x] = find(par[x]);
    }
    
  }
  
  void unite(int x,int y){
    
    x = find(x);
    y = find(y);
    
    if(x == y)
      return;
    
    if(rank[x] < rank[y]){
      par[x] = y;
    }
    else{
      par[y] = x;
      if(rank[y] == rank[x]){
	rank[x]++;
      }
    }
  }

  int size(){
	  return treeSize;
  }
  
  bool same(int x,int y){
    return find(x) == find(y);
  }

};

class edge{
public:
	int u,v,cost;

	bool operator<(const edge &e)const{
		return e.cost>this->cost;
	}
};
edge es[10001];
int V,E;
int s,d;

int kruskal(){
	sort(es,es+E);
	UnionFindTree uft(V);
	int res=0;
	for(int k = 0; k < E; k++){
		edge &e=es[k];
		// 2ÂÈãÌM¹ðêÂÌssÉÂÈ°éÌÍ³Ê
		if(e.u>=s&&e.v<s){
			int netu=e.v;
			bool isc=false;
			for(int j = 0; j < s; j++){
				if(uft.same(j,e.u)){
					isc=true;
					break;
				}
			}
			if(isc){
				continue;
			}
		}
		else if(e.v>=s&&e.u<s){
			int netu=e.u;
			bool isc=false;
			for(int j = 0; j < s; j++){
				if(uft.same(j,e.v)){
					isc=true;
					break;
				}
			}
			if(isc){
				continue;
			}
		}
		// ¼ûÌssª¢¸ê©ÌM¹ÆqªÁÄ¢éê
		else{
			bool isc=false;
			for(int j = 0; j < s; j++){
				if(uft.same(j,e.v)){
					isc=true;
					break;
				}
			}
			bool isc2=false;
			for(int j = 0; j < s; j++){
				if(uft.same(j,e.u)){
					isc2=true;
					break;
				}
			}
			if(isc&&isc2)
				continue;
		}

		if(!uft.same(e.u,e.v)){
			uft.unite(e.u,e.v);
			res+=e.cost;
			bool f=true;
			for(int i = 0; i < d; i++){
				bool isc=false;
				for(int j = 0; j < s; j++){
					if(uft.same(j,i+s)){
						isc=true;
						break;
					}
				}
				if(!isc){
					f=false;
					break;
				}
			}
			if(f)
				break;
		}
	}
	return res;
}

int main(){
	
	while(cin>>s>>d&&!(s==0&&d==0)){

		E=V=0;
		V=s+d;
		for(int i = 0; i < s; i++){
			edge e;
			e.u=i;
			for(int j = 0; j < d; j++){
				e.v=j+s;
				cin>>e.cost;
				if(e.cost!=0){
					es[E]=e;
					E++;
				}
			}
		}
		for(int i = 0; i < d-1; i++){
			edge e;
			e.u=i+s;
			for(int j = i+1; j < d; j++){
				e.v=j+s;
				cin>>e.cost;
				if(e.cost!=0){
					es[E]=e;
					E++;
				}
			}
		}
		cout<<kruskal()<<endl;
	}
  
	return 0;
}

Measured memory_bytes: 0
# coding:utf-8

import sys

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    n = II()
    P = [II() for _ in range(n)]

    # dp[i]: 末尾がiになる正しい順序に並んでいる部分列の長さ
    dp = [0] * (n + 1)
    for i in range(n):
        dp[P[i]] += dp[P[i] - 1] + 1

    return n - max(dp)


print(main())


Measured memory_bytes: 18860
#include<bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef pair<int,int> P;

int main(){
	string s;
	cin>>s;
	if(s.size()==2&&s[0]==s[1]){
		cout<<1<<endl;
		return 0;
	}
	int ans=s.size();
	for(int i=1;i<s.size();i++){
		if(s[i]==s[i-1])ans--,i+=2;
	}
	cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 640
#include<bits/stdc++.h>
#define F(a,b,c,d) for (register int a=b;a<=c;a+=d)
#define FD(a,b,c,d) for (register int a=b;a>=c;a-=d)
using namespace std;
int main(){
	string st,st1;
	cin>>st>>st1;
	for (int i=1;i<=st.size();i++)
	{
		if (st==st1)
		{
			cout<<"Yes"<<endl;
			return 0;
		}
		st=st[st.size()-1]+st.substr(0,st.size()-1);
	}
	cout<<"No"<<endl;
	return 0;
}



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

using int32 = int_fast32_t;
using int64 = int_fast64_t;

const int32 INF = 1e9;
const int32 MOD = 1e9+7;
const int64 LLINF = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n"  )
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n"  )
#define ANS(n) cout << (n) << "\n"
#define REP(i,n) for(int64 i=0;i<(n);++i)
#define FOR(i,a,b) for(int64 i=(a);i<(b);i++)
#define FORR(i,a,b) for(int64 i=(a);i>=(b);i--)
#define ALL(obj) (obj).begin(),(obj).end()
#define pii pair<int32,int32>
#define pll pair<int64,int64>
#define pb(a) push_back(a)
#define mp make_pair

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    vector<vector<char>> ans(100, vector<char>(100, '.'));
    REP(i,50)REP(j,100)ans[i][j] = '#';
    int32 a, b;
    cin >> a >> b;
    a--;b--;
    int32 x = 0;
    int32 y = 0;
    while(a > 0){
        if(y >= 100){
            x += 2;
            y = x % 2;
        }
        ans[x][y] = '.';
        a--;
        y += 2;
    }
    x = 0;
    y = 0;
    while(b > 0){
        if(y >= 100){
            x += 2;
            y = x % 2;
        }
        ans[99-x][99-y] = '#';
        b--;
        y += 2;
    }
    cout << "100 100\n";
    REP(i,100){
        REP(j,100)cout << ans[i][j];
        cout << endl;
    }
    return 0;
}

Measured memory_bytes: 256
import java.util.*;

public class Main {

    Scanner sc = new Scanner(System.in);

    int[] dx = {1, 0};
    int[] dy = {0, 1};
    int h = sc.nextInt();
    int w = sc.nextInt();
    char[][] arr = new char[h][w];
    int[][] dp = new int[h+1][w+1];

    public static void main(String[] args) {
        new Main().run();
    }

    void run() {

        for (int i=0; i<h; i++) arr[i] = sc.next().toCharArray();

//        int res = Integer.MAX_VALUE;


        for (int i=0; i<w+1; i++) {
            dp[0][i] = (int)1e5;
        }

        for (int i=0; i<h+1; i++) {
            dp[i][0] = (int)1e5;
        }

        if (arr[0][0] == '#') dp[1][1] = 1;

        bfs(new Pair(0, 0));

        for (int i=0; i<=h; i++) {
            for (int j=0; j<=w-1; j++) {
//                System.out.print(dp[i][j] + " ");
            }
//            System.out.println(dp[i][w]);
        }

        System.out.println(dp[h][w]);

    }

    void bfs(Pair pair) {
        Queue<Pair> queue = new ArrayDeque<>();
        queue.add(pair);
        while (!queue.isEmpty()) {
            Pair p = queue.peek();
            for (int i=0; i<dx.length; i++) {
                int nx = p.x + dx[i];
                int ny = p.y + dy[i];
                if (h <= nx || w <= ny) continue;
                if (dp[nx+1][ny+1] != 0) continue;
                if (arr[nx][ny] == '.') {
                    dp[nx+1][ny+1] = Math.min(dp[nx][ny+1], dp[nx+1][ny]);
                } else {
                    if (1 <= nx && arr[nx-1][ny] == '.') {
                        if (1 <= ny && arr[nx][ny-1] == '.') {
                            dp[nx+1][ny+1] = Math.min(dp[nx][ny+1]+1, dp[nx+1][ny]+1);
                        } else {
                            dp[nx+1][ny+1] = Math.min(dp[nx][ny+1]+1, dp[nx+1][ny]);
                        }
                    } else {
                        if (1 <= ny && arr[nx][ny-1] == '.') {
                            dp[nx+1][ny+1] = Math.min(dp[nx][ny+1], dp[nx+1][ny]+1);
                        } else {
                            dp[nx+1][ny+1] = Math.min(dp[nx][ny+1], dp[nx+1][ny]);
                        }
                    }
                }
                queue.add(new Pair(nx, ny));
            }
            queue.poll();
        }
    }


    class Pair {
        int x;
        int y;
        public Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }


}


Measured memory_bytes: 22404
// Problem : 
// 			C - Walking Takahashi
// 			Editorial
// 		
// Contest : AtCoder - AtCoder Beginner Contest 175
// URL : https://atcoder.jp/contests/abc175/tasks/abc175_c
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define sort(a) sort(all(a))
#define ff first
#define ss second
#define vi vector<int>
#define pii pair<int, int>
#define vll vector<ll>
#define pll pair<ll,ll>
#define mod (int)1e9+7
#define FAST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define repf(i,a,b) for(i=a;i<b;i++)
#define repb(i,a,b) for(i=a;i>=b;i--)
#define N 100005
int main()
{
	FAST
    int t=1;
    // cin>>t;
    while(t--)
    {
		ll x, k, d, ans, cnt;
		cin>>x>>k>>d;
		if(x<0) x = -x;
	    if(x/d>=k) ans = x-k*d;
	    else{
	        k -= x/d;
	        cnt = x%d;
	        if(k%2==0) ans = cnt;
	        else ans = d - cnt;
	    }
	    cout << ans ;
    }
    return 0;
} 


Measured memory_bytes: 3604
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
#define all(x) x.begin(),x.end()

template<class T>
static inline std::vector<T> ndvec(size_t&& n, T val) noexcept {
  return std::vector<T>(n, std::forward<T>(val));
}

template<class... Tail>
static inline auto ndvec(size_t&& n, Tail&&... tail) noexcept {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(n, ndvec(std::forward<Tail>(tail)...));
}

int main() {
  i64 N, K;
  cin >> N >> K;
  i64 ans = 0;
  while(N > 0) {
    ans += 1;
    N /= K;
  }
  cout << ans << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
using Graph = vector<vector<int>>;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    // 頂点数と辺数
    int N, M;
    cin >> N >> M;

    // グラフ入力受取 (ここでは無向グラフを想定)
    Graph G(N);
    for (int i = 0; i < M; ++i)
    {
        int a, b;
        cin >> a >> b;
        --a, --b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    vector<int> dist(N, -1); // 全頂点を「未訪問」に初期化
    vector<int> prev(N, -1);
    queue<int> que;

    dist[0] = 0;
    que.push(0);

    while (!que.empty())
    {
        int v = que.front();
        que.pop();

        // v から辿れる頂点をすべて調べる
        for (int nv : G[v])
        {
            if (dist[nv] == -1)
            {
                dist[nv] = dist[v] + 1;
                prev[nv] = v;
                que.push(nv);
            }
        }
    }

    // 結果出力 (各頂点の頂点 0 からの距離を見る)
    cout << "Yes" << endl;
    for (int i = 1; i < N; ++i)
        cout << prev[i] + 1 << endl;
    return 0;
}


Measured memory_bytes: 10572
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
array = []
replace = [0]*(10**5+1)
for i in A:
    replace[i] += 1
sum_s = sum(A)
for i in range(Q):
    m,n = [int(x) for x in input().split()]
    replace[n] += replace[m]

    diff = n*replace[m] - replace[m]*m
    sum_s += diff
    replace[m] = 0
    print(sum_s)

Measured memory_bytes: 20872
n = int(input())

hapiness = [list(map(int,input().split())) for i in range(n)]

dp = [[0]*3 for i in range(n)]

dp[0][0], dp[0][1], dp[0][2] = hapiness[0][0], hapiness[0][1], hapiness[0][2]

for i in range(1,n):
    for j in range(3):
        if j == 0:
            dp[i][j] = max(dp[i-1][1], dp[i-1][2]) + hapiness[i][j]
        
        elif j == 1:
            dp[i][j] = max(dp[i-1][0], dp[i-1][2]) + hapiness[i][j]
        
        else:
            dp[i][j] = max(dp[i-1][1], dp[i-1][0]) + hapiness[i][j]

print(max(dp[n-1]))

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

int main() {
    int q,n;cin>>n>>q;
    stack<int > s[n];
    while(q--){
        int a;cin>>a;
        if(a==0){
            int t,x;cin>>t>>x;
            s[t].push(x);
        }
        else if(a==1){
            int t;cin>>t;
            if(s[t].empty())
                continue;
            cout<<s[t].top()<<endl;
        }
        else if(a==2){
            int t;cin>>t;
            if(s[t].empty())
                continue;
            s[t].pop();
        }
    }
    return 0;
}




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

using namespace std;

int n;
vector<int> adjList[212123];
bool win[212123];

int dist[212123];
int dist2[212123];

int bfs(int s, int *d) {
	int f, fx;
	f = 1, fx = s;
	queue<int> q;
	d[s] = 1;
	q.push(s);
	while (q.size()) {
		int x = q.front();
		q.pop();
		for (auto y : adjList[x]) {
			if (d[y]) continue;
			d[y] = d[x] + 1;
			if (d[y] > f) {
				f = d[y], fx = y;
			}
			q.push(y);
		}
	}
	return fx;
}

int main() {
	win[1] = true;
	win[2] = false;
	for (int i = 3; i < 212123; i++) {
		if (!win[i-1] || !win[i-2]) win[i] = true;
	}

	cin >> n;
	for (int i = 1; i < n; i++) {
		int a, b;
		cin >> a >> b;
		adjList[a].push_back(b);
		adjList[b].push_back(a);
	}
	int a = bfs(1, dist);
	int b = bfs(a, dist2);
	if (win[dist2[b]]) {
		cout << "First\n";
	} else cout << "Second\n";
	return 0;
}


Measured memory_bytes: 14836
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>

using namespace std;

int height,width,ans;

bool rangeCheck(int row,int col){
	if(row >= 0  && row <= height-1 && col >= 0 && col <= width-1)return true;
	else{
		return false;
	}
}

void recursive(short table[20][20],int row,int col,int count){
	if(table[row][col] == 3){
		if(ans == -1)ans = count;
		else{
			ans = min(ans,count);
		}
		return;
	}

	if(ans != -1 && count >= ans)return;
	if(count == 10)return;

	short new_table[4][20][20];

	for(int i = 0; i < 4; i++){
		for(int k = 0; k < height; k++){
			for(int p = 0; p < width; p++)new_table[i][k][p] = table[k][p];
		}
	}


	if(rangeCheck(row-1,col) == true && table[row-1][col] != 1){
		if(table[row-1][col] == 3){
			recursive(table,row-1,col,count+1);
			return;
		}

		for(int k = 2; rangeCheck(row-k,col) == true; k++){
			if(table[row-k][col] == 3){
				recursive(table,row-k,col,count+1);
				return;
			}else if(table[row-k][col] == 1){
				new_table[0][row-k][col] = 0;
				recursive(new_table[0],row-(k-1),col,count+1);
				break;
			}
		}
	}

	if(rangeCheck(row+1,col) == true && table[row+1][col] != 1){
		if(table[row+1][col] == 3){
			recursive(table,row+1,col,count+1);
			return;
		}

		for(int k = 2; rangeCheck(row+k,col) == true; k++){
			if(table[row+k][col] == 3){
				recursive(table,row+k,col,count+1);
				return;
			}else if(table[row+k][col] == 1){
				new_table[1][row+k][col] = 0;
				recursive(new_table[1],row+(k-1),col,count+1);
				break;
			}
		}
	}

	if(rangeCheck(row,col-1) == true && table[row][col-1] != 1){
		if(table[row][col-1] == 3){
			recursive(table,row,col-1,count+1);
			return;
		}

		for(int k = 2; rangeCheck(row,col-k) == true; k++){
			if(table[row][col-k] == 3){
				recursive(table,row,col-k,count+1);
				return;
			}else if(table[row][col-k] == 1){
				new_table[2][row][col-k] = 0;
				recursive(new_table[2],row,col-(k-1),count+1);
				break;
			}
		}
	}

	if(rangeCheck(row,col+1) == true && table[row][col+1] != 1){
		if(table[row][col+1] == 3){
			recursive(table,row,col+1,count+1);
			return;
		}

		for(int k = 2; rangeCheck(row,col+k) == true; k++){
			if(table[row][col+k] == 3){
				recursive(table,row,col+k,count+1);
				return;
			}else if(table[row][col+k] == 1){
				new_table[3][row][col+k] = 0;
				recursive(new_table[3],row,col+(k-1),count+1);
				break;
			}
		}
	}

}

void func(){
	short table[20][20];

	for(int i = 0; i < height; i++){
		for(int k = 0; k < width; k++)scanf("%hd",&table[i][k]);
	}

	short row = -1,col = -1;

	for(int i = 0; i < height; i++){
		for(int k = 0; k < width; k++){
			if(table[i][k] == 2){
				row = i;
				col = k;
				table[i][k] = 0;
				break;
			}
		}
		if(row != -1)break;
	}

	ans = -1;

	recursive(table,row,col,0);

	printf("%d\n",ans);

}

int main(){

	while(true){
		scanf("%d %d",&width,&height);
		if(width == 0 && height == 0)break;

		func();
	}

	return 0;
}

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

using namespace std;

typedef long long ll;

string s, t;
int nr;

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

    cin>>s>>t;
    for (int i=0; i<s.size(); i++)
        if (s[i] == t[i])
            nr++;

    cout<<nr;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(void){
    int n;
    double t,a,h;
    cin>>n>>t>>a;
    double min_num=100000;
    int ans;
    for (int i=0;i<n;i++) {
        cin>>h;
        double tmp=abs(a-(t-h*0.006));
        if (min_num>tmp) {
            min_num=tmp;
            ans=i;
        }
    }
    cout<<ans+1<<endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <numeric>
#include <cmath>

using namespace std;

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

#define all(x) x.begin(),x.end()

const ll mod = 1e9+7;
const ll INF = 1e9;
const ll MAXN = 1e9;

int main()
{
  string s;
  cin >> s;
  int n = s.size();

  if(s[0] == '0' || s[n-1] == '1'){
    cout << -1 << endl;
    return 0;
  }

  s = s[n-1] + s;

  for(int i = 0; i <= n; i++){
    if(s[i] != s[n-i]){
      cout << -1 << endl;
      return 0;
    }
  }

  int now = 2;
  vector<P> ans;
  ans.push_back(P(1,2));
  int v = 3;
  for(int i = 2; i < n; i++){
    if(s[i] == '0'){
      ans.push_back(P(now,v));
      v++;
    }else{
      ans.push_back(P(now,v));
      now = v;
      v++;
    }
  }

  for(int i = 0; i < ans.size(); i++){
    cout << ans[i].first << " " << ans[i].second << endl;
  }


  return 0;
}

Measured memory_bytes: 2416
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
# Aの累積和
a_sum = [0]
for i in range(n):
    a_sum.append(a_sum[i] + a[i])
# Bの累積和
b_sum = [0]
for i in range(m):
    b_sum.append(b_sum[i] + b[i])
b_sum.append(10**20)
ans = 0
# Aを固定する
for i in range(n+1):
    t = a_sum[i]
    l = 0
    r = len(b_sum)
    # Bのうち何冊目まで読めるか二分探索する
    while l+1<r:
        # c:探索範囲の中央
        c = (l + r) // 2
        # 中央がK以下の場合は、求めたい位置が右にあるので
        # 探索範囲の左端を中央にする
        if t+b_sum[c]<=k:
            l = c
        # 中央がKよりも大きい場合は、求めたい位置が左にあるので
        # 探索範囲の右端を中央にする
        else:
            r = c
    # 合計がKを超えないか確認する
    if a_sum[i]+b_sum[l]<=k:
        ans = max(ans,i+l)
print(ans)

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

int main(){
  int N;
  cin >> N;
  vector<int> s(N);
  for(int i=0;i<N;i++){
    cin >> s[i];
  }
  int cnt=1;
  for(int i=0;i<N;i++){
    if(s[i]%2==0){
     if(s[i]%3!=0&&s[i]%5!=0){
       cnt=0;
       break;
     }
    }
  }
  
  if(cnt){
    cout << "APPROVED" <<endl;
  }
  else{
    cout << "DENIED" <<endl;
  }
}


Measured memory_bytes: 3596
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vi64 = vector<int64_t>;
using vvi64 = vector<vi64>;
#define rep(i, n) for (int64_t i = 0; i < (n); ++i)
#define repe(i, n) for (int64_t i = 0; i <= (n); ++i)
#define reps(i, s, n) for (int64_t i = (s); i < (n); ++i)
#define repse(i, s, n) for (int64_t i = (s); i <= (n); ++i)
#define repr(i, n) for (int64_t i = (n); i >= 0; --i)
#define all(xs) (xs).begin(), (xs).end()
#define pp_vec(xs) rep (i, (xs).size()) if { (i) cout << endl; cout << (xs)[i] << " "; };
const int64_t inf = (int64_t)1e18;
const int64_t mod = (int64_t)1e9 + 7;


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

  int A, P; cin >> A >> P;
  cout << (A * 3 + P) / 2 << endl;

  return 0;
}

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

dp = [float('inf')] * 100010

dp[0] = 0

def chmin(a, b):
    if a > b:
        return b
    else:
        return a

for i in range(1, N):
    dp[i] = chmin(dp[i], dp[i-1] + abs(h[i] - h[i-1]))
    if i > 1:
        dp[i] = chmin(dp[i], dp[i-2] + abs(h[i] - h[i-2]))

print(dp[N-1])


Measured memory_bytes: 13924
program main
  implicit none
  character(10**5)::S
  integer::i
  read*,S
  do i=1,len_trim(S)-1
    if(S(i:i)==S(i+1:i+1))then
      print"(i0,A,i0)",i," ",i+1
      stop
    endif
  end do
  do i=1,len_trim(S)-2
    if(S(i:i)==S(i+2:i+2))then
      print"(i0,A,i0)",i," ",i+2
      stop
    endif
  end do
  print"(i0,A,i0)",-1," ",-1
end program main

Measured memory_bytes: 768
#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define int long long
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e18;

signed main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    int n = s.size();
    int a = 0, b = 0;
    rep(i, 0, n){
        if(s[i] == '0') a++;
        else b++;
    }
    cout << min(a, b) * 2 << endl;
}

Measured memory_bytes: 512
#include <iostream>
#include <vector>
 
int main(void) {
  int n;
  while (std::cin >> n) {
    int ans = 0;
    for (int i = 0; i <= 9; i++) {
      for (int j = 0; j <= 9; j++) {
        for (int k = 0; k <= 9; k++) {
          for (int l = 0; l <= 9; l++) {
            if(i + j + k + l == n)ans++;
          }
        }
      }
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

Measured memory_bytes: 3060
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <numeric> // accumulate(v.begin(), v.end(), 0)

using namespace std;
#define ll long long

int main(){
  int A, B, T;
  cin >> A >> B >> T;
  cout << floor((T + 0.5) / A) * B << endl;
  return 0;
}

Measured memory_bytes: 256
#include<iostream>

using namespace std;

int main(){
  int workers_num;
  cin >> workers_num;
  int subordinates[workers_num] = {};
  int bosses;
  
  for(int i=0;i < workers_num-1;i++){
    cin >> bosses;
    subordinates[bosses-1]++;
  }
  
  for(int i=0;i < workers_num-1;i++){
    cout << subordinates[i] << endl;
  }
  
  cout << 0;
  return 0;
}


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

int main() {
	int N;
	cin >> N;
	vector<int> a(N);
	double ave = 0;
	rep(i, N) {
		cin >> a.at(i);
		ave += a.at(i);
	}
	ave /= N;
	int ans = 0;
	for (int i = 1;i < N;++i) {
		if (abs(a.at(i) - ave) < abs(a.at(ans) - ave)) ans = i;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
w = input()
from collections import defaultdict
dic = defaultdict(lambda : 0)
for i in w:
    dic[i] += 1
counter = 0
for key in dic.keys():
    if dic[key] % 2 != 0:
        counter = -1
        break
if counter == 0:
    print('Yes')
else:
    print('No')

Measured memory_bytes: 3316
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
  ll a,b,k;
  cin>>a>>b>>k;
  for(int i=0;i<k;i++){
    if(i%2==0){
      if(a%2==1)
        a--;
      b+=a/2;
      a/=2;
    }else{
      if(b%2==1)
        b--;
      a+=b/2;
      b/=2;
    }
  }
  cout<<a<<" "<<b;
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<bitset>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<functional>
using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
//#define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592

using ll = long long;
using Pi = pair<int,int>;
using Pl = pair<ll,ll>;
struct Combination{
    vector<long long> _fac,_finv,_inv;
    const int m = MOD;

    Combination(int sz) : _fac(sz),_finv(sz),_inv(sz){
        _fac[0] = _fac[1] = 1;
        _finv[0] = _finv[1] = 1;
        _inv[1] = 1;
        for(int i = 2; i <= sz; i++){
            _fac[i] = _fac[i-1]*i%m;
            _inv[i] = m-_inv[m%i]*(m/i)%m;
            _finv[i] = _finv[i-1]*_inv[i]%m;
        }
    }

    inline int fac(int n){return _fac[n];}
    inline int inv(int n){return _inv[n];}
    inline int finv(int n){return _finv[n];}

    long long comb(int n,int k){
        if(n < k) return 0;
        if(n < 0 || k < 0) return 0;
        return _fac[n]*(_finv[k]*_finv[n-k]%m)%m;
    }
};
int main(){
    int N,K; cin >> N >> K;
    Combination C(2020);
    repr(i,1,K+1){
        int ans = C.comb(K-1,i-1)*C.comb(N-K+1,i)%MOD;
        cout << ans << endl;
    }
}
    



Measured memory_bytes: 256
#include <iostream>
#include <cassert>
#include <vector>
#include <algorithm>

int N;
std::vector<int> c;
std::vector<int> score;
int current;

using Graph = std::vector<std::vector<int>>;
Graph G;

void dfs(const int cv, const int pv) {
  score[cv] = c[current++];
  for (auto nv : G[cv]) {
    if (nv == pv)
      continue;
    dfs(nv, cv);
  }
}

int main()
{
  // Input
  std::cin >> N;
  G.resize(N);
  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    std::cin >> a >> b;
    G[a - 1].push_back(b - 1);
    G[b - 1].push_back(a - 1);
  }
  c.resize(N);
  for (int i = 0; i < N; ++i)
    std::cin >> c[i];

  // Main
  std::sort(c.begin(), c.end(), std::greater<int>());

  score.assign(N, -1);
  current = 0;
  int root = 0;
  dfs(root, -1);

  // Output
  int res = 0;
  for (int i = 1; i < N; ++i)
    res += c[i];
  std::cout << res << std::endl;
  for (int i = 0; i < N; ++i)
    std::cout << score[i] << " ";

  return 0;
}


Measured memory_bytes: 1408
public partial class IntroductionToProgramming{
	public static void Main(){
		string[] line = System.Console.ReadLine().Split(' ');
		int a = int.Parse(line[0]);
		int b = int.Parse(line[1]);
		int c = int.Parse(line[2]);
		
		int m1;
		int m2;
		int m3;

		m1 = a <= b ? (a <= c ? a : c) : (b <= c ? b : c);
		m2 = a <= b ? (b <= c ? b : (a <= c ? c : a)) : (a <= c ? a : (b <= c ? c: b));
		m3 = a <= b ? (b <= c ? c : b) : (a <= c ? c : a);

		System.Console.WriteLine(m1.ToString() + " " + m2.ToString() + " " + m3.ToString());
	}
}

Measured memory_bytes: 5616
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO 自動生成されたメソッド・スタブ
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int num = Integer.parseInt(br.readLine());
		String a = "ABD";

		if (num < 1000) {
			a = "ABC";
		}
		System.out.println(a);
	}

}


Measured memory_bytes: 21716
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <utility>
#include <iomanip>

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

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9+7;

int solve(const vector<int>& a, int i, int ans){
    return ans - abs(a[i+1] - a[i]) - abs(a[i] - a[i-1]) + abs(a[i+1] - a[i-1]);
}

signed main(){
    int n;
    cin >> n;

    vector<int> a(n+2);
    rep(i, n) cin >> a[i+1];

    int ans = 0;
    rep(i, (int)a.size()-1){
        ans += abs(a[i+1] - a[i]);
    }

    for(int i = 1; i <= n; i++){
        cout << solve(a, i, ans) << endl;
    }
    return 0;
}


Measured memory_bytes: 2680
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<string>
#include<stack>
#include<queue>
#define Vmaxn 100010
#define Emaxn 500010
#define inf 2147483646

using namespace std;

struct Node
{
    int x,y,val,pre;
};
//邻接表存储方法
int g_[Vmaxn];
Node g[Emaxn];

int n,t,Dmax=0;
int h[Vmaxn];

void dfs(int s,int u){
    h[u]=1;
    if(s>Dmax){
        Dmax=s;
        t=u;
    }
    int j=g_[u];
    while(j!=-1)
    {
        int v=g[j].y;
        if(h[v]==0){
            dfs(s+g[j].val,v);
        }
        j=g[j].pre;
    }
}

int main()
{
    int i;
    cin>>n;
    memset(g_,-1,sizeof g_);
    for(i=0;i<2*(n-1);i++)
    {
        cin>>g[i].x>>g[i].y>>g[i].val;
        g[i].pre=g_[g[i].x];
        g_[g[i].x]=i;

        g[i+1].x=g[i].y;g[i+1].y=g[i].x;g[i+1].val=g[i].val;
        g[i+1].pre=g_[g[i+1].x];
        g_[g[i+1].x]=i+1;
        i++;
    }
    memset(h,0,sizeof h);
    dfs(0,0);

    Dmax=0;
    memset(h,0,sizeof h);
    dfs(0,t);
    cout<<Dmax<<endl;

    return 0;
}



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

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define ull unsigned long long
#define ld long double

template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;

#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) rep2(i, 0, n)
#define rep1(i, n) rep1(i, 1, n + 1)

// #define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
// #define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()  //

#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair

#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)

// 総数を1000000007（素数）で割った余り
const long long mod = 1e9 + 7;

const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};

ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << std::fixed << std::setprecision(15);
    //    cout << std::setfill('0') << std::setw(2);

    string S;
    std::cin >> S;

    int n = S.size();  // 桁

    ll ans = 0;
    rep(i, 1 << n - 1) {
        ll temp = 0;
        rep(j, n - 1) {
            if (i >> j & 1) {
                ans += temp * 10 + S[j] - '0';
                temp = 0;
            } else {
                temp = temp * 10 + S[j] - '0';
            }
        }
        ans += temp * 10 + S[n - 1] - '0';
    }
    cout << ans << endl;

    return 0;
}


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

bool dp[1000005];
int cnt[1000005];

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	dp[1] = true;
	int x, y;
	rep(i,m){
		cin >> x >> y;
		dp[y] |= dp[x];
		cnt[x]--;
		cnt[y]++;
		if(cnt[x] == -1) dp[x] = false;
	}
	int ans = 0;
	rep(i,n){
		if(dp[i+1]) ans++;
	}
	cout << ans << endl;
	return 0;
}


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

if __name__ == '__main__':
    N = int(input())
    A = []
    for i in range(N):
        A.append(int(input()))

    B = set()
    for a in A:
        if a not in B:
            B.add(a)
        else:
            B.remove(a)

    print(len(B))


Measured memory_bytes: 13140
#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>;

int main() {
  int a, b, k;
  cin >> a >> b >> k;
  if (b - a + 1 < 2 * k) {
    for (int i = a; i <= b; ++i) cout << i << endl;
  } else {
    rep(i, k) cout << a + i << endl;
    rep(i, k) cout << b - k + 1 + i << endl;
  }
  return 0;
}

Measured memory_bytes: 3572
#!/usr/bin/env python3
import sys
INF = float("inf")
from collections import deque

import heapq


class MaxHeap(object):
    def __init__(self, x):
        self.heap = [-e for e in x]
        heapq.heapify(self.heap)

    def push(self, value):
        heapq.heappush(self.heap, -value)

    def pop(self):
        return -heapq.heappop(self.heap)


class MinHeap(object):
    def __init__(self, x):
        self.heap = [e for e in x]
        heapq.heapify(self.heap)

    def push(self, value):
        heapq.heappush(self.heap, value)

    def pop(self):
        return heapq.heappop(self.heap)


def solve(N: int, a: "List[int]"):

    mae = MinHeap(a[:N])
    mae_tot = [sum(a[:N])]
    for curr in range(N, 2*N):
        mae.push(a[curr])
        mmin = mae.pop()
        mae_tot.append(mae_tot[-1]+a[curr]-mmin)

    ushiro = MaxHeap(a[2*N:])
    ushiro_tot = [sum(a[2*N:])]
    for curr in range(2*N-1, N-1, -1):
        ushiro.push(a[curr])
        umax = ushiro.pop()
        ushiro_tot.append(ushiro_tot[-1]+a[curr]-umax)

    m = -INF
    for mt, ut in zip(mae_tot, reversed(ushiro_tot)):
        m = max(m, mt-ut)
    print(m)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(3*N)]  # type: "List[int]"
    solve(N, a)


if __name__ == '__main__':
    main()


Measured memory_bytes: 53364
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define mo(x) (x%mod+mod)%mod

using namespace std;

const int mod = 1e9+7;
const int maxn = 1000010;
typedef long long ll;
int inv[maxn], jc[maxn], ijc[maxn];
int n = 0;

int C(int n, int m) {
    if (n < m) return 0;
    return 1ll*jc[n]*ijc[m]%mod*ijc[n-m]%mod;
}

int main() {
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
    scanf("%d", &n);
    inv[1] = 1; jc[0] = ijc[0] = 1;
    for (int i = 2; i <= n; i++) inv[i] = mo(-1ll*inv[mod%i]*(mod/i)%mod);
    for (int i = 1; i <= n; i++) {
        jc[i] = 1ll*jc[i-1]*i%mod;
        ijc[i] = 1ll*ijc[i-1]*inv[i]%mod;
    }
    int ans = 1ll*n*jc[n-1]%mod;
    for (int i = 1; i <= n; i++) {
        int t = 1ll*C(i-1, n-i-1)*jc[i]%mod*jc[n-1-i]%mod;
        ans = mo(ans-t);
    }
    printf("%d\n", ans);
    return 0;
}


Measured memory_bytes: 11904
import math
A,B,H,M = map(int,input().split())

theta = abs(30*H+M/2-6*M)
if theta == 180:
    print(A+B)
else:
    if theta > 180:
        theta = 360-theta

    theta = math.radians(theta)
    print((A**2+B**2-2*A*B*math.cos(theta))**0.5)


Measured memory_bytes: 62568
from sys import stdin
import copy

n = int(input())
S = [stdin.readline()[:-1] for i in [0]*(n)]
#S.append(stdin.readline())
s = []
for i in S:
    s.append("".join(sorted(i)))
s.sort()
arr = []
temp = 1
for i in range(n-1):
    if temp>1 and s[i]!=s[i+1]:
        arr.append(temp)
        temp = 1
    if s[i]==s[i+1]:
        temp += 1
if temp>1:
    arr.append(temp)

ans = 0
for i in arr:
    ans += i*(i-1)//2
print(ans)


Measured memory_bytes: 24028