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

using namespace std;
#define rep(i, N) for(int i=0;i<int(N);++i)
#define rep1(i, N) for(int i=1;i<int(N);++i)
#define all(a) (a).begin(),(a).end()
#define print(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<_<<", "; cerr<<"]"<<endl; }
#define printpair(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<"{"<<_.first<<","<<_.second<<"}"<<", "; cerr<<"]"<<endl; }
#define dump(x) cerr<<#x<<": "<<x<<endl;
#define bit(k) (1LL<<(k))
typedef long long ll;
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
template<class T> using vec = vector<T>;
template<class T> using vvec = vector<vec<T>>;

template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
    os << "{" << p.first << ", " << p.second << "}";
    return os;
}

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

const int INF = (ll) 1e9;
const ll INFLL = (ll) 1e18 + 1;
const ll MOD = (ll) 1e9 + 7;
const double PI = acos(-1.0);

/*
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const string dir = "DRUL";
*/
template<typename T, typename U>
T _pow(T a, U n) {
    T res = 1;
    rep(i, n)res *= a;
    return res;
}
template<typename T> 
map<T,int> factorize(T x){
    map<T,int> mp;
    for (T i = 2; i*i <= x; i++){
        while (x%i == 0) {
            x /= i;
            mp[i]++;
        }
        if (x == 1) break;
    }
    if (x != 1) mp[x]++;
    return mp;
}
vector< int64_t > divisor(int64_t n) {
  vector< int64_t > ret;
  for(int64_t i = 1; i * i <= n; i++) {
    if(n % i == 0) {
      ret.push_back(i);
      if(i * i != n) ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}
int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    ll X;
    cin >> X;
    auto v = divisor(X);
    //print(v);
    int sz = v.size();
    rep(i,sz){
        //a-b = v[i]とする
        //a = b + v[i]
        for(ll b = -1000; b <= 1000;b++){
            ll a = b + v[i];
            ll sl  = _pow(a,5) - _pow(b,5);
            if(sl == X){
                cout << a << " " << b << endl;
                return 0;
            }
            a = b - v[i];
            sl  = _pow(a,5) - _pow(b,5);
            if(sl == X){
                cout << a << " " << b << endl;
                return 0;
            }
        }
    }
}

Measured memory_bytes: 3656
#include<bits/stdc++.h>
using namespace std;
int main(){
  long N;cin>>N;
  vector<long>A(N),SP(N),SN(N);
  long ansp=0,ansn=0;
  for(int i=0;i<N;i++)cin>>A[i];
  for(int i=0;i<N;i++){
    SP[i]=(i==0?0:SP[i-1])+A[i];
    if(i%2==0&&SP[i]<=0){ansp+=1-SP[i];SP[i]=1;}
    if(i%2==1&&SP[i]>=0){ansp+=SP[i]+1;SP[i]=-1;}
  }
  
  for(int i=0;i<N;i++){
    SN[i]=(i==0?0:SN[i-1])+A[i];
    if(i%2==0&&SN[i]>=0){ansn+=SN[i]+1;SN[i]=-1;}
    if(i%2==1&&SN[i]<=0){ansn+=1-SN[i];SN[i]=1;}
  }
  cout<<min(ansp,ansn);
}



Measured memory_bytes: 2560
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <cctype>
#include <iostream>
#include <functional>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <iterator>
#include <cmath>

using namespace std;

int main()
{
    //freopen("in", "r", stdin);
    //freopen("out", "w", stdout);

    long long n, m;
    scanf("%lld%lld", &n, &m);

    long long r = min(n, m / 2);
    n -= r;
    m -= 2 * r;

    r += m / 4;
    printf("%lld\n", r);
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

typedef vector<int> vi;

int n;
int b[10][10];
vi ans[12];
int sx[] = {-1,0,1,0}, sy[] = {1,1,1,2};
int mx[] = {1,2,0,1,2,0,1,2}, my[] = {0,0,1,1,1,2,2,2};
int lx[] = {-1,0,1,-2,-1,0,1,2,-1,0,1,0}, ly[] = {1,1,1,2,2,2,2,2,3,3,3,4};

bool rec(int d){
  if(d==n){
    rep(i,10)rep(j,10)if(b[i][j])return false;
    return true;
  }

  rep(i,10)rep(j,10){
    if(b[i][j]){
      b[i][j]--;
      bool flag = false;
      if(i<6 && j>1 && j<8){
	bool f = true;
	rep(k,12){
	  if(!b[i+ly[k]][j+lx[k]]){ f = false; break; }
	}
	
	if(f){
	  rep(k,12)b[i+ly[k]][j+lx[k]]--;
	  vi v(3); v[0] = j; v[1] = i+2; v[2] = 3;
	  ans[d] = v;
	  if(rec(d+1))return true;
	  rep(k,12)b[i+ly[k]][j+lx[k]]++;
	  flag = true;
	}
      }

      if(i<8 && j<8){
	bool f = true;
	rep(k,8){
	  if(!b[i+my[k]][j+mx[k]]){ f = false; break; }
	}
	
	if(f){
	  rep(k,8)b[i+my[k]][j+mx[k]]--;
	  vi v(3); v[0] = j+1; v[1] = i+1; v[2] = 2;
	  ans[d] = v;
	  if(rec(d+1))return true;
	  rep(k,8)b[i+my[k]][j+mx[k]]++;
	  flag = true;
	}
      }

      if(i<8 && j>0 && j<9){
	bool f = true;
	rep(k,4){
	  if(!b[i+sy[k]][j+sx[k]]){ f = false; break; }
	}
	
	if(f){
	  rep(k,4)b[i+sy[k]][j+sx[k]]--;
	  vi v(3); v[0] = j; v[1] = i+1; v[2] = 1;
	  ans[d] = v;
	  if(rec(d+1))return true;
	  rep(k,4)b[i+sy[k]][j+sx[k]]++;
	  flag = true;
	}
      }
      b[i][j]++;
      if(!flag)return false;
    }
  }
  return false;
}

int main(){
  cin >> n;
  rep(i,10)rep(j,10)cin >> b[i][j];
  rec(0);
  rep(i,n)cout << ans[i][0] << " " << ans[i][1] << " " << ans[i][2] << endl;
}

Measured memory_bytes: 1216
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>

using namespace std;
int main(){
  int N;
  cin >> N;

  vector<int> H(N);
  for(int i=0;i<N;i++){
    cin >> H[i];
  }
  int res = 0;
  int sum;
  do{
    int start = 0;
    int min = 0;
    for(int i=0;i<N;i++){
      if(H[i] != 0){
        if(min == 0){
          min = H[i];
        }else if(H[i]<min){
          min = H[i];
        }
        if(i == N-1){
          for(int j=start;j<=i;j++){
            H[j] -= min;
          }
          res += min;
        }
      }else{
        for(int j=start;j<i;j++){
          H[j] -= min;
        }
        res += min;
        start = i+1;
        min = 0;
      }
    }


    sum = 0;
    for(int i=0;i<N;i++){
      sum += H[i];
    }
  }while(sum > 0);
  cout << res << endl;
  return 0;
}


Measured memory_bytes: 256
#include<stdio.h>

int main(void)
{
	int i;
	int t, n, max1, max2, min1, min2, b0, b1, b2, b3, b4, m1, m2, m3, m4, s1, s2, s3, s4;
	while (1)
	{
		scanf("%d", &n);
		if (n == 0)
			break;
		max1 = max2 = 14400;
		min1 = min2 = -1;
		for (i = 0; i < n; i++)
		{
			scanf("%d%d%d%d%d%d%d%d%d", &b0, &m1, &s1, &m2, &s2, &m3, &s3, &m4, &s4);
			t = m1 * 60 + m2 * 60 + m3 * 60 + m4 * 60 + s1 + s2 + s3 + s4;
			if (t < max1)
			{
				max2 = max1;
				b2 = b1;
				max1 = t;
				b1 = b0;
			}
			else if (t < max2)
			{
				max2 = t;
				b2 = b0;
			}
			if (t > min1)
			{
				min2 = min1;
				b3 = b4;
				min1 = t; 
				b4 = b0;
			}
			else if (t > min2)
			{
				min2 = t;
				b3 = b0;
			}
		}
		printf("%d\n%d\n%d\n", b1, b2, b3);
	}
	return 0;
}

Measured memory_bytes: 600
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  	vector<int> data(3);
  	for(int i = 0; i < 3; i++){
    	cin >> data.at(i);
    }
  
  	swap(data.at(0), data.at(1));
  	swap(data.at(0), data.at(2));
  
  	for(int i = 0; i < 3; i++){
    	cout << data.at(i)<< " ";
    }
  
  	cout << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define rep(X,A,B) for(int X=A;X<=B;X++)
#define tep(X,A,B) for(int X=A;X>=B;X--)
#define pb push_back
#define LL long long
const LL MOD=998244353;
const int N=200005;
const int MAXN=15;
const int maxn=14;
const int MXX=19;
const int mxx=18;
using namespace std;

int n,a[N];
LL lef[N],rit[N];

void READ(){
	scanf("%d",&n);
	rep(i,1,n)scanf("%d",&a[i]),a[i]-=i;
	sort(a+1,a+n+1);
}

LL SOLVE(){
	rep(i,1,n)lef[i]=lef[i-1]+a[i];
	tep(i,n,1)rit[i]=rit[i+1]+a[i];
	LL ans=rit[1]-1LL*a[1]*n;
	rep(i,2,n){
		ans=min(ans,1LL*a[i]*i-lef[i]+rit[i]-1LL*a[i]*(n-i+1));
	}
	return ans;
}

int main(){
	READ();
	printf("%lld\n",SOLVE());
	return 0;
}

Measured memory_bytes: 4096
#include <iostream>
#include <queue>
#include <vector>
#include <cstring>

using namespace std;

constexpr int MAX_N = 50;
constexpr int MAX_C = 100;
constexpr int INF = (1 << 29);

vector<int> G[MAX_N], G2[MAX_N][3];
bool visited[MAX_N][MAX_C];

bool dfs(int curr, int goal, int rem)
{
    if (visited[curr][rem]) return 0;
    visited[curr][rem] = 1;
    
    if (rem == 0) {
        return (curr == goal);
    }    
    for (auto& next : G[curr]) {
        if (dfs(next, goal, rem - 1)) {
            return 1;
        }
    }
    return 0;
}

void solve(int N)
{    
    vector<int> d(N, INF);
    d[N - 1] = 0;

    bool update = 1;
    while (update) {
        update = 0;

        for (int i = 0; i < N; i++) {
            int cost = 0;
            for (int j = 0; j < 3; j++) {
                int mc = INF;
                for (auto& u : G2[i][j]) {
                    mc = min(mc, d[u]);
                }
                cost = max(cost, mc + 1);                
            }

            if (cost < d[i]) {
                d[i] = cost;
                update = 1;
            } 
        }            
    }
    
    if (d[0] == INF) {
        cout << "IMPOSSIBLE" << endl;
    } else {
        cout << d[0] << endl;
    }
}

int main()
{
    int N, M, a[3];
    cin >> N >> M >> a[0] >> a[1] >> a[2];
    for (int i = 0; i < M; i++) {
        int u, v;
        cin >> u >> v; u--; v--;
        G[u].emplace_back(v);
    }
    
    for (int i = 0; i < 3; i++) {
        for (int from = 0; from < N; from++) {
            for (int to = 0; to < N; to++) {
                if (from == to) continue;
                memset(visited, 0, sizeof(visited));
                if (dfs(from, to, a[i])) {
                    G2[from][i].emplace_back(to);
                }
            }
        }
    }       
    solve(N);
    return 0;
}

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

int n, m;
vector<int> v[100010];
vector<int> tpv;
int par[100010];
int used[100010];

void tp(int t) {
	used[t] = 1;
	for (int i = 0; i < v[t].size(); i++) {
		int u = v[t][i];
		if (used[u] == 1) continue;
		tp(u);
	}
	tpv.push_back(t);
	return;
}

int main(){
	cin >> n >> m;
	vector<bool> b(n+1, true);
	for (int i = 0; i < n+m-1; i++) {
		int p, q; cin >> p >> q;
		v[p].push_back(q);
		b[q] = false;
	}
	for (int i = 1; i <= n; i++) {
		if (b[i]) {
			tp(i);
		}
	}
	reverse(tpv.begin(), tpv.end());
	for (int i = 0; i < n; i++) {
		int t = tpv[i];
		for (int j = 0; j < v[t].size(); j++) {
			int s = v[t][j];
			par[s] = t;
		}
	}
	for (int i = 0; i < n; i++) {
		cout << par[i+1] << endl;
	}
	return 0;
}


Measured memory_bytes: 10744
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <queue>
using ll = long long;
using graph = std::vector<std::vector<ll>>;

using namespace std;

int main() {
    int N;
    cin >> N;
    map<string,ll> m = map<string,ll>();
    for (int i = 0; i < N; ++i) {
        string tmp;
        cin >> tmp;
        m[tmp]++;
    }
    ll M = 0;
    auto begin = m.begin(), end = m.end();
    for (auto iter = begin; iter != end; iter++) {
        // first: key, second: value
        if(M < iter -> second){
            M = iter -> second;
        }
    }
    for (auto iter = begin; iter != end; iter++) {
        // first: key, second: value
        if(M == iter -> second){
            cout << iter -> first << endl;
        }
    }

}


Measured memory_bytes: 25216
#include <bits/stdc++.h>
using namespace std;
long long int n,m,c[102],v[102],dp[106][100006],mx=1e5;

long long int cal(int i,int val){ 
    if(i==n+1){ 
        if(val==0)return 0;
        else return 1e18;
    }
    if(dp[i][val]!=-1)return dp[i][val];
    long long int sol=cal(i+1,val);
    if(val-v[i]>=0)sol=min(sol,cal(i+1,val-v[i])+c[i]);
    return dp[i][val]=sol;
} 
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++){ 
        cin>>c[i]>>v[i];
    
    }
    memset(dp,-1,sizeof dp);
    //cout<<cal(0,90)<<endl; 
    for(int i=mx;i>=0;i--){ 
        if(cal(0,i)<=m){ 
            cout<<i;
            return 0;
        }
    } 

        
        
        
        
    return 0;
}


Measured memory_bytes: 83072
from heapq import heappop,heappush
n = int(input())
A = sorted(list(map(int,input().split())))
P = []
M = []
if n == 2:
    print(A[1]-A[0])
    print(A[1],A[0])
    exit()
def move():
    while len(P)!= 1:
        mm = heappop(M)
        pp = heappop(P)
        print(mm,pp)
        heappush(M,mm-pp)
    while len(M) != 0:
        mm = heappop(M)
        pp = heappop(P)
        print(pp,mm)
        heappush(P,pp-mm)
for a in A:
    if a>=0:
        P.append(a)
    else:
        M.append(a)

if A[-1]>=0 and A[0]<0:
    print(sum(abs(a) for a in A))
    while len(P)!= 1:
        mm = heappop(M)
        pp = heappop(P)
        print(mm,pp)
        heappush(M,mm-pp)
    while len(M) != 0:
        mm = heappop(M)
        pp = heappop(P)
        print(pp,mm)
        heappush(P,pp-mm)
elif A[0]>=0:
    print(sum(A)-min(A)*2)
    mm = heappop(P)
    pp = heappop(P)
    print(mm,pp)
    heappush(M,mm-pp)
    while len(P)!= 1:
        mm = heappop(M)
        pp = heappop(P)
        print(mm,pp)
        heappush(M,mm-pp)
    while len(M) != 0:
        mm = heappop(M)
        pp = heappop(P)
        print(pp,mm)
        heappush(P,pp-mm)
else:
    print(sum(abs(a) for a in A)+max(A)*2)
    mm = M[-1]
    pp = M[-2]
    print(mm,pp)
    M = M[:-2]
    heappush(P,mm-pp)
    while len(P)!= 1:
        mm = heappop(M)
        pp = heappop(P)
        print(mm,pp)
        heappush(M,mm-pp)
    while len(M) != 0:
        mm = heappop(M)
        pp = heappop(P)
        print(pp,mm)
        heappush(P,pp-mm)

Measured memory_bytes: 14520
#include<bits/stdc++.h>
#define ll long long int
#define ul unsigned long long int
#include<vector>
#define mp make_pair
#define endl "\n"
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define lb lower_bound
#define forn(i,n) for(ll i=0 ; i<n ; i++)
#define arr(a,n) for(ll i=0;i<n;i++){ cin>>a[i]; }
#define ub upper_bound
#define pb push_back
#define mod 1000000007
using namespace std;

ll ipow(ll x, ll n) 
{ 
    ll result = 1; 
    while (n) { 
        if (n & 1) 
            result = result * x % mod; 
        n = n / 2; 
        x = x * x % mod; 
    } 
    return result; 
} 

int main(){
	int k,a,b;
	cin>>k>>a>>b;
	int flag=0;
	for(int i=a;i<=b;i++){
		if(i%k==0){
			flag=1;
			break;
		}
	}
	if(flag==1)
	cout<<"OK"<<endl;
	else
	cout<<"NG"<<endl;
return 0;
}

Measured memory_bytes: 3604
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <string>
#define PB push_back
#define FT first
#define SD second
#define MP make_pair
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int>  P;
const int N=105,MOD=7+1e9;
int a[20],G[N][N];
int n,m,ans,cnt;
int solve()
{
    if(cnt<2) return 0;
    int res=0;
    for(int i=0;i<cnt;i++){
        int _min = INF;
        for(int j=0;j<cnt;j++) {
            if(i==j) continue;
            _min = min(_min,G[a[i]][a[j]]);
        }
        res += _min;
    }
    return res;
}
void dfs(int u)
{
    ans = max(ans,solve());
    for(int i=u;i<=n;i++){
        bool fg=1;
        for(int j=0;j<cnt;j++){
            int v=a[j];
            if(G[i][v]==0) {
                fg=0;
                break;
            }
        }
        if(fg) {
            a[cnt++]=i;
            dfs(i+1);
            cnt--;
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    ans=0;
    for(int i=0;i<m;i++){
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        G[u][v]=G[v][u]=w;
        ans = max(ans,w*2);
    }
    dfs(1);
    printf("%d\n",ans);
    return 0;
}

Measured memory_bytes: 1232
#include"bits/stdc++.h"
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template<typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2>& l, const pair<T1, T2>& r) { return make_pair(l.first + r.first, l.second + r.second); }
template<typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2>& l, const pair<T1, T2>& r) { return make_pair(l.first - r.first, l.second - r.second); }
typedef pair<int, int> P;

lint N, K, maxv, _gcd;

lint gcd(lint a, lint b) {
	if (b == 0) return a;
	else return gcd(b, a % b);
}

int main() {
	cin.tie(0); ios_base::sync_with_stdio(false);
	cin >> N >> K;
	vector<lint> vec(N);
	REP(i, N) {
		cin >> vec[i];
	}
	maxv = vec[0];
	_gcd = vec[0];
	REP(i, N - 1) {
		_gcd = gcd(_gcd, vec[i + 1]);
		maxv = max(maxv, vec[i + 1]);
	}
	if (K > maxv) {
		cout << "IMPOSSIBLE";
		return 0;
	}
	else if (K % _gcd != 0) {
		cout << "IMPOSSIBLE";
		return 0;
	}
	cout << "POSSIBLE";
}


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

#define fi first
#define se second
#define be begin
#define en end
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ALL(a) (a).be() , (a).en()
#define REP(i,n) for(int (i)=0;(i)<(n);(i)++)        //repeat n times
#define REP2(i,s,n) for(int (i)=(s);(i)<(n);(i)++)   //repeat from s to n 
#define REPD(i,n) for(int (i)=(n);(i)>=0;(i)--)      //repeat from n to 0
#define REPD2(i,s,e) for(int (i)=(s);(i)>=(e);(i)--) //repeat from s to e
#define RANGE(i,v) for(auto &(i):v)                  //repeat range
#define ASIZE(a) (sizeof(a) / sizeof(a[0]))          //array size

using LL = long long;

template<typename T> using V = vector< T >;
using Vi = V<int>;
using Vll = V<LL>;
using Vs = V<string>;

LL f(Vll a, LL b){
    LL sum_ab = 0;
    for( LL i : a){
        sum_ab += abs(i-b);
    }
    return sum_ab;
}

int main(){
    int N;
    cin>>N;
    Vll A(N);
    REP(i,N) cin>>A[i];
    Vll AA(N);
    REP(i,N) AA[i] = A[i] - (i+1);
    sort(ALL(AA));
    if(N%2==1){
        cout<<f(AA,AA[N/2])<<endl;
    }else{
        cout<<(f(AA,AA[N/2])+f(AA,AA[N/2-1]))/2<<endl;
    }
    return 0;
}

Measured memory_bytes: 6528
//   _/                _/                 _/_/_/   _/
//_/_/_/_/   _/_/   _/_/_/_/   _/_/    _/       _/_/
// _/     _/    _/   _/     _/    _/  _/_/_/     _/
//_/     _/    _/   _/     _/    _/  _/    _/   _/
// _/_/   _/_/       _/_/   _/_/      _/_/     _/
#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<map>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
const int MOD=1e9+7;
const double pi=3.14159265358979323846;
const int inf=1e9;
const ll INF=5e18;
typedef pair<int,int> P;
int main() {
  cin.tie(0),cout.tie(0);
  ios::sync_with_stdio(false);
  string s,t,ans="";
  int n,m,dp[3005][3005]={},cnt;
  cin >> s >> t;
  n=s.size(),m=t.size();
  for(int i=1; i<=n; i++) {
    for(int j=1; j<=m; j++) {
      if(s[i-1]==t[j-1]) {
        dp[i][j]=dp[i-1][j-1]+1;
      }
      else {
        dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
      }
    }
  }
  while(dp[n][m]) {
    if(dp[n][m]==dp[n][m-1]) {
      m--;
    }
    else if(dp[n][m]==dp[n-1][m]) {
      n--;
    }
    else {
      ans+=s[n-1];
      n--,m--;
    }
  }
  reverse(ans.begin(),ans.end());
  cout << ans << endl;
}

Measured memory_bytes: 35584
<?php
fscanf(STDIN, "%d %d", $N, $x);
$a = explode(" ", trim(fgets(STDIN)));

sort($a);
// var_dump($a);

$ans = 0;

foreach ($a as $key => $v) {
    if ($key == $N - 1) {
        if ($v == $x) {
            $ans++;
        }
    } else {
        if ($v > $x) {
            break;
        } else {
            $x -= $v;
            // echo $x . PHP_EOL;
            $ans++;
        }
    }
}

echo $ans;



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

int main(){
  int n;
  cin >> n;
  int s;
  int sum=0;
  int ss[n] = {};
  for (int i=0; i<n; i++){
    cin >> s;
    sum += s;
    ss[i] = sum;
  }
  //cout << ss[0] << " " << ss[1] << " " << ss[2] << endl;
  int ans=100000;
  for (int j=0; j<n; j++){
    ans = min(ans, abs(sum-2*ss[j]));
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())

using namespace std;
using ll = long long;

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 void dump(vector<T> v) { for (auto& x : v) cerr << x << " "; cerr << endl; }
template<class T> inline void dump(vector<pair<T, T>> v) { for (auto& p : v) cerr << p.first << " " << p.second << endl; }
template<class T> inline void dump(vector<vector<T>> vv) { for (auto& v : vv) {for (auto& x : v) cerr << x << " "; cerr << endl;} }

constexpr int INF = 1e9 + 5;
constexpr long long INFLL = 1LL << 62;
constexpr double eps = (1e-9);

constexpr int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return x ? mod - x : 0;}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}

  friend ostream& operator << (ostream& os, const mint& m) noexcept {
    return os << m.x;
  }

  friend istream& operator<< (istream& is, mint& m) noexcept {
    return is >> m.x;
  }
};

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

  string l;
  cin >> l;
  int n = l.size();

  vector<vector<mint>> dp(n+1, vector<mint>(2, 0));
  dp[0][0] = 2;  // {1, 0}, {0, 1}
  dp[0][1] = 1;  // {0, 0}

  for (int i = 0; i < n - 1; i++) {
    // smaller -> smaller
    dp[i+1][1] += dp[i][1] * 3;
    // exact -> smaller
    dp[i+1][1] += dp[i][0] * (l[i+1] == '0' ? 0 : 1);
    // exact -> exact
    dp[i+1][0] += dp[i][0] * (l[i+1] == '0' ? 1 : 2);
  }

  mint ans = dp[n-1][0] + dp[n-1][1];

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 8720
import heapq
import sys
input=sys.stdin.readline

def dijkstra_heap(s):
  d=[float("inf")]*n
  used=[True]*n 
  d[s]=0
  used[s]=False
  edgelist=[]
  for e in edge[s]:
    heapq.heappush(edgelist,e)
  while len(edgelist):
    minedge=heapq.heappop(edgelist)
    if not used[minedge[1]]:
      continue
    v=minedge[1]
    d[v]=minedge[0]
    used[v]=False
    for e in edge[v]:
      if used[e[1]]:
        heapq.heappush(edgelist,[e[0]+d[v],e[1]])
  return d

N,m,s=map(int,input().split())
L=[list(map(int,input().split())) for i in range(m)]
L2=[list(map(int,input().split())) for i in range(N)]
n=N*(50*(N-1)+1)
edge=[[] for i in range(n)]
for i in range(m):
  for j in range(50*(N-1)+1):
    if j>=L[i][2]:
      edge[L[i][0]-1+N*j].append([L[i][3],L[i][1]-1+N*(j-L[i][2])])
      edge[L[i][1]-1+N*j].append([L[i][3],L[i][0]-1+N*(j-L[i][2])])
for i in range(N):
  for j in range(50*(N-1)):
    edge[i+N*j].append([L2[i][1],i+N*min(50*(N-1),j+L2[i][0])])
ans=[[] for i in range(N)]
D=dijkstra_heap(min(50*(N-1),s)*N)
for i in range(len(D)):
  ans[i%N].append(D[i])
for i in range(1,N):
  print(min(ans[i]))

Measured memory_bytes: 173608
h,w=gets.split(" ").map(&:to_i)
n=gets.to_i
a=gets.split(" ").map(&:to_i)
ret=Array.new(h){Array.new(w,0)}
x=0
y=0
for i in 0...n
    a[i].times{
        ret[y][x]=i+1
        if y&1==1
            if x==0
                y+=1
            else
                x-=1
            end
        else
            if x==w-1
                y+=1
            else
                x+=1
            end
        end
    }
end
for i in 0...h
    puts ret[i].join(" ")
end

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

int main() {
    int n;
    cin>>n;
    vector<int> a(n),b(n);
    int ans = 0;
    rep(i,n) cin>>a[i];
    rep(i,n) cin>>b[i];
    
    vector<int> s(n+1),t(n+1);
    
    rep(i,n){
        s[i+1] = s[i] +a[i];
    }
    rep(i,n){
        t[i+1] = t[i] +b[i];
    }
    for(int i=1;i<=n;i++){
        ans = max(ans, s[i]+t[n]-t[i-1]);
    }
    cout<< ans << endl;
}



Measured memory_bytes: 256
#include <iostream>

using namespace std;
using ll = long long;
const int MX = 200010;
ll f[MX],inv[MX],fi[MX];
constexpr ll mod = 998244353;
void solve(){
    inv[1] = 1;
    for(int i=2;i<MX;i++){
        inv[i] = mod - (mod/i)*inv[mod%i]%mod;
    }
   f[0] = fi[0] = 1;
    for(int i=1;i<MX;i++){
        f[i] = f[i-1]*i%mod;
        fi[i] = fi[i-1]*inv[i]%mod;
    }
}

ll nck(ll n, ll k){
    if(n<0 || k<0 || n<k) return 0;
    return f[n]*fi[k]%mod*fi[n-k]%mod;
}

ll pw(ll a, ll x){
    ll ret = 1;
    while(x){
        if(x&1) (ret *= a) %= mod;
        (a *= a) %= mod; x /= 2;
    }
    return ret;
}

int main(){
    int i,n,m,k;
    cin >> n >> m >> k;
    solve();
    ll ans = 0;
    for(i=0;i<=k;i++){
        (ans += (nck(n - 1,n - 1 - i)*m%mod*pw(m - 1,n - i - 1))%mod) %= mod;
    }
    cout << ans << endl;
}

Measured memory_bytes: 8328
#include<bits/stdc++.h>
using namespace std;
int main(){
    while(1){
        int m,f,r;
        cin>>m>>f>>r;
        if(m==-1&&f==-1&&r==-1)break;
        if(m==-1||f==-1){
            cout<<'F'<<endl;
        }
        else if(m+f>=80)cout<<'A'<<endl;
        else if(m+f>=65)cout<<'B'<<endl;
        else if(m+f>=50)cout<<'C'<<endl;
        else if(m+f<30)cout<<'F'<<endl;
        else if(r>=50)cout<<'C'<<endl;
        else cout<<'D'<<endl;
    }
}



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

int main()
{
  std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
  i64 a, t, r;
  cin >> a >> t >> r;
  cout << (double)t / a * r << endl;
  return 0;
}



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

k = [0] * n
while 1:
    f = 0
    for i in range(n):
        if a[i] % 2 == 0:
            f = 1
            a[i] //= 2
            k[i] += 1
    if not f:
        break
print(sum(k))

Measured memory_bytes: 4148
#include<stdio.h>
#include<limits.h>
#define N 1000
 
int min(i,j){
  if(i >=j) return j;
  else{
 return i;
  }
}
 
void matrix(int p[],int a){
  int i,m[N][N],l,j,k,q,n;
  n = a - 1;
  for(i = 1; i <= n; i++){
    m[i][i] = 0;
  }
  for(l = 2; l <= n; l++)
    {
    for(i = 1; i <= n-l+1; i++)
      {
      j = i + l - 1;
      m[i][j] = INT_MAX;
      for(k = i; k <= j-1;k ++)
    {
        q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j];
        m[i][j] = min(m[i][j], q);
    }
      }
    }
  printf("%d\n",m[i][j]);
}
 
int main(){
  int a[100],i,t,n;
 
  scanf("%d",&n);
 
  for(i = 1; i < n; i ++){
    scanf("%d %d",&a[i],&t);
  }
  scanf("%d %d",&a[i],&a[i+1]);

  matrix(a,n+2);
 
  return 0;
}

Measured memory_bytes: 2956
#define NDEBUG
NDEBUG


#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cstring>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <memory>
#include <queue>
#include <random>


using namespace std;


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


typedef int64_t i64;


// TC_REMOVE_BEGIN
/// caide keep
bool __hack = std::ios::sync_with_stdio(false);
/// caide keep
auto __hack1 = cin.tie(nullptr);
// TC_REMOVE_END


// Section with adoption of array and vector algorithms.


//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//
//template <class T> using StdTree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;


namespace template_util {
    

    constexpr int bytecount(uint64_t x) {
        return x ? 1 + bytecount(x >> 8) : 0;
    }

    template<int N>
    struct bytetype {
        
    };

    
    /// caide keep
    template<uint64_t N>
    struct minimal_uint : bytetype<bytecount(N)> {
    };
}


template<class T>
T next(istream& in) {
    T ret;
    in >> ret;
    return ret;
}


#define dbg(...) ;


void solve(istream& in, ostream& out) {
  auto n = next<int>(in);
  i64 countE = 0;
  i64 countV = 0;
  FORA(i, 1, n + 1) {
    countV += i * 1LL * (n - i + 1);
  }
  FOR(i, n-1) {
    auto x = next<i64>(in);
    auto y = next<i64>(in);
    if (x > y) swap(x, y);
    countE += x * (n - y + 1);
  }
  dbg(countV, countE);
  out << countV - countE << "\n";
}

//#include <fstream>


int main() {
//  ifstream fin("bridges.in");
//  ofstream fout("bridges.out");
    solve(cin, cout);
//  solve(fin, fout);
    return 0;
}



Measured memory_bytes: 3620
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<utility>
#include<cmath>
#include<climits>
#include<queue>
#include<stack>
#include<numeric>
#include<set>
#include<iomanip>
#include<map>
#include<type_traits>
#include<tuple>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define rep(i,N) for(ll (i)=0;(i)<(N);(i)++)
const int mod = 1000000007;

int main(){
	int n;
	cin >> n;
	if(n == 7 || n == 5 || n == 3)	cout << "YES" <<endl;
	else cout << "NO" << endl;
}

Measured memory_bytes: 256
from itertools import accumulate
from math import ceil

N = int(input())
A = list(map(int, input().split()))


class BinaryIndexedTree:
    def __init__(self, n):
        self.size = n
        self.bit = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.bit[i]
            i -= (i & -i)
        return s

    def add(self, i, x):
        while i <= self.size:
            self.bit[i] += x
            i += (i & -i)

    def reset(self):
        self.bit = [0] * (self.size + 1)


BIT = BinaryIndexedTree(N + 1)
ng, ok = 10 ** 9 + 1, 0
while abs(ok - ng) > 1:
    X = (ok + ng) // 2

    B = [(1 if a >= X else -1) for a in A]
    B = [0] + list(accumulate(B))

    tmp = 0
    B_sorted = {b: i for i, b in enumerate(sorted(B), start=1)}
    BIT.reset()
    for j, b in enumerate(B):
        tmp += BIT.sum(B_sorted[b])
        BIT.add(B_sorted[b], 1)

    if tmp >= ceil((N * (N + 1) // 2) / 2):
        ok = X
    else:
        ng = X

print(ok)


Measured memory_bytes: 135700
///
// File:  2199.cpp
// Author: ymiyamoto
//
// Created on Sat Dec  9 23:16:36 2017
//

#include <cstdint>
#include <iostream>
#include <vector>

#define MAX UINT32_MAX

using namespace std;

int32_t main()
{
  while (true) {
    uint32_t N, M;
    cin >> N >> M;
    if (N == 0 && M == 0) break;

    vector<int32_t> C(M);
    vector<int32_t> x(N + 1);

    for (uint32_t i = 0; i < M; i++) {
      cin >> C[i];
    }

    for (uint32_t i = 1; i <= N; i++) {
      cin >> x[i];
    }

    vector<vector<uint64_t>> dp(N + 1, vector<uint64_t>(256, MAX));
    dp[0][128] = 0;

    for (uint32_t i = 0; i < N; i++) {
      for (int32_t j = 0; j < 256; j++) {
        if (dp[i][j] == MAX) continue;
        for (uint32_t k = 0; k < C.size(); k++) {
          int32_t val = min(max(0, j + C[k]), 255);
          int32_t diff = (x[i + 1] - val);
          dp[i + 1][val] = min(dp[i + 1][val], dp[i][j] + diff * diff);
        }
      }
    }

    uint64_t minval = MAX;
    for (int32_t i = 0; i < 256; i++) {
      minval = min(minval, dp[N][i]);
    }

    cout << minval << endl;
  }

  return 0;
}

Measured memory_bytes: 43788
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Numerics;
using System.Text;
using static System.Console;
using static System.Math;

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            var n = ReadInt();
            var list = new List<int>();
            var ans = 0;



            for (int i = 0; i < n; i++)
            {
                var t = ReadInt();
                list.Add(t);
            }
            var max = list.Max();


            var yakusuu = new List<int>() {};
            for (int i = 1; i <= max; i++)
            {
                if (max % i == 0)
                {
                    yakusuu.Add(i);
                }
            }
            

            for (int i = 0; i < n; i++)
            {
                ans += yakusuu[LowerBound(yakusuu.ToArray(), list[i])]-list[i];
            }
            WriteLine(ans);
        }

        //lower_bound https://webbibouroku.com/Blog/Article/cs-lowerbound-upperbound
        public static int LowerBound<T>(T[] a, T v)
        {
            return LowerBound(a, v, Comparer<T>.Default);
        }

        public static int LowerBound<T>(T[] a, T v, Comparer<T> cmp)
        {
            var l = 0;
            var r = a.Length - 1;
            while (l <= r)
            {
                var mid = l + (r - l) / 2;
                var res = cmp.Compare(a[mid], v);
                if (res == -1) l = mid + 1;
                else r = mid - 1;
            }
            return l;
        }

        private static string Read() { return ReadLine(); }
        private static string[] Reads() { return (Read().Split()); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}



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

int main() {
    int thp, tatk, ahp, aatk;
    cin >> thp >> tatk >> ahp >> aatk;
    while (thp > 0 && ahp > 0) {
        ahp -= tatk;
        if (ahp <= 0) {
            cout << "Yes" << endl;
            break;
        }
        thp -= aatk;
        if (thp <= 0) {
            cout << "No" << endl;
            break;
        }
    }
    return 0;
}

Measured memory_bytes: 3620
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (long long  i=a;i<n;i++)
#define sz(A) (int)(A.size())
#define all(x) (x).begin(),(x).end()
#define get(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;

//a(n) = Sum_{ k = 1..n } k / 2 * floor(n / k) * floor(1 + n / k).
void solve() {
	ll n;
	ll ans = 0;
	cin >> n;
	rep(i, 1, n + 1) {
		ll y = n / i;
		ll k = (y*(y + 1)) / 2;
		ans += i*k;
	}
	cout << ans << endl;
}
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int T_ = 1;
	//cin >> T_;
	for (int i = 0; i < T_; i++)solve();
	return 0;
}







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

using namespace std;
using ll = long long;
using ld = long double;

void solve() {
  int n;
  cin >> n;
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if ((int)ceil(log10(i + 0.01)) % 2 == 1) {
      ++cnt;
    }
  }
  cout << cnt << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);

  solve();
  return 0;
}


Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
#define ll long long 
// memo  fixed  setprecision(20);
using vvll = vector<vector<ll>>;
ll mod =1e9+7;
/*"itob" int to "N"base */           template<typename TypeInt> string itob(const TypeInt v, int base){static const char table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";string ret;static numeric_limits<TypeInt> t;TypeInt n = v;if (t.is_signed) {if (v < 0) n *= -1;}while (n >= base) {ret += table[n%base];n /= base;}ret += table[n];if (t.is_signed) {if (v < 0 && base == 10) ret += '-';}reverse(ret.begin(), ret.end());return ret;}
/*"chmin" a = MAX*/                  template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
/*"chmax" a = MIN*/                  template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
/*"ctoi"  char to int*/              int ctoi(char c) {return c-'0';}
/*"gcd"   MAX Euclidean */           ll gcd(ll a,ll b){if(b==0)return a; return gcd(b,a%b);}
/*"lcm"   MIN*/                      ll lcm(ll a,ll b){ll g = gcd(a,b);return a/g*b;}
/*"primecheck"If prime,return true.*/bool primecheck(ll n){if(n < 2) return false;else{for(int i = 2; i * i <= n; i++){if(n % i == 0) return false;}return true;}}
string  reverserange(string s,ll a,ll b){reverse(s.begin()+a-1,s.begin()+b); return s;}
ll modpow(ll a,ll n, ll mod){ll res = 1;while(n>0){if (n%2==1){res = res * a % mod;}a = a * a % mod;n/=2;}return res;}
int main() {
    int k,a,b;
    cin >> k >>a >>b;
    if(a%k==0){
        cout <<"OK"<<endl;
        return 0;
    }
    ll che = a/k;
    che*=k;
    che+=k;
    if(a<=che&&b>=che)cout<<"OK"<<endl;
    else cout<<"NG"<<endl;
}
    



Measured memory_bytes: 3652
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
using namespace std;
typedef long long int ll;
typedef pair<int,int> pii;

template<class T> T umax(T a,T b) {return (a>b?a:b);}
template<class T> T umin(T a,T b) {return (a<b?a:b);}
template<class T,class P> T umax(T a,P b) {return (a>b?a:b);}
template<class T,class P> T umin(T a,P b) {return (a<b?a:b);}

typedef long long int ll;
const int maxn = 1e5+7;
const ll mod = 1e9+7;

bool compare(const pair<ll,ll>& x, const pair<ll,ll>& y) {
    if(x.second == y.second) return x.first < y.first;
    return x.second < y.second;
}

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

ll gcd(ll a,ll b) {
    if(b == 0) return a;
    return gcd(b,a%b);
}

int main() {
    int a,b;
    cin >> a >> b;
    for(int i=1;i<=10000;i++) {
        int a1 = (i*8)/100;
        int b1 = (i*10)/100;
        if(a1 == a && b1 == b) {
            cout << i << endl;
            return 0;
        }
    }
    cout << -1 << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define maxx 100100
long long dp[200][maxx],peso[200],valor[200];

int main() {
	long long w;
	int n;
	cin >> n >> w;
	long long v = 0;
	for(int i = 1; i <= n; i++){
		cin >> peso[i] >> valor[i];
		v += valor[i];
	}

	for(int i = 0; i <= n; i++){
		for(int j = 0; j <= v+10; j++){
			dp[i][j] = 1e11;
			if(j == 0) dp[i][j] = 0;
		}
	}
	
	long long  ans = 0;
	for(int i = 1; i <= n; i++){
		for(long long j = 1; j <= v; j++){
			if(j - valor[i] >= 0 && (dp[i-1][j-valor[i]] + peso[i]) <= w){
				ans = max(j,ans);
				dp[i][j] = min(peso[i]+dp[i-1][j-valor[i]],dp[i-1][j]);
			}
			else dp[i][j] = dp[i-1][j];
		}
	}
	/*
	for(int i = 0; i <= n; i++){
		for(int j = 0; j <= v; j++){
			cout << dp[i][j] << " ";
		}
		cout << endl;
	}*/
	cout << ans<< endl;
	
	return 0;
}

Measured memory_bytes: 80128
// g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
//#define int ll
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
// #define mt make_tuple
//typedef tuple<pii,int,int> tp;
//typedef vector<tp> vt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
//template<class C>constexpr int size(const C &c){return (int)c.size();}
//template<class T,size_t N> constexpr int size(const T (&xs)[N])noexcept{return (int)N;}
const double PI=acos(-1);
const double EPS=1e-7;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
typedef string::const_iterator State;
int q;
Def formula(State &i,bool h){
//	cout<<*i<<" "<<h<<endl;
	Def out;
	if(*i=='('){
		i++;
		out=formula(i,0);
	}else if(*i=='T')out=1;
	else if(*i=='F')out=0;
	else if(*i=='-')out=1-formula(++i,1);
	else if(isalpha(*i))out=(q>>(*i-'a'))&1;
	if(h)return out;
	i++;
	int op=0;
	if(*i=='+')op=1;
	if(*i=='-'){op=2;i++;}
	if(*i=='*')op=3;
	if(op){
		i++;
		Def out1=formula(i,0);
		if(op==1)out|=out1;
		if(op==2)out=!out||out&&out1;
		if(op==3)out&=out1;
	}
	return out;
}
int main(){
	string s,t;
	while(1){
		getline(cin,s);
		if(s=="#")break;
		rep(i,s.size())if(s[i]=='='){
			t=s.substr(i+1);
			s=s.substr(0,i);
		}
		bool h=true;
		rep(i,1<<11){
			q=i;
			State S=s.begin(),T=t.begin();
			int ss=formula(S,0);
			int tt=formula(T,0);
//			cout<<ss<<" "<<tt<<endl;
			if(ss!=tt){
				h=false;
				break;
			}
		}
		if(h)cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}

Measured memory_bytes: 3224
# coding: utf-8
from fractions import gcd
def divisor(N):
    # 約数列挙
    divs = []
    for i in range(1, int(N**0.5)+1):
        if N % i == 0:
            divs.append(i)
            if i != N // i:
                divs.append(N//i)
    return divs
def eratosthenes(N):
    # 素数判定
    flag = True
    for i in range(2, int(N**0.5) + 1):
        if N % i == 0:
            flag = False
            break
    return flag

A, B = map(int, input().split())
A_divs = divisor(A)
B_divs = divisor(B)
ans = 1
L = list(set(A_divs) & set(B_divs))
ans = 0
for i in range(len(L)):
    if eratosthenes(L[i]):
        ans += 1
print(ans)

Measured memory_bytes: 7288
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    n, a, b = LI()
    print(min(n*a,b))
    return

#B
def B():
    a = S()
    print(a.count("1"))
    return

#C
def C():
    n = II()
    a = list(str(n))
    a = list(map(int,a))
    if not n % sum(a):
        print("Yes")
    else:
        print("No")
    return

#D
def D():
    n = II()
    a = LI()
    ans = 0
    for a_ in a:
        while not a_ % 2:
            ans += 1
            a_ /= 2
    print(ans)
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    C()


Measured memory_bytes: 38640
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
    int N, M; cin >> N >> M;
    if (N < M) {
        for (int i = 0; i < M; ++i) {
            cout << N;
        }
    }
    else {
        for (int i = 0; i < N; ++i) {
            cout << M;
        }
    }
    cout << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <array>
#include <vector>
#include <map>
#include <queue>
#include <string>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstdio>
using namespace std;

int main(void){
    int N,M;
    cin >> N >> M;
    vector<int> l(M);
    vector<int> r(M);
    for(int i=0; i<M; i++){
        cin >> l[i] >> r[i];
    }
    int ans=0;
    pair<int, int> edge = make_pair(l[0], r[0]);
    for(int i=1; i<M; i++){
        if(r[i] < edge.first || edge.second < l[i]){
            edge = make_pair(-2, -1);
        }else if(l[i] <= edge.first && edge.second <= r[i]){
            edge = edge;
        }else if(l[i] >= edge.first && edge.second >= r[i]){
            edge = make_pair(l[i], r[i]);
        }else if(l[i] <= edge.first && r[i] >= edge.first && r[i] <= edge.second){
            edge = make_pair(edge.first, r[i]);
        }else if(l[i] >= edge.first && l[i] <= edge.second && r[i] >= edge.second){
            edge = make_pair(l[i], edge.second);
        }
    }
    if(!(edge.second < 0)){
        ans = edge.second - edge.first + 1;
    }
    cout << ans << endl;
    return 0;
}

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

using namespace std;
 
#define rep2(x,fr,to) for(int (x)=(fr);(x)<(to);(x)++)
#define rep(x,to) for(int (x)=0;(x)<(to);(x)++)
#define repr(x,fr,to) for(int (x)=(fr);(x)>=(to);(x)--)
#define all(c) (c).begin(),(c).end()
#define sz(v) (int)(v).size()

typedef int64_t ll; typedef vector<int> VI; typedef pair<int,int> pii;
const int MD = 1e9+7; typedef vector<ll> VL;
void dbg(){ cerr << "\n"; }  template <typename T,typename ...T2>
void dbg(const T& fst, const T2&...rst){ cerr << fst << ": "; dbg(rst...); }

int main()
{
	cin.tie(0); ios_base::sync_with_stdio(false);
	int n, m;
	cin >>n >>m;
	VI a(n+3);
	rep(i, m){
		int w; cin >>w; a[w]=1;
	}
	VL dt(n+2);
	dt[0]=1;
	rep(i, n+1) rep2(j, 1, 3) {
		if(i+j <= n && a[i+j]==0) (dt[i+j] += dt[i]) %=MD;
	}
	//for(auto x :dt) printf("%d, ",x); puts("");
	cout << dt[n] <<"\n";
}


Measured memory_bytes: 1536
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = s; i < (int)(n); i++)
#define out(x) cout << x << endl
#define INF 1000000;
#include <map>
#include <math.h>
 
using ll = long long;
using namespace std;

ll n, k;
const ll mod = 1000000007;


int main(){
   int a, b, c;
   cin >> a >> b >> c;
   int a_rest = a-b;
   if(c-a_rest<0) out(0);
   else out(c-a_rest);
}

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

using namespace std;
template<typename T>
using reversed_priority_queue = std::priority_queue<T, std::vector<T>, std::greater<T> >;
typedef unsigned long long int ull;
typedef long long int ll;
#define rep(i,a,b) for (ll i=(a); i<(b); i++)
typedef pair<ll,ll> PLL;
// }}}

signed main() {
    ll n,ans=0;
    cin>>n;
    rep(i,0,n){
        ll t=0, a;
        cin>>a;
        while(a%2==0){
            t++;
            a/=2;
        }
        ans+=t;
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 256