akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>

// Shrotening
#define fst first
#define snd second
#define pb push_back
#define mp std::make_pair

// Loop
#define FOR(i,a,b) for(auto i=(a);i<(b);++i)
#define RFOR(i,a,b) for(auto i=(a);i>=(b);--i)

#define REP(i,a) for(long i=0;i<(a);++i)
#define RREP(i,a) for(long i=(a);i>=0;--i)

#define EACH(i,a) for(auto (i)=(a).begin(),_END=(a).end();i!=_END;++i)
#define REACH(i,a) for(auto (i)=(a).rbegin(),_END=(a).rend();i!=_END;++i)

//Algorithm
#define ALL(a) (a).begin(), a.end()
#define RALL(a) (a).rbegin(), a.rend()
#define EXIST(a,x) ((a).find(x)!=(a).end())
#define SORT(a) std::sort((a).begin(), (a).end())
#define UNIQUE(a) std::sort((a).begin(), a.end()), a.erase(std::unique((a).begin(), a.end()), a.end());
#define SUM(a) std::accumulate((a).begin(), (a).end(), 0);

//Setting
#define OPT std::cin.tie(0);std::ios::sync_with_stdio(false);

//debug message
bool debug = true;
#define MSG(s)   if(debug){std::cout << s << std::endl;}
#define DEBUG(x) if(debug){std::cout << "debug(" << #x << "): " << x << std::endl;}

//alias
typedef long long LL;
typedef std::vector<char> VC;
typedef std::vector<int>  VI;
typedef std::vector<long> VL;
typedef std::vector<long long> VLL;

typedef std::vector< VC > VC2;
typedef std::vector< VI > VI2;
typedef std::vector< VL > VL2;
typedef std::vector< VLL > VLL2;

typedef std::pair<int,int> PII;

int N;
int main() {
    std::cin >> N;

    int ans = N;
    for(int i=0; i<=N; ++i) {
        int cost = 0;
        for(int j=i; j>0; j /= 6) {
            cost += j % 6;
        }
        for(int j=N-i; j>0; j /= 9) {
            cost += j % 9;
        }
        ans = std::min(ans, cost);
    }
    std::cout << ans << std::endl;
}

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

void swap(int* A, int p, int q){
    int reserve = A[p];
    A[p] = A[q];
    A[q] = reserve;
}

int main(){
    //input
    int n;
    cin >> n;
    int A[n];
    for(int i=0; i<n; i++){
        cin >> A[i];
    }
    
    //calculation
    int k = 0; //number of swaps
    int mini;
    for(int i=0; i<n; i++){
        mini = i;
        for(int j=i; j<n; j++){
            if(A[j] < A[mini]){
                mini = j;
            }
        }
        if(i != mini){
            swap(A, i, mini);
            k++;
        }
    } 
    
    //output
    for(int i=0; i<n-1; i++){
        cout << A[i] << " ";
    }
    cout << A[n-1] << endl;
    cout << k << endl;
}

Measured memory_bytes: 1160
def subsolve(pop, push):
    ans = 0
    for i in range(pop + 1):
        rpop, lpop = i, pop - i
        r = [] if rpop == 0 else vs[-rpop:]
        l = [] if lpop == 0 else vs[:lpop]
        ans = max(ans, sum(sorted(r + l)[push:]))
    return ans


NEGINF = -float("inf")
n, k = map(int, input().split())
vs = list(map(int, input().split()))
ans = 0

for k_ in range(k + 1):
    for i in range(k_ + 1):
        push, pop = i, k_ - i
        if push > pop or pop > n:
            continue
        ans = max(ans, subsolve(pop, push))


print(ans)


Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(var, begin, end) for (int var = (begin); var <= (end); var++)
#define RFOR(var, begin, end) for (int var = (begin); var >= (end); var--)
#define REP(var, length) FOR(var, 0, length - 1)
#define RREP(var, length) RFOR(var, length - 1, 0)
#define EACH(value, var) for (auto value : var)
#define SORT(var) sort(var.begin(), var.end())
#define REVERSE(var) reverse(var.begin(), var.end())
#define RSORT(var) SORT(var); REVERSE(var)
#define OPTIMIZE_STDIO ios::sync_with_stdio(false);cin.tie(0);cout.precision(10);cout << fixed
#define endl '\n'

const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
struct mint {
    ll x;
    mint() : x(0) {}
    mint(ll x) : x((x % MOD + MOD) % MOD) {}
    mint& operator+=(const mint& r) { if ((x += r.x) >= MOD) { x -= MOD; } return *this; }
    mint& operator-=(const mint& r) { if ((x -= r.x) < 0) { x += MOD; } return *this; }
    mint& operator*=(const mint& r) { if ((x *= r.x) > MOD) { x %= MOD; } return *this; }
    mint& operator++() { if ((++x) >= MOD) { x -= MOD; } return *this; }
    mint operator++(int) { mint ret = x; if ((++x) >= MOD) { x-= MOD; } return ret; }
    mint& operator--() { if ((--x) < 0) { x += MOD; } return *this; }
    mint operator--(int) { mint ret = x; if ((--x) < 0) { x += MOD; } return ret; }
    mint operator+(const mint& r) { mint ret; return ret = x + r.x; }
    mint operator-(const mint& r) { mint ret; return ret = x - r.x; }
    mint operator*(const mint& r) { mint ret; return ret = x * r.x; }
    mint operator-() { return mint() - *this; }
    bool operator<(const mint& a) { return x < a.x; }
    bool operator>(const mint& a) { return x > a.x; }
    bool operator<=(const mint& a) { return x <= a.x; }
    bool operator>=(const mint& a) { return x >= a.x; }
    bool operator==(const mint& a) { return x == a.x; }
    bool operator!=(const mint& a) { return x != a.x; }
};
ostream& operator<<(ostream& os, const mint& r) { os << r.x; return os; }
ostream& operator>>(ostream& os, const mint& r) { os >> r.x; return os; }

void solve(istream& cin, ostream& cout) {
  int N;
  cin >> N;
  vector<int> B(N);
  REP(i, N - 1) cin >> B[i];
  vector<int> A(N);
  A[0] = B[0];
  FOR(i, 1, N - 1) {
    if(i != N - 1) A[i] = min(B[i - 1], B[i]);
    else A[i] = B[i - 1];
  }
  int ans = 0;
  REP(i, A.size()) ans += A[i];
  cout << ans << endl;
}

#ifndef TEST
int main() {
  OPTIMIZE_STDIO;
  solve(cin, cout);
}
#endif


Measured memory_bytes: 256
#ifdef _DEBUG
#include "MyLib.h"
#else
#define main_C main
#include "bits/stdc++.h" 
#include <regex>
#define _USE_MATH_DEFINES
#include <math.h>
#define FOR(i,s,e) for (int i = int(s); i < int(e); ++i)
#define REP(i,e) FOR(i,0,e)
#define INF (INT_MAX/2)
#define EPS (1.0e-8)
#define LINF (LONG_MAX/2)
const int MGN = 8;
const int ARY_SZ_MAX = 10000000;
using namespace std;
using ll = long long; using ull = unsigned long long;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vl = vector<ll>; using vvl = vector<vl>;
using vd = vector<double>; using vs = vector<string>;
using pii = pair<int, int>; using pll = pair<ll, ll>;
// functions

#endif


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

    int N; cin >> N;
    vvi A(2, vi(N));
    REP(i, 2)
        REP(j, N) cin >> A[i][j];

    // dp[i][j] : Max of sum of the route from (0,0) to (i,j)
    vvi dp(2, vi(N, 0));
    dp[0][0] = A[0][0];
    FOR(j, 1, N) dp[0][j] = dp[0][j - 1] + A[0][j];

    dp[1][0] = A[0][0] + A[1][0];
    FOR(j, 1, N) dp[1][j] = max(dp[1][j - 1], dp[0][j]) + A[1][j];

    int ans = dp[1][N-1];

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 256
import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        // 整数の入力
        int a = sc.nextInt();
        // スペース区切りの整数の入力
        int b = sc.nextInt();
        int k = sc.nextInt();
      int tintin=b-a;
        // 文字列の入力
   
  List<Integer> list = new ArrayList<Integer>();
  
      
      if(b-a-1+2>=k*2){
 		 for(int i=0;i<k;i++){
  		  list.add(a+i);
		}
  
		  for(int i=0;i<k;i++){
 		 	list.add(b-k+1+i);
 		 }
      }else{
      for(int i=0;i<=tintin;i++){
      	list.add(a+i);
      	}
      }
 
      for (Integer unti: list){
  System.out.println(unti);
}
      
	}
}

Measured memory_bytes: 23764
program WalkingTakahashi
!
      implicit none
      integer(16) :: X,K,D
      integer(16) :: ans1,ans2
      intrinsic abs,mod
!
      read(*,*) X,K,D
!
      ans1 = abs(X)/D
      ans2 = ans1 + 1
!
      if(K > ans1) then
        if(mod(K-ans1,2) == 0) write(*,*) abs(X)-ans1*D
        if(mod(K-ans1,2) == 1) write(*,*) ans2*D-abs(X)
      else
        write(*,*) abs(X)-K*D
      end if
!
      end program WalkingTakahashi

Measured memory_bytes: 2832
//by yjz
#include<bits/stdc++.h>
#define FF first
#define SS second
#define MP make_pair
#define PB push_back
typedef long long ll;
using namespace std;
const int maxn = 511;
ll inf;
int n;
int a[maxn][maxn];
ll dp[maxn][maxn], ca[maxn][maxn], cb[maxn][maxn];
ll suma[maxn][maxn], sumb[maxn][maxn];
void upd(ll &x, ll v) {x=min(x, v);}
int main()
{
	scanf("%d", &n);
	for (int i=1; i<=n; i++)
	{
		for (int j=1; j<=n; j++)
		{
			if (i!=j) scanf("%d", &a[i][j]);
		}
	}
	for (int i=1; i<=n; i++)
	{
		ll sum = 0;
		for (int j=i; j>=0; j--)
		{
			ca[i][j] = sum;
			sum += a[j][i];
		}
	}
	for (int i=1; i<=n; i++)
	{
		ll sum = 0;
		for (int j=1; j<=i; j++)
		{
			cb[i][j] = sum;
			sum += a[i][j];
		}
	}
	for (int i=0; i<=n; i++)
	{
		for (int j=0; j<=n; j++)
		{
			suma[i][j] = ca[i][j];
			sumb[i][j] = cb[i][j];
		}
	}
	for (int i=1; i<=n+1; i++) for (int j=0; j<=n+1; j++)
	{
		suma[i][j] += suma[i-1][j];
		sumb[i][j] += sumb[i-1][j];
	}
	memset(dp, 20, sizeof(dp));
	inf = dp[0][0];
	dp[n+1][n+1] = 0;
	for (int i=n+1; i>=1; i--)
	{
		for (int j=i; j<=n+1; j++)
		{
			ll &cur = dp[i][j];
			if (cur>=inf) continue;
			for (int k=0; k<i; k++)
			{
				ll c = 0;
				c += suma[j][i-1]-suma[i-1][i-1];
				c += sumb[j][k]-sumb[i-1][k];
				upd(dp[k][i-1], dp[i][j]+c);
			}
		}
	}
	cout<<dp[0][0]<<endl;
	return 0;
}

Measured memory_bytes: 11520
#include<bits/stdc++.h>
using namespace std;
#define intl long long int

intl pow1(intl x,intl y){
    intl res=1;
    while(y){
        if(x%2!=0){
            res*=x;
        }
        x*=x;
        y/=2;
    }
    return res;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    intl i,j,n,m,k;
    cin>>n>>m>>k;
    vector<vector<intl> > ar(n,vector<intl>(m));
    intl black=0;
    vector<intl> row(n);
    vector<intl> rowpre(n);
    vector<intl> col(m);
    vector<intl> colpre(m);
    for(i=0;i<n;i++){
        intl tmp=0;
        for(j=0;j<m;j++){
            char c;
            cin>>c;
            if(c=='.'){
                ar[i][j]=0;
            }
            else{
                ar[i][j]=1;
                tmp++;
                black++;
            }
            if(ar[i][j]){
                col[j]++;
            }
        }
        row[i]=tmp;
    }
    for(i=0;i<n;i++){
        rowpre[i]=row[i];
        if(i>0){
            rowpre[i]+=rowpre[i-1];
        }
        // printf("row rowpre are %lld %lld\n",row[i],rowpre[i]);
    }
    for(i=0;i<m;i++){
        colpre[i]=col[i];
        if(i>0){
            colpre[i]+=colpre[i-1];
        }
        // printf("col colpre are %lld %lld\n",col[i],colpre[i]);
    }
    black-=k;
    intl ans=0;
    intl val=1;
    vector<intl> arr(7);
    for(i=0;i<7;i++){
        arr[i]=val;
        val*=2;
    }
    intl n1=arr[n];
    intl m1=arr[m];
    // printf("n1 m1 are %lld %lld\n",n1,m1);
    for(i=0;i<n1;i++){
        vector<intl> ar1;
        intl x=i;
        for(intl i1=0;i1<n;i1++){
            if(x%2!=0){
                ar1.push_back(i1);
            }
            x/=2;
        }
        for(j=0;j<m1;j++){
            vector<intl> ar2;
            intl y=j;
            for(intl j1=0;j1<m;j1++){
                if(y%2!=0){
                    ar2.push_back(j1);
                }
                y/=2;
            }
            intl n2=ar1.size();
            intl m2=ar2.size();
            // printf("n2 m2 are %lld %lld\n",n2,m2);
            intl tmp1=0;
            for(intl i1=0;i1<n2;i1++){
                for(intl j1=0;j1<m;j1++){
                    if(ar[ar1[i1]][j1]){
                        tmp1++;
                    }
                }
            }
            for(intl i1=0;i1<n;i1++){
                for(intl j1=0;j1<m2;j1++){
                    if(ar[i1][ar2[j1]]){
                        tmp1++;
                    }
                }
            }
            for(intl i1=0;i1<n2;i1++){
                for(intl j1=0;j1<m2;j1++){
                    if(ar[ar1[i1]][ar2[j1]]){
                        tmp1--;
                    }
                }
            }
            if(tmp1==black){
                ans++;
            }
        }
    }
    cout<<ans<<"\n";
}

Measured memory_bytes: 3652
#include <iostream>
#include <algorithm>
#include <vector>
#define rep(i, a, b) for(int i = a; i < b; i++)
using namespace std;

int main() {
  vector<vector<int>> v(3, vector<int>(3)), c(3, vector<int>(3, -1));
  rep(i, 0, 3) {
    rep(j, 0, 3) {
      cin >> v[i][j];
    }
  }
  int n;
  cin >> n;
  vector<int> b(n);
  rep(i, 0, n) cin >> b[i];
  rep(i, 0, n) {
    rep(j, 0, 3) {
      rep(k, 0, 3) {
        if(v[j][k] == b[i]) {
          c[j][k] = 0;
        }
      }
    }
  }
  rep(i, 0, 3) {
    if(c[i][0] == 0 && c[i][1] ==  0 && c[i][2] == 0) {
      cout << "Yes\n";
      return 0;
    }
    if(c[0][i] == 0 && c[1][i] == 0 && c[2][i] == 0) {
      cout << "Yes\n";
      return 0;
    }
  }
  if(c[0][0] == 0 && c[1][1] == 0 && c[2][2] == 0) {
    cout << "Yes\n";
    return 0;
  }
  if(c[2][0] == 0 && c[1][1] == 0 && c[0][2] == 0) {
    cout << "Yes\n";
    return 0;
  }
  cout << "No\n";
}

Measured memory_bytes: 3624
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll longinf=1LL<<60;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define F first
#define S second
constexpr char ln = '\n';

const int mx=100010;
const ll mod=1e9+7;

int main(){
  int n;
  string s,t;
  cin >> n >> s >> t;

  int cnt = 0;
  rep(i,n){
    bool flag = true;
    rep(j,n-i){
      if(s[i+j]!=t[j]){
        flag = false; break;
      }
    }
    if(flag){
      cnt = n-i;
      break;
    }
  }
  int ans = n*2-cnt;
  cout << ans << ln;
  return 0;
}

Measured memory_bytes: 256
#include <stdio.h>

int main(){
  int N,i,x=0,MAX=0;
  char S[101];
  scanf("%d",&N);
  scanf("%s",S);
  for(i=0;i<N;i++){
    if(S[i]=='I'){
      x++;
    }else{
      x--;
    }
    if(x>MAX) MAX=x;
  }
  printf("%d\n",MAX);
  return 0;
}

Measured memory_bytes: 128
import java.util.Scanner;
class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		while(true){
			int n = scan.nextInt();
			if(n == 0){
				break;
			}
			String[][] rule = new String[n][2];
			for(int i = 0;i < n;i++){
				for(int j = 0;j < 2;j++){
					rule[i][j] = scan.next();
				}
			}
			int m = scan.nextInt();
			boolean flag = false;
			for(int i = 0;i < m;i++){
				String str = scan.next();
				for(int j = 0;j < n;j++){
					if(str.equals(rule[j][0])){
						flag = true;
						System.out.print(rule[j][1]);
						break;
					}
				}
				if(flag){
					
				}else{
					System.out.print(str);
				}
				flag = false;
			}
			System.out.println();
			
		}
	}
}

Measured memory_bytes: 12000
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  string s;
  cin >> s;
  cout << s[0] << s[1] << s[2] << endl;
  
return 0;
}

Measured memory_bytes: 3624
#include<iostream>
#include<string>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<queue>

using namespace std;

int n;
long long a[100];

int main(){
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>a[i];
  }
  int mi = 0;
  int ma = 0;
  for(int i=0;i<n;i++){
    if(abs(a[i]) > ma){
      mi = i;
      ma = abs(a[i]);
    }
  }
  cout<<(2*n-2)<<endl;
  for(int i=0;i<n;i++)if(mi != i){
    cout<<(mi+1)<<" "<<(i+1)<<endl;
  }
  if(a[mi] > 0){
    for(int i=0;i<n-1;i++){
      cout<<(i+1)<<" "<<(i+2)<<endl;
    }
  } else {
    for(int i=n-1;i>0;i--){
      cout<<(i+1)<<" "<<(i)<<endl;
    }
  }

  return 0;
}

Measured memory_bytes: 256
import sys
input = sys.stdin.readline
import random
import time
import copy
time0=time.time()

D=int(input())
C=[0]+list(map(int,input().split()))
S=[0]+[[0]+list(map(int,input().split())) for i in range(D)]

T=[0]
for i in range(1,D+1):
    MAX=-1
    for j in range(1,27):
        if S[i][j]>MAX:
            k=j
            MAX=S[i][j]
    T.append(j)

ANS=0
LAST=[[0]*27 for i in range(D+1)]
for day in range(1,D+1):
    for j in range(1,27):
        LAST[day][j]=LAST[day-1][j]
    t=T[day]
    ANS+=S[day][t]
    LAST[day][t]=day

    for j in range(1,27):
        ANS-=C[j]*(day-LAST[day][j])

while time.time()-time0<1.77:
    BT=copy.deepcopy(T)
    BLAST=copy.deepcopy(LAST)
    BANS=ANS
    
    d=random.randint(1,D)
    after_t=random.randint(1,26)

    before_t=T[d]

    ANS+=S[d][after_t]-S[d][before_t]

    T[d]=after_t

    for day in range(d,D+1):
        if T[day]!=before_t:
            ANS+=C[before_t]*(day-LAST[day][before_t])
            LAST[day][before_t]=LAST[day-1][before_t]
            ANS-=C[before_t]*(day-LAST[day][before_t])
        else:
            break

    ANS+=C[after_t]*(d-LAST[d][after_t])
    LAST[d][after_t]=d

    for day in range(d+1,D+1):    
        if T[day]!=after_t:
            ANS+=C[after_t]*(day-LAST[day][after_t])
            LAST[day][after_t]=LAST[day-1][after_t]
            ANS-=C[after_t]*(day-LAST[day][after_t])

    if ANS*100<BANS*98:
        T=BT
        LAST=BLAST
        ANS=BANS

print(*T[1:])


Measured memory_bytes: 179288
#[allow(unused_macros)]
macro_rules! input_byline {
    ($($r:tt)* ) => {
        let stdin = std::io::stdin();
        let mut q = std::collections::VecDeque::new();
        let mut next = || {
            if q.is_empty() {
                let mut s = String::new();
                stdin.read_line(&mut s);
                for v in s.split_whitespace() {
                    q.push_back(v.to_string());
                }
            }
            q.pop_front().unwrap()
        };
        input_inner!{next, $($r)*}
    };

}

#[allow(unused_macros)]
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

trait Printable {
    fn p(&self);
}

impl<T: std::fmt::Display> Printable for T {
    fn p(&self) {
        println!("{}", self);
    }
}

impl<T: std::fmt::Display> Printable for [T] {
    fn p(&self) {
        for i in 0..self.len() {
            if i != 0 {
                print!(" ");
            }
            print!("{}", self[i]);
        }
        println!("");
    }
}

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{BinaryHeap, HashMap};

#[allow(dead_code)]
type Graph = Vec<Vec<usize>>;
#[allow(dead_code)]
const M: usize = 1e9 as usize + 9;

fn main() {
    input! {
        n: usize,
        x: usize,
        mut a: [usize; n],
    }

    let mut ans = 0;
    for i in 0..n - 1 {
        if a[i] + a[i + 1] > x {
            ans += min(a[i + 1], a[i] + a[i + 1] - x);
            a[i + 1] -= min(a[i + 1], a[i] + a[i + 1] - x);
        }
        if a[i] + a[i + 1] > x {
            ans += a[i] - x;
        }
    }
    println!("{}", ans);
}


Measured memory_bytes: 4352
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
using namespace std;

int f(int n, int m) {
    if (n == 0) return 1;
    return m * f(n - 1, m) + f(n - 1, m + 1);
}

void dfs(int n, int m, string& s, vector<string>& vs) {
    if (n == 0) {
        vs.emplace_back(s);
        return;
    }
    for (int i = 0; i <= m; ++i) {
        s.push_back(char('a' + i));
        dfs(n - 1, m + (i == m), s, vs);
        s.pop_back();
    }
}

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

    int N;
    cin >> N;
    vector<string> vs;
    string s;
    dfs(N, 0, s, vs);
    sort(vs.begin(), vs.end());
    vs.erase(unique(vs.begin(), vs.end()), vs.end());
    for (int i = 0; i < vs.size(); ++i) {
        cout << vs[i] << endl;
    }
}

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

int main() {
  int N, K, x, sum=0;
  
  cin >> N >> K;
  
  int i=0;
  while ( i < N ) {
    cin >> x;
    
    if ( x < (K - x) ) {
      sum += 2*x;
    }
    else {
      sum += 2*(K-x);
    }
    i++;
  }
  cout << sum << endl;
}


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

struct P {
    ll a;
    ll b;

    bool operator<(const P &another) const {
        if (a == another.a)
            return b < another.b;
        else
            return a < another.a;
    };
};

int main() {
	ll N,M;
	cin >> N >> M;
  vector<P> p(N);
	rep(i,N){
		cin >> p[i].a >> p[i].b;
	}

	sort(all(p));
	ll ans = 0;
	ll nokori = M;
	rep(i,N){
		if(nokori >= p[i].b){
			nokori -= p[i].b;
			ans+=p[i].a*p[i].b;
		} else {
			ans+=p[i].a*nokori;
			nokori=0;
		}
	}
	cout << ans << endl;
}

Measured memory_bytes: 1792
#include <iostream>
using namespace std;
int main(void){
    
    while(1){
        int A, B;
        cin >> A >> B;
        
        if(A == 0 && B == 0) break;
        
        else if(A < B) cout << A << " " << B << endl;
        else cout << B << " " << A << endl;
    }
    
    return 0;
}



Measured memory_bytes: 3012
#include <stdio.h>

int main (void)
{
    int N;
    int a[10001];
    int cnt = 0;
    scanf("%d", &N);
    int i;
    for (i = 0; i < N; i++){
        a[i] == 0;
    }
    for (i = 0; i < N; i++){
        scanf("%d", &a[i]);
    }
    i = 1;
    while (i < N) {
        if (a[i - 1] == a[i]){
            cnt++;
            i += 2;
        } else {
            i++;
        }
    }
    printf("%d", cnt);
    return 0;
}

Measured memory_bytes: 128
#include <iostream>
using namespace std;
int main(void){
    // Your code here!
    int a;
    int b,c,d;
    cin >> a;
    b = a%10;
    c = a%100 - b;
    d = a-b-c;
    //cout << b << endl << c << endl << d << endl; 
    if(b == d/100){
        cout << "Yes" << endl;
    }
    else cout << "No" << endl;
    return 0;
    
}


Measured memory_bytes: 256
import java.util.*;class Main{public static void main(String[]g){int n=1000002,a[]=new int[n],i,j,m;for(i=0;i<n;)a[i]=i++;for(i=1;i++<n/2;){if(a[i]<1)continue;m=0;for(j=i;++j<n;){if(a[j]<1)continue;if(a[j]%i<1)a[j]=0;if(a[j]>0)m=j;}if(a[m]<=i*i)break;}for(i=1,a[i]=j=0;++i<n;)a[i]=a[i]>0?++j:j;for(Scanner S=new Scanner(System.in);S.hasNext();System.out.println(a[S.nextInt()]));}}


Measured memory_bytes: 30516
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
//#define cerr if(false) cerr
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
using namespace std;
using ll = long long;
using pii = pair<int,int>;
template<typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> a){
    os << '(' << a.first << ',' << a.second << ')';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os, vector<T> v){
    for(auto x:v)os << x << ' ';
    return os;
}
void debug(){cerr << '\n';}
template<typename H, typename... T>
void debug(H a, T... b){
    cerr << a;
    if(sizeof...(b))cerr << ", ";
    debug(b...);
}
class UnionFind {
private:
    int sz;
    vector<int> par, nrank;
public:
    UnionFind(){}
    UnionFind(int node_size) : sz(node_size), par(sz), nrank(sz, 0){
        iota(par.begin(), par.end(), 0);
    }
    int find(int x){
        if(par[x] == x) return x;
        else return par[x] = find(par[x]);
    }
    void unite(int x,int y){
        x = find(x), y = find(y);
        if(x == y) return;
        if(nrank[x] < nrank[y]) swap(x,y);
        par[y] = x;
        if(nrank[x] == nrank[y]) nrank[x]++;
    }
    bool same(int x,int y){
        return find(x) == find(y);
    }
};
bool check(vector<vector<int>> &g){
    int n = (int) g.size()-1;
    UnionFind uf(n+1);
    set<int>st;
    for(int i = 1; i <= n; i++){
        int s = 0;
        for(auto x:g[i]){
            s += x;
            uf.unite(i,x);
        }
        st.insert(s);
        if(st.size()>=2)return false;
    }
    for(int i = 1; i <= n; i++){
        if(!uf.same(1,i))return false;
    }
    show(*st.begin());
    return true;
}
auto solve(int n){
    vector<vector<int>>g;
    g.resize(n+1);
    vector<pii>e;
    for(int i = 1; i <= n; i++){
        for(int j = i + 1; j <= n; j++){
            if(i + j != n + !(n % 2)){
                e.push_back(pii(i,j));
                g[i].push_back(j);
                g[j].push_back(i);
            }
        }
    }
    assert(check(g));
    return e;
}
int main(){
//    for(int i = 3; i <= 100; i++){
//        solve(i);
//    }
    int n;
    cin >> n;
    auto e = solve(n);
    cout << e.size() << endl;
    for(auto x:e){
        cout << x.first << " " << x.second << "\n";
    }
}

Measured memory_bytes: 384
import itertools
n=int(input())
f=[list(map(int,input().split())) for i in range(n)]
p=[list(map(int,input().split())) for i in range(n)]
ans=-1000000000
for x in itertools.product([0,1],repeat=10):
    if all(i==0 for i in x):
        continue
    cnt=[0]*n
    tmp=0
    for i in range(n):
        for t in range(10):
            if x[t]==0:continue
            cnt[i]+=f[i][t]
        tmp+=p[i][cnt[i]]
    ans=max(ans,tmp)
print(ans)

Measured memory_bytes: 3064
#include<stdio.h>
#include<math.h>

int main(){
  double p,x,y,x1,y1,x2,y2;

  scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);

  if(x1>x2)x=x1-x2;
  else x=x2-x1;

  if(y1>y2)y=y1-y2;
  else y=y2-y1;

  p=sqrt(x*x+y*y);
  printf("%f\n",p);
  return 0;
}

Measured memory_bytes: 652
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
typedef int64_t lint;
using namespace std;
using ll = long long;
using P = pair<ll,ll>;

//マクロ
//forループ関係
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
//Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
//xにはvectorなどのコンテナ
#define ALL(x) (x).begin(),(x).end() //sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) //sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x)) //最大値を求める
#define MIN(x) *min_element(ALL(x)) //最小値を求める
#define debug(x) cerr << #x << ": " << x << "\n"//手動デバッグ
#define YES(c) cout<<((c)?"YES\n":"NO\n"),0
#define Yes(c) cout<<((c)?"Yes\n":"No\n"),0
#define O(p) cout<<p<<"\n",0
#define OS(p) cout<<p<<" ",0
#define ON(p) cout<<p,0
//定数
#define INF 1000000000000 //10^12:極めて大きい値,∞
#define inf 2147483647 //int値の最大値
#define MOD 1000000007 //10^9+7:合同式の法
#define MAXR 100000 //10^5:配列の最大のrange(素数列挙などで使用)
#define endl "\n"//endlを置き換える
//略記
#define PB emplace_back //vectorヘの挿入
#define MP make_pair //pairのコンストラクタ
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素
#define CST(x) cout<<fixed<<setprecision(x)//小数点以下の桁数指定

const int dy[4]={1,0,-1,0};
const int dx[4]={0,1,0,-1};//^>v<

template<class T=int> T in(){
    T x;
    cin>>x;
    return (x);
}
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 gcd(int a, int b) {
    if (b==0) return a;
    else return gcd(b, a%b);
}
int lcm(int a, int b) {
    return a * b / gcd(a, b);
}
int two(int n){
	ll ans=1;
	rep(i,n){
		ans*=2;
	}
	return ans;
}
//------------------------------------------------------------





//------------------------------------------------------------
int main() {
	ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
//------------------------------------------------------------
    int r,c;cin>>r>>c;
    //vector<vector<int>> Old(r ,vector<int>(c));
    vector<vector<int>> New(r+1,vector<int>(c+1));
    ll sum=0;
    rep(i,r){
        rep(j,c){
            cin>>New[i][j];
            sum+=New[i][j];
        }
    }
    New[r][c]=sum;
    rep(i,r){
        ll sum1=0;
        rep(j,c){
            sum1+=New[i][j];
        }
        New[i][c]=sum1;
    }
    rep(j,c){
        ll sum2=0;
        rep(i,r){
            sum2+=New[i][j];
        }
        New[r][j]=sum2;
    }
    rep(i,r+1){
        rep(j,c+1){
            if(j==c){
                O(New[i][j]);
            }else{
                OS(New[i][j]);
            }
        }
    }
//------------------------------------------------------------
	return 0;
}



Measured memory_bytes: 3244
import math
import fractions


N,X = map(int,input().split())

x = list(map(int,input().split()))

x_mi = []
x_pl = []

for i in x:

    if i-X < 0:
        x_mi.append(X - i)

    else:
        x_pl.append(i - X)

now_gcd = -1

for i in range(len(x_mi)):

    if now_gcd < 0:
        now_gcd = x_mi[i]

    else:
        now_gcd = fractions.gcd(now_gcd , x_mi[i])

for i in range(len(x_pl)):

    if now_gcd < 0:
        now_gcd = x_pl[i]

    else:
        now_gcd = fractions.gcd(now_gcd , x_pl[i])

print (now_gcd)

Measured memory_bytes: 16280
n,m=map(int,input().split())
potion=[]
for i in range(n):
    potion_R = list(map(int,input().split()))
    potion.append(potion_R)
potion.sort()
ans=0
number=m
for i in potion:
    if i[1]<=number:
        number-= i[1]
        ans+= i[0]*i[1]
    else:
        ans+= i[0]*number
        break
print(ans)

Measured memory_bytes: 27760
#include<bits/stdc++.h>
using namespace std;
const int N=200010,M=19,oo=1e9;
int n,a[N],maxp,pow2[M];
pair<int,int> st[2][N][M];
struct Node{
	int l,r,v,o;
	bool operator<(Node k)const{return v>k.v;}
	Node(int a=0,int b=0,pair<int,int> c=make_pair(0,0)){
		l=a,r=b,v=c.first,o=c.second;
	}
};
priority_queue<Node>q;
pair<int,int> que(int l,int r,bool b){
	int p=0,len=r-l+1;
	while((1<<p)<len) p++;
	p--;
	b=(l&1)?b:!b;
	pair<int,int> ans=st[b][l][p];
	if(st[b][r-pow2[p]+1][p].first<ans.first) ans=st[b][r-pow2[p]+1][p];
	return ans;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	while((1<<maxp)<n) maxp++;
	pow2[0]=1;
	for(int i=1;i<=maxp;i++) pow2[i]=2*pow2[i-1];
	for(int i=1;i<=n;i++){
		st[i&1][i][0]=make_pair(a[i],i);
		st[(i&1)^1][i][0]=make_pair(oo,i);
	}
	for(int i=0;i<=maxp;i++) st[0][n+1][i]=st[1][n+1][i]=make_pair(oo,n+1);
	for(int i=1;i<=maxp;i++){
		for(int j=1;j<=n;j++){
			if(st[0][j][i-1].first<st[0][min(j+pow2[i-1],n+1)][i-1].first) st[0][j][i].first=st[0][j][i-1].first,st[0][j][i].second=st[0][j][i-1].second;
			else st[0][j][i].first=st[0][min(j+pow2[i-1],n+1)][i-1].first,st[0][j][i].second=st[0][min(j+pow2[i-1],n+1)][i-1].second;
			if(st[1][j][i-1].first<st[1][min(j+pow2[i-1],n+1)][i-1].first) st[1][j][i].first=st[1][j][i-1].first,st[1][j][i].second=st[1][j][i-1].second;
			else st[1][j][i].first=st[1][min(j+pow2[i-1],n+1)][i-1].first,st[1][j][i].second=st[1][min(j+pow2[i-1],n+1)][i-1].second;
		}
	}
	q.push(Node(1,n,que(1,n,1)));
	int o1,o2;
	while(!q.empty()){
		Node x;
		x=q.top();q.pop();
		o1=x.o,o2=que(o1,x.r,0).second;
		printf("%d %d ",a[o1],a[o2]);
		if(o1>x.l) q.push(Node(x.l,o1-1,que(x.l,o1-1,1)));
		if(o1<o2-1) q.push(Node(o1+1,o2-1,que(o1+1,o2-1,1)));
		if(o2<x.r) q.push(Node(o2+1,x.r,que(o2+1,x.r,1)));
	}
	return 0;
}

Measured memory_bytes: 62452
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
  string s;
  cin >> s;
  int num_a = 0;
  int num_z = 0;
  bool find_a = false;
  bool find_z = false;
  
  for(int i = 0; i < s.size(); i++)
  {
    if(find_a == false && s.at(i) == 'A')
    {
      num_a = i;
      find_a = true;
    }
    if(find_a && s.at(i) == 'Z') 
    {
      num_z = i;
      find_z = true;
    }
  }
  if(find_z) cout << num_z - num_a + 1 << endl;
}

Measured memory_bytes: 640
N, K = map(int, input().split())
D = list(input().split())

for i in range(N, 10*N+1):
    s = str(i)
    for j in range(len(str(i))):
        if(s[j] in D):
            break
        if(j == len(str(i))-1):
            print(i)
            quit()

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

using namespace std;

/**
 * calculate GCD(greatest common divisor)
 * @param a
 * @param b
 * @return
 */
unsigned euclidean_gcd(unsigned a, unsigned b) {
    if (a < b) return euclidean_gcd(b, a);
    unsigned r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

/**
 * change minimum
 * @tparam T
 * @param a
 * @param b
 * @return bool
 */
template<class T>
inline bool changeMinimum(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

/**
 * change maximum
 * @tparam T
 * @param a
 * @param b
 * @return bool
 */
template<class T>
inline bool changeMaximum(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

/**
 * output answer
 * @tparam T
 * @param answer
 * @return
 */
template<typename T>
T output(T answer) {
    cout << answer << endl;
    return 0;
}

/** ----from here ---------------------------------------------------------- */

int main() {


    int N;
    cin >> N;
    vector<int> a(N - 1);

    for (int i = 0; i < N - 1; ++i) {
        cin >> a.at(i);
    }
    vector<int> s(N, 0);
    for (int i = 0; i < N - 1; ++i) {
        s.at((a.at(i)-1))++;
    }
    for (int i = 0; i < N; ++i) {
        cout << s[i] << endl;
    }


    return 0;
}

Measured memory_bytes: 4808
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)  
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define RVSORT(v) sort(v.rbegin(), v.rend());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999
#define MOD 1000000007
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

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

int gcd(int a,int b){return b?gcd(b,a%b):a;}

ll modpow(ll n,ll k){
  if(k == 1){
    return n;
  }else if(k == 0){
    return 1;
  }else if(k%2==0){
    ll memo = modpow(n,k/2)%MOD;
    return memo*memo%MOD;
  }else{
    return n * modpow(n,k-1)%MOD;
  }
}

int main(){

  ll n,k;cin>>n>>k;
  vector<ll> v;
  REP(i,n){
    ll l;cin>>l;
    v.pb(l);
  }
  VSORT(v);
  ll t = n-k;
  ll ans = 0;
  REP(i,t){
    ans += v[i];
  }

  cout<<ans<<endl;
 

  return 0;
}

Measured memory_bytes: 2420
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc=new Scanner(System.in);
        sc.next();
        String po=sc.next();
      String po2=po.replace("ABC","");
      System.out.println((po.length()-po2.length())/3);
    }
}


Measured memory_bytes: 23124
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author lstd
 */

#include <iostream>
#include <fstream>

#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <bitset>
#include <random>
#include <chrono>
#include <complex>
#include <algorithm>
#include <utility>
#include <functional>
#include <cmath>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <ctime>

using namespace std;

#define sqr(x) ((x)*(x))
#define mp make_pair
#define pb push_back
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define RE(i, n) FOR(i,1,n)
#define REP(i,n) FOR(i, 0, (int)(n)-1)
#define fst first
#define snd second

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

string to_string(string s) {
    return '"' + s + '"';
}

string to_string(const char* s) {
    return to_string((string) s);
}

string to_string(bool b) {
    return (b ? "true" : "false");
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
    return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A>
string to_string(A v) {
    bool first = true;
    string res = "{";
    for (const auto &x : v) {
        if (!first) {
            res += ", ";
        }
        first = false;
        res += to_string(x);
    }
    res += "}";
    return res;
}



void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << to_string(H);
    debug_out(T...);
}

int XMOV[4] = {0, 0, 1, -1};
int YMOV[4] = {1, -1, 0, 0};

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

using namespace std;
#define MOD 1000000007
#define MULT(a,b) (((a%MOD)*(b%MOD))%MOD)

ll f[2005], inv[2005], pows[2005][2005];

ll bpow(ll a, ll b){
	if (b == 0)
		return 1;
	else if ( b % 2 == 0 )
		return bpow((MULT(a, a)), b/2)%MOD;
	else
		return MULT(bpow(a, b-1), a);
}

ll minv(ll a){
	return bpow(a, MOD-2);
}

ll fact(ll x) {
	if (x == 1 || x == 0)
		return 1;
	else if (f[x] != -1)
		return f[x];
	else
		return f[x] = MULT(x, fact(x-1));
}

ll c(ll n, ll m){
	if (n < 0 || m < 0 || n < m) return 0;
	//n!/(n-m)!m!
	ll a, b, c;
	a = fact(n);
	b = minv(fact(n-m));
	c = minv(fact(m));
	return MULT(a, MULT(b, c));
}

void init(){
	memset(f, -1, sizeof(f));
	memset(pows, -1, sizeof(pows));
	fact(2005);
	for(int i = 1; i < 2005; i++)
		inv[i] = minv(i);
}



class DBlueAndRedBalls {
public:
	void solve(std::istream& in, std::ostream& out) {
		int k, n;
		init();
		in >> n >> k;
		for (int i = 1; i <= k; i++){
			ll bucket_ways = c(n-k+1, i);
			ll blue_balls_arrangements = c(k-1, i-1);
			out << (i == 1 ? c(n-k+1, i) : MULT(bucket_ways, blue_balls_arrangements)) << "\n";
		}


	}
};

int main() {
	DBlueAndRedBalls solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}

Measured memory_bytes: 31744
#include <bits/stdc++.h>
#pragma GCC optimize ("O2,unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=998244353;
const int N=405;

ll n, m, k, u, v, x, y, t, a, b, ans, sum;
ll A[N], B[N];
ll F[N], I[N];
ll dp[N][N][N];

ll powmod(ll a, ll b){
	ll res=1;
	for (; b; b>>=1, a=a*a%mod) if (b&1) res=res*a%mod;
	return res;
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	F[0]=1;
	for (int i=1; i<N; i++) F[i]=F[i-1]*i%mod;
	I[N-1]=powmod(F[N-1], mod-2);
	for (int i=N-1; i; i--) I[i-1]=I[i]*i%mod;
	cin>>n;
	for (int i=1; i<=n; i++) cin>>A[i]>>B[i], sum+=A[i];
	dp[0][0][0]=-1;
	int sumb=0;
	for (int i=1; i<=n; i++){
		ll p=A[i]*powmod(sum, mod-2)%mod;
		sumb+=B[i]-1;
		for (int sa=0; sa<=sum; sa++){
			for (int sx=0; sx<=sumb; sx++){
				int lim=min(sx, (int)B[i]-1);
				ll tmp=0, mul=1;
				if (sa>=A[i]){
					for (int j=0; j<=lim; j++){
						tmp=(tmp + dp[i-1][sa-A[i]][sx-j]*mul%mod*I[j])%mod;
						mul=mul*p%mod;
					}
				}
				dp[i][sa][sx]=(dp[i-1][sa][sx] - tmp)%mod;
			}
		}
	}
	for (int sa=1; sa<=sum; sa++) for (int sx=0; sx<=sumb; sx++){
		ll val=sa*powmod(sum, mod-2)%mod;
		val=powmod(val, mod-1-sx-1)*F[sx]%mod;
		ans=(ans + dp[n][sa][sx]*val)%mod;
	}
	if (ans<0) ans+=mod;
	cout<<ans<<"\n";
	
	return 0;
}


Measured memory_bytes: 511484
// problem: "https://atcoder.jp/contests/abc140/tasks/abc140_b"

#include<bits/stdc++.h>

using namespace std;

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

    vector<int>A,B,C;
    B.push_back(-1);
    C.push_back(-1);

    int i,x;

    for(i=0;i!=n;i++)
    {
        cin>>x;
        A.push_back(x);
    }

    for(i=0;i!=n;i++)
    {
        cin>>x;
        B.push_back(x);
    }

    for(i=0;i!=n-1;i++)
    {
        cin>>x;
        C.push_back(x);
    }

    int s = B[A[0]];
    for(i=1;i!=n;i++)
    {
        s += B[A[i]];

        if(A[i] == A[i-1]+1)
            s += C[A[i-1]];
    }

    cout<<s;

    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin>>n;
  vector<int> a(n);
  for(int i=0;i<n;i++)
    cin>>a.at(i);
  int c=0;
  for(int i=2;i<n;i++)
    if((a[i-2]<a[i-1]&&a[i-1]<a[i])||(a[i-2]>a[i-1]&&a[i-1]>a[i]))
      c++;
  cout<<c<<endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define MOD 1000000007
#define endl "\n"
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(var) (var).begin(), (var).end()
#define MAXX 9000000000000000000
#define pii pair<int,int>
#define vi vector<int>
#define vvi vector<vector<int> >
#define vpi vector<pair<int,int> > 
#define vvpi vector<vector<pair<int,int> > >

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif

	int n,i,j,x,y,fre[200005];
	string s;
	cin >> n >> s;
	if(s[0] == 'W')
	{
		fre[0] = 0;
	}
	else
	{
		fre[0] = 1;
	}
	for(i=1;i<n;i++)
	{
		if(s[i] == 'W')
		{
			fre[i] = fre[i-1];
		}
		else
		{
			fre[i] = fre[i-1] + 1;
		}
	}

	if(fre[n-1] == 0)
	{
		cout << 0;
		return 0;
	}
	cout << fre[n-1] - fre[fre[n-1]-1];

	return 0;
}

Measured memory_bytes: 5332
using System;
using System.Linq;

namespace AizuOnlineJudgement.ITP1_2_C
{
	public static class Program
	{
		public static void Main (string[] args)
		{
			var abc = Console.ReadLine ().Split (' ').Select(s => int.Parse (s)).ToArray ();
			Array.Sort (abc);
			Console.WriteLine (abc[0] + " " + abc[1] + " " + abc[2]);
		}
	}
}



Measured memory_bytes: 9700
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            int n = sc.nextInt();
            if (n == 0) {
                break;
            }
            PriorityQueue<Integer> s = new PriorityQueue<>();
            for (int i = 0; i < n; i++) {
                s.add(sc.nextInt());
            }
            s.poll();
            int p = 0;
            while (s.size() > 1) {
                p += s.poll();
            }
            System.out.println(p / (n - 2));
        }
    }
}

Measured memory_bytes: 29412
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int h=5000;
char s[5050],t[5050];
int q1[20202],q2[20202];
bool tp1[20202],tp2[20202];
int n;
int jud(int p1,int p2)
{
	int ret=0;
	for(int i=p1,j=p2;q1[i]!=1||q2[j]!=1;i--,j--)
		ret+=abs(q1[i]-q2[j]);
	for(int i=p1+1,j=p2+1;q1[i]!=n+1||q2[j]!=n+1;i++,j++)
		ret+=abs(q1[i]-q2[j]);
	return ret;
}
int main()
{
	int cnt1,cnt2,ans=214748364;
	scanf("%d",&n);
	scanf("%s",s+1);
	scanf("%s",t+1);
	cnt1=cnt2=h;
	for(int i=2;i<=n;i++)
		if(s[i]!=s[i-1])
		{
			q1[++cnt1]=i;
			tp1[cnt1]=s[i]-'0';
		}
	for(int i=2;i<=n;i++)
		if(t[i]!=t[i-1])
		{
			q2[++cnt2]=i;
			tp2[cnt2]=t[i]-'0';
		}
	q1[h]=q2[h]=1;
	tp1[h]=s[1]-'0';
	tp2[h]=t[1]-'0';
	for(int i=h-1;i>=0;i--)
	{
		q1[i]=q2[i]=1;
		tp1[i]=tp1[i+1]^1;
		tp2[i]=tp2[i+1]^1;
	}
	for(int i=cnt1+1;i<=cnt1+h+1;i++)
	{
		q1[i]=n+1;
		tp1[i]=tp1[i-1]^1;
	}
	for(int i=cnt2+1;i<=cnt2+h+1;i++)
	{
		q2[i]=n+1;
		tp2[i]=tp2[i-1]^1;
	}
	for(int i=h+h-cnt1-1;i<=cnt2+1;i++)
		if(tp1[h]==tp2[i])
			ans=min(ans,jud(h,i));
	printf("%d",ans);
	return 0;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
#define mod 1000000007
#define pb push_back
#define ll long long
#define mp make_pair
using namespace std;
int main(){
	vector<ll> a,b,ex;
	ll n,asum=0,bsum=0,def=0,cnt=0;
	cin>>n;
	for(int i=0;i<n;i++){
		ll x;
		cin>>x;
		asum+=x;
		a.pb(x);
	}
	for(int i=0;i<n;i++){
		ll x;
		cin>>x;
		bsum+=x;
		b.pb(x);
	}
	if(bsum>asum){
		cout<<-1<<endl;
		return 0;
	}
	else{
		cnt = 0;
		for(int i=0;i<n;i++){
			if(a[i]<b[i]){
				cnt++;
				def += b[i]-a[i];
			}
			else{
				ex.pb(a[i]-b[i]);
			}
		}
		sort(ex.begin(),ex.end());
		for(int i=0;i<ex.size();i++){
			if(def>0){
				cnt++;
				def-=ex[ex.size()-1-i];
			}
		}
	}
	cout<<cnt<<endl;
	return 0;
}

Measured memory_bytes: 3316
#include <bits/stdc++.h>
using namespace std;
int main() {
	long int x, y; cin >> x >> y;
	long int n = y;
	int count = 0;
	for(int i=1;i<=100;i++){
		while(n>=x){
			n = n/2;
			count++;
		}
	}
	cout << count << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cmath>
#include <vector>
#include <limits>
#include <algorithm>
#include <functional>
using namespace std;

#define REP( i, n )	for( int (i) = 0; (i) < (n); (i)++ )
#define ALL( x )	(x).begin(), (x).end()

int main() {
	vector<int> m(10);
	REP( i, 10 ) cin >> m[i];

	sort( ALL(m), greater<int>() );
	cout << m[0] << endl << m[1] << endl << m[2] << endl;
	
}


Measured memory_bytes: 3152
import sys
sys.setrecursionlimit(10 ** 9)

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.root = [-1]*(n+1)
        self.rank = [0]*(n+1)

    def find(self, x):#親となる要素を探索
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find(self.root[x])#再帰
            return self.root[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return

        elif self.rank[x] > self.rank[y]:#深い木に連結
            self.root[x] += self.root[y]
            self.root[y] = x#yの親をxとする

        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

    def issame(self, x, y):#x, yが同じ集合か判定
            return self.find(x) == self.find(y)


    def count(self, x):#要素の個数
        return (-1)*self.root[self.find(x)]

n, m = map(int, input().split())
uf = UnionFind(n)

for i in range(m):
    a, b = map(int, input().split())
    uf.unite(a-1, b-1)

ans = set()

for i in range(n):
    ans.add(uf.find(i))

print(len(ans)-1)

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

int main(){
	int x1, x2, x3, x4, x5;
	scanf("%d %d %d %d %d", &x1, &x2, &x3, &x4, &x5);
	if(x1==0)
		printf("1");
	else if(x2==0)
		printf("2");
	else if(x3==0)
		printf("3");
	else if(x4==0)
		printf("4");
	else if(x5==0)
		printf("5");
	printf("\n");
	return 0;	
}

Measured memory_bytes: 1616