akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
#define Set(a,b) memset(a,b,sizeof(a))
template<class T>inline void init(T&x){
	x=0;char ch=getchar();bool t=0;
	for(;ch>'9'||ch<'0';ch=getchar()) if(ch=='-') t=1;
	for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+(ch-48);
	if(t) x=-x;return;
}typedef long long ll;
const int N=120;
int S[N],a[N];
int main()
{
	int sum=0;
	int n;init(n);
	for(int i=1;i<=n;++i) {
		init(a[i]);S[i]=a[i]+S[i-1];
		sum+=a[i];
	}
	int ans=1e9;
	for(int i=1;i<=n;++i) {
		if(a[i]!=a[i+1]) ans=min(ans,abs(S[i]-(sum-S[i])));
	}cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        int n = Integer.parseInt(sc.next());
        String s = Integer.toString(n);
        boolean flag = false;
        for(int i = 0; i < s.length(); i++){
            if(s.charAt(i) == '7') flag = true;
        }
        System.out.println(flag ? "Yes" : "No");
    }
}

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

using namespace std;

int main ()
{
  	int N;
  	cin >> N;
  	vector<int> A(N);
  	for(int i = 0; i < N; i++){
      	cin >> A.at(i);
    }
  	int max = A.at(0), min = A.at(0);
  	for(int i = 1; i < N; i++){
      	if(A.at(i) > max){
          	max = A.at(i);
        }
      	if(A.at(i) < min){
			min = A.at(i);
        }
    }
  	cout << max - min;
  	return 0;
}

Measured memory_bytes: 256
let inf = 1 lsl 40
let n, ma, mb = Scanf.scanf " %d %d %d" @@ fun a b c -> a, b, c
let abcs = Array.init n @@ fun _ -> Scanf.scanf " %d %d %d" @@ fun a b c -> a, b, c
let dp = Array.(init n @@ fun _ -> make_matrix 401 401 inf)
let a, b, c = abcs.(0)
let ans = ref inf
let _ = Array.(iter (fun a -> a.(0).(0) <- 0) dp; dp.(0).(a).(b) <- c; for i = 1 to n - 1 do let a, b, c = abcs.(i) in for j = 1 to 400 do for k = 1 to 400 do dp.(i).(j).(k) <- min dp.(i - 1).(j).(k) (if j >= a && k >= b then dp.(i - 1).(j - a).(k - b) + c else inf) done done done; iteri (fun i -> iteri (fun j p -> if p > 0 && i * mb = j * ma then ans := min !ans p)) dp.(n - 1)); Printf.printf "%d\n" @@ if !ans = inf then -1 else !ans

Measured memory_bytes: 53500
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
typedef long long ll;
const int inf = 1e9 + 7;
using pii = pair<int, int>;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
const int mod = 1e9 + 7;
ll power(ll a,ll b){return b?power(a*a%mod,b/2)*(b%2?a:1)%mod:1;}

int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    char c[20];
    while (scanf("%s", c)) {
        if (c[0] == '#') break;
        string s = string(c);
        vector<string> v;
        rep(i, (1 << s.size())) {
            bool invalid = false;
            string t = s;
            rep(j, s.size()) {
                if (i & (1 << j)) {
                    if (t[j] == 'z') {invalid = true; break;}
                    t[j]++;
                }
            }
            if (invalid) continue;
            string u = t;
            int table[26] = {0};
            rep(i, u.size()) {
                if (u[i] == 'a') continue;
                if (table[u[i] - 'a']) continue;
                table[u[i] - 'a'] = 1;
                u[i]--;
            }
            if (u != s) continue;
            v.push_back(t);
        }
        printf("%d\n", (int)v.size());
        sort(v.begin(), v.end());
        if (v.size() <= 10) {
            rep(i, v.size()) printf("%s\n", v[i].c_str());
        } else {
            rep(i, 5) printf("%s\n", v[i].c_str());
            rep(i, 5) printf("%s\n", v[v.size() - 5 + i].c_str());
        }
    }
}




Measured memory_bytes: 4184
#include <stdio.h>

int main(){
    int H, M, i, j;
    while(1){
        scanf("%d%d",&H,&M);
        if(H==0&&M==0){break;}
            for(i=0;i<H;i++){
                for(j=0;j<M;j++){
                    if(j%2==1){
                        if(i%2==1) printf("#");
                        else printf(".");
                    }
                    else{
                        if(i%2==1) printf(".");
                        else printf("#");
                    }
                }
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}



Measured memory_bytes: 2104
// This amazing code is by Eric Sunli Chen.
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
template<typename T> bool get_int(T &x)
{
	char t=getchar();
	bool neg=false;
	x=0;
	for(; (t>'9'||t<'0')&&t!='-'&&t!=EOF; t=getchar());
	if(t=='-')neg=true,t=getchar();if(t==EOF)return false;
	for(; t<='9'&&t>='0'; t=getchar())x=x*10+t-'0';
	if(neg)x=-x;return true;
}
template<typename T> void print_int(T x)
{
	if(x<0)putchar('-'),x=-x;
	short a[20]= {},sz=0;
	while(x>0)a[sz++]=x%10,x/=10;
	if(sz==0)putchar('0');
	for(int i=sz-1; i>=0; i--)putchar('0'+a[i]);
}
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define get1(a) get_int(a)
#define get2(a,b) (get1(a)&&get1(b))
#define get3(a,b,c) (get1(a)&&get2(b,c))
#define printendl(a) print_int(a),puts("")
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
const LL Linf=1ll<<61;
const double pi=acos(-1.0);

const int mod=1e9+7;
struct sparse_table
{
	int lg[200111],n;
	pii v[20][200111];
	void construct(int n,int *a)
	{
		this->n=n;
		for(int i=2;i<=n;i++)lg[i]=lg[i>>1]+1;
		for(int i=1;i<=n;i++)v[0][i]=mp(a[i],i);
		for(int i=1;i<20;i++)for(int j=1;j<=n;j++)
		{
			int nxt=j+(1<<i-1);
			if(nxt>n)v[i][j]=v[i-1][j];
			else v[i][j]=min(v[i-1][j],v[i-1][nxt]);
		}
	}
	pii query(int l,int r)
	{
		if(l>r)return mp(-1,-1);
		int len=lg[r-l+1];
		return min(v[len][l],v[len][r-(1<<len)+1]);
	}
}thor;
vector<int>g[200111],vs[200111];
int n,f[200111][20],dep[200111],pw2[200111],pre[200111],suf[200111],a[200111];

void dfs(int x){for(auto v:g[x])dfs(v);vs[dep[x]].pb(x);}
int getlca(int u,int v)
{
	if(dep[u]<dep[v])swap(u,v);
	for(int i=19;i>=0;i--)if(dep[u]-dep[v]>>i)u=f[u][i];
	if(u==v)return u;
	for(int i=19;i>=0;i--)if(f[u][i]!=f[v][i]){u=f[u][i];v=f[v][i];}
	return f[u][0];
}

pii solve(int l,int r)
{
	if(l==r)return mp(1,1);
	int mxv=thor.query(l+1,r).ff,curl=l;
	vector<pii> q;q.pb(mp(0,0));
	while(true)
	{
		pii now=thor.query(curl+1,r);
		if(now.ff!=mxv){q.pb(solve(curl,r));break;}
		q.pb(solve(curl,now.ss-1));curl=now.ss;
	}
	int sz=(int)q.size()-1;
	pre[0]=suf[sz+1]=1;
	for(int i=1;i<=sz;i++)pre[i]=1ll*pre[i-1]*q[i].ss%mod;
	for(int i=sz;i>=1;i--)suf[i]=1ll*suf[i+1]*q[i].ss%mod;
	int sum=0;for(int i=1;i<=sz;i++)sum=(sum+1ll*pre[i-1]*suf[i+1]%mod*q[i].ff)%mod;
	return mp(sum,(pw2[r-l+1]-sum+mod)%mod);
}
int main()
{
	pw2[0]=1;for(int i=1;i<200111;i++)pw2[i]=pw2[i-1]*2%mod;
	
	get1(n);
	for(int i=1;i<=n;i++){get1(f[i][0]);g[f[i][0]].pb(i);dep[i]=dep[f[i][0]]+1;}
	for(int i=1;i<20;i++)for(int j=1;j<=n;j++)f[j][i]=f[f[j][i-1]][i-1];
	dfs(0);
	int m=*max_element(dep+1,dep+n+1),ans=0;
	for(int i=0;i<=m;i++)
	{
		int sz=(int)vs[i].size();
		for(int j=1;j<sz;j++)a[j]=dep[getlca(vs[i][j-1],vs[i][j])];
		thor.construct(sz-1,a);
		ans=(ans+1ll*solve(0,sz-1).ff*pw2[n-sz+1])%mod;
	}
	printendl(ans);
	return 0;
}

Measured memory_bytes: 81024
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <utility>
#include <tuple>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstring>
#include <cmath>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
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; }

vector<long long> divisor(long long n) {
  vector<long long> res;
  for (long long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      res.push_back(i);
      long long j = n / i;
      if (j != i) res.push_back(j);
    }
  }
  sort(res.begin(), res.end());
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  auto div = divisor(n);
  ll ans = 1e18;
  rep(i, div.size()/2+1)
    chmin(ans, div[i]+n/div[i]-2);
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 384
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
    int n,i,cnt=1;
    scanf("%d",&n);
    char s[n][11];
    for(i=0;i<n;i++) scanf("%s",&s[i]);
    qsort(s,n,11,strcmp);
    for(i=1;i<n;i++){
        if(strcmp(s[i],s[i-1])!=0) cnt++;
    }
    printf("%d",cnt);
    return 0;
}


Measured memory_bytes: 5740
/*BY-ADITYA OJHA
  LEARN DSA SINCERELY*/
#include <bits/stdc++.h>
#define ll long long int
#define forin for(int i=0;i<n;i++)
#define forjn for(int j=0;j<n;j++)
#define pb push_back
#define vx vector<ll> x
#define fas1  ios_base::sync_with_stdio(false)
#define fas2   cin.tie(NULL)
using namespace std;


int main()
{
     fas1;
     fas2;
     string s;
     cin>>s;
     ll flag=0;
     for(int i=0;i<s.size()-1;i++)
     {
         if(s[i]!=s[i+1])
         {
             flag=1;

         }
     }if(flag)cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;
     return 0;

}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;

string delstr(string input,string obj){
    string ret;
    auto index=input.find(obj);
    while(index!=-1){
        input.replace(index,obj.length(),"");
        index=input.find(obj);
    }
    ret=input;
    return ret;
}

int main(){
    string s;
    cin>>s;
    
    string d="dream",dr="dreamer",e="erase",er="eraser";
    
    string result=delstr(delstr(delstr(delstr(s,er),e),dr),d);
    if(""==result){
        cout<<"YES"<<endl;
    }else{
        cout<<"NO"<<endl;
    }
    
    return 0;
}

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP0(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define RREP0(i, n) for (int i = (n)-1; i >= 0; --i)
#define REP1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define RREP1(i, n) for (int i = (n); i >= 1; --i)

typedef long long LL;
typedef pair<int, int> pii;

const int INTINF = int(1e9) + 1;
const LL LLINF = LL(1e18) + 1;
long double eps = 1.0E-14;
#define pow10(n) int(1e##n + n)

void solve()
{
    int N;
    cin >> N;
    vector<LL> S(N + 1);
    map<LL, int> mp;
    mp[0]++;

    REP1 (i, N)
    {
        LL A;
        cin >> A;
        S[i] = S[i - 1] + A;
        mp[S[i]]++;
    }

    LL cnt = 0;

    for (auto p : mp)
    {
        LL n = p.second;
        cnt += n * (n - 1) / 2;
    }

    cout << cnt << endl;
}

int main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(20);
    solve();
    return 0;
}


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

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

long lcm(long a, long b) {
	return a * b / gcd(a, b);
}

int main() {
	long a, b;
	int c, d;
	cin >> a >> b >> c >> d;
	long div_c   = b/c        - (a-1)/c;
	long div_d   = b/d        - (a-1)/d;
	long div_lcm = b/lcm(c,d) - (a-1)/lcm(c,d);

	cout << (b-a+1) - (div_c + div_d - div_lcm) << endl;
}


Measured memory_bytes: 256
n = int(input())
a = [int(x) for x in input().split()]

a.sort(reverse=True)

a_ = []
b_ = []
for i,j in enumerate(a):
    if i%2 == 0:
        a_.append(j)
    else:
        b_.append(j)
        
print(sum(a_)-sum(b_))


Measured memory_bytes: 38256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
typedef long long ll;
int main() {
    // ifstream in("data1.txt");
    // cin.rdbuf(in.rdbuf());
    string S;
    cin >> S;
    int ans = 1e9;

    rep(i, S.length() - 2) {
        string tmp = {S[i], S[i + 1], S[i + 2]};
        int tmp2 = stoi(tmp);
        if(abs(ans) > min(abs(753 - tmp2), ans)) {
            ans = min(abs(753 - tmp2), ans);
        }
    }

    cout << ans << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i < b; i++)
#define Rep(i, a, b) for(int i = a; i <= b; i++)
#define _GLIBCXX_DEBUG
#define V vector<ll>
#define ll long long
using namespace std;


int main() {
    ll n, a, b;
    cin >> n >> a >> b;

    if((b-a)%2 == 0) cout << (b-a)/2 << "\n";
    else{
        cout << min(a-1, n-b) + 1 + (b-a-1)/2 << "\n";
        // +1 は端での調整
    }


}

Measured memory_bytes: 256
def expression(a,b):
    plus = a+b
    minus =a-b
    times = a*b

    return max([plus,minus,times])

if __name__ == '__main__':
#    n = int(input().split())
#    n = int(input())
    a,b = map(int,input().split())
#    n = list(map(int,input().split()))
#   a = [ input() for i in range(n)]

    print(expression(a,b))

Measured memory_bytes: 3064
N = int(input())
A = list(map(int, input().split()))
B = A[:]
B.insert(0, 0)
B.append(0)

sum = 0

for i in range(N+1):
  sum += abs(B[i+1] - B[i])
  
for i in range(N):
  #立ち寄る
  x = abs(B[i+1] - B[i]) + abs(B[i+2] - B[i+1])
  #立ち寄らない
  y = abs(B[i+2] - B[i])
  print(sum - x + y)

Measured memory_bytes: 20556
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
typedef long long ll;


int main()
{
  int N,A,B,C,X;
  while(cin>>N>>A>>B>>C>>X){
    if(N == 0) return 0;
    int Y[N];
    FOR(i,0,N) cin >> Y[i];
    if(N == 1 && X == Y[0]) {
      cout << 0 << endl;
      continue;
    }
    int fr = 0;
    int now = X == Y[0];
    int rnd = X;
    bool ok = false;
    while(fr < 10000) {
      rnd = (A * rnd + B) % C;
      fr++;
      if(Y[now] == rnd) now++;
      if(now == N) {
        cout << fr << endl;
        ok = true;
        break;
      }
    }
    if(!ok) cout << -1 << endl;
  }
  return 0;
}

Measured memory_bytes: 3088
N = int(input())
words = list(input() for _ in range(N))
done = []
for w in words:
  if len(done) == 0:
    done.append(w)
  else:
    if w in done or (done[-1])[-1] != w[0]:
      print('No')
      exit()
    else:
      done.append(w)
print('Yes')

Measured memory_bytes: 3060
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
using namespace std;
int main(){
  int a,b,k;cin>>a>>b>>k;
  int i;
  int count=0;
  for(i=b;i>0;i--){
    if(a%i==0 and b%i==0) count++;
    if(count==k) {cout<<i<<endl;break;}
  }


}


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

using namespace std;

int main() {
  long N, K; cin >> N >> K;
  vector<long> A(N); for (int i=0; i<N; i++) cin >> A[i];
  vector<long> F(N); for (int i=0; i<N; i++) cin >> F[i];
  
  sort(A.begin(), A.end());
  sort(F.rbegin(), F.rend());
  
  long falsy = -1, truthy = A[N-1] * F[0];
  
  while (truthy - falsy > 1) {
    long target = (truthy + falsy) / 2;
    
    long cost = 0;
    
    for (int i=0; i<N; i++) {
      if (A[i] * F[i] <= target) continue;
      
      cost += A[i] - (target / F[i]);
    }
    
    if (cost <= K) truthy = target;
    else falsy = target;
  }
  
  cout << truthy << endl;
}

Measured memory_bytes: 3328
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 32

ll POW[NUM];
int table[NUM];


int main(){

	POW[0] = 1;
	for(int i = 1; i < NUM; i++){
		POW[i] = 2*POW[i-1];
	}

	ll num;
	scanf("%lld",&num);

	int index = 0;

	for(int i = NUM-1; i >= 0; i--,index++){
		if(POW[i] <= num){

			table[index] = 1;
			num -= POW[i];

		}else{

			table[index] = 0;
		}
	}

	//そのまま
	for(int i = 0; i < NUM; i++){
		printf("%d",table[i]);
	}
	printf("\n");

	//反転
	for(int i = 0; i < NUM; i++){
		printf("%d",1-table[i]);
	}
	printf("\n");

	//左シフト
	for(int i = 1; i < NUM; i++){
		printf("%d",table[i]);
	}
	printf("0\n");

	//右シフト
	printf("0");
	for(int i = 0; i < NUM-1; i++){
		printf("%d",table[i]);
	}
	printf("\n");

	return 0;
}




Measured memory_bytes: 3212
#include<stdio.h>
int main(){
	int n,k;
	scanf("%d %d" , &n, &k);
	int arr[n];
	for(int i = 0 ; i < n ; i++){
		scanf("%d" , &arr[i]);
	}
	for(int i = k ; i < n ; i++){
		if(arr[i] > arr[i-k]){
			printf("Yes\n");
		}
		else{
			printf("No\n");
		}
	}	
	return 0;
}

Measured memory_bytes: 2404
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <iomanip>
#include <limits>
#include <list>
#include <queue>
#include <tuple>
#include <map>
using namespace std;
#define MOD (long long int)(1e9+7)
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define INF (int)(1e9+10)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))


int main(void){
	int n,k;
	cin>>n>>k;
	cout<<(2*k-1<=n?"YES":"NO")<<endl;
	return 0;
}


Measured memory_bytes: 256
/**
 * @author karayuu
 */
fun main(args: Array<String>) {
    val (W, H, x, y, r) = readLine()!!.split(" ").map { it.toInt() }

    val x_m = x - r
    val x_M = x + r
    val y_m = y - r
    val y_M = y + r

    if  (0 <= x_m && x_M <= W) {
        if (0 <= y_m && y_M <= H) {
            println("Yes")
            return
        }
    }
    println("No")
}


Measured memory_bytes: 33880
cost = 1000
out = 0
n, t = map(int, input().split())
list1 = [input() for i in range(n)]
for i in list1:
    a, b = map(int, i.split())
    if b<=t:
        if a<=cost:
            cost = a
    else:
        out +=1
    
if out==n:
    print('TLE')
else:
    print(cost)

Measured memory_bytes: 3060
N, M = gets.split.map(&:to_i)
AB = M.times.map{gets.split.map(&:to_i)}

class UFTree
  def initialize(n)
    @p = Array.new(n + 1, -1)
  end
  def find(x)
    @p[x] < 0 ? x : @p[x] = find(@p[x])
  end
  def unite(a, b)
    x = find(a)
    y = find(b)
    if x != y
      x, y = y, x if @p[y] < @p[x]
      @p[x] += @p[y]
      @p[y] = x
    end
  end
  def member(x)
    -@p[find(x)]
  end
end

tree = UFTree.new(N)
tot = N * (N - 1) / 2
ans = [tot]
AB[1...M].reverse.each do |a, b|
  if tree.find(a) != tree.find(b)
    tot -= tree.member(a) * tree.member(b)
    tree.unite(a, b)
  end
  ans << tot
end 

puts ans.reverse

Measured memory_bytes: 14132
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
#pragma GCC optimize("Ofast")

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
#define int long long int
typedef complex<long double> cd;
const long double pi=acos(-1);
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <db, db> pdd;
typedef vector<int> vi;
typedef vector<vector<int> > matrix;

#define m1 make_pair
#define pb push_back
#define flush fflush(stdout)
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,v) memset(x,v,sizeof(x))

#define fi first
#define se second
#define endl "\n"
#define debug(x) (cerr << #x << ": " << x << "\n")
#define setbits(x) __builtin_popcount(x)
#define setbitsll(x) __builtin_popcountll(x)
#define all(x) x.begin(),x.end()
#define pii pair<int,int>

inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0)return 1;ll p=power(a,n/2,m);p=(p%m*p%m)%m;if(n%2) return (p%m*a%m)%m;else return p;}
const double EPS = 1e-9;
const ll MOD =998244353;
const ll hell=1000000007;
const int INF = 1e18;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
std::mt19937 rng(seed);

double startTime;
double getCurrentTime() {
    return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
const int dx[] = { -1, 0, 1, 0 };
const int dy[] = { 0, 1, 0, -1 };

const int N=100005;

int dp[N][101];
int w[N+1],profit[N+1];

void solve()
{
    int n,W;
    cin>>n>>W;
    for(int i=1;i<=n;i++)
    {
        cin>>w[i]>>profit[i];
    }
    for(int i=1;i<=W;i++)
    {
        
        for(int j=1;j<=n;j++)
        {
            dp[i][j]=dp[i][j-1];
            if(w[j]<=i)
            {
                dp[i][j]=max(dp[i][j],dp[i-w[j]][j-1]+profit[j]);
            }
        }
        
    }
    cout<<dp[W][n]<<endl;
    
    
    
    
    
    
    
}


int32_t main()
{
    IOS;
    int t;
    t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}






Measured memory_bytes: 79232
sa=list(input())
sb=list(input())
sc=list(input())
t="a"
while True:
  if t=="a":
    if len(sa)==0:
      print("A")
      break
    t=sa.pop(0)
  elif t=="b":
    if len(sb)==0:
      print("B")
      break
    t=sb.pop(0)
  elif t=="c":
    if len(sc)==0:
      print("C")
      break
    t=sc.pop(0)

Measured memory_bytes: 3064
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main(){
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var X, Y, Z int
	fmt.Fscan(r, &X, &Y, &Z)
	X, Y = Y, X
	X, Z = Z, X
	fmt.Println(X, Y, Z)
}

Measured memory_bytes: 1840
using System.Text;
using System;

public class Hello
{
    public static void Main()
    {
        var s = Console.ReadLine().Trim();
        var q = int.Parse(Console.ReadLine().Trim());
        var rev = 0;
        var sb1 = new StringBuilder();
        var sb2 = new StringBuilder();
        for (int i = 0; i < q; i++)
        {
            string[] line = Console.ReadLine().Trim().Split(' ');
            if (line[0] == "1") rev = (rev + 1) % 2;
            else
            {
                var f = int.Parse(line[1]);
                var c = line[2];
                if ((rev == 0 && f == 1) | (rev == 1 && f == 2)) { sb1.Append(c); }
                else { sb2.Append(c); }
            }
        }
        if (rev == 0)
        {
            revWrite(sb1.ToString());
            Console.Write(s);
            Console.WriteLine(sb2);
        }
        else
        {
            revWrite(sb2.ToString());
            revWrite(s);
            Console.Write(sb1);
            Console.WriteLine();
        }
    }
    static void revWrite(string s)
    {
        var sL = s.Length;
        var sb = new StringBuilder();
        for (int i = sL - 1; i >= 0; i--) sb.Append(s[i]);
        Console.Write(sb);
    }
}


Measured memory_bytes: 17204
#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long int n,i,j,sum=0,x,r;
	map<int, int>m;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>x;
		sum+=x;
		m[x]++;
	}
	long long int q,b,c;
	cin>>q;
	for(i=0;i<q;i++)
	{
		cin>>b>>c;
		r=c-b;
		sum=sum+m[b]*r;
		m[c]=m[c]+m[b];
		m[b]=0;
		cout<<sum<<endl;
	}
	return 0;
}

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

using namespace std;

int main (void)
{
    string s1,s2;
    cin >> s1 >> s2;
    int count=0;
    sort(s1.begin(),s1.end());
    sort(s2.begin(),s2.end(),greater<char>());      
    if(s2.compare(s1)>0) cout << "Yes" << endl;
    else cout << "No" << endl;
    
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define si(a) scanf("%d",&a)
#define fore(i,a,b) for(int i=a;i<b;++i)
#define sorte(a) sort(a.begin(), a.end())
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
int main(){
	int n;
	si(n);
	vector<pll> v(n);
	fore(i,0,n) scanf("%lld %lld",&v[i].second,&v[i].first);
	sorte(v);
	bool sw = true;
	ll t = 0;
	fore(i,0,n){
		t = t+v[i].second;
		if(t>v[i].first){
			sw =false;
			break;
		}
	}
	if(sw)printf("Yes\n");
	else
		printf("No\n");
	return 0;
}

Measured memory_bytes: 3328
#include<cstdio>
#include<stack>
#include<queue>
#include<algorithm>

using namespace std;

class P{
	public:
	int cnt;
	int bef;
	stack< int > S[3];
};

int main(){
	int n,m;
	while(scanf("%d%d",&n,&m),(n|m)){
		P f;
		for(int i = 0;i < 3; i++){
			int x;
			scanf("%d",&x);
			for(int j = 0; j < x; j++){
				int tmp;
				scanf("%d",&tmp);
				f.S[i].push(tmp);
			}
		}
		f.cnt = 0;
		f.bef = -1;
		queue< P > Q;
		Q.push(f);
		bool flg = true;
		while( !Q.empty()){
			P p = Q.front();Q.pop();
			if(p.cnt > m){
				flg = false;
				break;
			}
			bool h = false;
			for(int i = 0; i < 3; i++){
				if(i == 1)continue;
				if(p.S[i].size() == n){
					printf("%d\n",p.cnt);
					h = true;
				}
			}
			if( h ){
				break;
			}
			int top0 = (p.S[0].size()>0)?p.S[0].top() : 0;
			int top1 = (p.S[1].size()>0)?p.S[1].top() : 0;
			int top2 = (p.S[2].size()>0)?p.S[2].top() : 0;
			int copy0 = top0 , copy1 = top1 , copy2 = top2;
			P sub;
			sub.cnt = p.cnt+1;
			if(top0 < top1 && p.bef != 0){
				p.S[1].pop();
				p.S[0].push(top1);
				for(int i = 0; i < 3; i++)sub.S[i] = p.S[i];
				sub.bef = 1;
				Q.push(sub);
				p.S[0].pop();
				p.S[1].push(copy1);
			}
			if(top0 > top1 && p.bef != 1) {
				p.S[0].pop();
				p.S[1].push(top0);
				for(int i = 0; i < 3; i++)sub.S[i] = p.S[i];
				sub.bef = 0;
				Q.push(sub);
				p.S[1].pop();
				p.S[0].push(copy0);
			}
			if(top1 < top2 && p.bef != 2){
				p.S[2].pop();
				p.S[1].push(top2);
				for(int i = 0;i < 3; i++)sub.S[i] = p.S[i];
				sub.bef = 3;
				Q.push(sub);
				p.S[1].pop();
				p.S[2].push(copy2);
			}
			if(top1 > top2 && p.bef != 3){	
				p.S[1].pop();
				p.S[2].push(top1);
				for(int i = 0; i < 3; i++)sub.S[i] = p.S[i];
				sub.bef = 2;
				Q.push(sub);
				p.S[2].pop();
				p.S[1].push(copy1);
			}
		}
		if( !flg )puts("-1");
	}
	return 0;
}

Measured memory_bytes: 1084
#include<iostream>
#include<vector>
using namespace std;
int main() {
	int n; cin >> n;
	vector<long long> h(n);
	for (int i = 0; i < n; i++) {
		cin >> h[i];
	}
	bool ans = true;
	for (int i = n - 1; i > 0; i--) {
		if (h[i] + 1 < h[i - 1]) {
			ans = false;
		}
		else if (h[i] < h[i - 1]) {
			h[i - 1] = h[i - 1] - 1;
		}

	}
	if (ans) { cout << "Yes" << endl; }
	else { cout << "No" << endl; }
	return 0;
}

Measured memory_bytes: 1024
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
 
int main()
{
    int H, W;
    int h, w;
 
    while (1) {
        scanf("%d%d", &H, &W);
        if (H == 0 && W == 0) break;
         
        for (w = 0; w < W; w++) putchar('#');
        putchar('\n');
 
        for (h = 2; h < H; h++) {
            putchar('#');
            for (w = 2; w < W; w++) putchar('.');
            putchar('#');
            putchar('\n');
        }
 
        for (w = 0; w < W; w++) putchar('#');
        putchar('\n');
 
        putchar('\n');
    }
    return 0;
}


Measured memory_bytes: 2048
#include<bits/stdc++.h>
using namespace std;
#define arep(i,x,n) for(int i=int(x);i<(int)(n);i++)
#define rep(i,n) for(long long i = 0;i < n;++i)
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9+7  
using ll = long long; 
using P=pair<int,int>;
using lP=pair<ll,ll>;
using fP=pair<double,double>;


int main(){
    int a,b;
    cin>>a>>b;
    if(a>2*b)cout<<a-2*b<<endl;
    else cout<<0<<endl;
    return 0;
}

Measured memory_bytes: 256
N,Q=map(int,input().split())
S=input()
Query=[list(map(int,input().split())) for i in range(Q)]

ANS=[0]*(N)
for i in range(N-1):
    if S[i:i+2]=="AC":
        ANS[i]=1

SUM=[0]
for i in range(N):
    SUM.append(SUM[-1]+ANS[i])

for x,y in Query:
    if S[y-1:y+1]=="AC":
        y-=1
    print(SUM[y]-SUM[x-1])


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

using namespace std;

int h, w, k;
bool s[10][1000];
int ans = 1e8;

int poww(int x, int w)
{
    int a = x;
    while (w > 0)
    {
        a *= x;
        w--;
    }
    return a;
}

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

    cin >> h >> w >> k;

    for (int i = 0; i < h; i++)
    {
        string st;
        cin >> st;
        for (int j = 0; j < w; j++)
            s[i][j] = (st[j] == '1');
    }

    for (int i = 0; i < poww(2, h - 1); i++)
    {
        int curr = __builtin_popcount(i);
        vector<int> v(__builtin_popcount(i) + 1, 0);

        for (int j = 0; j < w - 1; j++)
        {
            bool z = false;
            int nxt = 0;
            int vc = 0;
            for (int t = 0; t < h; t++)
            {
                v[vc] += s[t][j];
                nxt += s[t][j + 1];
                if (!z && v[vc] + nxt > k)
                {
                    curr++;
                    z = true;
                }
                if ((i >> t) & 1)
                {
                    vc++;
                    nxt = 0;
                }
            }
            bool broken = false;
            if (z)
            {
                for (int t = 0; t <= __builtin_popcount(i); t++)
                {
                    if (v[t] > k)
                    {
                        broken = true;
                        curr = 1e8;
                    }
                    v[t] = 0;
                }
            }
            if (broken)
                break;
        }
        ans = min(ans, curr);
    }

    cout << ans;

    return 0;
}


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

using namespace std;

int main()
{
    int n, k;
    cin >> n >> k;
    int cnt=0;
    while(n--)
    {
        int x;
        cin >> x;
        if((x-k)>=0)
            cnt++;
    }
    cout << cnt;
    return 0;
}


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

using namespace std;

#define int long long 
#define INF 1e9
#define LINF 1e18

typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<int, P> Pi;
typedef pair<P, P> PP;

const int MOD = 1e9 + 7;
const int dy[] = { 0, 0, 1, -1 };
const int dx[] = { 1, -1, 0, 0 };

template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }

int N, M;
P p[100000];

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

    cin >> N >> M;
    for(int i = 0; i < M; ++i) cin >> p[i].second >> p[i].first;
    sort(p, p + M);
    int res = 0, t = 0;
    for(int i = 0; i < M; ++i) {
        if(t <= p[i].second) {
            ++res;
            t = p[i].first;
        }
    }
    cout << res << endl;
    return 0;
}


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

#define ll long long
#define INF 99999999
#define rep(i,n) for(int i=0,temp=(int)(n);i<temp;++i) //vec.size()がnの時等の高速化
#define all(x) (x).begin(),(x).end()
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define vint vector<int>
#define vvint vector<vector<int>>

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; }
int gcd(int a,int b){return b?gcd(b,a%b):a;}


int main(){
  int a,b,n;cin >> n >> a >> b;
  cout << min(b,a*n) << endl;

  return 0;
}


Measured memory_bytes: 256
import bisect
N = int(input())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))

cnt = 0
for j in range(N):
  target = B[j]
  insert_indexA = bisect.bisect_left(A, target)
  insert_indexC = bisect.bisect_right(C, target)
  cnt = cnt + insert_indexA * (len(C)-insert_indexC)

print(cnt)

Measured memory_bytes: 23092
#include <iostream>
#include <cstdlib>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;


int main(int argc, char const *argv[])
{
	stack<int> S1;
	stack<pair<int, int> > S2;
	char ch;
	int sum = 0;
	for (int i = 0; cin >> ch; i++) {
		if (ch == '\\') S1.push(i); 
		else if (ch == '/' && S1.size() > 0) {
			int j = S1.top(); S1.pop();
			sum += i - j;
			int a = i -j;
			while (S2.size() > 0 && S2.top().first > j) {
				a += S2.top().second; S2.pop();
			}
			S2.push(make_pair(j, a));
		}
	}

	vector<int> ans;
	int t = 0;
	while (S2.size() > 0) {
		ans.push_back(S2.top().second);
		S2.pop();
	}
	reverse(ans.begin(), ans.end());
	cout << sum << endl;
	cout << ans.size();
	for (int i = 0; i < ans.size(); i++) {
		cout << " ";
		// t += ans[i];
		cout << ans[i];
	}
	cout << endl;

	return 0;
}

Measured memory_bytes: 1284
N,L = map(int,input().split())
A = [0]*N
for i in range(N):
  A[i]=L+i
  
if 0 in A:
  print(sum(A))
elif A[N-1] > 0:
  print(sum(A)-min(A))
else:
  print(sum(A)-max(A))  

Measured memory_bytes: 3060
#include <bits/stdc++.h>
#include <string>
using namespace std;
using ll=long long;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define inf 1001001001
typedef pair<int,int> P;
#define mod 1000000007



int main(){
   string s;
   cin >> s;
   int l=s.length();
   int ans;
   for(int i=1;i<l;i++){
       int len=l-i;
       if(len%2==1) continue;
       string a=s.substr(0,len/2);
       string b=s.substr(len/2,len/2);
       if(a==b){
           ans=i;
           break;
       }
   }
   cout << l-ans << endl;
    return 0;
}

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

int main() {
    const int MAX = 10001;
    bool isPrime[MAX];
    fill_n(isPrime, MAX, true);
    for (int i = 4; i < MAX; i += 2) {
        isPrime[i] = false;
    }
    for (int i = 3; i * i <= MAX; i += 2) {
        if (isPrime[i]) {
            for (int j = i * 2; j < MAX; j += i) {
                isPrime[j] = false;
            }
        }
    }
    while (true) {
        int n;
        cin >> n;
        if (n == 0) {
            break;
        }
        if (n % 2 == 0) {
            n--;
        }
        for (int i = n; i >= 5; i -= 2) {
            if (isPrime[i] && isPrime[i - 2]) {
                cout << i - 2 << " " << i << endl;
                break;
            }
        }
    }
}

Measured memory_bytes: 876
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb(x) push_back(x);
#define mp(x,y) make_pair(x,y)
#define mem(dp,a) memset(dp,a,sizeof dp);
#define all(a) a.begin(),a.end()
#define sall(a) sort(all(a))
#define X first
#define Y second
typedef long long int ll;
typedef pair<ll,ll> pp;

#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 boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree< pp , null_type,less< pp >, rb_tree_tag,tree_order_statistics_node_update>

ll power(ll b, ll exp){
    ll res = 1;
    while(exp > 0){
        if(exp % 2){
            res = 1LL * res * b % mod;
        }
        b = 1LL * b * b % mod;
        exp /= 2;
    }
    return res;
}

int main()
{
    boost
    ll t,n;
    cin >> t;
    ll ans=(t%1000);
    if( ans>0 )
        ans=1000-ans;
    cout << ans ;
    return 0;
}


Measured memory_bytes: 3636
/*** 挿入用ソース ***/
#include<iostream>
#include<queue>
#include<vector>
using namespace std;

#define MAX_NODE_NUM 10

/** ダイクストラ法：グラフ構造で２点間の最短経路を求めるアルゴリズム **/
//返り値：２点間の最小コスト
/* 前提 */
//1.繋がっていない点のコスト（cost[from][to]）は無限大（と考えていい数値）がセットされている
//2.コストがマイナスの経路は無い
int dijkstra(int start,int goal,int cost[MAX_NODE_NUM][MAX_NODE_NUM],int node_num){

  //探索済みか（最短経路が確定したか）
  bool searched[node_num];
  
  //ゴールまでのコスト
  int goal_cost;

  //優先度付きキューを使用する
  //(コスト、現在位置）のpairを要素とする
  //コストが小さい順のキューとなるよう宣言
  priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;

  if(start == goal)
    return 0;

  for (int i = 0; i < node_num; i++) {
    searched[i] = false;
  }

  
  //開始点をキューに入れる。コスト０
  q.push(make_pair(0,start));
  while(true){
    //コスト最小（キュー先頭）を取り出す
    pair<int,int> p = q.top();
    q.pop();

    //取り出したノードが探索済み（最短経路確定済み）なら無視する
    if(searched[p.second]){
      continue;
    }

    //現在ノードを探索済み（最短経路確定済み）とする
    searched[p.second] = true;
    
    if(p.second == goal){
      //ゴールに到達した場合、ループを終了する
      goal_cost = p.first;
      break;
    } else {
      //ゴールに到達しなかった場合、最短経路が確定していない隣接するノードをキューへ追加する
      for (int k = 0; k < node_num; k++) {
	if(!searched[k]){
	  q.push(make_pair(p.first+cost[p.second][k],k));
	}
      }
    }
  }

  return goal_cost;
}

int main(){
  int h,w;
  int c[10][10];
  int a[201][201];
  int cnt[10] = {0,0,0,0,0,0,0,0,0,0};
  int cost = 0;

  //入力（数字カウント含む）
  
  cin >> h >> w;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      cin >> c[i][j];
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      
      //数字別の件数カウント
      if(a[i][j] != -1){
	cnt[ a[i][j] ]++;
      }
    }
  }

  
  //計算
  
  for (int i = 0; i < 10; i++) {
    //元の数字をスタート、１をゴールとする最短経路問題と考える
    //i=1はゴール済みなので考えない。出現しない数字（cnt==0）も考えない
    if(i!=1 && cnt[i]>0){
      cost += cnt[i] * dijkstra(i,1,c,10);
    }
  }

  cout << cost << endl;
  
  return 0;
}


Measured memory_bytes: 384
#include<iostream>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<cctype>

using namespace std;

int main(){
	int a,b,c;
	
	cin>>a>>b>>c;
	if(a<=c && c<=b){
	cout<<"Yes"<<endl;
	}else cout<<"No"<<endl;
	
	
return 0;

}




Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define rep1(i,n) for(int i=1; i<=(n); ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define sz(v) (int)v.size()
#define pb push_back
using namespace std;
struct UnionFind {
  vector<int> par;
  vector<int> rank;
  vector<int> num;
  
  UnionFind(int N) : par(N),rank(N),num(N) {
        for(int i = 0; i < N; i++){ 
          par[i] = i;
          rank[i] = 0;
          num[i] = 1;
        }
   }
      
  void init(int N){
        for(int i = 0; i < N; i++){ 
          par[i] = i;
          rank[i] = 0;
          num[i] = 1;
        }
  }
  
  int root(int x) {
    if (par[x] == x) return x;
    return par[x] = root(par[x]);
  }
  
  int ranker(int x){
    x = root(x);
    return rank[x];
  }
  
  int number(int x){
    x = root(x);
    return num[x];
   }
  
  void unite(int x, int y){
    int rx = root(x);
    int ry = root(y);
    if ( rx == ry ) return;
    if ( rank[rx] < rank[ry] ){
      par[rx] = ry;
      num[ry] += num[rx];
      num[rx] = 0;
    } else {
      par[ry] = rx;
      num[rx] += num[ry];
      num[ry] = 0;
      if ( rank[rx] == rank[ry] ) rank[rx]++;
    }
   }
  
  bool same(int x, int y){
    return root(x) == root(y);
  }
};

int N,M,temp,x,y;

int main(){
    cin >> N >> M;
    UnionFind tree(N+5);
    vector<int> p(N+1);
    rep1(i,N) cin >> p[i];
    rep(i,M){
        cin >> x >> y;
        tree.unite(x,y);
    }
    int ct = 0;
    rep1(i,N) if(tree.same(i,p.at(i))) ct++;
    cout << ct << endl;
    
}

Measured memory_bytes: 1792
#include <stdio.h>
#include <stdlib.h>

int main(){
        int n,height,width;
        int ret;

        ret = scanf("%d%d%d", &n,&height,&width);
        if(ret == EOF) return -1;


        int A,B;
        int count=0;
        for(int i=0; i<n; i++){
                ret = scanf("%d%d", &A, &B);
                if(ret == EOF) return -1;
                if(A>=height && B>=width){
                        count++;
                }
        }

        printf("%d\n", count);

        return 0;
}


Measured memory_bytes: 128
S = list(input())
flag = False
count = 0
max_ = 0
for i in range(3):
    if flag == True:
        if S[i] == "R":
            count += 1
        else:
            flag = False
            count = 0
    else:
        if S[i] == "R":
            count += 1
            flag = True
    if count > max_:
        max_ = count
print(max_)

Measured memory_bytes: 9060
N=int(input())
A1=list(map(int,input().split()))
A2=list(map(int,input().split()))

A2=A2[::-1]
for i in range(N-1):
    A1[i+1]=A1[i+1]+A1[i]
    A2[i+1]=A2[i+1]+A2[i]
A2=A2[::-1]

P=0
for i in range(N):
    P=max(P,A1[i]+A2[i])
print(P)

Measured memory_bytes: 3064
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#define rad M_PI/3
#define N 500001
#define Max 10000
#define NTL -1
int n;
int heap[Max];
int main(){
  int i,parent,left,right;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d",&heap[i]);
  }
  for(i=0;i<n;i++){
    printf("node %d: key = %d, ",i+1, heap[i]);
    parent = (i-1)/2;
    left = i*2+1;
    right = i*2+2;
    if(i!=0) printf("parent key = %d, ",heap[parent]);
    if(left<n)printf("left key = %d, ",heap[left]);
    if(right < n) printf("right key = %d, ", heap[right]);
    printf("\n");
  }
  return 0;
}


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

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

int main(){
  string s; cin >> s;
  long long c = 0, ans = 0;
  rep(i,s.size()){
    if(s[i] == 'B') c++;
    else ans += c;
  }
  cout << ans << endl;
}


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

int main()
{
	int n;cin>>n;
	for ( int i=0; i<n; i++ )
	{
		double x1,y1,x2,y2,x3,y3;
		cin>>x1>>y1>>x2>>y2>>x3>>y3;
		
		double a1=2*(x2-x1);
		double b1=2*(y2-y1);
		double c1=x1*x1-x2*x2+y1*y1-y2*y2;
		double a2=2*(x3-x1);
		double b2=2*(y3-y1);
		double c2=x1*x1-x3*x3+y1*y1-y3*y3;
		double x=(b1*c2-b2*c1)/(a1*b2-a2*b1);
		double y=(c1*a2-c2*a1)/(a1*b2-a2*b1);
		//	http://www.h6.dion.ne.jp/~jpe02_tn/ki-5.html

		double r = sqrt((x1-x)*(x1-x)+(y1-y)*(y1-y));

		printf( "%.3f %.3f %.3f\n", x, y, r );
	}
	return 0;

}

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define contains(v, t) (find(v.begin(), v.end(), t) != v.end())
#define MOD 1000000007
typedef long long ll;
typedef unsigned long long ull;

int main() {
  string s;
  cin >> s;
  if (s.length() % 2 == 1) {
    cout << "No";
    return 0;
  }

  for (int i = 0; i < s.length(); i += 2) {
    string t = string();
    t += s[i];
    t += s[i + 1];
    if (t != "hi") {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes";
}

Measured memory_bytes: 256
import Data.Array
import qualified Data.IntSet as S

main = interact $ show . sol . map read . lines

sol (n:as) = f S.empty (a!1) 1
  where
  a = listArray (1,n) as
  f s i c
    | i `S.member` s  = -1
    | i == 2          = c
    | otherwise       = f (i `S.insert` s) (a!i) (c+1)

Measured memory_bytes: 35324
#include <bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i=0;i<int(N);++i)
using i64 = int64_t;
using ll = long long;
int main() {
  int n,k;
  cin >> n >> k;
  vector<int> p(n);
  rep(i,n){
    cin >> p.at(i);
  }
  sort(p.begin(),p.end());
  int ans = 0;
  rep(i,k){
    ans += p.at(i);
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 3628
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
// #include <atcoder/all>

using boost::multiprecision::cpp_int;
using namespace std;
// using namespace atcoder;

#if __has_include("print.hpp")
  #include "print.hpp"
#endif

#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MOD 1000000007

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 ll;
typedef pair<ll, ll> p;
/* warshall_floyd(dist,prev)
    入力：初期化した dist, prev
    計算量：O(|V|^3)
    副作用：dis[i][j]にiからjへの最短路のコストを格納、prev[i][j]にiからjへの最短路でのjの1つ前の点を格納
*/
void warshall_floyd(vector<vector<long long>> &dist, vector<vector<long long>> &prev) {
  int V = dist.size();
  for (int k = 0; k < V; k++) {
    for (int i = 0; i < V; i++) {
      for (int j = 0; j < V; j++) {
        if (dist[i][k] + dist[k][j] < dist[i][j]) {
          dist[i][j] = dist[i][k] + dist[k][j];
          prev[i][j] = prev[k][j];
        }
      }
    }
  }
}

/* get_path(prev, s, t)
   入力：warshall_floyd で得た prev, スタート s, ゴール t
   出力：s から t への最短路のパス
   */
vector<int> get_path(const vector<vector<long long>> &prev, int s, int t) {
  vector<int> path;
  for (int cur = t; cur != s; cur = prev[s][cur]) {
    path.push_back(cur);
  }
  path.push_back(s);
  reverse(path.begin(), path.end());
  return path;
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<ll>> dist(n, vector<ll>(n, INT_MAX)), prev(n, vector<ll>(n));
  rep(i, n) dist[i][i] = 0;
  vector<vector<int>> used(n, vector<int>(n, -1));
  rep(i, m){
    int from, to, cost;
    cin >> from >> to >> cost;
    from--; to--;
    dist[from][to] = dist[to][from] = cost;
    prev[from][to] = from;
    prev[to][from] = to;
    used[from][to] = 0;
    used[to][from] = 0;
  }

  warshall_floyd(dist, prev);

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if(i == j) continue;
      auto path = get_path(prev, i, j);
      rep(k, (int)path.size()-1) used[path[k]][path[k+1]] = true;
    }
  }

  ll res = 0;
  rep(i, n) rep(j, n) if(i!=j && used[i][j] != -1) res += !used[i][j];
  res /= 2;

  cout << res << endl;


}


Measured memory_bytes: 3856
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(long long i = 0;i < n;i++)
#define repr(i, n) for(int i = n;i >= 0;i--)
#define Sort(v, n) sort(v, v+n);
#define VSort(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999
#define cY cout<<"Yes";
#define cN cout<<"No";
#define cA(a) cout<<a;
const ll MOD = 1000000007;



int main() {
    ll n;
    cin >> n;
    ll a[n];
    bool flag = 0;
    rep(i,n) {
        cin >> a[i];
        if(a[i]%2 == 1) flag = 1;
    }
    if(flag) {
        cout << "first";
    } else {
        cout << "second";
    }
}



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

#define M_PI       3.14159265358979323846   // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a,n) for(ll a = 0;a < n;a++)
#define repi(a,b,n) for(ll a = b;a < n;a++)

using namespace std;

static const ll INF = 1e15;

const ll mod = 1000000007;

int main() {
	ll a;
	cin >> a;
	ll b = a;
	b += a * a;
	b += a * a * a;
	cout << b << endl;
	return 0;
}


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

using namespace std;

typedef long long int ll;

void solve()
{
	int n;
	cin >> n;
    int arr[n-1], arr1[n+1] = {0};
    for(int i = 0 ; i < n - 1; i++) cin >> arr[i];
    unordered_map<int, int> mp1;
    for(int i = 0; i < n - 1; i++)
    {
    	mp1[arr[i]]++;
    }
    for(auto i : mp1)
    {
    	arr1[i.first] = i.second;
    }
    for(int i = 1; i <= n; i++)
    {
    	cout << arr1[i] << "\n";
    }
}

int main()
{
	int t = 1;
	//cin >> t;
	for(int i = 0; i < t; i++)
	{
		///cout << "Case #" << i + 1 << ": ";
		solve();
	}
	return 0;
}



Measured memory_bytes: 12960
#include<iostream>
#include<cstring>
using namespace std;
int main(){
    string s,p;
    cin >> s >> p;
    string ans = "No";
    for(int i = 0;i < (int)s.length();i++){
        bool flag = true;
        for(int j = 0;j < (int)p.length();j++){
            if(s[(i+j)%(int)s.length()]!=p[j]){
                flag = false;
                break;
            }
        }
        if(flag){
            ans = "Yes";
            break;
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3216