akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())
#define PRINT(V) for (auto v:(V)) {cerr << v << " ";} cerr << endl
#define dump(x) cerr << #x << " : " << (x) << endl
#define dump2(x, y) cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
inline ll GCD(ll a, ll b){return b ? GCD(b, a % b) : a;}
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
/* END OF TEMPLATE */

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    ll n, l;
    cin >> n >> l;
    // l -> l+n-1
    ll ans = 0;
    repr(i, l, l+n){
        ans += i;
    }
    if(l > 0){
        ans -= l;
    }else if(l+n-1 < 0){
        ans -= l+n-1;
    }
    cout << ans << endl;
    return 0;
}

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

using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() {printf("yes\n");}
void no() {printf("no\n");}
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int N; ll K;
Pll P[int(1e5+5)];

int main(){
  cin >> N >> K;
  for (int i = 0; i < N; i++){
    ll A, B; cin >> A >> B;
    P[i] = {A, B};
  }
  sort(P, P + N);

  for (int i = 0; i < N; i++){
    if (K <= P[i].se){
      cout << P[i].fi << endl;
      return 0;
    }
    K -= P[i].se;
  }

  return 0;
}



Measured memory_bytes: 5172
'use strict';
const main = arg => {
  const input = arg.trim().split('\n');
  const K = parseInt(input[0], 10);
  const range = input[1].split(' ').map(n => parseInt(n, 10));

  if (Math.ceil(range[0] / K) * K >= range[0] && Math.ceil(range[0] / K) * K <= range[1]) {
    console.log("OK");
  } else {
    console.log("NG");
  }


  // console.log(K, range, Math.ceil(range[0] / K) * K);
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));

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

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;

const int mod = 998244353, maxn = 3010;
int dp[maxn][maxn];

void solve(){
	
	int n, s;
	cin >> n >> s;
	
	vector<int> a(n);
	for(int i = 0; i < n; i++)
		cin >> a[i];
	
	
	dp[0][0] = 1;
	for(int pos = 0; pos < n; pos++){
		for(int soma = 0; soma <= s; soma++){
			if(soma + a[pos] <= s){
				dp[pos+1][soma+a[pos]] += dp[pos][soma];
				dp[pos+1][soma+a[pos]] %= mod;
			}
			
			dp[pos+1][soma] += 2*dp[pos][soma];
			dp[pos+1][soma] %= mod;
		}
	}
	
	cout << dp[n][s] << endl;
}

int32_t main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	
	int tc = 1;
	//cin >> tc;
	while(tc--)
		solve();
}


Measured memory_bytes: 74128
import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());
        int k = Integer.parseInt(scanner.nextLine());

        int result = 1;
        for (int i = 0; i < n; i++) {
            result = Math.min(result * 2, result + k);
        }
        System.out.println(result);
    }
}


Measured memory_bytes: 22608
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AOJ_ITP_1_3_D
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split(' ');
            int a = int.Parse(s[0]);
            int b = int.Parse(s[1]);
            int c = int.Parse(s[2]);
            //a <= i <= bのとき、cの約数となるiはいくつあるか
            int count = 0;
            for(int i = a; i <= b; i++)
            {
                if (c % i == 0)
                    count++;
            }
            Console.WriteLine(count);
        }
    }
}



Measured memory_bytes: 17976
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define fir first
#define sec second
#define mod 1000000007
#define ll long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read()
{
	char ch=getchar(); int nega=1; while(!isdigit(ch)) {if(ch=='-') nega=-1; ch=getchar();}
	int ans=0; while(isdigit(ch)) {ans=ans*10+ch-48;ch=getchar();}
	if(nega==-1) return -ans;
	return ans;
}
typedef pair<int,int> pii;
inline int add(int x,int y){return x+y>=mod?x+y-mod:x+y;}
inline int add(int x,int y,int z){return add(add(x,y),z);}
inline int sub(int x,int y){return x-y<0?x-y+mod:x-y;}
inline int mul(int x,int y){return 1LL*x*y%mod;}
inline int mul(int x,int y,int z){return mul(mul(x,y),z);}
int qpow(int x,int y)
{
	int ans=1;
	while(y)
	{
		if(y&1) ans=mul(ans,x);
		x=mul(x,x);
		y>>=1;
	}
	return ans;
}
int Inv(int x){return qpow(x,mod-2);}
#define N 70005
int fac[N],inv[N],pinv[N];
int f[N][17],a[N],pw[N];
void init()
{
	fac[0]=1; for(int i=1;i<N;i++) fac[i]=mul(fac[i-1],i);
	inv[0]=inv[1]=1; for(int i=2;i<N;i++) inv[i]=mul(mod-mod/i,inv[mod%i]);
	pinv[0]=1; for(int i=1;i<N;i++) pinv[i]=mul(pinv[i-1],inv[i]);
	pw[0]=1; for(int i=1;i<=20;i++) pw[i]=mul(pw[i-1],2);
}
int C(int x,int y){return x>=y?mul(fac[x],pinv[x-y],pinv[y]):0;}
signed main()
{
	init();
	int n,m; cin>>n>>m;
	for(int i=1;i<=m;i++) a[i]=read();
	f[0][m+1]=1;
	for(int i=1;i<(1<<n);i++)
	{
		for(int j=1;j<=m;j++)
		{
			for(int k=0;k<n;k++)
			{
				if(!(i>>k&1)) continue;
				int r=0;
				for(int c=j+1;c<=m+1;c++) r=add(r,f[i^(1<<k)][c]);
				int S=pw[n]-a[j];
				// printf("%d %d %d %d %d %d\n",i,j,k,S-1,pw[k]-1,fac[pw[k]-1]);
				for(int c=0;c<n;c++) if(i>>c&1) if(c!=k) S-=pw[c];
				int t=mul(C(S,pw[k]-1),fac[pw[k]]);
				f[i][j]=add(f[i][j],mul(r,t));
			}
		}
	}
	int ans=0;
	for(int i=0;i<(1<<n);i++)
	{
		int r=0;
		for(int j=1;j<=m+1;j++) r=add(r,f[i][j]);
		// printf("%d %d\n",i,r);
		int S=0;
		for(int j=0;j<n;j++) if(!(i>>j&1)) S+=pw[j];
		r=mul(r,fac[S]);
		if(__builtin_popcount(i)&1) ans=sub(ans,r);
		else ans=add(ans,r);
	}
	cout<<mul(ans,pw[n])<<endl;
	return 0;
}




Measured memory_bytes: 8804
if __name__ == '__main__':
    while True:
        try:
            a, b, c, d, e, f = map(float, input().split())
            x = (c*e - f*b)/(a*e - b*d)
            y = (c*d - f*a)/(b*d - e*a)
            if x == -0.0:
                x = 0.0
            if y == -0.0:
                y = 0.0
            print("{0:.3f}".format(x),end = ' ')
            print("{0:.3f}".format(y))
        except EOFError:
            break


Measured memory_bytes: 5580
#include<stdio.h>
int main(){
  int f,i,h=1;
  char s[22];
  scanf("%s",s);
  for(i=0;s[i];i++){
    if(s[i]=='A')f=1;
    if(s[i]=='Z'&&f==1){
      printf("AZ");
      f=h=0;
    }
  }
  if(h)printf("-1");
  printf("\n");
  return 0;
}

Measured memory_bytes: 524
#include <iostream>

using namespace std;

const int Nmax = int(1e5 + 5),MOD = 1e9 + 7;
int dp[Nmax];
int N,M,X;

int main() {
    cin >> N >> M;
    while(M--) {
        cin >> X;
        dp[X] = -1;
    }
    dp[N] = 1;
    for(int i = N - 1; i >= 0; --i)
        if(dp[i] == -1)
            dp[i] = 0;
        else
            dp[i] = (dp[i + 1] + dp[i + 2]) % MOD;
    cout <<dp[0];
    return 0;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define MAX 500009
#define MOD 1000000007
#define INF 1000000000000000000

vector<pair<int,int>> tree;
int n=1;

void update(int i){
  int x=n-1+i;
  tree.at(x).first=i;
  tree.at(x).second=i;
  while(x>0){
    x=(x-1)/2;
    tree.at(x).first=min(tree.at(2*x+1).first,tree.at(2*x+2).first);
    tree.at(x).second=max(tree.at(2*x+1).second,tree.at(2*x+2).second);
  }
}

pair<int,int> solve(int a,int b,int i,int left,int right){
  if(b<=left||right<=a){
    return make_pair(n,-1);
  }
  if(a<=left&&right<=b){
    return tree.at(i);
  }
  pair<int,int> p1,p2;
  p1=solve(a,b,2*i+1,left,(left+right)/2);
  p2=solve(a,b,2*i+2,(left+right)/2,right);
  return make_pair(min(p1.first,p2.first),max(p1.second,p2.second));
}

int main(){
  int N;
  cin>>N;
  vector<pair<int,int>> a(N);
  for(int i=0;i<N;i++){
    cin>>a.at(i).first;
    a.at(i).second=i;
  }
  sort(a.begin(),a.end());
  while(n<N){
    n<<=1;
  }

  ll ans=0;
  tree.resize(2*n-1,make_pair(N,-1));
  for(int i=0;i<N;i++){
    int left=solve(0,a.at(i).second,0,0,n).second;
    int right=solve(a.at(i).second,N,0,0,n).first;
    ans+=(ll)a.at(i).first*((ll)a.at(i).second-(ll)left)*((ll)right-(ll)a.at(i).second);
    update(a.at(i).second);
  }

  cout<<ans<<endl;
}


Measured memory_bytes: 5888
//おまじない
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#define INF 1e9+7
#define roop(i,m,n) for(int i=m;i<n;i++)
#define mroop(i,m,n) for(int i=m;i<n;i--)
#define NO cout<<"NO"<<endl;
#define YES cout << "YES"<<endl;
#define No cout << "No"<<endl;
#define Yes cout << "Yes"<<endl;
using namespace std;
typedef long long int ll;

//(int)'a'は97 (int)'A'は65
//おまじない



int main() {
	
	char c[3];
	cin >> c;

	if (c[0] == c[2]) {
		Yes;
	}
	else {
		No;
	}

	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <tuple>
#include <fstream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <cmath>
#include <queue>
#include <map>
#include <functional>
#include <cstdint>
#define REP(i, n) for(ll i = 0; i < n; i++)
//using ll = long long;
using namespace std;

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  string s, s1;
  int k, l;
  cin >> k;
  cin >> s;
  l = s.length();

  if (l <= k){
    cout << s << endl;
  } else {
    s1 = s.substr(0,k) + "...";
    cout << s1 << endl;
  }

}
  

Measured memory_bytes: 3652
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
#  = int(input())
n, m  = map(int, input().split())
#  = list(map(int, input().split()))
ab = [tuple(map(int, input().split())) for i in range(m)]
#
# c = collections.Counter()

doro = [0] * (n + 1)

for ab_ in ab:
    a, b = ab_
    doro[a] += 1
    doro[b] += 1

for d in doro[1:]:
    print(d)


Measured memory_bytes: 3316
package main

import (
	"bufio"
	"fmt"
	"log"
	"math"
	"os"
	"sort"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func main() {
	sc.Split(bufio.ScanWords)
	log.SetFlags(log.Lshortfile)
	e := make([]int, 3)
	e[0] = nextInt()
	e[1] = nextInt()
	e[2] = nextInt()
	sort.Ints(e)
	fmt.Println(e[0] * e[1] / 2)
}

func nextString() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

var dy = []int{0, 1, 0, -1}
var dx = []int{1, 0, -1, 0}
var MAX = math.MaxInt32

func pow(a, b int) int {
	return int(math.Pow(float64(a), float64(b)))
}

func lcm(a, b int) int {
	return a * b / gcd(a, b)
}
func gcd(a, b int) int {
	if a < b {
		a, b = b, a
	}
	for b != 0 {
		a, b = b, a%b
	}
	return a
}
func max(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r < a[i] {
			r = a[i]
		}
	}
	return r
}
func min(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r > a[i] {
			r = a[i]
		}
	}
	return r
}
func sum(a []int) (r int) {
	for i := range a {
		r += a[i]
	}
	return r
}
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

type Pair struct {
	a, b int
}

type Pairs []Pair

func (p Pairs) Len() int {
	return len(p)
}
func (p Pairs) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

func (p Pairs) Less(i, j int) bool {
	if p[i].a == p[j].a {
		return p[i].b < p[j].b
	}
	return p[i].a < p[j].a
}


Measured memory_bytes: 640
#include <stdio.h>
#include <vector>
using namespace std;

#define PB push_back

constexpr int kN = int(2E5 + 10);

int a[kN];

int main() {
	int n;
	vector<int> v;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = n; i >= 1; i--) for (int j = i << 1; j <= n; j += i) a[i] ^= a[j];
	for (int i = 1; i <= n; i++) if (a[i]) v.PB(i);
	printf("%d\n", int(v.size()));
	if (!v.empty()) printf("%d", v[0]);
	for (int i = 1; i < int(v.size()); i++) printf(" %d", v[i]);
	printf("\n");
}


Measured memory_bytes: 2424
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

typedef long long int int64;

#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define ABS(a) ((a)>(0)?(a):-(a))

void run(void){
  int a,b,c;
  scanf("%d%d%d",&a,&b,&c);
  int min=MIN(a,MIN(b,c));
  int max=MAX(a,MAX(b,c));
  printf("%d\n",max-min);
}

int main(void){
  run();
  return 0;
}


Measured memory_bytes: 128
import java.lang.Error
import java.util.ArrayDeque

fun main(args: Array<String>){
    val SA = ArrayDeque(readLine()!!.toList())
    val SB = ArrayDeque(readLine()!!.toList())
    val SC = ArrayDeque(readLine()!!.toList())
    SA.add('e')
    SB.add('e')
    SC.add('e')

    var turn = 'a'
    while(SA.isNotEmpty() && SB.isNotEmpty() && SC.isNotEmpty()){
        turn = when(turn){
            'a' -> SA.poll()
            'b' -> SB.poll()
            'c' -> SC.poll()
            else -> throw Error()
        }
        if(turn == 'e'){
            when{
                SA.isEmpty() -> 'A'
                SB.isEmpty() -> 'B'
                SC.isEmpty() -> 'C'
                else -> throw Error()
            }.let { println(it) }
            return
        }
    }
}

Measured memory_bytes: 33732
n, a, b = map(int, input().split())
if n <= a:
    print(n)
else:
    sum_blue = a
    n -= (a + b)
    if n < 0:
        n = 0
    loop_num = n // (a + b)
    sub_num = n - loop_num * (a + b)
    sum_blue += loop_num * a
    if sub_num <= a:
        sum_blue += sub_num
    else:
        sum_blue += a

    print(sum_blue)

Measured memory_bytes: 3064
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
  ll n,a[5],c;scanf("%lld%lld%lld%lld%lld%lld",&n,&a[0],&a[1],&a[2],&a[3],&a[4]);
  c = min({a[0],a[1],a[2],a[3],a[4]});
  printf("%lld\n",((n%c) ? n/c+1:n/c)+4);
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define xmod int(1e9)
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pi 3.14159265358
#define inf long long(1e18)
using namespace std;

int n; 
int main(){
	scanf("%d",&n);
	printf("%d\n",3*n*n);
	return 0;
}
//

Measured memory_bytes: 256
#![allow(unused_imports)]
use std::io;
use std::cmp;
use std::collections::BinaryHeap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::f64::consts::PI;
use std::str::FromStr;
use std::cmp::Reverse;

#[allow(dead_code)]
type Error = Box<dyn std::error::Error>;

fn read_line() -> String {
    let mut buf = String::new();
    io::stdin().read_line(&mut buf).ok();
    buf
}

struct Scanner {
    buff: Vec<String>,
    pos: usize,
}

#[allow(dead_code)]
impl Scanner {
    pub fn new() -> Self {
        Scanner {
            buff: Vec::new(),
            pos: 0,
        }
    }

    pub fn next<T: FromStr>(&mut self) -> T {
        while self.buff.len() <= self.pos {
            self.pos = 0;
            self.buff = read_line().split_whitespace().map(|s| s.to_owned()).collect();
        }

        let result = self.buff[self.pos].parse::<T>();
        self.pos += 1;
        result.ok().unwrap()
    }

    pub fn next_line(&mut self) -> String {
        let result  = read_line().trim().to_owned();
        self.buff.clear();
        self.pos = 0;

        result
    }

    pub fn next_vec<T: FromStr>(&mut self) -> Vec<T> {
        self.buff.clear();
        self.pos = 0;
        read_line().trim().split_whitespace().map(|s| s.parse().ok().unwrap()).collect()
    }
    
    pub fn next_line_chars(&mut self) -> Vec<char> {
        let result  = read_line().trim().chars().collect();
        self.buff.clear();
        self.pos = 0;

        result
    }
}

fn func(n: u64, a: u64, b: u64, c: u64, d: u64, memo: &mut HashMap<u64, u64>) -> u64 {
    if n == 0 {
        return 0;
    }
    if n == 1 {
        return d;
    }

    if let Some(&v) = memo.get(&n) {
        return v;
    }
    let n2 = n / 2 * 2;
    let n2u = (n+1) / 2 * 2;
    let n3 = n / 3 * 3;
    let n3u = (n+2) / 3 * 3;
    let n5 = n / 5 * 5;
    let n5u = (n+4) / 5 * 5;

    let mut ans = 1_000_000_000_000_000_010;
    if n < ans /d {
        ans = n * d;
    }
    ans = cmp::min(ans, (n - n2) * d + a + func(n2/2, a, b, c, d, memo));
    ans = cmp::min(ans, (n2u - n) * d + a + func(n2u/2, a, b, c, d, memo));
    ans = cmp::min(ans, (n - n3) * d + b + func(n3/3, a, b, c, d, memo));
    ans = cmp::min(ans, (n3u - n) * d + b + func(n3u/3, a, b, c, d, memo));
    ans = cmp::min(ans, (n - n5) * d + c + func(n5/5, a, b, c, d, memo));
    ans = cmp::min(ans, (n5u - n) * d + c + func(n5u/5, a, b, c, d, memo));

    memo.insert(n, ans);

    ans
}


fn main() {
    let mut sc = Scanner::new();
    let t: usize = sc.next();
    for _i in 0..t {
        let n: u64 = sc.next();
        let a: u64 = sc.next();
        let b: u64 = sc.next();
        let c: u64 = sc.next();
        let d: u64 = sc.next();

        let mut memo = HashMap::new();
        println!("{}", func(n, a, b, c, d, &mut memo));
    }
}


Measured memory_bytes: 2612
#include<stdio.h>

int main(void){
int n,i;
int cnt = 0;

scanf("%d",&n);

for(i=0;i<4;i++){
  if(n % 10 == 2)
    cnt += 1;
  n = n / 10;
  }
  printf("%d\n",cnt);
  return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;
const int INF = 1e9 + 1;
const ll LLINF = 1e18;
const int MOD = 1e9 + 7;

int main() {
    int A, B, C, D, E, F;
    cin >> A >> B >> C >> D >> E >> F;

    vector<int> absum;
    static bool ab[30];
    for(int i = 0; 100 * A * i <= F; ++i) {
        for(int j = 0; 100 * (A * i + B * j) <= F; ++j) {
            int tmp = A * i + B * j;
            if(tmp * 100 == 0)
                continue;
            if(!ab[tmp]) {
                absum.push_back(tmp);
                ab[tmp] = true;
            }
        }
    }

    double ma = 0;
    int ans1, ans2;
    for(auto i : absum) {
        for(int j = 0; j * C <= i * E && 100 * i + j * C <= F; ++j) {
            int tmp = min(i * E - j * C, F - 100 * i - j * C) / D;
            tmp = j * C + tmp * D;
            if((double)100 * tmp / (i * 100 + tmp) >= ma) {
                ans1 = 100 * i + tmp, ans2 = tmp;
                ma = (double)100 * tmp / (i * 100 + tmp);
            }
        }
    }
    cout << ans1 << " " << ans2 << endl;
    return 0;
}


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

using namespace std;

const int max_n = 200222, inf = 1000111222;

int n;
long long ans;
char s[max_n];

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    scanf("%s", s);
    n = strlen(s);
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
        if (s[i] == 'A') {
            ++cnt;
        } else if (i + 1 < n && s[i] == 'B' && s[i + 1] == 'C' && cnt) {
            ans += cnt;
            ++i;
        } else {
            cnt = 0;
        }
    }
    printf("%lld\n", ans);
    return 0;
}


Measured memory_bytes: 384
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        double x=0;
        for(int i =1 ; i<=n ; i++){
            
            if(i%2==1)x++;
        }
        System.out.println(x/n);
    }
}


Measured memory_bytes: 22868
#include <cstdio>

using namespace std;

int main() {
    char S[256];
    scanf("%s", &S);
    bool flag = false;
    for(int i=0; S[i]; ++i) {
        flag |= (S[i]=="LR"[i&1]);
    }
    printf(flag ? "No\n" : "Yes\n");
    return 0;
}


Measured memory_bytes: 128
xxx = gets()
yyy = xxx.split()

W = yyy[0].to_i
H = yyy[1].to_i
x = yyy[2].to_i
y = yyy[3].to_i
r = yyy[4].to_i

x1 = x - r
x2 = x + r
y1 = y - r
y2 = y + r

m = 0
n = 0

if 0 <= x1 && x2 <= W
 m = 1
end

if 0<= y1 && y2 <= H
 n = 1
end

if m ==1 && n ==1
 puts "Yes"
else
 puts "No"
end



Measured memory_bytes: 8872
/** 
 * Dont raise your voice, improve your argument.
 * --Desmond Tutu
 */

#include <bits/stdc++.h>
using namespace std;

#define DEBUG
#ifndef DEBUG
#define endl "\n"
#endif

const double PI=3.1415926535897932384626433;
typedef long long ll;
#define fori(n) for(ll i=0; i<(n); i++)

#define cins(s) string s; cin>>s;
#define cini(i) int i; cin>>i;
#define cinll(l) ll l; cin>>l;
#define cind(d) double d; cin>>d;
#define cinai(a, n) vi a(n); fori(n) { cin>>a[i]; }
#define cinall(a, n) vll a(n); fori(n) { cin>>a[i]; }
#define cinas(s, n) vs s(n); fori(n) { cin>>s[i]; }

typedef pair<int, int> pii;
typedef pair<double,double> pdd;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<string> vs;
typedef vector<ll> vll;

int main() {
cin.tie(nullptr);
std::ios::sync_with_stdio(false);
    cini(n);
    vector<pii> xy(n);
    for(int i=0; i<n; i++) {
        cin>>xy[i].first>>xy[i].second;
    }

    vector<vector<double>> dist(n, vector<double>(n));

    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
            dist[i][j]=dist[j][i]=hypot(xy[i].first-xy[j].first, xy[i].second-xy[j].second);

    vi p(n);
    iota(p.begin(), p.end(), 0);
    double sum=0.0;
    int cnt=0;
    do {
        for(int i=0; i<n-1; i++)
            sum+=dist[p[i]][p[i+1]];
        cnt++;
    }while(next_permutation(p.begin(), p.end()));

    sum/=cnt;
    cout<<setprecision(18)<<sum<<endl;

}



Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
string MAP[100];
ll vx[4]={-1,0,1,0};
ll vy[4]={0,1,0,-1};
ll h,w;
bool check(ll x,ll y){
  if(x>=0&&x<h&&y>=0&&y<w)return true;
  else return false;
}
int main(){
  cin>>h>>w;
  ll ans=1e9;
  for(int i=0;i<h;i++)
    	cin>>MAP[i];
  queue<pair<pair<ll,ll>,ll>>que;
  bool used[100][100]={};
  used[0][0]=true;
  que.push(make_pair(make_pair(0,0),1));
  while(!que.empty()){
    pair<ll,ll>now = que.front().first;
    ll cost=que.front().second;
    que.pop();
    if(now.first==h-1&&now.second==w-1){
      ans=min(cost,ans);
      continue;
    }
    for(int i=0;i<4;i++){
      if(check(vx[i]+now.first,vy[i]+now.second)&&MAP[vx[i]+now.first][vy[i]+now.second]=='.'&&used[vx[i]+now.first][vy[i]+now.second]==false){
        que.push(make_pair(make_pair(vx[i]+now.first,vy[i]+now.second),cost+1));
        used[vx[i]+now.first][vy[i]+now.second]=true;
      }
    }
  }
  ll cnt=0;
  for(int i=0;i<h;i++)for(int j=0;j<w;j++)
    if(MAP[i][j]=='#')cnt++;
  if(ans==1e9)cout<<-1<<endl;
  else
  cout<<h*w-ans-cnt<<endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <functional>
#include <numeric>
#include <limits>
#include <cmath>
#include <iomanip>
#include <sstream>
 
#define ALL(a)  (a).begin(),(a).end()
 
using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

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

    ll ans = 0;

    ll n;
    cin >> n;

    (n % 2) ? (cout << (n+1)/2 << endl) : (cout << n/2 << endl);

}

Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define repp(i,l,r,k)for(ll i=(l);i<(r);i+=(k))
#define INF ((1LL<<62)-(1LL<<31))
#define max(p,q)((p)>(q)?(p):(q))
#define min(p,q)((p)<(q)?(p):(q))
#define bit(n,m)(((n)>>(m))&1)
int upll(const void*a, const void*b){return*(ll*)a<*(ll*)b?-1:*(ll*)a>*(ll*)b?1:0;}
int downll(const void*a, const void*b){return*(ll*)a<*(ll*)b?1:*(ll*)a>*(ll*)b?-1:0;}
void sortup(ll*a,int n){qsort(a,n,sizeof(ll),upll);}
void sortdown(ll*a,int n){qsort(a,n,sizeof(ll),downll);}
ll pom(ll a,ll n,int m){ll x=1;for(a%=m;n;n/=2)n&1?x=x*a%m:0,a=a*a%m;return x;}
//#define MOD 998244353
#define MOD 1000000007
#define invp(a,p)pom(a,p-2,p)

ll n,k;
ll h[100010];
ll dp[100110];
int main(){
	scanf("%lld%lld",&n,&k);
	rep(i,0,n)scanf("%lld",h+i+1);
	rep(i,2,n+1)dp[i]=INF;
	rep(i,1,n){
		rep(j,1,k+1)dp[i+j]=min(dp[i+j],dp[i]+llabs(h[i]-h[i+j]));
	}
	printf("%lld\n",dp[n]);
	return 0;
}

Measured memory_bytes: 1664
N=int(input())
from collections import defaultdict
from collections import deque
branch=defaultdict(set)
for i in range(N-1):
    a,b,c=map(int,input().split())
    branch[a]|={(b,c),}
    branch[b]|={(a,c),}
Q,K=map(int,input().split())
dist=[float('inf')]*(N+1)
dist[K]=0
check=deque([K])
while len(check)>0:
    now=check.popleft()
    for nex,c in branch[now]:
        if dist[nex]>dist[now]+c:
            dist[nex]=dist[now]+c
            check.append(nex)
for q in range(Q):
    x,y=map(int,input().split())
    print(dist[x]+dist[y])

Measured memory_bytes: 60716
n = int(input())
xList = list(map(int,input().split()))

nowMinPoint = 9999999999
for chkp in range(1,100):
    tmpP = 0
    for xi in range(1,n+1):
        tmpP += abs(chkp-xList[xi-1])*abs(chkp-xList[xi-1])
    if nowMinPoint > tmpP:
        nowMinPoint = tmpP

print(nowMinPoint)


Measured memory_bytes: 39408
def  isOk(arg):
    if arg > mn:
        return True
    else:
        return False

def bisect_m(ng,ok):
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if isOk(l[mid]):
            ok = mid
        else:
            ng = mid
    return ok

n = int(input())
l = list(map(int,input().split()))
l.sort()
ans = 0
for i in range(n-2):
    a = l[n-1-i]
    for j in range(i+1,n-1):
        b = l[n-1-j]
        #print(a,b)
        mn = a - b
        ng = -1
        ok = n-1-j
        ok = bisect_m(ng,ok)
        ans += n-1-j - ok
        #print(a,b,l[ok],mn,ok)


print(ans)



Measured memory_bytes: 49244
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
 
#define int long long
#define REP(i,n) for(int i=0;(i)<(n);(i)++)
#define ll long long
 
 
 
signed main() {
	string A;
	cin >> A;
 
	int len = A.length();
 
	int ans = 0;
	for (int i = 1;i < len;i++) {
		ans += i;
	}
 
 
	int alpha[26];
	REP(i, 26) alpha[i] = 0;
	REP(i,len){
		for (char c = 'a';c <= 'z';c++) {
			if (A[i] == c) alpha[c - 'a']++;
		}
	}
 
	REP(i, 26) {
		int a = alpha[i];
		ans = ans - a * (a - 1) / 2;
	}
 
	cout << ans + 1 << endl;
	return 0;
}

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

int main() {
  string s;
  cin >> s;
  for(int i = 0;i < s.size()-1;i++){
    for(int j = i+1;j < s.size();j++){
      if(s.at(i) == s.at(j)){
        cout << "no" << endl;
        return 0;
      }
    }
  }
  cout << "yes" << endl;
}


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

int n, x, i, sum, d[1005];

int main(void) {;
    scanf("%d%d", &n, &x);
    for (i = 0; i < n; i++) {
        scanf("%d", &d[i]);
    }
    sort(d, d + n);
    i = sum = 0;
    while (i < n) {
       if (x < d[i])
            break;
       else {
            x -= d[i++];
            sum++;
       }
    }
    if (x > 0)
        sum += x / d[0];
    printf("%d\n", sum);
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fr first
#define sc second
#define pb push_back
#define mp make_pair
#define sz(c) c.size()
#define gcdll(x, y) __gcd(x, y)
#define lcmll(x, y) (x * y) / gcdll(x, y)
#define all(v) (v).begin(), (v).end()
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define bs_v(v, x) binary_search(all(v), x)
#define bs_a(a, i, n, x) binary_search(a + i, a + n, x)
#define IOS                       \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);

void solve()
{
    int n;cin>>n;
    int cnt = 0;
    bool f=false;
    rep(i,0,n){
        int x,y;cin>>x>>y;
        if(x==y)cnt++;
        else cnt=0;
        if(cnt>=3)f=true;
    }
    if(f)cout<<"Yes";
    else cout<<"No";
}

int main()
{
    IOS;
    int T = 1;
    //cin >> T;
    rep(i, 1, T + 1)
    {
        solve();
    }
    return 0;
}

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

typedef long long ll;
const int INF = 1e9;
const int N = 1e5 + 5;

int a[N];

int main()
{
	int n; scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	int ans = 0;
	for (int i = 1; i <= n; i++)
		if (a[i] == i) swap(a[i], a[i + 1]), ans++;
	printf("%d\n", ans);
	return 0;
} 

Measured memory_bytes: 640
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false)
#define ll long long
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define mp(a, b) make_pair(a, b)
#define INF 0x3f3f3f3f

inline int read() {
  int x = 0, neg = 1; char op = getchar();
  while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); }
  while (isdigit(op)) { x = 10 * x + op - '0'; op = getchar(); }
  return neg * x;
}
inline void print(int x) {
  if (x < 0) { putchar('-'); x = -x; }
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}

const int maxn = 2e5 + 10;
int n,a[maxn],b[maxn];
signed main() {
  IO;
  cin >> n;
  rep(i,0,n-1){
    cin >> a[i] >> b[i];
  }
  sort(a ,a + n);
  sort(b ,b + n);
  if(n % 2 == 0){
    int k1 = a[n / 2] + a[n / 2 - 1];
    int k2 = b[n/2] + b[n / 2 - 1];
    int ans = abs(k1 - k2) + 1;
    cout << ans << '\n';
  }else {
    int ans = abs(a[n/2] - b[n/2]) + 1;
    cout << ans << '\n';
  }
  return 0;
}

Measured memory_bytes: 5176
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <set>
#include <map>
#include <cstring>
#include <cstdio>
#include <stack>
#define INF 1000000000
#define rep(i,n) for(int i =0;i<n;i++)
using namespace std;
typedef long long int ll;

int main(){
    for(;;){
        int N,M;
        cin >> N>>M;
        if(!N and !M)break;
        int r;
        cin >> r;
        vector<int> g(10001*721,0);
        map<ll, int> hoge;
        for (int i=0; i<r; i++) {
            int t,n,m,s;
            cin >>t>>n>>m>>s;
            t-=540;
            if (s==1) {
                hoge[1000*m+n]=t;
            }else{
                for (int j=hoge[1000*m+n];j<t; j++) {
                    g[m*721 + j] = 1;
                }
            }
        }
        int q;
        cin >>q;
        for (int qq = 0; qq < q; qq++) {
            int ts,te,m,ans = 0;
            cin >> ts >> te >>m;
            for (int i= ts-540; i<te-540; i++) {
                if (g[m*721+i] == 1) {
                    ans++;
                }
            }
            cout << ans <<endl;
        }
    }
}

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

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9+7;
// const ll MOD = 998244353;
const ll INF = 1ll<<60;

#define FOR(i,a,b) for (ll i=(a);i<(ll)(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

int main(int argc, char **argv)
{
	ll N, C; cin >> N >> C;
	ll MAX = 100010;
	vector<vector<ll>> imosc(C, vector<ll>(MAX, 0));
	vector<ll> imos(100010, 0);
	REP(i, N)
	{
		ll s, t, c; cin >> s >> t >> c;
		--c;
		imos[s-1]++;
		imos[t]--;
		imosc[c][s-1]++;
		imosc[c][t]--;
	}

	REP(i, MAX-1)
		imos[i+1] += imos[i];

	REP(i, C)
	{
		REP(k, MAX-1)
		{
			imosc[i][k+1] += imosc[i][k];
		}
	}


	ll res{0};
	REP(i, MAX)
	{
		ll tmp = imos[i];
		REP(j, C)
		{
			tmp -= max(0ll, imosc[j][i]-1);
		}

		res = max(res, tmp);
	}

	std::cout << res << std::endl;
}


Measured memory_bytes: 24576
program strange_nim
  implicit none
  integer :: n, a, k, d, m, i, g = 0
  read(*,*) n
  do i = 1, n
    read(*,*) a, k
    do
      d = a/k
      m = mod(a,k)
      if (m == 0) exit
      a = a-(m+d)/(d+1)*(d+1)
    end do
    g = xor(g,d)
  end do
  if (g == 0) then
    write(*,'(a)') "Aoki"
  else
    write(*,'(a)') "Takahashi"
  end if
end program strange_nim

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long int
#define pb push_back
#define mp make_pair
const int mod=1e9+7;
void solve()
{
  int x;
  cin>>x;

  int k=360,temp=360;
  int cnt=1;
  while(k%x!=0)
  {
    k+=temp;
  }
  cout<<k/x<<endl;
}

int main()
{
  fastio
  solve();
}


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

using namespace std;
#define ll long long
#define ld long double

ll binPow(ll a, ll b) {
    ll ans = 1;
    while(b > 0) {
        if(b & 1) ans *= a;
        a *= a;
        b >>= 1;
    }
    return ans;
}

ll binPowMod(ll a, ll b, ll m) {
    ll ans = 1;
    a %= m;
    while(b > 0) {
        if(b & 1) ans *= a % m;
        a *= a % m;
        b >>= 1;
    }
    return ans;
}

int main()
{
    ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
    int n;
    cin >> n;
    if(n < 1200) cout << "ABC\n";
    else if(n < 2800) cout << "ARC\n";
    else cout << "AGC\n";
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rrep(i, n) for(int i = (n-1); i >= 0; i--)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL<<62;
const int IINF = 1000000000;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using Graph = vector<vector<int>>;

typedef pair<int, int> P;

struct UnionFind {
    vector<int> d;

    UnionFind(int N=0) : d(N, -1){} 

    int find(int x) {
        if (d[x] < 0) return x;
        return d[x] = find(d[x]);
    }

    bool unite(int x, int y) {
        x = find(x); 
        y = find(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 find(x) == find(y);
    }

    int size(int x){
        return -d[find(x)];
    }
};

int main(){

    int N,M; cin >> N >> M;
    vector<pair<int,int>> edges;
    rep(i,M){
        int A, B; cin >> A >> B;
        A--;
        B--;
        edges.emplace_back(A,B);
    }

    UnionFind uf(N);
    rep(i,M){
        uf.unite(edges[i].first, edges[i].second);
    }
    int ans = 0;
    rep(i,N){
        ans = max(ans,uf.size(i));
    }
    cout << ans << endl;
}

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

typedef long long ll;

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

template<class T>
std::istream &operator >> (std::istream &in, std::vector<T> &tgt){
  T input; in >> input;
  tgt.push_back(input);
  return in;
}

template<class T>
std::ostream &operator << (std::ostream &out, std::vector<T> &tgt){
  for(int i=0;i<tgt.size();i++)
    out << tgt.at(i) << " ";
  return out;
}

int main(){

  cin.tie(0);
  ios::sync_with_stdio(false);

  int n; cin >> n;
  vector<int> a; rep(i,n) cin >> a;
  vector<int> b; b = a;

  int c=0;
  int min=0;
  rep(i,32){
    rep(j,n){
      c += b[j] & 1;
      b[j] = b[j] >> 1;
    }
    if(n%3==0){
      if(c&&c!=2*n/3){
        cout << "No" << endl;
        return 0;
      }else if(!min&&c==2*n/3){
        min=i+1;
      }
    }else{
      if(c){
        cout << "No" << endl;
        return 0;
      }
    }
    c=0;
  }

  cout << "Yes" << endl;
  return 0;
}

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

int main(){
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int N, r[4] = {2, 3, 4, 6}; cin >> N;

    unordered_set<int> S;
    if(N<=5){
        S.emplace(2), S.emplace(3), S.emplace(25);
        if(N>=4) S.emplace(30);
        if(N==5) S.emplace(60);
    }
    else{
        int cur = 0, last = -1;
        for(int i=0;i<(N+3)/4;i++){
            for(int j=0;j<4 and 4*i+j<N;j++){
                last = 6 * i + r[j];
                S.emplace(last), cur += last;
            }
        }
        if(cur%6==2) S.erase(8), S.emplace((last+5)/6*6);
        else if(cur%6==3) S.erase(9), S.emplace(last%6==0 ? last+6 : last+2);
        else if(cur%6==5) S.erase(9), S.emplace(last%6==3 ? last+1 : last+2);
    }
    for(int e : S) cout << e << ' ';
    cout << endl;
    return 0;
}

Measured memory_bytes: 1280
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;

double dist(double x, double y) {
    return sqrt(x*x+y*y);
}

int main() {
    long long a,b,h,m;
    cin >> a>>b>>h>>m;

    double rad = 2*M_PI*m/60.0;
    double xb = b * sin(rad);
    double yb = b * cos(rad);

    rad = 2*M_PI * (h+m/60.0)/12.0;
    double xa = a * sin(rad);
    double ya = a * cos(rad);

    double ans = dist(xa-xb, ya-yb);
    //cout << ans << endl;
    printf("%.12f\n", ans);
}


Measured memory_bytes: 4060
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;

const int INF = 1 << 25;
int t[20][20];
int dp[1 << 16];

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

	int N;
	while(cin >> N, N) {
		for(int i = 0; i < N; i++) {
			for(int j = 0; j < N + 1; j++) {
				cin >> t[i][j];
			}
		}

		fill(dp, dp + (1 << N), INF);
		dp[0] = 0;
		for(int i = 0; i < 1 << N; i++) {
			for(int j = 0; j < N; j++) {
				if(i & (1 << j)) continue;
				int mint = t[j][0];
				for(int k = 0; k < N; k++) {
					if(i & (1 << k))
						mint = min(mint, t[j][k + 1]);
				}
				dp[i | (1 << j)] = min(dp[i | (1 << j)], dp[i] + mint);
			}
		}

		cout << dp[(1 << N) - 1] << endl;
	}
}

Measured memory_bytes: 1452
class UnionFind():
  def __init__(self, n):
    self.par = [i for i in range(n+1)]
    self.rank = [0] * (n+1)
    
  def find(self, x):
    if self.par[x] == x:
      return x
    else:
      self.par[x] = self.find(self.par[x])
      return self.par[x]

  def unite(self, x, y):
    x = self.find(x)
    y = self.find(y)
    if self.rank[x] < self.rank[y]:
      self.par[x] = y
    else:
      self.par[y] = x
      
    if self.rank[x] == self.rank[y]:
      self.rank[x] += 1

  def same(self, x, y):
    return self.find(x) == self.find(y)
  
N,M=map(int,input().split())
P=list(map(int,input().split()))
UF=UnionFind(N)

for i in range(M):
  x,y=map(int,input().split())
  UF.unite(x,y)
  
Ans=0
  
for i in range(1,N+1):
  if UF.same(P[i-1],UF.par[i]):
    Ans+=1

print(Ans)


Measured memory_bytes: 20152
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main(){
  string a,b,c;
  cin>>a>>b>>c;
  int A=a.size();
  int B=b.size();
  if(a[A-1]==b[0]&&b[B-1]==c[0]){
      cout<<"YES"<<endl;
  }
  else{
    cout<<"NO"<<endl;
  }
  
  
    return 0;
}


Measured memory_bytes: 256
# 最小値に置き換える関数
def chmin(a, b):
    if a > b:
        a = b
        return True
    
    return False

# 入力
N = int(input())
H = [int(i) for i in input().split()]

# dpテーブルの生成
dp = [float("inf") for i in range(N)]
# 初期条件のセット(足場1)
dp[0] = 0

# N番目の足場までのループ
# i = 1のときはi-2がないことに注意
for i in range(N):
    # ノードi-1からの遷移を計算
    dp[i] = min(dp[i], dp[i-1]+abs(H[i] - H[i-1]))
    if i > 1:
        dp[i] = min(dp[i], dp[i-2]+abs(H[i] - H[i-2]))

# dpテーブルの末尾が答え
print(dp[N-1])

Measured memory_bytes: 14052
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>

using namespace std;

int main(void) {
	int n;
	cin >> n;
	int num = 0;
	int t = n;
	for (int i = 0; i < 9; i++) {
		num += t % 10;
		t /= 10;
		if (num != 0 && i == 8 && n%num == 0) {
			cout << "Yes";
			return 0;
		}
	}
	cout << "No";
	return 0;
}

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

int n;
vector<int> a;

int solve(int x, int now);

int main() {
  cin >> n;
  for(int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    a.push_back(x);
  }
  cout << min(solve(0, 1), solve(1, 1)) << endl;
  return 0;
}

int solve(int x, int now) {
  int ans = 0;
  while(now < a.size() &&
        (a[now - 1] < a[now]) == (now + x) % 2)
    ++now;
  if(now == a.size()) return 0;
  swap(a[now], a[now - 1]);
  ans = solve(x, now + 1);
  swap(a[now], a[now - 1]);
  if(now + 1 < a.size() &&
     (a[now] < a[now + 1]) != (now + 1 + x) % 2 &&
     (a[now - 1] < a[now + 1]) == (now + x) % 2) {
    swap(a[now], a[now + 1]);
    ans = min(ans, solve(x, now + 1));
    swap(a[now], a[now + 1]);
  }
  return ans + 1;
}


Measured memory_bytes: 6560
n = int(input())
a_i = [int(_) for _ in input().split()]

mod = 1000000007
fact = 1
for i in range(n):
    fact = (fact * (i + 1)) % mod

s = [0] * n
s[0] = 1 * fact
for i in range(n-1):
    s[i+1] = (s[i] + (fact * pow(i+2, mod-2, mod)) % mod) % mod

ans = 0
for j in range(n):
    p = (s[j] + s[n-j-1] - fact) % mod
    ans += (p * a_i[j]) % mod
    ans = ans % mod

print(ans)


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

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

    ll a[3 * n];
    for(int i = 0; i < 3 * n; i++){
        cin >> a[i];
    }

    sort(a, a + (3 * n));

    ll ans = 0;
    int k = 3 * n - 2;
    for(int i = 0; i < n; i++){
        ans += a[k];
        k -= 2;
    }

    cout << ans << endl;
}

Measured memory_bytes: 2560
#include <iostream>
#include <algorithm>
#include <vector>
typedef long long int ll;
using namespace std;
int main(){
	int n,cnt=0;
	ll h,a,maxat = 0;
	cin >> n >> h;
	vector<ll> t(n);
	for(int i=0;i<n;++i){
		cin >> a >> t[i];
		maxat = max(a,maxat);
	}
	sort(t.begin(),t.end(),[](const ll a,const ll b){return a > b;});
	
	for(ll i : t){
		if(i >= maxat){
			h -= i;
			++cnt;
			if(h <= 0){
				cout << cnt << endl;
				return 0;
			}
		}
	}
	
	if(h <= 0){
		cout << cnt << endl;
		return 0;
	}
	cout << cnt + (h / maxat + (h % maxat ? 1 : 0)) << endl;
	
}

Measured memory_bytes: 1024
import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong();
    long sum = 0;
    for (long i = 1; i <= n; i++) {
      if (i%3 != 0 && i%5 != 0) {
        sum = sum + i;
      }
    }
    System.out.println(sum);
  }
}

Measured memory_bytes: 27192
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0;i<int(n);++i)
using namespace std;
template<class A>void pr(A a){cout << (a) << endl;}
template<class A,class B>void pr(A a,B b){cout << a << " "  ;pr(b);}
template<class A,class B,class C>void pr(A a,B b,C c){cout << a << " " ;pr(b,c);}
template<class A,class B,class C,class D>void pr(A a,B b,C c,D d){cout << a << " " ;pr(b,c,d);}
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
int INF=numeric_limits<int>::max();
// for copy
// for(int i=0;i<N;++i) {

ll facMod(ll x, ll y, ll MOD) {
  if(x==y) {
    return y;
  }
  return (x * facMod(x-1, y, MOD))%MOD;
}

ll repeatSquaring(ll n, ll p, ll MOD) { // nのp乗 mod m
  if(p==0) return 1;
  if(p%2==0) {
    ll t = repeatSquaring(n, p/2, MOD);
    return t*t % MOD;
  } else {
    return (n * repeatSquaring(n, p-1, MOD))%MOD;
  }
}

// ll repeatSquaring(ll n,ll p,ll MOD) {
//   ll res=1;
//     while(p>0) {
//       if(p&1) res =res * n % MOD; // pが1の場合のみこの操作を行う(?)
//       n = n * n % MOD;
//       p >>= 1;
//     }
//     return res;
// }

int main(void) {
  ll n,a,b;cin>>n>>a>>b;
  // --a;
  // --b;
  ll MOD=pow(10,9)+7;

  ll x,y;
  x = facMod(n,n-a+1,MOD);
  y = facMod(a,1,MOD);
  ll nota = (x*repeatSquaring(y,MOD-2,MOD))%MOD;
  // pr(repeatSquaring(y,MOD-2,MOD)%MOD);

  x = facMod(n,n-b+1,MOD);
  y = facMod(b,1,MOD);
  ll notb = (x*repeatSquaring(y,MOD-2,MOD))%MOD;
  // pr(repeatSquaring(y,MOD-2,MOD)%MOD);

  pr((repeatSquaring(2,n,MOD)-(nota+notb)%MOD-1+2*MOD)%MOD);
  return 0;
}


Measured memory_bytes: 6528
#include <bits/stdc++.h>
#define range(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define rep(i,n) range(i,0,n)
using namespace std;

string s;

int main(void){
	while(cin >> s){
		if(cin.eof()) break;
		string out="";
		int n=s.size();
		rep(i,n){
			if(s[i]=='@'){
				int m=s[i+1]-'0';
				rep(j,m) out+=s[i+2];
				i+=2;
			}else
				out+=s[i];
		}
		cout << out << endl;
	}
	return 0;
}

Measured memory_bytes: 1192
use maplit::hashmap;
use petgraph::unionfind::UnionFind;
use proconio::input;
use proconio::marker::Usize1;

fn main() {
    input! {
        n: usize,
        abs: [(Usize1, Usize1)],
    }

    let mut uf = UnionFind::<usize>::new(n);
    for (a, b) in abs {
        uf.union(a, b);
    }

    let mut counts = hashmap!();
    for label in uf.into_labeling() {
        *counts.entry(label).or_insert(0) += 1;
    }

    println!("{}", *counts.values().max().unwrap());
}


Measured memory_bytes: 10380
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=(n)-1;i>=0;i--)
#define VSORT(v) sort(v.begin(), v.end())
#define SORT(v, n) sort(v, v+n)
#define VREV(v,n) reverse(v,v+n)
#define REV(v,n) reverse(v,v+n)
#define vi vector<int>
#define pf push_front
#define pb push_back
#define P pair<int,int>
#define Pll pair<ll,ll>

template<class T, size_t N> size_t countof(const T (&array)[N]) { return N; }
template <class T> void chmin(T&a,const T&b) { a = min(a,b); }
template <class T> void chmax(T&a,const T&b) { a = max(a,b); }
 
void print(){cout<<"\n";}
template <class Head, class... Tail>
void print(Head&& h,Tail&&... t){ 
	if(sizeof...(t)==0)
		cout<<h;
	else
		cout<<h<<' ';
	print(move(t)...);
}

void mprint(auto *matrix,auto N){
	REP(i,N) cout<<matrix[i]<<(i!=N-1?' ':'\n');
	return;
}

const double EPS =1e-9;
const int INF =2e+9;
const ll INFL =9e+18;
const long MOD =1e+9+7;
#define PI 3.14159265258979

#define iP pair<int,P>
int dy[]={0, 0, 1, -1, 1, 1, -1, -1};
int dx[]={1, -1, 0, 0, 1, -1, -1, 1};

inline int in(){int x; cin>>x; return x;}


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

	char S[300][300];
	int N,result=0;

	cin>>N;
	REP(i,N)
		REP(j,N) cin>>S[i][j];

	REP(B,N){
		REP(i,N){ 
			REP(j,N){
				if(S[i][(j+B)%N]!=S[j][(i+B)%N]) goto Exit;
			}
		}
		result+=N;
	Exit:
		result+=0;
	}
	print(result);	
	return 0;
}



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

int main(void){
    
    int W,H,x,y,r;
    
    while(cin >> W >> H >> x >> y >> r)
    {
        if(((0<= x - r) && (x + r) <= W) && ((0<= y - r) && (y + r) <= H))
        {
            cout << "Yes" << endl;
        }else
        {
            cout << "No" << endl;
        }
    }
}



Measured memory_bytes: 3100
use std::io;

fn read_line() -> String {
    let mut s = String::new();
    io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

fn main() {
    let input:Vec<i64> = read_line().split_whitespace().map(|x|x.parse::<i64>().unwrap()).collect::<Vec<i64>>();
    let N = input[0];
    let Y = input[1];

    let mut senen = 0;
    let mut gosenen = 0;
    let mut itimanen = 0;

    for i in 0..(N+1) {
        for j in 0..(N+1) {
            let result = (Y-((i*10000)+(j*5000)))/1000;
            if 0 <= result && (i+j+result) == N{
                senen = result;
                gosenen = j;
                itimanen = i;
            }
        }
    }

    if senen == 0 && itimanen == 0 && gosenen ==0 {
        println!("-1 -1 -1");
    }else{
        println!("{} {} {}",itimanen,gosenen,senen);
    }
}

Measured memory_bytes: 4352