akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
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;
}
typedef long long int ll;

#define EPS (1e-7)
#define INF (1LL << 60)
#define PI (acos(-1))
#define MOD (1000000007)
#define ALL(v) (v).begin(), (v).end()
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

//-------------------------------------
const ll MAX_V = 100010;

// 行き先toとコストcost
struct edge
{
    ll to;
    ll cost;
};
typedef pair<ll, ll> P; // firstは最短距離、secondは頂点の番号

// int V;
// vector<edge> graph[MAX_V]; // グラフ(隣接リスト)
// int d[MAX_V];              //最短距離

void dijkstra(ll s, vector<vector<edge>> &graph, vector<ll> &d, ll V)
{
    priority_queue<P, vector<P>, greater<P>> pq;
    for (ll i = 0; i < V; i++)
    {
        d[i] = INF;
    }
    d[s] = 0;
    pq.push(P(0, s));

    while (!pq.empty())
    {
        P p = pq.top(); // 未確定の中から確定させた頂点
        pq.pop();
        ll v = p.second; // 取り出した頂点の番号
        // もう探索する必要のない経路がpqに残っていた場合は枝刈り
        if (d[v] < p.first)
        {
            continue;
        }

        for (ll i = 0; i < (ll)graph[v].size(); i++)
        {
            edge e = graph[v][i];
            if (d[e.to] > d[v] + e.cost)
            {
                d[e.to] = d[v] + e.cost;
                pq.push(P(d[e.to], e.to));
            }
        }
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, m, s, t;
    cin >> n >> m >> s >> t;
    s--;
    t--;
    vector<vector<edge>> graph_yen(MAX_V);
    vector<vector<edge>> graph_snuke(MAX_V);
    vector<ll> d_yen(MAX_V);
    vector<ll> d_snuke(MAX_V);

    for (ll i = 0; i < m; i++)
    {
        ll u, v, a, b;
        cin >> u >> v >> a >> b;
        u--;
        v--;
        graph_yen[u].push_back(edge{v, a});
        graph_yen[v].push_back(edge{u, a});
        graph_snuke[u].push_back(edge{v, b});
        graph_snuke[v].push_back(edge{u, b});
    }

    dijkstra(s, graph_yen, d_yen, n);
    dijkstra(t, graph_snuke, d_snuke, n);

    vector<ll> ans(n + 1, INF);
    for (ll i = n - 1; i >= 0; i--)
    {
        ans[i] = min(ans[i + 1], (ll)d_yen[i] + d_snuke[i]);
    }

    for (ll i = 0; i < n; i++)
    {
        cout << (ll)1e15 - ans[i] << endl;
    }
}

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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
using PII = pair<int, int>;
using PSI = pair<string, int>;
using PIS = pair<int, string>;
using VPII = vector<PII>;
using VPSI = vector<PSI>;
using VPIS = vector<PIS>;
using PPSII = pair<PSI,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VPPSII = vector<PPSII>;

int N;

int main() {
  cin >> N;

  VPPSII spn(N);
  REP(i,N) {
      string s; int p;
      cin >> s >> p;
      spn.at(i)=make_pair(make_pair(s,-p),i+1);
  }
  sort(spn.begin(),spn.end());

    REP(i,N) {
        cout << spn.at(i).second << endl;
    }

}

Measured memory_bytes: 256
def read():
    return tuple(map(int, input().split()))


def main():
    input()
    A = read()
    B = read()
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()


Measured memory_bytes: 19244
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long int w,h,x,y,r;

cin>>w>>h>>x>>y>>r;

w=w-r;
h=h-r;

if(r<=x && x<=w && r<=y && y<=h)
    cout<<"Yes"<<endl;
else
    cout<<"No"<<endl;

}



Measured memory_bytes: 3092
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long ll;
// Welcome to my source code!

int main() {
    int n, m;
    cin >> n >> m;
    int a[m], b[m];
    for (int i = 0; i < m; i++) cin >> a[i] >> b[i];
    int ab[n][n];
    for (int i = 0; i < n; i++)for (int j = 0; j < n; j++) ab[i][j] = 0;
    for (int i = 0; i < m; i++) {
        ab[a[i]-1][b[i]-1]++;
        ab[b[i]-1][a[i]-1]++;
    }
    ll sum;
    for (int i = 0; i < n; i++) {
        sum = 0;
        for (int j = 0; j < n; j++) {
            sum += ab[i][j];
        }
        cout << sum << endl;
    }
}

Measured memory_bytes: 256
n = int(input())
al = [int(input()) for _ in range(n)]

res = 0
i = n-1
while i>=0:
    j = i
    while j-1>=0 and al[j]==al[j-1]+1:
        j -= 1
    res += al[i]
    if al[j]>j:
        print(-1)
        exit()
    if j-1>=0 and al[j-1]<al[j]:
        print(-1)
        exit()
    i = j-1
print(res)



Measured memory_bytes: 16956
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int N;
  string search = "";
  cin >> N;
  string s;
  for (int i = 0; i < N; i++) {
    cin >> s;
    if (search.find(s) == string::npos){
      search+=s;
    }
  }
  if (search.size()==3){
   cout << "Three" << endl;
  }
  else{
   cout << "Four" << endl;
  }
 
}

Measured memory_bytes: 3612
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
 
int main() {
	cin.tie(0);
    ios::sync_with_stdio(false);

    int N;
    cin >> N;

    int ans = 0;

    for(int i = 0; i < N; i++) {
        int a;
        cin >> a;
        ans += a - 1;
    }

    cout << ans << endl;
}   

Measured memory_bytes: 256
n,t=map(int, input().split())
e = [[int(i) for i in input().split()] for i in range(n)]
f=[]
for i in range(0,len(e)):
  if e[i][1]<=t:
    f.append(e[i][0])
print(min(f) if len(f)!=0 else "TLE")
  

  

Measured memory_bytes: 3060
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define print(x) cout << (x) << endl
typedef long long ll;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;

using namespace std;

int main(){
    int n;  cin >> n;
    vector<int> A(n), B(n), ca(200005,0), cb(200005,0), la(200005,0), lb(200005,0), ra(200005,0), rb(200005,0);
    for(int i=0; i<n; i++)  cin >> A[i];
    for(int i=0; i<n; i++)  cin >> B[i];
    for(int i=0; i<n; i++){
        ca[A[i]]++; 
        cb[B[i]]++;
        if(la[A[i]] == 0)   la[A[i]] = i+1;
        if(lb[B[i]] == 0)   lb[B[i]] = i+1;
        ra[A[i]] = i+1;
        rb[B[i]] = i+1;
    }
    int d = 0;
    for(int i=0; i<n+1; i++) if(ca[i] + cb[i] > n){print("No"); return 0;}
  	print("Yes");
    for(int i=0; i<n+1; i++) if(ra[i] > 0 && lb[i] > 0) d = max(d, ra[i]-lb[i]+1);
    vector<int> bb(2*n);
    for(int i=0; i<2*n; i++)    bb[i] = B[i%n];
    for(int i=0; i<n; i++)  cout << bb[i+n-d] << " ";
}

Measured memory_bytes: 11148
#include<bits/stdc++.h>
#define lb long double
#define N 110 
#define eps 1e-10
using namespace std;
struct Po{lb x,y;}a[N],p1,p2;
vector<Po> no;
int n,q;
void get(Po &x){scanf("%Lf %Lf",&x.x,&x.y);}
Po operator -(Po x,Po y){return (Po){x.x-y.x,x.y-y.y};}
lb operator *(Po x,Po y){return x.x*y.y-x.y*y.x;}
Po operator *(Po x,lb y){return (Po){x.x*y,x.y*y};}
Po operator +(Po x,Po y){return (Po){x.x+y.x,x.y+y.y};}
lb operator ^(Po x,Po y){return x.x*x.x+x.y*y.y;}
int pa(int x){if (x>n)x-=n;return x;}
void getans(Po p1,Po p2,Po q1,Po q2){
	lb x=(p2-p1)*(q1-p1),y=(q2-p1)*(p2-p1);
	if (fabs(x+y)<eps){
		if (fabs(x)<eps)no.push_back(q1);
		return ;
	}
	lb z=x/(x+y);
	if (z<-eps||z>1-eps) return ;
	no.push_back((q1*(1-z))+(q2*z));
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++) get(a[i]);
	scanf("%d",&q);
	for (int i=1;i<=q;i++){
		get(p1);get(p2);
		no.clear();
		for (int j=1;j<=n;j++){
			if ((p2-p1)*(a[j]-p1)>eps)no.push_back(a[j]);
			getans(p1,p2,a[j],a[pa(j+1)]);
		}
		lb ansn=0;
		for (int i=1;i<(int)no.size()-1;i++)ansn=ansn+(no[i]-no[0])*(no[i+1]-no[0]);
		printf("%.10Lf\n",ansn/2);
	}
	return 0;
}
/*
3
0 0
1 0
0 1
1
0 0 100 0
*/


Measured memory_bytes: 3268
#include<bits/stdc++.h>
using namespace std;
int n;
vector<int>graph[100005];
int dp[100005];
int f(int curr){
	int&ret = dp[curr];
	if(~ret) return ret;
	ret = 0;
	for(int i=0; i<graph[curr].size(); i++){
		int nexter = graph[curr][i];
		ret = max(ret,f(nexter)+1);
	}
	return ret;
}
int main(){
	memset(dp,-1,sizeof(dp));
	int m;
	cin>>n>>m;
	while(m--){
		int a,b;
		cin>>a>>b;
		graph[a].push_back(b);
	}
	int mx = 0;
	for(int i=1; i<=n; i++)	mx = max(mx,f(i));
	cout<<mx; 
}

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

const int mxN = 2e5 + 5;
int a[mxN], b[mxN];

int main() {
	long long n, k;
	cin >> n >> k;
	for(int i = 1; i <= n; ++i)
		cin >> a[i];
	for(int i = 1; i <= n; ++i)
		cin >> b[i];
	sort(a + 1, a + n + 1 , greater<int>());
	sort(b + 1, b + n + 1);
	long long low = 0, high = 1e13, mid, answer = 1e14;
	while(low <= high) {
		mid = (low + high) / 2;
		long long required = 0;
		for(int i = 1; i <= n; ++i) {
			required += max((long long) 0, a[i] - (mid / b[i]));
		}
		if(required <= k) {
			answer = min(answer, mid);
			high = mid - 1;
		}
		else
			low = mid + 1;
	}
	cout << answer;
}


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

int main()
{
    int n,i,ans=0;
    string s,s1="ABC";
    cin>>n;
    cin>>s;
    for(i=0;i<=s.size()-3;i++)
    {
        int cnt=0;
        for(int j=0;j<3;j++)
        {
            if(s[i+j]==s1[j])
                cnt++;
        }
        if(cnt==3)
            ans++;
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <iterator>  // std::back_inserter()
#include <tuple>  // std::tuple std::get
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>  // std::copy()
#include <functional>  // std::greater<T>()
#include <utility>  // std::swap()
#include <numeric>  // accumulate(ALL(vec), 0)  0 は初期値
#include <bitset>  // static_cast<std::bitset<8>>
#include <sstream>  // std::stringstream
#include <cmath>
#include <climits>  // INT_MIN
#include <cctype>  // std::isdigit()
#include <iomanip>  // std::setprecision()
using namespace std;

#define ALL(obj) (obj).begin(), (obj).end()
#define REP(i, n) for (int i=0; i < (int)(n); i++)  // 0 ~ n-1
#define REPN(i, n) for (int i=1; i <= (int)(n); i++)  // 1 ~ n
#define MIN(vec) min_element(ALL((vec)))  // イテレータのため、値を取り出すときは * を先頭につける
#define MAX(vec) max_element(ALL((vec)))
#define IDX(vec, element_iter) distance((vec).begin(), element_iter)
#define SUM(vec) accumulate(ALL((vec)), 0)  // 0 は初期値
#define COPY(vec1, vec2) copy(ALL(vec1), back_inserter(vec2))  // vec1をvec2にコピーする vec2は空にしておく必要あり
typedef long long ll;

const int MOD = 1000000007;  // 1 000 000 007


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

    // input
    int N, A;
    cin >> N >> A;
    
    // calculation
    int amari = N%500;
    if (amari <= A) {
        cout << "Yes" << "\n";
    } else {
        cout << "No" << "\n";
    }
    
    return 0;
}


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

int main() {
	string str;
	int num,
		a = 0,
		b = 0,
		c = 0,
		d = 0,
		e = 0,
		f = 0,
		g = 0,
		h = 0,
		i = 0,
		j = 0,
		k = 0,
		l = 0,
		m = 0,
		n = 0,
		o = 0,
		p = 0,
		q = 0,
		r = 0,
		s = 0,
		t = 0,
		u = 0,
		v = 0,
		w = 0,
		x = 0,
		y = 0,
		z = 0;
	while (1) {
		getline(cin, str);
		if (str == "")break;
		for (num = 0; num < str.length(); num++) {
			switch (tolower(str[num])) {
			case 'a':
				a++;
				break;
			case 'b':
				b++;
				break;
			case 'c':
				c++;
				break;
			case 'd':
				d++;
				break;
			case 'e':
				e++;
				break;
			case 'f':
				f++;
				break;
			case 'g':
				g++;
				break;
			case 'h':
				h++;
				break;
			case 'i':
				i++;
				break;
			case 'j':
				j++;
				break;
			case 'k':
				k++;
				break;
			case 'l':
				l++;
				break;
			case 'm':
				m++;
				break;
			case 'n':
				n++;
				break;
			case 'o':
				o++;
				break;
			case 'p':
				p++;
				break;
			case 'q':
				q++;
				break;
			case 'r':
				r++;
				break;
			case 's':
				s++;
				break;
			case 't':
				t++;
				break;
			case 'u':
				u++;
				break;
			case 'v':
				v++;
				break;
			case 'w':
				w++;
				break;
			case 'x':
				x++;
				break;
			case 'y':
				y++;
				break;
			case 'z':
				z++;
				break;
			default:
				break;
			}
		}
	}
	cout << "a : "<<a<<endl;
	cout << "b : " << b << endl;
	cout << "c : " << c << endl;
	cout << "d : " << d << endl;
	cout << "e : " << e << endl;
	cout << "f : " << f << endl;
	cout << "g : " << g << endl;
	cout << "h : " << h << endl;
	cout << "i : " << i << endl;
	cout << "j : " << j << endl;
	cout << "k : " << k << endl;
	cout << "l : " << l << endl;
	cout << "m : " << m << endl;
	cout << "n : " << n << endl;
	cout << "o : " << o << endl;
	cout << "p : " << p << endl;
	cout << "q : " << q << endl;
	cout << "r : " << r << endl;
	cout << "s : " << s << endl;
	cout << "t : " << t << endl;
	cout << "u : " << u << endl;
	cout << "v : " << v << endl;
	cout << "w : " << w << endl;
	cout << "x : " << x << endl;
	cout << "y : " << y << endl;
	cout << "z : " << z << endl;

	return 0;
}


Measured memory_bytes: 3148
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
    int count;
    int win=0;
    char s[20];
    scanf("%s",s);
    for(count=0;s[count]!='\0';count++){
        if(s[count]=='o'){win++;}
    }
    if(15-count>=8-win){printf("YES\n");}
    else{printf("NO\n");}
}

int min(int a,int b){
    int ans;
    if(a>b){return b;}
    else {return a;}
}

int max(int a,int b){
    int ans;
    if(a>b){return a;}
    else {return b;}
}

Measured memory_bytes: 128
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		final int N = sc.nextInt();
		int m = N / 111;
		if (N%111 != 0) {
			m += 1;
		}
		int t = m * 111;
		System.out.println(t);
	}
}


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

#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define OP(m) cout << m << endl
typedef long long ll;
typedef unsigned long long ull;

const int p = 1000;
int dp[1001][p + 1];

int main()
{

    rep(i, 1001) rep(j, p + 1) dp[i][j] = 0;

    for (int i = 1; i <= 1000; i++)
    {
        int num = i;
        for (int j = 2; j <= p; j++)
        {
            while (num % j == 0)
            {
                num /= j;
                dp[i][j]++;
            }
            dp[i][j] += dp[i - 1][j];
        }
    }

    ll mod = 1e9 + 7;
    ll sum = 1;
    int n;
    cin >> n;

    for (int j = 1; j <= p; j++)
    {
        sum = (sum * (dp[n][j] + 1)) % mod;
    }

    OP(sum);
    return 0;
}

Measured memory_bytes: 4096
#include <bits/stdc++.h>
using namespace std;
int n;
int a[105];
int ans = 0;

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


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<math.h>
#include<map>
using namespace std;
int main() {
    int n,m;
    cin >> n >> m;
    int a,sum=0;
    for(int i=0;i<m;i++){
        cin >> a;
        sum += a;
    }
    if(n < sum){
        cout << "-1" << endl;
    }else{
        cout << n - sum << endl;
    }
    return 0;
}

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

int main(){
    int N, ans = 0; cin >> N;
    vector<int> A(N);
    for (auto& a : A) {cin >> a; a--;}
    for (int i = 0; i < N; i++) if (i == A[A[i]]) ans++;
    cout << ans/2 << endl;
    return 0;
}

Measured memory_bytes: 640
using System;

namespace myApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            string s = Console.ReadLine();
            string[] a = s.Split(' ');
            int[] num = new int[n];
            for(int i = 0; i < n; i++)
                num[i] = int.Parse(a[i]);

            int count = 0;
            int warusu = 2;
            bool hanbetsu = true;
            do
            {
                for(int i = 0; i < n ; i++)
                {
                    if(num[i] % warusu != 0)
                    {
                        hanbetsu = false;
                        break;
                    }
                }
                if(hanbetsu)
                {
                    count++;
                    warusu *= 2;
                }
            }while(hanbetsu);
            Console.WriteLine(count);
        }
    }
}


Measured memory_bytes: 11092
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int64_t i=0; i<n; i++)
#define rep1(i, n) for(int64_t i=1; i<=n; i++)
#define all(v) v.begin(), v.end()
using pint = pair<int, int>;
using tint = tuple<int, int, int>;
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; }

int main() {
	int64_t N;
	int64_t K;
	cin >> N >> K;
	vector<int64_t> v(N);
	rep(i, N) {
		cin >> v.at(i);
	}

	int64_t a=0, b=0, c=0, x, y, z;
	vector<int64_t> t(N);
	rep1(i, N+1) {
		if (t.at(a) > 0) {
			b = t.at(a);
			c = i;
			break;
		}
		t.at(a) = i;
		a = v.at(a)-1;
	}

	if (c > K) {
		a = 0;
		rep(i, K) {
			a = v.at(a)-1;
		}
		std::cout << a+1 << '\n';
		return 0;
	}
	x = K - b+1;
	y = x % (c-b);
	rep(i, y) {
		a = v.at(a)-1;
	}
	std::cout << a+1 << '\n';
}


Measured memory_bytes: 6424
def c_snuke_the_wizard(N, Q, S, Magic):
    # ゴーレムが隣のゴーレムを追い抜くことはありえない
    # (複数体重なれば、その後同じように動いていくため)。
    # よって、 左端で消滅する、最初に最も右にいるゴーレムleftと、
    # 右端で消滅する、最初に最も左にいるゴーレムrightが分かればいい。
    # (left >= right とはなりえない)
    # これを二分探索で求めれば O(Qlog(N)) で解を得られる。
    def is_vanished(x, direction):
        """
        x番のゴーレムがdirection(leftまたはrightのいずれか)で指定される端で
        消滅するかどうか判定する
        """
        for t, d in Magic:
            if t == S[x]:
                # 呪文で指定されたマスに注目したゴーレムがいた。
                # 呪文の内容に従ってゴーレムを移動させる
                if d == 'L':
                    x -= 1
                else:
                    x += 1
            if x in (-1, N):
                return True  # 左端/右端 で消滅することとなった
        return False

    def binary_search(accept, reject, direction):
        """
        direction が 'left' の場合は accept番とその左側のゴーレムが消滅する。
        direction が 'right' の場合は accept番とその右側のゴーレムが消滅する。
        direction によって accept と reject の位置関係が逆転することに注意。
        """
        while abs(accept - reject) > 1:
            mid = (accept + reject) // 2
            if is_vanished(mid, direction):
                accept = mid
            else:
                reject = mid
        return accept + 1 if direction == 'left' else N - accept
    return max(N - binary_search(-1, N, 'left') - binary_search(N, -1, 'right'), 0)

N, Q = [int(i) for i in input().split()]
S = input()
Magic = [input().split() for j in range(Q)]
print(c_snuke_the_wizard(N, Q, S, Magic))

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

#define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ = malloc(size);void * stack_extend_origin_memory_;char * stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024));

#define END_STACK_EXTEND asm volatile("mov %%rax, %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_);

using pii = pair<int, int>;

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

int H, W;
int h[1000][1000];
bool visited[1000][1000];
set<pii> v[1000][1000];

void dfs(pii p) {
    const int y = p.first;
    const int x = p.second;
    visited[y][x] = true;
    for(int i = 0; i < 4; ++i) {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if(ny < 0 || H <= ny || nx < 0 || W <= nx) {
            continue;
        }
        if(h[ny][nx] >= h[y][x]) {
            continue;
        }
        if(!visited[ny][nx]) {
            dfs(make_pair(ny, nx));
        }
        for(auto& pp : v[ny][nx]) {
            if(v[y][x].size() < 2) {
                v[y][x].insert(pp);
            }
        }
    }
    if(v[y][x].empty()) {
        v[y][x].insert(p);
    }
}

int main() {
	BEGIN_STACK_EXTEND(128*1024*1024);
    cin >> H >> W;
    for(int i = 0; i < H; ++i) {
        for(int j = 0; j < W; ++j) {
            cin >> h[i][j];
        }
    }

    int res = 0;
    for(int i = 0; i < H; ++i) {
        for(int j = 0; j < W; ++j) {
            if(!visited[i][j]) {
                dfs(make_pair(i, j));
            }
            res += v[i][j].size() >= 2;
        }
    }
	END_STACK_EXTEND;
	cout << res << endl;
}


Measured memory_bytes: 159616
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1000000007;
const int INF = 1001001001;

template<class Type>
Type modpow(Type a, Type n, Type mod)
{
	Type res = 1;
	while (0 < n) {
		if (n & 1) {
			res = res * a % mod;
		}
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}

template<class Type>
Type gcd(Type x, Type y)
{
	Type r;
	if (x < y) swap(x, y);

	while (0 < y) {
		r = x % y;
		x = y;
		y = r;
	}
	return x;
}

template<class Type>
Type lcm(Type x, Type y)
{
	return x * y / gcd(x, y);
}

template<class Type>
bool is_prime(Type x)
{
	if (x == 2) return true;
	if (x < 2 || x % 2 == 0) return false;

	for (Type i = 3; i <= sqrt(x); i += 2) {
		if (x % i == 0) return false;
	}
	return true;
}

template<class Type>
vector<bool> eratos(Type n)
{
	vector<bool> isPrime(n + 1, true);
	isPrime[0] = isPrime[1] = false;
	for (Type i = 2; i <= n; i++) {
		if (isPrime[i]) {
			Type j = i + i;
			while (j <= n) {
				isPrime[j] = false;
				j = j + i;
			}
		}
	}
	return isPrime;
}

bool binary_search_judge(vector<int> a, int index, int key)
{
	if (key <= a[index]) return true;
	else return false;
}

int binary_search(vector<int> a, int key)
{
	int ng = -1;
	int ok = a.size();
	while (1 < abs(ng - ok)) {
		int mid = (ng + ok) / 2;
		if (binary_search_judge(a, mid, key)) ok = mid;
		else ng = mid;
	}
	return a[ok] == key;
}

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

	if (n == 1 && m == 1) cout << 1 << endl;
	else if (n == 1 && 1 < m) cout << m - 2 << endl;
	else if (m == 1 && 1 < n) cout << n - 2 << endl;
	else cout << (n - 2) * (m - 2) << endl;
}

Measured memory_bytes: 256
//  Created by conan1024hao in 2019.
//  Copyright © 2019 conan1024hao. All rights reserved.
//  専用ライブラリです、自由にコピーして構いません。
//  感谢看我的代码！Wechat:conan1024hao Twitter/QQ:810396815
#pragma GCC optimize ("O3")
#include <iostream>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
#define IINF 10e8
#define INF 10e16
#define MOD 1000000007
#define mod 1000000007
#define rep(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define Endl endl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define chmax(x,y) x=mmax(x,y)
#define chmin(x,y) x=mmin(x,y)
#define all(x) (x).begin(),(x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
using namespace std;
//using Int=int_fast64_t;
typedef long long int ll;
typedef pair<ll,ll>Pll;
typedef pair<int,int>Pin;
long long GCD(long long a, long long b) { return b ? GCD(b, a%b) : a; }
long long LCM(long long a, long long b)  {return a/GCD(a,b)*b;}
int dx[8]={-1,0,1,0,1,1,-1,-1};
int dy[8]={0,-1,0,1,1,-1,1,-1};
char dir[4]={'u','l','d','r'};
ll cmp1(pair<ll,string>a,pair<ll,string> b){
        if(a.fi!=b.fi)
        return a.fi<b.fi;
        else
        return a.se<b.se;
}

//---------------------------------------------------------------------------
const int MAX = 510000;
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
long long COM(ll n, ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
long long modpow(long long a, long long n) {
    long long res = 1;
    while (n > 0) {
         if (n & 1) res = res * a % MOD;
         a= a * a % MOD;//次の準備
         n >>= 1;
      }
 return res;
}
ll waru(ll a,ll b){
    return a*modpow(b,MOD-2)%MOD;
}
//---------------------------------------------------------------------------
int main(){//問題をちゃんと見ろ!!!!!!!!!!!!!!!!!      llか??????????        memset()!!!!!!!!!!!!       ペナを減らせ!!!!!!!!!!!!!
    cin.tie(0);
    ios::sync_with_stdio(false);
    //-------------------------------
    ll x;cin>>x;
    //6 5 4 6 5 4
    ll ans=0;
    ans+=2*(x/11);
    x%=11;
    if(x>=6){
        x-=6;
        ans++;
    }
    if(x>=5){
        x-=5;
        ans++;
    }
    if(x)ans++;
    cout<<ans<<endl;
    //-------------------------------
    return 0;
}
//---------------------------------------------------------------------------









Measured memory_bytes: 256
// https://atcoder.jp/contests/agc032/tasks/agc032_b

fun main() {
    agc032b()
}

fun agc032b() {
    val n = readLine()!!.toInt()
    val ans = mutableListOf<String>()
    val isEven = n % 2 == 0
    for (i in 1 until n) {
        for (j in i + 1 .. n) {
            val isLink = if (isEven) {
                linkInEven(i, j, n)
            } else {
                linkInOdd(i, j, n)
            }
            if (isLink) ans.add("$i $j")
        }
    }
    println(ans.size)
    println(ans.joinToString("\n"))
}

private fun linkInOdd(i: Int, t: Int, n: Int): Boolean {
    if (i == t) return false
    if (t == n - i) return false
    return true
}

private fun linkInEven(i: Int, t: Int, n: Int): Boolean {
    if (i == t) return false
    if (t == n - i + 1) return false
    return true
}


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

void min_self(int& a, int b)
{
  a = min(a, b);
}

const int inf = 1e9 + 5;
int main()
{
  int n, w;
  cin >> n >> w;

  vector<int> dp(100001, inf);
  dp[0] = 0;
  for (int i = 0; i < n; ++i)
  {
      vector<int> dp1(100001, inf);
      int weight_i;
      int value_i;
      cin >> weight_i >> value_i;

      for (int j = 0; j < 100001; ++j)
      {
          min_self(dp1[j], dp[j]);
          if(j - value_i >= 0)
          min_self(dp1[j], weight_i + dp[j - value_i]);

          if(dp1[j] > inf) dp1[j] = inf;
      }

      dp = dp1;

  }
  
  int i = 100000;

  for (; i >= 0; --i)
  {
     if(dp[i] <= w) break;
  }

  cout << i << endl;

  return 0;
}

Measured memory_bytes: 1024
def main():
    S = input()
    n = len(S)
    a = {j: i for i, j in enumerate('abcdefghijklmnopqrstuvwxyz')}
    S = [a[i] for i in S]
    dp = [10**9]*(n+1)
    dp[0] = 0

    parity = 0
    dict_parity = {0: 0}

    for i, s in enumerate(S):
        parity ^= 2**s
        ans = i+1
        if parity not in dict_parity:
            dict_parity[parity] = i+1
        else:
            ans = min(ans, dp[dict_parity[parity]]+1)
        for j in range(26):
            parity2 = parity ^ 2**j
            if parity2 in dict_parity:
                ans = min(ans, dp[dict_parity[parity2]]+1)
        dp[i+1] = ans
        if dict_parity[parity] > ans:
            dict_parity[parity] = i+1
    print(dp[-1])


main()

Measured memory_bytes: 72056
#include<bits/stdc++.h>
using namespace std;
 
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int, int>
#define vi              vector<int>
#define si              set<int>
#define usi             unordered_set<int>
#define mii             map<int, int>
#define umii            unordered_map<int, int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int, vi, greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define endl            '\n'
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(t)            int t; cin>>t; while(t--)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());
 
void jaiswal_sahil()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// #ifndef ONLINE_JUDGE
//     freopen("input.txt", "r", stdin);
//     freopen("output.txt", "w", stdout);
// #endif
}

const int N = 1e6;
int dp[N];
int n, k;

// top down Approach
int minCost(int i, int *a){

    if(i >= n-1)
        return 0;

    if(dp[i] != -1)
        return dp[i];

    int q1 = inf;
    for(int j=i+1; j<=i+k; j++){
        if(j < n){
            int subProb = abs(a[i] - a[j]) + minCost(j, a);
            q1 = min(q1, subProb);
        }
        
    }
    
    return dp[i] = q1;

}

// bottom up Approach
int minCostBU(int n, int *a){

    memset(dp, 0, sizeof(dp));
    dp[2] = abs(a[0] - a[1]);
    for(int i=3; i<=n; i++){
        dp[i] = min(abs(a[i-1] - a[i-2]) + dp[i-1], abs(a[i-1] - a[i-3]) + dp[i-2]);
    }
    return dp[n];

}

int32_t main()
{
    jaiswal_sahil();

    cin>>n>>k;
    int a[n];
    for(int i=0; i<n; i++)
        cin>>a[i];
    
    memset(dp, -1, sizeof(dp));
    int ans = minCost(0, a);

    // int ans = minCostBU(n, a);
    cout<<ans<<endl;
    return 0;

}

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

public class Main{
	public static void main(String[] args){

	Scanner stdIn = new Scanner(System.in);
	BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
	
	int n = stdIn.nextInt();

	int ans = 0;
	for(int i = 0;i < n;i++){
		int a = stdIn.nextInt();
		ans += a - 1;
	}
	System.out.println(ans);
		
	}
}

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

int main() {
 int S;
 cin >> S;
 int h = S/3600;
 int ah = h * 3600;
 int ahs = S - ah;
 int m = ahs /60;
 int am = m * 60;
 int s = ahs - am;
 cout << h << ":" << m << ":" << s << endl;

}

Measured memory_bytes: 3084
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define INF 999999999
#define ll long long
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;


int v[200000];

int main(){
  int x1, x2, n;
  cin >> n;
  x1 = x2 = 0;
  REP(i,n){
    cin >> v[i];
    if(v[i] > x1){
      x2 = x1;
      x1 = v[i];
    }
    else if(v[i] > x2){
      x2 = v[i];
    }
  }
  REP(i,n){
    if(v[i] == x1) cout << x2 << endl;
    else cout << x1 << endl;
  }
  return 0;
}

Measured memory_bytes: 2432
#include <bits/stdc++.h>
#include <bitset>
#include <iomanip>
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,K,Q,A,B;
string S, T;
//const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
const ll INF = 1LL << 60;
typedef pair<ll,ll> P;

int main(){
    cin>>N>>M;
    class union_find{
            vector<long> parent, tree_size;
        public:
            union_find(unsigned long _n) : parent(_n), tree_size(_n, 1){
                for(unsigned long i = 0; i < _n; ++i)parent[i] = i;
            }
            ll find(ll x){
                while(parent[x] != x)x = parent[x] = parent[parent[x]];
                return x;
            }
            void merge(ll a, ll b){
                a = find(a);
                b = find(b);
    	    if(a==b) return;
                if(tree_size[a] < tree_size[b])swap(a, b);
                tree_size[a] += tree_size[b];
                parent[b] = a;
                return;
            }
    	bool same(ll a, ll b){
    	    a = find(a);
    	    b = find(b);
    	    return a == b;
    	}
        };
    //uf.mergeのように使う
    //ufはMAX_N+1でとるか、代入時に0-indexedにする
    union_find uf(N);
    mat color(100010, vec(0));
    rep(i,N) {
        cin>>K;
        rep(j,K){
            cin>>A;
            color[A].push_back(i);
        }
    }
    rep(i,100010){
        if(color[i].size() > 1) {
            reps(j, 1, color[i].size()){
                uf.merge(color[i][0], color[i][j]);
            }
        }
    }
    reps(i,1,N) {
        if(!uf.same(0, i)){
            cout<<"NO"<<endl;
            return 0;
        }
    }
    cout<<"YES"<<endl;
}

Measured memory_bytes: 5500
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools

INF = 10 ** 10

M, D = [int(x) for x in stdin.readline().rstrip().split()]

cnt = 0

for i in range(1, M+1):
    for j in range(1, D+1):
        if j < 10:
            continue

        s = str(j)
        d10 = int(s[0])
        d1 = int(s[1])

        if d10 >= 2 and d1 >= 2:
            if i == (d10*d1):
                cnt+= 1

print(cnt)

Measured memory_bytes: 66028
//                     بِسْمِ ٱللَّٰهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ
//                   لآ اِلَهَ اِلّا اللّهُ مُحَمَّدٌ رَسُوُل اللّهِ




#include <bits/stdc++.h>

using namespace std;
#define endl "\n"

int main()
{

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

    if(a%b==0)cout<<(a/b)*c << endl;
    else cout << (a/b+1)*c << endl;


	return 0;
}


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

int main() {
  string s;
  cin>>s;
  string h=s;
  reverse(h.begin(),h.end());
  int N=s.size();
  string t=s.substr(0,(N-1)/2);
  string p=s.substr(0,(N-1)/2);
  reverse(p.begin(),p.end());
  string d=s.substr((N+3)/2-1);
  string y=s.substr((N+3)/2-1);
  reverse(y.begin(),y.end());
  //cout<<t<<endl<<p<<endl<<d<<endl<<y<<endl;
  if(s==h&&t==p&&d==y){cout<<"Yes"<<endl;}
  else{cout<<"No"<<endl;}
  return 0;
}

Measured memory_bytes: 256
var
  n:longint;
begin
  readln(n);
  if n<2  then  begin writeln(1);  halt; end else
  if n<4  then  begin writeln(2);  halt; end else
  if n<8  then  begin writeln(4);  halt; end else
  if n<16 then  begin writeln(8);  halt; end else
  if n<32 then  begin writeln(16); halt; end else
  if n<64 then  begin writeln(32); halt; end else
                begin writeln(64); halt; end
end.


Measured memory_bytes: 128
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef pair<int,int> P;
typedef long long int ll;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}


int main() {
    int n,k;
    cin >> n >> k;
    n -= k-1;
    int ans = n - 1;
    if(k==1)ans = 0;

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


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

int main(){
  long long A, B, N;
  cin>>N>>A>>B;
  long long C;
  C=N/(A+B);
  if (N%(A+B)<=A){
      cout << C*A+N%(A+B)<<endl;
  }
  else{
      cout << (C+1)*A<<endl;
  }
}

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

using namespace std;

// 2つの値の和がaとなる順列の数。
int func2(int a) {
	if (a > 18) {
		return 0;
	}
	int count = 0;
	
	for (int i = 0; i <= min(a, 9); ++i) {
		if (a - i < 10) {
			++count;
		}
	}
	return count;
}

// 3つの値の和がaとなる順列の数
int func3(int a) {
	if (a > 27) {
		return 0;
	}
	int count = 0;

	for (int i = 0; i <= min(a, 9); ++i) {
		count += func2(a - i);
	}
	return count;
}

// 4つの値の和がaとなる順列の数
int func4(int a) {
	if (a > 36) {
		return 0;
	}
	int count = 0;
	for (int i = 0; i <= min(a, 9); ++i) {
		count += func3(a - i);
	}
	return count;
}

int main() {
	int n;

	while (cin >> n) {
		cout << func4(n) << endl;
	}
	return 0;
}

Measured memory_bytes: 1160
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i;
    bool ans=false;
    cin>>n;
    for(i=1;i<=9;i++)
    {
        if(n%i==0&&n/i<=9)
        {
            ans=true;
            break;
        }
    }
    cout<<(ans?"Yes":"No")<<endl;
}


Measured memory_bytes: 3576
#include <stdio.h>
int main(){
    int i,n, a[10000],min,max;
    long long int sum = 0;
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    min = a[0];
    max = a[0];
    for (i = 0; i < n; i++)
    {
  
        if (min>a[i])
        {
            min = a[i];
        }
        if (max < a[i])
        {
            max = a[i];
        }
  
        sum +=a[i];
  
    }
    printf("%d %d %lld\n", min, max, sum);
    return 0;
}

Measured memory_bytes: 1860
// #include "knapsack_1.h"
    #include <iostream>
    #include <iterator>
     
    #include <vector>
     
    struct Item {
        int weight;
        int value;
     
        Item(int w, int v)
            : weight{w}
            , value{v}
        {
        }
    };
     
    long long max_value(const std::vector<Item>& items, int capacity)
    {
        std::vector<long long> dp(capacity + 1);
        std::vector<long long> prev = dp;
     
        for (const auto& item : items) {
            for (int w = 0; w <= capacity; ++w) {
                if (item.weight > w)
                    dp[w] = prev[w];
                else {
                    dp[w] = std::max(prev[w], item.value + prev[w - item.weight]);
                }
            }
            std::swap(dp, prev);
        }
     
        return prev.back();
    }
     
    int main(int argc, char* argv[])
    {
        int n, capacity;
        std::cin >> n >> capacity;
        std::vector<Item> items;
        items.reserve(n);
     
        for (int i = 0; i < n; ++i) {
            int weight, value;
            std::cin >> weight >> value;
            items.emplace_back(weight, value);
        }
        std::cout << max_value(items, capacity) << std::endl;
        return 0;
    }

Measured memory_bytes: 1792
#include <iostream>
#include <algorithm>
#include <complex>
#include <utility>
#include <vector>
#include <string>
#include <queue>
#include <tuple>
#include <cmath>
#include <bitset>
#include <cctype>
#include <set>
#include <map>
#include <numeric>
#include <functional>
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(),(V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
inline void Yes(bool condition){ if(condition) PRINT("Yes"); else PRINT("No"); }
template<class itr> void cins(itr first,itr last){
    for (auto i = first;i != last;i++){
        cin >> (*i);
    }
}
template<class itr> void array_output(itr start,itr goal){
    string ans = "",k = " ";
    for (auto i = start;i != goal;i++) ans += to_string(*i)+k;
    if (!ans.empty()) ans.pop_back();
    PRINT(ans);
}
ll gcd(ll a, ll b) {
    return a ? gcd(b%a,a) : b;
}
const ll INF = 1e15;
const ll MOD = 1000000007;
typedef pair<ll,ll> P;
const ll MAX = 400010;
constexpr ll nx[8] = {1,0,-1,0,-1,-1,1,1};
constexpr ll ny[8] = {0,1,0,-1,-1,1,-1,1};
class UnionFind{
    private:
        vector<ll> par;
        vector<ll> siz;
    public:
        UnionFind(ll sz){
            par.resize(sz);
            siz.assign(sz,1LL);
            rep(i,sz) par[i] = i;
        }

        ll find(ll x){
            if (par[x] == x) return x;
            else return par[x] = find(par[x]);
        }

        void unite(ll x,ll y){
            x = find(x);
            y = find(y);
            if (x == y) return;
            if (siz[x] < siz[y]) swap(x,y);
            siz[x] += siz[y];
            par[y] = x;
        }

        bool same(ll x,ll y){
            return find(x) == find(y);
        }

        ll size(ll x){
            return siz[find(x)];
        }
};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n,m,x,y,a;
    cin >> n >> m;
    vector<ll> p(n);
    rep(i,n){
        cin >> p[i];
        p[i]--;
    }
    UnionFind g(n);
    rep(i,m){
        cin >> x >> y;
        --x;
        --y;
        g.unite(x,y);
    }
    ll ans = 0;
    rep(i,n){
        if (g.same(i,p[i])) ans++;
    }
    PRINT(ans);
}


Measured memory_bytes: 2560
#include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#define int long long
#define pb push_back
#define x first
#define y second
#define mk(a,b) make_pair(a,b)
#define rr return 0
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template<class value, class cmp = less<value> >
using ordered_set = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class value, class cmp = less_equal<value> >
using ordered_multiset = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class key, class value, class cmp = less<key> >
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;

/// find_by_order()
/// order_of_key()
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int randll(int l = INT_MIN, int r = INT_MAX) {
    return uniform_int_distribution<int>(l, r)(rng);
}
const int INF = 1e9, MOD = 1e9 + 7; /// think
const ll LINF = 1e18;


const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
inline bool inside (int x, int y, int n, int m) {
    return 0 <= x && 0 <= y && x < n && y < m;
}

template<class T> bool umin (T &a, T b) {return a > b ? (a = b, true) : false; }
template<class T> bool umax (T &a, T b) {return a < b ? (a = b, true) : false; }

main()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int a, b;
    cin >> a >> b;
    cout << min(string(a, '0' + b), string(b, '0' + a)) << '\n';
}


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

typedef struct Process{
	string name;
	int time;
	Process(string n,int t){ 
		name = n; 
		time = t;
	};
	Process() {
		name = " ";
		time = 0;
	};
};

template<class Type> class Queue{
private:
	int head=0, tail=0;
	Type queue[100009];
public:
	Queue( ) { }; 
	
	void push(const Type& p) {
		queue[tail] = p;
		tail=(tail+1) % 100009;
	}

	Type pop() {
		Type x = queue[head];
		head=(head+1)%100009;
		return x;
	}

	Type front() {
		return queue[head];
	}

};

int main() {
	int n, q,allTime=0;
	cin >> n >> q;
	Process p;
	Queue<Process> Q;
	for (int i = 0;i < n;i++) {
		cin >> p.name; cin >> p.time;
		Q.push(p);
	}
	while (n>0) {
		if (Q.front().time <= q) {
			allTime+=Q.front().time;
			cout << Q.front().name << " " <<allTime<< endl;
			Q.pop();
			n--;
		}
		else {
			allTime += q;
			Q.push(Process(Q.front().name,Q.front().time-q));
			Q.pop();
		}
	}

}

Measured memory_bytes: 9464
#include <iostream>
using namespace std;
int main(void){
    int n, k, d;
    int a[200];
    
    cin >> n >> k;
    for(int i=1; i<=n; i++){
        a[i] = 0;
    }
    
    for(int i=0; i<k; i++){
        cin >> d;
        for(int j=0; j<d; j++){
            int tmp;
            cin >> tmp;
            a[tmp]++;
        }
    }
    
    int cnt =0;
    for(int i=1; i<=n; i++){
        if(a[i] == 0)cnt++;
    }
    
    cout << cnt << endl;
    return 0;
}


Measured memory_bytes: 3632
#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 3000000000000000000
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define REP(i,n) for(long long i = 0;i < n;++i)    
#define seg_size 524288
double dot(complex<double> a, complex<double> b) {
	return a.real() * b.real() + a.imag() * b.imag();
}
double gyaku_dot(complex<double> a, complex<double> b) {
	return a.real() * b.imag() - a.imag() * b.real();
}
double leng(complex<double> a) {
	return sqrt(a.real()*a.real() + a.imag()*a.imag());
}
double angles(complex<double> a, complex<double> b) {
	double cosine = dot(a, b) / (leng(a) * leng(b));
	double sine = gyaku_dot(a, b) / (leng(a) * leng(b));
	double kaku = acos(cosine);
	if (sine <= 0) {
		kaku = 2 * Ma_PI - kaku;
	}
	return kaku;
}
vector<int> convex_hull(vector<complex<double>> a) {
	vector<int> ans;
	double now_minnest = a[0].real();
	int now_itr = 0;
	REP(i, a.size()) {
		if (now_minnest > a[i].real()) {
			now_minnest = a[i].real();
			now_itr = i;
		}
	}
	ans.push_back(now_itr);
	complex<double> ba(0, 1);
	while (true) {
		int now_go = 0;
		double now_min = 0;
		int starter = ans[ans.size() - 1];
		for (int i = 0; i < a.size(); ++i) {
			if (i != starter) {
				double goa = angles(ba, a[i] - a[starter]);
				if (goa > now_min) {
					now_min = goa;
					now_go = i;
				}
			}
		}
		if (now_go == ans[0]) break;
		ans.push_back(now_go);
		ba = complex<double>(a[now_go] - a[starter]);
	}
	return ans;
}
int main(){
	int d;
	cin >> d;
	cout << "Christmas";
	d = 25 - d;
	REP(i, d) {
		cout << " Eve";
	}
	cout << endl;
	return 0;
}

Measured memory_bytes: 888
#include<bits/stdc++.h>
const int mod=1000000007;
using namespace std;
int main(){
    int h,w,k;
    cin>>h>>w>>k;
    if(w==1){
        cout<<"1"<<endl;
        return 0;
    }
    int W=1;
    for(int i=0;i<w-1;i++)W*=2;
    long long dp[h+1][w]={};
    dp[0][0]=1;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            int left=0,right=0,straight=0;
            for(int l=0;l<W;l++){
                int x=l;
                int line[w-1]={};
                bool b=false;
                for(int m=0;m<w-1;m++){
                    if(x%2){
                        line[m]=1;
                        if(m && line[m-1]){
                            b=true;
                            break;
                        }
                    }
                    x/=2;
                }
                if(b)continue;
                if(j==0){
                    if(line[j])right++;
                    else straight++;
                }
                else if(j==w-1){
                    if(line[j-1])left++;
                    else straight++;
                }
                else{
                    if(line[j-1])left++;
                    else if(line[j])right++;
                    else straight++;
                }
            }
            if(j)dp[i+1][j-1]+=dp[i][j]*left%mod;
            if(j<w-1)dp[i+1][j+1]+=dp[i][j]*right%mod;
            dp[i+1][j]+=dp[i][j]*straight%mod;
        }
    }
    cout<<dp[h][k-1]%mod<<endl;
    return 0;
}

Measured memory_bytes: 256