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

using namespace std;

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

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

// typedef
typedef long long llint;
typedef unsigned long long ull;
typedef complex<long double> Complex;
typedef pair<int, int> P;
typedef tuple<int, int, int> TP;
typedef vector<int> vec;
typedef vector<vec> mat;

// constant
const long long MOD = (long long)1e9 + 7;
const int INF = (int)1e9;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};
const double pi = 3.14159265358979323846;

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

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

/* べき乗(mのn乗)を計算する関数. O(logn) */
template <typename T>
T modpow(T m, T n, const T& p) {
  if (n == 0) return 1;
  T ret = modpow(m * m % p, n / 2, p);
  if (n % 2) ret *= m;
  return ret % p;
}
/* フェルマーの小定理を使って二項係数を計算. O(r) */
template <typename T>
T binomial(T n, T r, const T& p) {
  if (r > n - r) return binomial(n, n - r, p);
  T ansMul = 1;
  T ansDiv = 1;
  for (T i = 0; i < r; i++) {
    ansMul *= (n - i);
    ansDiv *= (i + 1);
    ansMul %= p;
    ansDiv %= p;
  }
  // ansDivの逆元
  T ans = (ansMul * modpow(ansDiv, p - 2, p)) % p;
  return ans;
}

llint fact[202020];

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

  llint n, k;
  cin >> n >> k;
  k = min(n - 1, k);

  fact[0] = 1;
  for (llint i = 1; i <= n; i++) {
    fact[i] = fact[i - 1] * i % MOD;
    fact[i] %= MOD;
  }

  llint ans = 0;
  for (llint i = 0; i <= k; i++) {
    llint x = fact[n];
    x *= modpow(fact[i], MOD - 2, MOD), x %= MOD;
    x *= modpow(fact[n - i], MOD - 2, MOD), x %= MOD;
    x *= fact[n - 1], x %= MOD;
    x *= modpow(fact[i], MOD - 2, MOD), x %= MOD;
    x *= modpow(fact[n - 1 - i], MOD - 2, MOD), x %= MOD;
    ans += x, ans %= MOD;
  }
  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 1792
#include<cstdio>
#include<queue>
#include<utility>

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 400;

const int di[] = {1, 0, -1, 0};
const int dj[] = {0, 1, 0, -1};

bool color[MAX_N][MAX_N];
int H, W;

char ch[MAX_N + 1];

void input(){
	scanf("%d%d", &H, &W);
	for(int i = 0; i < H; ++i){
		scanf("%s", ch);
		for(int j = 0; j < W; ++j){
			color[i][j] = (ch[j] == '#');
		}
	}
}

bool isin_(int i, int j){
	return i >= 0 && i < H && j >= 0 && j < W;
}

bool check_(int i, int j, int d){
	int ni = i + di[d];
	int nj = j + dj[d];
	if(!isin_(ni, nj)) return false;
	if(color[i][j] == color[ni][nj]) return false;
	return true;
}

bool used[MAX_N][MAX_N];

long long solve(){
	long long ans = 0;
	long long b = 0, w = 0;
	for(int i = 0; i < H; ++i){
		for(int j = 0; j < W; ++j){
			if(used[i][j]) continue;
			b = 0;
			w = 0;
			queue<P> que;
			used[i][j] = true;
			que.push(P(i, j));
			while(!que.empty()){
				P p = que.front();
				que.pop();
				int ci = p.first, cj = p.second;
				if(color[ci][cj]) b++;
				else w++;
				for(int d = 0; d < 4; ++d){
					if(!check_(ci, cj, d)) continue;
					int ni = ci + di[d], nj = cj + dj[d];
					if(used[ni][nj]) continue;
					used[ni][nj] = true;
					que.push(P(ni, nj));
				}
			}
			ans += b * w;
		}
	}
	return ans;
}

int main(){
	input();
	long long ans = solve();
	printf("%lld\n", ans);
	return 0;
}


Measured memory_bytes: 512
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int A[] = new int[N];
		for(int i=0;i<N;i++) A[i]=sc.nextInt();
		
		int cnt = 0;
		
		boolean allplus = false;
		for(int i=0;i<N;i++) {
			if(A[i]==0) {
				allplus=true;
				break;
			}
			if(A[i]<0) cnt ++;
		}
		
		
		for(int i=0;i<N;i++) {
			A[i] = Math.max(A[i], -A[i]);
		}
		
		int min = Integer.MAX_VALUE;
		for(int i=0;i<N;i++) {
			min = Math.min(min, A[i]);
		}

		
		long ans = 0;
		for(int tmp:A) {
			ans += tmp;
		}
		
		if(allplus);
		else{
			if(cnt%2==0) {
				//全部プラス
			}else {
				//evenの中で絶対値が最小のもののみマイナスにする。
				ans -= min*2;
			}
		}
		System.out.println(ans);
	}
}


Measured memory_bytes: 50388
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		while (true) {
			String str = br.readLine();
			if (str.equals("0")) break;
			int sum = 0;
			for (int i = 0; i < str.length(); i++) {
				sum += Character.getNumericValue(str.charAt(i));
			}
			System.out.println(sum);
		}
	}
}

Measured memory_bytes: 24248
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
 
public class Main {
 
    void solve() throws IOException {
        int N = ni();
        int sb = 0;
        int ea = 0;
        int sbea = 0;
        int ans = 0;
        for (int i = 0; i < N; i++) {
            String s = ns();
            if(s.startsWith("B") && (!s.endsWith("A"))) sb++;
            if(s.endsWith("A") && (!s.startsWith("B"))) ea++;
            if(s.startsWith("B")&&s.endsWith("A")) sbea++;
            for (int j = 0; j < s.length()-1; j++) {
                if(s.substring(j,j+2).equals("AB")) ans++;
            }
        }
        if(sbea==0){
            if(sb+ea>1)ans += Math.min(sb,ea);
        }else{
            if(sb==0 && ea==0) ans += sbea-1;
            else ans += sbea + Math.min(sb,ea);
        }
        out.println(ans);
    }

    final int mod = 1000000007;
    int mul(int x, int y){
        return (int)((x * 1L * y) % mod);
    }
    int add(int x, int y) {
        x += y;
        return x >= mod ? (x - mod) : x;
    }
    int sub(int x, int y){
        x = add(x,mod-y);
        return x >= mod ? (x - mod) : x;
    }
    String ns() throws IOException {
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine(), " ");
        }
        return tok.nextToken();
    }
 
    int ni() throws IOException {
        return Integer.parseInt(ns());
    }
 
    long nl() throws IOException {
        return Long.parseLong(ns());
    }
 
    double nd() throws IOException {
        return Double.parseDouble(ns());
    }
 
    String[] nsa(int n) throws IOException {
        String[] res = new String[n];
        for (int i = 0; i < n; i++) {
            res[i] = ns();
        }
        return res;
    }
 
    int[] nia(int n) throws IOException {
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            res[i] = ni();
        }
        return res;
    }
 
    long[] nla(int n) throws IOException {
        long[] res = new long[n];
        for (int i = 0; i < n; i++) {
            res[i] = nl();
        }
        return res;
    }
 
    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok;
 
    public static void main(String[] args) throws IOException {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        tok = new StringTokenizer("");
        Main main = new Main();
        main.solve();
        out.close();
    }
}

Measured memory_bytes: 39812
#include <bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    vector<vector<int>> ans(101,vector<int>(101,0));

    for(int i=0;i<n;i++){
        int a,b,k;
        cin>>a>>k;
        for(int j=0;j<k;j++){
            cin>>b;
            ans[a][b]=1;
        }
    }

    for(int i=1;i<n+1;i++){
        for(int j=1;j<n+1;j++){
            if(j-1) cout<<' ';
            cout<<ans[i][j];
        }
        cout<<endl;
    }
}


Measured memory_bytes: 3140
import java.util.Scanner;

public class Main {

	public static void main(String args[]){

		Scanner cin = new Scanner(System.in);

		long target = cin.nextLong();
		long tmp = 0L;

		for(long i =0L;i<=target;i++){
			if(i*(i+1)>=2*target){
				tmp =i;
				break;
			}
		}
		System.out.println(tmp);
	}
}


Measured memory_bytes: 23636
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define print(x) cout << (x) << endl
typedef long long ll;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;

using namespace std;

int main(){
    ll n, m;   cin >> n >> m;
    if(2*n > m) print(m/2);
    else{
        ll ans = n;
        m -= 2*n;
        ans += m/4;
        print(ans);
    }
}

Measured memory_bytes: 3584
import java.util.*;
import java.io.*;
public class Main{
  public static void main(String agrs[])
  {
    Scanner in = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);
    int n = in.nextInt();
    int a = in.nextInt();
    out.println(n * a);
    out.close();
  }
}

Measured memory_bytes: 35752
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> cnt(N + 1, 0);
  for (int i = 0; i < N; ++i) {
    int ai;
    cin >> ai;
    ++cnt[ai];
  }
  sort(cnt.rbegin(), cnt.rend());
  int result = accumulate(cnt.begin() + K, cnt.end(), 0);
  cout << result << endl;
}

Measured memory_bytes: 1024
import sys
import math
import random
input = sys.stdin.readline


def readstr():
    return input().strip()


def readint():
    return int(input())


def readnums():
    return map(int, input().split())


def readstrs():
    return input().split()


A, B, N = readnums()

'''

x = B - 1のとき最大？
f(A(B - 1) / B) = f(A - A/B)
A*f((B-1)/B) = 0

x = 2B - 1のとき
f(A(2B - 1) / B) = f(2A - A/B)
A*f((2B-1)/B) = A*f(2 - 1/B) = A

A <= Bのとき
x = B - 1のとき最大で、その値はA - 1

A > Bのとき
f(A - A/B) = A - A // B - 1
f(2A - A/B) - A = A - A//B - 1

'''

# test
# test = []
# N = random.randint(1, 10 ** 6)
# A = random.randint(1, 10 ** 6)
# B = random.randint(1, 10)
# print(A, B, N)
# for x in range(1, N + 1):
#     test.append(int(A * x / B) - A * int(x / B))

# print(max(test))

if A <= B:
    if N >= B - 1:
        print(A - 1)
    else:
        print(int(A * N / B))
else:
    if N >= B - 1:
        print(A - A // B - 1) if B != 1 else print(0)
    else:
        print(int(A * N / B))


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

using namespace std;

const int N=1e6+3;

#define pb push_back
#define all(x) (x).begin(),(x).end()
typedef long long ll;

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

const int inf=1e9;

int n,m,a[N],b[N],k,q,u,v,x,y,tc,c,l,r,ans,sz[N];

char s[N];

pair<int,int> p[N];

int main(){
	
	scanf("%d",&n);
	for(int i=0;i<n;i++)
		scanf("%d %d",&p[i].first,&p[i].second);
	double avg=0;
	int cnt=0;
	sort(p,p+n);
	do{
		cnt++;
		for(int i=1;i<n;i++){
			double x=p[i].first-p[i-1].first;
			double y=p[i].second-p[i-1].second;
			avg+=(double)sqrt(x*x + y*y);
		}
	}while(next_permutation(p,p+n));
	printf("%.9lf\n",avg/cnt);
}


Measured memory_bytes: 256
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cstdlib>
#include<string>
#include<bitset>
#include<iomanip>
#include<deque>
#include<utility>
#include<functional>
#include<sstream>
#include<ctime>
#define INF 1000000000
#define fi first
#define se second
#define N 210005
#define P 1000000007
#define debug(x) cerr<<#x<<"="<<x<<endl
#define MP(x,y) make_pair(x,y)
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
char s[N];
int pre[N],last[27];
int dp[N], nxt[N];
int main()
{
    int n, i, j, m;
    dp[0] = 0;
    scanf("%s", s + 1);
    n = strlen(s + 1);
    m = n;
    n += 26;
    for (i = m + 1; i <= n; i++)
        s[i] = 96 + i - m;
    for (i = 1; i <= n; i++)
    {
        dp[i] = INF;
        for (j = i - 1; j >= 0; j--)
        {
            dp[i] = min(dp[i], dp[j] + 1);
            if (s[i] == s[j])
                break;
        }
        //debug(dp[i]);
    }
    int ans = INF;
    for (i = n; i > m; i--)
        ans = min(ans, dp[i]);
    //debug(ans);
    for (i = n; i; i--)
    {
        if (i > m&&dp[i] != ans)
            continue;
        if (i <= m && nxt[i] == 0)
            continue;
        for (j = i - 1; j >= 0; j--)
        {
            if (dp[i] == dp[j] + 1)
            {
                if (!nxt[j])
                    nxt[j] = i;
                else if (s[nxt[j]] > s[i])
                    nxt[j] = i;
            }
            if (s[i] == s[j])
                break;
        }
    }
    //debug(nxt[0]);
    //debug(s[23]);
    vector<char> rel;
    for (int x = nxt[0]; x; x = nxt[x])
        rel.push_back(s[x]);
    for (auto x : rel)
        printf("%c", x);
    cout << endl;
    return 0;
}
// davidlee1999WTK 2017/
// ios::sync_with_stdio(false);
//#pragma comment(linker, "/STACK:102400000,102400000") compiler c++,not g++
//#include<bits/stdc++.h>
/*

*/

Measured memory_bytes: 2048
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <string>
using namespace std;

int main(){
    string S;
    cin >> S;
    long long cnt = 0, ret = 0;
    for (int i=0;i<S.size();i++){
        if (S[i] == 'W'){
            ret += cnt;
        }
        else {
            ++cnt;
        }        
    }
    cout << ret;
    return 0;
}


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

namespace Curtain
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());
            var takoyakis = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();

            int total = 0;

            for (int i = 0; i < takoyakis.Length; i++)
            {
                for (int j = i + 1; j < takoyakis.Length; j++)
                {
                    total += takoyakis[i] * takoyakis[j];
                }
            }

            Console.WriteLine(total);
        }
    }
}


Measured memory_bytes: 13396
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int N; cin >> N;
  vector<ll> a(3 * N);
  for(int i = 0; i < 3 * N; i++) cin >> a.at(i);
  sort(a.begin(), a.end(), greater<ll>());
  ll sum = 0;
  for(int i = 0; i < 2 * N; i++) {
    if(i % 2 == 1) {
      sum += a.at(i);
    }
  }
  cout << sum << endl;
}


Measured memory_bytes: 2560
import sys
import numpy as np
# for AtCoder
import fractions
from collections import deque 
from functools import reduce

n, k = map(int, input().split())

mi = n
mo = n % k

if mi > mo:
    mi = mo
if mi > abs(mo - k):
    mi = abs(mo - k)    
print(mi)    

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

typedef vector<int> vi;
typedef vector<vi> vvi;
 
vi combine (const vi &l, const vi &r){
	if (l.empty() || r.empty())
		return l.empty() ? r : l;
	vi res(l.size() + r.size());
	merge(l.begin(), l.end(), r.begin(), r.end(), res.begin(), greater <int>());
	return res;
}
 
vi dfs (int x, const vvi &g, vector<int> &used){
	used[x] = 1;
	vi res;
	for (int i = 0; i < g[x].size(); i++){
		int v = g[x][i];
		if (used[v]) continue;
		res = combine(res, dfs(v, g, used));
	}
	res.insert(res.begin(), x);
	return res;
}

int main(){
	int n;
	scanf("%d", &n);
	vi a(n);
	for (int i = 0; i < n; i++){
		scanf("%d", &a[i]);
	}
	sort(a.begin(), a.end());
	vvi g(n);
	for (int i = 0; i < n; i++){
		for (int j = 0; j < i; j++){
			if (__gcd(a[i], a[j]) != 1){
				g[i].push_back(j), g[j].push_back(i);
			}
		}
	}
	for (int i = 0; i < n; i++){
		sort(g[i].begin(), g[i].end());
	}
	vi res;
	vi used(n);
	for (int i = 0; i < n; i++){	
		if (!used[i]) res = combine(res, dfs(i, g, used));
	}
	for (int i = 0; i < n; i++){
		printf("%d ", a[res[i]]);
	}
	printf("\n");
}


Measured memory_bytes: 17024
import java.util.*;
import java.lang.*;
import java.math.*;


public class Main {
	
	Scanner sc = new Scanner(System.in);
	
	void run() {
		
		int n ;
		int count = 0;
		
		while( sc.hasNext() ) {
			
			n = sc.nextInt();
			count = 0;
			
			for (int i=0;i<10;i++) {
				for (int j=0;j<10;j++) {
					for (int k=0;k<10;k++) {
						for (int l=0;l<10;l++) {
							
							if ( n == i+ j + k + l  )
								count++;
							
						}
					}
				}
			}
			
			System.out.println(count);
			
		}
		
	}
	
	public static void main(String[] args) {
		
		new Main().run();
		
	}
	
}

Measured memory_bytes: 12000
#include<stdio.h>
#include<stdlib.h>

int main(){
  long int a,kotae=1000;
  scanf("%ld",&a);
  for(;a>99;a=a/10){
    if(abs(a%1000-753)<kotae){
      kotae=abs(a%1000-753);
    }
  }
  printf("%ld",kotae);
  return 0;
}


Measured memory_bytes: 128
N = int(input())
A_list = [int(i) for i in input().split()]
M_list = []

M_list.append(sum(A_list)- 2 * sum(A_list[1::2])) 
#print(M_list[0])

for i in range (0,N-1):
         M_list.append(2 * A_list[i] - M_list[i])


print(*M_list)

Measured memory_bytes: 69348
#include<stdio.h>

int main(void)
{
  int  h, w, i, j, p[100][101];

  scanf("%d %d", &h, &w);

  for(i=0; i<h; i++) {
    scanf("%s", &p[i][0]);
  }

  for(j=0; j<w+2; j++){
    printf("#");
  }
  printf("\n");
  for(i=0; i<h; i++) {
    printf("#%s#\n", p[i]);
  }
  for(j=0; j<w+2; j++){
    printf("#");
  }
  printf("\n");

  return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <algorithm>
int main(void){
    int n,m;m=0;
    scanf("%d",&n);
    int l[n+1];
    for(int i=0;i<n;i++){
        scanf("%d",l+i);
    }
    std::sort(l,l+n);
    l[n]=5000;
    int a,b,c;
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            a=j;b=n;
            while(b-a>1){
                c=a+b;
                c/=2;
                if(l[c]<l[i]+l[j]){
                    a=c;
                }
                else b=c;
            }
            m+=a-j;
        }
    }
    printf("%d\n",m);
}

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

int main() {
  int n, k;
  cin >> n >> k;
  
  int dist = 0;
  int x;
  for(int i = 0; i < n; i++) {
    cin >> x;
    dist += 2 * (int)min(x - 0, k - x);
  }
  
  cout << dist << endl;
}


Measured memory_bytes: 256
X, K, D = gets.chomp.split(' ').map(&:to_i)

divided = X.abs / D

if divided > K then
    puts X.abs - D * K
else
    x = X.abs - D * divided
    k = K - divided
    #puts "x = #{x}, k = #{k}"
    if k.even? then
        puts x
    else
        puts (x - D).abs
    end
end


Measured memory_bytes: 14272
#include<cstdio>
struct edge{
	int fr,to,w;
}e[65];
int main(){
	int l,n=0,m=0,maxi=0;
	scanf("%d",&l);
	while((1<<n)-1<l) ++n; --n;
	while((1<<maxi)<=l) ++maxi; --maxi; 
	printf("%d ",n+1);
	for(int i=1;i<=n;i++){
		e[++m]=(edge){i,i+1,0};
		e[++m]=(edge){i,i+1,(1<<(n-i))};
	}
	int cnt=0;
	//e[++m]=(edge){1,n+1,l};
	for(int i=0;i<maxi;i++){
		if(l&(1<<i)){
			e[++m]=(edge){1,n-i+1,l-(1<<i)};
			l-=(1<<i);
		}
	}
	printf("%d\n",m);
	for(int i=1;i<=m;i++)
		printf("%d %d %d\n",e[i].fr,e[i].to,e[i].w);
	return 0;
}

Measured memory_bytes: 128
#include<stdio.h>

 int A[50];

 int fib(int n){
      int i;
      A[0]=A[1]=1;
      for(i=2;i<=n;i++){
            A[i]=A[i-1]+A[i-2];
            }
      return A[n];
      }


 int main(){
      int n;
      scanf("%d",&n);
      int k=fib(n);
      printf("%d\n",k);
      return 0;
     }



Measured memory_bytes: 2052
unittest
{
	assert( [ "1 1 2 2" ].parse.expand.solve == 14 );
	assert( [ "314 159 2653 589" ].parse.expand.solve == 602215194 );
}



import std.conv;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto rcrc = input.front.split.to!( long[] );
	return tuple( rcrc[ 0 ], rcrc[ 1 ], rcrc[ 2 ], rcrc[ 3 ] );
}

auto solve( long r1, long c1, long r2, long c2 )
{
	// 二項係数の二次元累積和表は、2つの一次元累積和表で代替できる
	
	// 範囲の一つ外が欲しいので減らす
	r1--;
	c1--;
	
	// 累積和表を作成
	auto r1s = solve2( r1, c2 );
	auto r2s = solve2( r2, c2 );
	
	// 範囲内の総和を計算
	return r2s[ c2 ] - r2s[ c1 ] - r1s[ c2 ] + r1s[ c1 ];
}

auto solve2( long r, long c )
{
	auto ts = new ModWorld!()[ 2 + c ];
	foreach( i; 1L .. ts.length.signed )
	{
		ts[ i ] = ts[ i - 1 ] + ( r + i ).combination( i );
	}
	ts.popFront;
	return ts;
}

// モジュラ整数型（n mod M）
struct ModWorld( long M = 10L ^^ 9 + 7 )
{
	long n;
	
	// 演算子オーバーロード
	void opUnary( string OP )() if( OP == "++" ){ this.n = ( this.n + 1 ) % M; }
	void opUnary( string OP )() if( OP == "--" ){ this.n = ( this.n + M - 1 ) % M; }
	void opOpAssign( string OP )( ModWorld r ) if( OP == "+"  ){ this.n = ( this.n + r.n ) % M; }
	void opOpAssign( string OP )( ModWorld r ) if( OP == "-"  ){ this.n = ( this.n + M - r.n ) % M; }
	void opOpAssign( string OP )( ModWorld r ) if( OP == "*"  ){ this.n = ( this.n * r.n ) % M; }
	void opOpAssign( string OP )( ModWorld r ) if( OP == "/"  ){ this *= r ^^ ModWorld( M - 2 ); }
	void opOpAssign( string OP )( ModWorld r ) if( OP == "^^" )
	{
		auto a = this.n;
		this.n = 1L;
		for( auto i = r.n; 0 < i; i >>= 1 )
		{
			if( i & 1 ) this.n = this.n * a % M;
			a = a ^^ 2 % M;
		}
	}
	auto opBinary( string OP )( ModWorld r )
	{
		auto t = this;
		t.opOpAssign!OP( r );
		return t;
	}
	
	// long型対応
	void opAssign( long r ){ this.n = r; }
	void opOpAssign( string OP )( long r ){ this.opOpAssign!OP( ModWorld( r ) ); }
	auto opBinary( string OP )( long r ){ return this.opBinary!OP( ModWorld( r ) ); }
	auto opBinaryRight( string OP )( long l ){ return ModWorld( l ).opBinary!OP( this ); }
	auto opEquals( long r ){ return this.n == r; }
	
	// format用出力
	string toString()
	{
		return this.n.to!string;
	}
}

// 階乗
auto factorial( long M = 10L ^^ 9 + 7 )( long x )
{
	static memo = [ ModWorld!M( 1L ) ];
	if( memo.length <= x )
	{
		auto i = memo.length.signed;
		memo.length = x + 1;
		for( ; i <= x; i++ ) memo[ i ] = memo[ i - 1 ] * i;
	}
	return memo[ x ];
}

// 組み合わせ数
auto combination( long M = 10L ^^ 9 + 7 )( long n, long k )
{
	if( n < k ) return ModWorld!M( 0L );
	else        return n.factorial!M / ( k.factorial!M * ( n - k ).factorial!M );
}


Measured memory_bytes: 54012
#include <bits/stdc++.h>
#include <iomanip>
#include <math.h>
#include <unistd.h>
#include <stdio.h>
#define rep(i,x) for(ll i = 0;i<x;i++)
#define all(a) (a).begin(),(a).end()
#define vecin(a) rep(i,a.size())cin >> a[i]
#define vecout(a) rep(i,a.size())cout << a[i] << " ";cout << "\n"
using ll = long long;
using ld = long double;
using namespace std;
using dou = double;
const ll inf = 2147483647;
const ll INF = 1LL << 60;
const ld pi = 3.14159265358;
const ll mod = 1000000007;
typedef pair<ll,ll> P;
using graph = vector<vector<ll>>;
template<class T> inline bool chmax(T& a, T b){if(a < b){a = b; return true;}return false;}
template<class T> inline bool chmin(T& a, T b){if(a > b){a = b; return true;}return false;}
template<class T> inline bool change(T a,T b){if(a > b){swap(a,b);return true;}return false;}
ll gcd(ll a,ll b){
    if(a < b)swap(a,b);
    if(a % b == 0)return b;
    else return gcd(b,a%b);
}
//素数判定O(sqrt(N))
bool isp(ll n){
    bool res = true;
    if(n == 1)return false;
    else{
        for(ll i = 2;i * i <= n;i++){
            if(n % i == 0){
                res = false;
                break;
            }
        }
        return res;
    }
}
const ll cmax = 1000000;
ll fac[cmax], finv[cmax], inv[cmax];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (ll i = 2; i < cmax; i++){
        fac[i] = fac[i - 1] * i % mod;
        inv[i] = mod - inv[mod%i] * (mod / i) % mod;
        finv[i] = finv[i - 1] * inv[i] % mod;
    }
}

// 二項係数計算
ll nCk(ll n, ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
//nのm乗をMODで割ったあまりO(logN)
ll modpow(ll n,ll m,ll MOD){
    ll res = 1;
    while(m){
        if(m & 1)res = (res * n) % MOD;
        m >>= 1;
        n *= n;
        n %= MOD;
    }
    return res;
}

//printf("%.10f\n", N);
/*  vector<ll> vec = {1,2,5,6,8,9,10};
    cout << binary_search(all(vec),5) << endl; -> true*/
/*
    vector<ll> vec = {1,2,5,6,8,9,10};
    auto n = upper_bound(all(vec),4);
    cout << *n << endl; -> 5*/

//    cout << fixed << setprecision(15);

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll N;
    cin >> N;
    if(N % 1000 == 0){
        cout << "0\n";return 0;
    }
    cout << (1000 - N % 1000) << "\n";
}


Measured memory_bytes: 3648
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int n, k, i, j, INF = 1e9 + 5;
    cin >> n >> k;
    int a[n + 1];
    vector<int> dp(n + 1, INF);
    for(i = 1; i <= n; ++i)
        cin >> a[i];
 
    dp[1] = 0;
    dp[2] = abs(a[2] - a[1]);
    for(i = 3; i <= n; ++i) {
        for(j = 1; j <= k; ++j) {
            if((i - j) > 0)
                dp[i] = min(dp[i], abs(a[i] - a[i - j]) + dp[i - j]);
            else
                break;
        }
    }
    cout << dp[n] << "\n";
    return 0;
}

Measured memory_bytes: 1152
#include <stdio.h>
#include <string.h>
#define LEN 100005
 
typedef struct pp {
  char name[100];
  int t;
} P;
 
P Q[LEN];
int head, tail, n;
 
void enqueue(P x) {
  Q[tail] = x;
  tail = (tail + 1) % LEN;
}
 
P dequeue() {
  P x = Q[head];
  head = (head + 1) % LEN;
  return x;
}
 
int min(int a,int b) {
  return a < b ? a : b; 
}
 
int main() {
  int elaps = 0, c;
  int i, q;
  P u;
  char str[64];
  
  scanf("%d %d",&n ,&q);
  for(i=1; i<=n; i++) {
    scanf("%s", Q[i].name);
    scanf("%d", &Q[i].t);
  }
  head = 1; tail = n + 1;
 
  while(head != tail) {
    u = dequeue();
    c = min(q, u.t);
    u.t -= c;
    elaps += c;
    if(u.t > 0) enqueue(u);
    else {
      printf("%s %d\n",u.name,elaps);
    }
  }
  return 0;
}


Measured memory_bytes: 11852
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define N 500005
using namespace std;
int f[N],c[N],n,m,a[N];
int main()
{
	cin>>n>>m;
	fo(i,1,n) scanf("%d",&a[i]);
	int d=m;
	fo(i,1,n) 
	{
		if(abs(d-a[i])<d) d=abs(d-a[i]);
		c[i]=d;
	}
	f[n+1]=1;
	fod(i,n,1)
	{
		f[i]=f[i+1];
		if(abs(f[i]-a[i])<f[i]) f[i]=f[i]+a[i];
	}
	int q;
	cin>>q;
	c[0]=m;
	fo(i,1,q)
	{
		int x;
		scanf("%d",&x);
		if(c[x-1]>=f[x+1]) printf("YES\n");
		else printf("NO\n");
	}
}


Measured memory_bytes: 7808
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<long long, long long>
#define ff first
#define ss second
const int MAXV = 1e3;

main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, W; cin >> N >> W;
    int w[N], v[N];
    for(int i = 0; i < N; i++) cin >> w[i] >> v[i];
    ll DP[MAXV*N+1]; fill(DP, DP+MAXV*N+1, LLONG_MAX);
    DP[0] = 0;
    for(int i = 0; i < N; i++){
        for(int j = MAXV*N; j >= v[i]; j--){
            if(DP[j - v[i]] != LLONG_MAX){
                DP[j] = min(DP[j], DP[j - v[i]] + w[i]);
            }
        }
    }
    for(int i = MAXV*N; i >= 0; i--){
        if(DP[i] <= W){
            cout << i;
            break;
        }
    }
}

Measured memory_bytes: 4384
h,w = map(int,input().split())
A = [list(input()) for _ in range(h)]
dp = [[0]*(w) for _ in range(h)]
dp[0][0] = 1
mod = 10**9+7
for i in range(h):
    if A[i][0]=='.':
        dp[i][0] = 1
    else:
        break
for i in range(w):
    if A[0][i]=='.':
        dp[0][i] =1
    else:
        break
for i in range(1,h):
    for j in range(1,w):
        if A[i][j]=='.':
            dp[i][j] = dp[i][j-1] + dp[i-1][j]
            dp[i][j] %= mod
print(dp[-1][-1])

Measured memory_bytes: 117060
#include<bits/stdc++.h>
using namespace std;
int main(){
	int N;
	cin>>N;
	int	sum=0;
	for(int i=0;i<N;i++){
		int l,r;
		cin>>l>>r;
		sum+=r-l+1;
	}
	cout<<sum<<endl;
}


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

int main(){
    string s1,s2;
    cin>>s1>>s2;
    int ans=0;
    for(int i=0;i<s1.length();++i){
        if(s1[i]!=s2[i])ans++;
    }
    cout<<ans<<endl;
    return 0;
}

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

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

using namespace std;

using ll = long long;
using P = pair<int, int>;

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

    int a, b; cin >> a >> b;

    cout << (b%a?b-a:a+b);
}


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

#define int long long

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  string s;
  int b = 0, w = 0;
  cin >> s;

  int ans = 0;  
  for ( int i = 0; i < (int)s.size(); i++ ) {
    if ( s[i] == 'W' ) {
      ans += b;
    } else {
      b++;      
    }
  }

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


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

using namespace std;

constexpr int MOD = 1000000007;

using Long = long long;
template<typename T>
using V = vector<T>;
template<typename T>
using Vv = V<V<T>>;
using Vb = V<bool>;
using Vvb = V<Vb>;
using Vi = V<int>;
using Vvi = V<Vi>;
using Vl = V<Long>;
using Vvl = V<Vl>;
using Vd = V<double>;
using Vvd = V<Vd>;
using Vs = V<string>;

template <typename First, typename Second>
istream& operator>>(istream& is, pair<First, Second>& v);

template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
    for (auto& x : v) {
        is >> x;
    }
    return is;
}

template <typename First, typename Second>
istream& operator>>(istream& is, pair<First, Second>& v) {
    is >> v.first >> v.second;
    return is;
}

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (const auto& x : v) {
        os << x << ' ';
    }
    return os;
}

// *** SOLUTION BEGIN ***

void solve() {
    int n;
    Long c;
    cin >> n >> c;
    V<pair<Long, int>> s(n);
    cin >> s;

    vector<Long> pos(n + 1);
    vector<Long> neg(n + 1);
    Long cur = 0;
    Long x = 0;
    for (int i = 0; i < n; ++i) {
        cur += s[i].second - (s[i].first - x);
        pos[i + 1] = max(pos[i], cur);
        x = s[i].first;
    }
    cur = 0;
    x = c;
    for (int i = n - 1; i >= 0; --i) {
        cur += s[i].second - (x - s[i].first);
        neg[i] = max(neg[i + 1], cur);
        x = s[i].first;
    }
    Long best = max(pos[n], neg[0]);
    cur = 0;
    x = 0;
    for (int i = 0; i < n; ++i) {
        cur += s[i].second - 2 * (s[i].first - x);
        best = max(best, cur + neg[i + 1]);
        x = s[i].first;
    }
    cur = 0;
    x = c;
    for (int i = n - 1; i >= 0; --i) {
        cur += s[i].second - 2 * (x - s[i].first);
        best = max(best, cur + pos[i]);
        x = s[i].first;
    }
    cout << best;
}

// *** SOLUTION END ***

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cout.precision(10);
    cout << fixed;
    solve();
    return 0;
}


Measured memory_bytes: 3456
# -*- coding: utf-8 -*-
"""
Created on Mon Sep  7 22:04:16 2020

@author: liang
"""

N = int(input())
A = [int(x) for x in input().split()]
d = [0]*N

for a in A:
    d[a-1] += 1
#print(d)
score = 0
for  a in d:
    score += a*(a-1)//2
#print(score)
for a in A:
    print(score - d[a-1] + 1)


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

using namespace std;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;
using vld = vector<ld>;
using vvld = vector<vld>;

typedef pair<ll, ll> P;

#define bit(n) (1LL << (n))

//#define int long long

#define all(v) v.begin(), v.end()

#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n; i++)

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)

template <class T>
inline void chmax(T& a, T b) {
  a = std::max(a, b);
}
template <class T>
inline void chmin(T& a, T b) {
  a = std::min(a, b);
}

#define mod (ll)(1e9 + 7)
// #define mod (998244353ll)

const long long INF = 1LL << 60;

// constexpr long long INF = 1LL << 60;

// Ford-Fulkerson
template <typename T>
class MaxFlow {
  struct Edge {
    int to;
    T cap;
    int rev; // 逆辺のindex(G[to][rev]が逆辺)
  };

 private:
  vector<vector<Edge>> G;
  vector<bool> used;
  int sink;

  // f: 頂点vに流れてきている量
  T dfs(int v, T f) {
    if (v == sink) {
      return f;
    }

    used[v] = true;
    for (auto e = G[v].begin(); e != G[v].end(); e++) {
      if (used[e->to] || e->cap <= 0) {
        continue;
      }

      T d = dfs(e->to, min(f, e->cap));
      if (d > 0) {
        e->cap -= d;
        G[e->to][e->rev].cap += d;
        return d;
      }
    }

    return 0;
  }

 public:
  MaxFlow(int max_v) : G(max_v), used(max_v) {
  }

  void add_edge(int from, int to, T cap) {
    G[from].push_back((Edge){to, cap, (int)G[to].size()});
    G[to].push_back((Edge){from, 0, (int)G[from].size() - 1});
  }

  // source: Start
  // sink: Goal
  T solve(int source, int sink) {
    this->sink = sink;

    T flow = 0;
    for (;;) {
      fill(all(used), false);

      T f = dfs(source, INF);
      if (f == 0) {
        return flow;
      }
      flow += f;
    }
  }

  // 残余グラフ上で始点から到達できる頂点集合をX、それ以外をY
  // とすれば(X, Y)が最小カットの解である。
};

// 二部グラフの最大マッチング
template <typename T>
class BipartiteMatching : private MaxFlow<T> {
 private:
  int start;
  int goal;
  int num_a;
  int num_b;

 public:
  BipartiteMatching(int _num_a, int _num_b)
    : MaxFlow<T>::MaxFlow(_num_a + _num_b + 12),
      start(_num_a + _num_b),
      goal(_num_a + _num_b + 1),
      num_a(_num_a),
      num_b(_num_b) {
    // startからaに辺を引く
    rep(i, num_a) {
      add_edge(start, i, INF);
    }
    // bからgoalに辺を引く
    rep(i, num_b) {
      MaxFlow<T>::add_edge(num_a + i, goal, INF);
    }
  }

  void add_edge(int a, int b, T cap) {
    MaxFlow<T>::add_edge(a, num_a + b, cap);
  }
  T solve() {
    return MaxFlow<T>::solve(start, goal);
  }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll n, e;
  cin >> n >> e;

  MaxFlow<ll> mf(n);

  rep(i, e) {
    ll a, b, c;
    cin >> a >> b >> c;
    mf.add_edge(a, b, c);
  }

  cout << mf.solve(0, n - 1) << endl;

  return 0;
}



Measured memory_bytes: 3200
// ====================================
//   author: M_sea
//   website: https://m-sea-blog.com/
// ====================================
#include <bits/stdc++.h>
#define file(x) freopen(#x".in","r",stdin); freopen(#x".out","w",stdout)
using namespace std;
typedef long long ll;

ll read() {
	ll X=0; char c=getchar();
	while (c<'0'||c>'9') c=getchar();
	while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();
	return X;
}

const int N=100000+10;

int n,sz[N],dep[N];
pair<ll,int> d[N];
vector<int> E[N];
vector<pair<int,int> > ans;

void dfs(int u,int f) {
	dep[u]=dep[f]+1;
	for (int v:E[u]) if (v!=f) dfs(v,u);
}

int main() {
	n=read();
	for (int i=1;i<=n;++i) d[i]=make_pair(read(),i);
	sort(d+1,d+n+1);
	for (int i=1;i<=n;++i) sz[i]=1;
	for (int i=n;i>1;--i) {
		ll dlt=2*sz[d[i].second]-n+d[i].first;
		int p=lower_bound(d+1,d+n+1,make_pair(dlt,0))-d;
		if (d[p].first!=dlt) { puts("-1"); return 0; }
		int u=d[i].second,v=d[p].second;
		ans.emplace_back(make_pair(u,v));
		E[u].emplace_back(v),E[v].emplace_back(u),sz[v]+=sz[u];
	}
	dep[0]=-1,dfs(d[1].second,0); ll s=0;
	for (int i=1;i<=n;++i) s+=dep[i];
	if (s!=d[1].first) { puts("-1"); return 0; }
	for (auto i:ans) printf("%d %d\n",i.first,i.second);
	return 0;
}
// /jk

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

int main()
{
	string S;
	cin >> S;

	int sz = S.size();
	int ans = sz;
	int s = 0;
	for( int i = 0; i < sz; i++ ) {
		if( S[i] == 'S' ) s++;
		else {
			if( s > 0 ) {
				s--;
				ans -= 2;
			}
		}
	}

	cout << ans << endl;
}


Measured memory_bytes: 640
#include <cstdio>

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

    int last_position = 0, lowest_score;

    for(int i = 1; i <= no_of_scores; i++)
    {
        int position, score;
        scanf("%d %d", &position, &score);

        if(position > last_position)
        {
            last_position = position, lowest_score = score;
        }
    }

    int maximum_players = last_position + lowest_score;
    printf("%d\n", maximum_players);

    return 0;
}


Measured memory_bytes: 128
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[3][100005];
ll cnt[3][100005],ans=0;
int main()
{
	//freopen("fruit.in","r",stdin);//重定向从文件读数据 
	//freopen("fruit.out","w",stdout);//重定向写数据到文件 
	memset(cnt,0,sizeof(cnt));
	int n,g;
	cin>>n;
	for (int i=0;i<3;++i){
		for (int j=0;j<n;++j) cin>>a[i][j];
	}
	for (int i=0;i<3;++i) sort(a[i],a[i]+n);
	for (int i=0;i<n;++i) cnt[2][i]=1;
	for (int i=1;i>=0;--i){
		int p=n-1;
		for (int j=n-1;j>=0;--j){
			cnt[i][j]=cnt[i][j+1];
			while(p>=0&&a[i+1][p]>a[i][j]){
				cnt[i][j]+=cnt[i+1][p];
				--p;
			}
		}
	}
	for (int i=0;i<n;++i) ans+=cnt[0][i];
	cout<<ans;
  	return 0;
}



Measured memory_bytes: 4992
using System;
using System.Collections.Generic;
using System.Linq;

namespace AtCoder
{
    struct Point
    {
        public int X;
        public int Y;
        public Point(int x, int y)
        {
            X = x;
            Y = y;
        }
    }

    enum Direction
    {
        None,
        North,
        East,
        South,
        West
    }

    class Abc170F
    {
        public static void Main()
        {
            var hwk = Console.ReadLine().Split(' ').Select(i => int.Parse(i)).ToArray();

            var height = hwk[0];
            var width = hwk[1];
            var k = hwk[2];

            var xy = Console.ReadLine().Split(' ').Select(i => int.Parse(i) - 1).ToArray();

            var y0 = xy[0];
            var x0 = xy[1];
            var y1 = xy[2];
            var x1 = xy[3];
            var map = new int[height][];
            for (int i = 0; i < height; i++)
                map[i] = Console.ReadLine().Select(c => c == '@' ? int.MinValue : int.MaxValue).ToArray();
            var queue = new Queue<Point>(new[] { new Point(x0, y0) });
            map[y0][x0] = 0;

            while (queue.Any())
            {
                var p = queue.Dequeue();
                var cost = map[p.Y][p.X];
                if (p.X == x1 && p.Y == y1)
                {
                    Console.WriteLine(cost);
                    return;
                }
                cost++;
                void MoveTo(Direction direction)
                {
                    int dx = 0, dy = 0;
                    switch (direction)
                    {
                        case Direction.North:
                            dy--;
                            break;
                        case Direction.South:
                            dy++;
                            break;
                        case Direction.East:
                            dx++;
                            break;
                        case Direction.West:
                            dx--;
                            break;
                    }

                    var next = p;
                    for (int i = 0; i < k; i++)
                    {
                        next.X += dx;
                        next.Y += dy;
                        if (next.X < 0 || width <= next.X ||
                            next.Y < 0 || height <= next.Y || map[next.Y][next.X] < cost)
                            return;
                        if (map[next.Y][next.X] == cost)
                            continue;
                        map[next.Y][next.X] = cost;
                        queue.Enqueue(next);
                    }
                }
                MoveTo(Direction.North);
                MoveTo(Direction.South);
                MoveTo(Direction.East);
                MoveTo(Direction.West);
            }
            Console.WriteLine(-1);
        }
    }
}


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

cnt_list = [0 for _ in range(len(a))]

i = 0

for a_i in a:
    while a_i % 2 == 0:
        cnt_list[i] += 1
        a_i /= 2
    i += 1

print(min(cnt_list))


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

#define CON_MAX 300001LL

#define MOD 998244353LL

long long int inv[ CON_MAX ] = {};
long long int invv[ CON_MAX ] = {};
long long int cc[ CON_MAX ] = {};

int main() {

  long long int n, a, b, k;
  cin >> n >> a >> b >> k;

  inv[0] = 1;
  inv[1] = 1;
  invv[0] = 1;
  invv[1] = 1;
  for ( long long int i = 2; i < CON_MAX; i++ ) {
    inv[i] = MOD - ( MOD / i ) * inv[ MOD % i ] % MOD;
    invv[i] = invv[i-1] * inv[i] % MOD;
  }

  cc[0] = 1;
  for ( long long int i = 1; i < CON_MAX; i++ ) {
    cc[i] = cc[i-1] * ( n + 1 - i ) % MOD;
  }

  long long int ans = 0;

  for ( long long int i = 0; i * a <= k; i++ ) {

    if ( i > n ) break;

    if ( ( k - i * a ) % b != 0 ) continue;
    long long int j = ( k - i * a ) / b;

    if ( j > n ) continue;

    long long int ci = cc[i] * invv[i] % MOD;
    long long int cj = cc[j] * invv[j] % MOD;

    ans += ci * cj % MOD;
    ans %= MOD;

  }

  cout << ans << endl;

  return 0;

}


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

int getSumOfDigits(int n){
    int sum=0;
    while(n>0){
        sum += n%10;
        n /= 10;
    }
    return sum;
}
int main(){
    int n,a,b;
    cin >> n >> a >> b;
    int ans = 0;
    for(int i=1;i<=n;i++){
        int sum = getSumOfDigits(i);
        if(sum>=a && sum<=b)ans += i;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
n,l = gets.split(' ').map(&:to_i)

lines = []
while line = gets
  lines << line.split(' ').map(&:to_i)
end

c = 0
lines.each do |line|
  ans = (line[0] * line[0]) + (line[1] * line[1])
  if ans <= l * l
    c += 1
  end
end

puts c

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

int main() {
  int n;
  set<int> s;
  cin>>n;
  for (int i = 0; i < n; ++i) {
    int d;
    cin>>d;
    s.insert(d);
  }
  cout<<s.size()<<endl;
}


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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)

typedef long long LL;
typedef unsigned long UL;

using namespace std;


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

	int n;
	cin >> n;

	int ans = 0;
	int bef = INT_MIN;
	int now = 0;
	int temp = 0;

	REP(i, n)
	{
		cin >> temp;

		if (temp <= bef)
		{
			++now;
		}
		else
		{
			ans = max(ans, now);
			now = 0;
		}

		bef = temp;
	}

	ans = max(ans, now);

	cout << ans << endl;

	return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#define MAX 25
#define N -1
struct Node{int p, l, r;};
struct Node T[MAX];

int n, D[MAX],H[MAX];
void setDepth(int x,int d){
  if(x==N)
    return;
  D[x]=d;
  setDepth(T[x].l,d+1);
  setDepth(T[x].r,d+1);
}

int setHeight(int x){
  int h1=0,h2=0;
  if(T[x].l!=N)
    h1=setHeight(T[x].l)+1;
  if(T[x].r!=N)
    h2=setHeight(T[x].r)+1;
  return H[x]=(h1 > h2 ? h1 : h2);
}

int getSibling(int x){
  if(T[x].p==N)
    return N;
  if(T[T[x].p].l!=x&&T[T[x].p].l!=N)
    return T[T[x].p].l;
  if(T[T[x].p].r!=x&&T[T[x].p].r!=N)
    return T[T[x].p].r;
  return N;
}

void print(int x){
  printf("node %d: ",x);
  printf("parent = %d, ",T[x].p);
  printf("sibling = %d, ",getSibling(x));
  int deg =0;
  if(T[x].l!=N)
    deg++;
  if(T[x].r!=N)
    deg++;
  printf("degree = %d, ",deg);
  printf("depth = %d, ",D[x]);
  printf("height = %d, ",H[x]);
  if(T[x].p==N){
    printf("root\n");
  }
  else if(T[x].l==N&&T[x].r==N){
    printf("leaf\n");
  }
  else{
    printf("internal node\n");
  }
}

int main(){
  int i,v,l,r,root=0;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    T[i].p=N;
  }
  for(i=0;i<n;i++){
    scanf("%d%d%d",&v,&l,&r);
    T[v].l=l;
    T[v].r=r;
    if(l!=N)
      T[l].p=v;
    if(r!=N)
      T[r].p=v;
  }
  for(i=0;i<n;i++){
    if(T[i].p==N){
      root=i;
    }
  }
  setDepth(root,0);
  setHeight(root);
  for(i=0;i<n;i++){
    print(i);
  }
  return 0;
}
    

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

int main(){
    int n, t;
    pair<int, int> menu[3000];
    cin >> n >> t;
    for(int i=0;i<n;++i){
        int a, b;
        cin >> a >> b;
        menu[i] = {a, b};
    }
    sort(menu, menu+n);

    static int64_t dp[3001][3001];
    for(int i=1;i<t;++i){
        for(int j=1;j<=n;++j){
            dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
            if(i >= menu[j-1].first) dp[i][j] = max(dp[i][j], dp[i-menu[j-1].first][j-1] + menu[j-1].second);
        }
    }

    for(int j=1;j<=n;++j){
        dp[t][j] = max(dp[t-1][j], dp[t][j-1]);
        dp[t][j] = max(dp[t][j], dp[t-1][j-1] + menu[j-1].second);
    }
    cout << dp[t][n] << endl;

    return 0;
}


Measured memory_bytes: 70656
#include<string>
#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<chrono>
#include<functional>
#include<random>
#include<unordered_set>
using namespace std;
typedef long long int llint;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
const llint mod=1000000007;
const llint big=1e17-10;
const long double pai=3.141592653589793238462643383279;
template <class T,class U>void mineq(T& a,U b){if(a>b){a=b;}}
template <class T,class U>void maxeq(T& a,U b){if(a<b){a=b;}}
int main(void){
	int i,j,n;cin>>n;
	string s,t;cin>>s>>t;
	string ans;
	for(i=0;i<=n;i++){
		int ok=0;
		for(j=0;i+j<n;j++){if(s[i+j]!=t[j]){ok=1;break;}}
		if(ok==1){continue;}
		cout<<i+n<<endl;
		break;
	}
	
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define eps 1e-3
#define pi acos(-1.0)
#define inf 0x3f
#define INF 0x3f3f3f3f
#define pb push_back
#define debug1 cout<<"&&";
#define debug2 cout<<"**";
#define ms(a, x) memset(a, x, sizeof(a))
#define for0(i, n) for(int i = 0; i < n; ++i)
#define for1(i, n) for(int i = 1; i <= n; ++i)
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, int> pli;
typedef pair<long long, long long> pll;
const int mod = 1e9 + 7;
const int N = 1e5+10;
const int M = 1e6+10;
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline ll Pow(ll a, ll b) { ll ans = 1; for( ; b; b >>= 1) { if(b&1) ans = ans * a % mod; a = a * a % mod;} return ans;}
inline ll Mul ( ll a, ll b)  { ll lf = a * ( b >> 25ll ) % mod * ( 1LL << 25 ) % mod; ll rg = a * ( b & ( ( 1ll << 25 ) - 1 ) ) % mod; return ( lf + rg ) % mod;}
//==================================================================================================================================================================//

ll n, W, dp[N];

int main() {
   scanf("%lld %lld", &n, &W);
   for1(i, n) {
      ll w, v;
      scanf("%lld %lld", &w, &v);
      for(ll j = W; j >= w; --j) {
         dp[j] = max(dp[j], dp[j-w] + v);
      }
   }
   printf("%lld\n", dp[W]);
   return 0;
}

Measured memory_bytes: 4596
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
int main() {
    int n;
    cin >> n;
    //カードの番号を読み込む
    vector<int> cardNumber(n);
    for (int i=0; i<n; ++i) {
        cin >> cardNumber.at(i);
    }
    //カードを番号の昇順に並べる
    sort(cardNumber.begin(), cardNumber.end());
    //カードを番号の降順に並べる
    reverse(cardNumber.begin(), cardNumber.end());
    
    //アリスとボブの点数をそれぞれ合計する
    int AliceScore = 0;
    int BobScore = 0;
    
    for (int i=0; i<n; ++i) {
        int parity = i % 2;
        if (parity == 0){
            AliceScore += cardNumber.at(i);
        } else if (parity == 1){
            BobScore += cardNumber.at(i);
        }
    }
    int dif = AliceScore - BobScore;
    cout << dif << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<ctype.h>
#include<cmath>
#include<queue>
#define MAXN 10001

using namespace std;
typedef long long ll;

int main()
{
    int n;
    scanf("%d",&n);
    int a[n];
    int ct_2=0,ct_4=0;
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        if(a[i]%4==0)
            ct_4++;
        else{
            if(a[i]%2==0)
                ct_2++;
        }
    }
    if(ct_2>=2)
        n-=ct_2-1;
    if(n<=(3+(ct_4-1)*2)&&ct_4>=1||n==1)
        printf("Yes\n");
    else printf("No\n");
    return 0;
}


Measured memory_bytes: 640