akhauriyash/Code-Regression huggingface dataset:
#![allow(unused_imports)]

use proconio::{input, fastout};
use proconio::marker::*;

#[fastout]
fn main() {
    input! {
    n: usize,
    }
    let p = 1_000_000_007;
    let mut d = vec![1; 1005];
    for i in 2..=n {
        let ps = primes(i);
        for p in ps {
            d[p] += 1;
        }
    }
    let ans:usize = d.iter().fold(1 ,|acc, &x| acc * x % p);
    println!("{}", ans);
}

fn primes(m: usize) -> Vec<usize> {
    let mut l = Vec::new();
    let mut mm = m;
    for i in 2.. {
        if i * i > mm {break}
        while mm % i == 0 {
            l.push(i);
            mm /= i;
        }
    }
    if mm != 1 {l.push(mm)}
    l
}


Measured memory_bytes: 2088
#include<iostream>
using namespace std;
#include<vector>
#include<set>
#include<cmath>
#define int long long int
int function(int arr[],int index,int n,int memo[])
{
	if(memo[index]!=-1)
	{
		return memo[index];
	}
	else
	{
		int j;
		bool result=false;
		int val=arr[index];
		for(j=index+val;j<=n;j+=val)
		{
			if(arr[j]>arr[index])
			{
				int here=function(arr,j,n,memo);
				if(here==0)
				{
					result=true;
					break;
				}
			}
		}
		for(j=index-val;j>=1;j-=val)
		{
			if(arr[j]>arr[index])
			{
				int here=function(arr,j,n,memo);
				if(here==0)
				{
					result=true;
				}
			}
		}
		if(result)
		{
			memo[index]=1;
			return 1;
		}
		else
		{
			memo[index]=0;
			return 0;
		}
	}
}
int32_t main()
{
	 ios_base::sync_with_stdio(false);
	 cin.tie(NULL);
	 cout.tie(NULL);
	 int n;
	 cin>>n;
	 int arr[3*n+1];
	 int j;
	 for(j=1;j<=3*n;j++)
	 {
		 cin>>arr[j];
	 }
	 int prefix[3*n+1]={0};
	 int sum=0;
	 multiset<int> st;
	 for(j=1;j<=n;j++)
	 {
		 sum+=arr[j];
		 st.insert(arr[j]);
	 }
	 prefix[n]=sum;
	 for(j=n+1;j<=2*n;j++)
	 {
		 int x=arr[j];
		 int y=*st.begin();
		 if(x>y)
		 {
			 st.erase(st.begin());
			 st.insert(x);
			 sum+=(x-y);
		 }
		 prefix[j]=sum;
	 }
	 int suffix[3*n+1];
	 multiset<int> st2;
	 int sum2=0;
	 for(j=3*n;j>2*n;j--)
	 {
		 sum2+=arr[j];
		 st2.insert(arr[j]);
	 }
	 suffix[2*n+1]=sum2;
	 for(j=2*n;j>n;j--)
	 {
		 int x=arr[j];
		 int y=*st2.rbegin();
		 if(x<y)
		 {
			 auto it=st2.end();
			 it--;
			 st2.erase(it);
			 st2.insert(x);
			 sum2+=(x-y);
		 }
		 suffix[j]=sum2;
	 }
	 int answer=-1000000000000000000;
	 for(j=n;j<=2*n;j++)
	 {
		 int here=prefix[j]-suffix[j+1];
		 answer=max(answer,here);
	 }
	 cout<<answer<<endl;
}


Measured memory_bytes: 16384
//shan61916
#include <bits/stdc++.h>
    using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n" 
typedef long long ll;
typedef unsigned long long ull ;
typedef double dll ;

int main(){
 IOS
#ifdef SHAN
    freopen("input.txt" , "r" , stdin);  
#endif
  ll n, k;
  cin >> n >> k;
  --k;
  string s;
  cin >> s;
  for(ll i = 0 ; i < n; i++) {
    if(i == k) {
      cout << char(tolower(s[i]));
    }
    else {
      cout << s[i] ;
    }
  }
  return 0;
} //good night.

Measured memory_bytes: 256
n = int(input())
A = [int(x) for x in input().split()]
ans = 0
min_num = int(1e10)
odd_num = 0
for a in A:
    if a < 0:
        odd_num += 1
    a = abs(a)
    ans, min_num = ans + a, min(min_num, a)
if odd_num % 2 == 0:
    print(ans)
else:
    print(ans - min_num * 2)


Measured memory_bytes: 14412
#include <iostream>
#include <stack>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
	stack <int> s;
	int x,y,z; string ret;
	
	while(cin >> ret){
		if(ret == "+" || ret == "-" || ret =="*" || ret == "/"){
			y = s.top(); s.pop();
			x = s.top(); s.pop();
			switch(ret[0]){
				case '+': z = x + y; break;
				case '-': z = x - y; break;
				case '*': z = x * y; break;
				case '/': z = x / y; break;
			}
			s.push(z);
		}
		else
			s.push(atoi(ret.c_str()));
	}
	cout << s.top();
	cout << endl;
}

Measured memory_bytes: 1200
#pragma GCC optimize(2)
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, int> pdd;

const ll mod = 1e9 + 7;
const int N = 1e3 + 10, M = 1e5 + 10;
const ll INF = 1e18;
const int inf = 0x3f3f3f3f;
const double eps = 1e-4;

#define pi acos(-1)
#define mk make_pair

map<ll, int> mp;

ll fpow(ll a, ll b)
{
    ll res = 1;
    while (b)
    {
        if (b & 1)
            res = res * a;
        b >>= 1;
        a = a * a;
    }
    return res;
}

int main()
{
    for (int i = -N; i < N; i++)
    {
        mp[fpow(i, 5)] = i;
    }
    int x;
    cin >> x;
    for (int i = -N; i < N; i++)
    {
        if (mp.count(x + fpow(i, 5)))
        {
            cout << mp[x + fpow(i, 5)] << ' ' << i << endl;
            break;
        }
    }
    return 0;
}

/*

*/

Measured memory_bytes: 3768
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b) FOR(i,0,b)
#define INF mugen
#define dump(x) cerr<<#x<<"="<<x<<endl
#define ALL(a) (a).begin(),(a).end()
#define EACH(e,v) for(auto& e:v)
#define SORT(v) sort(ALL(v))
#define PERM(v) SORT(v);for(bool c##p=1;c##p;c##p=next_permutation(ALL(v)))
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<vii> viii;
typedef pair<int,int> P;
template <class T> void chmin(T & a, T const & b) { if (b < a) a = b; }
template <class T> void chmax(T & a, T const & b) { if (b > a) a = b; }
template <typename T>
string to_string(const T &n){ostringstream stm;stm << n;return stm.str();}
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
using ll = long long;
const ll mod = 1000'000'007;
#define int long long
int n;
int a[112345];
int naga[112345];
signed main(){
    cin>>n;
    rep(i,n){
        cin>>a[i];
    }
    sort(a,a+n);

    int flag=0;
    int tate=0;
    int ans=1;
    for(int i=n-1;i>=1;i--){
        if(a[i]==a[i-1]){
            ans*=a[i];
            flag++;
            i--;
        }
        if(flag==2)break;
    }
    if(flag==2){
        cout<<ans<<endl;
        return 0;
    }
    cout<<0<<endl;


 
}

Measured memory_bytes: 1024
import java.util.*;
import static java.util.Comparator.*;
class Restaurant {
    private String city;
    private int score;
    private int index;
    public Restaurant(String city, int score, int index) {
        this.city = city;
        this.score = score;
        this.index = index;
    }
    public String getCity() {return this.city;}
    public int getScore() {return this.score;}
    public int getIndex() {return this.index;}
}
public class Main {
    public static void main(String[] args) throws Exception {
        Scanner stdIn = new Scanner(System.in);
        int N = stdIn.nextInt();        
        List<Restaurant> rstList = new ArrayList<>();
        for(int i = 1; i <= N; i++) {
            rstList.add(new Restaurant(stdIn.next(), -stdIn.nextInt(), i));
        }
        rstList.sort(comparing(Restaurant::getCity).thenComparing(Restaurant::getScore));
        for(Restaurant rst : rstList) {
            System.out.println(rst.getIndex());
        }
    }
}

Measured memory_bytes: 26836
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <map>
#include <numeric>
#include <algorithm>
#include <stdio.h>

int main(void){
  int k,n,out=0;
  std::cin >> k >> n;
  std::vector<int> a(n);
  std::vector<int> s(n);
  for(int i=0;i<n;i++){
    std::cin >> a[i];
  }
  for(int i=0;i<n-1;i++){
    s[i] = a[i+1] - a[i];
  }
  s[n-1] = a[0] + k - a[n-1];
  std::sort (s.begin(),s.end());
  for(int i=0;i<n-1;i++){
    out = out + s[i];
  }
  std::cout << out;
  return 0;
}

Measured memory_bytes: 4744
import sys
sys.setrecursionlimit(10**8)

# ナップザック問題 典型2
N, W = map(int, input().split())
wv = [list(map(int, input().split())) for i in range(N)]
V = sum([v for w, v in wv])

dp = [[10**10]*(V+1) for i in range(N+1)]
dp[0][0] = 0

for i in range(N):
    for j in range(0, V+1):
        w, v = wv[i]
        if (j-v) >= 0:
            dp[i+1][j] = min([dp[i][j], dp[i][j-v]+w])
        else:
            dp[i+1][j] = dp[i][j]

ans = 0
for i in range(0, V+1):
    if dp[N][i] <= W:
        ans = i

print(ans)


Measured memory_bytes: 120428
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(a) (long long)((a).size())
const double pi = 3.14159265358979323846;

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

    ll n;
    cin>>n;
    ll i,j;
    vector<string> s(n);
    map<string,ll> mp;
    for(i=0;i<n;i++) cin>>s[i], mp[s[i]]++;
    cout<<"AC x "<<mp["AC"]<<"\n";
    cout<<"WA x "<<mp["WA"]<<"\n";
    cout<<"TLE x "<<mp["TLE"]<<"\n";
    cout<<"RE x "<<mp["RE"]; 
}

Measured memory_bytes: 6380
#define _USE_MATH_DEFINES
 
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>

using namespace std;

int main(){

	int card[4];
	char c;

	while(cin >> card[0] ){
		for(int i=1;i<5;i++){
			cin >> c >> card[i];
		}
		sort(card, card+5);

		int two = 0;
		bool three = 0;
		bool four = 0;

		for(int i=0;i<5;i++){
			int count = 1;
			for(int j=i+1;j<5;j++){
				if(card[i]==card[j]){
					count++;
				}
				else{
					break;
				}
			}
			if(count==2){
				two+=1;
				i+=1;
			}
			else if(count==3){
				three=1;
				i+=2;
			}
			else if(count==4){
				four=1;
				i+=3;
			}
		}

		if(two && three){
			cout << "full house" << endl;
		}else if(two==2){
			cout << "two pair" <<endl;
		}else if(two==1){
			cout << "one pair" <<endl;
		}else if(three){
			cout << "three card" <<endl;
		}else if(four){
			cout << "four card" <<endl;
		}else{
			bool straight_flag = 1;
			bool ace_flag = 1;
			bool has_13 = 0;
			if(card[4]==13){
				has_13=1;
			}

			for(int i=3;i>=0;i--){
				if(ace_flag && card[i+1]-1==card[i]){
					//cout<<1;
				}else if(ace_flag && has_13 && card[i]==i+1){
					//cout<<0;
					ace_flag=0;
				}else{
					straight_flag=0;
					break;
				}
			}

			if(straight_flag){
				cout << "straight" << endl;
			}else{
				cout << "null" << endl;
			}
		}
	}
	return 0;
}

Measured memory_bytes: 1176
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String N = sc.next();
		
		if (N.length() > 2) {
			System.out.print(new StringBuilder(N).reverse().toString());
		} else {
			System.out.println(N);
		}
	}
}

Measured memory_bytes: 23764
#include <cstdio>
#include <cctype>
#include <algorithm>
#define repu(i,x,y) for (int i=x; i<=y; ++i)
#define repd(i,x,y) for (int i=x; i>=y; --i)
using namespace std;

typedef long long LL;
int n,a[500100],d[500100];
LL minw[500100];

int getint()
{
    char ch;
    while (!isdigit(ch=getchar()));
    int x=ch-'0';
    for (; isdigit(ch=getchar()); x=x*10+ch-'0');
    return x;
}

int main()
{
    scanf("%d%d",&n,&d[0]);
    repu(i,1,n)
        a[i]=getint(),d[i]=min(abs(d[i-1]-a[i]),d[i-1]);
    minw[n+1]=1;
    repd(i,n,1)
    {
        minw[i]=minw[i+1]+a[i];
        if (minw[i+1]*2<=a[i])
            minw[i]=a[i]-minw[i+1];
        if (abs(minw[i+1]-a[i])>=minw[i+1])
            minw[i]=minw[i+1];
    }
    for (int m=getint(); m--;)
    {
        int x=getint();
        puts(minw[x+1]<=d[x-1]?"YES":"NO");
    }
    return 0;
}

Measured memory_bytes: 9728
n = int(input())
str = ""

one = False
while n > 0:
    mod_c = (n-1) % 26
    chr_n = (mod_c + 97)
    str = chr(chr_n) + str
    n = n-mod_c+1
    if n == 26:
        break
    n = int(n / 26)

print(str)

Measured memory_bytes: 9096
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
ll s[100000];
bool cmp(ll a,ll b)
{
	return a>b;
}
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF) {
		for(int i=0; i<n; i++) {
			scanf("%lld",&s[i]);
		}
		sort(s,s+n,cmp);
		int x=0,y=0;
		for(int i=0; i<n-1; i++) {
			if(s[i]==s[i+1]){
				if(!x){
					x=s[i];
					i++;
				}else{
					y=s[i];
					break;
				}
			}
		}
//		__int64 max=x*y;
		printf("%lld\n",(ll)x*y);
	}
	return 0;
}

Measured memory_bytes: 896
#include<stdio.h>
#include<string.h>

#define N 100

int main(){

  int i,j,n,tmp,min,count=0,a[N];

  scanf("%d",&n);

  for(i=0;i<n;i++){

    scanf("%d",&a[i]);
  }
  for(i=0;i<n;i++){
    min=i;
    for(j=i;j<n;j++){
      if(a[j]<a[min]){
	min=j;
      }
    }
    if(a[i]!=a[min]){
    tmp=a[i];
    a[i]=a[min];
    a[min]=tmp;
    count++;
    }
  }
  printf("%d",a[0]);

  for(i=1;i<n;i++){

    printf(" %d",a[i]);
  }
  printf("\n%d\n",count);

  return 0;
}

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

F=[-1]*100
F[0]=F[1]=1

for i in range(2,100):
    F[i]=i*F[i-2]

#print(F)

n=int(input())

if n%2==1:
    print(0)
    sys.exit()
else:
    ANS=0
    for i in range(1,100):

        ANS+=n//(2*(5**i))


    print(ANS)

Measured memory_bytes: 3064
using System;
using System.Collections.Generic;
using System.IO;

namespace ABC134
{
    class Program5
    {
        static void Main()
        {
            //greedy
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
            var N = int.Parse(Console.ReadLine());
            var list = new List<int>();
            for (int i = 0; i < N; i++)
            {
                var num = int.Parse(Console.ReadLine());
                var index = list.UpperBound(num);
                if (index >= list.Count)
                    list.Add(num);
                else
                    list[index] = num;
            }

            Console.WriteLine(list.Count);
            Console.Out.Flush();
        }
    }

    public static partial class BinarySearch
    {
        static int binarySearch<T>(IList<T> a, T v, IComparer<T> cmp, bool islb)
        {
            var l = 0;
            var r = a.Count - 1;
            while (l <= r)
            {
                var m = (l + r) / 2;
                var res = -cmp.Compare(a[m], v); // reverse
                if (res < 0 || (res == 0 && !islb)) l = m + 1;
                else r = m - 1;
            }
            return l;
        }
        public static int LowerBound<T>(this IList<T> a, T v, Comparison<T> f) { return binarySearch(a, v, Comparer<T>.Create(f), true); }
        public static int LowerBound<T>(this IList<T> a, T v) { return binarySearch(a, v, Comparer<T>.Default, true); }
        public static int UpperBound<T>(this IList<T> a, T v, Comparison<T> cmp) { return binarySearch(a, v, Comparer<T>.Create(cmp), false); }
        public static int UpperBound<T>(this IList<T> a, T v) { return binarySearch(a, v, Comparer<T>.Default, false); }

    }
}


Measured memory_bytes: 18272
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<numeric>
#include<map>
#include<unordered_map>
#include<iostream>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;
typedef pair<ll, ll> mp;


/*
大きい方から貪欲？？？
*/

ll n,a[200010],ans=0,cnt=1,b31=1;
vector<int> b,c;
map<int,int> m;

int main(void){
	rep(i,31)b31*=2;
	cin>>n;
	rep(i,n)cin>>a[i];
	sort(a,a+n);
	reg(i,1,n-1){
		if(a[i]==a[i-1]){
			cnt++;
		}else{
			b.push_back(a[i-1]);
			c.push_back(cnt);
			cnt=1;
		}
	}
	b.push_back(a[n-1]);
	c.push_back(cnt);
	rep(i,b.size()){
		m[b[i]]=i;
	}
	irep(i,b.size()){
		ll obj = b31;
		while(obj>1){
			if(m.count(obj-b[i])){
				if(m[obj-b[i]]==i){
					if(c[i]>=2){
						ans+=c[i]/2;
						c[i]%=2;
					}
				}else if(c[i]>0 && c[m[obj-b[i]]]>0){
					int tmp = min(c[i],c[m[obj-b[i]]]);
					ans+=tmp;
					c[i]-=tmp;
					c[m[obj-b[i]]]-=tmp;
				}
			}
			obj/=2;
		}
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 12780
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100005];
int dp[2][100005];
void dfs(int u, int p, int idx, int d){
    dp[idx][u] = d;
    for(int i=0;i<g[u].size();i++){
        int v = g[u][i];
        if(v == p) continue;
        dfs(v, u, idx, d+1);
    }
}
int main(){
    int n, u, v, a, b;
    scanf("%d%d%d", &n, &u, &v);
    for(int i=1;i<=n-1;i++){
        scanf("%d%d", &a, &b);
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(u, 0, 0, 0);
    dfs(v, 0, 1, 0);
    int d = dp[0][v]%2;
    int ans = 0;
    for(int i=1;i<=n;i++){
        if(dp[0][i] < dp[1][i]) ans = max(ans, dp[1][i]-1);
    }
    printf("%d\n", ans);
}

Measured memory_bytes: 12928
#pragma warning(disable:4996)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <float.h>

#pragma region 定義
#define i_cin(X) scanf("%d", &X)
#define i_cin2(X,Y) scanf("%d %d",&X,&Y)
#define i_cin4(X,Y,Z,W) scanf("%d %d %d %d",&X,&Y,&Z,&W)
#define i_cin3(X,Y,Z) scanf("%d %d %d",&X,&Y,&Z)
#define l_cin(X) scanf("%ld",&X)
#define f_cin(X) scanf("%f",&X)
#define l_cin2(X,Y) scanf("%ld %ld",&X,&Y)
#define s_cin(X) scanf("%s",&X)
#define ll_cin(X) scanf("%lld", &X)

#define  i_cout(X) printf("%d\n",X)
#define  f_cout(X) printf("%g\n",X)
#define  i_cout2(X,Y) printf("%d %d\n",X,Y)
#define  l_cout(X) printf("%ld\n",X)
#define  s_cout(X) printf("%s\n",X)
#define  ll_cout(X) printf("%lld\n",X)

typedef long long  ll;
#define rept(x, s, n) for (int x = s; x < n; x++)

#define S_RTN(S)  s_cout(S);return 0


#define _itoa(A,N) sprintf(A, "%d", N);

int i_cins(int n, int* A);
int l_cins2(int n, long* A, long* B);
int s_dsort(const void* a, const void* b);
int s_asort(const void* a, const void* b);

int  _gcd(int a, int b);
int  _swp(int* a, int* b);
int _cknum(char* a, int n);
int _atoi(char* s, int len);


int s_asorts(const void* a, const void* b);

//昇順
int s_asorts(const void* a, const void* b) {
	return(strcmp((char*)a, (char*)b));
}
int s_dsort(const void* a, const void* b) {
	return(*(int*)b - *(int*)a);
}
int s_asort(const void* a, const void* b) {
	return(*(int*)a - *(int*)b);
}
int l_cins2(int n, long* a, long* b) {
	int i;
	rept(i, 0, n) {
		l_cin2(*(a + i), *(b + i));
	}
	return 0;
}
int i_cins(int n, int* a) {
	int i;
	for (i = 0; i < n; i++) {
		i_cin(*(a + i));
	}

	return 0;
}
int _gcd(int a, int b) {
	int r, tmp;
	if (a < b) { tmp = a; a = b; b = tmp; }
	r = a % b;
	while (r) {
		if (!r) break;
		a = b; b = r;
		r = a % b;
	}
	return(b);
}

long _max(long a, long b) {
	return a > b ? a : b;
}
long _min(long a, long b) {
	return a < b ? a : b;
}
int _swp(int* a, int* b)
{
	int tmp;
	tmp = *b; *b = *a; *a = tmp;
	return 0;
}

int _cknum(char* a, int n) {
	int i;
	char t = '0';

	for (i = 0; i < n; i++) {
		if (a[i] < '0' || a[i]>'9') return 1;
	}
	return 0;
}


int _atoi(char* s, int len) {
	char tmp[20];
	memcpy(tmp, s, len);
	tmp[len] = 0x00;
	return (atoi(tmp));
}
#pragma endregion


int main(void)
{
	ll  n, k, r;

	ll_cin(n);
	ll_cin(k);

	r = n % k;
	if (r < k - r) ll_cout(r);
	    else       ll_cout(k - r);

	return 0;
}



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

using namespace std;


#define M_DEBUG if(0)


int main() {
	int n = 0;
	cin >> n;
	assert(n > 0 && n <= 100000);

	map<int, int> num2idx;
	for (int i = 1; i <= n; i++) {
		int a_i;
		cin >> a_i;
		assert(a_i > 0 && a_i <= n);
		M_DEBUG { cout << "i: " << i << ", a_i: " << a_i << endl; }
		num2idx.insert(make_pair(a_i, i));
	}

	for (auto a : num2idx) {
		cout << a.second << " ";
	}
	cout << endl;

	return 0;
}


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

using namespace std;

//#define DEBUG_MODE
#ifdef DEBUG_MODE
	#define _(QQQ) QQQ;
#else
	#define _(QQQ) ;
#endif
#define DBG cerr << '!' << endl;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define MOD 1000000007

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

ll n;
ll a[111111];
vector<vector<ll> > v(111111);

ll dfs(ll now,ll prev){
	_(cout << "dfs " << now << ' ' << prev << endl;)
	if(v[now].size() == 1){
		return a[now];
	}
	ll sum = 0;
	vector<ll> seica;
	REP(i,v[now].size()){
		if(v[now][i] == prev)continue;
		seica.PB(dfs(v[now][i],now));
	}
	REP(i,seica.size()){
		if(seica[i] == -1)return -1;
		sum += seica[i];
	}
	_(cout << "A SUM " << a[now] << ' ' << sum << endl;)
	if(seica.size() == 1){
		if(a[now] != sum){
			return -1;
		}
		return a[now];
	}
	ll ma = 0;
	REP(i,seica.size())ma = max(ma,seica[i]);
	ll x = sum - a[now];
	if(x < 0 || x > sum - ma || x > a[now] || (a[now] == 0 && sum > 0))return -1;
	return a[now] - x;
}
	

bool check(int num){
	_(cout << "num is " << num << endl;)
	ll sum = 0;
	vector<ll> seica;
	REP(i,v[num].size()){
		seica.PB(dfs(v[num][i],num));
		_(cout << seica[i] << endl;)
		if(seica[i] == -1)return false;
		sum += seica[i];
	}
	_(
	cout << "IN CHECK SUM IS " << sum << " A  " << a[num] << endl;
	SHOW1d(seica,seica.size());
	)
	if(sum % 2 != 0){
		return false;
	}
	REP(i,seica.size()){
		if(sum / 2 < seica[i]){
			return false;
		}
	}
	return (sum / 2) == a[num];
}

int main()
{
	cin >> n;
	REP(i,n)cin >> a[i];
	REP(i,n-1){
		ll x,y;cin >> x >> y;
		x--;y--;
		v[x].PB(y);
		v[y].PB(x);
	}
	if(n == 2){
		if(a[0] == a[1]){
			cout << "YES" << endl;
		}
		else{
			cout << "NO" << endl;
		}
		return 0;
	}
	
	REP(i,n){
		if(v[i].size() > 1){
			if(check(i)){
				cout << "YES" << endl;
			}
			else{
				cout << "NO" << endl;
			}
			return 0;
		}
	}
	
	return 0;
}

Measured memory_bytes: 14720
use std::io::Read;
use std::collections::HashMap;

fn main() {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();

    let answer = solve(&buf);

    println!("{}", answer);
}

fn solve(input: &str) -> String {
    let mut iterator = input.split_whitespace();

    let n: usize = iterator.next().unwrap().parse().unwrap();

    let factors: HashMap<usize, usize> = prime_factorization(n);

    let mut ans: usize = 0;
    for (_, v) in factors {
        ans += fact(v);
    }
    return ans.to_string();
}

fn fact(mut n: usize) -> usize {
    let mut i = 1;
    loop {
        if n >= i {
            n -= i;
            i += 1;
        } else {
            return i - 1;
        }
    }
}

fn prime_factorization(mut n: usize) -> HashMap<usize, usize> {
    let mut result: HashMap<_, _> = HashMap::new();

    for d in 2.. {
        if d * d > n {
            break;
        }
        while n % d == 0 {
            *result.entry(d).or_insert(0) += 1;
            n /= d;
        }
    }

    if n != 1 {
        result.insert(n, 1);
    }

    return result;
}


Measured memory_bytes: 2176
#include <cstdio>
#include <algorithm>
using namespace std;
#define ll long long
int main(){
	int n;
	scanf("%d", &n);
	ll a[n], c[n + 1], s = 0;
	fill(c, c + n + 1, 0);
	for (int i = 0; i < n; i++){
		scanf("%lld", &a[i]);
		c[a[i]]++;
	}
	for (int i = 1; i <= n; i++){
		s += c[i] * (c[i] - 1) / 2;
	}
	for (int i = 0; i < n; i++){
		printf("%lld\n", s - c[a[i]] + 1);
	}
}

Measured memory_bytes: 5632
process.stdin.resume();
    process.stdin.setEncoding('ascii');
     
    var input_stdin = "";
    var input_stdin_array = "";
    var input_currentline = 0;
     
    process.stdin.on('data', function (data) {
        input_stdin += data;
    });
     
    process.on('SIGINT', function(){
        input_stdin_array = input_stdin.split("\n");
        main();
        process.exit();
    });
     
    process.stdin.on('end', function () {
        input_stdin_array = input_stdin.split("\n");
        main();    
    });
     
    function readLine() {
        return input_stdin_array[input_currentline++];
    }
     
    /////////////// ignore above this line ////////////////////
     
    function main() {
    	var N = parseInt(readLine());
    	var A = [], C = [];
    	var s = readLine().split(" ");
    	for (var i=0; i < N; i++){
    		A.push(parseInt(s[i]));
    		C.push(2);
    	}
    	C[0]=1;
    	for (var i=0; i < N; i++)
    		C[A[i]]--;
    	
    	//check
    	var ini;
    	if (N % 2 == 1){
    		ini = 0;
    	} else {
    		ini = 1;
    	}
    	
    	for (var i=ini; i < N; i += 2){
    		if (C[i] != 0){
    			console.log("0");
    			return;
    		}
    	}
    	
    	//calculates answer
    	var ans = 1, mod = 1e9+7;
    	for (var i=1; i < N; i += 2){
    		ans *= 2;
		ans %= mod;
    	}
    	console.log(ans.toString());
    }

Measured memory_bytes: 28340
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<P,long double> Pi;
typedef pair <long double,int> Pd;
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
const ll mod=1000000007;
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
ll gcd(ll a,ll b) {return b ? gcd(b,a%b):a;}
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 (a>b) { a=b; return 1; } return 0; }
ll pl=12000;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
vector<int> p(n),r(n);
vector<ll> a(n),b(n);
for(int i=0;i<n;i++){cin>>p[i];r[p[i]-1]=i+1;}
for(int i=0;i<n;i++){
    a[i]=30000*(i+1);
    b[i]=30000*(n-i-1)+r[i];
}
for(int i=0;i<n;i++)cout<<a[i]<<" ";
cout<<endl;
for(int i=0;i<n;i++)cout<<b[i]<<" ";
cout<<endl;
}



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


typedef unsigned long long ll;
ll t,i,j,n,k,x,y,a,b;
string s,s1,s2;
float f;
const ll M=1e9+7;


ll mod(ll x)
{
    return((x%M+M)%M);
}
ll add(ll a,ll b)
{
    return mod(mod(a)+mod(b));
}
ll mul(ll a,ll b)
{
    return mod(mod(a)*mod(b));
}

int main()
{   
    cin>>s;
    if(s[s.size()-1]=='s')
    cout<<s<<"es";
    else
    cout<<s<<"s";
    
    
    
    return 0;
}


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

#define Fast ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0)
#define FO freopen("in.txt", "r", stdin)
#define FC freopen("out.txt", "w", stdout)
#define aise cout<<"aise"<<endl
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mp make_pair
#define ll long long
#define all(x) x.begin(), x.end()
#define mset(v, a) memset(v, a, sizeof(v))
#define pll pair< ll, ll >
#define pdd pair< double, double >
#define ff first
#define ss second
#define pi acos(-1.0)
#define mxN 2010
#define inf 1e14
#define MOD 1000000007

int main()

{

    ll n, s, ans = 0;
    cin >> n;
    s = n;
    while(1)
    {

        if(s==0) break;
        ll x = s%10;
        ans+=x;
        s/=10;
    }
    if(n%ans) cout << "No";
    else cout << "Yes";
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,k;
	long long ans=0;
	cin>>n>>k;
	for(int a=1; a<=min(k,n); a++){
		if((2*a)%k==0){
			long long ap = n/k;
			if(n-(n/k)*k >= a)
				ap++;
			long long an = n/k;
			if(n-(n/k)*k >= k-a)
				an++;
			if(a%k == (k-a)%k)
				ans += ap*ap*ap;
			else
				ans += 3*ap*an*an;
		}
	}
	cout<<ans<<endl;
}

Measured memory_bytes: 256
# include "bits/stdc++.h"
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu)         FOR(i,0,upupu)

struct INIT{
    INIT(){
        std::ios::sync_with_stdio(false);
        std::cin.tie(0);
        cout << fixed << setprecision(13);
    }
}INIT;

int a, b;

int main(){
    cin >> a >> b;
    cout << 6 - a -b << endl;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
 
#define INF 1000000000
 
#define min(a,b) (((a)<(b))?(a):(b))
#define max(a,b) (((a)>(b))?(a):(b))

int main() {
	long N;
	char S[1001];
	long i, j;
	int len;
	int f[4] = {0};
	
	scanf("%s", S);
	len = strlen(S);
	
	for(i=0;i<len;i++) {
		if(S[i] == 'N') f[0] = 1;
		if(S[i] == 'W') f[1] = 1;
		if(S[i] == 'S') f[2] = 1;
		if(S[i] == 'E') f[3] = 1;
	}
	
	if(f[0] - f[2] == 0 && f[1] - f[3] == 0) {
		printf("Yes\n");
	} else {
		printf("No\n");
	}
	
	return 0;
	
}

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

class LCA{
private:
	vector<vector<int>> v;
	vector<vector<int>> parent;
	vector<int> depth;
	int h = 0;
	void dfs(int n,int m,int d){
		parent[0][n] = m;
		depth[n] = d;
		for(auto x:v[n]){
			if(x!=m) dfs(x,n,d+1);
		}
	}
public:
	LCA(int N,int root,vector<vector<int>>& tree){
		while((1<<h)<=N) h++;
		v = tree;
		parent = vector<vector<int>>(h,vector<int>(N,0));
		depth = vector<int>(N,0);
		dfs(root,-1,0);
		for(int j=0;j+1<h;j++){
			for(int i=0;i<N;i++){
				if(parent[j][i]<0) parent[j+1][i] = -1;
				else parent[j+1][i] = parent[j][parent[j][i]];
			}
		}
	}
	int lca(int n,int m){
		if(depth[n]>depth[m]) swap(n,m);
		for(int j=0;j<h;j++){
			if((depth[m]-depth[n]) >> j&1) m = parent[j][m];
		}
		if(n==m) return n;
		for(int j=h-1;j>=0;j--){
			if(parent[j][n]!=parent[j][m]){
				n = parent[j][n];
				m = parent[j][m];
			}
		}
		return parent[0][n];
	}
	int dep(int n){return depth[n];}
};
int N,Q,x,y,a,b;

int main(){
	cin >> N;
	vector<vector<int>> tree(N+1);
	for(int i=0;i<N;i++){
		int k;
		cin >> k;
		for(int j=0;j<k;j++){
			int c;
			cin >> c;
			tree[i].push_back(c);
			tree[c].push_back(i);
		}
	}
	LCA lca(N,0,tree);
	cin >> Q;
	for(int i=0;i<Q;i++){
		cin >> a >> b;
		int p = lca.lca(a,b);
		cout << p << endl;
	}
}


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

int main()
{
    ll n;
    cin >> n;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    bool buy = false;
    ll money = 1000;
    ll kabu = 0;
    repl(i, 1, n) {
        if(a[i] > a[i - 1]) {
            buy = true;
            kabu += money / a[i - 1];
            money %= a[i - 1];
        }
        else if(a[i] < a[i - 1]){
            buy = false;
            money += kabu * a[i - 1];
            kabu = 0;
        }
    }
    if(buy) money += kabu * a[n - 1];
    cout << money << endl;
    return 0;
}


Measured memory_bytes: 3644
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <queue>
#include <stack>
#include <cstdlib>
#include <map>
#include <iomanip>
#include <set>
#include <stdio.h>
#include <ctype.h>
#include <random>
#include <string.h>
#include <cmath>
#include <unordered_map>
#include <cstdio>
using namespace std;
#define pie 3.141592653589793238462643383279
#define mod 1000000007
#define int long long
#define P pair<int,int>
#define all(vec) vec.begin(),vec.end()

int gcd(int x, int y) {
	if (y == 0)return x;
	return gcd(y, x%y);
}
int lcm(int x, int y) {
	return x / gcd(x, y)*y;
}
int kai(int x, int y) {
	int res = 1;
	for (int i = x - y + 1; i <= x; i++) {
		res *= i; res %= mod;
	}
	return res;
}
int mod_pow(int x, int y, int m) {
	int res = 1;
	while (y > 0) {
		if (y & 1) {
			res = res * x % m;
		}
		x = x * x % m;
		y >>= 1;
	}
	return res;
}

int comb(int x, int y) {
	if (y > x)return 0;
	return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}

int n;
int ans;
vector<int> vec;

signed main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		int a, b; cin >> a >> b;
		ans -= b;
		vec.push_back(a + b);
	}
	sort(all(vec));
	reverse(all(vec));
	for (int i = 0; i < n; i += 2)ans += vec[i];
	cout << ans << endl;
}

Measured memory_bytes: 1400
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vl = vector<ll>;
using vll = vector<vector<ll>>;
using pii = pair<int,int>;
#define FOR(i,a,b) for(ll i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(obj) (obj).begin(), (obj).end()
#define pb push_back
#define double long double
const double pi = acos(-1);
const double EPS = 1e-10;

double crosss(pair<double,double> c,pair<double,double> d){
  return c.first * d.second - d.first * c.second;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<pair<double,double>> a(4);
  int n;
  pair<double,double> lineab, linecd;
  cin >> n;
  REP(i,n){
    REP(j,4){
      cin >> a[j].first >> a[j].second;
    }
    lineab.first = a[1].first - a[0].first;
    linecd.first = a[3].first - a[2].first;
    lineab.second = a[1].second - a[0].second;
    linecd.second = a[3].second - a[2].second;
    if (fabs(crosss(lineab,linecd)) == 0){
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  }
  
  
  return 0;
}



Measured memory_bytes: 3116
n = int(input())
p_list = list(map(int, input().split()))
cnt = 0
for i in range(1, n-1):
    if((p_list[i-1] > p_list[i] & p_list[i] > p_list[i+1]) | (p_list[i-1] < p_list[i] & p_list[i] < p_list[i+1])):
        cnt += 1
print(cnt)

Measured memory_bytes: 3060
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		int y = sc.nextInt();
		int big = Math.max(x, y);
		int small = Math.min(x, y);
		
		System.out.println(gcd(small, big % small));
	}
		
	public static int gcd(int a, int b) {
		if(b == 0) {
			return a;
		} else {
			return gcd(b, a % b);
		}
	}
}



Measured memory_bytes: 26236
#include <cassert>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
using namespace std;
typedef long long ll;

int vs[2][110];

int main(void) {
  int n;
  scanf("%d", &n);
  REP(i, 2) {
    REP(j, n) {
      scanf("%d", &vs[i][j]);
    }
  }
  int res = 0;
  REP(x, n) {
    int cur = 0;
    REP(j, x+1) {
      cur += vs[0][j];
    }
    for(int j = x; j < n; ++j) {
      cur += vs[1][j];
    }
    res = max(cur, res);
  }
  printf("%d\n", res);
     
  return 0;
}


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

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

struct UnionFind {
  // 子は親のID,親は-サイズ
  vector<int> d;
  UnionFind(int n) : d(n, -1) {}
  int root(int x) {
    if (d[x] < 0) return x;
    return d[x] = root(d[x]);
  }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return false;
    if (d[x] > d[y]) swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -d[root(x)]; }
};

int main() {
  int n, m, ans = 0, x, y, z, cnt = 0, t = 0;
  cin >> n >> m;

  UnionFind uf(n);
  unordered_set<int> st;

  rep(i, m) {
    cin >> x >> y >> z;
    x--, y--;
    z %= 2;
    uf.unite(x, y);
  }
  rep(i, n) if (uf.root(i) >= 0) st.insert(uf.root(i));

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

Measured memory_bytes: 7708
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf=1e15;
const int N=2e5+50;
ll n,m;
bool check(ll x)
{
	//放x个,至少要 
	ll p=x*2,c=m-p;
	if(m>=p)
	{
		if(n+c/2>=x)
		return true;
	}
	return false;
}
int main()
{
	while(cin>>n>>m)
	{
		ll l=0,r=1e12;
		ll ans;
		while(l<=r)
		{
			ll mid=(l+r)>>1;
			if(check(mid))
			{
				ans=mid;
				l=mid+1;
			}
			else
			r=mid-1;
		}
		cout<<ans<<endl;
	}
	return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
int main()
{
    int n,m,i,a,b;
    scanf("%d%d",&n,&m);
    int arr[n];
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    a=n/(m-1);
    b=n%(m-1);
    if(m-1==1)
        a--;
    else if(b>1)
        a++;

    printf("%d",a);
    return 0;
}


Measured memory_bytes: 512
def common_multiple(x, y):
    for i in range(2, min(x, y)+1):
        mod_x = x % i
        mod_y = y % i
        if (mod_x == 0 and mod_y == 0):
            return common_multiple(x // i, y // i) * i
        if (mod_x == 0):
            return common_multiple(x // i, y) * i
        if (mod_y == 0):
            return common_multiple(x, y // i) * i
    return  x * y

a = input().split(" ")
A,B,C,D=int(a[0]) - 1,int(a[1]),int(a[2]),int(a[3])

M = common_multiple(C,D)
B0 = B // C + B // D - B // M
A0 = A // C + A // D - A // M
print(B - A - B0 + A0)

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

using namespace std;

int main()
{
	string num;
	int sum_num;
	
	while(true)
	{
		cin >> num;
		
		if(num == "0")
			break;
		
		sum_num = 0;
		
		for(int i = 0; i < num.size(); i++)
			sum_num += (num[i] - '0');
		
		cout << sum_num << "\n";
	}
	return 0;
}


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

typedef int Capacity;
struct Edge {
    int src, dst;
    Capacity cap;
    Edge(int src_, int dst_, Capacity cap_) :
        src(src_), dst(dst_), cap(cap_) { }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

struct EdmondsKerp {
    int n;
    vector<int> prev, dist;
    vector<vector<Capacity>> cap, flow;
    vector<vector<int>> g;
    Capacity inf;
    EdmondsKerp(int n)
        : n(n), cap(n, vector<Capacity>(n)), flow(n, vector<Capacity>(n)),
          g(n, vector<int>()), inf(numeric_limits<Capacity>::max()/8){}
    EdmondsKerp(const Graph &graph){
        *this = EdmondsKerp(graph.size());
        rep(i,n) for(auto &e : graph[i]) addEdge(e.src, e.dst, e.cap);
    }
    void addEdge(int u, int v, Capacity c){
        cap[u][v] += c; cap[v][u] += c; flow[v][u] += c;
        g[u].push_back(v); g[v].push_back(u);
    }
    Capacity solve(int s, int t){
        Capacity res = 0, aug = 1;
        while(aug > 0){
            prev.assign(n, -1); dist.assign(n, inf); dist[s] = 0;
            res += (aug = augment(s,t));
        }
        return res;
    }
    Capacity augment(int s, int t){
        queue<pair<int,Capacity>> q;
        q.emplace(s,inf);
        Capacity aug = 0;
        while(q.size()){
            int v; Capacity f;
            tie(v,f) = q.front(); q.pop();
            if(v == t){ aug = f; break; }
            for(const int& d : g[v]){
                if(dist[d] <= dist[v] + 1 || cap[v][d] - flow[v][d] == 0) continue;
                dist[d] = dist[v] + 1; prev[d] = v;
                q.emplace(d, min(f, cap[v][d] - flow[v][d]));
            }
        }
        if(aug == 0) return 0;
        int c = t;
        while(c != s){
            int p = prev[c];
            flow[p][c] += aug; flow[c][p] -= aug;
            c = p;
        }
        return aug;
    }
};

int main(){
    int N,M;
    cin >> N >> M;
    Graph g(N);
    rep(i,M){
        int a,b,c;
        cin >> a >> b >> c;
        g[a].emplace_back(a,b,c);
    }
    EdmondsKerp ek(g);
    cout << ek.solve(0,N-1) << endl;
}

Measured memory_bytes: 1352
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define boost() ios_base :: sync_with_stdio(0); cin.tie(); cout.tie(); cout << fixed; cout << setprecision(15); srand(time(NULL))
#define endl '\n'
typedef long long ll;
const int _N = 1e5 + 11;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;

ll dist(pair <ll, ll> aa, pair <ll, ll> bb) {
	return abs(aa.first - bb.first) + abs(aa.second - bb.second);
}

int n, m;
pair <ll, ll> a[55], b[55];

int main() {
	boost();
//	freopen("in", "r", stdin);
	cin >> n >> m;
	for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
	for (int i = 0; i < m; i++) cin >> b[i].first >> b[i].second;
	for (int i = 0; i < n; i++) {
		ll d = LONG_LONG_MAX, res = -1;
		for (int j = 0; j < m; j++) {
			if (d > dist(a[i], b[j])) {
				d = dist(a[i], b[j]);
				res = j + 1;
			}
		}
		cout << res << endl;
	}
	return 0;
}


Measured memory_bytes: 256
import bisect
n = int(input())
a = sorted(list(map(int, input().split())))
x = a[-1]
N = a[-1]//2

a = a[:-1]
y = bisect.bisect_left(a,N)
if len(a) <= y: ans = a[y-1]
else:
    ans = a[y]
    if 0 < y and abs(N-ans) > abs(N-a[y-1]): ans = a[y-1]
    if y < len(a)-1 and abs(N-ans) > abs(N-a[y+1]): ans = a[y+1]
print(x,ans)

Measured memory_bytes: 20036
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
	int a,c;
	cin>>a;
	vector<int> b;
	for(int i=0;i<a;i++){
		cin>>c;
		b.push_back(c);
	}
	sort(b.begin(),b.end());
//	cout<<b[b.size()/2]<<" "<<b[b.size()/2+1]<<endl;
	if(b[b.size()/2-1]==b[b.size()/2]){
		cout<<0<<endl;
		return 0;
	}
	cout<<b[b.size()/2]-b[b.size()/2-1]<<endl;
	return 0;
}

Measured memory_bytes: 892
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REP2(i, n) for (int i = 1; i <= (int)(n); i++)
#define FOR(i, m, n) for (int i = m; i<n; i++)
#define SORT(x) sort(x.begin(), x.end())
#define REVE(x) reverse(x.begin(), x.end())
#define ALL(x) (x).begin(), (x).end()
#define SUM(x) accumulate(ALL(x));
#define vint(v,n) vector<int> v(n); REP(i,n) scanf("%lld", &v[i]);
struct initon
{
    initon()
    {
        cin.tie(0);
        ios::sync_with_stdio(false);
    };
};
int k=0,n=0, m=0, l=0,a=0, b=0, c=0, d=0,e=0,ans=0,ans2=0,x=0,y=0;
string s,t,S;
signed main() {
    cin >> n >> k;
    vint(A,n);
    SORT(A);
    FOR(i, n-k, n)
    ans += A[i];
    cout << ans<<endl;
}

Measured memory_bytes: 3692
#include<stdio.h>

typedef long long ll;

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

int main(void){
    ll A, B, C, D;
    ll cnt;
    ll i;
    ll g;
    ll Ac, Ad, Bc, Bd, Al, Bl;

    scanf("%lld %lld %lld %lld", &A, &B, &C, &D);

    g = gcd(C, D);
    ll lcm = C*D/g;

    Ac = A/C;
    Ad = A/D;
    Bc = B/C;
    Bd = B/D;
    Al = A/lcm;
    Bl = B/lcm;

    cnt = (Bc - Ac) + (Bd - Ad) -(Bl - Al);

    if(A%C == 0){
        cnt++;
    }
    if(A%D == 0){
        cnt++;
    }
    if(A%lcm == 0){
        cnt--;
    }

    cnt = (B-A+1)-cnt;

    printf("%lld", cnt);
    return 0;
}

Measured memory_bytes: 128
#include<stdio.h>
int main(void)
{
	int a,s,z,x,h,d,f,g,i,j;
	scanf("%d",&a);
	for(i=1;i<=a;i++){
		scanf("%d %d %d",&s,&z,&x);
		d=x*x;
		h=s*s+z*z;
		if(s<x&&x<z||x<s&&s<z){
			d=z*z;
			h=s*s+x*x;
		}
		if(z<x&&x<s||x<z&&z<s){
			d=s*s;
			h=z*z+x*x;
		}
		if(d==h){
			printf("YES\n");
		}
		if(d!=h){
			printf("NO\n");
		}
	}
	return 0;
}

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

#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define ALL(x) x.begin(),x.end()

using namespace std;

int main() {
  int n,m;
  while( cin >> n >> m, n|m ) {
    vector<int> a(n);
    rep(i,n) cin >> a[i];
    sort(ALL(a));
    int maxi = -1;
    rep(i,n) {
      int rem = m - a[i];
      int ptr = upper_bound(ALL(a),rem) - a.begin();
      --ptr;
      if( ptr == i ) --ptr;
      if( ( 0 <= ptr && ptr < n ) && a[i] + a[ptr] <= m ) maxi = max(maxi,a[i]+a[ptr]);
    }
    ( maxi == -1 ) ? puts("NONE") : printf("%d\n",maxi);
  }
  return 0;
}
/*

#include<iostream>
#include<vector>
#include<algorithm>

#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define ALL(x) x.begin(),x.end()

using namespace std;

int main() {
  int n,m;
  while( cin >> n >> m, n|m ) {
    vector<int> a(n);
    rep(i,n) cin >> a[i];
    int maxi = -1;
    rep(i,n) rep(j,n) if( i != j &&  ) maxi = max(maxi,);
    if( maxi == -1 ) puts("NONE");
    else             cout << maxi << endl;
    //( maxi == -1 ) ? puts("NONE") : printf("%d\n",maxi);
  }
  return 0;
}
*/

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

using namespace std;

#define pb push_back
#define mp make_pair
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long LL;
typedef pair<int, int> PII;

int n, k, MOD;
int mul[301][301];
int v[301], nv[301];

int main() {
    scanf("%d%d%d", &n, &k, &MOD);
    forn(i, 301) {
        mul[0][i] = 1;
    }
    for (int i = 1; i < 301; ++i) {
        mul[i][i] = 1;
        for (int j = i + 1; j < 301; ++j) {
            mul[i][j] = 0;
            int p = 1;
            for (int k = j - 1; k >= i - 1; --k) {
                mul[i][j] = (mul[i][j] + (LL)mul[i - 1][k] * p) % MOD;
                p = (LL)p * (i + 1) % MOD;
            }
        }
    }
    v[0] = 1;
    forn(_, k) {
        memset(nv, 0, sizeof nv);
        forn(i, 301) forn(j, 301) {
            nv[j] = (nv[j] + (LL)v[i] * mul[i][j]) % MOD;
        }
        memcpy(v, nv, sizeof v);
    }
    cout << v[n] << endl;
    return 0;
}


Measured memory_bytes: 640
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#include <iomanip>
#include <complex>
#include <bitset>
#include <stack>
using namespace std;
 
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define repl(i,s,n) for(int i=s; i<=n; ++i)
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define ktya(x) sort(all(x))
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 1e18
#define INFLL 1000000000000000007LL
#define SIZE 200105
#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mind(a,b) (a>b?b:a)
#define maxd(a,b) (a>b?a:b)
#define PI (acos(-1))
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
typedef pair<int,ll> pill; typedef pair<ll,int> plli; 
typedef pair<double, int> pdi;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }
typedef complex<double> P;
//ll MOD = 1000000007;
ll MOD=998244353;
typedef ll Weight;
struct Edge {
	int src, dst;
	Weight weight;
};
bool operator < (const Edge &e, const Edge &f) {
	return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
	e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
int N,M;
vector<ll> a;
int main(){
	string str;
	ll K;
	cin>>str;
	cin>>K;
	char now=str[0];
	int n=str.size();
	str+=";";
	int cnt=0;
	rep(i,str.size()){
		if(str[i]==now)cnt++;
		else{
			now=str[i];
			a.pb(cnt);
			cnt=1;
		}
	}
	//rep(i,a.size())cout<<a[i]<<endl;
	ll ans=0;
	if(str[0]==str[n-1]){
		if(a.size()==1){
			ans=(ll)(a[0]*K)/2;
		}else{
			ll len=0;
			int m=a.size();
			for(int i=1;i<m-1;i++){
				len+=a[i]/2;
			}
			ans=len*K+((a[0]+a[m-1])/2)*(K-1)+a[0]/2+a[m-1]/2;
		}
		
	}else{
		rep(i,a.size()){
			ans+=(ll)(a[i]/2);
		}
		ans*=K;
	}
	cout<<ans<<endl;
}

Measured memory_bytes: 384