akhauriyash/Code-Regression huggingface dataset:
/* ADS2016 FINAL CONTEST :) */
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <string>
#include <cstdio>

#define pb push_back
#define mk make_pair

using namespace std;

int main () {

	// freopen("fileName.in", "r", stdin);
	// freopen("fileName.out", "w", stdout);

	stack <char> st;

	string s;
	getline(cin,s);

	for(int i = 0; i < s.length(); i++){
		if(st.empty() || !(st.top() == 'S' && s[i] == 'T'))
			st.push(s[i]);
		else
			st.pop();
	}

	cout << st.size() << endl;
}

Measured memory_bytes: 644
#include <bits/stdc++.h>
using ll = long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define ALL(x) x.begin(),x.end()
const ll LINF = 1e18;
using namespace std;



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

    ll n;
    cin >> n;
    ll sum = LINF;
    for (ll i = 1; i*i <= n; ++i) {
        if (n % i == 0) {
            sum = min(sum, (i -1) + (n/i -1));
        }
    }
    cout << sum << endl;
    return 0;
}

Measured memory_bytes: 3640
import java.util.*;

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

        while(true){
            int h = sc.nextInt();
            int w = sc.nextInt();

            if(h==0 && w==0){
                break;
            }else{
                for(int i = 0;i<h;i++){
                    System.out.printf("#");
                    for(int j = 0;j<w-2;j++){
                        if(i==0 || i==h-1){
                            System.out.print("#");
                        }
                        else{
                            System.out.print(".");
                        }
                    }
                    System.out.println("#");
                }
            }
            System.out.println();
        }
    }
}


Measured memory_bytes: 33060
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

template<typename T>
class BIT {
public:
    explicit BIT(int n) : N(calcN_(n)), mVal(calcN_(n)+1, 0) {}
    void add(int idx, T val){
        for(int x=idx;x<=N;x+=x&-x) mVal[x] += val;
    }
    T sum(int idx){
        T res = 0;
        for(int x=idx;x>0;x-=x&-x) res += mVal[x];
        return res;
    }
private:
    int calcN_(int n){
        int res = 1;
        while(res < n) res *= 2;
        return res;
    }
    const int N;
    vector<T> mVal;
};

bool valid(const vector<int>& a, int thr){
    const int N = a.size();
    vector<int> sum(N+1, 0);
    vector<pair<int, int>> order;
    order.emplace_back(0, 0);
    for(int i=0;i<N;i++){
        sum[i+1] = sum[i] + (a[i] <= thr ? 1 : -1);
        order.emplace_back(sum[i+1], -(i+1));
    }
    sort(order.begin(), order.end());
    BIT<long long> bit(N+1);
    long long res = 0;
    for(auto& p : order){
        int idx = -p.second + 1;
        res += bit.sum(idx);
        bit.add(idx, 1);
    }
    return res > N*(N+1LL)/2/2;
}

int main(){
    int N; cin >> N;
    vector<int> a(N);
    for(auto& t : a) cin >> t;
    auto vals = a;
    sort(vals.begin(), vals.end());
    vals.erase(unique(vals.begin(), vals.end()), vals.end());
    int L = -1, R = vals.size()-1;
    while(R-L > 1){
        int mid = (L+R)/2;
        if(valid(a, vals[mid])) R = mid;
        else L = mid;
    }
    cout << vals[R] << endl;
}

Measured memory_bytes: 7356
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for(int i = 0; i < (n); ++i)
typedef long long ll;
const ll MOD = 1000000007;
using namespace std;

int main(){
    string n; cin >> n;
    if(n[0] == n[1] && n[1] == n[2]) cout << "Yes\n";
    else if(n[2] == n[3] && n[1] == n[2]) cout << "Yes\n";
    else cout << "No\n";
    return 0;
}

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 AtTest.Nikkei2019_2_qual
{
    class A
    {
        static void Main(string[] args)
        {
            var sw = new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false };
            SetOut(sw);

            Method(args);

            Out.Flush();
        }

        static void Method(string[] args)
        {
            int n = ReadInt();
            WriteLine((n-1) / 2);
        }

        private static string Read() { return ReadLine(); }
        private static char[] ReadChars() { return Array.ConvertAll(Read().Split(), a => a[0]); }
        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 <iostream>
#include <cassert>
#include <climits>
#include <bitset>
#include <stack>
#include <queue>
#include <iomanip>
#include <limits>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string.h>

using namespace std;
typedef long long ll;

typedef pair<ll,ll> P;
long long int INF = 1e18;
double Pi = 3.1415926535897932384626;
long long int mod = 1000000007;

vector<ll> G[500005];
vector<P> tree[500010];
priority_queue <ll> pql;
priority_queue <P> pqp;
//big priority queue
priority_queue <ll,vector<ll>,greater<ll> > pqls;
priority_queue <P,vector<P>,greater<P> > pqps;
//small priority queue
//top pop

int dx[8]={1,0,-1,0,1,1,-1,-1};
int dy[8]={0,1,0,-1,1,-1,-1,1};
char dir[] = "RULD";
//↓,→,↑,←

#define p(x) cout<<x<<endl;
#define el cout<<endl;
#define pe(x) cout<<x<<" ";
#define ps(x) cout<<fixed<<setprecision(25)<<x<<endl;
#define pu(x) cout<<x;
#define re(i,a,b) for(i=a;i<=b;i++);
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define deba(x) cout<< #x << " = " << x <<endl

ll rui(ll abc,ll bed){
    //abcのbed乗を計算する
    if(bed==0){return 1;}
    else{
        ll ced = rui(abc,bed/2);
        ced *= ced;
        ced %= mod;
        if(bed%2==1){ced*=abc; ced%=mod;}
        return ced;
    }
}



ll i,j,k,ii,jj;
ll n,m,num;
ll a,b,c,e,f,g,h;
ll x[800005],y[800005],z[900005];
ll d[800005],dp[500005];
ll vnum = 0,sum;
bool dame;
bool check[500005];


void unite(ll a,ll b){
    x[vnum] = a;
    y[vnum] = b;
    G[a].pb(b);
    G[b].pb(a);
    vnum++;
}

void dfs(ll v,ll dpn){
    if(check[v]){
        return;
    }else{
        sum += dpn;
        check[v] = true;
        for(int i=0;i<G[v].size();i++){
            if(!check[G[v][i]])dfs(G[v][i],dpn + 1);
        }
    }
}
int main(){
    cin>>n;
    for(i=0;i<n;i++){
        cin>>d[i];
        z[i] = d[i];
    }
    sort(d,d+n);
    for(i=0;i<n;i++){
        z[i] = (lb(d,d+n,z[i]) - d) * 100001 + i;
    }
    sort(z,z+n);
    //reverse(d,d+n);
    for(i=n-1;i>=1;i--){
        //大きい数字から見て行くよ
        dp[i]++;
        num = d[i] - n + 2 * dp[i];
        a = lb(d,d+n,num) - lb(d,d+n,-1);
        if(d[a] != num || a >= i){
            dame = true;
            break;
        }else{
            dp[a] += dp[i];
            unite(a+1,i+1);
        }
    }
    sum = 0;
    dfs(1,0);
    //p(sum);
    if(sum != d[0]){
        dame = true;
    }
    if(dame){
        p(-1);
        return 0;
    }else{
        for(i=0;i<vnum;i++){
            pe(z[x[i]-1]%100001 + 1);p(z[y[i]-1]%100001 + 1);
        }
    }
    return 0;
}



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

using namespace std;

int main()
{
    int N;
    cin >> N;
    vector <int> stones(N);
    vector <int> dp(N);
    for(int i = 0;i < N;i++)
    {
        cin >> stones[i];   
    }
    dp[0] = 0;
    dp[1] = abs(stones[1] - stones[0]);
    for(int i = 2;i < N;i++) {
        int s1 = abs(stones[i] - stones[i-1]) + dp[i-1];
        int s2 = abs(stones[i] - stones[i-2]) + dp[i-2];
        dp[i] = min(s1, s2);
    }
    cout << dp[N-1] << endl;
    return 0;
}

Measured memory_bytes: 1024
s = list(input())

a = int(s[0])
b = int(s[1])
c = int(s[2])
d = int(s[3])
if a + b + c + d ==7:
    print("{0}+{1}+{2}+{3}=7".format(a, b, c, d))
elif a - b + c + d ==7:
    print("{0}-{1}+{2}+{3}=7".format(a, b, c, d))
elif a + b - c + d ==7:
    print("{0}+{1}-{2}+{3}=7".format(a, b, c, d))
elif a + b + c - d ==7:
    print("{0}+{1}+{2}-{3}=7".format(a, b, c, d))
elif a - b - c + d ==7:
    print("{0}-{1}-{2}+{3}=7".format(a, b, c, d))
elif a + b - c - d ==7:
    print("{0}+{1}-{2}-{3}=7".format(a, b, c, d))
elif a - b + c - d ==7:
    print("{0}-{1}+{2}-{3}=7".format(a, b, c, d))
elif a - b - c - d ==7:
    print("{0}-{1}-{2}-{3}=7".format(a, b, c, d))

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

int main(){
    int N;
    cin>>N;
    vector<string> siri(N);
    bool check=true;
    for(auto &&x:siri){
        cin>>x;

    }

    for(int i=1;i<N;++i){
        if(siri[i-1][siri[i-1].size()-1]!=siri[i][0]){
            check=false;
            break;
        }

        for(int k=0;k<i;k++){
            if(siri[k]==siri[i]){
                check=false;
                break;
            }
        }
    }


    if(check){
        cout<<"Yes"<<endl;
    }else{
        cout<<"No"<<endl;
    }
}

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

int main()
{
	int N;
	while (cin >> N && N != 0)
	{
		int field[101][101] = {};
		int W, H,S,T;
		cin >> W >> H;
		for (int i = 0; i < N; i++)
		{
			int x, y;
			cin >> x >> y;
			field[y][x] = 1;
		}
		cin >> S >> T;
		int max_count = 0;
		for (int y = 1; y <= H - T + 1; y++)
		{
			for (int x = 1; x <= W - S + 1; x++)
			{
				int count = 0;
				for (int i = 0; i < T; i++)
				{
					for (int j = 0; j < S; j++)
					{
						if (field[y + i][x + j] == 1)count++;
					}
				}
				max_count = max(max_count, count);
			}
		}

		cout << max_count << endl;

	}
	return 0;
}

Measured memory_bytes: 1208
#include <cassert>
#include <cstdint>

template <template <class> class Container> class eratosthenes {
	using uint64 = ::std::uint_fast64_t;

public:
	using container_type = Container<uint64>;
	using size_type = typename container_type::size_type;

protected:
	container_type c;
	size_type s;

public:
	eratosthenes() : c(), s(0) {}
	explicit eratosthenes(const size_type size)
		: c((size >> 6) + 1, ~static_cast<uint64>(0)), s(size) {
		c[0] ^= 3;
		for (size_type i = 2; i * i < size; ++i)
			if (c[i >> 6] >> (i & 0x3F) & 1)
				for (size_type j = i * i; j < size; j += i)
					c[j >> 6] &= ~(static_cast<uint64>(1) << (j & 0x3F));
	}

	bool empty() const { return !s; }
	size_type size() const { return s; }

	bool test(const size_type i) const {
		assert(i < size());
		return c[i >> 6] >> (i & 0x3F) & 1;
	}
	bool operator[](const size_type i) const {
		assert(i < size());
		return c[i >> 6] >> (i & 0x3F) & 1;
	}
};

#include <iostream>
#include <vector>

template <class T> using vec_alias = ::std::vector<T>;

int main() {
	constexpr int N = 100000001;

	eratosthenes<vec_alias> pri(N);

	int n;
	::std::cin >> n;

	int cnt = 0;
	while (n--) {
		int x;
		::std::cin >> x;
		cnt += pri[x];
	}

	::std::cout << cnt << ::std::endl;
	return 0;
}



Measured memory_bytes: 14760
n, k = map(int, input().split())
s = [list(map(int, input().split())) for _ in range(k)]
dp = [0]*(n+1)
dp[1] = 1
mod = 998244353
dpsum = [0]*(n+1)
dpsum[1] = 1

for i in range(2, n+1):
    for j in range(k):
        li = max(i-s[j][1], 1)
        ri = i-s[j][0]
        if ri < 0:
            continue
        dp[i] += dpsum[ri]-dpsum[li-1]
        dp[i] %= mod
    dpsum[i] = dpsum[i-1]+dp[i]
print(dp[n])
    


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



int main()
{
  ll a,b,c,k,sum=0;
  cin>>a>>b>>c>>k;
  if(a>=b)
  {
    long double p;
    p=(long double)a/(long double)b;
    p=log(p)/log((ld)2);
    p=p+1;
    p=floor(p);
    sum+=(ll)p;
    b=b*(1<<(ll)p);
  }
  //cout<<b;
  if(c<=b)
  {
    long double p;
    p=(long double)b/(long double)c;
    p=log(p)/log((ld)2);
    p=p+1;
    p=floor(p);
    sum+=(ll)p;
    //c=c*(1<<p);
  }
  //cout<<a<<b<<c;

  if(sum<=k)
    cout<<"Yes";
  else
    cout<<"No";


  return 0;
}


Measured memory_bytes: 3680
//minamoto
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
const int N=15;
int a[N],n,res,ret;
int main(){
	scanf("%d",&n),res=ret=1;
	fp(i,1,n)scanf("%d",&a[i]),res*=3,ret*=(a[i]&1?1:2);
	printf("%d\n",res-ret);
	return 0;
}

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

#define GO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long int

const int maxr = 1e5 + 5;

string arr = "zabcdefghijklmnopqrstuvwxy";

int32_t main()
{
	GO;
	int n;
	cin >> n;
	string s = "";
	while (n > 0) {
		s.push_back(arr[n % 26]);
		if (n % 26 == 0)
			n -= 26;
		n /= 26;
	}
	reverse(s.begin(), s.end());
	cout << s << endl;
	return 0;
}

//!

Measured memory_bytes: 3652
#include <iostream>

using namespace std;

int main()
{
  long long N;
  cin >> N;
  
  int cnt = -1;
  for(long long i = 1; i*i <= N; i++) {
    if(N % i == 0) cnt = i;
  }
  
  long long tmp = N / cnt;
  int ans = 0;
  while(tmp) tmp /= 10, ans++;
  cout << ans << endl;
    
  return 0;
}

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

int main() {

    int N;
    cin>>N;
    int M;
    cin>>M;
    vector <pair<long long,long long>> A(N);
    for(int i=0;i<N;i++) {
        cin >> A.at(i).first >> A.at(i).second;
    }

    sort(A.begin(), A.end());

    int i=0;
    long long count=0;
    while(M>0){
        M = M - A.at(i).second;
        if(M>=0){
            count += A.at(i).first*A.at(i).second;
            i++;
        }else{
            count += (A.at(i).second+M)*A.at(i).first;

        }

    }
    cout<<count<<endl;
    return 0;
}



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

int main()
{
	int k, s;
	cin >> k >> s;

	int cnt = 0;
	for( int x = 0; x <= k ; x++ ) {
		for( int y = 0; y <= k ; y++ ) {
			if( s - x - y <= k and s - x - y >= 0 ) {
				cnt++;
			}
		}
	}

	cout << cnt << endl;
	return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <tuple>
#include <vector>
#include <cstring>
#include <cstdlib>

using namespace std;

const int N = 100'009;

int ct;
vector<tuple<int, int, int>> g[N];
int pa[N];
int tin[N];
int tout[N];

void dfs1(int x) {
  tin[x] = ct++;
  for (auto &e : g[x]) {
    int y = get<0>(e);
    if (y != pa[x]) {
      pa[y] = x;
      dfs1(y);
    }
  }
  tout[x] = ct++;
}

bool anc(int x, int y) {
  return tin[x] <= tin[y] && tout[x] >= tout[y];
}

const int LG = 20;

int kp[LG][N];

int lca(int x, int y) {
  if (anc(x, y)) {
    return x;
  }
  for (int i = LG - 1; i >= 0; --i) {
    if (!anc(kp[i][x], y)) {
      x = kp[i][x];
    }
  }
  return pa[x];
}

int qc[N];
int qd[N];
vector<int> q[N];
int cnt[N];
int sum[N];
int ans[N];

void dfs2(int x, int dist) {
  for (int i : q[x]) {
    int j = abs(i);
    int cur = dist - sum[qc[j]] + cnt[qc[j]] * qd[j];
    if (i < 0) {
      cur = -2 * cur;
    }
    ans[j] += cur;
  }
  for (auto &e : g[x]) {
    int y = get<0>(e);
    if (y == pa[x]) {
      continue;
    }
    int c = get<1>(e);
    int d = get<2>(e);
    ++cnt[c];
    sum[c] += d;
    dfs2(y, dist + d);
    --cnt[c];
    sum[c] -= d;
  }
}

int main() {
  int n, qq;
  scanf("%d%d", &n, &qq);
  for (int i = 0; i < n; ++i) {
    g[i].clear();
  }
  for (int i = 0; i < n - 1; ++i) {
    int x, y, c, d;
    scanf("%d%d%d%d", &x, &y, &c, &d);
    --x;
    --y;
    --c;
    g[x].emplace_back(y, c, d);
    g[y].emplace_back(x, c, d);
  }
  memset(pa, -1, sizeof pa);
  pa[0] = 0;
  ct = 0;
  dfs1(0);
  for (int i = 0; i < n; ++i) {
    kp[0][i] = pa[i];
  }
  for (int j = 1; j < LG; ++j) {
    for (int i = 0; i < n; ++i) {
      kp[j][i] = kp[j - 1][kp[j - 1][i]];
    }
  }
  for (int i = 0; i <= qq; ++i) {
    q[i].clear();
  }
  for (int i = 1; i <= qq; ++i) {
    int x, y;
    scanf("%d%d%d%d", qc + i, qd + i, &x, &y);
    --qc[i];
    --x;
    --y;
    q[x].push_back(i);
    q[y].push_back(i);
    q[lca(x, y)].push_back(-i);
  }
  memset(cnt, 0, sizeof cnt);
  memset(sum, 0, sizeof sum);
  memset(ans, 0, sizeof ans);
  dfs2(0, 0);
  for (int i = 1; i <= qq; ++i) {
    printf("%d\n", ans[i]);
  }
}


Measured memory_bytes: 27388
#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef priority_queue<int> PQ;
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
//vector<vector<int> > dp;
//vector<vector<vector<int> > > vvvi;
//dp=vector<vector<int> >(N, vector<int>(M,0));
//vector<pair<int,int> > v;
//v.push_back(make_pair(x,y));
//priority_queue<int,vector<int>, greater<int> > q2;

int G[210][210];
int ans = 0;
int vis[210];
int N;

/*void dfs(int k, int cnt) {
	vis[k] = cnt;
	REP(i, N) {
		if (G[k][i]) {
			if (vis[i] == -1) {
				dfs(i, cnt + 1);
			}
			if(vis[i]==)
		}
	}
}*/


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

	
	cin >> N;
	REP(i, N) {
		string S;
		cin >> S;
		REP(j, N) {
			int a=S[j]-'0';
			
			if (a)G[i][j]=1;
		}
	}

	REP(i, N) {
		int vis[210];
		REP(j, 210)vis[j] = -1;
		queue<int> Q;
		Q.push(i);

		int cnt = 1;
		vis[i] = cnt;
		cnt++;

		bool F = true;
		
		int tmp = 0;
		
		while (!Q.empty()) {
			int a = Q.front();
			Q.pop();
			bool f = false;
			REP(j, N) {
			
				if (j == a)continue;

				if (vis[j]!=-1&&vis[j] == vis[a]&&G[j][a]) {
					//cout << i << " " << j << " " << a << endl;
					F = false;
				}
				if (G[a][j]&&vis[j]==-1) {
					vis[j] = vis[a] + 1;
					f = true;
					tmp = max(tmp, cnt);
				//	cout << i <<" "<< j <<" "<<cnt<< endl;
					Q.push(j);
				}
				
			}
			if (f)cnt++;
		}
		if (F) {
			REP(k, N)ans = max(ans, vis[k]);
			
			//cout << i << " " << cnt << endl;
		}
		/*if (i == 0) {
			REP(k, N)cout << vis[k];
			cout << endl;
		}*/
	}
	if (ans == 0)ans--;
	cout << ans << endl;
	return 0;
}



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

typedef pair<int,int> P;
int n;

int main(){
  int V,E;cin>>V>>E;
  vector<P> edge[V];
  while(E--){
    int u,v,d;cin>>u>>v>>d;u--;v--;
    edge[u].push_back(P(v,d));
    edge[v].push_back(P(u,d));
  }
  int T;cin>>T;
  vector<int> good[T];
  for(int i=0;i<T;i++){
    cin>>n;
    if(n){
      int d;cin>>d>>d;d--;
      good[i].push_back(d);
    }
  }
  while(T--)cout<<-1<<endl;
}

Measured memory_bytes: 640
#[allow(unused_imports)]
use std::cmp::{Ordering};

#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};

#[allow(unused_imports)]
use std::iter::FromIterator;

#[allow(unused_macros)]
macro_rules! min {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::min($x, min!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! max {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::max($x, max!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! get { 
    ([$t: ty]) => { 
        { 
            let mut line = String::new(); 
            stdin().read_line(&mut line).unwrap(); 
            line.split_whitespace().map(|t|t.parse::<$t>().unwrap()).collect::<Vec<_>>()
        }
    };
    ([$t: ty];$n: expr) => {
        (0..$n).map(|_|get!([$t])).collect::<Vec<_>>()
    };
    ($t: ty) => {
        {
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t: ty),*) => {
        { 
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*)
        }
    };
    ($t: ty; $n: expr) => {
        (0..$n).map(|_|get!($t)).collect::<Vec<_>>()
    };
    ($($t: ty),*; $n: expr) => {
        (0..$n).map(|_|get!($($t),*)).collect::<Vec<_>>()
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*).unwrap();
    }
}

const BIG_STACK_SIZE: bool = true;

#[allow(dead_code)]
fn main() {
    use std::thread;
    if BIG_STACK_SIZE {
        thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .name("solve".into())
            .spawn(solve)
            .unwrap()
            .join()
            .unwrap();
    } else {
        solve();
    }
}

fn solve() {
    let x = get!(String);
    let mut scount = 0;
    let mut tcount = 0;
    let mut count = 0;
    let mut sflag = false;
    for c in x.chars() {
        match c {
            'S' => {
                if sflag {
                    scount += 1;
                } else {
                    count += min!(scount, tcount);
                    scount -= min!(scount, tcount);
                    tcount = 0;
                    scount += 1;
                    sflag = true;
                }
            },
            'T' => {
                sflag = false;
                tcount += 1; 
            },
            _ => (),
        }
    }
    count += min!(scount, tcount);
    println!("{}", x.len()-2*count);
}


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


int main() {
    int X,A,B;
    cin>>X>>A>>B;
    int a=X-A;
    while(a>=B){
        a-=B;
    }
    cout<<a<<endl;
}

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

bool is_prime(int64_t n){

  bool flg=true;

  for(int64_t i=2;i*i<=n;i++){
    if(n%i==0){
      flg=false;
      break;
    }
  }

  return flg;
}


int main() {

  int64_t n;
  cin>>n;

  int64_t cnt=0;
  for(int i=0;i<n;i++){
    int64_t wk;
    cin>>wk;

    if(is_prime(wk))
      cnt++;
  }

  cout<<cnt<<endl;

	return 0;
}


Measured memory_bytes: 3060
N = int(input())
q = []
c = [0 for i in range(N)]
l = [0 for i in range(N)]
A = [[] for i in range(N)]
W = [[] for i in range(N)]

for i in range(N-1):
	u, v, w = map(int,input().split())
	u = u - 1
	v = v - 1
	
	A[u].append(v)
	A[v].append(u)
	W[u].append(w)
	W[v].append(w)

q.append(0)
while len(q):
	i = q.pop(0)
	c[i] = 1
	k = 0
	for j in A[i]:
		if c[j] == 0:
			q.append(j)
			l[j] = l[i] + W[i][k]
		k = k + 1

for i in l:
	print('{}'.format(i%2))
	

Measured memory_bytes: 42736
/*
* @author:  codancer
* @createTime:  2020-07-05, 20:05:20
*/
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
#define pb push_back
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define fep(i,a,b) for(int i=(a);i>=(b);i--)
typedef vector<int> VI;
typedef vector<ll> VII;
typedef pair<int,int> pii;
string maze[10];
bool used[10][10];
int main(){
	int n,m,k;
	cin>>n>>m>>k;
	int black=0;
	rep(i,0,n-1){
		cin>>maze[i];
	}
	rep(i,0,n-1){
		rep(j,0,m-1){
			if(maze[i][j]=='#') ++black;
		}
	}
	ll ans=0;
	for(int i=0;i<(1<<(n+m));i++){
		int tmp=black;
		memset(used,0,sizeof(used));
		for(int j=0;j<(n+m);j++){
			if((i>>j)&1){
				if(j<n){//row
					for(int c=0;c<m;c++){
						if(maze[j][c]=='#'&&!used[j][c]){
							 --tmp;
							 used[j][c]=1;
						}
					}
				}else{//col
					int col=j-n;
					for(int r=0;r<n;r++){
						if(maze[r][col]=='#'&&!used[r][col]){
							 --tmp;
							 used[r][col]=1;
						}
					}
				}
			}
		}
		if(tmp==k) ++ans;
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 3636
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
int a[N];

int main()
{
	ios::sync_with_stdio(false);
	
	int n;
	cin >> n;
	int mn = n+1, mx = 0;
	for (int i=0; i<n; i++)
	{
		int x; cin >> x;
		mn = min(mn, x);
		mx = max(mx, x);
		a[i] = x;
	}
	bool ok = true;
	if (mx > mn + 1)
		ok = false;
	if (mx == mn)
	{
		if (mn != n - 1 && mn * 2 > n)
			ok = false;
	}
	else
	{
		int cnt = 0;
		for (int i=0; i<n; i++)
			if (a[i] == mn)
				cnt++;
		int all = mn + 1;
		if (all <= cnt)
			ok = false;
		if ((all - cnt) * 2 + cnt > n)
			ok = false;
	}
	if (ok)
		cout << "Yes\n";
	else
		cout << "No\n";


	return 0;
}


Measured memory_bytes: 640
N, M = map(int, input().split())
Xlist = sorted(list(map(int, input().split())))
Llist = []
for i in range(M - 1):
    Llist.append(Xlist[i + 1] - Xlist[i])
Llist.sort()
print(sum(Llist[:M - N]) if N < M else 0)


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

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double PI  = acos(-1.0);

using ll = long long;
using namespace std;

int main() {
    int n, k;

    cin >> n >> k;

    ll ct = 0;
    ll tmp;
    REP(i, n) {
        cin >> tmp;
        if (tmp>= k) {
            ct++;
        }
    }

    cout << ct << endl;

    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a) = min((a), (b)))
#define chmax(a, b) ((a) = max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

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

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);
const double eps = 1e-10;

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

int main(){
    ll l, r; cin>>l>>r;

    vector<ll> v;
    for(ll i=0; ; i++){
        if((ll)2e9 < i * 2019) break;
        v.emplace_back(i * 2019);
    }

    bool valid = false;
    for(int i=0; i<v.size(); i++){
        if(l <= v[i] && v[i] <= r) valid = true;
    }

    if(valid){
        cout << 0 << endl;
        return 0;
    }

    vector<ll> v2;
    for(ll i=0; ; i++){
        if((ll)2e9 < i * 673) break;
        v2.emplace_back(i * 673);
    }

    valid = false;
    for(int i=0; i<v2.size(); i++){
        if(l <= v2[i] && v2[i] <= r) valid = true;
    }

    bool valid3 = false;
    for(int i=l; i<=min(r, l+2); i++){
        if(i % 3 == 0) valid3 = true;
    }

    if(valid && valid3){
        cout << 0 << endl;
        return 0;
    }

    ll ans = INF;
    for(ll i=l; i<=r; i++){
        for(int j=i+1; j<=r; j++){
            ans = min(ans, (i * j) % 2019);
        }
    }

    cout << ans << endl;
}

Measured memory_bytes: 43364
#include<stdio.h>
int main(void)
{
	int a,b,c;
	scanf("%d %d",&a,&b);
	c=a+b;
	if(c%3==0 || a%3==0 || b%3==0){
		printf("Possible\n");
	}
	else{
		printf("Impossible\n");
	}
	return 0;
}

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

// 型定義
typedef long long ll;
typedef pair<ll, ll> P;

// forループ
#define REP(i,n) for(ll i=0; i<(ll)(n); ++i)

// 定数宣言
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;

// グラフ表現
using Graph = vector<vector<int>>;

// グラフの辺表現
using Edge = map<pair<int,int>,int>;

// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int main()
{
    cout << fixed << setprecision(15);
    ll L;
    cin >> L;

    double a1;
    double a2;
    double a3;

    int Li = L/3;
    if(L % 3 == 0){
        a1 = 0;
        a2 = 0;
        a3 = 0;
    }
    if(L % 3 == 1){
        a1 = 1.0/3.0;
        a2 = 1.0/9.0;
        a3 = 1.0/27.0;
    }
    if(L % 3 == 2){
        a1 = 2.0/3.0;
        a2 = 4.0/9.0;
        a3 = 8.0/27.0;
    }
    double ans = Li  * Li  * Li   + 3 * Li  * Li  * a1 + 3 * Li * a2 + a3;
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 256
#include <cstdio>

bool pri_sub[100000] = {false};
int arr[50000];
int num;

int main(){
	//gen prime number
	pri_sub[0] = pri_sub[1] = true;
	for(int i = 2;i <= 50000;i++){
		if(pri_sub[i] == false){
			for(int j = i*2; j <= 50000; j+=i){
				pri_sub[j] = true;
			}
		}
	}
	num = 0;
	for(int i = 2; i <= 50000;i++){
		if(pri_sub[i] == false){
			arr[num] = i;
			num++;
		}
	}

	//proc
	int n;
	while(scanf("%d",&n), n!=0){
		int hd = 0;
		int tl = num -1;
		int cnt = 0;
		while(hd <= tl){
			if(arr[hd] + arr[tl] == n){
				cnt++;
				hd++;
			}
			else if(arr[hd] + arr[tl] < n){
				hd++;
			}
			else{ // > n
				tl--;
			}
		}
		printf("%d\n",cnt);
	}

	return 0;
}



Measured memory_bytes: 2760
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <cmath>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <iostream>
#include <random>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include <fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
#include <cctype>
#include <bitset>
using namespace std;

using ll = long long;
using vll = vector<ll>;
using pll = pair<ll, ll>;
using qll = queue<ll>;
using vb = vector<bool>;
using mll = map<ll, ll>;
using sll = stack<ll>;
#define REP(i,n) for(ll i(0);(i)<(n);(i)++)
#define rep(i,n) for(ll i(0);(i)<(n);(i)++)
#define ALL(a) a.begin(), a.end()
#define elnd endl //* missspell check
const ll INF = 1LL << 60;

void solve(long long N, long long K){
    ll cnt = 0;
    for(ll b = K+1; b <=N; b++){
        ll t = N/b;
        cnt += t*(b-K);
        if(K>0)
            cnt += max(0LL, N%b-K+1);
        else
            cnt += N%b;
    }
    cout << cnt<<endl;
}

int main(){
    long long N;
    scanf("%lld",&N);
    long long K;
    scanf("%lld",&K);
    solve(N, K);
    return 0;
}


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

const int maxn = 1e5+10, maxlogn = 20;

int n;

namespace pers_seg{
	const int maxx = maxn*maxlogn;
	struct val{
		int cnt, len;
		void add(int l) {
			cnt++;
			len += l;
		}
	};
	struct node{
		val v;
		node *ls, *rs;
	}t[maxx];
	int tot;
	node *rt[maxn];
	void copy(node *&x) {
		t[++tot] = *x;
		x = t+tot;
	}
	void init(node *&u, int l, int r) {
		debug("pers_seg::init [%d, %d]\n", l, r);
		u = t+(++tot);
		if (l == r) return;
		int m = (l+r)>>1;
		init(u->ls, l, m);
		init(u->rs, m+1, r);
	}
	void modify(node *&u, int l, int r, int x, const val &y) {
		assert(u);
		debug("pers_seg::modify [%d, %d]:%d\n", l, r, x);
		copy(u);
		if (l == r) {
			u->v = y;
			return;
		}
		int m = (l+r)>>1;
		if (x <= m) modify(u->ls, l, m, x, y);
		else modify(u->rs, m+1, r, x, y);
	}
	const val& get(node *u, int l, int r, int x) {
		debug("pers_seg::get [%d, %d]:%d", l, r, x);
		assert(u);
		debug("(%d)\n", u-t);
		if (l == r) return u->v;
		int m = (l+r)>>1;
		if (x <= m) return get(u->ls, l, m, x);
		else return get(u->rs, m+1, r, x);
	}
}

using namespace pers_seg;

namespace graph{
	struct edge{
		int b, c, d;
	};
	vector<edge> e[maxn];
	int fa[maxn][maxlogn], len[maxn], dep[maxn];
	void add(int a, int b, int c, int d) {
		e[a].push_back((edge){b, c, d});
	}
	void add2(int a, int b, int c, int d) {
		add(a, b, c, d), add(b, a, c, d);
	}
	void init(int u) {
		debug("graph::init %d %d\n", u, fa[u][0]);
		if (!fa[u][0]) {
			dep[0] = -1;
			init(rt[u], 1, n-1);
		}
		for (int i = 1; i < maxlogn; ++i) fa[u][i] = fa[fa[u][i-1]][i-1];
		for (int i = 0; i < e[u].size(); ++i) {
			edge E = e[u][i];
			int v = E.b, c = E.c, d = E.d;
			if (v == fa[u][0]) continue;

			rt[v] = rt[u];
			val tmp = get(rt[v], 1, n-1, c);
			tmp.add(d);
			modify(rt[v], 1, n-1, c, tmp);
			fa[v][0] = u;
			len[v] = len[u]+e[u][i].d;
			dep[v] = dep[u]+1;

			init(v);
		}
	}
	int lca(int u, int v) {
		debug("graph::lca %d{%d} %d{%d}", u, dep[u], v, dep[v]);
		if (dep[u] < dep[v]) swap(u, v);
		for (int i = maxlogn-1; ~i; --i)
			if (dep[fa[u][i]] >= dep[v]) {
				u = fa[u][i];
			}
		debug(" [%d %d]", u, v);
		for (int i = maxlogn-1; u != v; i = max(0, i-1)) {
			if (i == 0 || fa[u][i] != fa[v][i]) {
				u = fa[u][i];
				v = fa[v][i];
			}
		}
		debug(" (%d)\n", u);
		return u;
	}
	int query(int x, int y, int u, int v) {
		debug("graph::query %d %d %d %d\n", x, y, u, v);
		int l = lca(u, v);
		val vu = get(rt[u], 1, n-1, x), vv = get(rt[v], 1, n-1, x), vl = get(rt[l], 1, n-1, x);
		int ans = len[u]+len[v]-2*len[l];
		ans -= (vu.len+vv.len-2*vl.len);
		ans += (vu.cnt+vv.cnt-2*vl.cnt)*y;
		return ans;
	}
}
using namespace graph;

int main() {
	int q;
	scanf("%d%d", &n, &q);
	int a, b, c, d;
	for (int i = 0; i < n-1; i++) {
		scanf("%d%d%d%d", &a, &b, &c, &d);
		add2(a, b, c, d);
	}
	init(1);
	while (q--) {
		scanf("%d%d%d%d", &a, &b, &c, &d);
		printf("%d\n", query(a, b, c, d));
	}
}


Measured memory_bytes: 70016
import java.io.*;
import java.util.*;

public class Main {
	public static void main(String[] args) {
		int n = 0;
		String str = null;
		
		try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
			n = Integer.parseInt(br.readLine());
			str = br.readLine();
		} catch (IOException e) {
			return;
		}
		
		int max = 0;
		for (int i = 1; i < n - 1; i++) {
			String x = str.substring(0, i);
			String y = str.substring(i, n);
			
			int tmp = 0;
			if (x.length() < y.length()) {
				for (int j = 0; j < x.length(); j++) {
					if (x.indexOf(x.charAt(j)) == j &&
						y.indexOf(x.charAt(j)) >= 0) {
						tmp++;
					}
				}
			} else {
				for (int j = 0; j < y.length(); j++) {
					if (y.indexOf(y.charAt(j)) == j &&
					x.indexOf(y.charAt(j)) >= 0) {
						tmp++;
					}
				}
			}
			
			if (tmp > max) max = tmp;
		}
		
		System.out.println(max);
	}
}


Measured memory_bytes: 22868
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int INF = (1<<30);
const ll INFLL = (1ll<<60);
const ll MOD = (ll)(1e9+7);

#define l_ength size

void mul_mod(ll& a, ll b){
	a *= b;
	a %= MOD;
}

void add_mod(ll& a, ll b){
	a = (a<MOD)?a:(a-MOD);
	b = (b<MOD)?b:(b-MOD);
	a += b;
	a = (a<MOD)?a:(a-MOD);
}

int main(void){
	int n,i;
	string s;
	cin >> n;
	cin >> s;
	if(n%2){
		cout << "No" << endl;
		return 0;
	}
	n /= 2;
	for(i=0; i<n; ++i){
		if(s[i]!=s[i+n]){
			cout << "No" << endl;
			return 0;
		}
	}
	cout << "Yes" << endl;
	return 0;
}


Measured memory_bytes: 256
Scanf.scanf "%d %d" @@ fun a b ->
  let ans = ref 0 in
  for x = 1 to 9 do
    for y = 0 to 9 do
      for z = 0 to 9 do
        let n = 10000 * x + 1000 * y + 100 * z + 10 * y + x in
          if a <= n && n <= b then incr ans
      done
    done
  done;
  Printf.printf "%d\n" !ans

Measured memory_bytes: 384
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int N;
ll T;
int cnt;
int coun;
map<string,int> smap;
struct edge{
  int to;
  ll c;
};
  
vector<edge> G[100008];
vector<int> V[100008];
int F[100008];
typedef pair<ll,ll> P;

int st,ed;
int stoi(string s){
  if( smap.count(s) == 0  ) return smap[s] = cnt++;
  return smap[s];
}

struct state{
  int id;
  P c;
  state(int id,P c) : id(id),c(c) {}
  state(){}
  bool operator<(const state s) const {
    return c > s.c;
  }
};

P mem[100008];
#define INF (1LL<<60LL)
P dijk(){
  fill(mem,mem+coun,P(INF,INF) );
  priority_queue<state> q;
  for(int i=0;i<(int)V[st].size();i++){
    q.push( state( V[st][i], P(0,0) ) );
    mem[V[st][i]] = P(0,0);
  }
  while( !q.empty() ){
    state p = q.top(); q.pop();
    if( mem[p.id] < p.c ) continue;
    // cout << p.id << " " << p.c.first << " " << p.c.second  << " "<< F[p.id] << endl;
    int sid = F[p.id];
    if( sid == ed ) return p.c;
    for(int i=0;i<(int)V[sid].size();i++){
      int to = V[sid][i];
      if( to == p.id ) continue;
      P nc = p.c;
      nc.first += T;
      nc.second += 1LL;
      //cout << "to " << to << " cost " << nc.first << " "<< nc.second << endl;
      if( mem[to] > nc ) {
        mem[to] = nc;
        q.push( state(to,nc) );
      }
    }
    for(int i=0;i<(int)G[p.id].size();i++){
      edge e = G[p.id][i];
      P nc = p.c;
      nc.first += e.c;
      //      if( e.c == 27 ) cout << "e.to " << e.to << " [ " << F[e.to] << " ] " <<  " cost " << nc.first << " "<< nc.second << " " << mem[e.to].first << endl;
      if( mem[e.to] > nc ){
        mem[e.to] = nc;
        q.push( state(e.to,nc) );
      }
    }
  }
  return P(-1,-1);
}

int main(){
  cin >> N >> T;
  string A,B;
  cin >> A >> B;
  st = stoi(A); ed = stoi(B);
  coun = 0;
  for(int i=0;i<N;i++){
    int a; cin >> a;
    int be = coun;
    for(int j=0;j<a;j++){
      string t; cin >> t;
      int v = stoi(t);
      V[v].push_back(coun);
      F[coun] = v;
      coun++;
    }
    for(int j=0;j<a-1;j++){
      ll c; cin >> c;
      G[be].push_back( (edge){be+1,c} );
      G[be+1].push_back( (edge){be,c} );
      be++;
    }
  }
  P res = dijk();
  if( res == P(-1,-1) ) cout << -1 << endl;
  else cout << res.first << " "<< res.second << endl;
      
}

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

signed main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    
    //int t=1 ; cin>>t ; while(t--)
    {
        int n;
        cin >> n;

        int a[10][10];
        memset(a, 0, sizeof(a));

        for(int i=1;i<=n;i++)
        {
            int j = i;
            while(j >= 10)
                j /= 10;
            a[j][i%10]++;
        }

        int ans = 0;
        for(int i=1;i<=9;i++)
        {
            for(int j=1;j<=9;j++)
                ans += a[i][j] * a[j][i];
        }

        cout << ans << '\n';
    }
    
    cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
    return 0;
}



Measured memory_bytes: 256
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

def judge(bit):
    # i人目の証言を検証する
    for i in range(N):
        # i人目が正直でなければその証言は無視する
        if not (bit & (1<<i)):
            continue
        for v in V[i]:
            x = v[0]
            y = v[1] # y = 1: 正直 y = 0: 不親切
            # y = 1なのに不親切はダメ
            if y == 1 and not(bit & (1<<x)):
                return False
            # y = 0なのに正直者はダメ
            if y == 0 and (bit & (1<<x)):
                return False
    return True

N = int(input())
V = {}
for i in range(N):
    A = int(input())
    V[i] = []
    for j in range(A):
        x, y = input_nums()
        V[i].append((x-1, y)) # 0-index

res = 0
for bit in range(1 << N):
    if judge(bit):
        count = 0
        for i in range(N):
            if bit & (1 << i):
                count += 1
        res = max(res, count)

print(res)

Measured memory_bytes: 44892
#include <algorithm>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using ll = long long;
using namespace std;
static ll imin = std::numeric_limits<ll>::min() / 10;
static ll imax = std::numeric_limits<ll>::max() / 10;
#define rep(i, n) for (ll i = 0; (i) < (n); ++(i))
#define rep1(i, n) for (ll i = 1; (i) < (n); ++(i))
#define rev_rep(i, n) for (ll i = (n)-1; (i) >= 0; --(i))
#define range(a, i, b) for (ll i = (a); (i) < (b); ++(i))

#ifdef LOCAL
#define dump(x) cout << #x << ":" << x << "\n";
#endif
#ifndef LOCAL
#define dump(x)
#endif

#define all(v) v.begin(), v.end()
#define readl(N) \
  ll N;          \
  cin >> N;
#define readlv(N, vec) \
  vector<ll> vec(N);   \
  rep(i, N) { cin >> vec[i]; }
#define greater_than [](ll l, ll r) { return l > r; }
template <class T1, class T2>
std::ostream& operator<<(std::ostream& os, std::map<T1, T2>& mp);
template <class T1, class T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2>& pair);
template <class T>
std::ostream& operator<<(std::ostream& os, std::vector<T>& vec);
template <class T1, class T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2>& pair) {
  os << '(' << pair.first << ',' << pair.second << ')';
  return os;
}

template <class T>
std::ostream& operator<<(std::ostream& os, std::vector<T>& vec) {
  std::size_t n = vec.size();
  if (n == 0) {
    return os;
  }
  os << " ";
  for (ll i = 0; i < n - 1; ++i) {
    os << vec[i] << " ";
  }
  os << vec[n - 1];
  os << std::endl;
  return os;
}

template <class T1, class T2>
std::ostream& operator<<(std::ostream& os, std::map<T1, T2>& mp) {
  std::size_t n = mp.size();
  if (n == 0) {
    return os;
  }
  os << " ";
  for (auto&& x : mp) {
    os << x << " ";
  }
  os << std::endl;
  return os;
}

template <class T>
std::ostream& operator<<(std::ostream& os, std::set<T>& mp) {
  std::size_t n = mp.size();
  if (n == 0) {
    return os;
  }
  os << " ";
  for (auto&& x : mp) {
    os << x << " ";
  }
  os << std::endl;
  return os;
}

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

ll fact(ll n) {
  ll res = 1;
  while (n > 0) {
    res *= n;
    n--;
  }
  return res;
}

ll pow(ll i, ll n) {
  ll res = 1;
  while (n >= 0) {
    res *= i;
    n--;
  }
  return res;
}

ll gcd(ll l, ll r) {
  if (l < r) {
    return gcd(r, l);
  }
  if (r == 0) {
    return l;
  }
  return gcd(r, l % r);
}

template <class T>
vector<T> accum_sum(vector<T> const& a) {
  vector<T> b(a.size() + 1);
  b[0] = 0;
  rep(i, a.size()) { b[i + 1] = b[i] + a[i]; }
  return b;
}
constexpr ll mod = 1e9 + 7;

int main(int argc, char const* argv[]) {
  string S;
  cin >> S;
  ll zeros = count(all(S), '0');
  ll ones = count(all(S), '1');
  cout << min(zeros, ones) * 2;
  return 0;
}


Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int dp[N],n,m;
bool can[N];
int main()
{
	for(int i=1;i<N;++i)dp[i]=1e9+7;
	cin>>n>>m;
	can[0]=true;
	for(int i=0;i<n;i++)
	{
		int x,y;
		cin>>x>>y;
		for(int j=1e5;j>=y;j--)
		{
			if(can[j-y]&&dp[j-y]+x<=m)
			{
				can[j]=true;
				dp[j]=min(dp[j-y]+x,dp[j]);
			}
		}
	}
	for(int i=1e5;i>=0;i--)
	{
		if(can[i])
		{
			cout<<i<<endl;
			return 0;
		}
	}
	return 0;
}

Measured memory_bytes: 768
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input! {
        n: usize,
        m: usize,
        ab: [(usize, usize); m],
    }
    let mut degree = vec![0; n];
    for (a, b) in ab {
        degree[a - 1] += 1;
        degree[b - 1] += 1;
    }
    for d in degree {
        println!("{}", d);
    }
}


Measured memory_bytes: 4352
#include <iostream>
#include <vector>
#define REP(i, n) for(int i = 0; i < (n); ++i)
using ll = long long;
using namespace std;

constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 100010;

int n, x, y;
vector<int> g[MAX];

pair<ll, ll> rec(int v, int p) {
	ll white = 1, black = 1;
	for (int nv : g[v]) {
		if (nv == p) continue;
		auto res = rec(nv, v);
		white *= (res.first + res.second);
		black *= (res.first);
		white %= MOD;
		black %= MOD;
	}
	return make_pair(white, black);
}

int main() {
	cin >> n;
	REP(i, n - 1) {
		cin >> x >> y;
		--x, --y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	auto res = rec(0, 0);
	cout << (res.first + res.second) % MOD;
	return 0;
}

Measured memory_bytes: 12672
#include "bits/stdc++.h"
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

using ll = long long;

int main() {
    int n;
    cin>>n;
    vector<int> k(n),v(n),b(n);
    for(auto &a:v){
		cin>>a;
	}
	for(auto &a:b){
		cin>>a;
	}
	for(int i=0;i<n;i++){
		k[i] = i+1;
	}
	int i=0,j=0,sum=1;
	do{
		if(k==v){
			i=sum;
		}
		if(k==b){
			j=sum;
		}
		sum++;
	}while(next_permutation(k.begin(),k.end()));
	cout<<abs(i-j)<<endl;
}


Measured memory_bytes: 256
my $input_lines = <STDIN>;

#my $input_lines = ('10 8');

my @data = split(/ /, $input_lines);

my $answer = 1;
for(my $i=0; $i < $data[0]; $i++){
  if($i == 0){
    $answer = $data[1];
  }else{
    $answer = $answer * ($data[1]-1);
  }
}

print "$answer";

Measured memory_bytes: 512