akhauriyash/Code-Regression huggingface dataset:
n = int(input())
ara = [input() for _ in range(n)]
pref = []
suff = []

for s in ara:
	pos = neg = 0
	for ch in s:
		if ch == '(':
			pos += 1
		elif pos == 0:
			neg += 1
		else:
			pos -= 1
	if pos >= neg:
		pref.append([pos, neg])
	else:
		suff.append([pos, neg])


pref.sort(key=lambda x: x[1])
suff.sort(key=lambda x: x[0], reverse=True)

totPos = 0

ara = pref + suff

for pos, neg in ara:
	if neg > totPos:
		print("No")
		exit(0)
	totPos -= neg
	totPos += pos


if totPos != 0:
	print("No")
else:
	print("Yes")


Measured memory_bytes: 270716
#include<stdio.h>
 
int main()
{
	int n;
    scanf("%d", &n);
    long long sum = 0;
    for (int i = 0; i <= n; i++)
    {
        if (i % 3 != 0 && i % 5 != 0)
        {
            sum += i;
        }
 
    }
    printf("%lld\n", sum);
	return 0;
}

Measured memory_bytes: 1708
#include <iostream>
using namespace std;
int n,p[10][10],ans;

void saiki(int sum,int x,int day,int I){
  int f=1;
  for(int i=I+1;i<n;i++)
    if(p[day][i]/p[day-1][i]) 
      for(int j=1;x/(p[day-1][i]*j);j++){
	f=0;
	saiki(sum+p[day][i]*j,x-p[day-1][i]*j,day,i);
      }
  if(f)ans=max(ans,sum+x);
}

int main(){
  int d,x;
  cin>>n>>d>>x;
  for(int i=0;i<d;i++)
    for(int j=0;j<n;j++)cin>>p[i][j];

  ans=x;
  for(int i=0;i<d-1;i++)saiki(0,ans,i+1,-1);
  cout<<ans<<endl;
  return 0;
}

Measured memory_bytes: 1164
s = list(input())
n = len(s)

#  < > < だと n=2
#  一旦0を全部に入れる
a = [0]*(n+2)

#  左から右に　< だと　a[i+1]はa[i]+1
for i in range(n):
    if s[i] == '<':
        a[i+1] = a[i]+1

#  右から左に > だた a[i]はa[i+1]+1
for i in range(n-1, -1, -1):
    if s[i] == '>':
        a[i] = max(a[i], a[i+1]+1)

print(sum(a))

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

using namespace std;
#define N 100100
#define SIGMA 26

int n, i, ans[16], sel[255];
bool valid (int pos, int mx){
    if (sel[ans[pos]] || (!sel[ans[pos]] && (ans[pos] - 1 == mx)))
        return 1;
    return 0;
}
void bkt (int k, int mx){
    if (k == n - 1){
        for (int it = 0; it < n; ++it)
            cout << char(ans[it] + 'a');
        cout << '\n';
        return;
    }
    for (int it = 0; it < n; ++it){
        ans[k + 1] = it;
        if (valid(k + 1, mx)){
            ++sel[it];
            bkt(k + 1, max(mx, it));
            ans[k + 1] = 0;
            --sel[it];
        }
    }
}
int main()
{
    cin >> n;
    for (i = 0; i < n; ++i)
        ans[i] = 0;
    sel[0] = 1;
    bkt (0, 0);
    return 0;
}


Measured memory_bytes: 1536
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner scan = new java.util.Scanner(System.in);
        int n = scan.nextInt();
        int[] a = new int[n];
        boolean b = true;
        for(int i = 0; i < n; i++){
            a[i] = scan.nextInt();
            if(a[i] % 2 != 0){
                if(b){
                    b = true;
                }
            }else if(a[i] % 3 == 0 || a[i] % 5 == 0){
                if(b){
                    b = true;
                }
            }else{
                b = false;
            }
        }
        if(b){
            System.out.println("APPROVED");
        }else{
            System.out.println("DENIED");
        }
    }
}

Measured memory_bytes: 22996
#include <iostream>

using namespace std;

int main()
{
    int n,v[200005]={},x;
    cin>>n;
    for(int i=2;i<=n;i++){
        cin>>x;
        v[x]++;
    }
    for(int i=1;i<=n;i++){
        cout<<v[i]<<endl;
    }
    return 0;
}


Measured memory_bytes: 4408
#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#define sqr(x) (x)*(x)
using namespace std;
vector<int> bi[200005];
int n,m,i,j,x,y;
int main()
{
	ios::sync_with_stdio(0);
	cin>>n>>m;
	for (i=1;i<=m;i++)
	{
		cin>>x>>y;
		bi[x].push_back(y);
		bi[y].push_back(x);
	}
	sort(bi[1].begin(),bi[1].end());
	sort(bi[n].begin(),bi[n].end());
	for (i=2;i<n;i++)
	{
		if (binary_search(bi[1].begin(),bi[1].end(),i)&&binary_search(bi[n].begin(),bi[n].end(),i)) 
		{
			cout<<"POSSIBLE";
			return 0;
		}
	}
	cout<<"IMPOSSIBLE";
	return 0;
}

Measured memory_bytes: 12016
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
typedef long long ll;
const int N=100005;
ll a,b;
using namespace std;

int main()
{
	#ifdef Kay
		freopen ("code.in","r",stdin);
		freopen ("code.out","w",stdout);
	#endif
	cin>>a>>b;
	if (max(a-b,b-a)<=1) puts("Brown");
	else puts("Alice");
	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <map>
#include <utility>
#include <numeric>
#include <typeinfo>

using namespace std;

int main(){
  int N;
  cin >> N;
  vector<int> A(N);
  for(int i=0;i<N;i++)  cin >> A.at(i);

  vector<int> B(N);
  for(int i=0;i<N;i++)  B.at(i)=A.at(i)-i-1;
  sort(B.begin(),B.end());
  
  int b;
  if(N%2==0){
    int x=B.at(N/2-1);
    int y=B.at(N/2);
    b=(x+y)/2;
  }
  else  b=B.at(N/2);

  long long ans=0;
  for(int i=0;i<N;i++){
    ans+=abs(B.at(i)-b);
  }
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 1792
#pragma GCC optimize("Ofast")  
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
using namespace std;
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define int long long 
#define endl "\n"
#define all(x) (x).begin(),(x).end()
const int MOD = 1e9+7;
const int mxN = 2e5+3;

void solve(){
	int n;
	cin >> n;
	int ans = 1 - n;
	cout << ans << endl;
}

int32_t main(){
	IO;
	
	int nT = 1;
	//cin >> nT;
	for(int i = 1; i <= nT; ++i)
		solve();
	return 0;
}


Measured memory_bytes: 3632
#include <stdio.h>
int main(void){
    int a,b,c,k;
    scanf("%d %d %d %d",&a,&b,&c,&k);
    if(k<=a)
        printf("%d",k);
    else if(k<=a+b)
        printf("%d",a);
    else
        printf("%d",2*a-k+b);
}


Measured memory_bytes: 1728
#dpでできないかな？
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n=I()
lis=LI()
z=list(accumulate(lis))
#print(list(accumulate(lis)))
mn=inf
for i in range(n-1):
    dif=abs(z[-1]/2-z[i])
    if dif<mn:
        mn=dif
print(int(2*mn))

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

using namespace std;

int main() {
	long long int N;
	cin >> N;
	vector<long long int>D( N );

	for( size_t i = 0; i < N; i++ ) {
		cin >> D[i];
		if( i ) {
			D[i] += D[i - 1];
		}
	}
	long long int ans = LLONG_MAX / 6;
	for( size_t i = 0; i < N - 1; i++ ) {
		ans = min( ans, abs( D[i] - ( D[N - 1] - D[i] ) ) );
	}
	cout << ans << endl;
}

Measured memory_bytes: 1792
N=int(input())
ans=N
for i in range(N+1):
    cnt=0
    t = i
    while t>0:
        cnt+=t%6
        t//=6
    t=N-i
    while t>0:
        cnt+=t%9
        t//=9
    ans=min(ans,cnt)
print(ans)

Measured memory_bytes: 3060
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>

using namespace std;

char s[4096], t[4096];
int v[4096][4096];

int main() {
	scanf("%s%s", s, t);
	int sLen = strlen(s);
	int tLen = strlen(t);
	for(int i=0; i<=sLen; ++i) {
		v[0][i] = i;
	}
	for(int j=0; j<tLen; ++j) {
		v[j+1][0] = j+1;
		for(int i=0; i<sLen; ++i) {
			if(s[i]==t[j]) {
				v[j+1][i+1] = v[j][i];
			}
			else {
				v[j+1][i+1] = min(v[j+1][i], v[j][i+1])+1;
			}
		}
	}
	string ret;
	while(sLen && tLen) {
		if(s[sLen-1]==t[tLen-1]) {
			ret.push_back(s[sLen-1]);
			--sLen;
			--tLen;
		}
		else if(v[tLen][sLen-1]<v[tLen-1][sLen]) {
			--sLen;
		}
		else {
			--tLen;
		}
	}
	reverse(ret.begin(), ret.end());
	printf("%s\n", ret.c_str());
	return 0;
}


Measured memory_bytes: 48896
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
 
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
 
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
 
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
 
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a,x) for (auto& a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
 
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert

template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
const int MOD = 1000000007;
const char nl = '\n';
const int MX = 100001; //check the limits, dummy

const int mxN = 1e6 + 10;

int prime[mxN];
vi cnt(mxN);

void sieve() { 
    prime[1] = 1; 
    for (int i=2; i<mxN; i++) 
        prime[i] = i; 
    for (int i=4; i<mxN; i+=2) 
        prime[i] = 2; 
  
    for (int i=3; i*i<mxN; i++) {
        if (prime[i] == i) {  
            for (int j=i*i; j<mxN; j+=i) {
            	if (prime[j]==j) {
                    prime[j] = i;
            	} 
            } 
                
        } 
    } 
} 

void factorise(int x) { 
	map <int,int> store;
    while (x != 1) 
    { 
        if(store[prime[x]] == 0) {
        	cnt[prime[x]]++;
        	store[prime[x]]++;
        }
        
        x = x / prime[x]; 
    } 
} 
 
int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);  

/*#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r+", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w+", stdout);
#endif  */

    sieve();
    int N;
    cin >> N;
    vi A(N);

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

    int hcf = A[0];
    for(int i=0;i<N;i++) {
    	hcf = __gcd(A[i],hcf);
    }
    bool pc = true, sc = (hcf == 1);
    for(int i=0;i<N;i++) {
    	factorise(A[i]);
    }
    for(int i=1;i<=1000000;i++) {
    	if(cnt[i] > 1) {
    		// cout << i << '\n';
    		pc = false;
    	}
    }
    if(pc) {
    	cout << "pairwise coprime" << '\n';
    }
    else if (sc) {
    	cout << "setwise coprime" << '\n';
    }
    else {
    	cout << "not coprime";
    }
	
    
}
 
// read the question correctly (ll vs int)
// template by bqi343

Measured memory_bytes: 15016
#include<stdio.h>

int main(){
    int n,i,j,tmp,flag,count=0;
    int A[100];

    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
       scanf("%d",&A[i]); 
    }

    
 for(i=0;i<n;i++)
 {
    for(j=n-1;j>i;j--)
    {
        if(A[j] < A[j-1])
        {
            tmp = A[j-1];
            A[j-1] = A[j];
            A[j] = tmp;
            
            count++;
        }
    }
  }

    for(i=0;i<n;i++)
    {
        if(i>0)printf(" ");
        printf("%d",A[i]);
    }
    printf("\n%d\n",count);
    return 0;
}


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

int main() {
	long int N,a;
	cin >> N;
	if(N>=1&&N<10){
		cout << N<<endl;
	}
	else if(N>=10&&N<100){
		cout << 9<<endl;
	}
	else if(N>=100&&N<1000){
		cout << N-90<<endl;
	}
	else if(N>=1000&&N<10000){
		cout << 909<<endl;
	}
	else if(N>=10000&&N<100000){
		cout << N-9090<<endl;
	}
	else{
		cout <<90909<<endl;
	}
}

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

int main()
{
    int H, W, K;
    cin >> H >> W >> K;

    vector<string> C_li(H);
    for (int i = 0; i < H; i++)
    {
        cin >> C_li[i];
    }

    int res = 0;
    for (int i = 0; i < (1 << H); i++)
    {
        for (int j = 0; j < (1 << W); j++)
        {
            int black = 0;
            for (int k = 0; k < H; k++)
            {
                for (int l = 0; l < W; l++)
                {
                    if (((i >> k) & 1) == 0 && ((j >> l) & 1) == 0 && C_li[k][l] == '#')
                    {
                        black++;
                    }
                }
            }
            if (black == K)
            {
                res++;
            }
        }
    }

    cout << res << endl;
}


Measured memory_bytes: 3632
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#include <limits.h>

using namespace std;

typedef long long 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;
}

int main(void) {
    int a, b;
    cin >> a >> b;

    if (a <= b) {
        cout << a << endl;
    } else {
        cout << a - 1 << endl;
    }
    return 0;
}


Measured memory_bytes: 3580
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <cstdlib>
#include <utility>
#include <cmath>
#include <queue>
#include <stack>
#include <cstring>

using namespace std;

#define ll long long

#ifndef ONLINE_JUDGE
#define debug(format, ...) fprintf(stderr, \
    "%s:%d: " format "\n", __func__, __LINE__,##__VA_ARGS__)
#else
#define debug(format, ...)
#define NDEBUG
#endif

int ac, wa, tle, re;

int main()
{
    int n; scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        char s[15]; scanf("%s", s);
        if (s[0] == 'A') {
            ac++;
        } else if (s[0] == 'W') {
            wa++;
        } else if (s[0] == 'T') {
            tle++;
        } else {
            re++;
        }
    }
    printf("AC x %d\nWA x %d\nTLE x %d\nRE x %d", ac, wa, tle, re);
    return 0;
}


Measured memory_bytes: 1728
#include<bits/stdc++.h>
#define LL long long
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define debug(x) cout<<#x<<"="<<x<<endl;
using namespace std;

int main(void)
{
	int  a[2],b[2],e,f;
	scanf("%d%d%d%d%d%d",&a[0],&a[1],&b[0],&b[1],&e,&f);
	double res3=0;
	int  res1=a[0]*100;int res2=0;
	//debug(a[0]);debug(a[1]);
	rep(i,0,33)
	{
		rep(j,0,33)
		{
			int tem1=i*a[0]+j*a[1];
			if(100*tem1>f||tem1==0)continue;
			//debug(tem1);
			for(int k=0;k<=(tem1*e)/b[0];k++)
			{
				if(100*tem1+k*b[0]>f)break;
				int tem2=(tem1*e-k*b[0])/b[1];
				int tem3=(f-100*tem1-k*b[0])/b[1];
				//debug(tem2);debug(tem3);
				if(tem3<=0)tem3=0;
				tem2=tem2<tem3?tem2:tem3;
				double p=(100.0*(tem2*b[1]+k*b[0]))/((double)100*tem1+tem2*b[1]+k*b[0]);
				//debug(p);debug(b[0]*k+b[1]*tem2);debug(100*tem1+b[0]*k+b[1]*tem2);debug(tem1);debug(k);
				if(p>res3)
				{
					res3=p;
					res2=b[0]*k+b[1]*tem2;
					res1=100*tem1+res2;
				}
			}
		}
	}
	printf("%d %d",res1,res2);
	return 0;
}

Measured memory_bytes: 256
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
## dp ##
def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]
## math ##
def to_bin(x: int) -> str: return format(x, 'b') # rev => int(res, 2)
def to_oct(x: int) -> str: return format(x, 'o') # rev => int(res, 8)
def to_hex(x: int) -> str: return format(x, 'x') # rev => int(res, 16)
MOD=10**9+7
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(MAX_NUM+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(MAX_NUM**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, MAX_NUM+1, i):
            is_prime[j] = False
    return [i for i in range(MAX_NUM+1) if is_prime[i]]
def prime_factor(n):
    ass = []
    for i in range(2,int(n**0.5)+1):
        while n % i==0:
            ass.append(i)
            n = n//i
    if n != 1:
        ass.append(n)
    return ass
## libs ##
from itertools import accumulate as acc, combinations as combi, product, combinations_with_replacement as combi_dup
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left
#======================================================#

def main():
    a, b = MII()
    a_f = set(prime_factor(a))
    b_f = set(prime_factor(b))
    a_f.add(1)
    b_f.add(1)
    c = a_f & b_f
    print(len(c))

if __name__ == '__main__':
    main()

Measured memory_bytes: 3444
#include <stdio.h>

int main(void) {
	int x, y;
	scanf("%d%d", &x, &y);

	int b = (y - 2*x)/2;
	int a = x-b;

	if (a<0 || b<0 || a+b != x || 2*a + 4*b != y) {
		puts("No");
		return 0;
	}

	puts("Yes");
}


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

typedef long long   signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)
#define  inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define  dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))

#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define PQ priority_queue

#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
#define  FOR(it, v) for(auto it =  v.begin(); it !=  v.end(); ++it)
#define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)

template<typename T> bool   setmin(T & a, T b) { if(b <  a) { a = b; return true; } else { return false; } }
template<typename T> bool   setmax(T & a, T b) { if(b >  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }
template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

int n, a[4000], s[4000], dp[2001][2001], pos[2][4000], x[2][4000][2001];

int main() {
	cin >> n;
	inc(i, 2 * n) { string ss; cin >> ss >> a[i]; a[i]--; s[i] = (ss == "B" ? 1 : 0); }
	
	inc(i, 2 * n) { pos[s[i]][a[i]] = i; }
	
	inc(k, 2) {
	incII(v, 0, n) {
		int c = 0;
		dec(i, 2 * n) {
			x[k][i][v] = c;
			if(s[i] == k && a[i] < v) { c++; }
		}
	}
	}
	
	incII(i, 0, n) {
	incII(j, 0, n) {
		dp[i][j] = 1e8;
	}
	}
	dp[0][0] = 0;
	
	incII(i, 0, n) {
	incII(j, 0, n) {
		int pb = pos[0][i];
		int pw = pos[1][j];
		if(i < n) { setmin(dp[i + 1][j], dp[i][j] + x[0][pb][i] + x[1][pb][j]); }
		if(j < n) { setmin(dp[i][j + 1], dp[i][j] + x[0][pw][i] + x[1][pw][j]); }

	}
	}
	
	cout << dp[n][n] << endl;
	
	return 0;
}


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

#define NEW(p,n){p=malloc((n)*sizeof(p[0]));if(p==NULL){printf("not enough memory\n");exit(1);};}
//pの型の変数n個の要素分のメモリを確保し、そのアドレスをpに代入するマクロ

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))

int main(void){
    int N, M;
    scanf("%d %d",&N, &M);

    //全部通れるIDカードの範囲が[L,R]
    int L,R;
    int ltmp,rtmp;
    L=1;
    R=N;

    for(int j=1;j<=M;j++){
        scanf("%d %d",&ltmp, &rtmp);
        if(ltmp>L) L=ltmp;
        if(rtmp<R) R=rtmp;
    }

    int ans=R-L+1;
    if(ans<0) ans=0;
    printf("%d\n",ans);

    return 0;
}

//入力ごとに1回ずつ処理するのではなく、一気に処理することも考える！

Measured memory_bytes: 128
#include<iostream>
#include<cmath>
#include<string>
#include<cctype>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
        long long a,b,gcda,gcdb,c;
        while(scanf("%lld %lld",&a,&b)!=EOF){
                if(a<b){
                        swap(a,b);
                }
                gcda=a;
                gcdb=b;
                while(gcda%gcdb){
                        c=gcdb;
                        gcdb=gcda%gcdb;
                        gcda=c;
                }
                printf("%lld %lld\n",gcdb,(a*b)/gcdb);
        }
        return 0;
}

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

typedef long long ll;
typedef pair<int,int> ii;
typedef tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;

int main() {
	cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
	int n; cin>>n;
	vector<int> a(n);
	for (int i=0; i<n; i++)
		cin>>a[i];
	int ret=0;
	for (int i=0; i<n; i++)
		if ((i+1)%2) {
		if (a[i]%2) {
			ret++; {
			}
		}
	}
	cout<<ret<<endl;
	return 0;
}


Measured memory_bytes: 3592
import sys

s, k = sys.stdin.read().split()
k = int(k)
n = len(s)

def main():
    for i in range(1, n+1):
        if s[i-1] != '1':
            break
    
    return 1 if k < i else int(s[i-1])

if __name__ == '__main__':
    ans = main()
    print(ans)

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





int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n = 4;
    int a[4];
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    sort(a, a + n);
    do{
        int p = 1, val = 0;
        for(int i = n - 1; i >= 0; i--){
            val += a[i] * p;
            p *= 10;
        }
        if(val== 1794){
            return cout << "YES", 0;
        }
    }while(next_permutation(a, a + n));
    cout << "NO";
}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<list>
#include<cstring>
#define mod             1000000007
#define int long long
using namespace std;
int32_t main()
{

    map <int,char>mp;
    char ch='a';
    for(int i=1; i<26; i++)
    { mp[i]=ch;
    ch++;

    }
    mp[0]='z';
    int n;
    cin >> n;
    list<char> v;

    while(n!=0)
    {
   v.push_front(mp[n%26]);
    if(n%26==0){n=(n/26)-1;}
    else{n=n/26;}



 }
 for(auto it:v)
    {
        cout << it;
    }
    cout << endl;
    return 0;}


Measured memory_bytes: 3132
k = int(input())
count = 1
cur_remainder = 7%k
if cur_remainder==0:
    print(count)
    exit()
remainder_set=set([cur_remainder])
while True:
    cur_remainder = (cur_remainder*10+7)%k
    count += 1
    if cur_remainder==0:
        print(count)
        exit()
    if cur_remainder in remainder_set:
        print(-1)
        exit()
    remainder_set.add(cur_remainder)
    

Measured memory_bytes: 77952
#include <bits/stdc++.h>  // clang-format off
using Int = long long;
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif  // clang-format on

/**
 *    author:  knshnb
 *    created: Sun May 24 18:45:32 JST 2020
 **/

signed main() {
    Int n;
    std::cin >> n;
    std::vector<Int> a(n);
    REP(i, n) std::cin >> a[i];
    Int Q;
    std::cin >> Q;
    REP(q, Q) {
        Int b, m, e;
        std::cin >> b >> m >> e;
        std::rotate(a.begin() + b, a.begin() + m, a.begin() + e);
    }
    REP(i, n) std::cout << a[i] << "\n "[i < n - 1];
}



Measured memory_bytes: 3212
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <string>
#include <map>
#include <algorithm>
using namespace std;


int main()
{
	int n;
	vector<long long> a;
	long long sum = 0;
	long long sum2 = 0;

	cin >> n;
	for(int i=0;i<n;i++){
		long long x;
		cin >> x;
		a.push_back(x);
		sum += x;
		if(i%2==1){
			sum2 += x;
		}
	}
	sum /= 2;

	long long d = sum - sum2;
	cout << 2*d;
	for(int i=0;i<n-1;i++){
		cout << " " << 2*(a[i] - d);
		d =a[i] - d;
	}	
	cout << endl;

	return 0;
}

Measured memory_bytes: 2292
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vin=vector<int>;
using vll=vector<long long>;
using vvin=vector<vector<int>>;
using vvll=vector<vector<long long>>;
using vstr=vector<string>;
using vvstr=vector<vector<string>>;
using vch=vector<char>;
using vvch=vector<vector<char>>;
using vbo=vector<bool>;
using vvbo=vector<vector<bool>>;
using vpii=vector<pair<int,int>>;
using pqsin=priority_queue<int,vector<int>,greater<int>>;
#define mp make_pair
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep2(i,s,n) for(int i=(s);i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define decp(n) cout<<fixed<<setprecision((int)n)
const int inf=1e9+7;
const ll INF=1e18;

vin prime;

void eratosthenes(int x){
    vbo isprime(x+1,true);
    isprime[0]=false;
    isprime[1]=false;
    for(ll i=2;i<=x;i++){
        if(isprime[i]){
            prime.push_back(i);
            for(ll j=2*i;j<=x;j+=i)isprime[j]=false;
        }
    }
    return;
}

int main(){
    int n;cin>>n;
    eratosthenes(55555);
    int res=0;int i=0;
    while(res<n){
        if(prime[i]%5==1){
            cout<<prime[i]<<endl;
            res++;
        }
        i++;
    }
}

Measured memory_bytes: 256
#include <algorithm>
#include <iostream>
#include <vector>
#include <functional>
#include <string>
#include <cmath>  //sqrt 
#include <iomanip> //setprecision
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define beg begin()
#define end end()

void _main(){
    int n,m; cin >> n >> m;
    vector<int> h(n);
    vector<vector<int>> g; g.resize(n);
    rep(i,n) cin >>h[i];
    rep(i,m){
        int a,b; cin >> a >> b; a--; b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    auto goodcheck = [&](int& i,int& x){
        for(auto y:g[i]){
            if(x <= h[y]) return false;
        }
        return true;
    };
    int count=0;
    rep(i,n){
        int x = h[i];
        if(goodcheck(i,x)) count++;
    }
    cout << count << endl;
    
    return;
}
int main() {
    cout << fixed << setprecision(10);
    _main();
    return 0;
}
    

Measured memory_bytes: 8408
def main():
    N, Y = map(int, input().split())

    for i in range(N+1):
        for j in range(N+1):
            if 10000*i + 5000*j + 1000*(N-i-j) == Y and N-i-j >= 0:
                print(i, j, N-i-j)
                exit()
    print("-1 -1 -1")
if __name__ ==  "__main__":
    main()

Measured memory_bytes: 3060
#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define int long long
#define MRE assert(0);
const int inf = 1e17;
const int mod = 998244353;
typedef pair<int, int> P;
typedef pair<int, P> PP;
string s[10];
int H, W, K;

signed main() {
	cin >> H >> W >> K;
	rep(i, H)cin >> s[i];
	int res = inf;
	rep(I, 1 << (H-1)) {
		vector<int>V;
		int cnt = 0;
		rep(j, H-1) {
			if (I&(1 << j)) {
				cnt++;
				V.push_back(j);
			}
		}
		V.push_back(H - 1);
		vector<int>nw(10);
		rep(j, W) {
			vector<int>tw(10);
			int u = 0;
			rep(m, V.size()) {
				for (int l = u; l <= V[m]; l++)if (s[l][j] == '1')tw[m]++;
				u = V[m] + 1;
			}
			int flg = 0;
			rep(m, V.size()) {
				if (nw[m] + tw[m] > K)flg++;
			}
			if (!flg) {
				rep(m, V.size())nw[m] += tw[m];
			}
			else {
				cnt++;
				rep(m, V.size()) {
					nw[m] = tw[m];
					if (tw[m] > K)cnt = inf;
				}
			}
		}
		res = min(res, cnt);
	}
	cout << res << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}



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

    ll n;
    cin>>n;
    ll a[n];
    ll ans = -1e18;
    for(int i=0;i<n;i++) cin>>a[i];
    if(n%2==0){
        ll dp[n]={};
        for(int i=0;i<n;i++) dp[i] = -1e18;
        dp[0]=a[0], dp[1]=a[1];
        for(int i=2;i<n;i++){
            if(i%2){
                dp[i] = max(dp[i-2],dp[i-3])+a[i];
            }
            else{
                dp[i] = dp[i-2] + a[i];
            }
        }
        chmax(ans,max(dp[n-1],dp[n-2]));
    }
    else{
        if(n==3){
            ans = max({a[0],a[1],a[2]});
            cout << ans << endl;
            return 0;
        }
        ll sum = 0;
        for(int i=0;i<n;i++) sum += a[i];
        ll dp[n][3];
        for(int i=0;i<n;i++){
            dp[i][0]=1e18;
            dp[i][1]=1e18;
            dp[i][2]=1e18;
        }
        dp[0][0] = a[0];
        dp[1][0] = a[1];
        dp[1][1] = a[1];
        dp[1][2] = a[0]+a[1];
        for(int i=2;i<n;i++){
            dp[i][0] = dp[i-2][0] + a[i];
            if(i%2==0){
                dp[i][1] = min(dp[i-2][1],dp[i-1][0]) + a[i];
            }
            else{
                dp[i][2] = min(dp[i-2][2],min(dp[i-1][0],dp[i-1][1])) + a[i];
            }
        }
        ans = sum - min(dp[n-1][1],dp[n-2][2]);
    }
    cout << ans << endl;
}


Measured memory_bytes: 9892
N, M, C = map(int, input().split())
B = [list(map(int, input().split())) for i in range(N+1)]
cnt = 0
for i in range(1, N+1):
    sums = 0
    for p in range(M):
        sums += B[i][p] * B[0][p]
        if (p == (M-1)):
            sums += C
            if sums > 0:
                cnt += 1
print(cnt)


Measured memory_bytes: 3060
using System;

namespace B___Two_Switches
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = Console.ReadLine().Split(' ');
            var a = int.Parse(input[0]);
            var b = int.Parse(input[1]);
            var c = int.Parse(input[2]);
            var d = int.Parse(input[3]);
            if (Math.Max(a, c) <= Math.Min(b, d))
            {
                Console.WriteLine(Math.Min(b, d) - Math.Max(a, c));
            }
            else
            {
                Console.WriteLine(0);  
            }
        }
    }
}

Measured memory_bytes: 13140
#include <bits/stdc++.h>
#define maxn 100010
#define modn 1000000007
char str[maxn];
int cnt[30],n,i,ans=1;
int main()
{
	scanf("%d",&n);
	scanf("%s",str);
	for (i=0;i<n;i++)
		cnt[str[i]-'a']++;
	for (i=0;i<26;i++)
		ans=1LL*ans*(cnt[i]+1)%modn;
	ans=(ans-1+modn)%modn;
	printf("%d",ans);
	return 0;
}

Measured memory_bytes: 384
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int, K: int, R: int, S: int, P: int, T: str):
    s = ['b']*(N)
    
    l = [
        ['s', S],
        ['r', R],
        ['p', P]
    ]
    l = sorted(l, key=lambda x:-x[1])

    d = {
        'r':'p',
        's':'r',
        'p':'s'
    }

    for i in range(N):
        s[i] = d[T[i]]


    for a, b in l:
        for i in range(N):
            if s[i] == a and i >= K and s[i-K] == a:
                s[i] = 'b'

    ans = 0
    for i in range(N):
        if s[i] == 'r' and T[i] == 's':
            ans += R
        elif s[i] == 's' and T[i] == 'p':
            ans += S
        elif s[i] == 'p' and T[i] == 'r':
            ans += P
    print(ans)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    R = int(next(tokens))  # type: int
    S = int(next(tokens))  # type: int
    P = int(next(tokens))  # type: int
    T = next(tokens)  # type: str
    solve(N, K, R, S, P, T)

if __name__ == '__main__':
    main()


Measured memory_bytes: 4212
import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int N = Integer.parseInt(sc.next());
		int Ma = Integer.parseInt(sc.next());
		int Mb = Integer.parseInt(sc.next());
		int[][] abc = new int[3][N];
		int MaxCost = 100*N+1;
		int MaxWeight =10*N+1;
		int budget = MaxCost;
		for(int i=0;i<N;i++){	//データの受け取り
			for(int kind=0;kind<3;kind++) abc[kind][i] = Integer.parseInt(sc.next());
		}
		
		int[][][] DynPlan = new int[N+1][MaxWeight][MaxWeight];//i番目まででの最小コスト
		for(int Num=0;Num<N+1;Num++)for(int a=0;a<MaxWeight;a++)for(int b=0;b<MaxWeight;b++)DynPlan[Num][a][b]=MaxCost;
		
		DynPlan[0][0][0]=0;//最小コストの設定
		for(int Num=1;Num<N+1;Num++){
			for(int a=0;a<MaxWeight;a++){
				for(int b=0;b<MaxWeight;b++){
					DynPlan[Num][a][b]=Math.min(DynPlan[Num][a][b],DynPlan[(Num-1)][a][b]);
					if(Math.max(a+abc[0][Num-1], b+abc[1][Num-1])<MaxWeight) DynPlan[Num][a+abc[0][Num-1]][b+abc[1][Num-1]]=Math.min(DynPlan[Num][a+abc[0][Num-1]][b+abc[1][Num-1]],abc[2][Num-1]+DynPlan[(Num-1)][a][b]);
				}
			}
		}

		for(int c=1;c*Math.max(Ma, Mb)<MaxWeight;c++){//最小コストの計算
			budget=Math.min(budget,DynPlan[N][c*Ma][c*Mb]);
		}
		if(budget==MaxCost)budget=-1;
		System.out.println(budget);
		
		sc.close();
	}
	public static int calc(){
		int calc=0;
		return calc;
	}
}



Measured memory_bytes: 41796
#include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define P pair<int, int>
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
const int INF = 1e9 + 7;
const int MAX_S = 105;
const int MAX_T = 105;
const int MAX_D = 105;
const int MAX_N = 105;
const int MAX_C = 105 * MAX_T;

// 体力 -> 消費カロリー
int dp1[MAX_T][MAX_T][MAX_S];
// 消費カロリー -> 幸福度
int dp2[MAX_C];
// 体力 -> 幸福度
int dp3[MAX_D][MAX_S];

int S, T, U, N, O, D; 
int e[MAX_T], c[MAX_T], h[MAX_N], a[MAX_N];

void make_dp1(){
  memset(dp1, -1, sizeof(dp1));
  dp1[0][0][0] = 0;
  REP(i, T){
    int E = e[i], C = c[i];
    REP(j, U + 1){
      REP(k, S + 1){
        if(dp1[i][j][k] < 0) continue;
        //トレーニングしない
        dp1[i + 1][j][k] = max(dp1[i + 1][j][k], dp1[i][j][k]);
        //する
        if(k + E <= S && j < U)
          dp1[i + 1][j + 1][k + E] = max(dp1[i + 1][j + 1][k + E], dp1[i][j][k] + C);
        //cout <<"dp1[" <<i <<"][" <<j <<"][" <<k <<"] = " <<dp1[i][j][k] <<endl;
      }
    }
  }
}

void make_dp2(){
  REP(i, N)
    FOR(j, a[i], MAX_C) dp2[j] = max(dp2[j], dp2[j - a[i]] + h[i]);
}

void make_dp3(){
  memset(dp3, -1, sizeof(dp3));
  dp3[0][S] = 0;
  REP(i, D){
    REP(j, S + 1){
      if(dp3[i][j] < 0) continue;
      //体力kを費やしてトレーニングをする
      for(int k = 0; k <= j; ++k){
        int kcal = dp1[T][U][k], ns = min(S, j - k + O);
        if(kcal == -1) continue;
        //cout <<k <<", " <<ns <<", " <<kcal <<", " <<dp2[kcal] <<endl;
        dp3[i + 1][ns] = max(dp3[i + 1][ns], dp3[i][j] + dp2[kcal]);
      }
      //cout <<"dp3[" <<i <<"][" <<j <<"] = " <<dp3[i][j] <<endl;
    }
  }
}

int main() {
  scanf("%d %d %d %d %d %d", &S, &T, &U, &N, &O, &D);
  REP(i, T) scanf("%d %d", &e[i], &c[i]);
  REP(i, N) scanf("%d %d", &h[i], &a[i]);
  make_dp1();
  make_dp2();
  make_dp3();
  int ans = -1;
  REP(i, S + 1) ans = max(ans, dp3[D][i]);
  printf("%d\n", ans);

  return 0;
}

Measured memory_bytes: 5804
S = input()
N = len(S)
MOD = 10**9 + 7
mask = 'ABC'

dp = [[0,0,0,0] for _ in range(N+1)]

for i in range(N, -1, -1) :
    for j in range(4) :
        if i == N :
            if j == 3 :
                dp[i][j] = 1
            else :
                dp[i][j] = 0
        else :
            if j == 3 :
                weight = 3 if S[i] == '?' else 1
                dp[i][j] = weight * dp[i+1][j]
            else :
                weight = 3 if S[i] == '?' else 1
                able = 1 if (S[i] == mask[j] or S[i] == '?') else 0
                dp[i][j] = weight * dp[i+1][j] + able * dp[i+1][j+1]
            dp[i][j] %= MOD


print(dp[0][0] % MOD)

Measured memory_bytes: 27600
#include <string>
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
int main()
{
   int a, b;
   cin >> a >> b;
   int min = a, max = b;
   if(a > b){
       min = b;
       max = a;
   }
   rep(i, max){
       cout << min;
   }
   cout << endl;
}

Measured memory_bytes: 3640
#include <iostream>
//#include <ios>
#include <vector>
#include <algorithm>

using namespace std;

int main(){
  ios::sync_with_stdio(false);
  int M, N;
  vector<int> u, v;

  //input
  cin >> M;
  for(int i = 0; i < M; ++i){
    int m;
    cin >> m;
    u.push_back(m);
  }

  cin >> N;
  for(int i = 0; i < N; ++i){
    int n;
    cin >> n;
    v.push_back(n);
  }

  //calc
  int res = 0;

  vector<int>& l = (M >= N ? u : v);
  vector<int>& s = (M >= N ? v : u);  

  for(auto x : s){
    if(find(l.begin(), l.end(), x) != l.end()){
      ++res;
    }
  }
  
  cout << res << endl;
}

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

signed main(){
    int t = 1; //cin >> t;
    while(t--){
        string s; cin >> s;
        if(s[s.size()-1] == 's')
        {
            cout << s << "es";
        }
        else cout << s << "s";
    }    
}

Measured memory_bytes: 3628
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i< (n); i++)
int main(){
int a=0, b=0, c=0, x=0;
cin >> a >> b >> c >> x;
int sum=0, count=0;
rep(i, a+1){
    rep(j, b+1){
        rep(k, c+1){
            sum=500*i+100*j+50*k;
            if(sum==x) count++;
        }
    }
}
cout << count << endl;

}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(V) V.begin(),V.end()
const ll MOD = 1000000007;

int main() {
	string S;
	cin >> S;
	if (S == "RRR") {
		cout << 3 << endl;
	}
	else if (S == "RRS" || S == "SRR") {
		cout << 2 << endl;
	}
	else if (S == "SSS") {
		cout << 0 << endl;
	}
	else {
		cout << 1 << endl;
	}
}

Measured memory_bytes: 3592
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const int MAX=1e5;
const int M=1e9;
int n,w;
vector<vector<ll>> dp(100,vector<ll>(MAX+1));
vector<int> wi(100);
vector<int> vi(100);
 
ll calc(int index,int val){
    if(index==-1){
        if(val!=0){
            return M+1; 
        }
        return 0;
    }
    if(dp[index][val]!=-1){
        return dp[index][val];
    }
    ll ans;
    if(val>=vi[index]){
        ans=min(wi[index]+calc(index-1,val-vi[index]),calc(index-1,val));
    }
    else{
        ans=calc(index-1,val);
    }
    return dp[index][val]=ans;
}
void solve(){
    cin>>n>>w;
    for(int i=0;i<n;i++){
        cin>>wi[i]>>vi[i];
        for(int j=0;j<=MAX;j++){
            dp[i][j]=-1;
        }
    }
    int max_happ=0;
    for(int i=0;i<=MAX;i++){
        ll res=calc(n-1,i);
        if(res<=w){
            max_happ=i;
        }
    }
    cout<<max_happ<<'\n';
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
    return 0;
}


Measured memory_bytes: 79360
#include <bits/stdc++.h>
using namespace std;
bool ans = true;
long long n, a[1000000], mx;
int main() {
	cin>>n;
	for (int i = 0; i < n; i++) {
		cin>>a[i];
		if (mx-1 > a[i]) ans = false;
		mx = max(mx,a[i]);
	}
	if (ans) cout<<"Yes";
	else cout<<"No";
}


Measured memory_bytes: 1024
#include<bits/stdc++.h>
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
static const ll MAX = 1000000000000000;
static const int NIL = -1;
using namespace std;
const long double EPS = 0.0000000001;
const long double PI = (acos(-1));
const int MOD = 1000000007;

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

bool is_prime(long long N) {
    if (N == 1) return false;
    for (long long i = 2; i * i <= N; ++i) {
        if (N % i == 0) return false;
    }
    return true;
}

int main(){

    int q,l,r; cin >> q;
    vector<int> p(100001,0);
    vector<int> s(100001,0);
    vector<int> ans(q,0);

    for(int i = 1; i <= 100000; i ++){
        if(i%2 == 0) s[i] = s[i-1];
        else if(is_prime(i) && is_prime((i+1)/2)){
            s[i] = s[i-1] + 1;
        }else {
            s[i] = s[i-1];
        }

    }

    rep(i,q){

        cin >> l >> r;
        ans[i] = s[r] - s[l-1];
    }

    rep(i,q) cout << ans[i] <<  endl;

}

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

#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i, x, n) for(int i = x; i >= n; i--)
#define rrep(i, n) RREP(i,n,0)
#define pb push_back

using namespace std;

// #define DEBUG_IS_VALID

#ifdef DEBUG_IS_VALID
#define DEB 1 
#else
#define DEB 0
#endif
#define DUMPOUT cout
#define dump(...) if(DEB) DUMPOUT<<"  "<<#__VA_ARGS__<<" :["<<__LINE__<<":"<<__FUNCTION__<<"]"<<endl<<"    "; if(DEB) dump_func(__VA_ARGS__)
template<typename T>ostream& operator << (ostream& os, vector<T>& vec) { os << "{"; for (int i = 0; i<vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : ", "); os << "}"; return os; }
void dump_func(){DUMPOUT << endl;}
template <class Head, class... Tail>void dump_func(Head&& head, Tail&&... tail){ DUMPOUT << head; if (sizeof...(Tail) == 0) { DUMPOUT << " "; } else { DUMPOUT << ", "; } dump_func(std::move(tail)...);}
template<class T> inline void chmax(T& a,T const& b){a=max(a,b);}
template<class T> inline void chmin(T& a,T const& b){a=min(a,b);}

using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;

const int mod=1e9+7,INF=1<<30;
const double EPS=1e-12,PI=3.1415926535897932384626;
const ll lmod = 1e9+7,LINF=1LL<<60; 

int N;
ll X[100005];
ll a,b;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> a >> b;
  rep(i,N) cin >> X[i];
  ll ans = 0LL;
  rep(i,N-1) ans += min((X[i+1]-X[i])*a,b);
  cout << ans << endl;
  return 0;
}


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

using namespace std;
#ifdef _DEBUG
#define _GLIBCXX_DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define ll long long
#define ll1 1ll
#define ONE 1ll
#define DBG 1
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define loop(n) rep(loop, (0), (n))
#define all(c) begin(c), end(c)
const int INF =
sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
using pii = pair<int, int>;
// template<class T> ostream &operator<<(ostream &os,T &t){dump(t);return os;}
template <typename T, typename S>
istream &operator>>(istream &is, pair<T, S> &p) {
	is >> p.first >> p.second;
	return is;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> &p) {
	os << p.first << " " << p.second;
	return os;
}

template <typename T> void printvv(const vector<vector<T>> &v) {
	cerr << endl;
	rep(i, 0, v.size()) rep(j, 0, v[i].size()) {
		if (typeid(v[i][j]).name() == typeid(INF).name() and v[i][j] == INF) {
			cerr << "INF";
		}
		else
			cerr << v[i][j];
		cerr << (j == v[i].size() - 1 ? '\n' : ' ');
	}
	cerr << endl;
}
/*
typedef __int128_t Int;
std::ostream &operator<<(std::ostream &dest, __int128_t value) {
std::ostream::sentry s(dest);
  if (s) {
	__uint128_t tmp = value < 0 ? -value : value;
	char buffer[128];
	char *d = std::end(buffer);
	do {
	  --d;
	  *d = "0123456789"[tmp % 10];
	  tmp /= 10;
	} while (tmp != 0);
	if (value < 0) {
	  --d;
	  *d = '-';
	}
	int len = std::end(buffer) - d;
	if (dest.rdbuf()->sputn(d, len) != len) {
	  dest.setstate(std::ios_base::badbit);
	}
  }
  return dest;
}

__int128 parse(string &s) {
  __int128 ret = 0;
  for (int i = 0; i < s.length(); i++)
	if ('0' <= s[i] && s[i] <= '9')
	  ret = 10 * ret + s[i] - '0';
  return ret;
}
*/

#ifndef _DEBUG
#define printvv(...)
#endif
void YES(bool f) { cout << (f ? "YES" : "NO") << endl; }
void Yes(bool f) { cout << (f ? "Yes" : "No") << endl; }
template <class T> bool chmax(T &a, const T &b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}
template <class T> bool chmin(T &a, const T &b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}

signed main(signed argc, char *argv[]) {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(12);

	int N, M; cin >> N >> M;
	vector<pii>v(N); rep(i, 0, N) { cin >> v[i]; }
	sort(all(v));
	int ans = 0;
	rep(i, 0, N) {
		int n=min(M, v[i].second);
		ans += n * v[i].first;
		M -= n;
	}
	cout << ans << endl;

	return 0;
}


Measured memory_bytes: 1792
def solve(n, P, Q):
    facts = [1] * n
    for i in range(2, n):
        facts[i] = facts[i - 1] * i

    def count_order(X):
        cands = [True] * n
        num = 0
        for i, x in enumerate(X):
            num += sum(cands[:x - 1]) * facts[n - i - 1]
            cands[x - 1] = False
        return num

    return abs(count_order(P) - count_order(Q))

_n = int(input())
_P = list(map(int, input().split()))
_Q = list(map(int, input().split()))
print(solve(_n, _P, _Q))


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

int main() {
  int A, B;
  char opp;
  cin >> A >> opp >> B;

  if (opp == '+') {
      cout << A + B << endl;
  }

  if (opp == '-') {
      cout << A - B << endl;
  }

}


Measured memory_bytes: 256
import Data.Int
main = do
  [n,m] <- map read . words <$> getLine :: IO [Int64]
  print $ solve n m 

inf :: Int64
inf = 10^9+7

powMod :: Int64 -> Int64 -> Int64
powMod x y = (x*y) `mod` inf

prod :: Int64 -> Int64
prod 1 = 1
prod x = powMod x (prod (x-1))

solve :: Int64 -> Int64 -> Int64
solve x y
  | abs (x-y) > 1 = 0
  | even (x+y) = powMod (powMod (prod x) (prod y)) 2
  | otherwise = powMod (prod x) (prod y)

Measured memory_bytes: 3068