akhauriyash/Code-Regression huggingface dataset:
#pragma GCC optimize ("-O3")
#include <bits/stdc++.h>
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> p32;
typedef pair<ll,ll> p64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<ll> v32;
typedef vector<vector<ll> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
ll MOD = 1e9+7;;
#define forn(i,e) for(ll i = 0; i < e; i++)
#define forsn(i,s,e) for(ll i = s; i < e; i++)
#define rforn(i,s) for(ll i = s; i >= 0; i--)
#define rforsn(i,s,e) for(ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 1e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define vvv64 vector<vv32>
#define vvvv64 vector<vvv32>
 
ll exp(ll n, ll e)
{
    if (e==0)
        return 1;
    ll temp=exp((n*n)%MOD,e/2);
    return e%2==0 ? temp : (temp*n)%MOD;
}

ll inverse(ll n)
{
  return exp(n,MOD-2);
}

int LIM=2e5+100;
vv64 adj(LIM);
v64 down(LIM);
// v64 up(n+1);
v64 S(LIM);
v64 ans(LIM);
v64 fact(LIM,1);
void dfs1(int n,int par)
{
  ll ans=1;
  S[n]=1;
  for (int i=0; i<sz(adj[n]); i++)
  {
    if  (adj[n][i]==par)
      continue;
    dfs1(adj[n][i],n);
    ans=(ans*down[adj[n][i]])%MOD;
    S[n]+=S[adj[n][i]];
    ans=ans*inverse(fact[S[adj[n][i]]]);
    ans%=MOD;
  }
  ans=(ans*fact[S[n]-1])%MOD;
  down[n]=ans;
}


int N;
void dfs2(int n,int par,ll val)
{
  ll total=down[n];
  total=(total*inverse(fact[S[n]-1]))%MOD;
  total=(total*fact[N-1])%MOD;
  total=(total*val)%MOD;
  total=(total*inverse(fact[N-S[n]]))%MOD;
  if (n==1)
    total=down[n];
  ans[n]=total;
  for (int i=0; i<sz(adj[n]); i++)
  {
    if (adj[n][i]==par)
      continue;
    int c=adj[n][i];
    ll temp=total;

    // if (n==1)
    //   cout<<temp<<"a"<<" "<<N-1-S[c]<<" "<<fact[N-1]<<endl;
    temp=(temp*inverse(fact[N-1]))%MOD;
    // if (n==1)
    //   cout<<temp<<"a"<<" "<<N-1-S[c]<<" "<<inverse(fact[N-1])<<endl;
    temp=(temp*fact[N-1-S[c]])%MOD;


    temp=(temp*fact[S[c]])%MOD;
    temp=(temp*inverse(down[c]))%MOD;
    // if (n==1)
    //   cout<<temp<<endl;;
    dfs2(c,n,temp);
  }
}
void solve()
{
  // cout<<inverse(2)<<"c"<<endl;
  int n;
  cin>>n;
  N=n;
  for (int i=1; i<n; i++)
  {
    int a,b;
    cin>>a>>b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs1(1,1);
  // cout<<down[1]<<endl;  
  dfs2(1,1,1);
  for (int i=1; i<=n; i++)
  {
    cout<<ans[i]<<endl;;
  }
}
 
int main() {
    fast_cin();
    // /Sieve();
    ll t=1;
    for (int i=1; i<LIM; i++)
    {
      fact[i]=(fact[i-1]*i)%MOD;
    }
    // cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}


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

using namespace std;
typedef long long ll;

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

  vector<ll> w(n), v(n);
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }

  vector<ll> cum[4];
  for (int i = 0; i < n; i++) {
    cum[w[i] - w[0]].push_back(v[i]);
  }
  for (int i = 0; i < 4; i++) {
    sort(cum[i].begin(), cum[i].end());
    cum[i].push_back(0);
    reverse(cum[i].begin(), cum[i].end());
    for (int j = 1; j < cum[i].size(); j++) {
      cum[i][j] += cum[i][j - 1];
    }
  }

  ll ans = 0;
  for (int i = 0; i < cum[0].size(); i++) {
    for (int j = 0; j < cum[1].size(); j++) {
      for (int k = 0; k < cum[2].size(); k++) {
        for (int l = 0; l < cum[3].size(); l++) {
          ll sumw = i * w[0] + j * (w[0] + 1) + k * (w[0] + 2) + l * (w[0] + 3);
          if (sumw > W) continue;
          ans = max(ans, cum[0][i] + cum[1][j] + cum[2][k] + cum[3][l]);
        }
      }
    }
  }
  cout << ans << endl;

  return 0;
}

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

using namespace std;

int main() {
    string S;

    cin >> S;

    for (int i = 0; i < S.length(); i++) {
        if(i % 2 == 0) {
            if(S[i] == 'L') {
                cout << "No" << endl;
                return 0;
            }
        } else {
            if(S[i] == 'R') {
                cout << "No" << endl;
                return 0;
            }
        }
    }

    cout << "Yes" << endl;

    return 0;
}

Measured memory_bytes: 256
import java.io.*;

public class Main {

  public static void main(String[] args) throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
    String s = br.readLine();

    br.close();

    StringBuilder t = new StringBuilder(s);

    System.out.println(s.length() == 2 ? s : t.reverse());
  }
}

Measured memory_bytes: 22484
func gcd(_ x: Int, _ y: Int) -> Int {
    if y == 0 { return x }
    return gcd(y, x % y)
}

let K = Int(readLine()!)!
var res: Int = 0

for a in 1...K {
    for b in a...K {
        for c in b...K {
            if Set([a, b, c]).count == 3 {
                res += gcd(gcd(a, b), c) * 6
            } else if Set([a, b, c]).count == 2 {
                res += gcd(gcd(a, b), c) * 3
            } else {
                res += gcd(gcd(a, b), c)
            }
        }
    }
}
print(res)  // (๑•ૅㅁ•๑)

Measured memory_bytes: 8060
S = list(input())

pre_list = []
tar_list = [S[0]]
count = 1
i = 1
try:
	while i < len(S):
		pre_list = tar_list #一つ前のリストを引き継ぐ
		tar_list = []
		tar_list.append(S[i])
		i += 1
		#一緒だったら続ける
		while tar_list == pre_list:
			tar_list.append(S[i])
			i += 1
		#異なった時に止めてカウントをアップ
		# print(pre_list,':',tar_list)
		count += 1

except IndexError:
	pass
print(count)

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

const int MAX_V=1e5+1;

vector<int> edge[MAX_V];
stack<int> ans;

bool used[MAX_V];

void visit(int now){
	if(used[now])return;
	used[now]=true;
	for(int i=0;i<edge[now].size();i++){
		visit(edge[now][i]);
	}
	ans.push(now);
}

int main(){
	fill(used,used+MAX_V,false);
	int V,E;
	cin>>V>>E;
	for(int i=0;i<E;i++){
		int a,b;
		cin>>a>>b;
		edge[a].push_back(b);
	}
	for(int i=0;i<V;i++)if(!used[i])visit(i);
	while(!ans.empty()){
		cout<<ans.top()<<endl;
		ans.pop();
	}
}

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

int main(){
    int N;
    cin>>N;
    vector<int> C(N-1),S(N-1),F(N-1);
    for(int i=0;i<N-1;i++) cin>>C[i]>>S[i]>>F[i];

int t=0;
for(int i=0;i<N-1;i++){
    t=0;
    for(int j=i;j<N-1;j++){
    if(t<S[j]) t = S[j];
    else if(t%F[j]==0);
    else t = t + F[j] - t%F[j];
    t+=C[j];
    }
    cout<<t<<endl;
}
cout<<"0"<<endl;
return 0;
}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main()
{
	long long int n,q,h,s,d;
	cin>>q>>h>>s>>d>>n;
	long long int cost_1=4*q,cost_2=0,ans=0;
	if(cost_1>2*h)
	{
		cost_1=2*h;
	}
	if(cost_1>s)
	{
		cost_1=s;
	}
	cost_2 = 2*cost_1;
	if(cost_2>d)
	{
		cost_2=d;
	}
	if((n%2)==0)
	{
		ans = (cost_2)*(n/2);
	}
	else
	{
		ans = (cost_2)*(n/2);
		ans+=cost_1;
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstring>
#include<vector>
#include <algorithm>
#include<cstdlib>
#include<set>
#include<math.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i,n) for(ll i=0;i<n;i++)


int main()
{
	int n, m;
	cin >> n >> m;
	vector<int> s(m);
	vector<int> c(m);
	vector<int> ans(n,0);
  ans[0]=1;
	rep(i, m) cin >> s[i] >> c[i];
	bool tf = true;
	if(m==0)
    {
      if(n==1) cout<<0;
      else if(n==2) cout<<10;
      else if(n==3) cout<<100;
    }
  else
  {
        for (int i = 0; i < m; i++)
	{
		for (int t = i + 1; t < m; t++)
		{
			if (s[i] == s[t] && c[i] != c[t]) tf = false;
          if(s[i]==1&&c[i]==0) tf=false;
		}
	}
    
	if (tf == false)
	{
		cout << -1;
	}
	else
	{
		if (n == 1)
		{
			if (m == 0)
			{
				cout << 0;
			}
			else
			{
				cout << c[0];
			}
		}

		else
		{
			rep(i, m)
			{
				ans[s[i] - 1] = c[i];
			}
			if (ans[0] == 0)
			{
				cout << -1;
			}
			else
			{
				rep(i, n)
				{
					cout << ans[i];
				}
			}

		}
	}
  }
	cout << "\n";
  
	return 0;
}

Measured memory_bytes: 3604
#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
int par[200010];
int r[200010];

void init(int n){
  for(int i=0;i<n;i++){
    par[i]=i;
    r[i]=0;
  }
  return;
}

int root(int n){
  if(par[n]==n){
    return n;
  }
  else{
    return par[n]=root(par[n]);
  }
}

void unite(int x, int y){
  x=root(x);
  y=root(y);
  if(x==y) return;
  if(r[x]<r[y]){
    par[x]=y;
  }
  else {
    par[y]=x;
    if(r[x]==r[y]) r[x]++;
  }
  return;
}

bool same(int x,int y){
  return root(x)==root(y);
}

int main() {
  int n,m;
  cin>>n>>m;
  init(n);
  int p[n];
  int a[n];
  for(int i=0;i<n;i++){
    cin>>p[i];
    a[p[i]-1]=i;
  }
  for(int i=0;i<m;i++){
    int a,b;
    cin>>a>>b;
    unite(a-1,b-1);
  }
  int ans=0;
  for(int i=0;i<n;i++){
    if(same(a[i],i)) ans++;
  }
  cout<<ans<<endl;
  return 0;
}

Measured memory_bytes: 1792
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * Built using CHelper plug-in Actual solution is at the top
 */

public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		Task solver = new Task();
		solver.solve(1, in, out);
		out.close();
	}

	static class Task {
		public void solve(int testNumber, InputReader in, PrintWriter out) {
			int N = in.nextInt();
			int[] A = new int[N];
			int[] B = new int[N];
			int[] C = new int[N - 1];
			for(int i = 0; i < N; i++) {
				A[i] = in.nextInt();
			}
			for(int i = 0; i < N; i++) {
				B[i] = in.nextInt();
			}
			for(int i = 0; i < N - 1; i++) {
				C[i] = in.nextInt();
			}
			int total = 0;
			int tmp = 0;
			for(int i = 0; i < N; i++) {
				total += B[A[i] - 1];
				if(i != 0) {
					if(A[i] - 1 == A[i - 1]) {
						total += C[A[i] - 2];
					}	
				}
			}
			System.out.println(total);
			
		}
	}
	
	static boolean permutation_2(String s, String t){
        if(s.length() != t.length()){
            return false;
        }
        
        int[] letters = new int[256]; //文字コードの仮定;
        
        char[] s_array = s.toCharArray();  
        for(char c:s_array){
            letters[c]++;
        }
        
        for(int i=0;i<t.length();i++){
            int c = (int)t.charAt(i);
            if(--letters[c] < 0){
                return false;
            }
        }
        return true;
    }

	static class InputReader {
		public BufferedReader reader;
		public StringTokenizer tokenizer;

		public InputReader(InputStream stream) {
			reader = new BufferedReader(new InputStreamReader(stream), 32768);
			tokenizer = null;
		}

		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}

		public char nextChar() {
			return next().charAt(0);
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

	}
}


Measured memory_bytes: 21332
H, W = map(int, input().split())
S = [['.'] * (W+2)]
S += [['.'] + list(input()) + ['.'] for _ in range(H)]
S += [['.'] * (W+2)]

ans = []
for i in range(1, H + 1):
  s = str()
  for j in range(1, W + 1):
    tmp = 0
    if S[i][j] == '.':
      for k in range(-1, 2, 1):
        for l in range(-1, 2, 1):
          if S[i+k][j+l] == '#':
            tmp += 1
      s += str(tmp)
    else:
      s += '#'
  ans += [s]
    
for i in ans:
  print (i)

Measured memory_bytes: 3064
import java.math.*;
import java.util.*;

public class Main {
    static int gcd(int a, int b) {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int xi[] = new int[n + 1];
        xi[0] = sc.nextInt();
        for(int i=1;i<=n;i++)
            xi[i] = sc.nextInt();
        Arrays.sort(xi);
        int diff[] = new int[n];
        int tmp = xi[0];
        for (int i = 1; i <= n; i++) {
            diff[i - 1] = xi[i] - tmp;
            tmp = xi[i];
        }
        int ans = diff[0];
        for (int i = 1; i < diff.length; i++) {
            ans = gcd(ans, diff[i]);
        }
        if (diff.length == 1)
            ans = diff[0];
        System.out.println(ans);
    }
}


Measured memory_bytes: 51012
#include <iostream>
#include <string>
#include <vector>

using namespace std;
int main(){
    int n, m;
    cin >> n >> m;
    vector<pair<int, string>> p(m);
    int pi;
    string si;
    for(int i=0; i<m; i++){
        cin >> pi >> si;
        p[i].first = pi-1;
        p[i].second = si;
    }

    vector<bool> ac(n, false);
    int seto = 0;
    vector<int> wa(n, 0);
    int pena = 0;
    for(int i=0; i<m; i++){
        if(ac[p[i].first]==false){
            if(p[i].second == "AC"){
                ac[p[i].first] = true;
                pena += wa[p[i].first];
                seto += 1;
            }else{
                wa[p[i].first] += 1;
            }
        }
    }
    cout << seto <<" "<< pena << endl;

}

Measured memory_bytes: 6912
#include<iostream>
using namespace std;
int main() {
	int n;
	cin>>n;
	if(n>=400&&n<=599) cout<<8<<endl;
	if(n>=600&&n<=799) cout<<7<<endl;
	if(n>=800&&n<=999) cout<<6<<endl;
	if(n>=1000&&n<=1199) cout<<5<<endl;
	if(n>=1200&&n<=1399) cout<<4<<endl;
	if(n>=1400&&n<=1599) cout<<3<<endl;
	if(n>=1600&&n<=1799) cout<<2<<endl;
	if(n>=1800&&n<=1999) cout<<1<<endl;
}

Measured memory_bytes: 3620
dp=zeros(Int,101,8)
p=[1,1,2,3,5,8,13,21]
mod=10^9+7
H,W,K=map(x->parse(Int,x),split(readline()))
dp[1,1]=1
for i=1:H
	for j=1:W
		if j>1
			dp[i+1,j-1]+=dp[i,j]*p[j-1]*p[W-j+1]
			dp[i+1,j-1]%=mod
		end
		dp[i+1,j]+=dp[i,j]*p[j]*p[W-j+1]
		dp[i+1,j]%=mod
		if j<W
			dp[i+1,j+1]+=dp[i,j]*p[j]*p[W-j]
			dp[i+1,j+1]%=mod
		end
	end
end
println(dp[H+1,K])


Measured memory_bytes: 113160
#include <bits/stdc++.h>
#define rep(i,n) for (ll i = 0; i < n; ++i)
#define sz(x) int(x.size())
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++)
using namespace std;

typedef int64_t ll;

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; }
const int INF = 1001001001;
const long long INFL = (1LL<<60);
const double eps = (1e-9);

//組み合わせの数nCrを計算
ll calcNumOfCombination(ll n, ll r){
  ll num = 1;
  for(ll i = 1; i <= r; i++){
    num = num * (n - i + 1) / i;
  }
  return num;
}

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

  ll n, m;
  cin >> n >> m;

  vector<ll> csum(n);
  map<int, int> mp;
  mp[0]++;  // 左端
  ll sum = 0;
  rep(i, n) {
    int tmp;
    cin >> tmp;
    sum += tmp;
    mp[sum % m]++;
  }

  ll ans = 0;
  for (auto& p : mp) {
    if (p.second <= 1) continue;
    ans += calcNumOfCombination(p.second, 2);
  }

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 3968
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[][] c = new int[3][3];
        int[] a = new int[3];
        int[] b = new int[3];
        for(int i=0; i<3; i++) {
            for(int j=0; j<3; j++) {
                c[i][j] = Integer.parseInt(sc.next());
            }
        }
        String ans = "No";
        for(int i=0; i<=100; i++) {
            a[0] = i;
            b[0] = c[0][0] - i;
            b[1] = c[0][1] - i;
            b[2] = c[0][2] - i;
            a[1] = c[1][0] - b[0];
            a[2] = c[2][0] - b[0];
            if(c[1][1] == a[1]+b[1] && c[1][2] == a[1]+b[2] && c[2][1] == a[2]+b[1] && c[2][2] == a[2]+b[2]) {
                ans = "Yes";
                break;
            }
        }
        
        System.out.println(ans);
        
    }
}


Measured memory_bytes: 21844
#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
using namespace std;
typedef long long ll;

int main(){
    string s;
    ll ans = 0;
    cin >> s;
    int l = s.size() - 1;
    for(int mask = 0; mask < (1LL << l); mask++){
        ll sum = 0, num = s[0] - '0';
        for(int i = 0; i < l; i++){
            if(mask & (1LL << i)){
                // + を入れる
                sum += num;
                num = s[i + 1] - '0';
            }else{
                // + を入れない
                num = num * 10 + s[i + 1] - '0';
            }
        }
        sum += num;
        ans += sum;
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
#include<stdio.h>
int main()
{
    int c[3][3];
    int i,j;
    int a[3];
    int b[3];
    int ans,same;
    while(scanf("%d%d%d",&c[0][0],&c[0][1],&c[0][2])!=EOF)
    {
        ans=1;
        scanf("%d%d%d",&c[1][0],&c[1][1],&c[1][2]);
        scanf("%d%d%d",&c[2][0],&c[2][1],&c[2][2]);
        for(i=0; i<2; i++)
        {
            for(j=1;j<=2;j++)
            {
                if(c[i][j-1]-c[i][j]!=c[i+1][j-1]-c[i+1][j])
                    {
                        ans=0;
                        break;
                    }
            }
            if(ans==0)
                break;
        }
        if(ans!=0)
            for(i=0;i<2;i++)
            {
                for(j=1;j<=2;j++)
                {
                    if(c[j-1][i]-c[j][i]!=c[j-1][i+1]-c[j][i+1])
                    {
                        ans=0;
                        break;
                    }
                }
                if(ans==0)
                    break;
            }
        if(ans==0)
            printf("No\n");
        else
            printf("Yes\n");
    }
    return 0;
}


Measured memory_bytes: 128
/*
    Author:zeke
    
    pass System Test!
    GET AC!!
*/
#include <iostream>
#include <queue>
#include <vector>
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include <algorithm>
#include <functional>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <map>
#include <iomanip>
#include <utility>
#include <stack>
using ll = long long;
using ld = long double;
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rep3(var, min, max) for (ll(var) = (min); (var) < (max); ++(var))
#define repi3(var, min, max) for (ll(var) = (max)-1; (var) + 1 > (min); --(var))
#define Mp(a, b) make_pair((a), (b))
#define F first
#define S second
#define Icin(s) \
    ll(s);      \
    cin >> (s);
#define Scin(s) \
    ll(s);      \
    cin >> (s);
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;
}
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef vector<V> VV;
typedef vector<P> VP;
ll MOD = 1e9 + 7;
ll INF = 1e18;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n;
    cin>>n;
    V vec(n);
    rep(i, n) cin >> vec[i];
    ll a=0;
    ll b=0;
    ll c=0;
    ll res = 1;
    rep(i,n){
        ll cnt = 0;
        
        if(vec[i]==a){
            
            cnt++;
        }
        if (vec[i] == b)
        {
            
            cnt++;
        }
        if (vec[i] == c)
        {
            
            cnt++;
        }
        if(cnt==0){
            cout << 0 << endl;
            return 0;
        }
        res *= cnt;
        res %= MOD;
        if (vec[i] == a){
            a++;
            continue;
        }
        if (vec[i] == b)
        {
            b++;
            continue;
        }
        if (vec[i] == c)
        {
            c++;
            continue;
        }
    }
    cout << res << endl;
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(first, second) '(' << first << ',' << second << ')'
constexpr ll MOD = 1000000007;
//constexpr ll MOD = 998244353;
ll INF = 1001001001001001001;
long double EPS = 1e-9;
long double PI = 3.141592653589793238;
template <typename T>
void remove(std::vector<T> &vector, unsigned int index)
{
   vector.erase(vector.begin() + index);
}

using Graph = vector<vector<tuple<ll, ll, ll>>>;

// MOD確認

ll N;
ll A[200010];
ll B[200010];

int main(){
   cin >> N;
   rep(i,N) cin >> A[i] >> B[i];
   sort(A,A+N);
   sort(B,B+N);
   if(N % 2 == 1){
      cout << B[N/2] - A[N/2] + 1 << endl;
   }else{
      cout << (B[N/2] + B[N/2-1]) - (A[N/2] + A[N/2-1]) + 1 << endl;
   }
}


Measured memory_bytes: 6760
#include <bits/stdc++.h>
using namespace std;
 
#define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define lli long long int
#define rep(i,n,z) for(int i=z;i<n;i++)
#define rrep(i,z) for(int i=z;i>=0;i--)
#define nl cout<<endl
#define vi vector<int>
#define vlli vector<long long int>
#define umap unordered_map
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define ipair pair <int,int>
#define llipair pair <lli,lli>
#define pq priority_queue
#define displaymatrix(a,m,n) for(int i=0;i<m;i++){for(int j=0;j<n;j++)cout<<a[i][j]<<" ";cout<<endl;}
#define printarray(a,n) for(int i=0;i<n;i++){cout<<a[i]<<" ";}nl;
#define vinput(a,n) vlli a(n);rep(i,n,0)cin>>a[i]
#define ainput(a,n) rep(i,n,0)cin>>a[i]
#define SO(a) sort(a.begin(),a.end())
#define all(x) (x).begin(),(x).end()
#define SOP(a,comp) sort(a.begin(),a.end(),comp)
#define inf INT_MAX
#define endl '\n'

int n,m;
const int S = 2e5 + 5;
vlli adj[S];
vector<bool> vis(S,false);

int cnt;

void dfs(int x){
    if(vis[x])return;
    cnt++;
    vis[x] = true;

    for(auto y: adj[x])dfs(y);
}
int main()
{
    fastio;
    cin>>n>>m;
    int x,y;
    rep(i,m,0){
        cin>>x>>y;
        adj[x].pb(y),adj[y].pb(x);
    }

    int ans = 0;
    rep(i,n+1,1){
        if(vis[i])continue;
        cnt = 0;
        dfs(i);
        ans = max(ans,cnt);
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 20768
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    int N, a[100], ans = 0;
    cin >> N;
    for (int i = 0; i < N; i++) cin >> a[i];
    sort(a, a + N);
    for (int i = 1; i < N; i++) ans += a[i] - a[i-1];
    cout << ans << endl;
}

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define repds(i, n) for (int i = (n); i > 0; --i)
#define loop(i, x, n) for (int i = (x); i < (n); ++i)
#define loops(i, x, n) for (int i = (x); i <= (n); ++i)
#define loopd(i, x, n) for (int i = (x); i > (n); --i)
#define loopds(i, x, n) for (int i = (x); i >= (n); --i)
#define itrep(i, a) for (auto i = begin(a); i != end(a); ++i)
#define itrepd(i, a) for (auto i = rbegin(a); i != rend(a); ++i)
#define each(i, x) for (auto &&i : x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(a) ((int)a.size())
#define pow2(n) (n * n)
#define int ll
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
using p_queue = priority_queue<T, vector<T>, greater<T>>;
constexpr ll inf = static_cast<ll>(1e18);
constexpr int iinf = static_cast<int>(1e9);
constexpr double dinf = 1e10;
constexpr ld ldinf = 1e18;
constexpr double eps = 1e-9;
ll mod = static_cast<ll>(1e9 + 7);
template <class T, class... Args>
constexpr void mins(T &a, Args... args) { a = min<T>({a, args...}); }
template <class T, class... Args>
constexpr void maxs(T &a, Args... args) { a = max<T>({a, args...}); }
template <class F>
class FixPoint : private F {
public:
    explicit constexpr FixPoint(F &&f) : F(std::forward<F>(f)) {}
    template <class... Args>
    constexpr decltype(auto) operator()(Args &&... args) const { return F::operator()(*this, std::forward<Args>(args)...); }
};
template <class F>
constexpr decltype(auto) funcs(F &&f) { return FixPoint<F>{std::forward<F>(f)}; }
template <class T>
std::vector<T> vecs(int n, T &&val) { return std::vector<T>(std::forward<int>(n), std::forward<T>(val)); }
template <class... Args>
decltype(auto) vecs(int n, Args &&... args) { return std::vector<decltype(vecs(std::forward<Args>(args)...))>(std::forward<int>(n), vecs(std::forward<Args>(args)...)); }
template <class V, class T>
void fill(V &x, const T &val) { x = val; }
template <class V, class T>
void fill(vector<V> &vec, const T &val) {
    for (auto &&v : vec)
        fill(v, val);
}

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

signed main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    cout.flush();
    return 0;
}

Measured memory_bytes: 3636
#!/usr/bin/perl

my ($whi,$bla) = map {$_ - 0} split(/\s+/o,<STDIN>);

my $w = '.';
my $b = '#';
my @l = ();
for(my $i=0;$i<50;$i++){
  push(@l,($w x 100));
}
for(my $i=0;$i<50;$i++){
  push(@l,($b x 100));
}

my $cb = $bla - 1;

if( $cb > 0 ){
  for(my $i=0;$i<16;$i++){
    for(my $j=1;$j<=98;$j++){
      substr($l[3*$i+1+(($j % 2) == 1 ? 0 : 1)],$j,1) = $b;
      $cb --;
      last if $cb <= 0;
    }
    last if $cb <= 0;
  }
}

my $cw = $whi - 1;

if( $cw > 0 ){
  for(my $i=0;$i<16;$i++){
    for(my $j=1;$j<=98;$j++){
      substr($l[50+3*$i+1+(($j % 2) == 1 ? 0 : 1)],$j,1) = $w;
      $cw --;
      last if $cw <= 0;
    }
    last if $cw <= 0;
  }
}

print "100 100\n";
for(my $i=0;$i<100;$i++){
  print "$l[$i]\n";
}

exit(0);



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

int main(){
int n;
scanf("%d",&n);
int a[n];
int ans[n];
for(int i=0;i<n;i++){
	scanf("%d",&a[i]);
}
int sum=0;
sum=sum+abs(a[0]);
for(int i=1;i<n;i++){
	sum=sum+abs(a[i]-a[i-1]);
}
sum=sum+abs(a[n-1]);
if(a[0]*(a[1]-a[0])>=0){
	ans[0]=sum;
}else if(abs(a[0])>abs(a[1]-a[0])){
	ans[0]=sum-2*abs(a[1]-a[0]);
}else{
	ans[0]=sum-2*abs(a[0]);
}
for(int i=0;i<n-1;i++){
	if((a[i]-a[i-1])*(a[i+1]-a[i])>=0){
		ans[i]=sum;
	}else if(abs(a[i]-a[i-1])>abs(a[i+1]-a[i])){
		ans[i]=sum-2*abs(a[i+1]-a[i]);
	}else{
		ans[i]=sum-2*abs(a[i]-a[i-1]);
	}
}
if((a[n-1]-a[n-2])*(-a[n-1])>=0){
	ans[n-1]=sum;
}else if(abs(a[n-1]-a[n-2])>abs(a[n-1])){
	ans[n-1]=sum-2*abs(a[n-1]);
}else{
	ans[n-1]=sum-2*abs(a[n-1]-a[n-2]);
}

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

}

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


int main(){
	long long l, r, i, j, ans = 114514;
	cin >> l >> r;

	if (r - l >= 2018){
	       	cout << 0 << endl;
		return 0;
	}
	else {
		for (i = l; i <= r - 1; i++){
			for (j = i + 1; j <= r; j++){
				ans = min(ans, (i * j) % 2019);
			}
		}
		cout << ans << endl;
	}
	return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define per(i,a,b) for(ll i=(b-1);i>=(a);--i)
#define clr(a, b) memset((a), (b) ,sizeof(a))
#define ctos(c) string(1,c)
#define print(x) cout<<#x<<" = "<<x<<endl;

#define MOD 1000000007

int main() {
  ll n,c,k;
  cin>>n>>c>>k;
  vector<ll> v;
  rep(i,0,n){
    ll a;
    cin>>a;
    v.pb(a);
  }
  sort(all(v));
  ll index = 0;
  ll lim = v[index]+k;
  ll t = 1;
  ll ans = 1;
  index++;
  while(1){
    if(index==n)break;
    if(v[index]>lim||t+1>c){
      lim = v[index]+k;
      t = 1;
      ans++;
    }
    else{
      t++;
    }
    index++;
  }
  printf("%lld\n",ans);
  return 0;
}

Measured memory_bytes: 1400
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;} return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;} return 0;}
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(vec) vec.begin(),vec.end()
typedef long long ll;
typedef pair<ll,ll> l_l;
typedef pair<int,int> i_i;
const ll mod=1e9+7;
const int inf=1<<30;

int main(){
  int n; cin >> n;
  vector<int> a(n);
  rep(i,n) cin >> a[i];
  multiset<int> st;
  reverse(all(a));
  rep(i,n){
    if(st.upper_bound(a[i])==st.end()){
      st.insert(a[i]);
    }
    else{
      st.erase(st.find(*st.upper_bound(a[i])));
      st.insert(a[i]);
    }
  }
  int ans=st.size();
  cout << ans << endl;
}

Measured memory_bytes: 5376
#include <bits/stdc++.h>
#define rep0(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main()
{
    int A,B,C;
    cin >> A >> B >> C;
    cout << C/A*B;
}


Measured memory_bytes: 3640
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
std::vector<int> weight;
vector<ll> value;
vector< vector<ll> > ans;
ll solveKnapsack(int N,int C){
	if(N==0 || C==0){
		return 0;
	}
	if(ans[N][C]!=-1){
		return ans[N][C];
	}
	if(weight[N-1]<=C){
		ans[N][C] = max(value[N-1]+solveKnapsack(N-1,C-weight[N-1]),
			solveKnapsack(N-1,C));
	}
	else{
		ans[N][C] = solveKnapsack(N-1,C);
	}
	return ans[N][C];
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	ll N,W;
	cin>>N>>W;
	weight.resize(N);
	value.resize(N);
	ans.resize(N+1);
	for(int i=0;i<N;i++){
		ans[i].resize(W+1,-1);
		cin>>weight[i]>>value[i];
	}
	ans[N].resize(W+1,-1);
	for(int i=0;i<=W;i++){
		ans[0][i] = 0;	
	}
	for(int i=0;i<=N;i++){
		ans[i][0] = 0;	
	}
	
	long long final_ans = solveKnapsack(N,W);

	if(final_ans==-1){
		cout<<"0\n";
	}
	else{
		cout<<final_ans<<endl;
	}
	return 0;
}

Measured memory_bytes: 82276
#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <climits>
#include <cstdlib>
#include <vector>
#include <queue>
#include <unordered_map> 
#include <iomanip>
using namespace std;
#define ALL(a) (a).begin(),(a).end()
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define INF (INT_MAX/3)
#define PSB push_back
#define PN 1000000007
typedef long long LL;
//#define int long long


//booooooooooooost
//#include <boost/multiprecision/cpp_int.hpp>
//#include <boost/multiprecision/cpp_dec_float.hpp>
//namespace mp = boost::multiprecision;	//多倍長整数(mp::cpp_int), 多倍長小数(mp::cpp_dec_float_100)



LL inv_mod(LL a, LL m)
{
	LL b, x, u, q, tmp;

	b = m; x = 1; u = 0;
	while (b > 0) {
		q = a / b;
		tmp = u; u = x - q * u; x = tmp;
		tmp = b; b = a - q * b; a = tmp;
	}

	return (x < 0) ? m + x : x;
}



signed main() {

	int K, T;
	vector<int> a;
	cin >> K >> T;
	REP(i, T) {
		int tmp;
		cin >> tmp;
		a.PSB(tmp);
	}

	sort(ALL(a));
	int ans = a.back();
	for (int i = 0; i < a.size() - 1; ++i) ans -= a[i];

	cout << max(0, ans - 1) << endl;



	return 0;
}

Measured memory_bytes: 256
n = int(input())
ab = [list(map(int, input().split())) for _ in range(n)]

a = [x[0] for x in ab]
a.sort()
b = [y[1] for y in ab]
b.sort()

if n % 2 == 0:
    print((b[n // 2 - 1] + b[n // 2]) - (a[n // 2 - 1] + a[n // 2]) + 1)
else:
    print(b[(n - 1) // 2] - a[(n - 1) // 2] + 1)

Measured memory_bytes: 49360
#include <stdio.h>
#define BUF_SIZE 100000


int main() {
  char command[][10] = {"click", "back", "forward", "show"};

  int n, m, W, H;
  int i, j, k, l, c;
  int x, y;
  char str[10];

  int buf[BUF_SIZE], p;

  struct {
    char name[21];
    int bi;
    struct {
      int x1, y1, x2, y2;
      char link[21];
    } button[100];

  } page[100];

  while(1) {
    scanf("%d", &n);
    if(n == 0) break;
    scanf("%d %d", &W, &H);
    for(i = 0; i < n; i++) {
      scanf("%s %d", page[i].name, &page[i].bi);
      for(j = 0; j < page[i].bi; j++) {
	scanf("%d %d %d %d %s",
	      &page[i].button[j].x1,
	      &page[i].button[j].y1,
	      &page[i].button[j].x2,
	      &page[i].button[j].y2,
	      page[i].button[j].link
	      );
      }
    }

    for(i = 0; i < BUF_SIZE; i++) {
      buf[i] = -1;
    }
    buf[0] = 0;
    p = 0;

    scanf("%d", &m);
    for(i = 0; i < m; i++) {
      scanf("%s", str);
      for(c = 0; c < 4; c++) {
	if(strcmp(str, command[c]) == 0) break;
      }
      switch(c) {
      case 0:
	scanf("%d %d", &x, &y);
	for(j = 0; j < page[buf[p]].bi; j++) {
	  if(x >= page[buf[p]].button[j].x1 &&
	     x <= page[buf[p]].button[j].x2 &&
	     y >= page[buf[p]].button[j].y1 &&
	     y <= page[buf[p]].button[j].y2 ) {
	    for(k = 0; k < n; k++) {
	      if(strcmp(page[buf[p]].button[j].link, page[k].name) == 0) {
		p++;
		buf[p] = k;
		for(l = p+1; l < BUF_SIZE; l++) {
		  buf[l] = -1;
		}
		break;
	      }
	    }
	    break;
	  }
	}
	break;
      case 1:
	if(p != 0) p--;
	break;
      case 2:
	if(p != BUF_SIZE - 1 && buf[p+1] != -1) p++;
	break;
      case 3:
	printf("%s\n", page[buf[p]].name);
	break;
      }
    }
  }
  return 0;
}

Measured memory_bytes: 816
#include <bits/stdc++.h>
using namespace std;
long long f(long long x,long long y){
	if(!y)return -x;
	return x/y*y*2+f(y,x%y);
}
int main(){
	long long n,x;
	scanf("%lld%lld",&n,&x);
	printf("%lld",n+f(n-x,x));
	return 0;
}


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

using namespace std;
#define ll long long
const int N = 2e3 + 5, mod = 1e9 + 7;
int dp[N][N][3];
int a[N], b[N];

int n, m;

int solve(int i, int j, int s) {
    if (i == n || j == m) return s == 0;
    int &ret = dp[i][j][s];
    if (ret != -1) return ret;
    ret = 0;
    if (s == 0) {
        ret = 1 + solve(i + 1, j + 1, 1);
    }
    if (s == 1) {
        ret = solve(i + 1, j, 1);
        ret = (ret + solve(i, j, 2)) % mod;
    }
    if (s == 2) {
        ret = solve(i, j + 1, 2);
        if (a[i] == b[j]) {
            ret = (ret + solve(i, j, 0)) % mod;
        }
    }

    return ret;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    memset(dp, -1, sizeof(dp));
    cin >> n >> m;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < m; i++) cin >> b[i];

    cout << (solve(0, 0, 1) + 1) % mod << endl;
    return 0;
}

Measured memory_bytes: 47616
n = int(input())
yo =[]
for i in range(n):
    yo.append(list(map(int,input().split())))
now =[0, 0, 0]
for i in range(n):
    c = yo[i][0] - now[0]-(abs(yo[i][1]-now[1]) + abs(yo[i][2]-now[2]))
    if c >= 0 and c%2 == 0:
        now =yo[i]
    else:
        print('No')
        exit()
print('Yes')

Measured memory_bytes: 27300
Imports System.Collections.Generic
Imports System.Console

Module Program
    Sub Main()
        Dim D As Integer = NextInt()
        Dim N As Integer = NextInt()

        Dim ans As Integer = New Integer() {N, N + 1}(B2I(N = 100)) * CInt(100 ^ D)
        WriteLine(ans)
    End Sub

    Function B2I(ok As Boolean) As Integer
        If ok Then Return 1
        Return 0
    End Function

    Function NextInt() As Integer
        Return CInt(NextString())
    End Function

    Function NextString() As String
        Dim result As New List(Of Char)
        Do
            Dim tmp As Integer = Read()
            If tmp < 0 Then Exit Do
            Dim nextChar As Char = Chr(tmp)

            If Not Char.IsWhiteSpace(nextChar) Then
                result.Add(nextChar)
            ElseIf nextChar <> vbCr Then
                Exit Do
            End If
        Loop
        Return New String(result.ToArray())
    End Function

End Module


Measured memory_bytes: 20560
def main():
    H, W, M = (int(i) for i in input().split())
    hc = [0]*(H+1)
    wc = [0]*(W+1)
    maps = set()
    for _ in range(M):
        h, w = (int(i) for i in input().split())
        hc[h] += 1
        wc[w] += 1
        maps.add((h, w))
    mah = max(hc)
    maw = max(wc)
    ans = mah+maw
    hmaps = set()
    wmaps = set()
    for i, h in enumerate(hc):
        if mah == h:
            hmaps.add(i)
    for i, w in enumerate(wc):
        if maw == w:
            wmaps.add(i)

    for h in hmaps:
        for w in wmaps:
            if (h, w) not in maps:
                print(ans)
                return
    else:
        print(ans-1)


if __name__ == '__main__':
    main()


Measured memory_bytes: 170900
import math

def prime(a):
	if a==1:
		return 0
	n=2
	while n<=math.sqrt(a):
		if a%n==0:
			return 0
		n+=1
	return 1

N=int(input())

count=0
for i in range(N):
	a=int(input())
	count+=prime(a)
print(count)

Measured memory_bytes: 7796
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define rep(i,x) for(int i=0;i<(x);++i)
#define rep1(i,x) for(int i=1;i<=(x);++i)
#define rrep(i,x) for(int i=(x)-1;i>=0;--i)
#define rrep1(i,x) for(int i=(x);i>=1;--i)
#define int long long
#define FOR(i,a,x) for(int i=(a);i<(x);++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define omajinai ios::sync_with_stdio(false);cin.tie(0)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

template<typename T>T& max(T&a,T&b){if(a>=b)return a;return b;}
template<typename T>T& min(T&a,T&b){if(a<b)return a;return b;}
template<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<typename T>T get(){T a;cin>>a;return a;}
template<typename T>T rev(T a){reverse(all(a));return a;}
template<typename T>istream&operator>>(istream&is,vector<T>&vec){rep(i,vec.size())is>>vec[i];return is;}
template<typename T>vector<T>&sort(vector<T>&a){sort(all(a));return a;}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

int x[100010], y[100010];

int a[100010];
int nokori[100010];
bool flg[100010];

signed main()
{
    fill_n(a, 100010, 1);

    int N, M; cin >> N >> M;

    for (int i = 0; i < M; ++i) {
        scanf("%d%d",x+i,y+i);
    }

    flg[1] = true;

    for (int i = 0; i < M; ++i) {
        if (flg[x[i]]) {
            flg[y[i]] = true;
        }
        
        a[x[i]]--;
        a[y[i]]++;

        if (a[x[i]] == 0) flg[x[i]] = false;
    }

    int ans = 0;
    for (int i = 1; i <= N; ++i) ans += flg[i];

    cout << ans << endl;
}


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

using namespace std;

int main() {
    int N;
    cin >> N;
    vector<int> L(N);
    for(int n=0; n<N; n++) {
        cin >> L[n];
    }
    
    sort(L.begin(), L.end());
    int ans=0;
    for(int c=N-1; c>=0; c--) {
        for(int b=c-1; b>=0; b--) {
            auto end=L.begin();
            end += b;
            // cout << L[c]-L[b] << " ";
            // cout << end - upper_bound(L.begin(), end, L[c]-L[b]) << endl;
            ans += end - upper_bound(L.begin(), end, L[c]-L[b]);
        }
    }
    cout << ans << endl;

    return 0;
    
}

Measured memory_bytes: 256
#from fractions import gcd
#mod = 10 ** 9 + 7
#N = int(input())
#a = list(map(int,input().split()))
#a,b,c = map(int,input().split())
#ans = [0] * N

H,W = map(int,input().split())
word = []
for i in range(H):
    word.append('#'+input()+'#')

print('#'*(W+2))
for i in range(H):
    print(word[i])

print('#'*(W+2))


Measured memory_bytes: 3060
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
char s[N];
int n;
int totl,totr;
struct pos{
	int r,t;
	bool operator<(const pos &x)const{
		return r<x.r;
	}
};
struct neg{
	int l,t;
	bool operator<(const neg &x)const{
		return l<x.l;
	}
};
pos a[N];
neg b[N];
int numa,numb,l,r;
bool ans;
int main()
{
	//freopen("in.txt","r",stdin);
	scanf("%d",&n);
	for(int i=1,x,y;i<=n;++i){
		x=y=0;
		scanf("%s",s);
		for(int j=0;j<strlen(s);++j){
			if(s[j]=='(') ++x;
			else{
				if(x) --x;
				else ++y;
			}
		}
		totl+=x,totr+=y;
		if(x>=y) a[numa++]=(pos){y,x-y};
		else b[numb++]=(neg){x,y-x};
	}
	if(totl!=totr) printf("No\n");
	else{
		ans=false;
		sort(a,a+numa);
		sort(b,b+numb);
		for(int i=0;i<numa;++i){
			if(a[i].r<=l) l+=a[i].t;
			else{
				ans=true;
				break;
			}
		}
		for(int i=0;i<numb;++i){
			if(b[i].l<=r) r+=b[i].t;
			else{
				ans=true;
				break;
			}
		}
		if(ans) printf("No\n");
		else printf("Yes\n");
	}
}

Measured memory_bytes: 11444
#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 
#define pi 3.1415926535897
#define ll long long int
#define ld long double
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define mod 1000000007
#define mod1 998244353
#define inf 1000000000000000000
#define endl '\n'
ll dp[1005][1005];
ll arr[1005][1005];
int main()
{
	fastio;
	#ifdef APNA_IO
		freopen("input.txt" , "rt" ,stdin);
		freopen("output.txt" , "wt" ,stdout);
	#endif
	ll n,m;
	cin>>n>>m;
	for(ll i=1;i<=n;i++)
	{
		for(ll j=1;j<=m;j++)
		{
			char c;
			cin>>c;
			if(c=='.')
				arr[i][j]=1;
			else if(c=='#')
				arr[i][j]=0;
		}
	}
	ll k=1;
	for(ll i=1;i<=m;i++)
	{
		if(!arr[1][i])
			k=0;
		dp[1][i]=k;
	}
	for(ll i=2;i<=n;i++)
	{
		for(ll j=1;j<=m;j++)
		{
			if(arr[i][j])
				dp[i][j]=(dp[i-1][j]+dp[i][j-1])%mod;
			else
				dp[i][j]=0;
		}
	}
	cout<<dp[n][m]<<endl;
	return 0;
}

Measured memory_bytes: 16000
/*
                                                                                                                                               
8b        d8                           888888888888                                             88b           d88                  ad88888ba   
 Y8,    ,8P                                 88                                        ,d        888b         d888                 d8"     "8b  
  Y8,  ,8P                                  88                                        88        88`8b       d8'88                 ""      a8P  
   "8aa8"  ,adPPYba,   88       88          88  8b,dPPYba,  88       88  ,adPPYba,  MM88MMM     88 `8b     d8' 88   ,adPPYba,          ,a8P"   
    `88'  a8"     "8a  88       88          88  88P'   "Y8  88       88  I8[    ""    88        88  `8b   d8'  88  a8P_____88         d8"      
     88   8b       d8  88       88          88  88          88       88   `"Y8ba,     88        88   `8b d8'   88  8PP"""""""         ""       
     88   "8a,   ,a8"  "8a,   ,a88          88  88          "8a,   ,a88  aa    ]8I    88,       88    `888'    88  "8b,   ,aa         aa       
     88    `"YbbdP"'    `"YbbdP'Y8          88  88           `"YbbdP'Y8  `"YbbdP"'    "Y888     88     `8'     88   `"Ybbd8"'         88       
                                                                                                                                               
*/
#include <bits/stdc++.h>
using namespace std;

int n;
long long total, a[3010], dp[3010][3010];

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

	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		dp[i][i] = a[i];
		total += a[i];
	}

	for (int len = 1; len < n; len++) {
		for (int i = 0; i <= n - len; i++) {
			int j = i + len;
			long long x = ((i + 2) <= j) ? dp[i + 2][j] : 0;
			long long y = ((i + 1) <= (j - 1)) ? dp[i + 1][j - 1] : 0;
			long long z = (i <= (j - 2)) ? dp[i][j - 2] : 0;

			dp[i][j] = max(a[i] + min(x, y), a[j] + min(y, z));
		}
	}

	cout << 2 * dp[0][n - 1] - total << '\n';

	return 0;
} 

Measured memory_bytes: 70144
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
int main()
{
    int a,b,c;
    cin >> a >> b >> c;
    int ans = 0;
    for(int i = a;i <= b;i++)
    {
        if(c % i == 0)
        {
            ans++;
        }
    }
    cout << ans << "\n";
    
    return 0;
}


Measured memory_bytes: 3116
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <map>
#include <set>
#include <queue>
#include <list>
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,a,b) for(int i=int(a);i<int(b);++i)
#define crep(i) for(char i='a';i<='z';i++)
#define psortsecond(A,N) sort(A,A+N,[](const pii &a, const pii &b){return a.second<b.second;});
#define psortfirst(A,N) sort(A,A+N,[](const pii &a, const pii &b){return a.first<b.first;});
#define All(x) (x).begin(),(x).end()
int ctoi(const char c){
  if('0' <= c && c <= '9') return (c-'0');
  return -1;
}
using namespace std;
using pii  = pair<int,int>;
long long gcd(long long a, long long b){return (b == 0 ? a : gcd(b, a%b));}
long long lcm(long long a, long long b){return a*b/gcd(a,b);}
typedef long long ll;
#define MOD 1000000007
#define EPS 10e-8

ll V[107][107];

int main(){
    ll N,A,B,C;
    cin >> N;
    rep(i,N){
        cin >> A >> B;
        rep(j,B){
            cin >> C;
            V[A-1][C-1]=1;
        }
    }
    rep(i,N){
        rep(j,N){
            cout << V[i][j];
            if(j!=N-1){
                cout <<" ";
            }
        }
        cout << endl;
    }
}


Measured memory_bytes: 3136
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long ll;

ll vs[200000+10];
ll cs[200000+10];
ll rsLeft[200000+10];
ll rsRight[200000+10];
ll ts[10];

int main(void) {
  int n;
  scanf("%d", &n);
  REP(i, n) {
    scanf("%lld", &vs[i]);
    cs[i+1] = cs[i] + vs[i];
  }

  {
    ll s1 = vs[0];
    ll s2 = vs[1];
    int i1 = 1;
    rsLeft[2] = i1;
    for(int i2 = 3; i2 <= n-2; ++i2) {
      s2 += vs[i2-1];
      ll cur = abs(s1 - s2);
      while(i1 < i2) {
        ll tmp = abs((s1+vs[i1]) - (s2-vs[i1]));
        if(tmp <= cur) {
          cur = tmp;
          s1 += vs[i1];
          s2 -= vs[i1];
          ++i1;
        } else {
          break;
        }
      }
      rsLeft[i2] = i1;
    }
  }

  {
    ll s3 = vs[n-2];
    ll s4 = vs[n-1];
    int i3 = n-1;
    rsRight[n-2] = i3;
    for(int i2 = n-3; i2 >= 2; --i2) {
      s3 += vs[i2];
      ll cur = abs(s3 - s4);
      while(i2 < i3) {
        ll tmp = abs((s3-vs[i3-1]) - (s4+vs[i3-1]));
        if(tmp <= cur) {
          cur = tmp;
          s3 -= vs[i3-1];
          s4 += vs[i3-1];
          --i3;
        } else {
          break;
        }
      }
      rsRight[i2] = i3;
    }
  }

  // REP(i, n+1) {
  //   cerr << " " << rsLeft[i];
  // }
  // cerr << endl;
  // REP(i, n+1) {
  //   cerr << " " << rsRight[i];
  // }
  // cerr << endl;

  ll res = 1LL << 60;
  for(int i2 = 2; i2 <= n-2; ++i2) {
    int i1 = rsLeft[i2];
    int i3 = rsRight[i2];
    ts[0] = cs[i1] - cs[0];
    ts[1] = cs[i2] - cs[i1];
    ts[2] = cs[i3] - cs[i2];
    ts[3] = cs[n] - cs[i3];
    sort(ts, ts + 4);
    res = min(res, ts[3] - ts[0]);
  }
  printf("%lld\n", res);
  
  return 0;
}


Measured memory_bytes: 6528