akhauriyash/Code-Regression huggingface dataset:
package main
import (
   "fmt"
   "os"
   "bufio"
   "strconv"
)
var sc = bufio.NewScanner(os.Stdin)

func main() {
  sc.Split(bufio.ScanWords)
  first := nextInt()
  second := nextInt()
  divInt := first / second
  divRemain := first % second
  divFloat := float64(first) / float64(second)
  fmt.Println(strconv.Itoa(divInt) +" " + strconv.Itoa(divRemain) + " " + strconv.FormatFloat(divFloat, 'f', 6, 64))
}       
    
  
func nextInt() (int) {
    sc.Scan()
    v, e := strconv.Atoi(sc.Text())
    if e != nil { panic(e) }
    return v
}


Measured memory_bytes: 1172
#include<iostream>
#include<string>
#include <bits/stdc++.h>
#include<vector>
#include <algorithm>
#include <functional>
using namespace std;

int main(){
  string s,t;
  cin >> s;
  cin >> t;
  t.pop_back();
  if(s == t){
    cout << "Yes" << endl;
  }else{
    cout << "No" << endl;
  }

}


Measured memory_bytes: 3628
temp = input().split(" ")
n = int(temp[0])
inp = input().split(" ")
sousa = [0 for a in range(n)]
nsousa = 0
tempn = 0
flag = 0
for a in range(n):
    inp[a] = int(inp[a])
    if inp[a] == a+1:
        sousa[a] = 1
# print(sousa)
for a in range(n-1):
    if(sousa[a] == 1 and sousa[a+1] == 1):
        nsousa+=1
        sousa[a] = 0
        sousa[a+1] = 0
# print(sousa)
nsousa+=sum(sousa)
print(nsousa)
 

Measured memory_bytes: 10808
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    A.reverse()
    ans = 0
    for i in range(0, N-1, 2):
        ans += A[i] - A[i+1]
    if N % 2 == 1:
        ans += A[-1]
    return ans

print(solve())


Measured memory_bytes: 3060
import java.util.Scanner;
import java.util.*;
public class Main {
	
	
	
	public static long KnapsackM(int weight[] , int value[] ,int mw,int n) {
		long dp[][] = new long[n+1][mw+1];
		
		for(int i=0;i<=n;i++) {
			for(int w=0;w<=mw;w++) {
				if(i==0 || w==0) {
					dp[i][w] = 0;
				}else if(weight[i-1] <= w) {
					dp[i][w] = Math.max(value[i-1] + dp[i-1][w-weight[i-1]],  dp[i-1][w]);
				}else {
					dp[i][w] = dp[i-1][w];
				}
			}
		}
		
		return dp[n][mw];
		
	}
	
	
	  
	
	
    public static void main(String[] arg) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int mw = s.nextInt();
        int weight[] = new int[n];
        int value[] = new int[n];
        for(int i=0;i<n;i++) {
        	int wi = s.nextInt();
        	int vi = s.nextInt();
        	weight[i] = wi;
        	value[i] = vi;
        }
        System.out.println(KnapsackM(weight,value,mw,n));
//        System.out.println(knapSack(mw,weight,value,n));
    }
}

Measured memory_bytes: 127060
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <numeric>
#include <map>

using namespace std;    
typedef long long ll;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const bool DEBAG = true;

int main(){

    int n;
    string s;
    cin >> n;
    cin >> s;

    n = 0;
    int ans = 0;
    for(int i = 0; i < s.size(); i++){
        if(s[i] == 'I') n++;
        else n--;
        ans = max(ans, n);
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
int pivot(char c[][11], int b, int e)
{
    for(int i = b+1; i <= e; i++){
        int k = strcmp(c[b], c[i]);
        if(k != 0){
            return (k > 0 ? b : i);
        }
    }
    
    return -1;
}
void st_swap(char c[], char d[])
{
    char dm[11];
    strcpy(dm, c);
    strcpy(c, d);
    strcpy(d, dm);
}
int partition(char c[][11], int b, int e, int p)
{
    char piv[11];
    strcpy(piv, c[p]);
    while(1){
        while(strcmp(piv, c[b]) >  0) b++;
        while(strcmp(piv, c[e]) <= 0) e--;
        if(e < b) break;
        st_swap(c[b], c[e]);
    }
    return b;
}
void st_sort(char c[][11], int b, int e)
{
    int p;
    if((p = pivot(c, b, e)) == -1) return;
    int t = partition(c, b, e, p);
    st_sort(c, b, t-1);
    st_sort(c, t, e)  ;
}
int main()
{
    int n;
    long long ans = 0;
    cin >> n;
    char c[100000][11];
    for(int i = 0; i < n; i++){
        scanf("%s", c[i]);
        sort(c[i], c[i] + 10);
    }
    st_sort(c, 0, n-1);
    long long i = 0, j = 0;
    while(i < n){
        if(strcmp(c[i], c[j]) == 0){
            i++;
        }
        else{
            ans += (i - j) * (i - j - 1) / 2;
            j = i;
        }
    }
    ans += (i - j) * (i - j - 1) / 2;
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 1280
using System;
using System.Text;
using System.Collections.Generic;
class Solve{
    public Solve(){}
    StringBuilder sb;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        string[] str = Console.ReadLine().Split(' ');
        int N = int.Parse(str[0]);
        long K = int.Parse(str[1]);
        string s = Console.ReadLine();
        bool f = false;
        bool c = false;
        int count = 0;
        for(int i=0;i<N;i++){
            if(!f){
                if(s[i] == 'A' ^ c){
                    if(K == 1){
                        K--;
                        sb.Append("B");
                        count++;
                    }
                    else{
                        K -= 2;
                        c = !c;
                    }
                    if(K == 0){
                        f = true;
                    }
                }
                else{
                    K--;
                    c = !c;
                    if(K == 0){
                        f = true;
                    }
                }
            }
            else{
                if(s[i] == 'A' ^ c){
                    sb.Append("A");
                }
                else{
                    sb.Append("B");
                }
                count++;
            }
        }
        if(K != 0){
            if(N % 2 == 0){
                c = false;
                for(int i=0;i<N;i++){
                    if(c){
                        sb.Append("A");
                    }
                    else{
                        sb.Append("B");
                    }
                    c = !c;
                }
            }
            else{
                if(K % 2 == 0){
                    sb.Append("A");
                }
                else{
                    sb.Append("B");
                }
                c = false;
                for(int i=0;i<N-1;i++){
                    if(c){
                        sb.Append("A");
                    }
                    else{
                        sb.Append("B");
                    }
                    c = !c;
                }
            }
        } 
        else{
            for(int i=count;i<N;i++){
                if(c){
                    sb.Append("A");
                }
                else{
                    sb.Append("B");
                }
                c = !c;
            }
        }
        sb.Append("\n");
    }
}

Measured memory_bytes: 13340
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);

		int N = sc.nextInt();
		int[] a = new int[N];

		for (int i = 0; i < N; i++) {
			a[i] = sc.nextInt();
		}

		int cnt = 0;
		int min = a[0];
		for (int i = 1; i < a.length; i++) {
			if (min <= a[i]) {
				cnt++;
				min = a[i];
			}
		}

		System.out.println(cnt + 1);
	}

}

Measured memory_bytes: 21972
#include<iostream>
#include<map>
using namespace std;int main(){for(int n,v,i,V;cin>>n,i=n;){map<int,int>m;for(;i--;++m[v]<m[V]?:V=v)cin>>v;m[V]*2>n?cout<<V<<endl:cout<<"NO COLOR\n";}}

Measured memory_bytes: 1940
from collections import Counter
n = int(input())
a = list(map(int, input().split()))

temp = 0
c = Counter()
c[0] = 1
for i in a:
  temp += i
  c[temp] += 1
  
ans = 0
for _, x in (+c).items():
  ans += x * (x - 1)

ans /= 2
print(int(ans))

Measured memory_bytes: 55548
#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<string>
#include<iostream>
#include<vector>
#include<cstdlib>
using namespace std;
 
template <class T> using V = vector<T>;
 
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
 
const long long INF = 1LL << 60;
 
using ll = long long;
using db = double;
using st = string;
using ch = char;
using bl = bool;
using vll = V<ll>;
using vpll =V<pair<ll,ll>>;
using vst = V<st>;
using vdb = V<db>;
using vch = V<ch>;
using vbl = V<bl>;
 
 
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define rFOR(i,a,b) for(ll i=(a);i>(b);i--)
#define oFOR(i,a,b) for(ll i=(a);i<(b);i+=2)
#define bgn begin()
#define en end()
#define SORT(a) sort((a).bgn,(a).en)
#define REV(a) reverse((a).bgn,(a).en)
#define M(a,b) max(a,b)
#define rM(a,b) min(a,b)
#define fi first
#define se second
#define sz size()
#define gcd(a,b) __gcd(a,b)
#define co(a) cout<<a<<endl;
#define ci(a) cin>>a;
 

vll dp(100100);

ll sum(ll n) {
  ll m=0;
  FOR(i,0,20){
    m+=n%10;
    n/=10;
    if(n==0){
      break;
    }
  }
  return m;
}
 
 
const int MAX = 510000;
const int MOD = 1000000007;
 
long long fac[MAX], finv[MAX], inv[MAX];
 
void Comuse() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
 
 
#define comuse Comuse()
 
ll combi(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
 
 
 
 
ll lcm(ll a,ll b){
  ll n;
  n=a/gcd(a,b)*b;
  return n;
}
 
 
/****************************************\
| Thank you for viewing my code:)        |
| Written by RedSpica a.k.a. RanseMirage |
| Twitter:@asakaakasaka                  | 
\****************************************/
 
 
signed main() {
  ll n;	
  ci(n);
  co(n*n*n)
}

Measured memory_bytes: 1024
#include <iostream>
#include <cstdio>
#define ll unsigned long long
using namespace std;
ll a[1000006];
int read(){
	int cnt = 0;
	char ch = getchar();
	while (ch < '0' || '9' < ch)
		ch = getchar();
	while ('0' <= ch && ch <= '9'){
		cnt = cnt * 10 + ch - '0';
		ch = getchar();
	}
	return cnt;
}
int main(){
	int n = read(), x = read();
	for (int i = 1; i <= n; ++ i)
		a[i] = read();
	for (int i = n; i; -- i)
		a[i] += a[i + 1];
	ll ans = -1;
	for (int k = 1; k <= n; ++ k){
		ll sum = 1LL * (k + n) * x + 5 * a[max(1, n - 2 * k + 1)];
		for (int i = max(1, n - 2 * k + 1) - 1, j = 7; i >= 1; i -= k, j += 2)
			sum += (a[max(1, i - k + 1)] - a[i + 1]) * j;
		if (ans == -1 || ans > sum)
			ans = sum;
	}
	cout << ans << endl;
}


Measured memory_bytes: 1792
S=list(input())
K=int(input())
if K%2==0:
    A=S.copy()
    S=S+S
    ans=0
    x=0
    for i in range(len(S)-1):
        if S[i]==S[i+1]:
            S[i+1]=":"
            ans+=1
    if S[0]==S[-1] and K>3:
        x=(1)*(K//2-1)
    print((K//2)*ans+x)
else:
    ans=0
    A=S.copy()
    S=S+S
    x=0
    for i in range(len(S)-1):
        if S[i]==S[i+1]:
            S[i+1]=":"
            ans+=1
    if S[i]!=":" and K!=1:
        A=list(S[-1])+A
    for i in range(len(A)-1):
        if A[i]==A[i+1]:
            A[i+1]=":"
            x+=1
    print((K//2)*ans+x)

Measured memory_bytes: 3064
#include <stdio.h>
#define W 0
#define G 1
#define B 2

int M[100][100],n,d[100],f[100],c[100],tt;

void dfs_visit(int u)
{
  int i;
  c[u]=G;
  d[u]= ++tt;
  for(i=0;i<n;i++)
    {
      if(c[i]==W&&M[u][i]==1)
	dfs_visit(i);
    }
  f[u]=++tt;
  c[u]=B;
}

void dfs(void)
{
  int u;

  for(u=0;u<n;u++)
    {
      c[u]=W;
    }

  tt=0;
  for(u=0;u<n;u++)
    {
      if(c[u]==W)
	dfs_visit(u);
    }

  for(u=0;u<n;u++)
    {
      printf("%d %d %d\n",u+1,d[u],f[u]);
    }
}

int main()
{
  int i,j;
  scanf("%d",&n);

  for(i=0;i<n;i++)
    {
      for(j=0;j<n;j++)
	{
	  M[i][j]=0;
	}
    }

  int k,u,v;
  
  for(i=0;i<n;i++)
    {
      scanf("%d",&u);
      scanf("%d",&k);
      for(j=0;j<k;j++)
	{
	  scanf("%d",&v);
	  M[u-1][v-1]=1;
	}
    }

  dfs();

  return 0;
}

  

  

  



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

int n, m, a, b;
int main()
{
	int i, j, z;
	cin >> n >> m >> a >> b;
	for (i = 0; i < n; i++) {
		for (j = 0; j < m; j++) {
			z = 0;
			if (i < b) z = (z ^ 1);
			if (j < a) z = (z ^ 1);
			printf("%d", z);
		}
		puts("");
	}
    return 0;
}


Measured memory_bytes: 1280
/*Lucky_Glass*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<iostream>
using namespace std;
int n;
string str;
map<string,bool> vis;
int main() {
	scanf("%d",&n);
	char last;
	cin>>str;
	last=str[str.length()-1];
	vis[str]=true;
	for(int i=1; i<n; i++) {
		cin>>str;
		if(vis[str] || str[0]!=last) {
			printf("No\n");
			return 0;
		}
		last=str[str.length()-1];
		vis[str]=true;
	}
	printf("Yes\n");
	return 0;
}

Measured memory_bytes: 256
#include"bits/stdc++.h"
using namespace std;
typedef long long ll;
#define int ll
const int N=2e5+5;

int32_t main()
{
	#ifndef ONLINE_JUDGE
	freopen("in","r",stdin);
	freopen("out","w",stdout);
	#endif
	priority_queue<int> q;
	int n,k;cin>>n>>k;
	int x;
	for(int i=0;i<n;i++){cin>>x;q.push(x);}
	while(k-- and !q.empty())
	{
		x=q.top();q.pop();
		if(x>0)
		{
			x/=2;
			q.push(x);
		}

	}
	int ans=0;
	while(!q.empty()){ans+=q.top();q.pop();}

	cout<<ans;
	



	
}

Measured memory_bytes: 4252
main(){double a[8];for(;~scanf("%lf%lf%lf%lf%lf%lf%lf%lf",a,a+1,a+2,a+3,a+4,a+5,a+6,a+7);printf((*a<=a[6]&&a[1]<=a[7]&&a[4]<=a[2]&&a[5]<=a[3])?"YES\n":"NO\n"));}

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

int main(){
    int n;
    cin >> n;
    vi a(n);
    rep(i,n) cin >> a[i];

    ll ans = 0;
    ll cmax = a[0];
    rep(i,n){
        if(a[i]>=cmax){
            cmax = a[i];
        }
        else{
            ans += cmax-a[i];
        }
    }
    cout << ans << endl;
}

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

int main() {
  string S;
  cin >> S;
  int N = S.size(), M = 2019;
  reverse(S.begin(), S.end());
  vector<int> V(M);
  int dig = 1, sum = 0;
  V.at(sum)++;
  for (int i = 0; i < N; i++) {
    sum += (S.at(i) - '0') * dig;
    sum %= M;
    dig *= 10;
    dig %= M;
    V.at(sum)++;
  }
  int ans = 0;
  for (auto v : V) ans += v * (v - 1) / 2;
  cout << ans << "\n";
}

Measured memory_bytes: 3708
n = int(input())
a, b = list(map(int, input().split(" ")))
p = list(map(int, input().split(" ")))

count = 3*[0]

for i in p:
    if(i <= a): count[0] += 1
    elif(i <= b): count[1] += 1
    else: count[2] += 1


print(min(count))

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

int main(){
	int x;
	int i=1;
	while (1){
		cin >> x;
		if (x==0){
			break;
		}
		cout <<"Case " << i << ": "<<x<<endl;
		i=i+1;
	}
	return 0;
}

Measured memory_bytes: 1160
import java.util.Scanner;

public class Main
{
	public static void main(String[] args)
	{
		Scanner scan = new Scanner(System.in);
		int[] dirW = {-1,0,1,0};
		int[] dirH = {0,-1,0,1};

		while(scan.hasNext())
		{
			int n = scan.nextInt();
			if(n==0)
				break;

			int x1=0,x2=0;
			int[] width = new int[n];
			int y1=0,y2=0;
			int[] height = new int[n];


			for(int i=1;i<n;i++)
			{
				int card = scan.nextInt();
				int dir = scan.nextInt(); //0:左 1:下 2:右 3:上

				width[i] = width[card] + dirW[dir];
				height[i] = height[card] + dirH[dir];
				x1 = Math.min(x1, width[i]);
				x2 = Math.max(x2, width[i]);
				y1 = Math.min(y1, height[i]);
				y2 = Math.max(y2, height[i]);
			}
			int w=x2-x1+1;
			int h=y2-y1+1;
			System.out.println(w+" "+h);
		}
	}

}

Measured memory_bytes: 34144
#include<bits/stdc++.h>
using namespace std;
int main(void){
	int n;
	array<array<int,101>,101>arr;
	int u,k,v;
	int i,j;
	
	cin>>n;
	for(i=1;i<=n;i++){
		for(j=1;j<=n;j++){
			arr[i][j]=0;
		}
	}
	
	for(i=0;i<n;i++){
		cin>>u>>k;
		for(j=0;j<k;j++){
			cin>>v;
			arr[u][v]=1;
		}
	}
	for(i=1;i<=n;i++){
		cout<<arr[i][1];
		for(j=2;j<=n;j++){
			cout<<" "<<arr[i][j];
		}
		cout<<endl;
	}
	return 0;
}


Measured memory_bytes: 3132
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
typedef vector<int>VI;
int n,a,b,i;
vector<int>e[N];
bool f[N][20];
inline void uni(bool*a,bool*b){
	static bool ans[20];memset(ans,0,20);int i=0,x;
	for(;!a[i];)b[i++]=0;
	for(;i<19;++i)
		if(x=a[i]+b[i]+ans[i],x>1)memset(ans,0,i+1),ans[i+1]=1;
			else ans[i]=x;
	memcpy(a,ans,20);
}
void dfs(int u,int fa){
	f[u][0]=1;
	for(int v:e[u])if(v!=fa)
		dfs(v,u),uni(f[u],f[v]);
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>n;for(i=1;i<n;++i)cin>>a>>b,e[a].push_back(b),e[b].push_back(a);
	dfs(1,0);for(i=19;!f[1][i];--i);cout<<i<<endl;
	return 0;
}

Measured memory_bytes: 14464
#include<iostream>
#include<cstdio>
using namespace std;
int dist[110][110];
int a[10010],b[10010],c[10010];
int n,m;
int ans;
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)dist[i][j]=1e9;
	}
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%d",a+i,b+i,c+i);
		dist[a[i]][b[i]]=dist[b[i]][a[i]]=c[i];
	}
	for(int k=1;k<=n;k++)
	{
		for(int i=1;i<=n;i++)
		{
			if(i==k)continue;
			for(int j=1;j<=n;j++)
			{
				if(i==j || k==j)continue;
				dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
			}
		}
	}
	for(int i=1;i<=m;i++)
	{
		if(dist[a[i]][b[i]]!=c[i])ans++;
	}
	printf("%d",ans);
	return 0;
}

Measured memory_bytes: 256
object Main extends App {
    
    def is_ok(s: Array[Char]): Boolean = {
        if (s.isEmpty) {
            true
        } else {
            val c = s.head
            s.map(_ == c).reduceLeft(_ && _)
        }
    }
    
    def solve_impl(s: Array[Char], c: Char, i: Int): Int = {
        if (is_ok(s)) {
            i
        } else {
            var _s = s.tail
            for (i <- 0 until _s.length) {
                if (s(i) == c || s(i + 1) == c) {
                    _s(i) = c
                }
            }
            solve_impl(_s, c, i + 1)
        }
    }
    
    def solve(s: Array[Char]): Int = {
        val sgb = s.groupBy(identity)
        //val m = sgb.map(_._2.length).reduceLeft(_ max _)
        //val set = sgb.filter(_._2.length == m).map(_._1)
        val set = sgb.map(_._1)  
        
        var l = List(1000)
        for (ch <- set) {
            l = solve_impl(s, ch, 0) :: l
        }
        l.reduceLeft(_ min _)
    }
    
    val sc = new java.util.Scanner(System.in)
    val str = sc.next
    
    val s = str.toArray
    
    println(solve(s))
}

Measured memory_bytes: 29304
import java.io.*;

class Main {
    public static void main(String[] args) {
        try {
        	BufferedReader kb = new BufferedReader( new InputStreamReader( System.in ) );
        	int n = Integer.parseInt( kb.readLine( ) );
			String[] str = kb.readLine( ).split( " " );
			int[] S = new int[ n ];
			for( int i=0; i<n; i++ ) {
				S[i] = Integer.parseInt( str[i] );
			}

        	int q = Integer.parseInt( kb.readLine( ) );
			str = kb.readLine( ).split( " " );
			int[] T = new int[ q ];
			for( int i=0; i<q; i++ ) {
				T[i] = Integer.parseInt( str[i] );
			}

			int cnt = 0;
			for( int i=0; i<q; i++ ) {
				for( int j=0; j<n; j++ ) {
					if( T[i] == S[j] ) {
						cnt++;
						break;
					}
				}
			}
			System.out.println( cnt );

        } catch( IOException e ) {
            System.err.println( e );
        }
    }
}

Measured memory_bytes: 42516
#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp>
#define clr(x) memset(x,0,sizeof x))
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define Fod(i,b,a) for (int i=(b);i>=(a);i--)
#define fi first
#define se second
#define kill _z_kill
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define outval(x) cerr<<#x" = "<<x<<endl
#define outv(x) cerr<<#x" = "<<x<<"  "
#define outtag(x) cerr<<"--------------"#x"---------------"<<endl
#define outarr(a,L,R) cerr<<#a"["<<L<<".."<<R<<"] = ";\
	For(_x,L,R) cerr<<a[_x]<<" ";cerr<<endl;
#define User_Time ((double)clock()/CLOCKS_PER_SEC)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned uint;
typedef long double LD;
typedef vector <int> vi;
typedef pair <int,int> pii;
LL read(){
	LL x=0,f=0;
	char ch=getchar();
	while (!isdigit(ch))
		f=ch=='-',ch=getchar();
	while (isdigit(ch))
		x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return f?-x:x;
}
const int mod=998244353;
int Pow(int x,int y){
	int ans=1;
	for (;y;y>>=1,x=(LL)x*x%mod)
		if (y&1)
			ans=(LL)ans*x%mod;
	return ans;
}
void Add(int &x,int y){
	if ((x+=y)>=mod)
		x-=mod;
}
void Del(int &x,int y){
	if ((x-=y)<0)
		x+=mod;
}
int Add(int x){
	return x>=mod?x-mod:x;
}
int Del(int x){
	return x<0?x+mod:x;
}
void ckmax(int &x,int y){
	if (x<y)
		x=y;
}
void ckmin(int &x,int y){
	if (x>y)
		x=y;
}
const int N=300005;
int n;
int p[N],vis[N];
int cnt[N];
int val[N];
int Fac[N],Inv[N];
int C(int n,int m){
	if (m<0||m>n)
		return 0;
	return (LL)Fac[n]*Inv[m]%mod*Inv[n-m]%mod;
}
int main(){
	n=read();
	Fac[0]=1;
	For(i,1,n)
		Fac[i]=(LL)Fac[i-1]*i%mod;
	Inv[n]=Pow(Fac[n],mod-2);
	Fod(i,n,1)
		Inv[i-1]=(LL)Inv[i]*i%mod;
	For(i,1,n)
		p[i]=read();
	vi v;
	For(i,1,n){
		if (vis[i])
			continue;
		int cnt=0;
		for (int j=i;!vis[j];j=p[j])
			cnt++,vis[j]=1;
		v.pb(cnt);
	}
	sort(v.begin(),v.end());
//	outarr(v,0,(int)v.size()-1);
	if (v[0]>2)
		return puts("0"),0;
	for (auto i : v)
		cnt[i]++;
	For(i,1,n)
		for (int j=i+i;j<=n;j+=i)
			Add(val[j],(LL)i*cnt[i]%mod);
	int ans=1;
	For(i,1,n)
		if (cnt[i]&&i!=v[0]){
			int now=0;
			For(j,1,cnt[i])
				Add(now,(LL)Pow(i,cnt[i]-j)*Pow(cnt[i],cnt[i]-j)%mod*
					C(cnt[i]-1,cnt[i]-j)%mod*Pow(val[i],j)%mod);
			ans=(LL)ans*now%mod;
		}
	int r=v[0];
	ans=(LL)ans*Pow(cnt[r],max(0,cnt[r]-2))%mod;
	if (r==2)
		ans=(LL)ans*Pow(2,cnt[r]-1)%mod*cnt[r]%mod;
	cout<<ans<<endl;
	return 0;
}


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

h, n = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(n)]

inf = 10 ** 9
max_h = 2 * 10**4 + 10
dp = [inf] * max_h
dp[0] = 0
for a, b in ab:
    for i in range(max_h - a + 1):
        dp[i] = min(dp[i], dp[i - a] + b)

print(min(dp[h:]))

Measured memory_bytes: 42332
N = int(input())
# 1以上n未満の数について、それぞれ約数の個数を数える
i_Divs = [0] * (N+1)
base = 1
Max = int(N**0.5)+1
while base < Max:
    now = base
    while now <= N:
        if now//base >= Max:
            i_Divs[now] += 2
        else:
            #print(now,base,1)
            i_Divs[now] += 1
        now += base
    base += 1
#print(i_Divs)

ans = 0
for c in range(1,N):
    p = N-c
    ans += i_Divs[p]
print(ans)



Measured memory_bytes: 75880
import sys

sys.setrecursionlimit(110000)

d = {}


def calc_step_combination(num):
    if num == 0:
        return 0
    if num == 1 or num == 2:
        return 1
    if num in d:
        return d[num]
    res = calc_step_combination(num - 1) + calc_step_combination(num - 2)
    d[num] = res
    return res


def sol(n, a_list):
    step_batch = []
    batch_start = 0
    for a in a_list:
        step_batch.append(a - batch_start)
        batch_start = a + 1
    step_batch.append(n - batch_start + 1)

    result = 1
    for i, steps in enumerate(step_batch):
        result *= calc_step_combination(steps)
    print(result % 1000000007)


def main():
    n, m = map(int, input().split())
    a_list = []
    for i in range(0, m):
        a_list.append(int(input()))
    sol(n, a_list)


main()


Measured memory_bytes: 549860
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define MP make_pair
#define EACH(i,c) for(auto i: c)
#define SORT(c) sort((c).begin(),(c).end())

#define ALL(a)  (a).begin(),(a).end()

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	int A, B, C, D;
	cin >> A >> B >> C >> D;

	while(1)
	{
		C -= B;
		if(C <= 0){
			cout << "Yes" << endl;
			break;
		}
		A -= D;
		if(A <= 0){
			cout << "No" << endl;
			break;
		}
	}


	return 0;
}


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

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
  cin >> v.at(i);
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, e, t;
    cin >> b >> e >> t;
    for (int j = 0; j < e - b; j++) {
      swap(v.at(b + j), v.at(t + j));
    }
  }
  for (int i = 0; i < n; i++) {
    if (i == n - 1) {
      cout << v.at(i) << endl;
    } else {
      cout << v.at(i) << " ";
    }
  }
}



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

int main() {
	int n, t, a;
	cin >> n;
	cin >> t >> a;
	float min_dt = 2000.0;
	int min_id = 1;
	int id = 1;
	for (int i = 0; i < n; ++i) {
		int h;
		cin >> h;
		float tmp_t = (float)t - 0.006 * (float)h;
		float dt = abs(a - tmp_t);
		if (dt < min_dt) {
			min_id = id;
			min_dt = dt;
		}
		++id;
	}
	
	cout << min_id << endl; 
	return 0;
}

Measured memory_bytes: 256
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;

class UnionFind {
    int cnt;
    vector<int> par, rank, size;
public:
    UnionFind() {}
    UnionFind(int _n) :cnt(_n), par(_n), rank(_n), size(_n, 1) {
        for (int i = 0; i<_n; ++i) par[i] = i;
    }
    int find(int k) {
        return (k == par[k]) ? k : (par[k] = find(par[k]));
    }
    int operator[](int k) {
        return find(k);
    }
    int getSize(int k) {
        return size[find(k)];
    }
    void unite(int x, int y) {
        x = find(x); y = find(y);
        if (x == y) return;
        --cnt;
        if (rank[x] < rank[y]) {
            par[x] = y;
            size[y] += size[x];
        } else {
            par[y] = x;
            size[x] += size[y];
            if (rank[y] == rank[x]) ++rank[x];
        }
    }
    int count() {
        return cnt;
    }
};

void solve() {
    int N, M;
    cin >> N >> M;
    UnionFind uf(N);

    rep(i, M) {
        int x, y, z;
        cin >> x >> y >> z;
        --x; --y;
        uf.unite(x, y);
    }
    cout << uf.count() << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}

Measured memory_bytes: 1408
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static boolean[] sieve (int n) {
		boolean[] isPrime = new boolean[n + 1];
		Arrays.fill(isPrime, true);
		for (int i = 4; i < isPrime.length; i += 2)
			isPrime[i] = false;
		for (int i = 3; i * i <= n; i += 2) {
			for (int j = 3; i * j <= n; j += 2)
				isPrime[i * j] = false;
		}
		return isPrime;
	}

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		while (sc.hasNext()) {
			int n = sc.nextInt();
			boolean[] primes = sieve(n);
			int cnt = 0;
			if (n >= 2) {
				cnt = 1;
				for (int i = 3; i <= n; i += 2)
					if (primes[i]) cnt++;
			}
			System.out.println(cnt);
		}

	}

}

Measured memory_bytes: 35308
#include <bits/stdc++.h>
using namespace std;
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << x << endl
#define debugArray(x, n)                             \
  cerr << __LINE__ << ": " << #x << " = {";          \
  for (long long hoge = 0; (hoge) < (n); ++(hoge)) { \
    cerr << ((hoge) ? "," : "") << x[hoge];          \
  }                                                  \
  cerr << "}" << endl
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#endif

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string S;
  cin >> S;
  int Q;
  cin >> Q;
  string fr, ba;
  bool flip = false;
  while (Q--) {
    int T;
    cin >> T;
    if (T == 1)
      flip = !flip;
    else {
      int F;
      char C;
      cin >> F >> C;
      if ((F & 1) ^ flip)
        fr += C;
      else
        ba += C;
    }
  }
  string ans;
  if (flip) {
    reverse(S.begin(), S.end());
    reverse(ba.begin(), ba.end());
    ans = ba + S + fr;
  } else {
    reverse(fr.begin(), fr.end());
    ans = fr + S + ba;
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 4216
def main():
	N, K = map(int, input().split())
	S = input()
	current = "1"
	current_len = 0
	nums = []
	for i in range(N):
		if S[i] != current:
			nums.append(current_len)
			current_len = 1
			current = S[i]
		else:
			current_len += 1

	nums.append(current_len)
	if len(nums) %2 == 0:
		nums.append(0)

	distance = 2*K+1
	ans = 0
	sums = [0]*(len(nums)+1)
	for i in range(len(nums)):
		sums[i+1] = sums[i] + nums[i]

	for i in range(0, len(nums), 2):
		left = i
		right = min(i+distance, len(nums))
		tmp = sums[right] - sums[left]
		ans = max(tmp, ans)

	print(ans)


if __name__ == '__main__':
    main()

Measured memory_bytes: 7884
N, K = list(map(int, input().split()))
R, S, P = list(map(int, input().split()))
T = [i for i in input()]
result = 0

score = {"r": P, "s": R, "p": S}
for i in range(K):
      result += score[T[i]]
for i in range(K, N):
      if T[i] == T[i-K]:
            T[i] = ""
            continue
      else:
            result += score[T[i]]

print(result)

Measured memory_bytes: 4212
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		int N = scan.nextInt();
		int K = scan.nextInt();
		int[] p = new int[N];
		for (int i = 0; i < N; i++) {
			p[i] = scan.nextInt();
		}
		Arrays.sort(p);

		int sum = 0;

		for (int i = 0; i < K; i ++) {
			sum += p[i];
		}

		System.out.println(sum);
	}

}

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

int main(){

	float a,b,c,d,e,f,x,y;

	while(cin >> a){
		cin >> b;
		cin >> e;
		cin >> c;
		cin >> d;
		cin >> f;
		x=(d*e-b*f)/(a*d-b*c);
		y=(a*f-c*e)/(a*d-b*c);
		printf("%.3f %.3f\n",x==0?0:x,y==0?0:y);
	}
	return 0;
}

Measured memory_bytes: 1224
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;

int main() {
	int n,m;
	cin>>n>>m;
	int a[m];
	LL res = 0;
	for(int i=0;i<m;i++) {
		cin>>a[i];
		res+=a[i];
	}
	int out = n-res;
	if(out>=0) cout<<out;
	else cout<<-1;
	return 0;
}

Measured memory_bytes: 3636
/*
 
dunjen_master
 
*/
 
 
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
 
 
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
 
using namespace std;
 
const int MAX_N = int(1e6) + 10;
const long long int MOD=1e9+7;
const long long int INF=1e18;
 
typedef long long int ll;
typedef unsigned long long int llu;
 
 
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

inline ll mul(ll a,ll b){

    a=(a+MOD)%MOD;
    b=(b+MOD)%MOD;
    ll c=a*b;
    c=(c+MOD)%MOD;
    return c;
}
inline ll add(ll a,ll b){
    a=(a+MOD)%MOD;
    b=(b+MOD)%MOD;
    ll ans=(a+b);
    ans=(ans+MOD)%MOD;
    return ans;
}
ll power(ll a,ll b,ll c=0){
    ll ans=1;
    while(b){
        if(b&1){
            ans=mul(ans,a);
        }
        a=mul(a,a);
        b=b/2;
    }
    return ans;
}
template<class T1, class T2>
istream &operator>>(istream &in, pair<T1, T2> &P){
    in >> P.first >> P.second;
    return in;
}
 
template<class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &P){
    out << "(" << P.first << ", " << P.second << ")";
    return out;
}
template<class T>
istream &operator>>(istream &in, vector<T> &arr){
    for(auto &x: arr) in >> x;
    return in;
}
template<class T>
ostream &operator<<(ostream &out, const vector<T> &arr){
    for(auto &x: arr) out << x << ' '; cout << "\n";
    return out;
}
template<class T>
istream &operator>>(istream &in, deque<T> &arr){
    for(auto &x: arr) in >> x;
    return in;
}
template<class T>
ostream &operator<<(ostream &out, const deque<T> &arr){
    for(auto &x: arr) out << x << ' '; cout << "\n";
    return out;
}
mt19937 rnd(time(0));
 
inline ll gcd(ll a,ll b){
    while(b)a%=b,swap(a,b);
    return a;
}
  

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

/* ordered_set A;
   A.insert(x);
   A.erase(x);
   A.order_of_key(x) // no of elements less than (<) x
   *A.find_by_order(k-1) // kth smallest element
*/
int n;
int k;
int c;
int earliest[200005];
int latest[200005];
string s;

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



    cin>>n>>k>>c;

    fill(latest,latest+k,-1);
    fill(earliest,earliest+k,-1);

    cin>>s;


    int i=0;
    int j=0;

    while(i<n){
        if(s[i]=='o' && j<k){
            earliest[j++]=i;
            i+=c+1;
        }else{
            i++;
        }
    }

    i=n-1;
    j=k-1;

    while(i>=0){
        if(s[i]=='o' && j>=0){
            latest[j--]=i;
            i-=c;
        }

        i--;
    }

    for(int i=0;i<k;i++){
        if(latest[i]==earliest[i]){
            cout<<latest[i]+1<<" ";
        }
    }




    

    




    


    
    
    
    
 
    
}

    

    
 
    



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

using namespace std;

int main(){
	int N; cin >> N;
	vector<int> A(N);
	for(int i = 0; i < N; ++i) cin >> A[i];

	bool flag = true;

	for(int i = 0; i < N; ++i){
		if(A[i] % 2 == 0){
			if(!(A[i] % 3 == 0 || A[i] % 5 == 0)) flag = false;
		}
	}

	if(flag) cout << "APPROVED" << endl;
	else cout << "DENIED" << endl;

	return 0;
}


Measured memory_bytes: 256
=begin
push(d, x): dが0の場合、Aの先頭に整数xを挿入する。dが1の場合、末尾にxを挿入する。
pop(d): dが0の場合、Aの先頭の要素を削除する。dが1の場合、末尾の要素を削除する。
最初の数字0, 1, 2 は操作の種類を示し、それぞれpush、randomAccess、popを
=end

n = gets.to_i

b = []
n.times do |i|
    a = gets.split(" ").map(&:to_i)
    case a[0]
    when 0 then
        case a[1]
        when 0 then
            b.unshift(a[2])
        when 1 then
            b.push(a[2])
        end
    when 1 then
        print b[a[1].to_i], "\n"
    when 2 then
        case a[1]
        when 0 then
            b.delete_at(0)
        when 1 then
            b.pop()
        end
    end
end



Measured memory_bytes: 11540
#include <iostream>
#include <algorithm>
#include <math.h> 
#include <string>
#include <iomanip>
#include <vector>
using namespace std;
int main(){
    string s;
    cin >> s;
    for(int i = 0; i < 3; i++){
        if(s[i] == '9') s[i] = '1';
        else if (s[i] == '1') s[i] = '9';
    }
    cout << s << endl;
}

Measured memory_bytes: 256
import java.util.Scanner;
public class Main {
public static void main(String[] args){
	Scanner sc=new Scanner(System.in);
	int i=sc.nextInt();
	int N[]=new int[i];
	for(int j=0;j<i;j++){
		N[j]=sc.nextInt();
	}
	for(int s=0;s<i;s++){
		System.out.print(N[i-1-s]);
		if(s<(i-1))System.out.print(" ");
	}
	System.out.print("\n");
	}
}



Measured memory_bytes: 26488
#include<bits/stdc++.h>
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ms memset
#define MOD 1000000007
#define sp fixed<<setprecision
#define sz sizeof
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
bool pr[1000007];
ll fpow(ll x,ll y){x=x%MOD;ll res=1;while(y){if(y&1)res=res*x;res%=MOD;y=y>>1;x=x*x;x%=MOD;}return res;}
ll inv(ll a,ll m=MOD){ll c=m;ll y=0,x=1;if(m==1)return 0;while(a>1){ll q=a/m;ll t=m;m=a%m,a=t;t=y;y=x-q*y;x=t;}if(x<0)x+=c;return x;}
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	ll n;
	cin>>n;
	ll a[n+1],prefix[n+1];
	ll cost[n+1][n+1];
	memset(prefix,0,sizeof(prefix));
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		prefix[i]=prefix[i-1]+a[i];
	}
	for(int l=n;l>=1;l--)
	{
		for(int r=l;r<=n;r++)
		{
			if(l==r)
			cost[l][r]=0;
			else
			{
				cost[l][r]=LLONG_MAX;
				for(int k=l;k<=r-1;k++)
				{
					cost[l][r]=min(cost[l][r],cost[l][k]+cost[k+1][r]+prefix[r]-prefix[l-1]);
				}
			}
		}
	}
	cout<<cost[1][n];




}


Measured memory_bytes: 1536
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  for(int i=0;i<(n);++i)
#define FORq(i, m, n) for(int i = (m);i <= (n);++i)
#define SCD(n) scanf("%d",&n)
#define SCD2(m,n) scanf("%d%d",&m,&n)
#define SCD3(m,n,k) scanf("%d%d%d",&m,&n,&k)
#define PB push_back
#define MP make_pair
#define ARSCD(A,N) REP(i,N){SCD(A[i]);}
#define ARSCD1(A,N) FORq(i,1,N){SCD(A[i]);}
#define PRINTD(n) printf("%d\n",n)
#define PRINTLLD(n) printf("%lld\n",n)
#define DEBUG printf("%s\n","debug")
#define fst first
#define snd second
#define IN(x,S) (S.count(x) != 0)
using namespace std;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef long long ll;
//////////////////////////////////////////////////////


int main(){
	int N,C;SCD2(N,C);
	static int D[1001][1001];
	static int c[501][501];
	int cpt[3][31];
	
	if (N==1){
		PRINTD(0);
		return 0;
	}
	
	REP(i,3){
		REP(j,31){
			cpt[i][j] = 0;
		}
	}
	
	
	FORq(i,1,C){
		FORq(j,1,C){
			int x; SCD(x);
			D[i][j] = x;
		}
	}
	
	FORq(i,1,N){
		FORq(j,1,N){
			int x; SCD(x);
			c[i][j] = x;
			cpt[(i+j)%3][x]++;
			
		}
	}
	

	ll ans = 1000000000;
	
	FORq(i,1,C){
		FORq(j,1,C){
			FORq(k,1,C){
				
				ll cost = 0;
				if ((i==j) or (j==k) or (k==i)) continue;
				
				FORq(z,1,30){ // z -> i
					cost += cpt[0][z] * D[z][i];
				}
				FORq(z,1,30){ // z -> j
					cost += cpt[1][z] * D[z][j];
				}
				FORq(z,1,30){ // z -> k
					cost += cpt[2][z] * D[z][k];
				}
				
				
			ans = min(ans,cost);
			}
		}
	}
	
	PRINTLLD(ans);
	return 0;
}



Measured memory_bytes: 384
#include<iostream>
int gcd(int x,int y){
  int tmp1,r=1;
  if(y>x){
    tmp1=x;
    x=y;
    y=tmp1;
  }
  while(r>0){
    r=x%y;
    x=y;
    y=r;
  }
  return x;
}
int main(){
  int x,y;
  std::cin>>x>>y;
  std::cout<<gcd(x,y)<<"\n";
  return 0;
}



Measured memory_bytes: 3080
#include<bits/stdc++.h>
#define L long long
#define pb push_back
#define pi pair<int,int>
#define pii pair<pair<int,int>,int>
#define aa first
#define bb second
#define xx aa.aa
#define yy aa.bb
#define zz bb
#define mp make_pair
#define mpp(a,b,c) mp(mp(a,b),c)
#define g(a,b) g[h[a]+h[(1<<n)-1^b]]
using namespace std;
const int q=1000000007;
int n,m,x[20][20],f[40000],g[15000000],h[40000];
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	int i,j,k,l,u,w;
	scanf("%d%d",&n,&m);
	for(i=1;i<=m;i++)
	  {
	   scanf("%d%d",&j,&k);
	   j--;
	   k--;
	   x[j][k]=1;
	  }
	for(i=0;i<(1<<n);i++)
	  h[i]=h[i>>1]*3+(i&1);
	for(i=1;i<(1<<n);i++)
	  for(j=(i-1&i);j;j=(j-1&i))
	    {
		 k=i-j;
		 for(l=0;!(k&(1<<l));l++);
		 if(k==(1<<l))
		   {
		    for(w=0,u=0;u<n;u++)
		      if(j&(1<<u) && x[l][u])
		        w=(w*2+1)%q;
		    for(u=0;u<n;u++)
		      if(j&(1<<u) && x[u][l])
		        w=(w<<1)%q;
		   }
		 else
		   w=(L)g(j,1<<l)*g(j,k^(1<<l))%q;
		 g(j,k)=w;
		}
	for(i=0;i<(1<<n);i++)
	  if(!((i&1)^((i>>1)&1)))
	    {
		 f[i]=1;
		 for(j=i-1&i;j;j=(j-1&i))
		   f[i]=(f[i]+(L)f[j]*g(i^j,j))%q;
		}
	for(i=1,k=1;i<=m;i++)
	  k=(k<<1)%q;
	printf("%d\n",(k-f[(1<<n)-1]+q)%q);
	return 0;
}


Measured memory_bytes: 57728
import sys

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')

n = int(input())
a = [[]] * n
for i in range(n):
	a[i] = [int(x) for x in input().split()]

dp = [[]]
for i in range(n):
	if i == 0:
		dp[i] = a[i]
	else:
		dp.append([0,0,0])
		dp[i][0] = max(dp[i-1][1], dp[i-1][2]) + a[i][0]
		dp[i][1] = max(dp[i-1][0], dp[i-1][2]) + a[i][1]
		dp[i][2] = max(dp[i-1][0], dp[i-1][1]) + a[i][2]

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

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

using namespace std;

#define all(v)				((v).begin()), ((v).end())
#define allr(v)				((v).rbegin()), ((v).rend())
#define sz(v)				((int)((v).size()))
#define rep(i, v)		for(int i=0;i<sz(v);++i)
#define lp(i, n)		for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)	for(int i=(j);i<(int)(n);++i)
#define lpd(i, j, n)	for(int i=(j);i>=(int)(n);--i)
#define ll long long
#define pb					push_back
#define fIO ios_base::sync_with_stdio(0);cin.tie(0);

const ll inf = 1e8;
const double pi = 3.1415926535897;


const double EPS = (1e-7);
int dcmp(double x, double y) {	return fabs(x-y) <= EPS ? 0 : x < y ? -1 : 1;	}


typedef vector<int>                vi;
typedef vector<pair<int,int>>      vii;
typedef vector<ll>                 vll;
typedef vector<double>             vd;
typedef vector< vi >               vvi;
typedef vector< vd >               vvd;
typedef vector<string>             vs;

/**
    * PLEASEEE READDD PROBLEEEM CARFULLY ****
    * Always check for OVERFLOW ll vs int
    * Always check for array bounds
    * Check for function return
    * READ OUTPUT SECTION AGAIN!!!
**/


int main(){
    fIO

    int a, b;
    cin >> a >> b;

    if(a > 9 || b > 9) cout << -1;
    else cout << a * b;
}








Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
 
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)
#define all(n) n.begin(),n.end()
 
const int TO = 4, BO = 5, F = 0, B = 1, R = 2, L = 3; 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int OFS = 100;
struct dice {int d[6]; };

void printd(dice di)
{
    cout << di.d[TO] << endl;
    cout << di.d[F] << " " << di.d[R] << " " << di.d[B] << " " << di.d[L] << endl;
    cout << di.d[BO] << endl;
}
int RIGHT[7][7] = {
    {0,0,0,0,0,0,0,},
    {0,0,3,5,2,4,0,},
    {0,4,0,1,6,0,3,},
    {0,2,6,0,0,1,5,},
    {0,5,1,0,0,6,2,},
    {0,3,0,6,1,0,4,},
    {0,0,4,2,5,3,0,}
};
void roll(dice* di, int vec)
{
    int t;
    if(vec == F)
    {
        t = di->d[F];
        di->d[F] = di->d[TO];
        di->d[TO] = di->d[B];
        di->d[B] = di->d[BO];
        di->d[BO] = t;
    }
    if(vec == R)
    {
        t = di->d[R];
        di->d[R] = di->d[TO];
        di->d[TO] = di->d[L];
        di->d[L] = di->d[BO];
        di->d[BO] = t;
    }
    if(vec == L)
    {
        t = di->d[L];
        di->d[L] = di->d[TO];
        di->d[TO] = di->d[R];
        di->d[R] = di->d[BO];
        di->d[BO] = t;
    }
    if(vec == B)
    {
        t = di->d[B];
        di->d[B] = di->d[TO];
        di->d[TO] = di->d[F];
        di->d[F] = di->d[BO];
        di->d[BO] = t;
    }
};
const int W = 210;
typedef pair<int, int> pp;
 
int main()
{
    int n;
    while(cin >> n && n)
    {
        int tops[W][W] = {}, h[W][W] = {};
        REP(n)
        {
            int t, f;
            cin >> t >> f;
            dice tmp;
            tmp.d[TO] = t; tmp.d[F] = f; tmp.d[R] = RIGHT[t][f];
            tmp.d[L] = 7 - tmp.d[R]; tmp.d[B] = 7 - tmp.d[F];
            tmp.d[BO] = 7 - tmp.d[TO];
            int x = OFS; int y = OFS; int nowh = h[x][y]; int rollf = 1;
            //cout << "initialized  " << endl;
            while(rollf)
            {
                rollf = 0;
                vector<pp> r;
                r.push_back(pp( -tmp.d[F], F));r.push_back(pp(-tmp.d[R], R));
                r.push_back(pp( -tmp.d[B], B));r.push_back(pp(-tmp.d[L], L));
                sort(all(r));
                rep(j,4) if(nowh > h[x + dx[r[j].second]][y + dy[r[j].second]] &&
                        -r[j].first >= 4)
                {
                    roll(&tmp, r[j].second);
                    rollf = 1;
                    x += dx[r[j].second]; y += dy[r[j].second]; nowh = h[x][y];
                    break;
                }
            }
            h[x][y]++; tops[x][y] = tmp.d[TO];
        }
        //cout << "rolled " << endl;
        int ans[7] = {};
        rep(i, W)rep(j,W) ans[tops[i][j]]++;
        cout<<ans[1]<<" "<<ans[2]<<" "<<ans[3]<<" "
            <<ans[4]<<" "<<ans[5]<<" "<<ans[6]<<endl;
    }
    return 0;
}

Measured memory_bytes: 1548
INF =10001
N = int(input())
h = [int(i) for i in input().split()]
i = 2
dp = [INF for i in range(N)]
dp[0],dp[1] = 0,abs(h[1]-h[0])

while not i==N:
    dp[i] = min(dp[i-2]+abs(h[i] - h[i-2]),dp[i-1]+abs(h[i] - h[i-1]))
    i+=1
    
print(dp[N-1])

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

typedef queue<ll> qll;

int main()
{
    int k;
    cin>>k;
    int cnt=0;
    qll q;
    for(ll i=1;i<=9;i++)
    q.push(i);

    ll a;
    while(!q.empty() && cnt<k)
    {
        a=q.front();
        q.pop();
        cnt++;
        if(a%10!=0)
            q.push(10*a+a%10-1);
        q.push(10*a+a%10);
        if(a%10!=9)
            q.push(10*a+a%10+1);

    }
    cout<<a<<endl;
    return 0;
}


Measured memory_bytes: 1792
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    resolve(System.in, System.out);
  }

  static void resolve(InputStream is, PrintStream pw) {
    Scanner sc = new Scanner(is);
    int n = Integer.parseInt(sc.next());
    int nextExpectation = 1;
    int brokenBlock = 0;
    for (int i = 0; i < n; i++) {
      int ai = Integer.parseInt(sc.next());
      if (ai == nextExpectation) {
        nextExpectation++;
      } else {
        brokenBlock++;
      }
    }
    if (nextExpectation == 1) {
      pw.println(-1);
    } else {
      pw.println(brokenBlock);
    }
  }
}


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

using namespace std;

int main(void) {
	int N, C;
	cin >> N >> C;

	vector<vector<int> >D;
	int amari1[31] = { 0 };
	int amari2[31] = { 0 };
	int amari3[31] = { 0 };

	for (int i = 1; i <= C; i++) {
		vector<int> kari;
		for (int j = 0; j < C; j++) {
			int karint;
			cin >> karint; kari.push_back(karint);
		}
		D.push_back(kari);
	}
	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= N; j++) {
			int x; cin >> x;
			if ((i + j) % 3 == 1) {
				amari1[x]++;
			}
			else if ((i + j) % 3 == 2) {
				amari2[x]++;
			}
			else {
				amari3[x]++;
			}
		}
	}

	int miniwa = 250000001;

	for (int i = 1; i <= C; i++) {
		for (int j = 1; j <= C; j++) {
			for (int k = 1; k <= C; k++) {
				//まず、ijkは相異なるチェック
				if (i == j)continue;
				if (i == k)continue;
				if (j == k)continue;
				//ijkセットにかえるときの違和感チェック
				//i = 1,j = 2,k = 3担当
				int iwa = 0;

				for (int x = 1; x <= C; x++) {
					iwa += D[x - 1][i - 1] * amari1[x];
					iwa += D[x - 1][j - 1] * amari2[x];
					iwa += D[x - 1][k - 1] * amari3[x];
				}

				//miniwa 更新
				miniwa = min(iwa, miniwa);
			}
		}
	}

	cout << miniwa;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int K;
  cin >> K;
 
  int N = 7;
  int ans = -1;
  for (int i = 1; i <= K; i++) {
    if (N%K==0) {
      ans = i;
      break;
    }
    N = (N%K)*10+7;
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 3608