akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <set>
#define rep(i,n)  for(int i=0; i<n; i++)
using namespace std;
int main(void){
    int n;
    cin >> n;
    set<int> s;
    rep(i, n){
        int d;
        cin >> d;
        s.insert(d);
    }
    cout << s.size() << endl;
}


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

int main() {
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;

  int x3, x4, y3, y4;

  int dx = x2 - x1;  // 0
  int dy = y2 - y1;  //  1

  x3 = x2 - dy;
  y3 = y2 + dx;

  x4 = x3 - dx;
  y4 = y3 - dy;

  printf("%d %d %d %d\n", x3, y3, x4, y4);
}

Measured memory_bytes: 256
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
vector<ll> a, f;
ll n, k;
bool check(ll t) {
  ll tempk = k;
  for (int i = 0; i < n; i++) {
    ll cost = a[i] * f[i];
    if (cost <= t)continue;
    ll down =
        (cost - t) % f[i] == 0 ? (cost - t) / f[i] : (cost - t) / f[i] + 1;
    if (down <= tempk) {
      tempk -= down;
    } else {
      return false;
    }
  }
  return true;
}

int main() {
  cin >> n >> k;
  a.resize(n);
  f.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> f[i];
  }
  sort(a.begin(), a.end());
  // digesting cost
  sort(f.begin(), f.end(), greater<int>());
  ll lb = -1, ub = 100000000000000;
  while (ub - lb > 1) {
    ll mid = (ub + lb) / 2;
    if (check(mid)) {
      ub = mid;
    } else {
      lb = mid;
    }
  }
  cout << ub << endl;
}


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

const string YES = "Yes";
const string NO = "No";

long long n;
vector<string> S;
string ans[100005];


void dfs(long long i, long long A, long long B, long long C) {
  if (A < 0 || B < 0 || C < 0) return;
  if (i == n) {
    cout << YES << endl;
    for (int j=0; j<n; j++) cout << ans[j] << endl;
    exit(0);
  } else {
    if (S[i] == "AB") {
      ans[i] = 'A';
      dfs(i+1, A+1, B-1, C);
      ans[i] = 'B';
      dfs(i+1, A-1, B+1, C);
    } else if (S[i] == "AC") {
      ans[i] = 'A';
      dfs(i+1, A+1, B, C-1);
      ans[i] = 'C';
      dfs(i+1, A-1, B, C+1);
    } else {
      ans[i] = 'B';
      dfs(i+1, A, B+1, C-1);
      ans[i] = 'C';
      dfs(i+1, A, B-1, C+1);
    }
  }
}

void solve(long long N, long long A, long long B, long long C, std::vector<std::string> s){
  n = N;
  S = s;
  dfs(0, A, B, C);
  cout << NO << endl;
}

int main(){
    long long N;
    scanf("%lld",&N);
    long long A;
    scanf("%lld",&A);
    long long B;
    scanf("%lld",&B);
    long long C;
    scanf("%lld",&C);
    std::vector<std::string> s(N);
    for(int i = 0 ; i < N ; i++){
        std::cin >> s[i];
    }
    solve(N, A, B, C, std::move(s));
    return 0;
}

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

import static java.lang.System.in;

public class Main {
    public static void main(String[] args)throws IOException{
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n], b = new int[n];
        for(int i=0;i<n;i++) a[i]=sc.nextInt();
        for(int i=0;i<n;i++) b[i]=sc.nextInt();
        long need=0,have=0;
        for(int i=0;i<n;i++){
            if(a[i]<=b[i]){
                have += (b[i]-a[i])/2;
            }else{
                need += a[i]-b[i];
            }
        }
        String ans = have>=need?"Yes":"No";
        System.out.println(ans);
    }
}


Measured memory_bytes: 43532
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#define MAX_N 50000
#define INF (long)1e9
const int MOD = 1e9 + 7;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
using namespace std;
typedef long long ll;
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	int a, b, d;
	while(cin >> a >> b >> d, a | b | d)
	{
		int ans_x, ans_y;
		int sum1 = INF, sum2 = INF;
		for(int i = 0; i < MAX_N; i++)
		{
			int j = (a * i - d) / b;
			if(j < 0) continue;
			int num = abs(a * i - b * j);
			if(num == d)
			{
				if(i + j <= sum1 && a * i + b * j <= sum2)
				{
					sum1 = i + j;
					sum2 = a * i + b * j;
					ans_x = i; ans_y = j;
				}
			}
		}
		for(int j = 0; j < MAX_N; j++)
		{
			int i = (b * j - d) / a;
			if(i < 0) continue;
			int num = abs(b * j - a * i);
			if(num == d)
			{
				if(i + j <= sum1 && a * i + b * j <= sum2)
				{
					sum1 = i + j;
					sum2 = a * i + b * j;
					ans_x = i; ans_y = j;
				}
			}
		}
		for(int i = 0; i < MAX_N; i++)
		{
			int j = (d - a * i) / b;
			if(j < 0) continue;
			if(a * i + b * j == d)
			{
				if(i + j <= sum1 && a * i + b * j <= sum2)
				{
					sum1 = i + j;
					sum2 = a * i + b * j;
					ans_x = i; ans_y = j;
				}
			}
		}
		cout << ans_x << " " << ans_y << endl;
	}
	return 0;
}



Measured memory_bytes: 3104
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;


public class D
{
	public static void Main()
	{
		int N = Int32.Parse(Console.ReadLine());
		string[] args = Console.ReadLine().Split(' ');
		long[] A = new long[N];
		for(int i = 0; i < N; i++)
			A[i] = Int64.Parse(args[i]);

		long ans = 0;
		int numMinus = 0;
		long minAbs = Int64.MaxValue;
		for(int i = 0; i < N ; i++)
		{
			minAbs = Math.Min(minAbs, Math.Abs(A[i]));
			if(A[i] >= 0)
			{
				ans += A[i];
			}
			else
			{
				ans += (-A[i]);
				numMinus++;
			}
		}
		if((numMinus % 2) == 1)
			ans -= (2 * minAbs);

		Console.WriteLine(ans);		
	}	
}





Measured memory_bytes: 25564
#include <stdio.h>
 
int main( void )
{
	long long A,B ;
	scanf("%ld %ld", &A, &B ) ;
   	long long M[2][4] = {{B^0,B^A,B^1,B^(A-1)},{A,1,A-1,0}};
	printf("%ld\n", M[B%2][(B-A)%4]) ;
	return 0 ;
}

Measured memory_bytes: 128
import java.util.*

fun main(args: Array<String>) {
    val sc = Scanner(System.`in`)
    val a = sc.nextInt()
    val b = sc.nextInt()
    println(answer063aAlt(a, b))
}

fun answer063aAlt(a: Int, b: Int): String {

    return (if (a + b < 10) {
        a + b
    } else {
        "error"
    }
            ).toString()

}

Measured memory_bytes: 31260
#include <iostream>
#include <algorithm>

#define MOD 1000000007

using namespace std;

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

    long dp[L.length() + 1][2];
    for(int i=0; i<=L.length(); i++){
        dp[i][0] = 0;
        dp[i][1] = 0;
    }

    dp[0][0] = 0;
    dp[0][1] = 1;
    for(int i=0; i<L.length(); i++){
        if(L[i] == '1') dp[i+1][1] = dp[i][1]*2%MOD;
        else dp[i+1][1] = dp[i][1]*1%MOD;

        dp[i+1][0] = dp[i][0]*3%MOD;
        if(L[i] == '1') dp[i+1][0] +=  dp[i][1]%MOD;

        dp[i+1][0] %= MOD;
        dp[i+1][1] %= MOD;
    }

    cout << (dp[L.length()][0] + dp[L.length()][1]) % MOD;
    return 0;
}

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

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

int nextInt() { int x; scanf("%d", &x); return x;}
ll nextLong() { ll x; scanf("%lld", &x); return x;}

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

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}


Measured memory_bytes: 256
from sys import stdin
from heapq import heappush, heappop
solution = [i for i in range(1, 16)] + [0]
sol_idx = (15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
MAX_DEPTH = 45
count = 0
neighbor = (
    (1, 4),
    (0, 2, 5),
    (1, 6, 3),
    (2, 7),
    (0, 5, 8),
    (1, 4, 6, 9),
    (2, 5, 7, 10),
    (3, 6, 11),
    (4, 9, 12),
    (5, 8, 10, 13),
    (6, 9, 11, 14),
    (7, 10, 15),
    (8, 13),
    (9, 12, 14),
    (10, 13, 15),
    (11, 14)
)
distance = (
    (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
    (0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6),
    (1, 0, 1, 2, 2, 1, 2, 3, 3, 2, 3, 4, 4, 3, 4, 5),
    (2, 1, 0, 1, 3, 2, 1, 2, 4, 3, 2, 3, 5, 4, 3, 4),
    (3, 2, 1, 0, 4, 3, 2, 1, 5, 4, 3, 2, 6, 5, 4, 3),
    (1, 2, 3, 4, 0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5),
    (2, 1, 2, 3, 1, 0, 1, 2, 2, 1, 2, 3, 3, 2, 3, 4),
    (3, 2, 1, 2, 2, 1, 0, 1, 3, 2, 1, 2, 4, 3, 2, 3),
    (4, 3, 2, 1, 3, 2, 1, 0, 4, 3, 2, 1, 5, 4, 3, 2),
    (2, 3, 4, 5, 1, 2, 3, 4, 0, 1, 2, 3, 1, 2, 3, 4),
    (3, 2, 3, 4, 2, 1, 2, 3, 1, 0, 1, 2, 2, 1, 2, 3),
    (4, 3, 2, 3, 3, 2, 1, 2, 2, 1, 0, 1, 3, 2, 1, 2),
    (5, 4, 3, 2, 4, 3, 2, 1, 3, 2, 1, 0, 4, 3, 2, 1),
    (3, 4, 5, 6, 2, 3, 4, 5, 1, 2, 3, 4, 0, 1, 2, 3),
    (4, 3, 4, 5, 3, 2, 3, 4, 2, 1, 2, 3, 1, 0, 1, 2),
    (5, 4, 3, 4, 4, 3, 2, 3, 3, 2, 1, 2, 2, 1, 0, 1),
    (6, 5, 4, 3, 5, 4, 3, 2, 4, 3, 2, 1, 3, 2, 1, 0)
)
def get_diff(B):
    return sum([distance[v][i] for i, v in enumerate(B)])
def get_next_board(board, space, prev):
    for nxt in neighbor[space]:
        if nxt == prev: continue
        b = board[:]
        b[space], b[nxt] = b[nxt], 0
        yield b, nxt
def answer_is_odd(board):
    return sum(divmod(board.index(0), 4)) % 2
def search(board):
    lower = get_diff(board)
    start_depth = lower
    if (lower % 2) ^ answer_is_odd(board): start_depth += 1
    for limit in range(start_depth, MAX_DEPTH + 1, 2):
        get_next(board, limit, 0, board.index(0), None, lower)
        if count > 0: return limit
def get_next(board, limit, move, space, prev, lower):
    global count
    if move == limit:
        if board == solution: count += 1
    else:
        for b, nxt in get_next_board(board, space, prev):
            p = board[nxt]
            new_lower = lower - distance[p][nxt] + distance[p][space]
            if new_lower + move <= limit:
                get_next(b, limit, move + 1, nxt, space, new_lower)
boad = []
for i in range(4):
    boad.extend(map(int, stdin.readline().split()))
print(search(boad))

Measured memory_bytes: 7968
fun main(args: Array<String>) {

    val n = readLine()!!.toInt()

    val tasks = (1..n).map { readLine()!!.split(" ").map { it.toInt() } }

    val sortedTasks = tasks.sortedBy { it.last() }.toTypedArray()

    if(sortedTasks.size == 1){
        println(if(sortedTasks.first().first() <= sortedTasks.first().last()) "Yes" else "No")
        return
    }

    var count = 0
    var sum = sortedTasks.first().first()
    for (i in 0 until sortedTasks.size - 1) {
        sum += sortedTasks[i + 1].first()
        if (sum > sortedTasks[i + 1].last()) break
        count++
    }

    if (count == sortedTasks.size - 1) println("Yes") else println("No")
}

Measured memory_bytes: 133248
#include <bits/stdc++.h>
#define lli long long int
using namespace std;
#define mod 1000000007
#define MOD 1000000037
#define mod1 998244353
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 
#define INF 2e9

lli n;
vector<lli>color;
vector<vector<lli> >v;
vector<lli>dp1,dp2;
vector<bool>vis;

lli dfs(lli u)
{
    vis[u]=true;
    if(v[u].size()==1 and u!=0)
    {
        dp2[color[u]]+=1;
        return 1;
    }
    lli tn=0,ei=dp2[color[u]];
    for(int i=0;i<v[u].size();i++)
    {
        if(!vis[v[u][i]])
        {
            lli t=dfs(v[u][i]);
            lli en=t-dp2[color[u]]+ei;
            dp1[color[u]]+=en*1LL*(en+1)/2;
            dp2[color[u]]+=en;
            ei=dp2[color[u]];
            tn+=t;
        }
    }
    dp2[color[u]]+=1;
    // cout << u+1 << endl;
    // cout << tn << " " << ei << endl;
    // cout << dp2[0] << " "<< dp2[1] << " " << dp2[2] << endl;
    // cout << dp1[0] << " "<< dp1[1] << " " << dp1[2] << endl;
    return tn+1;
}   

int main()
{
    cin >> n;
    color.resize(n);
    v.resize(n);
    dp1.resize(n,0);
    dp2.resize(n,0);
    vis.resize(n,false);
    map<lli,lli>ma;
    for(lli i=0;i<n;i++)
    {
        cin >> color[i];
        color[i]-=1;
        ma[color[i]]=1;
    }
    for(lli i=1;i<n;i++)
    {
        lli x,y;
        cin >> x >> y;
        x-=1;y-=1;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(0);
    lli nn=n;
    // for(lli i=0;i<n;i++)
    //     cout << dp2[i] << " ";
    // cout << endl;
    // for(lli i=0;i<n;i++)
    //     cout << dp1[i] << " ";
    // cout << endl;
    for(lli i=0;i<n;i++)
    {
        if(ma.find(i)!=ma.end())
        {
            lli en=nn-dp2[i];
            dp1[i]=dp1[i]+en*1LL*(en+1)/2;
        }
    }
    // for(lli i=0;i<n;i++)
    //     cout << dp1[i] << " ";
    // cout << endl;
    lli tw=n*1LL*(n+1)/2;
    for(lli i=0;i<n;i++)
    {
        if(ma.find(i)!=ma.end())
            cout << tw-dp1[i] << endl;
        else
            cout << "0" << endl; 
    }
}

Measured memory_bytes: 42224
#include <bits/stdc++.h>
using namespace std;
double r;
#define pi acos(-1)

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie();
	cout.tie();
	cin >> r;
	cout << fixed << setprecision(20) << 2*pi*r << '\n';
}

Measured memory_bytes: 3804
#include <iostream>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cassert>
#include <climits>
#include <bitset>
#include <functional>

#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)

using namespace std;

const int64_t kRem = 1000000007;

int64_t pow_rem(int64_t x, int64_t m, int64_t rem)
{
	if (m == 0) {
		return 1;
	}
	if (m == 1) {
		return x;
	}

	if (m & 1) {
		int64_t val = pow_rem(x, m / 2, rem);
		val = val * val;
		val %= rem;
		val *= x;
		val %= rem;
		return val;
	}
	else {
		int64_t val = pow_rem(x, m / 2, rem);
		val = val * val;
		val %= rem;
		return val;
	}
}

int rem13(int m, int a) {
	if (m == 0) return a;
	else {
		int remm = pow_rem(10, m, 13);
		return (remm * a) % 13;
	}
}

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

	string s; cin >> s;

	vector<int> unfixed;

	int obj_val = 5;
	int sum_fixed = 0;
	FOR_LT(i, 0, s.size()) {
		int sidx = s.size() - 1 - i;
		char c = s[sidx];
		if (c == '?') {
			unfixed.push_back(i);
		}
		else {
			int val = rem13(i, c - '0');
			obj_val -= val;
			if (obj_val < 0) obj_val += 13;

			sum_fixed += val;
			if (sum_fixed >= 13) sum_fixed -= 13;
			//cout << "sum : " << i << " " << val << " " << sum_fixed << endl;
		}
	}

	vector<int64_t[13]> dp(unfixed.size() + 1);

	//cout << obj_val << " " << sum_fixed << endl;

	{
		dp[0][sum_fixed] = 1;
	}
	FOR_LT(i, 0, unfixed.size()) {
		int v = rem13(unfixed[i], 1);
		//cout << i << " " << unfixed[i] << " " << v << endl;
		FOR_LT(j, 0, 10) {
			int vt = (v * j) % 13;
			//cout << j << " " << vt << " " <<  endl;
			FOR_LT(k, 0, 13) {
				dp[i + 1][(k + vt) % 13] += dp[i][k];
			}
		}

		FOR_LT(k, 0, 13) {
			dp[i + 1][k] %= kRem;
		}

		//cout << i << " : ";
		FOR_LT(k, 0, 13) {
			//cout << dp[i + 1][k] << " ";
		}
		//cout << endl;
	}

	cout << dp[unfixed.size()][5] << endl;

	return 0;
}

Measured memory_bytes: 11080
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f
#define MAXN int(1e6)+5
#define endl '\n'
#define ll long long
#define int long long
#define f first
#define s second
#define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0)
#define debug(x) cout << "DEBUG " << x << endl
#define debug2(x, y) cout << "DEBUG " << x << " " << y << endl
#define debug3(x, y, z) cout << "DEBUG " << x << " " << y << " " << z<< endl
#define debug4(x, y, z, o) cout << "DEBUG " << x << " " << y << " " << z<< " " << o << endl
using namespace std;
typedef pair<ll, ll> pii;
typedef vector<vector<ll>> mat;
const ll mod = 1e9+7;
 
int n;
 
mat mult(mat a, mat b) {
	mat c(n, vector<ll>(n, 0));
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			for(int z = 0; z < n; z++)
				c[i][j] = (c[i][j] + (a[i][z]*b[z][j])%mod)%mod;
	return c;
}
 
mat expo(mat a, int p) {
	if(p == 1) return a;
	if(p & 1) return mult(a, expo(a, p-1));
	mat b = expo(a, p/2);
	return mult(b, b);
}
 
int32_t main() {
	
	int k;
	cin >> n >> k;
	mat bs(n, vector<ll>(n, 0));
	mat recur(n, vector<ll>(1, 0));
	recur[0][0] = 1;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			cin >> bs[i][j];
	
	bs = expo(bs, k);
	
	int xd = 0;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			xd = (bs[i][j] + xd)%mod;
	cout << xd << endl;
}
 


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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define Abs(x)((x) < 0 ? (x) * -1 : (x))

#define rep(x, y) for ((x) = 0; (x) < (y); (x)++)
#define repin(x, y) for ((x) = 0; (x) <= (y); (x)++)
#define nep(x, y) for ((x) = (y) - 1; 0 <= (x); (x)--)
#define nepi(x, y, z) for ((x) = (y) - 1; (z) <= (x); (x)--)
#define repi(x, y, z) for ((x) = (z); (x) < (y); (x)++)
#define repiin(x, y, z) for ((x) = (z); (x) <= (y); (x)++)
#define reps(x, y, z) for ((x) = 0; (x) < (y); (x) += (z))
#define repis(x, y, z, s) for ((x) = (z); (x) < (y); (x) += (s))
#define repit(x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define nepit(x) for (__typeof((x).rbegin()) it = (x).rbegin(); it != (x).rend(); it++)

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

#define Mem0(x) memset(x, 0, sizeof(x))
#define Mem1(x) memset(x, -1, sizeof(x))

#define Unique(v) v.resize(unique(All(v)) - v.begin())

#define peq(p0, p1)(p0.first == p1.first && p0.second == p1.second)

#define End '\n'
#define Out(x) cout<<(x)<<End
#define OutP(p, sep, end) cout<<p.first<<sep<<p.second<<end

template<typename T> inline bool Max(T &x, const T &y) { return x < y ? x = y, 1 : 0; }
template<typename T> inline bool Min(T &x, const T &y) { return x > y ? x = y, 1 : 0; }
template<typename T> using V = vector<T>;
template<typename T> using VV = V<V<T> >;

#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define lb std::lower_bound
#define ub std::upper_bound

#define lbi(v, x) lb(All(v), (x))-v.begin()
#define ubi(v, x) ub(All(v), (x))-v.begin()

static const ll MOD = 1e9 + 7;
static const double PI = 3.141592653589793;

#define LOCAL 0

int main()
{
#if LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    freopen("debug.txt", "w", stderr);
#endif

    cin.tie(0);
    ios::sync_with_stdio(false);
	//std::cout.precision(18);

	int n, m;

	cin >> n >> m;

	map<int, int> table;
	int a;
	int ans = 0;

	int i, j;

	rep(i, n) {
	    cin >> j;

		while (j--) {
			cin >> a;

		    table[a]++;
		}
	}

	repit(table) if (it->Y == n) ans++;

	Out(ans);
	
	return 0;
}


Measured memory_bytes: 256
def slove():
    import sys
    import itertools
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    ls = list("357")
    cnt = 0
    for i in range(1, 10):
        for v in itertools.product(ls, repeat=i):
            if "3" in v and "5" in v and "7" in v:
                t = int("".join(v))
                if t > n:
                    break
                else:
                    cnt += 1
    print(cnt)


if __name__ == '__main__':
    slove()


Measured memory_bytes: 63216
#include<stdio.h>
int main(){
    int a[3][3];
    int c[3][3] = {{0,0,0},{0,0,0},{0,0,0}};
    int n,i,j,k;
    int b[100];
    int flag;
    
    for(i = 0 ; i < 3 ; i++){
        scanf("%d %d %d",&a[i][0],&a[i][1],&a[i][2]);
    }
    scanf("%d",&n);
    for(i = 0 ; i < n ; i++){
        scanf("%d",&b[i]);
    }
    
    for(i = 0 ; i < n ; i++){
        for(j = 0 ; j < 3 ; j++){
            for(k = 0 ; k < 3 ; k++){
                if(b[i] == a[j][k]){
                    c[j][k] = 1;
                }
            }
        }
    }
//    for(i = 0 ; i < 3 ; i++){
//        for(j = 0 ; j < 3 ; j++){
//            printf("%d ",c[i][j]);
//        }
//        printf("\n");
    
    if(c[0][0]==1 && c[0][1]==1 && c[0][2]==1) flag = 0;
    else if(c[1][0]==1 && c[1][1]==1 && c[1][2]==1) flag = 0;
    else if(c[2][0]==1 && c[2][1]==1 && c[2][2]==1) flag = 0;
    else if(c[0][0]==1 && c[1][0]==1 && c[2][0]==1) flag = 0;
    else if(c[0][1]==1 && c[1][1]==1 && c[2][1]==1) flag = 0;
    else if(c[0][2]==1 && c[1][2]==1 && c[2][2]==1) flag = 0;
    else if(c[0][0]==1 && c[1][1]==1 && c[2][2]==1) flag = 0;
    else if(c[0][2]==1 && c[1][1]==1 && c[2][0]==1) flag = 0;
    else flag = 1;
    
    if(flag == 0) printf("Yes\n");
    else printf("No\n");

    return 0;
}


Measured memory_bytes: 128
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <unordered_set>
#include <map>
#include <numeric>
#include <algorithm>

using namespace std;

int dp[101][2][5] = {};

int main() {
  std::string N;
  int K;
  std::cin >> N >> K;
  int L = N.size();
  dp[0][0][0] = 1;
  for (int i = 0; i < L; ++i) {
    for (int j = 0; j < 2; ++j) {
      for (int k = 0; k <= K; ++k) {
        const int D = j ? 9 : N[i] - '0';
        for (int d = 0; d <= D; ++d) {
          dp[i+1][j || (d < D)][k + (d != 0)] += dp[i][j][k];
        }
      }
    }
  }
  std::cout << dp[L][0][K] + dp[L][1][K] << std::endl;
  return 0;
}


Measured memory_bytes: 256
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

def main():
	S = input()
	s = list(S)
	num = 0
	for i in range(4):
		if S[i] == '+':
			num += 1
		else:
			num -= 1
	print(num)

if __name__ == '__main__':
	main()


Measured memory_bytes: 2940
#include<bits/stdc++.h>
using namespace std;
inline int rd(char c[]){
  int i, sz=0;
  for(;;){
    i = getchar_unlocked();
    if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
      break;
    }
  }
  c[sz++] = i;
  for(;;){
    i = getchar_unlocked();
    if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF){
      break;
    }
    c[sz++] = i;
  }
  c[sz]='\0';
  return sz;
}
inline void wt_L(char a){
  putchar_unlocked(a);
}
inline void wt_L(const char c[]){
  int i=0;
  for(i=0;c[i]!='\0';i++){
    putchar_unlocked(c[i]);
  }
}
char S[3][12];
int main(){
  int i;
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=0;Lj4PdHRW<3;Lj4PdHRW++){
      rd(S[Lj4PdHRW]);
    }
  }
  for(i=0;i<3;i++){
    S[0][i] = S[i][0] - 'a' + 'A';
  }
  S[0][3] = '\0';
  wt_L(S[0]);
  wt_L('\n');
  return 0;
}
// cLay varsion 20190721-1

// --- original code ---
// char S[3][12];
// {
//   rd(S(3));
//   rep(i,3) S[0][i] = S[i][0] - 'a' + 'A';
//   S[0][3] = '\0';
//   wt(S[0]);
// }


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

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  vector<int> L(N);
  for (int i = 0; i < N; i++) {
    cin >> L[i];
  }
  sort(L.begin(), L.end());
  int res = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      int k = lower_bound(L.begin(), L.end(), L[i] + L[j]) - L.begin();
      res += max(0, k - j - 1);
    }
  }
  cout << res << '\n';
  return 0;
}


Measured memory_bytes: 384
#include <stdio.h>
#define REP(i, n) for(i = 0; i < n; ++i)
#define FOR(i, a, b) for(i = a; i < b; ++i)

int add_sub(int a, int op, int b){
    if(op){
        return a + b;
    }else{
        return a - b;
    }
}
void pm(int a){
    if(a){
        printf("+");
    }else{
        printf("-");
    }
}

void show(int op, int X[4]){
    printf("%d", X[0]);
    pm((op & 4) > 0);
    printf("%d", X[1]);
    pm((op & 2) > 0);
    printf("%d", X[2]);
    pm((op & 1) > 0);
    printf("%d=7\n", X[3]);
}

int main(){
    int X[4];
    char c;
    int i;
    int ret, op = 0;

    REP(i, 4){
        scanf("%c", &c);
        X[i] = c - '0';
    }
    ret = X[0];

    REP(i, 8){
            ret = add_sub(ret, (i & 4) > 0, X[1]);
            ret = add_sub(ret, (i & 2) > 0, X[2]);
            ret = add_sub(ret, (i & 1) > 0, X[3]);
            if(ret == 7){
                op = i;
                break;
            }
            ret = X[0];
    }

    show(op, X);
    return 0;
}

Measured memory_bytes: 128
#include <iostream>
#include <vector>
#include <deque>

using namespace std;

int main()
{
    int n; cin >> n;
    deque<int>  b;

    for (int i = 0; i < n; ++i) {
        int tmp;
        cin >> tmp;
        if (i % 2 == n % 2) b.push_back(tmp);
        else b.push_front(tmp);
    }

    for (int i = 0; i < n; ++i) {
        if (i != 0) cout << " ";
        cout << b[i];
    }
    cout << endl;

    return 0;
}


Measured memory_bytes: 2944
n = gets.chomp.to_i
arr = gets.chomp.split.map(&:to_i)
t = []
n.times do |i|
  t << arr.shift
end

# swap????????????
def swap(list, idx1, idx2)
  list[idx1], list[idx2] = list[idx2], list[idx1]
  return list
end

def minCostPath(list)
  sorted = list.sort
  total = 0
  
  # list?????????
  list.size.times do |i|
    # ???????????????????????????????????????????????£?????????
    idx1 =sorted[i]
   
    # ??????????????????????????????????±??????????index????????£?????????
    idx2 = list.index idx1
    n = 0 
    while idx2 > i
      # ??????????????????
      n += 1

      sortedidx = list.index sorted[idx2]
      total += list[sortedidx]
      list = swap(list, idx2, sortedidx) 
      idx2 = sortedidx
    end
    total += [idx1 * n, idx1*2+sorted[0]*(n+2)].min
  end
  return total
end

total = minCostPath(t)
p total

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

using namespace std;
using ll = long long;
using pint = pair<int, int>;
#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define IREP(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define ALL(x) (x).bigin(), (x).end()
#define MOD 1000000007
#define IMAX INT_MAX
#define IMIN INT_MIN
#define LMAX LLONG_MAX
#define LMIN LLONG_MIN
template<typename T> inline void chmin(T &a, T b){ if(a > b) a = b; }
template<typename T> inline void chmax(T &a, T b){ if(a < b) a = b; }
template<typename T> inline void xch(T &a, T &b){ T w = a; a = b; b = w; }

int main() {
    int s;
    cin >> s;
    int fir = s / 100, sec = s % 100;
    if(0 < fir && fir < 13){
        if(0 < sec && sec < 13) cout << "AMBIGUOUS" << endl;
        else cout << "MMYY" << endl;
    }
    else{
        if(0 < sec && sec < 13) cout << "YYMM" << endl;
        else cout << "NA" << endl;
    }
    return 0;
}







Measured memory_bytes: 256
#include <stdio.h>
#include <string>
#include <iostream>
#include <iomanip>
#include <map>
#include <algorithm>
#include <vector>
#include <math.h>
#include <queue>
#define MOD (long long)1000000007
#define INF (long long)998244353
using namespace std;

int main(void){
    int n;
    cin >> n;
    vector<int> a(n);
    for(int i=0; i<n; i++)cin >> a[i];
    vector<long long> color(3,-1);
    long long count=0;
    long long ans=1;
    int flag=-1;
    for(int i=0; i<n; i++){
        flag=1;
        count=0;
        for(int j=0; j<3; j++){
            if(color[j]+1==a[i]){
                if(flag){
                    color[j]+=1;
                    flag=0;
                }
                count++;
            }
        }
        ans=(ans*count)%MOD;
    }
    cerr << color[0] << " " << color[1] << " " << color[2] << endl;
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for(int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  string s;
  ll k;
  cin >> s >> k;
  ll n = min(ll(s.size()), k);
  rep(i, n) {
    if(i == n - 1) {
      cout << s[i] << endl;
      return 0;
    }
    if(s[i] == '1') continue;
    cout << s[i] << endl;
    return 0;
  }
}

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;

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


signed main(void) {
    int m,n;
    cin >> n >> m;
    vector<vi> g(n);
    vi in(n);
    rep(i,m){
        int x,y;
        cin >> x >> y;
        x--;y--;
        g[x].push_back(y);
        in[y]++;
    }
    queue<int> q;
    rep(i,n)if(in[i] == 0)q.push(i);
    while(q.size()){
        int now = q.front();
        cout << now+1 << endl;
        q.pop();
        rep(i,g[now].size()){
            int to = g[now][i];
            in[to]--;
            if(in[to] == 0)q.push(to);
        }
    }
}



Measured memory_bytes: 3136
#include<bits/stdc++.h>
#define faster_io ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define mem(a,b) memset(a,b,sizeof a)
#define F first
#define S second
#define ll long long
#define pb push_back
#define pll pair<ll, ll>
using namespace std;
const ll N = 1e6 + 7, M = 1e5 + 7, mod = 1e9 + 7, inf = 1e18 + 7;
ll tc, n, m, q, a, b, c, d, i, j, k, l, sum;
ll fr[11][11];
string s;
vector < ll > vec;
int main()
{
    cin >> n;
    for(i = 1; i <= n; i++)
    {
        j = i;
        a = 0;
        while(j / 10)
            a = j / 10, j /= 10;
        if(!a)
            a = i;
        fr[a][i % 10]++;
    }
    for(i = 0; i < 10; i++)
        for(j = 0; j < 10; j++)
            sum += fr[i][j] * fr[j][i];
    cout << sum << endl;
}


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

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)

using ll = long long;
ll INF = LLONG_MAX;
 
using vc = vector<char>;
using vi = vector<int>;

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int H, A; cin >> H >> A;
    cout << H / A + (H % A != 0) << endl;
}

Measured memory_bytes: 256
import java.util.*;
import java.util.Map.Entry;
 
 
 class Main {
	 static int mod =  (int) (Math.pow(10,9)+7);
    public static void main(String[] args) {
    	
        Scanner sc = new Scanner(System.in);
        int N= sc.nextInt();
        int[] a = new int[N];
        int[] b = new int[N];
        int M = 0;
        int k = 0;
		for(int i=0;i<N;i++){
			a[i]=sc.nextInt();
		}
		for(int i=N;i>0;i--) {
			int sum=0;
			for(int j=i;j<N+1;j=j+i) {
				if(b[j-1]==1) {
					sum++;
				}
			}
//	        System.out.println(sum);
			if(sum%2!=a[i-1]) {
//		        System.out.println(i);
				b[i-1]=1;
				M++;
			}
		}
		
        System.out.println(M);
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<N;i++) {
			if(b[i]==1) {
				if(i==N-1) {
					sb.append(i+1);
				} else {
					sb.append(i+1);				
					sb.append(" ");
				}
			}
		}
        System.out.println(sb);
    }
}

Measured memory_bytes: 77184
#include <stdio.h>
#include <stdlib.h>

struct node{
  struct node *right;
  struct node *left;
  struct node *parent;
  int key;
};
typedef struct node * Node;
#define NIL NULL

Node root;

Node treeMinimum(Node x){
  while(x->left != NIL) 
    x = x->left;
  return x;
}

Node treeMaximum(Node x){
  while(x->right != NIL)
    x = x->right;
  return x;
}

Node treeSearch(Node u, int k){
  if(u == NIL || k == u->key)
    return u;
  if(k < u->key)
    return treeSearch(u->left,k);
  else
    return treeSearch(u->right,k);
}

Node treeSuccessor(Node x){
  Node y;
  if(x->right != NIL)
    return treeMinimum(x->right);

  y = x->parent;
  while(y != NIL && x == y->right) {
    x = y;
    y = y->parent;
  }
  return y;
}

void treeDelete(Node z){
  Node y; // node to be deleted
  Node x; // child of y

  if(z->left == NIL || z->right == NIL)
    y = z;
  else
    y = treeSuccessor(z);

  if(y->left != NIL)
    x = y->left;
  else
    x = y->right;

  if(x != NIL)
    x->parent = y->parent;

  if(y->parent == NIL)
    root = x;
  else if(y == y->parent->left)
    y->parent->left = x;
  else y->parent->right = x;

  if(y != z)
    z->key = y->key;

}

void insert(int k){
  Node y = NIL;
  Node x = root;
  Node z;

  z = malloc(sizeof(struct node));
  z->key = k;
  z->left = NIL;
  z->right = NIL;

  y = NIL;
  x = root;
  while(x != NIL) {
    y = x;
    if(z->key < x->key)
      x = x->left;
    else
      x = x->right;
  }
  z->parent = y;

  if(y == NIL)
      root = z;
    else if(z->key < y->key)
      y->left = z;
    else
      y->right = z;
}

void inorder(Node u){
  if(u == NIL)
    return;
  inorder(u->left);
  printf(" %d",u->key);
  inorder(u->right);
}
void preorder(Node u){
  if(u == NIL)
    return;
  printf(" %d",u->key);
  preorder(u->left);
  preorder(u->right);
}


int main(){
  int n, i, x;
  char com[20];
  scanf("%d", &n);

  for ( i = 0; i < n; i++ ){
    scanf("%s", com);
    if ( com[0] == 'f' ){
      scanf("%d", &x);
      Node t = treeSearch(root, x);
      if ( t != NIL ) printf("yes\n");
      else printf("no\n");
    } else if ( com[0] == 'i' ){
      scanf("%d", &x);
      insert(x);
    } else if ( com[0] == 'p' ){
      inorder(root);
      printf("\n");
      preorder(root);
      printf("\n");
    } else if ( com[0] == 'd' ){
      scanf("%d", &x);
      treeDelete(treeSearch(root, x));
    }
  }

  return 0;
}



Measured memory_bytes: 20456
#include <bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i=0;i<int(N);++i)
#define rep1(i,N) for(int i=1;i<int(N);++i)
#define all(a) (a).begin(),(a).end()
#define print(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<_<<", "; cerr<<"]"<<endl; }
#define printpair(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<"{"<<_.first<<","<<_.second<<"}"<<", "; cerr<<"]"<<endl; }
#define dump(x) cerr<<#x<<": "<<x<<endl;
#define bit(k) (1LL<<(k))
#define Yes "Yes"
#define No "No"
#define YES "YES"
#define NO "NO"
typedef long long ll;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
  os << "{" <<p.first << ", " << p.second << "}";
  return os;
}
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

const int INF = (ll)1e9;
const ll INFLL = (ll)1e18+1;
const ll MOD = (ll)1e9+7;
const double PI = acos(-1.0);
/*
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const string dir = "DRUL";
*/

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    vector<pair<int, int>> a(3);
    int x[3];
    cin >> x[0] >> x[1] >> x[2];
    int mx = max({x[0], x[1], x[2]});
    int sum = (x[0] + x[1] + x[2]);
    a[0] = {x[0]%2, x[0]};
    a[1] = {x[1]%2, x[1]};
    a[2] = {x[2]%2, x[2]};
    sort(all(a));
    int ans = 0;
    //全部等しいとき 1 1 1, 0, 0, 0
    if(a[0].first == a[2].first){
        ans +=  (3 * mx - sum)/2;;
    }
    // 0, 1, 1
    else if(a[1].first == 1 && a[2].first ==  1){
        a[1].second += 1;
        a[2].second += 1;
        ans += 2;
        ans += (3 * max({a[0].second,a[1].second,a[2].second}) - (sum + 4))/2;;
    }
    // 0, 0, 1
    else if(a[0].first == a[1].first){
        a[0].second += 1;
        a[1].second += 1;
        ans += 2;
        ans +=  (3 * max({a[0].second,a[1].second,a[2].second}) - (sum + 4))/2;;
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using Enu = System.Linq.Enumerable;

public class Program
{
    public void Solve()
    {
        int A = Reader.Int(), B = Reader.Int();
        if (A == 1) A = 20;
        if (B == 1) B = 20;

        Console.WriteLine(A == B ? "Draw" : A > B ? "Alice" : "Bob");
    }
}


class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    static TextReader reader = Console.In;
    static readonly char[] separator = { ' ' };
    static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    static string[] A = new string[0];
    static int i;
    static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Range(N, Int); }
    public static int[][] IntTable(int H) { return Range(H, IntLine); }
    public static string[] StringArray(int N) { return Range(N, Next); }
    public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); }
    public static string Line() { return reader.ReadLine().Trim(); }
    public static T[] Range<T>(int N, Func<T> f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; }
    static string[] Split(string s) { return s.Split(separator, op); }
    static string Next() { CheckNext(); return A[i++]; }
    static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}

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

main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	string a, b, xa, xb;
	cin >> a >> b;
	int ta = stoi(a), tb = stoi(b);
	for (int i = 0; i < ta; i++) xb += b;
	for (int i = 0; i < tb; i++) xa += a;
	cout << min(xa, xb) << '\n';
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <fstream>
#include <chrono>
#include <random>
#include <bitset>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
using ll = long long;
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}

int main(){
  ll N; cin >> N;
  vector<ll> A(N); rep(i,N) cin >> A[i];
  ll Q; cin >> Q;
  vector<pair<ll,ll>> BC(Q); rep(i,Q) cin >> BC[i].first >> BC[i].second;
  vector<ll> cnt(101000,0);
  rep(i,N) cnt[A[i]]++;
  ll sum = 0;
  rep(i,N) sum += A[i];
  rep(i,Q){
    ll b=BC[i].first;
    ll c=BC[i].second;
    sum -= b*cnt[b];
    sum += c*cnt[b];
    cnt[c]+=cnt[b];
    cnt[b]=0;
    cout << sum << '\n';
  }
  return 0;
}


Measured memory_bytes: 6324
//Ruthless Coding 
#include <bits/stdc++.h>

#define uni(x)      (x).resize(unique(ALL(x)) - (x).begin())
#define fprint(v)   for(auto x : v) cout << x << ' '
#define ALL(x)      (x).begin(), (x).end()
#define PI          3.14159265358979323846
#define MP(x, y)    make_pair(x, y)
#define SZ(x)       int((x).size())
#define PB(x)       push_back(x)
#define ld          long double
#define ll          long long
#define S           second 
#define F           first
#define nl          '\n'

using namespace std;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
mt19937_64 rnd;

const int N = 2e5 + 5;
const ll M = 1e9 + 7;
ll ad(ll a, ll b){
    return ((a % M) + (b % M)) % M;
}
ll mul(ll a, ll b){
    return ((a % M) * (b % M)) % M;
}
void tc(){
    ll n, k;
    cin >> n >> k;
    vector<int> a(n);
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    ll res = 0;
    for(int i = 0; i < n; i++){
        ll sm = 0;
        for(int j = i + 1; j < n; j++){
            if(a[j] < a[i]){
                sm++;
            }
        }
        ll lsm = 0;
        for(int j = 0; j < i; j++){
            if(a[j] < a[i]){
                lsm++;
            }
        }
        res = ad(res, mul(lsm, (k - 1) * k / 2));
        res = ad(res, mul(sm, k * (k + 1) / 2));
    }
    cout << res;
}
int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);
    int t = 1;
    // cin >> t;
    while(t--) tc();
    return 0;
}
/*
    *** Most Impo.. -> check base case always
    1. Overflow Check (*, +)
    2. Index check (0 - based or 1 - based)
    3. Check for n = 1, 2, 3, 4....
    4. Corner Cases
*/

Measured memory_bytes: 256
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
  Name:- OMHARI 
  Institution:- UNIVERSIT OF CALCUTTA(INFORMATION TECHNOLOGY)
  Email:- omharicu@gmail.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include<bits/stdc++.h>
using namespace std; 

#define F               first
#define S               second
#define ull             unsigned long long
#define ll              long long
#define ul              unsigned long
#define db              double
#define ST              string
#define pb              push_back
#define mp              make_pair
#define el              "\n"
#define vll             vector <ll>
#define vl              vector <long int>
#define vi              vector <int>
#define all(v)          (v.begin(), v.end()) 
#define rall(v)         (v.rbegin(), v.rend()) 
#define is_sort(v)      is_sorted all(v)
#define max_of(v)       *max_element all(v)
#define min_of(v)       *min_element all(v)
#define itos(s)         to_string(s)
#define cntone(x)       __builtin_popcountll(x)
#define cntzro(x)       __builtin_clz(x)
#define For(i, a, b)    for(int i=a; i<b; i++)
#define Forr(i, a, b)   for(int i=a; i>=b; i--)
const ll mod = 1e9+7;

void solve()
{
    int n;
    cin>>n;
    vll v(n+1);

    For(i, 1, n+1)
    {
        ll j=(ll)i;
        while(j<=n)
        {
            v[j]++;
            j+=i;
        }
    }
    ll ans=0;

    For(i, 1, n+1)
    ans+=(ll)(i*v[i]);

    cout<<ans;
}

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

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    /***************-Code starts from here-***************/
    // int t;
    // cin>>t;
    // while(t--)
    solve();
    return 0; 
} 

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

const int N=200100;
int n,f[N],lst[N][26];
char s[N];

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>s;n=strlen(s);
	f[n]=1;fill(lst[n],lst[n]+26,n);
	for(int i=n-1;~i;--i){
		memcpy(lst[i],lst[i+1],sizeof lst[i+1]);
		lst[i][s[i]-'a']=i;
		f[i]=n+1;
		for(int j=0;j<26;++j)f[i]=min(f[i],f[lst[i][j]+1]+1);
	}
	for(int len=f[0],i=0;len --> 0;){
		for(int j=0;j<26;++j)if(f[lst[i][j]+1]+1==f[i]){
			cout<<char('a'+j);i=lst[i][j]+1;
			break;
		}
	}
	cout<<'\n';
	return 0;
}


Measured memory_bytes: 21504
h, w, k = map(int, input().split())
s = [input() for _ in range(h)]
a = [[0]*w for _ in range(h)]
cnt = 1
later = []
for i in range(h):
    if '#' not in s[i]: continue
    later.append(i)
    fst = True
    for j in range(w):
        if not fst and s[i][j] == '#':
            cnt += 1
        if fst and s[i][j] == '#':
            fst = False
        a[i][j] = cnt
    cnt += 1

for i in later:
    if i >= h-1: continue
    if a[i+1][0] != 0: continue
    for j in range(w):
        k = i
        while k < h-1:
            if a[k+1][j] == 0:
                a[k+1][j] = a[k][j]
                k += 1
            else:
                break

if later[0] != 0:
    for i in range(later[0], 0, -1):
        for j in range(w):
            a[i-1][j] = a[i][j]
for ans in a:
    print(*ans)


Measured memory_bytes: 12820
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=3010;
LL dp[N][N][4],mp[N][N];
int main()
{
    int n,m,nn;
    scanf("%d%d%d",&n,&m,&nn);
    for(int i=1;i<=nn;i++)
    {
        int r,c;
        scanf("%d%d",&r,&c);
        scanf("%lld",&mp[r][c]);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            for(int k=1;k<=3;k++)
            {
                dp[i][j][k]=max(dp[i][j][k],max(dp[i][j-1][k],dp[i][j-1][k-1]+mp[i][j]));
                dp[i][j][0]=max(dp[i][j][0],dp[i-1][j][k]);
                dp[i][j][1]=max(dp[i][j][1],dp[i-1][j][k]+mp[i][j]);
            }
        }
    }
    LL res=0;
    for(int k=1;k<=3;k++)
        res=max(res,dp[n][m][k]);
    cout<<res<<endl;
    return 0;
}


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

public class Main {
  public static void main(String[] args) throws IOException{
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String s = in.readLine();
    String x_result = "x";
    for(int cnt = 1; cnt<s.length(); cnt++){
      x_result += "x";
    }
    System.out.println(x_result);
  }
}

Measured memory_bytes: 22740
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int N,K;
  cin >> N;
  cin >> K;
  vector<int> x(N);
  int lenth = 0;
  for(int i=0;i<N;i++){
  	cin >> x.at(i);
    if(x.at(i)< K-x.at(i)){
      lenth += x.at(i)*2;
    }else{
      lenth += (K-x.at(i))*2;
    }
  }
  cout << lenth;
}

Measured memory_bytes: 256