akhauriyash/Code-Regression huggingface dataset:
a = int(input())
b = int(input())
c = int(input())
x = int(input())

count = 0
for ia in range(0, a + 1):
    for ib in range(0, b + 1):
        for ic in range(0, c + 1):
            if 500 * ia + 100 * ib + 50 * ic == x:
                count += 1

print(count)


Measured memory_bytes: 9184
#!/usr/bin/python

import sys

H, W = [int(w) for w in sys.stdin.readline().split()]
N = int(sys.stdin.readline())
a = [int(w) for w in sys.stdin.readline().split()]

color = 0
for y in range(H):
  row = []
  for x in range(W):
    while a[color] <= 0: color += 1
    row.append(color + 1)
    a[color] -= 1
  if y % 2 != 0: row = reversed(row)
  s = ' '.join([str(x) for x in row])
  print(s)


Measured memory_bytes: 2820
N = gets.to_i
S = gets.chomp.chars.map(&:to_i)

puts 1000.times.count {|x|
  s = [x/100, x/10%10, x%10]
  !S.each do |c|
    if s[0] == c
      s.shift
      break if s.empty?
    end
  end
}

Measured memory_bytes: 15888
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <math.h>
#include <map>
#define MOD 1000000007
typedef long long ll;
using namespace std;
ll a[100001];
ll n;

class BIT{
public:

  vector<ll> bit;
  BIT(){}
  BIT(int size){
    bit.resize(size,0);
  }

  ll sum(int i){
    //if(i==0) return 0;
    ll s=0;
    while(i>0){
      s+=bit[i];
      i-=i&(-i);
    }
    return s;
  }

  void add(int i,int x){
    while(i<bit.size()){
      bit[i]+=x;
      i+=i&(-i);
    }
  }
};

bool check(ll m){
  int s[n+1]={};
  BIT bit(2*n+1);

  for(int i=1;i<=n;i++){
    if(a[i]>=m) s[i]=1;
    else s[i]=-1;
  }
  s[0]=0;
  for(int i=1;i<=n;i++) s[i]+=s[i-1];
  ll ans=0;
  for(int i=0;i<=n;i++){
    ans+=bit.sum(s[i]+n);
    if(i==n) break;
    bit.add(s[i]+n,1);
  }

  if(ans<n*(n+1)/4) return true;
  else return false;
}

int main(){
  //ll n;
  cin>>n;
  //ll a[n+1]={};
  ll ma=0;
  for(int i=1;i<=n;i++){
    cin>>a[i];
    ma=max(ma,a[i]);
  }

  ll l=0,r=ma+1;
  while(r-l>1){
    int m=(r+l)/2;
    if(check(m)) r=m;
    else l=m;
    //cout<<l<<" "<<r<<endl;
  }
  cout<<l<<endl;
  return 0;
}


Measured memory_bytes: 2944
#include<stdio.h>
int main(){
  int n,i,j,k,l,ny,nx;
  char s,e,f;
  double p=1;
  int X[]={0,1,0,-1};
  int Y[]={1,0,-1,0};
  while(scanf("%d",&n),n){
    int c[3][3]={0};
    scanf(" %c %c %c",&s,&e,&f);
    c[(s-'A')/3][(s-'A')%3]=p=1;
    while(n--){
      p*=4;
      int d[3][3]={0};
      for(i=0;i<3;i++){
	for(j=0;j<3;j++){
	  for(k=0;k<4;k++){
	    ny=i+Y[k];
	    nx=j+X[k];
	    if((f-'A')/3==ny&&(f-'A')%3==nx||nx<0||nx==3||ny<0||ny==3){
	     nx-=X[k];
	     ny-=Y[k];
	    }
	    d[ny][nx]+=c[i][j];
	  }
	}
     }
     for(i=0;i<9;i++)c[i/3][i%3]=d[i/3][i%3];
     //for(i=0;i<9;i++)printf("%d ",d[i/3][i%3]);printf("\n");
    }
    printf("%f\n",c[(e-'A')/3][(e-'A')%3]/p);
  }
  return 0;
}

Measured memory_bytes: 636
#include <iostream>
#include <string>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;

int main() {
	int n, k,ans;
	ans = 0;
	cin >> n >> k;
	vector<int> h(n);
	for (int i = 0; i < n; ++i) {
		cin >> h[i];
		if (h[i] >= k) ans++;
	}
	cout << ans << endl;

}

Measured memory_bytes: 640
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define FIO             ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

int dp[401][401];
mk(arr, 401, int);
mk(pre, 401, int);

int min_cost(int i, int j)
{
	if (i >= j)
		return dp[i][j] = 0;

	if (dp[i][j] != 0)
		return dp[i][j];

	int cur_cost = pre[j] - pre[i - 1];
	int ans = inf;
	for (int k = i + 1; k <= j; k++)
		ans = min(ans, min_cost(i, k - 1) + min_cost(k, j));

	return dp[i][j] = ans + cur_cost;
}

int32_t main()
{
	FIO;

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

	cout << min_cost(1, n) << endl;

	return 0;
}

Measured memory_bytes: 4956
#include <bits/stdc++.h>
using namespace std;
#define LL long long int
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    // code:
    
    int n,ans=1;
    cin>>n;
    if(n>=400 && n<600){
        ans=8;
    }
    if(n>=600 && n<800){
        ans=7;
    }
    if(n>=800 && n<1000){
        ans=6;
    }
    if(n>=1000 && n<1200){
        ans=5;
    }
    if(n>=1200 && n<1400){
        ans=4;
    }
    if(n>=1400 && n<1600){
        ans=3;
    }
    if(n>=1600 && n<1800){
        ans=2;
    }
    
    cout << ans << "\n";
    
    
    return 0;
}

Measured memory_bytes: 3648
a=[]
while True:
    try:
        a.append(input())
    except:
        break
n=len(a)
m=[0 for i in range(101)]
l=0
for j in range(n):
    k=int(a[j])
    m[k]+=1
    l=max(l,m[k])
    
for i in range(101):
    if m[i]==l:
        print(i)


Measured memory_bytes: 5600
n=int(input())
mod=10**9+7

import math
s=math.factorial(n)
i=2
ans=1
cnt=0
while s!=1:
    if s%i==0:
        s//=i
        cnt+=1
    else:
        ans*=(cnt+1)
        cnt=0
        i+=1

print(ans*(cnt+1)%mod)

Measured memory_bytes: 40848
#include <stdio.h>
#include <math.h>
//f(a,b)a,bで桁の大きい方
int min(int a,int b){
    if(a<b)return a;
    else return b;
}
int digit_min(long long int a,long long int b){
    int cnt=0;
    if(a<b){
        while(b>0){
            b/=10;
            cnt++;
        }}else{
            while(a>0){
                cnt++;
                a/=10;
            }
        }
    return cnt;
}
int main(void){
    long long int n;
    scanf("%lld",&n);
    long long int i,j;
    int ans=100,temp;
    for(i=1;i<=sqrt(n);i++){
        if(n%i==0){
            j=n/i;
            temp=digit_min(i,j);
            if(temp<ans)ans=temp;
        }
    }
    printf("%d\n",ans);
    return 0;
}


Measured memory_bytes: 128
#define __USE_MINGW_ANSI_STDIO 0
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <set>
#include <numeric>
#include <bitset>
#include <map>
#include <limits.h>
using namespace std;
typedef unsigned long ul;
typedef pair<ul, ul> P;
const int INF = 1e9;

long long gcd(long long x, long long y){
			if(x<y){
				long long temp = x;
			x = y;
				y = temp;
			}
			long long r = x % y;
				while(r!=0){
					x = y;
					y = r;
				r = x % y;
	    		}
			return y;
	    }

int main(){
int N;
string S;
cin >> N;
cin >> S;
vector<int> cnt(N+1,0);
for(int i = 0; i<N; i++){
    cnt[i+1] = cnt[i];
    if(S[i] == '#')cnt[i+1]++; 
}

int ans = INF;
for(int i = 0;i<N+1;i++){
int ans_tmp = cnt[i] + N-(cnt[N] - cnt[i])-i;
ans = min(ans,ans_tmp);

}
cout << ans << endl;
}



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

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;

const int mx=1e5+5;

int ara[3][mx];
int dp[3][mx];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	
	int n;
	cin>>n;
	
	for(int j=1;j<n+1;j++) for(int i=0;i<3;i++) cin>>ara[i][j];
	
	for(int i=0;i<3;i++) dp[i][1]=ara[i][1];
	
	for(int i=2;i<=n;i++)
	{
		dp[0][i]=max(dp[1][i-1]+ara[0][i],dp[2][i-1]+ara[0][i]);
		dp[1][i]=max(dp[0][i-1]+ara[1][i],dp[2][i-1]+ara[1][i]);
		dp[2][i]=max(dp[0][i-1]+ara[2][i],dp[1][i-1]+ara[2][i]);
	}
	
	int ans=-1;
	
	for(int i=0;i<3;i++) ans=max(ans,dp[i][n]);
	
	cout<<ans;
							
	cerr << "\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms\n";
	
	return 0;
}

Measured memory_bytes: 2560
N = int(input())
H = list(map(int,input().split()))
dp = [10**9 for i in range(N+1)]
dp[1] = 0
for i in range(2,N+1):
    x = H[i-2] - H[i-1]
    y = 100000
    if i >= 3:
        y = H[i-3] - H[i-1]
    if x < 0:
        x = -x
    if y < 0:
        y = -y
    x = min(dp[i-1]+x,dp[i-2]+y)
    dp[i] = x
print(dp[N])

Measured memory_bytes: 13980
#include <iostream>
using namespace std;
int main(void){
    // Your code here!
    int a, b, c;
    
    cin >> a >> b >> c;
    
    int ans = c-(a - b);
    
    if(ans<0){
        
        cout<<"0"<<endl;
        
    }
    else{
        
        cout<<ans<<endl;
        
    }

    
}

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

using namespace std;

const int inf = 0x3f3f3f3f;

struct Node {
	int d, x;
	Node(int _x = 0, int _d = 0):d(_d), x(_x) {}
};

int n, m, X[5], u, v;
int S[5][60][60];
int dis[60];
vector <int> G[60];
queue <Node> Q;
bool vis[60][200];

void BFS(int u) {
	memset(vis, 0, sizeof(vis));
	while(!Q.empty()) Q.pop();
	Q.push(Node(u, 0)); vis[u][0] = 1;
	while(!Q.empty()) {
		int x = Q.front().x, d = Q.front().d; Q.pop();
		for(int i = 0; i < 3; i ++) if(d == X[i]) {
			S[i][x][u] = 1;
		}
		if(d == X[2]) continue;
		for(unsigned i = 0; i < G[x].size(); i ++) {
			int y = G[x][i];
			if(!vis[y][d+1]) {
				Q.push(Node(y, d+1));
				vis[y][d+1] = 1;
			}
		}
	}
}

int main() {
	//freopen("C.in", "r", stdin);
	scanf("%d%d%d%d%d", &n, &m, &X[0], &X[1], &X[2]);
	sort(X, X+3);
	for(int i = 0; i < m; i ++) {
		scanf("%d%d", &u, &v);
		G[v].push_back(u);
	}
	memset(S, 0, sizeof(S));
	for(int i = 1; i <= n; i ++) BFS(i);
	
	memset(dis, 0x3f, sizeof(dis));
	dis[n] = 0;
	for(int p = 1; p <= 2*n; p ++) {
		for(int i = 1; i <= n; i ++) {
			int Mx = 0;
			for(int k = 0; k < 3; k ++) {
				int Mn = inf;
				for(int j = 1; j <= n; j ++) if(S[k][i][j]) {
					Mn = min(Mn, dis[j]);
				}
				Mx = max(Mx, Mn);
			}
			dis[i] = min(dis[i], Mx+1);
		}
	}
	
	if(dis[1] == inf) printf("IMPOSSIBLE\n");
	else printf("%d\n", dis[1]);
	return 0;
}

Measured memory_bytes: 1316
import java.util.*;

public class Main{
    public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
	
	int[] num = new int[5];
	for(int i=0; i<num.length; i++){
	    num[i] = sc.nextInt();
	}
	
	Arrays.sort(num);
	
	for(int i=num.length-1; i>0; i--){
	    System.out.print(num[i]+" ");
	}
	System.out.println(num[0]);
    }
}

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

int main(int argc,char* argv[]){
  long long L,R;
  bool flag = false;
  int  min=2019;
  cin >> L >> R;
  if(R-L>=2019) flag = true;  
  L%=2019;
  R%=2019;
  if(L>=R||flag) cout << "0" << endl;
  else{
    for(int i=L;i<R;i++){
      for(int j=i+1;j<=R;j++){
	if(i*j%2019<min) min = i*j%2019;
      }
    }
    cout << min << endl;
  }

  return 0;
}


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

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

    vector<vector<int>> a_matrix(n,vector<int>(m,0));
    vector<int> b_vector;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            int input;
            cin >> input;
            a_matrix[i][j] = input;
        }
    }
   for(int i=0;i<m;i++){
        int input;
        cin >> input;
        b_vector.push_back(input);
    }

    vector<int> c_vector;
    for(int i=0;i<n;i++){
        int sum=0;
        for(int j=0;j<m;j++){
            sum += a_matrix[i][j] * b_vector[j];
        }
        c_vector.push_back(sum);
        sum=0;
    }
    for(int i=0;i<n;i++){
        cout << c_vector[i] << endl;
    }
}


Measured memory_bytes: 3204
n = int(input())
A = list(map(int,input().split()))
a = 0
for i in range(n):
    if A[i] % 2 == 0:
        if A[i] % 3 != 0 and A[i] % 5 !=0:
            a = 1
if a == 0:
    print("APPROVED")
else:
    print("DENIED")
            
            

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

int main(){
  int N;
  cin>>N;
  vector<int> sakana(N);
  priority_queue<int> toriniku;
  for(int i=0; i<N; i++){
    int A;
    cin>>A;
    sakana.at(i)=A;
    toriniku.push(A);
  }
  int max=toriniku.top();
  toriniku.pop();
  int second=toriniku.top();
  for(int i=0; i<N; i++){
    if(sakana.at(i)!=max) cout<<max<<endl;
    else cout<<second<<endl;
  }
}


Measured memory_bytes: 3316
#include <bits/stdc++.h>
#define maxn 500010
using namespace std;
char s[maxn];
int a[maxn], dp[maxn];
int main() {
    int n;
    scanf("%d%s", &n, s+1);
    for (int i = 1; i <= n; ++i) {
        if (s[i] == '0') a[i] = i;
        else a[i] = a[i-1];
    }
    for (int i = 1; i <= n; ++i) {
        dp[i] = dp[i-1];
        if (s[i] == '1') {
            if (s[i-1] == '0' && s[i-2] == '1') {
                dp[i] = max(dp[i], dp[a[i-2]]+ (i-2) - a[i-2]);
                dp[i] = max(dp[i], dp[a[i-2]+1] + (i-2) - (a[i-2]+1));
            }
            else if (a[i]>1 && s[a[i]-1] == '1') dp[i] = max(dp[i], dp[a[i]-2] + i - a[i]);
        }
    }
    printf("%d\n", dp[n]);
    return 0;
}


Measured memory_bytes: 4608
N = int(input())
h = list(map(int, input().split()))
a, b = 0, abs(h[0] - h[1])
for n in range(N - 2):
    a, b = b, min(a + abs(h[n] - h[n + 2]), b + abs(h[n + 1] - h[n + 2]))
print(b)


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

#define MOD 1000000007
typedef long int ll;

#define rep(i, n) for(i=0;i<(int)(n);i++)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define bit(n) (1LL<<(n))
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return (a*b)/gcd(a,b);}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int main(void){
  ll a, b, c;
  ll tmp, tmp2;
  
  scanf("%ld %ld %ld", &a,&b,&c);
  c -= a+b;
  if(c < 0) c=0;
  
  tmp = 4*a*b;
  tmp2 = c*c;
  
  if(tmp < tmp2) printf("Yes");
  else printf("No");
  
  return 0;
}



Measured memory_bytes: 256
def main():
    L, R = map(int, input().split())
    # 60 の相異なる素因数は 3, 673 の 2つ
    if L + 672 <= R:
        print(0)
        return
    lst = []
    for i in range(L, R + 1):
        for j in range(i + 1, R + 1):
            if i * j % 2019 == 0:
                print(0)
                return
            lst.append(i * j % 2019)
    print(min(lst))
    return


main()


Measured memory_bytes: 6808
"use strict";
    
const main = arg => {
    arg = arg.trim().split("\n");
    const N = parseInt(arg[0].split(" ")[0]);
    const K = parseInt(arg[0].split(" ")[1]);
    
    const V = arg[1].split(" ").map(n=>parseInt(n));
    
    let answerList = [0];
    
    for(let a=0; a<=Math.min(K, N); a++) {
        for(let b=0; b<=Math.min(K, N); b++) {
            const rest = K - (a + b);
            
            if(N < a + b) continue;
            if(rest < 0) continue;
            
            let list = [];
            
            list = [...V.slice(0, a), ...V.slice(V.length - b, V.length)];

            list.sort((a,b)=>a-b);
            
            let deleteCnt = 0;
            
            for(let i=0; i<rest; i++) {
                if(list[i] < 0) {
                    deleteCnt++;
                } else {
                    break;
                }
            }
            
            list.splice(0, deleteCnt);
            
            if(list.length) {
                answerList.push(list.reduce((m,n)=>m+n));
            }
        }
    }
    
    const answer = Math.max(...answerList);
    
    console.log(answer);
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));

Measured memory_bytes: 10472
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
 
using namespace std;
using namespace __gnu_pbds;
 
#define debug(s) cerr << #s << " = " << s << '\n'
#define all(v) (v).begin(), (v).end()
#define mem(a,val) memset(a, val, sizeof a)
 
#define maxn 100010
#define INF 1e15
#define eps 1e-9
#define pi acos(-1.0)
#define ff first
#define ss second
#define endl '\n'
typedef long long ll;
 
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int a, b;
  cin >> a >> b;
  if (b >= a) cout << "unsafe" << endl;
  else cout << "safe" << endl;
}

Measured memory_bytes: 3648
#include<bits/stdc++.h>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#define endl  "\n"
#define ll long long
#define int long long
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cin.exceptions(cin.failbit);cout.tie(NULL);
using namespace std;
bool isPrime(int n)
{
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
    if (n % 2 == 0 || n % 3 == 0)
        return false;
    for (int i = 5; i * i <= n; i += 6)
    {
        if ((n % i == 0) || (n % (i + 2) == 0))
            return false;
    }
    return true;
}

int32_t main()
{
	int n;
	cin >> n;
	map<string,int> mp;
	for(int i=0;i<n;i++)
	{
		string s;
		cin >> s;
		mp[s]++;
	}
	
	cout << "AC x " << mp["AC"] << endl;
	cout << "WA x " << mp["WA"] << endl;
	cout << "TLE x " << mp["TLE"] << endl;
	cout << "RE x " << mp["RE"] << endl;
	 
}



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

int main(){
    int m, h;
    char cards[256], temp[256], result[256];
    int i, j=0, len;

    while(1){
        scanf("%s", cards);
        len=strlen(cards);
        if(strcmp(cards, "-")==0&&len==1)break;
        scanf("%d", &m);
        for(;m>0;m--){
            scanf("%d", &h);
            strcpy(temp, cards);
            for(i=0;i<h;i++){
                cards[len-h+i]=temp[i];
            }
            for(i=0;i<len-h;i++){
                cards[i]=temp[i+h];
            }
        }
        for(i=0;i<len;i++){
            result[j++]=cards[i];
        }
        result[j++]='\n';
    }
    result[j]='\0';
    printf("%s", result);

    return 0;
}

Measured memory_bytes: 2656
#include<iostream>
#include<vector>
#include<algorithm>
int n;
std::vector<int> a;
int main(){
	std::cin>>n;
	for(int i=0;i<n;++i){
		int x;
		std::cin>>x;
		a.push_back(x);
	}
	std::reverse(a.begin(),a.end());
	for(auto v:a){
		std::cout<<v;
		if(v!=*(a.end()-1))std::cout<<" ";
	}
	std::cout<<std::endl;
	return 0;
}

Measured memory_bytes: 1216
#include <stdio.h>
#include <string.h>
int main(){
  char s[11];int i,a=0,l=0;
  scanf("%s",s);
  for(i=0;i<strlen(s);i++){
    if(s[i]=='A'||s[i]=='T'||s[i]=='G'||s[i]=='C'){l++;a=(a<l?l:a);}
    else l=0;
  }
  printf("%d",a);
  return 0;
}

Measured memory_bytes: 1724
#include<stdio.h>
int main(){
	int n,ci,sum;
	int d[51];
	sum=0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&d[i]);
	}
	for(int i=1;i<n;i++){
		for(int j=i+1;j<=n;j++){
			sum=sum+d[i]*d[j];
		}
	}
	printf("%d",sum);
	return 0;
}

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

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

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()

const int MN = 100010;
int N;
string s;

bool ok(int k)
{
	set<int> st;
	int l = N - k, r = k;

	for (int i = l; i < r; ++i) {
		st.insert(s[i]);
	}

	return (int)st.size() <= 1;
}

int main() {
	cin >> s;
	N = s.size();

	int lo = 1, hi = N;

	if (ok(N)) {
		lo = N;
	}

	while (hi - lo > 1) {
		int m = (lo + hi) / 2;
		if (ok(m)) {
			lo = m;
		} else {
			hi = m;
		}
	}

	cout << lo << endl;

	return 0;
}

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

using namespace std;

int main()
{
    int a[3];
    cin  >> a[0] >> a[1] >> a[2];
    //cout << a[0] << " " << a[1] << " " << a[2] << endl;

    for(int i = 1; i < 3; i++)
    {
        int tmp = a[i];
        int j = i-1;
        while(j>=0 && tmp < a[j])
        {
            a[j+1] = a[j];
            j--;
        }
        a[j+1] = tmp;
    }

    cout << a[0] << " " << a[1] << " " << a[2] << endl;
    return 0;
}


Measured memory_bytes: 3092
#include<bits/stdc++.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;

int A,B,a[5000],b[5000];
int dp[5001];

int main(){
	scanf("%d%d",&A,&B);
	for(int i=0;i<A;i++) scanf("%d",&a[i]);
	for(int i=0;i<B;i++) scanf("%d",&b[i]);
	int res=0;
	for(int i=1;i<=A;i++) for(int j=B;j>=1;j--){
		if(a[i-1]==b[j-1]) dp[j]=max(dp[j-1]+1,dp[j]);
		res=max(res,dp[j]);
	}
	printf("%d\n",res);
}

Measured memory_bytes: 1248
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i<(int)(n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using Graph = vector<vi>;  // vi <=> vl
using vb = vector<bool>;
using boolGraph = vector<vb>;
using P = pair<int, int>;  // int <=> ll
const int MOD = 1e9+7;
const double EPS = 1e-9;

int main() {
  int N; cin >> N;
  Graph red(N, vi(2)), blue(N, vi(2));
  rep(i,N) rep(j,2) cin >> red[i][j];
  rep(i,N) rep(j,2) cin >> blue[i][j];

  sort(all(red));
  sort(all(blue));

  set<int> s;
  int r = 0, b = 0;
  int ans = 0;

  rep(i,2*N) {
    if (r != N && red[r][0] == i) {
      s.insert(red[r][1]);
      ++r;
    }
    else {
      if (!s.empty() && *begin(s) < blue[b][1]) {
        auto iter = s.lower_bound(blue[b][1]);
        --iter;
        s.erase(*iter);
        ++ans;
      }
      ++b;
    }
  }
  
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+9;
ll arr[maxn][2];
int main(){
    ll tol=0,n=0,k;
    ios::sync_with_stdio(0);
    while(cin>>n>>k){
        ll ma=1;
        while(ma<=k)ma<<=1;
        for(int i=0;i<n;i++){
            cin>>arr[i][0]>>arr[i][1];
        }
        ll ans=0;
        ll tol=0;
        ll tmp=(ma>>1)-1;
        if(k==ma/2ll){
            tmp=k;
        }
        for(int i=0;i<n;i++){
            if((arr[i][0]|k)<=k){
                tol+=arr[i][1];
            }
        }
        //cout<<"TOL"<<tol<<endl;
        ans=max(ans,tol);
        for(ll i=1;i<=k;i<<=1){
            if((k&(i))==0)continue;
            tol=0;
            for(int j=0;j<n;j++){
                if(arr[j][0]>=ma||(arr[j][0]&i)>0||(((arr[j][0]|k))>(k|(i-1)))){
                    continue;
                }
                tol+=arr[j][1];
            }
            //cout<<i<<':'<<tol<<endl;
            ans=max(ans,tol);
        }
        cout<<ans<<endl;
    }
}


Measured memory_bytes: 1920
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  while(cin>>n,n){
    int a[n],s=32,p=0;
    for(int i=0;i<n;i++)cin>>a[i];
    while(s){
      s-=(s-1)%5;
      cout<<s<<endl;
      s-=a[p++%n];
      if(s<0)s=0;
      cout<<s<<endl;
    }
  }
}

Measured memory_bytes: 2984
# 演習1-15
#時間がオーバーしたのでmathパッケージに頼り、素数の性質を利用
#素数判定機
import math as math

def prime_checker(limit):
    n = 0
    prime_index = []
    for k in range(2, limit+1):
        factor = 0
        
        # 2以外の偶数は素数ではないので無視する
        if k % 2 == 0 and k != 2:
            continue
        
        # 繰り返しの上限を対象の平方根にする
        for divisor in range(2, math.floor(math.sqrt(k))+1):
            if k % divisor == 0:
                factor += 1
                
        if factor == 0:
            prime_index.append(k)
            
    return prime_index

def prime_checker2(limit):
    n = 0
    prime_index = []
    for k in range(9754, limit+1):
        factor = 0
        
        
        # 2以外の偶数は素数ではないので無視する(test caseに合わせた)
        if k % 2 == 0 and k != 2:
            continue
        
        # 繰り返しの上限を対象の平方根にする
        for divisor in range(2, math.floor(math.sqrt(k))+1):
            if k % divisor == 0:
                factor += 1
                
        if factor == 0:
            prime_index.append(k)
            
    return prime_index

all_primes = prime_checker(10000)

while True:
    try:
        limit = int(input())

        if limit == 1:
            print(0)

        else:

            li = list(range(1,limit+1,1))
            li_r = list(reversed(li))

            #マッチするインデックスを返す
            index_r = []

            match_primes = [i for i in all_primes if i <= limit]
            index = match_primes
            index_r = [limit+1-j for j in match_primes]

            #インデックス番号が一致するものを抽出する
            print(len(list(set(index) & set(index_r))))
    except:
        break

            
    
    




Measured memory_bytes: 6832
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
//		System.out.println("Started");
		Main main = new Main();
		main.process();
	}
	
	public void process() {
		int nodeNum = 0, queryNum = 0;
		Node[] nodes;
		Scanner scanner = new Scanner(System.in);
		nodeNum = scanner.nextInt();
		queryNum = scanner.nextInt();
		nodes = makeSet(nodeNum);
		for(int i = 0; i < queryNum; i++) {
//			System.out.println("i = " + i);
			int command = scanner.nextInt();
//			System.out.print("com: " + command);
			Node target1 = nodes[scanner.nextInt()];
			Node target2 = nodes[scanner.nextInt()];
//			System.out.println(", t1: " + target1.getNum() + ", t2: " + target2.getNum());
			if(command == 0) union(target1, target2);
			else if(command == 1) same(target1, target2);
			else System.err.println("Input command is wrong!!");
		}
	}
	
	public void same(Node x, Node y) {
//		System.out.println("Process " + x.getNum() + " and " + y.getNum() + " same?");
		if(findSet(x).equals(findSet(y))) System.out.println("1");
		else System.out.println("0");
	}
	
	public Node[] makeSet(int x) {
		Node[] nodes = new Node[x];
		for(int i = 0; i < x; i++) nodes[i] = new Node(i);
		return nodes;
	}
	
	public void union(Node x, Node y) {
//		System.out.println("Process " + x.getNum() + " and " + y.getNum() + " union!");
		link(findSet(x), findSet(y));
	}
	
	public void link(Node x, Node y) {
		if(x.getRank() > y.getRank()) y.point(x);
		else {
			x.point(y);
			if(x.getRank() == y.getRank()) y.setRank(y.getRank() + 1);
		}
	}
	
	public Node findSet(Node x) {
		if(x != x.point()) x.point(findSet(x.point()));
		return x.point();
	}

	private class Node{
		private int rank = 0;
		private int num = 0;
		private Node destination;
		public Node(int num) {
			this.num = num;
			this.destination = this;
		}
		public void setRank(int rank) {
			this.rank = rank;
		}
		public int getNum() {
			return this.num;
		}
		public int getRank() {
			return this.rank;
		}
		public void point(Node node) {
			this.destination = node;
		}
		public Node point() {
			return this.destination;
		}
	}
}


Measured memory_bytes: 102148
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<iostream>
using namespace std;

struct Node
{
    int key;
    Node *left, *right, *parent;
};

Node *root, *NIL;

Node * treeMinimum(Node *x)
{
    while(x->left != NIL) x = x->left;
    return x;
}

Node * find(Node *u, int k)
{
    while (u != NIL && k != u->key)
    {
        if ( k < u->key)
        {
            u = u->left;
        }
        else
        {
            u = u->right;
        }
    }
    return u;
}

Node * treeSuccessor(Node *x)
{
    if (x->right != NIL) 
    {
        return treeMinimum(x->right);
    }

    Node *y = x->parent;
    while (y != NIL && x == y->right)
    {
        x = y;
        y = y->parent;
    }
    return y;
}

void treeDelete(Node *z)
{
    Node *y;
    Node *x;

    if (z->left == NIL || z->right == NIL)
    {
        y = z;
    }
    else
    {
        y = treeSuccessor(z);
    }

    if (y->left != NIL)
    {
        x = y->left;
    }
    else
    {
        x = y->right;
    }

    if (x != NIL)
    {
        x->parent = y->parent;
    }

    if (y->parent == NIL)
    {
        root = x;
    }
    else
    {
        if (y == y->parent->left)
        {
            y->parent->left = x;
        }
        else
        {
            y->parent->right = x;
        }
    }

    if (y != z)
    {
        z->key = y->key;
    }

    free(y);
}

void insert(int k)
{
    Node *y = NIL;
    Node *x = root;
    Node *z;

    z = (Node *)malloc(sizeof(Node));
    z->key = k;
    z->left = NIL;
    z->right = NIL;

    while ( x != NIL)
    {
        y = x;
        if (z->key < x->key)
        {
            x = x->left;
        }
        else
        {
            x = x->right;
        }
    }
    z->parent = y;
    if (y == NIL)
    {
        root = z;
    }
    else
    {
        if (z->key < y->key)
        {
            y->left = z;
        }
        else
        {
            y->right = z;
        }
    }
}

void inorder(Node *u)
{
    if ( u == NIL) return;
    inorder(u->left);
    printf(" %d", u->key);
    inorder(u->right);
}

void preorder(Node *u)
{
    if ( u == NIL) return;
    printf(" %d", u->key);
    preorder(u->left);
    preorder(u->right);
}

int main()
{
    int n, i, x;
    string com;

    scanf("%d", &n);

        for (i = 0; i < n; i++)
    {
        cin >> com;
        if ( com[0] == 'f')
        {
            scanf("%d", &x);
            Node *t = find(root, x);
            if (t != NIL)
            {
                printf("yes\n");
            }
            else
            {
                printf("no\n");
            }
        }
        else if (com == "insert")
        {
            scanf("%d", &x);
            insert(x);
        }
        else if (com == "print")
        {
            inorder(root);
            printf("\n");
            preorder(root);
            printf("\n");
        }
        else if (com == "delete")
        {
            scanf("%d", &x);
            treeDelete(find(root, x));
        }
    }
    return 0;
}


Measured memory_bytes: 22080
#ifdef LOCAL
#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstring>
#else
#include <bits/stdc++.h>
#endif
using namespace std ;

typedef long long ll ;
#define rep(i , n ) for ( int i =0; i < n ; i++)
#define _sort(arg) sort(begin(arg), end(arg))
#define MOD 1000000007
#define pb push_back
#define DEBUG(x) cout << #x << ": " << x << endl;

class UnionFind {
private:
	unsigned size_; 
	std::vector<unsigned> par, rank;
public:
	UnionFind() : size_(0), par(std::vector<unsigned>()), rank(std::vector<unsigned>()) {};
	UnionFind(unsigned size__) : size_(size__) {
		par.resize(size_); rank.resize(size_);
		for (unsigned i = 0; i < size_; i++) par[i] = i, rank[i] = 0;
	}
	unsigned size() { return size_; }
	//unsigned root(unsigned x) { return par[x] == x ? x : par[x] = root(par[x]); }
	unsigned root(unsigned x)
	{
		while(par[x] != x)
		{
			par[x] = par[par[x]];
			x = par[x];
		}
		return x;
	}
	bool same(unsigned x, unsigned y) { return root(x) == root(y); }
	void unite(unsigned x, unsigned y) {
		x = root(x), y = root(y);
		if (x == y) return;
		if (rank[x] < rank[y]) par[x] = y;
		else if (rank[x] == rank[y]) par[y] = x, rank[x]++;
		else par[y] = x;
	}
	bool operator==(const UnionFind &u) { return par == u.par; }
	bool operator!=(const UnionFind &u) { return par != u.par; }
};

/*
__attribute__((constructor))
void initial()
{
	cin.tie(NULL);
	ios::sync_with_stdio(false);
}
*/

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

	ll N, M;
	
	cin >> N >> M;

	ll H[N];
	ll t[N];
	ll c[N];
	rep(i, N)
	{
		cin >> H[i];
		t[i] = 0;
		c[i] = 0;
	}

	rep(i, M)
	{
		int a, b;
		cin >> a >> b;
		c[a-1]++;
		c[b-1]++;
		if (H[a-1] < H[b-1])
		{
			t[b-1]++;
		}
		else if (H[a-1] > H[b-1])
		{
			t[a-1]++;
		}
	}

	int ret = 0;
	rep(i, N)
	{
		if (t[i] == c[i])
		{
			ret++;
		}
	}

	cout << ret <<endl;


	
	return 0;
}


Measured memory_bytes: 5996
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <queue>
#include <stack>
#include <stdlib.h>
using namespace std;
#define rep(i,n) for (int i = 0; i <n; i++) 

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

int main(void)
{
    int a,b,c;
    std::cin >> a>>b>>c;
    std::cout << min(b/a,c) << std::endl;
}





Measured memory_bytes: 256
#include<stdio.h>
int main(){
	int m[28]={0},i,k,n,a,b,c,ha=0;
	char s[102]={0};
	scanf ("%s",s);
	for (i=0;i<strlen(s);i++){
		m[s[i]-'a']++;
		}
		for (i=0;i<28;i++){
		if(m[i]%2==1){
		ha++;
			break;	
		}
		}
		if(ha==0)
		{
		printf ("Yes");}
		else
		{
			printf ("No");
		}
	return 0;
	}

Measured memory_bytes: 128
#include <bits/stdc++.h>
#define ll long long
ll mod=1000000007;
#define vl vector<ll>
#define pl pair<ll,ll>
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,b) for(ll i=0;i<b;i++)
#define RFOR(i,a,b) for(ll i=b-1;i>=a;i--)
#define rsort(v) sort((v).rbegin(), (v).rend())
#define all(v) (v).begin(),(v).end()
using namespace std;
set<string>s;

ll n,k;
ll A[50];
ll solve(ll i, ll m){
    if(m==0)return 1;
    if(i>=n)return 0;
    ll res=solve(i+1,m)||solve(i+1,m-A[i]); //引くか引かないか
    return res;
}

int main(){
    ll q,M;
    cin>>n;
    rep(i,n)cin>>A[i];
    cin>>q;
    rep(i,q){
        cin>>M;
        cout<<(solve(0,M)?"yes":"no")<<endl;
    }
}


Measured memory_bytes: 3096
import math


def division(a, b):
    if b == 0:
        raise ZeroDivisionError()
    if a == 0:
        return 0, 0
    if a > 0 and b > 0:
        q = math.floor(a / b)
        return q, a - b * q
    if a > 0 and b < 0:
        q = math.ceil(a / b)
        return q, a - b * q
    if a < 0 and b > 0:
        q = math.floor(a / b)
        return q, a - b * q
    if a < 0 and b < 0:
        q = math.ceil(a / b)
        return q, a - b * q


def Ans(N, B):
    ans = ''
    while(N != 0):
        N, r = division(N, B)
        ans += str(r)
    if ans:
        print(ans[::-1])
    else:
        print(0)


if __name__ == '__main__':
    N = int(input())
    Ans(N, -2)

Measured memory_bytes: 3064
n = int(input())
lst = list(map(int, input().split()))

lst2 = []
for i, l in enumerate(lst, start= 1):
    lst2.append(l-i)

def median(lst):
    num = len(lst)
    sor = sorted(lst)
    if num%2==0:
        return round((sor[num//2] + sor[num//2-1])/2)
    else:
        return sor[num//2]
b = median(lst2)
ans = sum([abs(val - b) for val in lst2])
print(ans)        

Measured memory_bytes: 27588
S = input()
N = len(S)
if S[0:int((N-1)/2)] == S[0:int((N-1)/2)][::-1] and S[int((N+3)/2)-1:N] == S[int((N+3)/2)-1:N][::-1] and S == S[::-1]:
    print("Yes")
else:
    print("No")

Measured memory_bytes: 3060
<?php
list($H, $W, $A, $B) = array_map("intval", explode(" ", trim(fgets(STDIN))));
for($i = 0; $i < $B; $i++){
  echo str_repeat("0", $A),str_repeat("1", $W-$A),"\n";
}
for($i = $B; $i < $H; $i++){
  echo str_repeat("1", $A),str_repeat("0", $W-$A),"\n";
}


Measured memory_bytes: 5624
from bisect import bisect_left, bisect_right
n = int(input())
nums = [int(i) for i in input().split()]

nums.sort()
ans = 0
for i in range(n):
    for j in range(i + 1, n):
        a = nums[i]
        b = nums[j]
        ans += bisect_left(nums, a + b) - j - 1 
print(ans)

Measured memory_bytes: 43740
#include <bits/stdc++.h>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for(int i = int(a); i < int(b); i++)
#define rer(i, a, b) for(int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef complex<double> comp;
void Debug() {cerr << '\n'; }
template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){
	cerr<<arg<<" ";Debug(rest...);}
template<class T>ostream& operator<<(ostream& out,const vector<T>& v) {
	out<<"[";if(!v.empty()){rep(i,0,sz(v)-1)out<<v[i]<<", ";out<<v.back();}out<<"]";return out;}
template<class S, class T>ostream& operator<<(ostream& out,const pair<S, T>& v){
	out<<"("<<v.first<<", "<<v.second<<")";return out;}
const int MAX_N = 500010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
mt19937 rng; //use it by rng() % mod, shuffle(all(vec), rng)
///////////////////////////////////////////////////////////////////////////////////////////////////


int N, K;
vector<int> G[MAX_N];
int nexsz[MAX_N];
ll dp[MAX_N];
ll ans;

void loop(int v, int p, int dep, int wid) {
	MUL(ans, (K - min(dep, 2)) - wid);
	// debug(v, dep, wid, (K - min(dep, 2) - wid), ans);
	int nw = 0;
	rep(i, 0, sz(G[v])) {
		int n = G[v][i];
		if(n == p) continue;
		loop(n, v, dep + 1, nw++);
	}
}


void solve() {
	cin >> N >> K;
	rep(i, 0, N - 1) {
		int a, b; cin >> a >> b; a--; b--;
		G[a].pb(b);
		G[b].pb(a);
	}
	ans = 1;
	loop(0, -1, 0, 0);
	cout << ans << "\n";
}

uint32_t rd() {
	uint32_t res;
#ifdef __MINGW32__
	asm volatile("rdrand %0" :"=a"(res) ::"cc");
#else
	res = std::random_device()();
#endif
	return res;
}

int main() {
#ifndef LOCAL
	ios::sync_with_stdio(false);
    cin.tie(0);
#endif
    cout << fixed;
	cout.precision(20);
    cerr << fixed;
	cerr.precision(6);
	rng.seed(rd());
#ifdef LOCAL
	//freopen("in.txt", "wt", stdout); //for tester
    freopen("in.txt", "rt", stdin);
#endif	
	solve();
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
	return 0;
}



Measured memory_bytes: 22784
// D - Ki
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;

vi ans(200000);
vvi ab(200000);

void dfs(int cur, int pre){
	for(int nxt:ab[cur]){
		if(nxt==pre) continue;
		ans[nxt] += ans[cur];
		dfs(nxt,cur);
	}
}

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

	for(int i=0; i<n-1; i++){
		int a,b; cin>>a>>b;
		ab[--a].push_back(--b);
		ab[b].push_back(a);
	}

	for(int i=0; i<q; i++){
		int p,x; cin>>p>>x;
		ans[--p] += x;
	}

	dfs(0,-1);

	for(int i=0; i<n; i++) cout<< ans[i] <<(i<n-1?" ":"\n");
}

Measured memory_bytes: 23424
#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();
typedef long long ll;
typedef vector<int> vint;
typedef vector<char> vchar;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
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;
}
template <typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts)
{
    return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T &t, const V &v) { t = v; }

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T &t, const V &v)
{
    for (auto &e : t)
        fill_v(e, v);
}
int main()
{
    ll N,M;
    cin>>N>>M;
    if(N!=1 and M!=1)
    cout<<N*M-(2LL*N+2LL*M-4LL);
    else if(N==1 and M==1)
    cout<<1;
    else if(N==1)
    cout<<M-2;
    else if(M==1)
    cout<<N-2;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
const int MAX = (1e5)+1;
const long long MOD = (1e9)+7;

int n, m;
long long fact[MAX];
int main() {
	cin >> n >> m;
	if(abs(n-m) > 1) cout << 0, exit(0);
	fact[0] = 1;
	for(int i = 1; i <= max(n, m); i++) fact[i] = (fact[i-1] * i) % MOD;
	if(abs(n-m) == 1) cout << (fact[n] * fact[m]) % MOD;
	else cout << (2ll * fact[n] * fact[m]) % MOD;
}

Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	scanf("%d",&n);
	int a[100001];
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	bool flag=0;
	for(int i=n-1;i>0;i--){
		if(a[i]<a[i-1]){
			a[i-1]--;
			if(a[i]<a[i-1]){
				flag=1;
				break;
			}
		}
	}
	if(flag==0){
		printf("Yes\n");
	}else{
		printf("No\n");
	}
}

Measured memory_bytes: 640
#include<stdio.h>
int main(){
	int a,b,i,j;
	while(1){
		scanf("%d %d",&a,&b);
		if(a==b && a==0) break;
		for(i=0;i<a;i++){
			for(j=0;j<b;j++){
				if(i==0 || i==a-1){
					printf("#");
				}else{
					if(j==0 || j==b-1){
						printf("#");
					}else{
						printf(".");
					}		
				}
			}
			printf("\n");
		}
		printf("\n");
	}
}


Measured memory_bytes: 2028
# -*- coding: utf-8 -*-
"""
B - Great Ocean View
https://atcoder.jp/contests/abc124/tasks/abc124_b

"""
import sys


def solve(N, heights):
    max_height = -1
    res = []
    for h in heights:
        res.append(h >= max_height)
        max_height = max(max_height, h)
    return sum(res)


def main(args):
    N = int(input())
    heights = [int(h) for h in input().split()]
    ans = solve(N, heights)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])


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

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

using namespace std;
using lint=long long;

const lint INF=1LL<<61;

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

	vector<lint> cnt(n);
	rep(i,n){
		int a; scanf("%d",&a); a--;
		cnt[a]++;
	}

	sort(cnt.begin(),cnt.end());

	vector<int> next(n);
	next[n-1]=n;
	for(int i=n-2;i>=0;i--) next[i]=(cnt[i]<cnt[i+1]?i+1:next[i+1]);

	lint sum=0;
	rep(i,n) sum+=cnt[i];

	rep(i,n){
		lint ans=0;
		lint tmp=sum;
		lint last=0;
		int pos=n-i,j=1;
		while(pos<n && tmp>j*(cnt[pos]-last)){
			tmp-=j*(cnt[pos]-last);
			ans+=cnt[pos]-last;
			last=cnt[pos];
			j+=next[pos]-pos;
			pos=next[pos];
		}
		ans+=tmp/j;
		printf("%lld\n",ans);
		sum-=cnt[n-i-1];
	}

	return 0;
}


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


int main() {
    string s,s2;
    cin>>s>>s2;
    for(int i=0;i<s.length();i++) {
        if(s[i]!=s2[i]) {
            cout<<"No";
            return 0;
        }
    }
    cout<<"Yes";
    return 0;
}

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

int main(){
  int N, K;
  string S;
  cin >> N >> S >> K;
  
  char a = S.at(K - 1);
  
  for(char &i: S){
    if(i != a)
      i ='*';
  }
  
  cout << S << endl;
}


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

using namespace std;

#define INF (1e9)

typedef pair<int, int> P;

struct Q {
    int time;
    int togo;
    int status;
    int room;
    Q() {}
    Q(int a,int b,int c,int d) {
        this->time=a; this->togo=b; this->status=c; this->room=d;
    }
    bool operator<(const Q& rhs) const {
        return (this->time==rhs.time)?(this->togo<rhs.togo):(this->time<rhs.time);
    }
    bool operator>(const Q& rhs) const {
        return (this->time==rhs.time)?(this->togo>rhs.togo):(this->time>rhs.time);
    }
};

int main() {
    int N,M;
    int X;
    cin >> N >> M >> X;

    vector<int> T(N);
    for(int i=0; i<N; ++i) {
        cin >> T[i];
    }

    vector<P> to[N];
    for(int i=0; i<M; ++i) {
        int a,b,d;
        cin >> a >> b >> d;
        to[a-1].push_back(P(d,b-1));
        to[b-1].push_back(P(d,a-1));
    }

    priority_queue<Q,vector<Q>,greater<Q> > q;
    int time[N][2][X+1];

    for(int i=0; i<N; ++i) {
        for(int j=0; j<2; ++j) {
            for(int k=0; k<X+1; ++k) {
                time[i][j][k]=INF;
            }
        }
    }

    q.push(Q(0,X,0,0)); // time, togo, status(0=cold,1=normal,2=hot), room
    time[0][0][X]=0;
    // int cnt=0; // for debug
    while(!q.empty()) {
        Q now=q.top(); q.pop();
        if(time[now.room][now.status/2][now.togo]<now.time) continue;
        for(auto nxt : to[now.room]) {
            int nxt_togo=max(0,now.togo-nxt.first);
            if(T[nxt.second]==1) {
                if(time[nxt.second][now.status/2][nxt_togo]<=now.time+nxt.first) continue;
                time[nxt.second][now.status/2][nxt_togo]=now.time+nxt.first;
                q.push(Q(now.time+nxt.first,nxt_togo,now.status,nxt.second));
            } else if(T[nxt.second]==now.status || nxt_togo==0) {
                if(time[nxt.second][T[nxt.second]/2][X]<=now.time+nxt.first) continue;
                time[nxt.second][T[nxt.second]/2][X]=now.time+nxt.first;
                q.push(Q(now.time+nxt.first,X,T[nxt.second],nxt.second));
            }
        }
    }

    int ans=INF;

    for(int i=0; i<2; ++i) {
        for(int j=0; j<X+1; ++j) {
            ans=min(ans,time[N-1][i][j]);
        }
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 19652
//Bismillahir Rahmanir Rahim
#include<iostream>
using namespace std;
int main()
{
	int i,j,n,b,c,t,res,sum=0;
	string s1,s2;
	cin>>n;
	cin>>s1>>s2;

	for(i=0,j=0;i<n,j<n;i++,j++)
	{
		
		cout<<s1[i];
		cout<<s2[j];
	}
	cout<<endl;
}

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

using namespace std;

typedef long long LL;
typedef pair<int , int> P2;
typedef pair<pair<int , int> , int> P3;
typedef pair<pair<int , int> , pair<int , int> > P4;
#define PB(a) push_back(a)
#define MP(a , b) make_pair((a) , (b))
#define M3P(a , b , c) make_pair(make_pair((a) , (b)) , (c))
#define M4P(a , b , c , d) make_pair(make_pair((a) , (b)) , make_pair((c) , (d)))
#define repp(i,a,b) for(int i = (int)(a) ; i < (int)(b) ; ++i)
#define repm(i,a,b) for(int i = (int)(a) ; i > (int)(b) ; --i)

const int BIT_MAX = 40010;

template <class T> class BIT{
	T x[BIT_MAX + 1];
	
public:
	
	void dft(){
		fill(x,x+BIT_MAX+1,0);
	}
	
	void add(int a , T b){
		++a;
		for(; a <= BIT_MAX ; a += a & -a) x[a] += b;
	}
	
	T get(int a){
		++a;
		T b = 0;
		for(; a > 0 ; a -= a & -a) b += x[a];
		return b;
	}
};

const int MC = 40010;
int N;
int A[MC],B[MC],C[MC];
int s[MC];
double t[MC];
int u[MC];
BIT<int> bit;

bool eval(double z){
	repp(i,0,N) t[s[i]] = (C[s[i]] - A[s[i]] * z) / B[s[i]];
	sort(u,u+N,[&](const int p , const int q){return t[s[p]] < t[s[q]];});
	int w = 0;
	bit.dft();
	repp(i,0,N){
		w += bit.get(u[i]);
		bit.add(0,1);
		bit.add(u[i]+1,-1);
	}
	return w < (N * (N-1) / 2 + 1) / 2;
}

double BS(double x , double y , int t){
	double z = (x+y)/2;
	if(t == 0) return z;
	return eval(z) ? BS(z,y,t-1) : BS(x,z,t-1);
}

double solve(){
	sort(s,s+N,[&](const int p , const int q){return 1. * A[p] / B[p] < 1. * A[q] / B[q];});
	return BS(-1e9 , 1e9 , 100);
}

int main(){
	scanf("%d" , &N);
	repp(i,0,N){
		scanf("%d%d%d" , A+i , B+i , C+i);
		s[i] = i;
		u[i] = i;
	}
	double x = solve();
	repp(i,0,N) swap(A[i],B[i]);
	double y = solve();
	printf("%.12f %.12f\n" , x , y);
	return 0;
}


Measured memory_bytes: 1536
#include<bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
using namespace std;
typedef long long ll;
typedef pair<int,pair<int,int>> P;
const ll MOD=1000000007;
const ll INF=1000000010;
const ll LINF=4000000000000000010;
const double EPS=1e-9;
int dx[5]={0,1,0,-1,0};
int dy[5]={1,0,-1,0,0};
vector<int> G[100010];
int dfs(int i,int p){
    int res=0;
    for(auto e:G[i]){
        if(e==p)continue;
        res^=dfs(e,i)+1;
    }
    return res;
}
int main(){
    int n;cin>>n;
    for(int i=0;i<n-1;i++){
        int x,y;cin>>x>>y;x--;y--;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    if(dfs(0,-1)){
        cout<<"Alice"<<endl;
    }else{
        cout<<"Bob"<<endl;
    }
}


Measured memory_bytes: 10368
#include<iostream>
#include<cstdio>
using namespace std;
int main ()
{
	int m,i;
	scanf("%d",&m);
	long long sum=0,num=0;
	for(i=1;i<=m;++i)
	{
		long long a,b;
		scanf("%lld%lld",&a,&b);
		sum+=a*b;
		num+=b;
	}
	num--;
	num+=(sum-1)/9;
	printf("%lld\n",num);
}

Measured memory_bytes: 256
using System;
using System.IO;
using System.Linq;
using System.Runtime;
using System.Reflection;
using System.Diagnostics;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using static System.Math;

class P
{
    static void Main()
    {
        Console.WriteLine(new int[] { 7, 5, 3 }.Contains(int.Parse(Console.ReadLine())) ? "YES" : "NO");
    }
}


Measured memory_bytes: 13268