akhauriyash/Code-Regression huggingface dataset:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
	static int mod=1000000007;
	static Scanner scan=new Scanner(System.in);
	static long gcd (long a, long b) {return b>0?gcd(b,a%b):a;}
	static long lcm (long a, long b) {return a*b/gcd(a,b);}
	static int max(int a,int b) {return a>b?a:b;}
	static int min(int a,int b) {return a<b?a:b;}
	static long factorial(int i) {return i==1?1:i*factorial(i-1);}
	static int lower_bound(int a[],int key) {
		int low=0,high=a.length;
		while(low<high) {
			int mid=((high-low)/2)+low;
			if(a[mid]<=key)low=mid+1;
			else high=mid;
		}
		return high;
		}
	static int upper_bound(int a[],int key) {
		int low=0,high=a.length;
		while(low<high) {
			int mid=((high-low)/2)+low;
			if(a[mid]<key)low=mid+1;
			else high=mid;
		}
		return high;
		}
	static boolean isPrime (int n) {
		if (n==2) return true;
		if (n<2 || n%2==0) return false;
		double d = Math.sqrt(n);
		for (int i=3; i<=d; i+=2)if(n%i==0){return false;}
		return true;
	}
	static int upper_division(int a,int b) {
		if(a%b==0) {
			return a/b;
		}
		else {
			return a/b+1;
		}
	}
	static long lupper_division(long a,long b) {
		if(a%b==0) {
			return a/b;
			}
		else {
			return a/b+1;
		}
	}
	static long lmax(long a,long b) {return Math.max(a, b);}
	static long lmin(long a,long b) {return Math.min(a, b);}
	static int[] setArray(int a) {
		int b[]=new int[a];
		for(int i=0;i<a;i++) {
			b[i]=scan.nextInt();
		}
		return b;
	}
	static String reverce(String str) {
		String strr="";
		for(int i=str.length()-1;i>=0;i--) {
			strr+=str.charAt(i);
		}
		return strr;
	}
	static long modpow(long a,long n) {
		if(n==0) return 1;
		else if(n==1)return a;
		long tmp=modpow(a,n/2);
		tmp%=mod;
		tmp*=tmp;
		tmp%=mod;
		if(n%2==0)return tmp;
		else return (tmp*a)%mod;
	}
	public static void main(String[] args) {
		List<Boolean>visited=new ArrayList<Boolean>();
		List<ArrayList<Integer>>node=new ArrayList<ArrayList<Integer>>();
		int a=scan.nextInt();
		int b=scan.nextInt();
		for(int i=0;i<a;i++) {
			visited.add(false);
			node.add(new ArrayList<Integer>());
		}
		for(int i=0;i<b;i++) {
			int c=scan.nextInt();
			int d=scan.nextInt();
			ArrayList<Integer> list=node.get(c-1);
			list.add(d);
			node.set(c-1, list);
		}
		ArrayList<Integer> list=node.get(0);
		for(int i=0;i<list.size();i++) {
			int x=list.get(i);
			for(int j=0;j<node.get(x-1).size();j++) {
				if(node.get(x-1).get(j)==a) {
					System.out.println("POSSIBLE");
					return;
				}
			}
		}
		System.out.println("IMPOSSIBLE");
	}
}


Measured memory_bytes: 122416
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <climits>


int main(){
	
	int H, W;
	std::cin >> H >> W;

	std::vector<std::string> buf;
	for (int i = 0; i < H; ++i) {
		std::string input;
		std::cin >> input;
		buf.push_back(input);
	}

	for (int i = 0; i < W + 2; ++i) { std::cout << '#'; }
	std::cout << std::endl;

	for (int i = 0; i < H; ++i) {
		std::cout << '#' << buf[i] << '#' << std::endl;
	}

	for (int i = 0; i < W + 2; ++i) { std::cout << '#'; }
	std::cout << std::endl;

	return 0;
}



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

#define FOR(i,l,r) for(int i = (int) (l);i < (int) (r);i++)
#define ALL(x) x.begin(),x.end()
template<typename T> bool chmax(T& a,const T& b){ return a < b ? (a = b,true) : false; }
template<typename T> bool chmin(T& a,const T& b){ return b < a ? (a = b,true) : false; }
typedef long long ll;

int N,M;
vector<string> S;
bool vis [100] [100];
const int dx [] = {0,1,0,-1};
const int dy [] = {-1,0,1,0};

bool in_range(int y,int x)
{
	return y >= 0 && y < N && x >= 0 && x < M;
}

void dfs(int y,int x)
{
	if(vis [y] [x]) return;
	vis [y] [x] = true;
	FOR(i,0,4){
		int ny = y + dy [i],nx = x + dx [i];
		if(in_range(ny,nx) && S [y] [x] == S [ny] [nx]){
			dfs(ny,nx);
		}
	}
}

int main()
{
	while(cin >> N >> M,N){
		S.assign(N,"");
		FOR(i,0,N){
			cin >> S [i];
		}
		memset(vis,0,sizeof(vis));
		int ans = 0;
		FOR(i,0,N) FOR(j,0,M) if(vis [i] [j] == false){
			dfs(i,j);
			ans++;
		}
		cout << ans << endl;
	}

	return 0;
}

Measured memory_bytes: 4200
// テ・ツ淞コテヲツ慊ャテ」ツδ?」ツδウテ」ツδ療」ツδャテ」ツδシテ」ツδ?(テァツクツョテ・ツーツ湘ァツ可?

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define int long long
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
typedef pair<int, int> pii;
typedef long long ll;
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int W, H, C[4];
int board[35][35];
int cost[35][35][4];

struct Elem {
    int x, y, dir;
};

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

signed main() {
    while(cin >> W >> H, W || H) {
        rep(i,0,H) rep(j,0,W) {
            cin >> board[i][j];
            rep(k,0,4) cost[i][j][k] = INF;
        }
        rep(i,0,4) cin >> C[i];

        // North, East, South, West
        cost[0][0][1] = 0;
        queue<Elem> q;
        q.push(Elem{0, 0, 1});

        while(!q.empty()) {
            Elem t = q.front(); q.pop();
            int curcost = cost[t.x][t.y][t.dir];
            int ndir = (board[t.x][t.y] + t.dir) % 4;
            if(board[t.x][t.y] == 4) ndir = -1;

            rep(i,0,4) {
                int k = (t.dir + i) % 4;
                int c = (ndir != k ? C[i] : 0);
                int nx = t.x + dx[k], ny = t.y + dy[k];
                if(nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
                if(cost[nx][ny][k] > curcost + c) {
                    cost[nx][ny][k] = curcost + c;
                    q.push(Elem{nx, ny, k});
                }
            }
        }

        // rep(i,0,H) rep(j,0,W) rep(k,0,4) printf("debug: cost[%lld][%lld][%lld] = %lld\n", i, j, k, cost[i][j][k]);

        int ans = INF;
        rep(i,0,4) chmin(ans, cost[H-1][W-1][i]);
        cout << ans << endl;
    }
    return 0;
}

Measured memory_bytes: 3136
import random

a,b = map(int,input().split())

def is_prime(q,k=50):
    q = abs(q)
    #計算するまでもなく判定できるものははじく
    if q == 2: return True
    if q < 2 or q&1 == 0: return False

    #n-1=2^s*dとし（但しaは整数、dは奇数)、dを求める
    d = (q-1)>>1
    while d&1 == 0:
        d >>= 1
    
    #判定をk回繰り返す
    for i in range(k):
        a = random.randint(1,q-1)
        t = d
        y = pow(a,t,q)
        #[0,s-1]の範囲すべてをチェック
        while t != q-1 and y != 1 and y != q-1: 
            y = pow(y,2,q)
            t <<= 1
        if y != q-1 and t&1 == 0:
            return False
    return True

def factorize(n):
    fct = []  # prime factor
    b, e = 2, 0  # base, exponent
    while b * b <= n:
        while n % b == 0:
            n = n // b
            e = e + 1
        if e > 0:
            fct.append((b, e))
        b, e = b + 1, 0
    if n > 1:
        fct.append((n, 1))
    return fct


def num(fct):
    a = 1
    for base, exponent in fct:
        a = a * base**exponent
    return a

def divisorize(fct):
    b, e = fct.pop()  # base, exponent
    pre_div = divisorize(fct) if fct else [[]]
    suf_div = [[(b, k)] for k in range(e + 1)]
    return [pre + suf for pre in pre_div for suf in suf_div]

x=[]
y=[]
z=[]

if a==1:
  x.append(1)
else:
  fct_a = factorize(a)
  for div in divisorize(fct_a):
    x.append(num(div))

if b==1:
  y.append(1)
else:
  fct_b = factorize(b)
  for div in divisorize(fct_b):
    y.append(num(div))

z=x+y
w=[m for m in set(z) if z.count(m) > 1]

c=0

for i in range(0,len(w)):
  if is_prime(w[i]):
    c+=1
print(c+1)

Measured memory_bytes: 5736
n, m = map(int, input().split())

l_max = 0
r_min = 10**9

for i in range(m):
    l, r = map(int, input().split())
    l_max = max(l_max, l)
    r_min = min(r_min, r)

ans = 0
for i in range(l_max, r_min+1):
    ans += 1

print(ans)

Measured memory_bytes: 3064
int main(void)
{
	char number[1001];
	int sum,i;

	while (1) {
		scanf("%s", number);
		i = 0;
		sum = 0;

		while (number[i] != 0)
			sum += number[i++] - '0';

		if (!sum)	break;

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

Measured memory_bytes: 536
#include<stdio.h>
#include<string.h>

int main(void)
{
  int A[4];
  int B[4];
  int hit, blow;
  int i, j;

  hit = blow = 0;

  memset(A, 0, sizeof(A));
  memset(B, 0, sizeof(B));

  while(scanf("%d %d %d %d", &A[0], &A[1], &A[2], &A[3]) != EOF){
    for (i = 0;i < 4;i++){
      scanf("%d", &B[i]);
    }

    for (i = 0;i < 4;i++){
      if (A[i] == B[i]){
	hit++;
      }
    }

    for (i = 0;i < 4;i++){
      for (j = 0;j < 4;j++){
	if(A[i] == B[j]){
	  blow++;
	}
      }
    }
  
    printf("%d %d\n", hit, blow - hit);
    
    hit = blow = 0;
    memset(A, 0, sizeof(A));
    memset(B, 0, sizeof(B));
  }

  return 0;
}

Measured memory_bytes: 0
/*
    written by Pankaj Kumar.
    country:-INDIA
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
typedef set<pair<int,int>> spi;
typedef set<pair<ll,ll>> spl;
typedef vector<pair<int,int>> vpi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<pair<ll,ll>> vpl;
typedef vector<string> vs;
typedef map<ll,ll> ml;
typedef set<string> ss;
typedef set<char>sc;
typedef set<int> si;
typedef set<ll> sl;
#define pan cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
// define values.
#define mod 1000000007
#define phi 1.618
/* Bit-Stuff */
#define get_set_bits(a) (__builtin_popcount(a))
#define get_set_bitsll(a) ( __builtin_popcountll(a))
#define get_trail_zero(a) (__builtin_ctz(a))
#define get_lead_zero(a) (__builtin_clz(a))
#define get_parity(a) (__builtin_parity(a))
/*  Abbrevations  */
#define ff first
#define ss second
#define mp make_pair
#define line cout<<endl;
#define pb push_back
#define Endl "\n"
// loops
#define loop(i,start,end) for(ll i=ll(start);i<ll(end);i++)
#define loop0(num) for(ll i=0;i<ll(num);i++)
#define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];
// Some print
#define no cout<<"NO"<<endl;
#define yes cout<<"YES"<<endl;
#define cc ll test;cin>>test;while(test--)
// sort
#define all(V) (V).begin(),(V).end()
#define srt(V) sort(all(V))
#define srtGreat(V) sort(all(V),greater<ll>())
// function

ll power(ll x,ll y)
{
    ll res=1;
    while(y>0)
    {
        if(y%2==1)res*=x;
        y/=2; x*=x;
    }
    return res;
}
/* ascii value 
A=65,Z=90,a=97,z=122
*/
/*  -----------------------------------------------------------------------------------*/
ll solve()
{
    ll d,t,s;
    cin>>d>>t>>s;
    if((s*t)>=d)
        cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;
    return 0;
}

int main()
{
    //freopen("input.txt"a, "r", stdin);
    // cc
    // {
        solve();
    // }
}


Measured memory_bytes: 3632
n = int(input())
a = list(map(int, input().split()))

ave = sum(a)//len(a)
ans = int(1000000000)
for i in range(ave-2, ave +3):
    cost = 0
    for j in range(n):
        cost += (a[j]-i)*(a[j]-i)
    if cost < ans:
        ans = cost
print(ans)

Measured memory_bytes: 3060
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a


n = int(input())
cnt = [0]*1001
div = []
for i in range(2, n+1):
    div += prime_factorize(i)

for x in div:
    cnt[x] += 1

m = 1
for i in range(len(cnt)):
    if cnt[i] > 0:
        m *= (cnt[i]+1)
print(m % (10**9+7))

Measured memory_bytes: 9188
/*ex3_4
series2*/
 
# include <stdio.h>
int main(void){
      //各文字の箱を作って読み取り
    int N;
    scanf("%d", &N);
    int i=1;//調べている九九のうちの片方の数字(1から9)
    while(i<10){//割り切れるまで確かめる
        if (N%i==0 && N/i<10){//Nが1から9の中で割り切れるものがあり、かつその商が9以下の時、Yesである。
            printf("Yes\n");
            return 0;//Yesの時処理を終了させる
        }
        i++;
    }
    //いずれにも該当しない時はNoを出力する。
    printf("No\n");
    return 0;
}

Measured memory_bytes: 128
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int qpow(int x,int k)
{
    int r=1;
    while(k)
    {
        if(k&1)r=1ll*r*x%mod;
        k>>=1;x=1ll*x*x%mod;
    }
    return r;
}
int add(int x,int y){return x+y<mod?x+y:x+y-mod;}
int n;
char s[300300];

//int X[6] = {18, 40, 23, 2333333, 58, 720};
int X[6]={233,1919,114514,817,1001,9527};
int inv[6];
struct Node {
    int v[6];
    bool operator < (const Node &o)const{
        for(int i=0;i<6;++i)
            if(v[i]!=o.v[i])return v[i]<o.v[i];
        return 0;
    }
    void clear(){memset(v,0,sizeof(v));}
    void f(char c)
    {
        if(c=='+') for(int i=0;i<6;++i)v[i]=add(v[i],1);
        else if(c=='-') for(int i=0;i<6;++i)v[i]=add(v[i],mod-1);
        else if(c=='>') for(int i=0;i<6;++i)v[i]=1ll*v[i]*X[i]%mod;
        else if(c=='<') for(int i=0;i<6;++i)v[i]=1ll*v[i]*inv[i]%mod;
    }
};
map<Node,int>mp;
struct PNN{
    Node k,b;
    void clear() { for(int i=0;i<6;++i)k.v[i]=1,b.v[i]=0; }
    void f(char c)
    {
        if(c=='+')for(int i=0;i<6;++i)b.v[i]=add(b.v[i],mod-k.v[i]);
        else if(c=='-')for(int i=0;i<6;++i)b.v[i]=add(b.v[i],k.v[i]);
        else if(c=='>')for(int i=0;i<6;++i)k.v[i]=1ll*k.v[i]*inv[i]%mod;
        else if(c=='<')for(int i=0;i<6;++i)k.v[i]=1ll*k.v[i]*X[i]%mod;
    }
    Node calc(Node x)
    {
        Node res;
        for(int i=0;i<6;++i)
            res.v[i]=add(1ll*k.v[i]*x.v[i]%mod,b.v[i])%mod;
        return res;
    }
};
int main()
{
    for(int i=0;i<6;++i)inv[i]=qpow(X[i],mod-2);
    scanf("%d",&n);
    scanf("%s",s+1);
    Node C;C.clear();
    for(int i=n;i;--i)C.f(s[i]);
//    for(int i=0;i<6;++i)cout<<C.v[i]<<" ";cout<<endl;
    Node o;o.clear();
    mp[o]++;
    PNN cur;cur.clear();
    long long ans=0;
    for(int i=n;i;--i)
    {
        cur.f(s[i]);
//        for(int j=0;j<6;++j)cout<<cur.k.v[j]<<" "<<cur.b.v[j]<<endl;cout<<endl;
        Node tt=cur.calc(C);
//        for(int j=0;j<6;++j)cout<<tt.v[j]<<" ";cout<<endl;
        ans+=mp[cur.calc(C)];
        mp[cur.calc(o)]++;
    }
    printf("%lld\n",ans);
    return 0;
}


Measured memory_bytes: 39552
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iterator>
using namespace std;

int main() {

	int n, m, l;
	cin >> n >> m >> l;

	vector<vector<int>> a(n), b(m);
	for (auto& row : a) {
		copy_n(istream_iterator<int>(cin), m, back_inserter(row));
	}
	for (auto& row : b) {
		copy_n(istream_iterator<int>(cin), l, back_inserter(row));
	}

	vector<vector<long>> c(n);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < l; j++) {
			long cij = 0;
			for (int k = 0; k < m; k++) {
				cij += a[i][k] * b[k][j];
			}
			c[i].push_back(cij);
		}
	}

	for (auto& row : c) {
		for (size_t i = 0; i < row.size(); i++) {
			cout << ((i == 0) ? "" : " ") << row[i];
		}
		cout << endl;
	}

	return 0;
}



Measured memory_bytes: 3244
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int main() {
  string s;
  cin >> s;
  for(int i=0; i<s.size()-1; i++) {
    if(s[i]=='A'&&s[i+1]=='C') {
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout << "No" << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int ans=0,ans1=0; 
int main(){
    int n,m;
    cin >> n >> m;
    int a[55];
    for (int i=1;i<=n;i++) cin >> a[i];
    sort(a+1,a+1+n);
    for (int i=n;i>=n-m+1;i--) ans+=a[i];
    cout << ans;
} 

Measured memory_bytes: 256
#include<cstdio>
#include<algorithm>
#include<functional>
#include<cstring>
using namespace std;
int main(void)
{
	int i,j,p,len,flg,x;
	char s[201];
	scanf("%s",s);
	len=strlen(s);
	if(len%2==0)	x=len/2-1;
	else x=len/2;
	for(i=x;i>=1;i--)	{
		flg=1;
		for(j=0;j<i;j++)	{
			if(s[j]!=s[j+i])	{
				flg=0;	break;
			}
		}
		if(flg==1)	{
			p=i;
			break;
		}
	}
	printf("%d\n",p*2);

	return 0;
}

Measured memory_bytes: 128
#include<cmath>
#include<math.h>
#include<ctype.h>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cwchar>
#include<cwctype>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int n,a[100010],b[100010],du[100010];
int cnt,head[100010],to[200010],nxt[200010];
void add(int x,int y)
{
	cnt++;
	to[cnt]=y;
	nxt[cnt]=head[x];
	head[x]=cnt;
}
void dfs(int x,int fa)
{
	if (du[x]==1)
	{
		b[x]=a[x];
		return;
	}
	b[x]=2*a[x];
	for (int i=head[x];i;i=nxt[i])
	{
		int y=to[i];
		if (y==fa)
		{
			continue;
		}
		dfs(y,x);
		b[x]-=b[y];
	}
	if (b[x]>a[x])
	{
		puts("NO");
		exit(0);
	}
}
void work(int x,int fa)
{
	if (du[x]==1)
	{
		return;
	}
	int cnt=0;
	for (int i=head[x];i;i=nxt[i])
	{
		int y=to[i];
		if (y==fa)
		{
			continue;
		}
		work(y,x);
		if (b[x]+b[y]>a[x])
		{
			cnt+=b[x]+b[y]-a[x];
		}
	}
	if (cnt>b[x])
	{
		puts("NO");
		exit(0);
	}
}
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
	}
	for (int i=1;i<n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		add(x,y);
		add(y,x);
		du[x]++;
		du[y]++;
	}
	if (n==2)
	{
		if (a[1]==a[2])
		{
			puts("YES");
		}
		else
		{
			puts("NO");
		}
		return 0;
	}
	int rt=1;
	for (int i=1;i<=n;i++)
	{
		if (du[i]>1)
		{
			rt=i;
			break;
		}
	}
	dfs(rt,0);
	if (b[rt])
	{
		puts("NO");
		return 0;
	}
	work(rt,0);
	puts("YES");
    return 0;
}
//忽晴忽雨的江湖,祝你有梦为马,永远随处可栖。
//——大冰《他们最幸福》

Measured memory_bytes: 8704
/* 2017.3.18 Celicath */
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <stdint.h>
#include <queue>

int main()
{
	int X;
	scanf("%d", &X);

	unsigned int min = 0;
	unsigned int max = 50000;
	while (min < max)
	{
		unsigned int mid = (min + max) / 2;
		unsigned int c = mid * (mid + 1) / 2;
		if (c > X)
			max = mid;
		else if (c < X)
			min = mid + 1;
		else min = max = mid;
	}

	printf("%d", max);
	return -0;
}


Measured memory_bytes: 128
#include "bits/stdc++.h"

#ifdef PRINTERS
#include "printers.hpp"
using namespace printers;
#define tr(a)		cerr<<#a<<" : "<<a<<endl
#else
#define tr(a)    
#endif

#define ll          long long
#define pb          push_back
#define mp          make_pair
#define pii         pair<int,int>
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define hell        1000000007
#define endl        '\n'
#define rep(i,a,b)	for(int i=a;i<b;i++)
using namespace std;

void solve(){
	int N,M;
	cin>>N>>M;
	vi p(N+1);
	rep(i,1,N+1){
		int temp;
		cin>>temp;
		p[temp]=i;
	}
	vi dsu(N+1,-1);
	while(M--){
		int u,v;
		cin>>u>>v;
		while(dsu[u]>0)u=dsu[u];
		while(dsu[v]>0)v=dsu[v];
		if(u==v)continue;
		if(dsu[u]>dsu[v])swap(u,v);
		dsu[u]+=dsu[v];
		dsu[v]=u;
	}
	int ans=0;
	rep(i,1,N+1){
		int u=i;
		int v=p[i];
		while(dsu[u]>0)u=dsu[u];
		while(dsu[v]>0)v=dsu[v];
		if(u==v)ans++;
	}
	cout<<ans<<endl;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t=1;
	// cin>>t;
	while(t--){
		solve();
	}
	return 0;
}

Measured memory_bytes: 1024
#include <iostream>
using namespace std;
int main(){
  int H,W,x,y;
  double ans;
  cin >>W >> H >> x >> y;
  ans = (double)H*W/2;
  int flg;
  if ( (2*x == W) && (2*y == H) ){
	flg = 1;
  }else{
    flg = 0;
  }
  
  printf("%.10f %d\n", ans, flg);
  return 0;
  //cout << ans << ' ' <<flg <<endl;  
}

Measured memory_bytes: 256
import java.util.Scanner;

class Main{
  public static void main(String[] args){
    Scanner sc= new Scanner(System.in);
    int n = sc.nextInt();
    int[] x = new int[n];
    
    for(int i=0; i<n; i++){
      x[i] = sc.nextInt();
    }
    
    int min = x[0];
    int max = x[0];
    
    for(int i=0; i<n; i++){
      if(min > x[i]){
        min = x[i];
      }
      
      if(max < x[i]){
        max = x[i];
      }
    }
    
    int total = 0;
    int st = 2147483647;
    
    for(int i=min; i<=max; i++){
      for(int j=0; j<n; j++){
        total += (x[j]-i) * (x[j]-i);
        if(j == n-1 && st > total){
          st = total;
          total = 0;
        }else if(j == n-1){
          total = 0;
        }else{
        
        }
      }
    }
    
    System.out.println(st);
  }
}


Measured memory_bytes: 21844
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		int m = 100000;
		for (int i = 0; i < n; i++) {
			m = Double.valueOf(Math.ceil((double) m * 1.05)).intValue();
			int r = m % 1000;
			if (r > 0) {
				m += 1000 - r;
			}
		}
		System.out.println(m);
	}
}

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

#define int long long
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
template<class T,class U>inline void chmin(T &t,U f){if(t>f)t=f;}
template<class T,class U>inline void chmax(T &t,U f){if(t<f)t=f;}

int N,M;
int T[50010];
int S[50010];
int lis[]={9,11,0,2,4,5,7};

char t[3];
inline int in(){
    scanf("%s",t);
    return lis[t[0]-'A']+(t[1]=='#');
}

void solve(){
    scanf("%lld%lld",&N,&M);
    rep(i,N)T[i]=in();
    rep(i,M)S[i]=in();
    for(int fin=N-2;fin<N;fin++){
        bool ok=true;
        int pos=fin;
        for(int i=M-1;i>=0;i--){
            if(pos<0||pos==N){
                ok=false;
                break;
            }
            if(T[pos]==S[i])pos--;
            else if((T[pos]+1)%12==S[i])pos-=2;
            else if((T[pos]+11)%12==S[i])pos++;
            else{
                ok=false;
                break;
            }
        }
        if(ok&&pos==-1){
            puts("Yes");
            return;
        }
    }
    puts("No");
}

signed main(){
    int T;scanf("%lld",&T);
    while(T--)solve();
    return 0;
}

Measured memory_bytes: 3880
use std::io;
use std::str::FromStr;

type Range = (u64, u64);

fn input<T:FromStr>() -> T {
    let mut line: String = String::new();
    io::stdin().read_line(&mut line);
    line.trim().parse().ok().unwrap()
}

fn main() {
    let x: u64 = input();
    if x < 100 {
        println!("0");
        return;
    }
    if x >= 2000 {
        println!("1");
        return;
    }
    let mut ranges: Vec<Range> = Vec::new();

    for i in 0..20 {
        ranges.push((i * 100, i * 105));
    }
    for range in ranges {
        // println!("{:?}", range);
        if range.0 <= x && x <= range.1 {
            println!("1");
            return;
        }
    }
    println!("0");
}


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

// region template
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

ll gcd(ll a, ll b) { return __gcd(a, b); }

ll euclid(ll a, ll b, ll &x, ll &y) {
    if (b) { ll d = euclid(b, a % b, y, x);
        return y -= a/b * x, d; }
    return x = 1, y = 0, a;
}

typedef unsigned long long ull;
typedef long double ld;
ull mod_mul(ull a, ull b, ull M) {
    ll ret = a * b - M * ull(ld(a) * ld(b) / ld(M));
    return ret + M * (ret < 0) - M * (ret >= (ll)M);
}
ull mod_pow(ull b, ull e, ull mod) {
    ull ans = 1;
    for (; e; b = mod_mul(b, b, mod), e /= 2)
        if (e & 1) ans = mod_mul(ans, b, mod);
    return ans;
}

const ll mod = 1000000007;
struct Mod {
    ll x;
    Mod(ll xx) : x(xx) {}
    Mod operator+(Mod b) { return Mod((x + b.x) % mod); }
    Mod operator-(Mod b) { return Mod((x - b.x) % mod); }
    Mod operator*(Mod b) { return Mod((x * b.x) % mod); }
    Mod operator/(Mod b) { return *this * invert(b); }
    Mod invert(Mod a) {
        ll x, y, g = euclid(a.x, mod, x, y);
        assert(g == 1); return Mod((x + mod) % mod);
    }
    Mod operator^(ll e) {
        if (!e) return Mod(1);
        Mod r = *this ^ (e / 2); r = r * r;
        return e & 1 ? *this * r : r;
    }
};
// endregion

int main() {
    cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit);

    int n; cin >> n;
    vector<pair<string, int>> v(n);
    for (int i = 0; i < n; i++) cin >> v[i].first >> v[i].second;
    string x;
    cin >> x;
    int ans = 0;
    for (auto p : v) {
        if (x.empty()) ans += p.second;
        if (p.first == x) x = "";
    }
    cout << ans << endl;
}

Measured memory_bytes: 892
#define taskname "test"

#include <bits/stdc++.h>

using namespace std;

#define sz(x) (int)x.size()
#define fi first
#define se second

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

string s, t;

void read_input()
{
    cin >> s >> t;
}

void solve()
{
    for(int i = 0; i < sz(s); ++i)
        if(s[i] != t[i])
        {
            cout << "No\n";
            return;
        }
    cout << "Yes\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    read_input();
    solve();
}



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

using namespace std;

int main()
{
    long long N, M, K; cin >> N >> M >> K;

    vector<long long> A(N), AS(N+1, 0), B(M), BS(M+1, 0);

    for(int i=0; i<N; ++i)
    {
        cin >> A[i];
        AS[i + 1] = AS[i] + A[i];
    }
    for(int i=0; i<M; ++i)
    {
        cin >> B[i];
        BS[i + 1] = BS[i] + B[i];
    }

    int an = 0, bn = 0, ans = 0;
    for(an = N; an >= 0; --an)
    {
        while(bn + 1 <= M && BS[bn + 1] <= K - AS[an]) ++bn;
        if(K >= AS[an] + BS[bn])
            ans = max(an + bn, ans);
    }

    cout << ans << endl;
}

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

typedef long long ll;
#define max 1000002
int freq[max]={};

int main(){

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

    ll n, q;
    cin >> n;
    ll arr[n];
    ll sum = 0;

    for(ll i=0; i<n; i++){
        cin >> arr[i];
    }

    for(ll i=0; i<n; i++){
        sum+=arr[i];
        freq[arr[i]]++;
    }

    cin >> q;
    ll ans[q];
    ll x, y;
    for(ll i=0; i<q; i++){
        cin >> x >> y;
        if(freq[x]>0){
            sum = sum - freq[x]*x + freq[x]*y;
            freq[y]+=freq[x];
            freq[x]=0;
        }
        ans[i]=sum;
    }

    for(ll i=0; i<q; i++) cout<<ans[i]<<'\n';

    return 0;
}

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

int main(){
    long long R,C,K;
    cin >> R >> C >> K;
    long long r,c,v;
    long long i,j,k,l;
    vector<pair<pair<long long, long long>, long long> > vec;
    for(i=1; i<=K; i++){
        cin >> r >> c >> v;
        vec.push_back(make_pair(make_pair(r,c),v));
    }
    sort(vec.begin(),vec.end());
    long long dp[2][C+1][4];
    for(i=0; i<=1; i++){
        for(j=0; j<=C; j++){
            for(k=0; k<=3; k++){
                dp[i][j][k]=0;
            }
        }
    }
    long long za;
    for(i=1; i<=R; i++){
        for(j=1; j<=C; j++){
            za = lower_bound(vec.begin(),vec.end(),make_pair(make_pair(i,j),(long long)0)) - vec.begin();
            if(za>=vec.size()){
                for(k=0; k<=3; k++){
                    dp[i%2][j][k]=dp[i%2][j-1][k];
                    for(l=0; l<=3; l++){
                        dp[i%2][j][k]=max(dp[i%2][j][k],dp[!(i%2)][j][l]);
                    }
                }
            }else{
                r=vec[za].first.first;
                c=vec[za].first.second;
                v=vec[za].second;
                if(r==i&&c==j){
                    dp[i%2][j][0]=dp[i%2][j-1][0];
                    for(l=0; l<=3; l++){
                        dp[i%2][j][0]=max(dp[i%2][j][0],dp[!(i%2)][j][l]);
                    }
                    for(k=1; k<=3; k++){
                        dp[i%2][j][k]=max(dp[i%2][j][k],dp[i%2][j-1][k]);
                        dp[i%2][j][k]=max(dp[i%2][j][k],dp[i%2][j-1][k-1]+v);
                        for(l=0; l<=3; l++){
                            dp[i%2][j][k]=max(dp[i%2][j][k],dp[!(i%2)][j][l]+v);
                        }
                    }
                }else{
                    for(k=0; k<=3; k++){
                        dp[i%2][j][k]=dp[i%2][j-1][k];
                        for(l=0; l<=3; l++){
                            dp[i%2][j][k]=max(dp[i%2][j][k],dp[!(i%2)][j][l]);
                        }
                    }
                }
            }
        }
    }
    long long ans=0;
    for(k=0; k<=3; k++){
        ans=max(ans,dp[R%2][C][k]);
    }
    cout << ans << endl;
    system("pause");
    return 0;
}

Measured memory_bytes: 9468
/**
 *	Author: houtaru
 *	Create: 03.02.2018 19:07:00
 */

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

string s;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);
    cin >> s;
    int res = (int) s.size();
    for (int i = 0; i < (int) s.size() - 1; ++i) if (s[i] != s[i + 1])
        res = min(res, max(i + 1, (int) s.size() - i - 1));
    cout << res << "\n";
    return 0;
}

Measured memory_bytes: 512
number=list(map(int,input().split()))
x,n=number[0],number[1]
tmp=[]
try:
    tmp=list(map(int,input().split()))
except:
    pass
answer=x
key_m=x
key_p=x
far_p,far_m=0,0
i,j=0,0
far_answer=0
while answer==x:
    key_p=x+i
    while key_p in tmp:
        i+=1
        key_p=x+i
    key_m=x-j
    while key_m in tmp:
        j+=1
        key_m=x-j
    far_m=int(x-key_m)
    far_p=int(key_p-x)
    if far_m==far_p:
        far_answer=far_m
    else:
        far_answer=min(far_p,far_m)

    if far_answer==far_m:
        answer=key_m
        break
    else:
        answer=key_p
print(answer)

Measured memory_bytes: 61900
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
 
int main() {
  int n,cnt,pos;
  cin>>n;
  vector<int> a(n);
  vector<int> j(n);
  rep(i,n) cin>>a.at(i);
  pos=1;
  while(1){
  if (j.at(pos-1)==1){
  cout<<-1<<endl;
  break;
  }
    
  j.at(pos-1)++;
  pos=a.at(pos-1);
  cnt++;
  if (pos==2){
  cout<<cnt<<endl;
  break;
  }
    
  }
}

Measured memory_bytes: 1024
#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <cmath>
#include <math.h>
#include <sstream>
#include <iomanip>
#include <time.h>
#include <fstream>
#include <map>

using namespace std;

int main(){
	string a,b;
	cin>>a>>b;
	// b_ = b;
	// while(b_ != 0){
	// 	b_ /= 10;
	// 	a *= 10;
	// }
	int y = stoi(a+b);
	int x =0;
	while (x*x <= y){
		if (x*x == y){
			cout<<"Yes"<<endl;
			return 0;
		}
		x++;
	}
	cout<<"No"<<endl;
	return 0;
}


Measured memory_bytes: 256
var GET=(function(){function f(s){return new g(s);}function g(s){this._s=s.trim().split("\n");this._y=0;}g.prototype.a=function(f){var s=this._s, y=this._y, r;if(typeof s[y]==="string")s[y]=s[y].split(" ").reverse();r=s[y].pop();if(!s[y].length)this._y++;return f?r:+r;};g.prototype.l=function(f){var s=this._s[this._y++].split(" ");return f?s:s.map(a=>+a);};g.prototype.m=function(n,f){var r=this._s.slice(this._y,this._y+=n).map(a=>a.split(" "));return f?r:r.map(a=>a.map(a=>+a));};g.prototype.r=function(n,f){var r=this._s.slice(this._y,this._y+=n);return f?r:r.map(a=>+a);};return f;})();
var o=GET(require("fs").readFileSync("/dev/stdin","utf8"));
function xArray(v){var a=arguments,l=a.length,r="Array(a["+--l+"]).fill(0).map(x=>{return "+v+";})";while(--l)r="Array(a["+l+"]).fill(0).map(x=>"+r+")";return eval(r);}

console.log(main());
function main(){
  var n = o.a();
  var a = o.l();
  var b = o.l();
  var ans = 0;
  for(var i = 0; i < n; i++){
    var mn = Math.min(a[i],b[i]);
    ans += mn;
    var y = b[i] - mn;
    if(y){
      mn = Math.min(a[i+1],y);
      ans += mn;
      a[i+1] -= mn;
    }
  }
  return ans;
}

Measured memory_bytes: 31008
# 2019/10/17

R,G,B,n=map(int,input().split())
cnt=0
for i in range(n//R+1):
    r=i*R
    rem=n-r
    for j in range((n-r)//G+1):
        g=j*G
        b=n-(r+g)
        if b%B:continue
        cnt+=1
print(cnt)

Measured memory_bytes: 3060
#include <stdio.h>

int main(void)
{
	int mHeight[10];
	int temp;
	int i, j;
	
	for(i = 0; i < 10; i++){
		scanf("%d", &mHeight[i]);
	}

	for(i = 0; i < 10 - 1; i++){
		for(j = 0; j < 10 - 1; j++){
			if(mHeight[j] < mHeight[j+1]){
				temp = mHeight[j];
				mHeight[j] = mHeight[j+1];
				mHeight[j+1] = temp;
			}
		}
	}

	for(i = 0; i < 3; i++){
		printf("%d\n", mHeight[i]);
	}

	return 0;
}

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

#define TYPE(c)   remove_reference_t<decltype(c)>
#define REP(i, n) for(TYPE(n) i = 0; i < n; i++)
#define FOR(v, c) for(TYPE(c.begin()) v = c.begin(); v != c.end(); v++)
#define ALL(c)    c.begin(), c.end()
#define SORT(c)   sort(ALL(c))
#define RSORT(c)  sort(ALL(c), greater<TYPE(c)::value_type>())
#define UNIQUE(c) c.erase(unique(ALL(c)), c.end())

using namespace std;
using ll = long long;

constexpr int MOD  = (int)1e9 + 7;
constexpr int INF  = (int)1e9 + 1;
constexpr ll  LINF = (ll)1e18 + 1;
template<typename T> constexpr bool chmax(T& a, const T& b)
{ if(a < b) { a = b; return true; } else { return false; } }
template<typename T> constexpr bool chmin(T& a, const T& b)
{ if(b < a) { a = b; return true; } else { return false; } }
template<typename S, typename T> ostream& operator<<(ostream &os, const pair<S, T>& p)
{ return os << "(" << p.first << "," << p.second << ")"; }
ostream& operator<<(ostream &os, const string& s)
{ FOR(v, s) os << *v; return os; }
template<typename T> auto operator<<(ostream& os, const T& t)
    -> typename conditional<true, ostream&, decltype(*t.begin())>::type
{ os << "["; FOR(v, t) { if(v != t.begin()) os << ","; os << *v; } os << "]"; return os; }

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

    int n;
    std::cin >> n;
    std::vector<int> A(n);
    REP(i, n) {
        std::cin >> A[i];
    }

    SORT(A);

    auto v   = INF;
    auto v_i = 0;
    REP(i, n) {
        if(chmin(v, (int)abs(A[i] - ceil(A.back() / 2.0)))) v_i = i;
    }
    std::cout << A.back() << " " << A[v_i] << std::endl;

    return 0;
}


Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
int const nax = 3e5 + 10;
using ll = long long ;
ll rec(ll x){
    if(x == 1) return 1;
    return 2 * rec(x / 2) + 1;
}

int main(){
    ll h;
    scanf("%lld", &h);
    printf("%lld\n", rec(h));
}
/*
    Good Luck
        -Lucina
*/


Measured memory_bytes: 256
n = int(input())
a = [int(x) for x in input().split()]
while True:
    a.sort()
    b = [i % a[0] for i in a[1:]]
    b.insert(0, a[0])
    b = [i for i in b if i != 0]
    if len(b) == 1: break
    else: a = b
print(b[0])

Measured memory_bytes: 14252
#include <iostream>
#include <cstring>
#include <map>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <set>
#include <vector>
#include <queue>
#include <list>
#include <numeric>
#include <stdio.h>
#include <string>
#include <cstdlib>
#include <math.h>
#include <stack>
#include <climits>
#include <bitset>

using namespace std;
typedef long long ll;

ll gcd(ll x, ll y) {
  ll tmp = 0;
  if (x < y){
    tmp=x;
    x=y;
    y=tmp;
  }
  while (y > 0) {
    ll r = x % y;
    x = y;
    y = r;
  }
  return x;
}

//for(int i = ; i < ; i++){}

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




Measured memory_bytes: 256
//  Created by conan1024hao in 2019.
//  Copyright © 2019 conan1024hao. All rights reserved.
//  専用ライブラリです、自由にコピーして構いません。
//  感谢看我的代码！Wechat:conan1024hao 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 <set>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
#define INF 10e16
#define MOD 1000000007
#define rep(i, a, n) for (ll i = a; i < (ll)(n); i++)
#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;
typedef long long int ll;
typedef pair<ll,ll>P;
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[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
ll cmp(pair<ll,string>a,pair<ll,string> b){
        if(a.fi!=b.fi)
        return a.fi<b.fi;
        else
        return a.se<b.se;
}

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

//---------------------------------------------------------------------------
int main(){//問題をちゃんと見ろ!!!!!!!!!!!!!!!!!      llか??????????
    cin.tie(0);
    ios::sync_with_stdio(false);
    //-------------------------------
    int n;cin>>n;int ans=0;
    string s;cin>>s;
    set<int>pin;
    for(int t1=0;t1<10;t1++){
        bool a1=false;bool a2=false;bool a3=false;
        char ct='0'+t1;
        int i,j,l;
        for(i=0;i<n;i++){
            if(s[i]==ct){
                a1=true;
                break;
            }
        }
        for(int t2=0;t2<10;t2++){
                ct='0'+t2;
            for(j=i+1;j<n;j++){
                if(s[j]==ct){
                    a2=true;
                    break;
                }
            }
            for(int t3=0;t3<10;t3++){
                ct='0'+t3;
                for(l=j+1;l<n;l++){
                    if(s[l]==ct){
                        a3=true;
                        break;
                    }
                }
                if(a1&&a2&&a3)
                    ans++;
                a3=false;
            }
            a2=false;
        }
        
    }
    cout<<ans<<endl;
    
    //-------------------------------
    return 0;
}
//---------------------------------------------------------------------------


Measured memory_bytes: 384
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <functional>
#include <tuple>
#include <climits>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <numeric>
#include <map>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <array>

using namespace std;

int main()
{
    int A, B, C;
    int five = 0, seven = 0;
    cin >> A >> B >> C;
    if(A == 5) five++;
    else if(A == 7) seven++;
    if(B == 5) five++;
    else if(B == 7) seven++;
    if(C == 5) five++;
    else if(C == 7) seven++;

    cout << ((seven==1 && five==2)?"YES":"NO") << endl;
}


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

int a[200010];
int b[200010];

int main() {
	long long n,q;
	long long ans;
	int x,y,a_min,b_min;

	cin >> n >> q ;
	a_min=n-2;
	b_min=n-2;
	ans=(n-2)*(n-2);
	rep(i,n) a[i]=n-2;
	rep(i,n) b[i]=n-2;
	if (q==0){
		cout << ans << endl;
		return 0;
	}
	rep(i, q){
		cin >> x >> y ;
		if(x==1){
			if(y<a_min+2){
				rep2(j,y,a_min+2) b[j]=b_min;
				a_min=y-2;
			}
			ans-=b[y];
		}else{
			if(y<b_min+2){
				rep2(j,y,b_min+2) a[j]=a_min;
				b_min=y-2;
			}
			ans-=a[y];
		}
	}

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

Measured memory_bytes: 5100
# #!/usr/bin/env python3
# # -*- coding: utf-8 -*-


def main():
    N = int(input())
    B = list(map(int, input().split()))

    result = B[0] + B[-1]

    if len(B) == 1:
        pass
    else:
        for i in range(N - 2):
            result += min(B[i], B[i + 1])

    print(result)


if __name__ == "__main__":
    main()


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

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

    vector<int> dp(N, 1000000000);
    dp[0] = 0;
    for (int i = 1; i < N; i++) {
        for (int j = 1; j <= i && j <= K; j++) {
            dp[i] = min(dp[i], dp[i - j] + abs(H[i] - H[i - j]));
        }
    }

    cout << dp[N - 1] << endl;
    return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> P(N, 0);
  for(auto& p : P){
    cin >> p;
  }
  vector<int> Q(N, 0);
  for(auto& q : Q){
    cin >> q;
  }

  vector<int> R(N, 0);
  for(int ii=0; ii<N; ii++){
    R[ii] = ii+1;
  }

  int pos_P = -1;
  int pos_Q = -1;
  int pos = 0;
  do{
    if(P==R) pos_P = pos;
    if(Q==R) pos_Q = pos;
    pos++;
  }while(next_permutation(R.begin(), R.end()));

  cout << abs(pos_Q - pos_P) << endl;

  return 0;
}

Measured memory_bytes: 256
#![allow(non_snake_case)]
#![allow(unused_imports)]
 
use proconio::{input, fastout};
use proconio::marker::*;
use whiteread::parse_line;
use std::collections::*;

#[fastout]
fn main() {
    input!{
       T: Chars, 
    }

    let mut ans: String = "".to_string();

    for c in T.iter() {
        if c == &'?' {
            ans.push('D')
        } else {
            ans.push(*c)
        }
    }

    println!("{}", ans);
}


Measured memory_bytes: 3280
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>; 
using pll = pair<long long, long long>;
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007LL;
constexpr long long INF = 1001001001LL;
constexpr long long LINF = 1001001001001001001;
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define rept(i, j, n) for(int i=(j); i<(n); i++)
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(){
    int n, m; cin >> n >> m;
    vector<ll> H(n); rep(i, n)cin >> H[i];
    vector<vector<ll>> G(n);
    rep(i, m){
        ll a, b; cin >> a >> b; a--, b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    int res = 0;
    rep(i, n){
        bool ok = true;
        for(auto e: G[i]){
            if(H[i] <= H[e])ok = false;
        }
        if(ok)res++;
    }
    cout << res << ln;
}


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

void main_()
{
    string S;
    cin >> S;

    int c0 = count(begin(S), end(S), '0');
    int c1 = count(begin(S), end(S), '1');
    cout << S.size() - abs(c1 - c0) << endl;
}
int main()
{
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios_base::sync_with_stdio(false);
    main_();
    return 0;
}


Measured memory_bytes: 512
//https://blog.csdn.net/hesorchen
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
#include <list>
#include <set>
#include <queue>
#include <string>
#include <cstring>
using namespace std;
#define ll long long
#define lowbit(a) (a & (-a))
#define endl "\n"
#define PI acos(-1.0)
#define mod 1000000009
#define INF 0x3f3f3f3f
#define MAX 200010

double p[105];
int main()
{
    int n, m;
    cin >> n >> m;
    double sum = 0;
    for (int i = 1; i <= n; i++)
    {
        cin >> p[i];
        sum += p[i];
    }
    sort(p + 1, p + 1 + n, greater<int>());

    double temp = sum * (1.0 / (4 * m));
    // cout << temp << " " << p[m] << endl;
    if (p[m] >= temp)
        cout << "Yes\n";
    else
        cout << "No\n";

    return 0;
}

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

using namespace std;

namespace {

	int cnt;
	void InsertionSort(vector<int>& A, int g)
	{
		for (int i = g; i < A.size(); ++i)
		{
			int v = A[i];
			int j = i - g;

			while (j >= 0 && A[j] > v) {
				A[j + g] = A[j];
				j -= g;
				cnt++;
			}

			A[j + g] = v;
		}
	}

	void ShellSort(vector<int>& A)
	{
		cnt = 0;
		vector<int> G;
		G.push_back(1);
		for (int i = 1; i <= A.size(); ++i)
		{
			int g = pow(4, i) + 3 * pow(2, i - 1) + 1;
			if (g > A.size()) break;
			else G.push_back(g);
		}
		reverse(G.begin(), G.end());

		for (auto i : G)
			InsertionSort(A, i);

		cout << G.size() << endl;
		cout << G[0];
		for (auto it = G.begin() + 1; it != G.end(); ++it)
			cout << ' ' << *it;
		cout << endl;
		cout << cnt << endl;
		for (auto i : A)
			cout << i << endl;
	}
}

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

	int n;
	cin >> n;

	vector<int> A(n);
	for (auto& i : A) cin >> i;

	ShellSort(A);

	return 0;
}

Measured memory_bytes: 6784
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

A, B, C, K = map(int, input().split())

if K % 2 == 0:
    ans = A - B
else:
    ans = B - A

print(ans)

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

ll gcd(ll a, ll b){
    if(a%b==0) return b;
    return gcd(b, a%b);
}
ll soinsu(ll a){
    ll b = 2;
    ll memo = a;
    ll x = 1;
    while(a != 1){
        if(a % b == 0){
            while(a%b == 0){
                a /= b;
            }
            x++;
        }
        if(memo < b * b) break;
        else b++;
    }
    if(a != 1) x++;
    return x;
}

int main(){
    ll a, b;
    cin >> a >> b;
    ll x = gcd(a, b);
    cout << soinsu(x) << endl;
    return 0;
}

Measured memory_bytes: 256
/*
ID: rajiv
TASK: test
LANG: C++14
*/
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<string>
#include<queue>
#include<stack>
#include<iomanip>
#include<sstream>
#include<iterator>
#include<random>
#include<chrono>

#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ONLINE_JUDGE

using namespace std;

#define deb(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) { cout << '\n';}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << '[' << *it << " = " << a << "]  ";
    err(++it, args...);
}
mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count());
int rnd(int a, int b){ // including a and b
	return a+(rng()%(b-a+1));
}
#define fcout cout<<fixed<<setprecision(15)
#define repp(i,j,k) for(int i=j; i<k; ++i)
#define rep(i,j) repp(i,0,j)
#define rrep(i,j,k) for(int i=j; i>=k; --i)
#define all(x) (x).begin(), (x).end()
#define sortr(x) sort(x.begin(), x.end())
#define lb(x,val) lower_bound(x.begin(), x.end(), val)
#define ub(x,val) upper_bound(x.begin(), x.end(), val)
#define pb push_back
#define endl '\n'

typedef long long int lli;
typedef pair<int,int> pii;
typedef pair<lli,lli> pll;
typedef vector<int> vi;
typedef vector<lli> vl;
typedef tuple<lli, lli> tii;
typedef tuple<lli, lli, lli> tiii;

//const lli M=998244353ll;
const lli M=1000000007ll;
const double D=1.0;

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

void solve(){
	int a; cin>>a;
	cout<<(a+a*a+a*a*a)<<endl;
	
}

int main(){
	fast_io
	#ifndef ONLINE_JUDGE
	freopen("test.in", "r", stdin);
	freopen("test.out", "w", stdout);
	#endif 
	int tc=1;
	//cin>>tc;
	while(tc--){solve();}
	return 0;
}

Measured memory_bytes: 3164