akhauriyash/Code-Regression huggingface dataset:
// Package main provides
//
// File:  d.go
// Author: ymiyamoto
//
// Created on Wed Dec 19 22:13:45 2018
//
package main

import "fmt"

func isPrime(n int) bool {
	for i := 2; i*i <= n; i++ {
		if n%i == 0 {
			return false
		}
	}
	return true
}

func main() {
	var N, M int
	fmt.Scan(&N, &M)

	if N == 1 {
		fmt.Println(M)
		return
	}

	if isPrime(M) {
		fmt.Println(1)
		return
	}

	for i := N; i <= M; i++ {
		if M%i == 0 {
			fmt.Println(M / i)
			return
		}
	}
}


Measured memory_bytes: 512
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = getString(sc);
		String[] otherString = s.split("hi");

		boolean isHitachi = true;
		for (String os : otherString) {
			if (!os.equals("")) {
				isHitachi = false;
				break;
			}
		}
		if (isHitachi) {
			System.out.println("Yes");
		} else {
			System.out.println("No");
		}
	}

	/**
	 * *汎用メソッド <br>
	 * スキャナーからの入力値をlongで返却
	 *
	 * @return
	 */
	public static long getLong(Scanner sc) {

		return Long.parseLong(sc.next());
	}

	/**
	 * *汎用メソッド <br>
	 * スキャナーからの入力値をintで返却
	 *
	 * @return
	 */
	public static int getInt(Scanner sc) {

		return Integer.parseInt(sc.next());
	}

	/**
	 * *汎用メソッド <br>
	 * スキャナーからの入力値をStringで返却
	 *
	 * @return
	 */
	public static String getString(Scanner sc) {

		return sc.next();
	}

	/**
	 * *汎用メソッド <br>
	 * スキャナーからの入力値をint配列で返却
	 *
	 * @return
	 */
	public static int[] getIntArray(Scanner sc) {
		String line = "";
		// 直前にnext()を実行していると改行コードだけ取ってしまうのでその対策
		while (line.equals("")) {
			line = sc.nextLine();
		}

		String[] strArray = line.split(" ");
		int[] intArray = new int[strArray.length];
		for (int i = 0; i < strArray.length; i++) {
			intArray[i] = Integer.parseInt(strArray[i]);
		}
		return intArray;
	}

}


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

#define rep(i, N) for(int (i) = 0; (i) < (N); i++)
#define all(v) v.begin(), v.end()
using i64 = int_fast64_t;

int main(){
    i64  A,B,K;
    cin >> A >> B >> K;
    i64 t = max(A-K, 0l);
    i64 s = max(K-A, 0l);
    
    cout << t << " ";
    cout << max(B-s, 0l) << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define SORT(x) sort(x.begin(), x.end())
#define RS(x) rsort(x.begin(),x.end()))
#define REVE(x) reverse(x.begin(), x.end())
#define ALL(x) (x).begin(), (x).end()
int INF = 1e9 + 1;
int MOD = 1e9+7;
signed main(){
    int a,b,ans=0,i=1;
    cin >> a >>b;
    while(i<b){
        --i;
        i+=a;
        ans++;
    }
    cout << ans;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
  unordered_map<string,int>out;
  int n;
  cin>>n;
  string a[n];
  for(int i=0;i<n;i++)
    cin>>a[i];
  out["AC"]=0;
  out["WA"]=0;
  out["TLE"]=0;
  out["RE"]=0;
  for(int i=0;i<n;i++)
    out[a[i]]++;
  cout<<"AC"<<" x"<<" "<<out["AC"]<<"\n";
   cout<<"WA"<<" x"<<" "<<out["WA"]<<"\n";
   cout<<"TLE"<<" x"<<" "<<out["TLE"]<<"\n";
   cout<<"RE"<<" x"<<" "<<out["RE"];
}


Measured memory_bytes: 6720
s = input()

k = [0] * (len(s) + 1)
for i in range(len(s)):
    if s[i] == '<':
        k[i + 1] = max(k[i + 1], k[i] + 1)

for i in reversed(range(len(s))):
    if s[i] == '>':
        k[i] = max(k[i], k[i + 1] + 1)

# print(k)
print(sum(k))

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

int main() {
  int a,b,c,d,e,N;
  cin >> a >> b >> c >> d >> e >> N;
  if (e - a > N ){
    cout <<":(" << endl;
  }
  else if  (e - a <= N ){
    cout <<"Yay!" << endl;
  }
}

Measured memory_bytes: 256
#define _USE_MATH_DEFINES
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>

using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

int main() {
  int n;
  scanf("%d", &n);
  vector<vector<ii>> a(n);
  for (int i = 0; i < n; ++i) {
    int len;
    scanf("%d", &len);
    auto& cur = a[i];
    cur.resize(len);
    for (int j = 0; j < len; ++j) {
      scanf("%d%d", &cur[j].first, &cur[j].second);
      --cur[j].first;
    }
  }
  int best = 0;
  for (int k = 1; k < (1 << n); ++k) {
    bool found = false;
    for (int i = 0; i < n; ++i) {
      if (k & (1 << i)) {
        int len = a[i].size();
        for (int j = 0; j < len; ++j) {
          int x = a[i][j].first, y = a[i][j].second;
          if (y != ((k >> x) & 1)) found = true;
        }
      }
    }
    // if (!found) {
    //   trace(k);
    //   for (int i = 0; i < n; ++i) {
    //     if (k & (1 << i)) cout << i << " ";
    //   }
    //   cout << endl;
    // }
    if (!found) best = max(best, __builtin_popcount(k));
  }
  printf("%d\n", best);
  return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#define rep(i,n)for(int i=0;i<(int)(n);i++)
int main(){
    int n,m,x,c[12]={0},a[12][12]={0},y,z[12],b,d=2e9;
    scanf("%d%d%d",&n,&m,&x);
    rep(i,n){
    scanf("%d",&c[i]);
        rep(j,m){
            scanf("%d",&a[i][j]);
        }
    }
    rep(i,2){rep(j,2){rep(k,2){rep(l,2){rep(o,2){rep(p,2){rep(q,2){rep(r,2){rep(s,2){rep(t,2){rep(u,2){rep(v,2){
        y=i*c[0]+j*c[1]+k*c[2]+l*c[3]+o*c[4]+p*c[5]+q*c[6]+r*c[7]+s*c[8]+t*c[9]+u*c[10]+v*c[11];
        for(b=0;b<m;b++){
            z[b]=i*a[0][b]+j*a[1][b]+k*a[2][b]+l*a[3][b]+o*a[4][b]+p*a[5][b]+q*a[6][b]+r*a[7][b]+s*a[8][b]+t*a[9][b]+u*a[10][b]+v*a[11][b];
            if(z[b]<x)break;
        }
        if(b==m&&y<d)d = y;
    }}}}}}}}}}}}
    if(d==(int)2e9)puts("-1");
    else printf("%d",d);
    return 0;
}

Measured memory_bytes: 1708
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int main(){
  int n, m;
  cin >> n >> m;
  int ans = 0;
  for(int i = 1; i * i <= m; i++){
    if(m % i != 0) continue;
    if((ll)n*i <= (ll)m){
      ans = max(ans, i);
    }
    int j = m / i;
    if((ll)n*j <= (ll)m){
      ans = max(ans, j);
    }
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define pb          push_back
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define endl        "\n"
#define fast        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define int         long long int
#define mod         1000000007
#define test        int t;cin>>t;while(t--)
#define REP(i,a,b)  for(int i=a,i<=b;i++)
#define MAXN        200001

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

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

    int dp[n+1];
    for(int i=0;i<=n;i++){
        dp[i]=INT_MAX;
    }
    dp[0]=0;dp[1]=abs(arr[1]-arr[0]);

    for(int i=2;i<n;i++){
        dp[i]=min(dp[i-1]+abs(arr[i]-arr[i-1]) , dp[i-2]+abs(arr[i]-arr[i-2]));
    }
    cout<<dp[n-1]<<endl;

    
    return 0;
}

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

int main() {
	long long N; cin >> N;
	vector<long long> A(5);
	long long mi = 1LL << 60;
	for (int i = 0; i < 5; i++)
	{
		cin >> A[i];
		mi = min(mi, A[i]);
	}
	if(N%mi == 0)
		cout << N/mi + 4 << endl;
	else
		cout << N/mi+1 + 4 << endl;
}

Measured memory_bytes: 256
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
    public static void main(String[]args){
		Scanner sc = new Scanner(System.in);
		PrintWriter ou = new PrintWriter(System.out);
		int n = Integer.parseInt(sc.next());
		int yono = 0;
		for(int i = 0 ; i < n ; i++){
			if(i + 1 != Integer.parseInt(sc.next())) yono++;
		}
		if(yono <= 2) ou.println("YES");
		else ou.println("NO");
		ou.flush();
	}
}

Measured memory_bytes: 35616
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int main() {
  string s, t = "CODEFESTIVAL2016";
  cin >> s;
  int cnt{};
  REP(i, s.size()) {
    if (s[i] != t[i])
      ++cnt;
  }
  cout << cnt << endl;
}


Measured memory_bytes: 256
n = int(input())
s = [None] * n
for i in range(n):
    s[i] = input()

min_a = [50] * 26

alp = [chr(i) for i in range(97, 97+26)]

for i in range(n):
    a = [0] * 26

    for j in range(len(s[i])):
        for k in range(26):
            if s[i][j] == alp[k]:
                a[k] += 1

    for l in range(26):
        if min_a[l] > a[l]:
            min_a[l] = a[l]

sum = 0
for i in range(26):
    sum += min_a[i]

s = ""
for i in range(26):
    for j in range(min_a[i]):
        s += alp[i]

print(s)

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

int main(){
	int a,b,c,x,y; cin>>a>>b>>c>>x>>y;
	cout<<min(a+b,c*2)*min(x,y)+abs(x-y)*(x>y?min(a,c*2):min(b,c*2))<<"\n"s;
}

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

int main(){
  int a,b,c,d,e,f,g,now,v[200],m[201][201]={};
  queue<int> q;
  cin >> a;
  for(int i=1;i<=a;i++){
    cin >>b>>c;
    for(int j=0;j<c;j++){
      cin >> d;
      m[b][d]=1;
    }
  }
  cin >> d;
  for(int i=0;i<d;i++){
    cin >> e >> f >> g;
    q.push(e);
    for(int j=1;j<=a;j++)v[j]=1200;
    v[e]=1;
    while(1){
      now=q.front();q.pop();
      if(now==f){
	if(v[f]>g){
	  cout << "NA" << endl;
	  break;
	}else {
	  cout << v[now] << endl;
	  break;
	}
      }
      for(int j=1;j<=a;j++){
	if(m[now][j]&&v[j]==1200){
	  v[j]=v[now]+1;
	  q.push(j);
	}
      }
      if(q.empty()){
	cout<< "NA" << endl;
	break;
      }
    }
    while(!q.empty())q.pop();
  }
  return 0;
}

Measured memory_bytes: 1376
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
#define END return 0
typedef int64_t ll;
const int mod7 = 1e9 + 7;
const int mod9 = 1e9 + 9;
const int inf = 1 << 30;
const ll INF = 1LL << 60;
template<typename T> T gcd(T a, T b) {return b ? gcd(b, a % b) : abs(a);}
template<typename T> T lcm(T a, T b) {return a / gcd(a, b) * 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 A, size_t N, typename T> void Fill(A (&array)[N], const T &val) {fill((T*)array, (T*)(array+N), val);}

int main() {
    int n, k;
    cin >> n >> k;
    vector<bool> snuke(100, true);
    rep(i, k) {
        int d;
        cin >> d;
        rep(j, d) {
            int a;
            cin >> a;
            a--;
            snuke[a] = false;
        }
    }
    int ans = 0;
    rep(i, n) ans += (snuke[i]);
    cout << ans << endl;

}

Measured memory_bytes: 3632
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define INF 0x3f3f3f3f
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
const ll mod=1000000007;
const double PI = acos(-1.0);
const double epsilon = PI / 180.0;//角度转弧度 
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
const int N = 2e5+10, M = N * 2;
int n;
ll a[N];
bool cmp(ll a, ll b) {
	return a > b;
}
int main() {
	cin>>n;
	rep(i, 1, n + 1) scanf("%lld", &a[i]);
	sort(a + 1, a + 1 + n, cmp);
	ll ans = a[1];
	rep(i, 3, n + 1) {
		ans += a[(i + 1) / 2];
	}
	cout<<ans;
	return 0;
}

Measured memory_bytes: 5276
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef vector<int> vei;
typedef vector<string> ves;
typedef vector<vector<int>> ve_vei;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<(int)(n);i++)
#define rep2(i,n) for(int i=2;i<(int)(n);i++)
#define fs first
#define sc second
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
 
int main() {
	//cout.precision(10);
	cin.tie(0);
	ios::sync_with_stdio(false);

    int A,B,C,X; cin>>A>>B>>C>>X;
    int cnt = 0;
    rep(i,A+1) rep(j,B+1) rep(k,C+1) if(500*i + 100*j + 50*k == X) cnt++;
    cout << cnt << endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#define INF 100000000
using namespace std;
typedef double d;
bool isintersect(d x1,d x2, d y1 , d y2 , d px, d py , d qx, d qy)
{
  return ((py-y1)*(x1-x2) + (x1 - px)*(y1-y2))*((qy-y1)*(x1-x2) + (x1 - qx)*(y1-y2)) >= 0;
}
int main(){
  d x1,y1,x2,y2,x3,y3,xp,yp;
  while( cin >>  x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> xp >> yp )
    {
      if( isintersect(x1,x2,y1,y2,xp,yp,x3,y3) &&
	  isintersect(x1,x3,y1,y3,xp,yp,x2,y2) &&
	  isintersect(x3,x2,y3,y2,xp,yp,x1,y1))cout << "YES" << endl;
      else cout << "NO" << endl;
    }
  return 0;
}

Measured memory_bytes: 1216
// 2011/01/13 Tazoe

#include <iostream>
using namespace std;

int main()
{
	while(true){
		int s[5][2];
		cin >> s[0][0] >> s[0][1];
		if(s[0][0]==0&&s[0][1]==0)
			break;

		for(int i=1; i<5; i++)
			cin >> s[i][0] >> s[i][1];

		int i_max = 0;
		int s_max = s[0][0]+s[0][1];
		for(int i=1; i<5; i++)
			if(s_max<s[i][0]+s[i][1]){
				i_max = i;
				s_max = s[i][0]+s[i][1];
			}

		cout << (char)('A'+i_max) << ' ' << s_max << endl;
	}

	return 0;
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
using P = pair<int,int>;
using ll = long long;
static const int INF = 10000000;
static const ll MOD = 1000000007;

ll gcd(ll a, ll b){return b!=0 ? gcd(b, a%b) : b;}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}

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



Measured memory_bytes: 256
# AGC032 Problem-AGC032

n = int(input())
a = list (map(int, input().split()))
#print (a)
a.insert(0,-1)
result = []
for i in range (n, 0, -1) :
    for j in range (i, -1, -1) :
#        print (a)
        if a[j] == j :
            result.append(j)
#            print (result)
            a = a[:j] + a[j+1:]
            break
        if j == 0 :
            result = [-1]
            break

result.reverse()
for ans in result :
    print (ans)


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

int main(){
  int A,B;
  cin >> A >> B;
  int s=0; 
  for(int i=A;i<=B;i++){
    if(i%10==i/10000%10 && i/10%10==i/1000%10)
      s++;
  }
  cout << s << endl;
}

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

typedef long long ll;

const int N = 2e5;
const ll MOD = 1e9 + 7;

ll mult(ll a, ll b)
{
    return (a * b) % MOD;
}
ll bin_pow(ll a, int n)
{
    ll res = 1;
    for (; n; n >>= 1, a = mult(a, a))
        if (n & 1) res = mult(res, a);
    return res;
}
ll inv(ll a)
{
    return bin_pow(a, MOD - 2);
}

ll f[N], rf[N];

void init_Comba()
{
    f[0] = 1;
    for (int i = 0; i < N - 1; ++i)
        f[i + 1] = mult(f[i], i + 1);
    rf[N - 1] = inv(f[N - 1]);
    for (int i = N - 1; i > 0; --i)
        rf[i - 1] = mult(rf[i], i);
}
ll A(int n, int k)
{
    if (n < k) return 0;
    return mult(f[n], rf[n - k]);
}


int deg[N];

int main()
{
    //freopen("input.txt", "r", stdin);


    init_Comba();

    int n, k;
    scanf("%d%d", &n, &k);

    for (int v = 0; v < n; ++v)
        deg[v] = 0;
    for (int i = 1; i < n; ++i)
    {
        static int u, v;
        scanf("%d%d", &u, &v);
        --u; --v;
        ++deg[u]; ++deg[v];
    }

    ll ans = k;
    ans = mult(ans, A(k - 1, deg[0]));
    for (int v = 1; v < n; ++v)
        if (deg[v] != 1) ans = mult(ans, A(k - 2, deg[v] - 1));

    printf("%lld", ans);

}


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

int main() {
  int L, R;
  int min = 2000000000;
  cin >> L >> R;
  if((R - L) <= 2019){
	L = L % 2019;
  	R = R % 2019;
  
  	if (L >= R){
      R += 2019;
  	}
  
  	int i, j;
  	if(L != 0 && R != 0){
      for (i = L; i <= R - 1; i++){
        for (j = L + 1; j <= R; j++){
          if(min > ((i * j) % 2019)) {
            min = (i * j) % 2019;
          }
        }
      }
    }
    else{
      min = 0;
    }
  }
  else{
    min = 0;
  }
  cout << min;
}

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

int main(void){
    int n;
    long long int ans=100000;
    cin>>n;
    for (int i=0;i<n;i++) {
        ans=ans*1.05;
        double tmp=ans%1000;
        ans-=tmp;
        tmp=tmp/1000;
        tmp=ceil(tmp);
        tmp*=1000;
        ans+=tmp;
    }
    cout<<ans<<endl;
}



Measured memory_bytes: 3096
#include "bits/stdc++.h"
using namespace std;

#define ASC(vec) vec.begin(), vec.end()		// 昇順ソート 例：sort(ASC(vec));
#define DESC(vec) vec.rbegin(), vec.rend()	// 降順ソート 例:sort(DESC(vec));
#define rep(i, n) for(i = 0; i < n; i++)

const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;

typedef long long ll;

// 4,8方向に探索する際に用いる配列
//int dx[4] = { 0,  0, 1, -1 };
//int dy[4] = { 1, -1, 0,  0 };
//int dx[8] = { 1, 0, -1,  0, 1, -1, -1,  1 };
//int dy[8] = { 0, 1,  0, -1, 1,  1, -1, -1 };

int main() 
{
	vector<int> A(3);
	int i;
	rep(i, 3) cin >> A[i];

	sort(ASC(A));
	int ans = 0;
	for (int i = 0; i < 2; i++)
	{
		ans += abs(A[i] - A[i + 1]);
	}
	cout << ans << endl;
}


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

typedef long long ll;

const ll MOD = 1e9 + 7;
const int N = 2e5;

struct node{
	node *next;
	int total[3];
};

node *root;

node nodes[N + 5];
int pcntr = 0;
inline node* allocc(){
	if(pcntr >= N) assert(0);
	return &nodes[pcntr++];
}

vector<int> lst[N + 5];
int dep[N + 5];
int occ[N + 5];
int po[N + 5];

inline int addmod(ll a, ll b) {
	if(a + b >= MOD) return a + b - MOD;
	return a + b;
}

inline int mulmod(ll a, ll b) {
	return (a * b) % MOD;
}

void dfs(int cur, int h) {
	dep[cur] = 1;
	occ[h]++;
	for(int i = 0;i < lst[cur].size();i++) {
		int to = lst[cur][i];
		dfs(to, h + 1);
		dep[cur] = max(dep[cur], dep[to] + 1);
	}
}

ll arr[3];

node *solve(int cur) {
	node *head = allocc();
	head->next = NULL;
	head->total[0] = head->total[1] = 1;
	head->total[2] = 0;
	int banned = -1;
	int take = 0;
	for(int i = 0;i < lst[cur].size();i++) {
		int to = lst[cur][i];
		if(dep[to] + 1 == dep[cur]) {
			banned = i;
			head->next = solve(to);
			take++;
			break;
		}
	}
	for(int i = 0;i < lst[cur].size();i++) {
		if(i == banned) continue;
		int to = lst[cur][i];
		node *tmp = solve(to);
		node *ct = head->next;
		take++;
		while(tmp != NULL) {
			arr[0] = arr[1] = arr[2] = 0;
			for(int i = 0;i < 3;i++) {
				for(int j = 0;j < 3;j++) {
					int k = min(i + j, 2);
					arr[k] = addmod(arr[k], mulmod(ct->total[i], tmp->total[j]));
				}
			}
			ct->total[0] = arr[0], ct->total[1] = arr[1], ct->total[2] = arr[2];
			if(take == lst[cur].size()) {
				ct->total[0] = addmod(ct->total[0], ct->total[2]);
				ct->total[2] = 0;
			}
			tmp = tmp->next, ct = ct->next;
		}
		if(take == lst[cur].size()) {
			while(ct != NULL) {
				ct->total[0] = addmod(ct->total[0], ct->total[2]);
				ct->total[2] = 0;
				ct = ct->next;
			}
		}
	}
	return head;
}

int main() {
	po[0] = 1;
	for(int i = 1;i <= N;i++) po[i] = mulmod(po[i - 1], 2);
	int n;
	scanf("%d",&n);
	for(int i = 1;i <= n;i++) {
		int x;
		scanf("%d",&x);
		lst[x].push_back(i);
	}
	dfs(0, 1);
	root = solve(0);
	int total = 0;
	for(int i = 1; root != NULL;i++, root = root->next) {
		total = addmod(total, mulmod(root->total[1], po[n + 1 - occ[i]]));
	}
	printf("%d\n", total);
}

Measured memory_bytes: 46336
ans = 0
 
for i in range(int(input())):
    t_i, a_i = list(map(lambda x: int(x), input().split()))
 
    if i == 0:
        t, a = t_i, a_i
 
    else:
        n = max((t + t_i - 1) // t_i, (a + a_i - 1) // a_i)
        t = n*t_i
        a = n*a_i
 
    ans = t + a

print(ans)

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

using namespace std;
int main(void){
    // Your code here!
    int s,t,u;
    cin>>s>>t>>u;
    if(t>=u){
        cout<<"delicious"<<endl;
    }
    else{
        if(s>=u-t){
            cout<<"safe"<<endl;
        }
        else{
            cout<<"dangerous"<<endl;
        }
    }
}


Measured memory_bytes: 256
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<climits>
#include<iostream>
#include<sstream>
#include<utility>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<set>
#include<stack>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;

int main()
{
	int N,F[105][105];
	ll P[105][11],res=-10000000000;
	cin>>N;
	for(int i=0;i<N;i++)
	{
		for(int j=0;j<10;j++)
			cin>>F[i][j];
	}
	for(int i=0;i<N;i++)
		for(int j=0;j<=10;j++)
			cin>>P[i][j];
	for(int cnt=1;cnt<1024;cnt++)
	{
		ll sc=0;
		int o[10]={0};
		for(int s=0;s<10;s++)
		{
			o[s]=(cnt>>s)&1;
		}
		for(int k=0;k<N;k++)
		{
			int c=0;
			for(int i=0;i<10;i++)
			{
				if(F[k][i]==1&&o[i]==1)c++;
			}
			sc+=P[k][c];
		}
		res=max(sc,res);
	}
	cout<<res<<endl;
}


Measured memory_bytes: 256
import copy

D = int(input())
c = list(map(int,input().split()))
s = []
for i in range(D):
    s.append(list(map(int,input().split())))
last = [0]*26
    
ans = 0
md = [0]*26
cand = []
cand2 = []
cand3 = []
tl = []

for day in range(D):
    for j in range(26):
        last[j] += 1
    
    for k in range(26):
        #k番目を選ぶことによる
        merit = s[day][k]
        merit += last[k] * c[k]
        demerit = 0
        for j in range(26):
            if j != k:
                demerit += c[j] * last[j]
        md[k] = merit - demerit

    #日程の確定
    md2 = sorted(md)
    t = md.index(md2[-1])
    last[t] = 0
    tl.append(t+1)
    
    can = md.index(md2[-2])
    cand.append(can+1)
    
    can2 = md.index(md2[-3])
    cand2.append(can2+1)
    
    can3 = md.index(md2[-4])
    cand3.append(can3+1)
    #print(t+1)
    

    
#得点計算

def calcans(tl):
    last = [0]*26
    ans = 0
    for day in range(D):
        #満足度プラス計算
        ans += s[day][tl[day]-1]
        for j in range(26):
            last[j] += 1
        last[tl[day]-1] = 0
        #満足度マイナス計算
        minus = 0
        for j in range(26):
            minus += c[j] * last[j]
        ans -= minus
    return ans

normal = calcans(tl)
change = -1

tl2 = copy.deepcopy(tl)
for ii in range(D):
    tl2[ii] = cand[ii]
    challenge = calcans(tl2)
    if normal < challenge:
        normal = challenge
    else:
        tl2[ii] = cand2[ii]
        challenge = calcans(tl2)
        if normal < challenge:
            normal = challenge
        else:
            tl2[ii] = cand3[ii]
            challenge = calcans(tl2)
            if normal < challenge:
                normal = challenge
            else:
                tl2[ii] = tl[ii]
        
    
for tlc in tl2:
    print(tlc)

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

//repetition
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)

//container util
#define all(x) (x).begin(),(x).end()

//typedef
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;


//conversion
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
inline ll toLL(string s) {ll v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m;
    cin >> n >> m;
    map<int,int> mp;
    VI x(n);
    map<int,int> tmp;
    map<int,int> pairCnt;
    rep(i,n){
      cin >> x[i];
      if(tmp[x[i]] > 0){
        tmp[x[i]]--;
        pairCnt[x[i]%m]++;
      }else {
        tmp[x[i]]++;
      }
      mp[x[i] % m]++;
    }


    ll ans = 0;
    rep(i,m){
      int x;
      if(i != (m-i)%m) x = min(mp[i] , mp[(m-i)%m]);
      else x = mp[i]/2;
      mp[i] -= x;
      mp[(m-i)%m] -= x;
      ans += x;
    }

    //cout << "ans" << ' ' << ans << "\n";
    rep(i,m){
      //cout << i << ':' << pairCnt[i] << "\n";
      if(pairCnt[i] > 0 && mp[i] > 0){
        ans += min(mp[i]/2,pairCnt[i]);
      }
    }

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


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

int main(){
    using namespace std;
    string s; cin >> s;
    map<char, int> m;
    for (int i = 0, N=s.size(); i < N; i++)
    {
        m[s[i]]++;
    }

    if(m.size() ==2 ) cout << "Yes" << endl;
    else cout << "No" << endl;
    
}


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

class Main {

	public static void main(String[] args) throws Exception {
		final Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int q = sc.nextInt();
		int[][] a = new int[n + 1][n + 1];
		for (int i = 0; i < m; i++) {
			int l = sc.nextInt();
			int r = sc.nextInt();
			a[l][r]++;

		}

		for (int i = 1; i <= n; i++) {
			for (int j = 0; j <= n; j++) {
				a[i][j] += a[i - 1][j];
			}
		}

		for (int i = 0; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				a[i][j] += a[i][j - 1];
			}
		}

		while (q-- > 0) {
			int l = sc.nextInt();
			int r = sc.nextInt();
			System.out.println(a[r][r] - a[r][l - 1] - a[l - 1][r] + a[l - 1][l - 1]);
		}
	}
}


Measured memory_bytes: 155048
#include <iostream>
using ll = long long;
using namespace std;

int main() {
    int w, h, n, x, y, a;
    cin >> w >> h >> n;
    int xp = w, xm = 0, yp = h , ym = 0;
    for (int i = 0; i < n; ++i) {
        cin >> x >> y >> a;
        if (a == 1) xm = max(xm,x);
        else if (a == 2) xp = min(xp,x);
        else if (a == 3) ym = max(ym,y);
        else if (a == 4) yp = min(yp,y);
    }
    cout << ((xp > xm) && (yp > ym) ? (xp-xm)*(yp-ym) : 0 ) << "\n";
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
  string s;
  getline(cin,s);
  int len=s.size();
  printf("%c",toupper(s[0]));
  for(int i=1;i<len;i++)
    if(s[i]==' ')
        printf("%c",toupper(s[i+1]));
    cout<<endl;
}


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

#define ll long long

using namespace std;

const int MAXN = 200010;
const int MAXM = 100010;
const int MAXINT = 2147483647;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;

int n, m, k;
ll ans, tot, cnt;

int a[MAXN];

string s;

inline int read() {
	int s = 0, f = 1;
	char ch = getchar();
	while ('0' > ch || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
	while ('0' <= ch && ch <= '9') {s = (s << 3) + (s << 1) + ch - 48; ch = getchar();}
	return s * f;
}

bool cmp(int x, int y) {
	return x > y;
}

int main(){
	int T;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
	}
	sort(a + 1, a + n + 1, cmp);
	ans = a[1];
	int last = a[2], p = 1;
	for (int i = 3; i <= n; i++) {
		if (i % 2) {
			last = a[++p];
		}
		ans += last;
 	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 4536
#include <iostream>

using namespace std;

int main()
{
	long long n, a;
	long long int ans = 0;
	
	cin >> n;
	
	for (int i = 0; i < n; i++) {
		cin >> a;
		ans += a - 1;
	}
	
	cout << ans << endl;
	
	return (0);
}

Measured memory_bytes: 256
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;

int main()
{
    ll n,m;
    cin>>n>>m;
    ll mi=0;
    ll k=m/2-n;
    if(k<=0){
        cout<<min(n,m/2)<<endl;
        return 0;
    }
    for(ll i=max(0ll,k-10000002);i<min(m+1,k+1000002);i++){
        mi=max(mi,min(n+i/2,(m-i)/2));
    }
    cout<<mi<<endl;
    return 0;
}

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

typedef long long ll;

#define PB push_back

int numDigits(int x) {
	if (x / 10 == 0) return 1;
	return 1 + numDigits(x / 10);
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	int n; cin >> n;
	int result = 0;
	for (int i = 1; i <= n; ++i) {
		if (numDigits(i) & 1) result++;
	}
	cout << result << "\n";
	
}

/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* print new lines
*/


Measured memory_bytes: 256
n,m = map(int,input().split())
a,ans = [[] for i in range(8)],[]
for i in range(n):
	x,y,z = map(int,input().split())
	a[0].append(x+y+z)
	a[1].append(x+y-z)
	a[2].append(x-y+z)
	a[3].append(x-y-z)
	a[4].append(-x+y+z)
	a[5].append(-x+y-z)
	a[6].append(-x-y+z)
	a[7].append(-x-y-z)
for i in range(8):
	a[i].sort(reverse=True)
	ans.append(0)
	for j in range(m):
		ans[i] += a[i][j]
print(max(ans))

Measured memory_bytes: 3428
#!/usr/bin/env python3

from collections import defaultdict
from copy import copy

def main():
    n, wmax = map(int, input().split())
    dp = defaultdict(int)
    dp[0] = 0
    for i in range(n):
        w, v = map(int, input().split())
        dppre = copy(dp)
        for y in dppre:
            dp[y + w] = max(dp[y + w], dppre[y] + v)
    res = 0
    for y, z in dp.items():
        if y <= wmax:
            res = max(res, z)
    print(res)

if __name__ == "__main__":
    main()


Measured memory_bytes: 5468
s = input()
flag = True
count = 1
for i in s:
    if (count % 2 == 1) and ( i == 'L'):
        flag = False
        count += 1
    elif (count % 2 == 0) and (i == 'R'):
        flag = False
        count += 1
    else:
        count += 1
if flag:
    print('Yes')
else:
    print('No')

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

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

int main(){
    int K, Y; cin >> K >> Y;
    if(500*K >= Y) cout << "Yes" << endl;
    else cout << "No" << endl;
}

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

struct kazu_t {
	int idnum;
	int p;
} ;

int compare_kazu(const void *a, const void *b) {
  struct kazu_t *A = (struct kazu_t *)a;
  struct kazu_t *B = (struct kazu_t *)b;
  return A->p - B->p;
}

int main(void)
{
	//変数の宣言
	int n;
	int i,j;
	int flag=0;
	int ans=0;
	int count=0;
	static struct kazu_t kazu[200010];
	//データの読み込み
	scanf("%d",&n);
	
	for(i=0;i<n;i++){
		scanf("%d",&kazu[i].p);
		kazu[i].idnum=i;
	}
	
//	printf("nは%dです\n", n);
//	printf("データの読み込み終了\n");
	//実際の処理
	qsort(kazu, n, sizeof(struct kazu_t), compare_kazu);
	
	count=1;
	for(i=1;i<n;i++){
		if(kazu[i-1].idnum<kazu[i].idnum){
			count++;
		}else{
			if(ans<count){
				ans=count;
			}
			count=1;
		}
	}
	if(ans<count) ans=count;
	
//	printf("計算部分終了\n");
	//出力
	
	printf("%d",n-ans);
	
//	printf("結果の出力終了\n");
	
}



Measured memory_bytes: 3324
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iterator>
#include <fstream>
#include <cstddef>
#include <cstring>

using namespace std;

typedef pair<int, int> P;
typedef long long ll;
#define PB push_back
#define rep(i, a) for(int i = 0; i < a; i++)
#define all(v) v.begin(),v.end()
#define FF first
#define SS second
#define mp make_pair
#define INT int_64
const ll INF = (1<<30);
const ll NEGA=-INF*10000; 


ll ans = 0;
int n,m;
int cnt=0;

int digit[50000]={0};
int main() 
{
  ll a,b,c,k;
  cin>>a>>b>>c>>k;
  if(k%2) cout<<b-a<<endl;
  else cout<<a-b<<endl;
}



Measured memory_bytes: 256
n, k = gets.split.map(&:to_i)
v = []
for i in 0...n do
  v[i] = gets.split.map(&:to_i)
end
v.sort! do |e, f|
  e[0] <=> f[0]
end
count = 0
for i in 0...n do
  count += v[i][1]
  break if count >= k
end
puts v[i][0]


Measured memory_bytes: 10488
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;

int r, g, b;

int main() {
	scanf("%d%d%d", &r, &g, &b);
	int tot = r * 100 + g * 10 + b;
	puts(tot % 4 == 0 ? "YES" : "NO");
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(ll i = 0; i<(ll)(n); i++)
#define REPS(i,n) for (ll i = 1; i<=(ll)(n); i++)
#define REPD(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define pb push_back
#define MOD 1000000007
#define MOD2 998244353
#define PI 3.141592653

#define N 110
#define K 110000

int main(){
	ll n, k; cin >> n >> k;
	vector<ll> choices(N);
	vector<vector<ll>> dp(K,vector<ll>(N));
	vector<ll> jud(K);
	REP(i,n) cin >> choices.at(i);
	FOR(i,1,k) {
		jud[i]=2;
		REP(j,n){
			if (i<choices[j]) dp[i][j] = 2;
			else if (i==choices[j]) dp[i][j] = 1;
			else dp[i][j] = 3 - jud[i-choices[j]];
			if (dp[i][j]==1) jud[i] = 1;
		}
	}
	if (jud[k]==1) cout << "First" << endl;
	else cout << "Second" << endl;
}

Measured memory_bytes: 99968
#include <bits/stdc++.h>
using i64 = long long;

int main() {
    int n, m, k;
    std::cin >> n >> m >> k;
    std::vector<std::vector<int>> ss(n + 1, std::vector<int>(m + 1));
    for (int i = 0; i < n; i++) {
        std::string s;
        std::cin >> s;
        for (int j = 0; j < m; j++) {
            ss[i + 1][j + 1] = ss[i + 1][j] + ss[i][j + 1] - ss[i][j] + (s[j] - '0');
        }
    }

    int ret = n + m;
    for (int bit = 0; bit < 1 << (n - 1); bit++) {
        int cnt = __builtin_popcount(bit);
        std::vector<int> on { 0 };
        for (int i = 0; i < n - 1; i++) if (bit & (1 << i)) on.push_back(i + 1);
        on.push_back(n);
        int lst = 0;
        for (int j = 1; j <= m; j++) {
            bool flg = false;
            for (int l = 1; l < on.size(); l++) {
                if (ss[on[l]][j] - ss[on[l - 1]][j] - ss[on[l]][lst] + ss[on[l - 1]][lst] > k) flg = true;
            }
            if (flg) {
                if (j - lst == 1) goto next;
                cnt++;
                lst = j - 1;
            }
        }
        if (ret > cnt) ret = cnt;
next:;
    }

    std::cout << ret << std::endl;

    return 0;
}


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

int main(void){
    int N;
    cin >> N;
    vector<char> c(N);
    int cnt_r = 0;
    for(int i = 0; i < N; i++){
        cin >> c[i];
        if(c[i] == 'R') cnt_r++;
    }
    
    int cnt_w = 0;
    for(int i = 0; i < cnt_r; i++){
        if(c[i] == 'W') cnt_w++;
    }
    
    cout << cnt_w << endl;
}


Measured memory_bytes: 3620
n,m=map(int,input().split())
from collections import defaultdict
d=defaultdict(int)

for i in range(m):
    a,b=map(int,input().split())
    a,b=a-1,b-1
    d[a]+=1
    d[b]+=1

for i in range(n):
    if d[i]%2==1:
        print('NO')
        exit()
print('YES')

Measured memory_bytes: 89880
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

const int INF = 0xfffffff;

struct edge {
	int to, cost;
	edge(int t, int c):to(t), cost(c) {}
};
typedef pair<int, int> P;

vector<vector<edge> > es;
vector<int> d;

void dijkstra(int s) {
	priority_queue<P, vector<P>, greater<P> > que;
	d[s] = 0;
	que.push(P(0, s));
	
	while(!que.empty()) {
		P p = que.top();
		que.pop();
		int v = p.second;

		if(d[v] < p.first)
			continue;

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


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

	for(int n, m; cin >> n >> m, n | m;) {
		d.clear();
		d.resize(n, INF);
		es.clear();
		es.resize(n);

		map<string, int> convert;
		for(int i = 0; i < 3; ++i) {
			string input;
			cin >> input;
			convert.insert(make_pair(input, i));
		}

		int id = 3;
		for(int i = 0; i < m; ++i) {
			string a, b;
			int d, t;
			cin >> a >> b >> d >> t;

			if(!convert.count(a)) convert.insert(make_pair(a, id++));
			if(!convert.count(b)) convert.insert(make_pair(b, id++));

			int id_a = convert[a];
			int id_b = convert[b];
			int cost = d / 40 + t;

			es[id_a].push_back(edge(id_b, cost));
			es[id_b].push_back(edge(id_a, cost));
		}

		dijkstra(1);
		cout << d[0] + d[2] << endl;
	}


	return EXIT_SUCCESS;
}

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

using namespace std;

long long memo[100];

long long lucas(int n)
{
    if (memo[n] != 0)
    {
        return memo[n];
    }
    if (n == 0)
    {
        return 2;
    }
    if (n == 1)
    {
        return 1;
    }
    long long val = lucas(n - 1) + lucas(n - 2);
    memo[n] = val;
    return val;
}

int main()
{
    int N;

    cin >> N;

    cout << lucas(N) << endl;

    return 0;
}

Measured memory_bytes: 256
N = int(input())
A = [0] + list(map(int, input().split()))

num = {}


# def xC2
def calc(x):
    return x*(x-1)//2


for a in A[1:]:
    if a not in num:
        num[a] = 0
    num[a] += 1

pattern = 0

for n in num:
    pattern += calc(num[n])

for i in range(1, N+1):
    n = num[A[i]]
    ans = pattern - calc(n) + calc(n-1)
    print(ans)



Measured memory_bytes: 32380
//56
#include<iostream>
#include<set>
#include<algorithm>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstdlib>

using namespace std;

set<int> cs(vector<set<int> > n,vector<char> o){
  if(o.empty()){
    return n[0];
  }else{
    set<int> st;
    for(int i=0;i<o.size();i++){
      set<int> l=cs(vector<set<int> > (n.begin(),n.begin()+i+1),vector<char> (o.begin(),o.begin()+i));
      set<int> r=cs(vector<set<int> > (n.begin()+i+1,n.end()),vector<char> (o.begin()+i+1,o.end()));
      for(set<int>::iterator k=l.begin();k!=l.end();k++){
	for(set<int>::iterator j=r.begin();j!=r.end();j++){
	  if(o[i]=='+'){
	    st.insert(*k+*j);
	  }else if(o[i]=='-'){
	    st.insert(*k-*j);
	  }else if(o[i]=='*'){
	    st.insert(*k**j);
	  }else if(*j){
	    int r=abs(*k)/abs(*j);
	    st.insert(((*k<0)^(*j<0))?-r:r);
	  }
	}
      }
    }
    return st;
  }
}

set<int> exp(char *s){
  vector<set<int> > n;
  vector<char> o;
  for(int i=0;s[i];){
    if(isdigit(s[i])){
      int d,r;
      sscanf(s+i,"%d%n",&d,&r);
      i+=r;
      set<int> ss;
      ss.insert(d);
      n.push_back(ss);
    }else if(s[i]!='('){
      o.push_back(s[i]);
      i++;
    }else{
      int bc=1;
      int j;
      for(j=i+1;bc;j++){
	bc+=(s[j]=='(')-(s[j]==')');
      }
      char ns[201]={};
      copy(s+i+1,s+j-1,ns);
      n.push_back(exp(ns));
      i=j;
    }
  }
  return cs(n,o);
}
 
int main(){
  char s[201];
  while(cin>>s,s[0]!='#'){
    cout<<exp(s).size()<<endl;
  }
  return 0;
}

Measured memory_bytes: 1336
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define rep2(i,x,n) for(int i=x; i<(n); i++)
#define all(n) begin(n),end(n)
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
const long long inf = numeric_limits<long long>::max();
int main()
{
  int h, w;
  cin >> h >> w;
  vector<bool> row(h, false);
  vector<bool> col(w, false);
  vector<string> grid(h);
  rep(i, h) {
    cin >> grid[i];
  }
  rep(i, h) {
    rep(j, w) {
      if(grid[i][j] == '#') {
        row[i] = true;
        col[j] = true;
      }
    }
  }
  rep(i, h) {
    bool render = false;
    if (row[i]) {
      rep(j, w) {
        if (col[j]) {
          render = true;
          cout << grid[i][j];
        }
      }
    }
    if (render) cout << endl;
  } 
  return 0;
}

Measured memory_bytes: 256
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

a,b,c,d = map(int,input().split())
C = b//c-(a-1)//c
D = b//d - (a-1)//d
def gcd(a, b):
    while(b != 0):
        a, b = b, a % b
    return a
    
def lcm(m,n):
    return (m*n)//gcd(m,n)
cd = lcm(c,d)
CD = b//cd - (a-1)//cd
print(b-a+1-C-D+CD)
# print(C,D,CD)

Measured memory_bytes: 38256
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;


public class Main{
	static Node[][] node;  //ノード
	static PriorityQueue<Node> open_queue;
	static Node currentNode;  //計算ノード
	static int ch, cw;
	static int h, w;  //高さ、幅
	static int[][] rWall;
	static int[][] dWall;

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		while(true){
			int count=0;

			w=sc.nextInt();
			h=sc.nextInt();

			if(w==0 && h==0){
				break;
			}

			rWall= new int[h][w];
			dWall= new int[h][w];

			for(int k=0;k<2*h-1;k++){
				if(k%2==0){
					for(int i=0;i<w-1;i++){
						rWall[count][i]=sc.nextInt();
					}
				}
				else{
					for(int i=0;i<w;i++){
						dWall[count][i]=sc.nextInt();
					}
					count++;
				}
			}

			node=new Node[h][w];  //ノード生成
			for(int i=0;i<h;i++){
				for(int j=0;j<w;j++){
					node[i][j]=new Node();
					node[i][j].Nh=i;
					node[i][j].Nw=j;
				}
			}

			MazeCalc();
		}
	}

	static void MazeCalc(){
		node[0][0].Start=true;
		node[h-1][w-1].Goal=true;
		node[0][0].f=0;
		open_queue = new PriorityQueue<Node>(1, new MyComparator());
		open_queue.add(node[0][0]);
		while(true){
			if(open_queue.size()==0){
				System.out.println(0);
				break;
			}
			currentNode=open_queue.poll();
			if(currentNode.Goal==true){
				System.out.println(currentNode.f+1);
				break;
			}
			Calc();
		}
	}
	static void Calc(){
		ch=currentNode.Nh;
		cw=currentNode.Nw;
		if(ch>0){
			if(dWall[ch-1][cw]==0 && node[ch-1][cw].f>currentNode.f+1){
				node[ch-1][cw].f=currentNode.f+1;
				open_queue.add(node[ch-1][cw]);
			}
		}
		if(ch<h-1){
			if(dWall[ch][cw]==0 && node[ch+1][cw].f>currentNode.f+1){
				node[ch+1][cw].f=currentNode.f+1;
				open_queue.add(node[ch+1][cw]);
			}

		}
		if(cw>0){
			if(rWall[ch][cw-1]==0 && node[ch][cw-1].f>currentNode.f+1){
				node[ch][cw-1].f=currentNode.f+1;
				open_queue.add(node[ch][cw-1]);
			}
		}
		if(cw<w-1){
			if(rWall[ch][cw]==0 && node[ch][cw+1].f>currentNode.f+1){
				node[ch][cw+1].f=currentNode.f+1;
				open_queue.add(node[ch][cw+1]);
			}
		}
	}

}

class Node{
	int cost; //直進、右折、反転、左折
	int f; //コスト
	int Nh, Nw;  //場所と向き
	boolean Start;
	boolean Goal;

	public Node(){
		cost=Integer.MAX_VALUE;
		f=Integer.MAX_VALUE;
		Nh=0;
		Nw=0;
		Start=false;
		Goal=false;
	}
}

class MyComparator implements Comparator<Object>{
    public int compare(Object obj1, Object obj2) {

        Node num1 = (Node)obj1;
        Node num2 = (Node)obj2;

        if(num1.f>num2.f) {
            return 1;
        } else if(num1.f<num2.f) {
            return -1;
        } else{
            return 0;
        }
    }
}

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

int main()
{
	int a, b, c;
	cin >> a >> b >> c;
	if( a==b && a!=c )
	{
		cout << "Yes\n";
	}
	else if( a==c && a!=b )
	{
		cout << "Yes\n";
	}
	else if( b==c && b!=a )
	{
		cout << "Yes\n";
	}
	else
	{
		cout << "No\n";
	}
}


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

vector<int> g[2020];

int dfs(int curr, int prev, int d) {
	if (d < 0) return 0;
	int res = 1;
	for (int next : g[curr]) if (next != prev) {
		res += dfs(next, curr, d - 1);
	}
	return res;
}

int main() {
	int n, K;
	cin >> n >> K;

	for (int i = 0; i < n - 1; i++) {
		int u, v;
		scanf("%d %d", &u, &v);
		g[u].push_back(v);
		g[v].push_back(u);
	}

	int ans = INT_MAX;
	if (K % 2 == 0) {
		for (int i = 0; i < n; i++) {
			ans = min(ans, n - dfs(i, -1, K / 2));
		}
	} else {
		for (int i = 0; i < n; i++) {
			for (int j : g[i]) {
				ans = min(ans, n - dfs(i, j, (K - 1) / 2) - dfs(j, i, (K - 1) / 2));
			}
		}
	}
	cout << ans << endl;
}

Measured memory_bytes: 384
n = int(input())
hs = input().split()

h = []
for s in hs:
    h.append(int(s))

max = 0
for i in range(0,n):
    if max < h[i]:
        max = h[i]
    if max - h[i] > 1:
        print("No")
        exit()

print("Yes")


Measured memory_bytes: 14224
#include <cstdio>
#include <list>
using namespace std;

int n;
int m;

int main() {
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0) return 0;

    list<int> l;
    for (int i=1; i<=n; i++) {
      l.push_back(i);
    }

    list<int>::iterator itr = l.begin();
    while (l.size() > 1) {
      for (int i=0; i<m-1; i++) {
        ++itr;
        if (itr == l.end()) itr = l.begin();
      }

      itr = l.erase(itr);
      if (itr == l.end()) itr = l.begin();
    }

    printf("%d\n", l.front());
  }
}

Measured memory_bytes: 2712
#include<bits/stdc++.h>
#define fo(i,a,b) for(int i=(a);i<(b);i++)
#define qi queue<int>
#define vi vector<int>
#define ll long long
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pii pair<int,int>
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define fi first
#define se second 
#define prq priority_queue
#define foreach(it,c) for(__typeof((c).begin()) it=c.begin();it!=c.end();it++)
//#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
using namespace std;
inline ll read(){
	ll x=0;
	int f=1;
	char c=getchar();
	for(;!isdigit(c);c=getchar()) if(c=='-')f=-f;
	for(;isdigit(c);c=getchar())x=x*10+(c-'0');
	return x*f;
}
inline void out(int x){
	if(x>=10)out(x/10);
	putchar(x%10+'0');
}
int main(){
	//freopen("a.in","r",stdin);
	//freopen("a.out","w",stdout);
	string s,t;
	cin>>s>>t;
	fo(i,0,s.size()) if(s[i]!=t[i]){
		cout<<"No\n";
		return 0;
	}
	cout<<"Yes\n";
	return 0;
}


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

int main(){
  ll n, k;
  cin >> n >> k;
  vi a(n+1);
  for(int i=1; i<=n; i++) cin >> a[i];
  vector<bool> ok(n+1,false);
  vi remember(n+1);
  remember[1] = 0;
  int here = 1; ok[here] = true;
  for(ll i=1; i<=k; i++){
    here = a[here];
    if(ok[here]){
      ll nokori = (k-i)%(i-remember[here]);
      i = k-nokori;
    }
    remember[here] = i;
    ok[here] = true;
  }
  cout << here << endl;
}


Measured memory_bytes: 4844
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
ll const INF = 1LL << 60;

int main() {
  ll N;
  string S;
  cin >> N >> S;
  ll cnt = 0;
  string ans = "";
  for (int i = 0; i < S.size(); i++) {
    if (S[i] == ')' && cnt == 0) {
      ans = '(' + ans + ')';
    } else if (S[i] == ')' && cnt > 0) {
      cnt--;
      ans += ')';
    } else {
      cnt++;
      ans += '(';
    }
  }
  for (int i = 0; i < cnt; i++) {
    ans += ')';
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int A,B,C,X;
  cin >> A >> B >> C >> X;
  int res = 0;
  for (int a = 0; a <= A; ++a) {
    for (int b = 0; b <= B; ++b) {
      for (int c = 0; c <= C; ++c) {
        if (a*500 + b*100 + c*50 == X) ++res; 
      }
    }
  }
  std::cout << res << endl;
}


Measured memory_bytes: 256
#!ruby -pal00F\n
def f a,i,j;a[i][j]=z=?0;[[1,0],[0,1],[-1,0],[0,-1]].map{|c,d|(a[i+c][j+d]||z)>z&&f(a,i+c,j+d)}end
a=[[z=?0]*13]+$F.map{|l|[z]+l.chars}+[[]]
$_=0
x=(1..12)
x.map{|i|x.map{|j|a[i][j]>z&&($_+=1;f a,i,j)}}

Measured memory_bytes: 6320