akhauriyash/Code-Regression huggingface dataset:
package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

var (
	readString func() string
	stdout     *bufio.Writer
)

func init() {
	readString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+11))
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = readInt()
	}
	return b
}

func readLengthAndSlice() []int {
	n := readInt()
	return readIntSlice(n)
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func minInt(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func minUint(a, b uint) uint {
	if a < b {
		return a
	}
	return b
}

func minInt64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func minUint64(a, b uint64) uint64 {
	if a < b {
		return a
	}
	return b
}

func maxInt(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func maxUint(a, b uint) uint {
	if a < b {
		return b
	}
	return a
}
func maxInt64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func maxUint64(a, b uint64) uint64 {
	if a < b {
		return b
	}
	return a
}

func absInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func absInt64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

// readString() string
// readInt() int
// readIntSlice(n int) []int
// readLengthAndSlice() []int
func main() {
	defer stdout.Flush()
	s := readString()
	printf("%s%d%s\n", s[:1], len(s)-2, s[len(s)-1:])
}


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

using namespace std ;

const int MAX = 1e5 + 10 ;

int arr[MAX] ;
int n , m ;

long long ans = 0ll ;

int main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n>>m ;
	vector< pair<long long , int> >vp ;
	for(int i = 0 ; i < n ; ++i)
	{
		long long x ;
		cin>>x ;
		vp.push_back({x , 0}) ;
	}
	for(int i = 0 ; i < m ; ++i)
	{
		long long x ;
		cin>>x ;
		vp.push_back({x , 1}) ;
	}
	n++,m++;
	sort(vp.begin() , vp.end()) ;
	for(int i = 0 ; i < vp.size() && n && m ; ++i)
	{
		if(vp[i].second == 0)
			ans += m*1ll*vp[i].first , n-- ;
		else
			ans += n*1ll*vp[i].first , m-- ;
	}
	return cout<<ans<<"\n" , 0 ;
}		

Measured memory_bytes: 6512
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define min(a, b) (a > b ? b : a)
#define chmax(a, b) a = (a >= b ? a : b)
#define chmin(a, b) a = (a <= b ? a : b)
#define out(a) cout << a << endl
#define outa(a, n) { rep(_, n) cout << a[_] << " "; cout << endl; }
#define outp(a, n) { cout << endl; rep(_, n) cout << a[_].F << " " << a[_].S << endl; }
#define SZ(v) (int)v.size()
#define inf (int)(1e9+7)
#define abs(x) (x >= 0 ? x : -(x))
#define ceil(a, b) a / b + !!(a % b)
#define FIX(a) fixed << setprecision(a)
#define LB(v, n) (int)(lower_bound(all(v), n) - v.begin())
#define UB(v, n) (int)(upper_bound(all(v), n) - v.begin())
ll pow(ll a, int b) { return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1; }
ll nck(int n, int k) { ll ret = 1; rep(i, k) {ret *= n - i; ret /= i + 1; } return ret; } // n <= 60
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }





int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    
    
    
    
    ll n;
    cin >> n;
    bitset<61> nn = n;
    
    
    ll dp[61][3] = {0}; // 桁, nまでj * 2^d残り
    // 59にしてて事故った
    dp[60][0] = 1;
    
    const ll mod = 1e9 + 7;
    
    for (int i = 59; i >= 0; --i) {
        rep(j, 3) { // i+1, j
            rep(k, 3) { // a, bのその桁の和
                int s = 2 * j + nn[i] - k;
                if (s < 0) continue; // 残ってない
                
                if (s >= 3) s = 2; // 等号入れ忘れて永遠に事故ってた
                (dp[i][s] += dp[i + 1][j]) %= mod;
            }
        }
    }
    
    ll ans = 0;
    rep(i, 3) (ans += dp[0][i]) %= mod;
    out(ans);
    
    // for (int i = 59; i >= 0; --i) { for (int j = 0; j < 3; ++j) cout << dp[i][j] << " "; cout << endl; }
}




Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <time.h>
#include <list>

using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define reps(x,c) for(auto x:c)
#define all(x) begin(x), end(x)
#define pb(a) emplace_back(a) 
#define mp(a,b) make_pair((a),(b))
#define pl pair<ll,ll>
#define INF 2147483600
#define MOD 1000000007
#define fi first
#define se second
#define dbg(x) cout << #x"="<< (x) << endl
#define dbgv(x) cout<<#x<<endl;reps(y,x){cout<<y<<" ";}cout<<endl

ll dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
pl path[60][60];
char table[60][60];
ll flg[60][60];
int main(){
  ll h,w;cin>>h>>w;
  rep(i,h){
    rep(j,w){
      cin>>table[i+1][j+1];
    }
  }

  flg[1][1]=1;
  pl now=mp(1,1);
  pl gl=mp(h,w);
  queue<pl> q;
  q.push(now);
  bool goal_flg=false;
  while(!q.empty()){
    now=q.front();q.pop();
    if(now==gl){
      goal_flg=true;
      break;
    }
    rep(i,4){
      if(flg[now.fi+dx[i]][now.se+dy[i]]==0&&table[now.fi+dx[i]][now.se+dy[i]]=='.'){
        flg[now.fi+dx[i]][now.se+dy[i]]=1;
        q.push(mp(now.fi+dx[i],now.se+dy[i]));
        path[now.fi+dx[i]][now.se+dy[i]]=now;
      }
    }
  }

  if(goal_flg==false){
    cout<<-1<<endl;
    return 0;
  }

  now.fi=h;
  now.se=w;
  pl next;
  table[h][w]='X';
  while(!(now.fi==1&&now.se==1)){
    next.fi=path[now.fi][now.se].fi;
    next.se=path[now.fi][now.se].se;
    table[next.fi][next.se]='X';
    now.fi=next.fi;
    now.se=next.se;
  }

  ll ans=0;
  FOR(i,1,h+1){
    FOR(j,1,w+1){
      if(table[i][j]=='.'){
        ans++;
      }
    }
  }
  cout<<ans<<endl;
  return 0;
}

Measured memory_bytes: 384
object Main {
  def main(args: Array[String]): Unit = {
    solve
  }

  def solve(): Unit = {
    val sc = new java.util.Scanner(System.in)
    val n = sc.nextInt
    sc.nextLine
    //    var list = Array.fill(n)(sc.nextInt)
    var list = sc.nextLine.split(" ").map(_.toInt)

    var count = 1
    var direction = 0
    var before = list(0)
    for (i <- 1 until n) {
      val num = list(i)
      if (before != num) {
        if (direction == 0) {
          direction = before - num // 方向を確定
        } else {
          if ((direction > 0 && before < num) || (direction < 0 && before > num)) {
            direction = 0
            count += 1
          }
        }
      }
      before = num
    }

    println(count)
  }
}


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

int main() {
	int a, b, c, d;
	cin >> a >> b >> c >> d;
	int x, y;
	x = (a + d - 1) / d; //後攻の攻撃回数
	y = (c + b - 1) / b; //先行の攻撃回数
	if (y <= x) cout << "Yes" << endl;
	else cout << "No" << endl;
}

Measured memory_bytes: 3620
def calc(D):
        global N
        global s
        n = list(range(1,N+1))
        v=0
        for i in range(N):
                p=n.index(D[i])
                v += s[N-i-1] * p
                n.pop(p)
        return v
        
N = int(input())
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]
n = list(range(1,N+1))
s = [0]*(N+1)
s[0] = n[0]

for i in range(1,N+1): s[i] = s[i-1] * n[i-1]

print(abs(calc(P)-calc(Q)))

Measured memory_bytes: 3064
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n;
string str;

int main(void){
	scanf("%d",&n);
	cin >> str;
	int res=0;
	int cnt=0;
	for(int i=0;i<n;i++){
		if(str[i]=='o')cnt=0;
		else cnt++;
		if(cnt==2)break;
		res++;
	}
	printf("%d\n",res);
	return 0;
}

Measured memory_bytes: 3468
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<utility>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<math.h>
#include <map>
using namespace std;
typedef long long int ll;
#define repi(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,a) repi(i,0,a)
#define all(u) u.begin(),u.end()
#define pl pair<ll,ll>
#define fi first
#define se second
#define inf 1000000007

string S;
ll K;
ll len1 = 0;
char s;

int main() {
    cin >> S >> K;
    rep(i,S.size()){
        if (S[i] == '1') len1++;
        else {
            s = S[i];
            break;
        }
    }
    if (len1 >= K) cout << 1 << endl;
    else{
        cout << s << endl;
    }
    
    
    return 0;
}


Measured memory_bytes: 256
// No.194619011720
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;
typedef long long ll;

ll merge(ll l1, ll r1, ll l2, ll r2) {
    //cout << l1 << " " << r1 << " " << l2 << " " << r2 << endl;
    if (l1 > l2) swap(l1, l2), swap(r1, r2);
    if (l2 > r1) return r1 - l1 + r2 - l2 + 2;
    else return max(r1, r2) - l1 + 1;
}

int main() {
#ifdef LOCAL_JUDGE
    freopen(".in", "r", stdin);
    freopen(".out", "w", stdout);
#endif

    ll a, b;

    cin >> a >> b;

    if (a == b) return puts("1"), 0;

    int p = 59;

    while (p >= 0 && ((a >> p) & 1) == ((b >> p) & 1)) --p;

    int q = p - 1;
    while (q >= 0 && !((b >> q) & 1)) --q;

    a &= (1ll << p) - 1;
    b &= (1ll << (p + 1)) - 1;

    ll l = a, r = (1ll << p) + (1ll << (q + 1)) - 1;

    cout << merge(l, r, (1ll << p) + a, (1ll << (p + 1)) - 1) << endl;

    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#define MOD 1000000007
#define mkp make_pair
typedef long long ll;
using namespace std;

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

  cout<<3*r*r<<endl;

  return 0;
}


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

int n, A[MAX], L[MAX];

int lis() {
  L[0]=A[0];
  int length=1;
  for (int i=1; i<n; i++) {
    if (L[length-1]<A[i]) L[length++]=A[i];
    else *lower_bound(L, L+length, A[i])=A[i];
  }
  return length;
}

int main() {
  scanf("%d", &n);
  for (int i=0; i<n; i++) scanf("%d", &A[i]);
  int res=lis();
  printf("%d\n", res);

  return 0;
}



Measured memory_bytes: 3908
fn main() {
    let s = read::<String>();
    let ans = match &s[..] {
        "SUN" => 7,
        "MON" => 6,
        "TUE" => 5,
        "WED" => 4,
        "THU" => 3,
        "FRI" => 2,
        "SAT" => 1,
        _ => 1,
    };
    println!("{}", ans);
}

#[allow(dead_code)]
fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}
#[allow(dead_code)]
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
#[allow(dead_code)]
fn read_vec2<T: std::str::FromStr>(n: u32) -> Vec<Vec<T>> {
    (0..n).map(|_| read_vec()).collect()
}
#[allow(dead_code)]
fn yn(result: bool) {
    if result {
        println!("Yes");
    } else {
        println!("No");
    }
}


Measured memory_bytes: 4352
//#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
using namespace std;
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<long long> vlong;
typedef vector<string> vstring;
#define vpush(a,x) a.push_back(x);
#define rep(i, n) REP(i, 0, n)
#define all(v) v.begin(), v.end()
#define REP(i, x, n) for(int i = x; i < n; i++)
const int INF = 1 << 30;
const int dx[] = {1,0,-1,0,1,1,-1,-1};
const int dy[] = {0,-1,0,1,1,-1,-1,1};
#define stp(x)  setprecision(x)
const long double PI=3.14159265358979323846264338327950;

struct dsu {
  public:
    dsu() : _n(0) {}
    dsu(int n) : _n(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }

  private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
};

int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n,q;
  cin>>n>>q;
  dsu uf(n);
  rep(i,q){
    int t,u,v;
    cin>>t>>u>>v;
    if(t==0){
      uf.merge(u,v);
    }
    else{
      if(uf.same(u,v)){
        cout<<1<<'\n';
      }
      else{
        cout<<0<<'\n';
      }
    }
  }
  return(0);
}

Measured memory_bytes: 3988
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, k = LI()
B = [0] * n
for _ in range(k):
    d = I()
    A = LI()
    for a in A:
        B[a - 1] = 1

ans = 0
for i in range(n):
    if B[i] == 0:
        ans += 1

print(ans)


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

#define ll long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int (i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int (i) = (m); (i) < (n); ++(i))
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y) cerr<<"("<<#x<<", "<<#y<<") = "<<"("<<x<<", "<<y<<")"<<endl;
#define debug3(x, y, z) cerr<<"("<<#x<<", "<<#y<<", "<<#z<<") = "<<"("<<x<<", "<<y<<", "<<z<<")"<<endl;
#define debugB(x, y) cerr<<#x<<": "<<bitset<y>(x) << endl;
#define line() cerr << "---------------" << endl;

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

template<typename T>
void printA(vector<T> &printArray, char between = ' ') {
   int paSize = printArray.size();
   for (int i = 0; i < paSize; i++) { cerr << printArray[i] << between;}
   if (between != '\n') {cerr << endl;}
}

// ------------------------------------------------------------------------------------------

int main() {
   int n;
   cin >> n;
   int sum = 0;
   rep (i, n) {
      int x;
      cin >> x;
      sum += (x&1);
   }

   if (sum&1) {
      puts("NO");
   } else {
      puts("YES");
   }

   return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main() {

  int N, M;
  cin >> N >> M;
  vector<double> vote(N);
  for(int i = 0; i < N; i++){
    cin >> vote.at(i);
  }
  sort(vote.begin(),vote.end());
  reverse(vote.begin(), vote.end());
  bool answer = true;
  double sum = 0;
  for(int i = 0; i < N; i++){
    sum += vote.at(i);
  }
  
  for(int i = 0; i < M; i++){
    if(vote.at(i) < sum/(4*M)){
      answer = false;
    }
  }
  
  if (answer == true ){
    cout << "Yes" << endl;
  }
  else{
    cout << "No" << endl;
  }

}

Measured memory_bytes: 256
#include<iostream>
#include<queue>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
using namespace std;
/****************************/
#define ll long long
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define qi queue<int>
#define mii map<int,int>
#define msi map<string,int>
#define vs vector<string>
#define vc vector<char>
/****************************/
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rep2(i,n) for(ll i=(n)-1;i>=0;i--)
#define rep3(i,a,n) for(ll i=(a);i<(n);i++)
#define rep4(i,a,n) for(ll i=(a)-1;i>=n;i--)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define pi 3.1415926535897932384626433832795
#define zero(n) memset(n,0,sizeof(n));
#define SZ(n) n.size()
#define sn st.nd 
ll dx[4]={0,-1,0,1};
ll dy[4]={1,0,-1,0};
/****************************/
string s;
ll n,m,k;
vi v;
vi uni(vi v)
{
	vi ans=v;
	sort(ans.begin(),ans.end());
	ans.erase(unique(ans.begin(),ans.end()),ans.end());
	return ans;
}
int quickpower(int m,int n)
{
    if(n==1) return m;
    int temp=quickpower(m,n/2);
    return (n%2==0 ? 1 : m)*temp*temp;
}
struct node
{
	int a,b,c,d;
};
int main()
{
	ios::sync_with_stdio(0);
	cin>>n>>m>>k;
	ll x=min(m-1,k);
	double d=(n*1.000000*x/(m*1.000000));
	double e=x*1.000000/(m*1.000000);
	cout<<floor(d)-n*floor(e);
	return 0;
}

Measured memory_bytes: 3796
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000001
using namespace std;

struct Info{
	void init(){
		cost = BIG_NUM;
	}
	int row,col,cost;
};

int main(){

	int W,H,first,minimum = BIG_NUM,minimum_first,next,current;
	Info start,goal;
	vector<Info> V[6];
	char buf[1001];

	while(true){
		scanf("%d %d",&W,&H);
		if(W == 0 && H == 0)break;

		for(int i = 1; i <= 5; i++)V[i].clear();

		for(int i = 0; i < H; i++){
			scanf("%s",buf);
			for(int k = 0; buf[k] != '\0'; k++){
				if(buf[k] == '.'){
					//Do nothing
				}else{
					if(buf[k] < 58){
						Info info;
						info.row = i;
						info.col = k;
						V[buf[k] - '0'].push_back(info);
					}else{
						if(buf[k] == 'S'){
							start.row = i;
							start.col = k;
						}else{
							goal.row = i;
							goal.col = k;
						}
					}
				}
			}
		}

		minimum = BIG_NUM;

		for(first = 1; first <= 5; first++){
			for(int i = 1; i <= 5; i++){
				for(int k = 0; k < V[i].size();k++)V[i][k].init();
			}
			goal.cost = BIG_NUM;

			if(first == 5){
				next = 1;
			}else{
				next = first+1;
			}

			for(int i = 0; i < V[next].size(); i++){
				V[next][i].cost = abs(start.row-V[next][i].row)+abs(start.col-V[next][i].col);
			}

			current = next;

			for(int i = 1; i <= 3; i++){
				next++;
				if(next == 6)next = 1;

				for(int a = 0; a < V[current].size(); a++){
					for(int b = 0; b < V[next].size();b++){
						V[next][b].cost = min(V[next][b].cost,V[current][a].cost+abs(V[next][b].row-V[current][a].row)+abs(V[next][b].col-V[current][a].col));
					}
				}
				current = next;
			}

			for(int i = 0; i < V[current].size();i++){
				goal.cost = min(goal.cost,V[current][i].cost+abs(goal.row-V[current][i].row)+abs(goal.col-V[current][i].col));
			}

			if(goal.cost < minimum){
				minimum = goal.cost;
				minimum_first = first;
			}
		}

		if(minimum == BIG_NUM){
			printf("NA\n");
		}else{
			printf("%d %d\n",minimum_first,minimum);
		}
	}

	return 0;
}

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

int main(void)
{
    int h, w;

    while (true)
    {

        cin >> h >> w;
        if (h == 0 && w == 0)
            break;
        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                if ((i + j) % 2 == 0)
                {
                    printf("#");
                }
                else
                {
                    printf(".");
                }
            }
            printf("\n");
        }
        printf("\n");
    }
}


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

void makeDivisors(ll a, vector<ll> &A) {
    ll n = pow(a, 0.5);
    for (ll i = 1; i <= n; i++) {
        if (a % i == 0) {
            A.push_back(i);
            if (i != a / i) {
                A.push_back(a / i);
            }
        }
    }
}

int main()
{
    ll n, m;
    cin >> n >> m;

    vector<ll> A;
    makeDivisors(m, A);

    sort(A.begin(), A.end(), greater<ll>());
    
    ll i = 0;
    while(1) {
        if (A[i] * n <= m) {
            cout << A[i] << endl;
            return 0;
        }
        i++;
    }

    return 0;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
 
int main() {
	ios::sync_with_stdio(0),
	cin.tie(0), cout.tie(0);
 
	long long int a[5];
	int sum,t;
	while(cin >>a[0]){
		t=0;
		sum=0;
		for(int i=1;i<5;i++){
			cin >>a[i];
		}
		for(int i=0;i<5&&t==0;i++){
			sum++;
			if(a[i]==0){
				cout <<sum<<'\n';
				t=1;
			}
		}
	}
	return 0;
}

Measured memory_bytes: 3652
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
int main()
{
	int a[3];
	scanf("%d%d%d",&a[0],&a[1],&a[2]);
	sort(a,a+3);
	int b=0;
	for (int i=2;i>0;i--)
	{
		b+=abs(a[i]-a[i-1]);
	}
	printf("%d",b);
	return 0;
}

Measured memory_bytes: 128
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#define popcount __builtin_popcount
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main()
{
    int n; cin>>n;
    int w[110];
    for(int i=0; i<n; i++) cin>>w[i];
    int ans=1e9;
    for(int t=0; t<n-1; t++){
        int s1=0, s2=0;
        for(int i=0; i<n; i++){
            if(i<=t) s1+=w[i];
            else s2+=w[i];
        }
        ans=min(ans, abs(s1-s2));
    }
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=203,INF=1<<29;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    string S;cin>>S;
    int ans=0,now=0;
    
    for(int i=0;i<S.size();i++){
        if(S[i]=='L') now--;
        else now++;
        
        if(S[i]=='L'&&now<0&&abs(now)%4==0) now=0;
        if(S[i]=='R'&&now==4){
            ans++;
            now=0;
        }
    }
    
    cout<<ans<<endl;
}



Measured memory_bytes: 3236
#include<stdio.h>
int main(){

  int n,m;
  scanf("%d%d",&n,&m);
  int a[m],i;
  for(i=0;i<m;i++)scanf("%d",&a[i]);
  int ans=n;

  for(i=0;i<m;i++)ans -=a[i];

  if(ans<0)ans=-1;

  printf("%d\n",ans);
  return 0;
}


Measured memory_bytes: 1772
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

template<class T> inline void chmin(T& a, const T& b) { if(b < a) a = b; }

#define X first
#define Y second

const int INF = 0xfffffff;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
const int MAX_N = 10;

int w, h;
int n;
vector<vector<int> > mat;
vector<int> x, y;
vector<string> field;
int memo[MAX_N + 1][1 << MAX_N];

int bfs(int from, int to) {
	vector<vector<int> > label(h, vector<int>(w, INF));
	queue<pair<int, int> > que;
	que.push(make_pair(x[from], y[from]));
	label[y[from]][x[from]] = 0;

	while(!que.empty()) {
		const int cx = que.front().X, cy = que.front().Y;
		que.pop();

		for(int i = 0; i < 4; ++i) {
			const int nx = cx + dx[i], ny = cy + dy[i];
			if(nx < 0 || ny < 0 || nx >= w || ny >= h || field[ny][nx] == 'x')
				continue;

			if(label[ny][nx] > label[cy][cx] + 1) {
				label[ny][nx] = label[cy][cx] + 1;
				que.push(make_pair(nx, ny));
			}
		}
	}

	return label[y[to]][x[to]];
}

int dfs(int cur, int visited) {
	if(visited == (1 << n) - 1)
		return 0;

	if(memo[cur][visited] != -1)
		return memo[cur][visited];

	int res = INF;
	for(int i = 0; i < n; ++i) {
		if(visited & (1 << i))
			continue;
		
		chmin(res, dfs(i, visited | (1 << i)) + mat[cur][i]);
	}

	return memo[cur][visited] = res;
}

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

	while(cin >> w >> h, w) {
		x.clear();
		y.clear();

		field.clear();
		field.resize(h);
		for(int i = 0; i < h; ++i)
			cin >> field[i];

		for(int i = 0; i < h; ++i) {
			for(int j = 0; j < w; ++j) {
				if(field[i][j] == '*') {
					x.push_back(j);
					y.push_back(i);
				}
			}
		}

		n = x.size();

		for(int i = 0; i < h; ++i) {
			for(int j = 0; j < w; ++j) {
				if(field[i][j] == 'o') {
					x.push_back(j);
					y.push_back(i);
					break;
				}
			}
		}

		mat.clear();
		mat.resize(n + 1, vector<int>(n + 1, INF));
		for(int i = 0; i <= n; ++i)
			mat[i][i] = 0;

		for(int i = 0; i < n; ++i)
			for(int j = i + 1; j <= n; ++j)
				mat[i][j] = mat[j][i] = bfs(i, j);

		memset(memo, -1, sizeof(memo));
		const int ans = dfs(n, 0);
		cout << (ans >= INF ? -1 : ans) << endl;		
	}

	return EXIT_SUCCESS;
}

Measured memory_bytes: 1304
#include <cstdio>
#include <set>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;

char s[5];

int main() {
    scanf("%s", s);
    if(s[0] == 'R' && s[1] == 'R' && s[2] == 'R') {
        printf("3\n");
    } else if(s[0] == 'S' && s[1] == 'S' && s[2] == 'S') {
        printf("0\n");
    } else if(s[0] == 'R' && s[1] == 'R') {
        printf("2\n");
    } else if(s[1] == 'R' && s[2] == 'R') {
        printf("2\n");
    } else {
        printf("1\n");
    }


    return 0;
}


Measured memory_bytes: 3160
#include <iostream>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)

int main() {
char x[1000]; 
std::stack<int> s;// スタックを表す配列
int a,b,c;
     while( scanf("%s", &x) != EOF ){
        if ( x[0] == '+' ){
        a = s.top();s.pop() ;
        b = s.top();s.pop() ;
        s.push(a+b);

    } else if ( x[0] == '-' ){
        a = s.top();s.pop() ;
        b = s.top();s.pop() ;
        s.push(b-a);

    } else if ( x[0] == '*' ){
        a = s.top();s.pop() ;
        b = s.top();s.pop() ;
        s.push(a*b);
    } else {
      s.push(atoi(x));
    }
  }
   printf("%d\n", s.top());

    return 0;
}


Measured memory_bytes: 3208
#include <bits/stdc++.h>
#define rep(i,n)for(long long i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
 
const int MOD=1000000007;
const int INF=0x3f3f3f3f;
const ll INFL=0x3f3f3f3f3f3f3f3f;
ll gcd(ll x, ll y){
  if(y==0) return x;
  else return gcd(y, x%y);
}
ll lcm(ll x,ll y){
  ll g=gcd(x,y);
  return x/g*y;
}

using Graph=vector<vector<int>>;
Graph G;

int main(){
  ll n, k;
  cin >> n >> k;
  vector<ll> num(k,0);
  for(int i=1;i<=n;i++) num[i%k]++;
  
  ll res = 0;
  for(int a = 0; a < k; a++){
    ll b = (k-a)%k;
    ll c = (k-a)%k;
    if((b+c)%k==0) res += num[a]*num[b]*num[c];
  }
  
  cout << res << endl;
  
  
}
    


Measured memory_bytes: 1792
#include <stdio.h>
int main(void){
    int p[3],alt;
    int i;
    for(i=0;i<3;i++){
        scanf("%d",&p[i]);
    }
    for(i=0;i<2;i++){
        if(p[i]>=p[i+1]){
            alt=p[i];
            p[i]=p[i+1];
            p[i+1]=alt;
        }
    }   
    printf("%d\n",p[0]+p[1]);
    return 0;
}

Measured memory_bytes: 128
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <cstdlib>
#include <iomanip>
#define rep(i,x) for(ll i=0;i<x;i++)
#define all(a) (a).begin(),(a).end()
using namespace std;
using ld = long double;
using ll = long long;
const ll INF = 10010010010;
typedef pair<ll, ll> P;
using graph = vector<vector<ll>>;
const ll mod = 1000000007;
ll gcd(ll a, ll b) {//a>b
    if (a < b)swap(a, b);
    if (a % b == 0) return b;
    else return gcd(b, a % b);
}/*フィボナッチテンプレ
ll memo[50];
ll fibo(ll n) {
    if (memo[n] != 0)return memo[n];
    if (n <= 1)return memo[n] = n;
    else return memo[n] = fibo(n - 1) + fibo(n - 2);
}
*/
bool isp(ll n) {
    bool res = true;
    for (ll i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            res = false;
            break;
        }
    }
    return res;
}

int main() {
  ld A,B;
  cin >> A >> B;
  cout << A / B << endl;
}

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

int main() {
	int n;
	cin >> n;
	double s = 0;
	for(int i = 0; i < n; i++) {
		double num;
		string type;
		cin >> num >> type;
		if(type[0]=='J') {
			s += num;
		}else {
			s += num*380000.0;
		}
	}
	cout << s << endl;
	return 0;
}


Measured memory_bytes: 256
from collections import defaultdict
n = int(input())
al = list(map(int, input().split()))


t = 0
d = defaultdict(lambda: 0)
for a in al:
    if a >= 3200:
        t += 1
    else:
        d[str(a // 400)] += 1
if len(d) == 0:
    print(1, t)
else:
    print(len(d), len(d)+t)


Measured memory_bytes: 38384
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using V = vector<int>;
using VV = vector<vector<int>>;
using VVV = vector<vector<vector<int>>>;
using VL = vector<ll>;
using VVL = vector<vector<ll>>;
using VVVL = vector<vector<vector<ll>>>;
using pq = priority_queue<int>;
template<class T>
using P = pair<T, T>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define REP(i,k,n) for(int i=(k);i<(n);i++)
#define all(a) (a).begin(),(a).end()
#define output(x,y) cout << fixed << setprecision(y) << x << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int under = (1 << 31);
int upper = under - 1;
ll UNDER = (1LL << 63);
ll UPPER = UNDER - 1;
const int MOD = 1e9 + 7;
const long double pi = 3.141592653589793;
int main(){
	int n;
	cin >> n;
	string res = (!n) ? "0" : "";
	while (n != 0) {
		int tmp = n % -2;
		n /= -2;
		if (tmp == -1) n++;
		if (tmp == 0) res += '0';
		else res += '1';
	}
	reverse(all(res));
	cout << res << endl;
	return 0;
}

Measured memory_bytes: 256
def main():
    N = int(input())
    n = 0
    for h in range(1, 3501):
        for w in range(1, 3501):
            a = 4*h*w-N*h-N*w
            if a <= 0:
                continue
            b = N*w*h
            if b%a == 0:
                n = b//a
                break
        if n > 0:
            break
    print(h, w, n)

if __name__ == "__main__":
    main()

Measured memory_bytes: 3064
//#define LOCAL

#include <fstream>
#include <iostream>
#include <cmath>
#include <algorithm>

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

using namespace std;

int K;
int c[5000];

int main()
{
#ifdef LOCAL
    ifstream in("input.txt");
    cin.rdbuf(in.rdbuf());
#endif

    while (true) {
        cin >> K;
        if (K == 0) break;

        int sum = 0;
        rep(i,K*(K-1)/2) {
            cin >> c[i];
            sum += c[i];
        }

        cout << sum / (K - 1) << endl;
    }
}

Measured memory_bytes: 3092
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<numeric>
using namespace std;
#define INF (1 << 30) - 1
#define LINF 1LL << 60
const int MOD = 1000000007;
using ll = long long;
using P = pair<int, int>;

vector<int> prime_factors(ll n){
	vector<int> ret;
	for(int i = 2; i * i <= n; ++i){
		if(n % i == 0){
			ret.push_back(i);
			while(n % i == 0)n /= i;
		}
	}
	if(n != 1)ret.push_back(n);
	return ret;
}

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

  auto factors = prime_factors(n);
  int m = factors.size();
  ll ans = 0;
  for(int b = 1; b < (1 << m); ++b){
  	ll mul = 1;
  	int cnt = 0;
  	for(int i = 0; i < m; ++i){
  		if(b >> i & 1){
  			mul *= factors[i];
  			++cnt;
  		}
  	}
  	if(cnt % 2)ans += n / mul;
  	else ans -= n / mul;
  }

  cout << n - ans << endl;
  return 0;
}




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

int main() {
    int a, b;
    cin >> a >> b;
    
    for (int i = 1; i <= 3; i++) {
        if ((a * b * i) % 2 == 1) {
            cout << "Yes" << endl;
            return 0;
        }
    }
    
    cout << "No" << endl;
    return 0;
}

Measured memory_bytes: 256
#pragma GCC optimize("Ofast,inline,unroll-loops,fast-math")
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

int main(void)
{
    string a;

    cin >> a;
    cout << (a == "ABC" ? "ARC" : "ABC") << endl;

    return 0;
}


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

int main() {
	int n;cin>>n;
	int a[n];
	int c4=0;
	int c2=0;
	int c1=0;
	for(int i=0;i<n;i++){
		cin>>a[i];
		if(a[i]%4==0){
			c4++;
		}
		if(a[i]%4==2){
			c2++;
		}
		if(a[i]%2){
			c1++;
		}
	}
	if(c4>=c1){
		cout<<"Yes"<<endl;
	}else if(c1-c4==1){
		if(c2){
			cout<<"No"<<endl;
		}else{
			cout<<"Yes"<<endl;
		}
	}else{
		cout<<"No"<<endl;
	}
	return 0;
}


Measured memory_bytes: 640
#include <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <numeric>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI=3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for(int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y) - 1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))


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

    int n, ma, mb;
    cin >> n >> ma >> mb;
    vs32 a(n), b(n), c(n);
    int sa = 0, sb = 0;
    rep (i, n)
    {
        cin >> a[i] >> b[i] >> c[i];
        sa += a[i];
        sb += b[i];
    }

    int ans = 1e9;
    int times = 1;
    while (ma * times <= sa && mb * times <= sb)
    {
        int Ma = ma * times;
        int Mb = mb * times;
        vector< vector< vs32 > > dp(n + 1, vector<vs32>(Ma + 1, vs32(Mb + 1, 1e9)));
        rep (i, n + 1) dp[i][0][0] = 0;
        rep (i, n)
        {
            rep (j, Ma + 1)
            {
                rep (k, Mb + 1)
                {
                    if (a[i] > j  || b[i] > k)
                    {
                        dp[i + 1][j][k] = dp[i][j][k];
                    }
                    else
                    {
                        dp[i + 1][j][k] = MIN(dp[i][j][k], dp[i][j - a[i]][k - b[i]] + c[i]);
                    }
                }
            }
        }
        if (dp[n][Ma][Mb] != 1e9)
        {
            ans = MIN(ans, dp[n][Ma][Mb]);
        }
        ++times;
    }

    if (ans == 1e9) ans = -1;
    cout << ans << "\n";
    return 0;
}



Measured memory_bytes: 8688
//long time no ac
#include<bits/stdc++.h>
using namespace std;

#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";

#define     SPEED       ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define 	setprecn(x) cout<<fixed<<setprecision(x);
#define 	ll          long long int
#define 	pb          push_back
#define 	pll         pair<ll,ll>
#define 	vll         vector<ll>
#define 	ff          first
#define 	ss          second
#define 	all(a)      (a).begin(),(a).end()
#define 	sz(x)       (ll)x.size()
#define 	hell        1000000007
#define 	rep(i,n)    for(i=0;i<n;i++)
#define 	repe(i,a,b) for(i=a;i<b;i++)
#define 	lbnd        lower_bound
#define 	ubnd        upper_bound
#define 	bs          binary_search
#define 	mp          make_pair

ll power(ll a,ll b)
{
	if(b==0)
		return 1;
	if(b==1)
		return a;
	ll c=power(a,b/2);
	if(b%2==0)
		return((c%hell)*(c%hell))%hell;
	return ((((c%hell)*(c%hell))%hell)*(a%hell))%hell;
}

/********************************************************/
//	d.erase(unique(all(d)), d.end());
ll h[100005],dp[100005],n;


ll f(ll x)
{
	if(x==n-1) return 0;
	if(x>n-1) return hell;
	if(dp[x]==-1)
	{
		dp[x]=min(f(x+1)+abs(h[x+1]-h[x]),f(x+2)+abs(h[x+2]-h[x]));
	}
	return dp[x];
}

int main()
{
	SPEED;
	ll k,t,c=0,i,j,m,cs=0;
	t=1;

	// cin>>t;
	string s;
	while(t--)
	{
		cin>>n;
		// n=s.length();
		// ll a[n];
		rep(i,n)
		{
			cin>>h[i];
			dp[i]=-1;
		}
		c=f(0);
		cout<<c;
		if(t!=0)
	   		cout<<"\n";
	}
	return 0;
}

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

struct UnionFind {
  vector<int> d;
  UnionFind(int n) : d(n, -1) {}
  int root(int x) {
    if(d[x] < 0) return x;
    return d[x] = root(d[x]);
  }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if(x == y) return false;
    if(d[x] > d[y]) swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -d[root(x)]; }
};

int main() {
  ll n, m;
  cin >> n >> m;
  vl p(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  UnionFind uf(n);
  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    uf.unite(x, y);
  }
  ll ans = 0;
  rep(i, n) if(uf.same(i, p[i])) ans++;
  cout << ans << endl;
}

Measured memory_bytes: 4556
#include <bits/stdc++.h>
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/tree_policy.hpp>
    #include <ext/pb_ds/detail/standard_policies.hpp>
    using namespace std;
    using namespace __gnu_pbds;
    #define ll long long
    #define int long long
    #define pb push_back
    #define For(i,n) for(ll  i=0;i<n;i++)
    #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) ;clock_t clk = clock();
    #define f(a,b,c) for(ll a=b;a<c;a++)
    #define ld long double
    #define read(t) ll t;cin>>t;
    #define all(x) x.begin(),x.end()
    #define fi first
    #define se second
    #define pii pair<ll,ll>
    #define endl "\n"
    #define what_is(x) cerr << #x << " is " << x << endl;
    #define time cerr << "Time (in ms): " << double(clock() - clk) * 1000.0 / CLOCKS_PER_SEC << '\n';
    const double pi=3.14159265358979323846264;
    typedef tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
    typedef tree<long long,null_type,greater<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set1;
/*.....................................................................................................................*/

    ll mod =(ll)1e9+7;
    ll add(ll x,ll y) {return (x+y)%mod;}
    ll mult(ll x, ll y){return ((x%mod )* (y%mod)) % mod;}
    long long binpow(long long x, long long p) 
    {
        if (p == 0) return 1;
    	if (p == 2 || (p & 1)) return mult(x, binpow(x, p - 1));
    	return binpow(binpow(x, p / 2), 2);
    }
    ll modinv(ll a) {return binpow(a,mod-2);}

    // bool check(ll y){}
    ll binsearch(ll l,ll r)
    {
        // while(r-l>1)
        // {
        //     ll mid=(l+r)/2;
        //     if(check(mid))l=mid;
        //     else r=mid;
        // }
        return l;
    }
    const ll man=(ll)1e5+5;
    // vector<ll>adj[man];
    // bool vis[man][man];
    // ll d[man],depth[man],par[man];
    // void addedge(ll u,ll v){adj[u].pb(v);adj[v].pb(u);}
    ll dx[4]={1,-1,0,0};ll dy[4]={0,0,1,-1};
/*........................................................................................................................*/

signed main()/*Check flow...question galat mat padhna */
{
    fast;
    char c;
    cin>>c;
    cout<<(char)(c+1)<<endl;
}

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

int main(){
    ll n;
    cin >> n;
    ll p[n];
    for(ll i=0;i<n;i++) cin >> p[i];
    ll res=0;
    for(ll i=0;i<n-1;i++){
        if(p[i]==i+1){
            swap(p[i],p[i+1]);
            res++;
        }
    }
    if(p[n-1]==n) res++;
    cout << res << endl;
    return 0;
}


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

typedef long long  ll;

ll MOD = 1000000007;
ll INF = 100000000;
double PI = 3.1415926535;

template<typename T>
void remove(std::vector<T>& vector, unsigned int index)
{
    vector.erase(vector.begin() + index);
}

vector<int> to[2510];
vector<int> ot[2510];
bool ok1[2510];
bool ok2[2510];
bool ok3[2510];

void dfs(int s){
  if(ok1[s]) return;
  ok1[s] = true;
  for(int i = 0;i < to[s].size();i++){
    dfs(to[s][i]);
  }
}

void dfs2(int s){
  if(ok2[s]) return;
  ok2[s] = true;
  for(int i = 0;i < ot[s].size();i++){
    dfs2(ot[s][i]);
  }
}

int main(){
  ll N,M,P;
  cin >> N >> M >> P;
  vector<tuple<ll,ll,ll>> edges;
  for(int i = 0;i < M;i++){
    ll a,b,c;
    cin >> a >> b >> c;
    a--;b--;
    c = -(c-P);
    edges.emplace_back(a,b,c);
    to[a].push_back(b);
    ot[b].push_back(a);
  }
  dfs(0);
  dfs2(N-1);
  for(int i = 0;i < 2510;i++){
    ok3[i] = (ok1[i] & ok2[i]);
  }
  vector<int> d(N,INF);
  d[0] = 0;
  bool upd = true;
  int step = 0;
  cout << endl;
  while(upd){
    upd = false;
    for(int i = 0;i < M;i++){
      int a = get<0>(edges[i]),b = get<1>(edges[i]),c = get<2>(edges[i]);
      if(!ok3[a]) continue;
      if(!ok3[b]) continue;
      int newD = d[a] + c;
      if(newD < d[b]){
        d[b] = newD;
        upd = true;
      }
    }
    step++;
    if(step > N){
      cout << -1 << endl;
      return 0;
    }
  }
  int ans = max(-d[N-1],0);
  cout << ans << endl;
}

Measured memory_bytes: 768
#include<bits/stdc++.h>
#include<string.h>
 
typedef long long int ll;
#define all(x) (x).begin(), (x).end()
 
using namespace std;

int nxt() {
    int x;
    cin >> x;
    return x;
}

ll nxtl(){
    ll x;
    cin>>x;
    return x;
}
 
void SieveOfEratosthenes(int n,vector <int> &primes) 
{ 
    // Create a boolean array "prime[0..n]" and initialize 
    // all entries it as true. A value in prime[i] will 
    // finally be false if i is Not a prime, else true. 
    bool prime[n+1]; 
    memset(prime, true, sizeof(prime)); 
  
    for (int p=2; p*p<=n; p++) 
    { 
        // If prime[p] is not changed, then it is a prime 
        if (prime[p] == true) 
        { 
            // Update all multiples of p greater than or  
            // equal to the square of it 
            // numbers which are multiple of p and are 
            // less than p^2 are already been marked.  
            for (int i=p*p; i<=n; i += p) 
                prime[i] = false; 
        } 
    } 
  
    // Print all prime numbers 
    for (int p=2; p<=n; p++) 
       if (prime[p]) 
          primes.push_back(p);
} 
 
ll max(ll a,ll b)
{
    if(a>b)
        return a;
    return b;
}
 
ll power(ll x, ll y,ll mod) 
{ 
    ll temp; 
    if( y == 0) 
        return 1; 
    temp = power(x, y/2,mod); 
    if (y%2 == 0) 
        return (temp*temp)%mod; 
    else
        return (((x*temp)%mod)*temp)%mod; 
}

int MinimumNumIncreasingSubsequences(vector <int> arr, int n) 
{ 
    multiset<int> last; 
  
    // last element in each  increasing subsequence  
    // found so far 
    for (int i = 0; i < n; i++) { 
  
        // here our current element is arr[i] 
        multiset<int>::iterator it = last.lower_bound(arr[i]); 
  
        // iterator to the first element larger  
        // than or equal to arr[i] 
        if (it == last.begin()) 
  
            // if all the elements in last larger 
            // than or to arr[i] then insert it into last 
            last.insert(arr[i]); 
  
        else { 
            it--; 
  
            // the largest element smaller than arr[i] is the number 
            // before *it which is it-- 
            last.erase(it); // erase the largest element smaller than arr[i] 
            last.insert(arr[i]); // and replace it with arr[i] 
        } 
    } 
    return last.size(); // our answer is the size of last 
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n=nxt();
    vector <int> v(n);
    generate(all(v),nxt);
    cout<<MinimumNumIncreasingSubsequences(v,n)<<endl;
    
    
    
    return 0;   
}


 
 


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

using namespace std;

int n,q;
int l[100000],r[100000];
vector <int> countlr;
string b;
int AC_num;

void enter()
{

    cin >> n >> q;
    cin >> b;
    for (int i = 0 ; i < q ; i ++) cin >> l[i] >> r[i];

}

void prep()
{
    countlr.resize(n);
    for (int i = 0 ; i < n ; i ++) countlr[i] = 0;
}

void process()
{
    prep();

    for (int i = 1 ; i < b.size(); i ++)
    {
        if (b[i] == 'C' && b[i-1] == 'A')
        {
            AC_num++;
        }
        countlr[i] = AC_num;
    }
    for (int i = 0 ; i < q ; i ++)
    {
        cout << countlr[r[i]-1] - countlr[l[i]-1] << endl;
    }
}

int main()
{
    enter();
    process();
    return 0;
}


Measured memory_bytes: 2176