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

using ll=long long;
const ll MOD=1e9+7;

ll powm(ll x,ll y){
    ll res=1;
    while(y){
        if(y&1){
            res*=x;
            res%=MOD;
        }
        x*=x;
        x%=MOD;
        y>>=1;
    }
    return res;
}
ll mod_inv(ll x){
    return powm(x,MOD-2);
}
int main(){
    int n;
    cin>>n;
    string s;
    cin>>s;
    int m=s.size();
    vector<vector<ll>> dp(2,vector<ll>(n+m+1,0));
    vector<ll> rig(n+1);
    dp[0][n]=1;
    rig[0]=dp[0][n+m];
    for(int i=0;i<n;i++){
        fill(dp[(i+1)&1].begin(),dp[(i+1)&1].end(),0LL);
        for(int j=0;j<n+m;j++){
            dp[(i+1)&1][j+1]+=dp[i&1][j]*2;
            dp[(i+1)&1][j+1]%=MOD;
            if(j-1>=0){
                dp[(i+1)&1][j-1]+=dp[i&1][j];
                dp[(i+1)&1][j-1]%=MOD;
            }
        }
        rig[i+1]=dp[(i+1)&1][n+m];
    }
    vector<ll> lef(n+1);
    vector<vector<ll>> dp2(2,vector<ll>(n+1,0));
    dp2[0][n]=1;
    lef[0]=1;
    for(int i=0;i<n;i++){
        fill(dp2[(i+1)&1].begin(),dp2[(i+1)&1].end(),0LL);
        for(int j=0;j<=n;j++){
            if(j!=n){
                dp2[(i+1)&1][j+1]+=dp2[i&1][j]*2;
                dp2[(i+1)&1][j+1]%=MOD;
            }
            if(j-1>=0){
                dp2[(i+1)&1][j-1]+=dp2[i&1][j];
                dp2[(i+1)&1][j-1]%=MOD;
            }
        }
        lef[i+1]=accumulate(dp2[(i+1)&1].begin(),dp2[(i+1)&1].end(),0LL);
        lef[i+1]%=MOD;
    }
    
    ll res=0;
    for(int i=0;i<=n;i++){
        res+=lef[i]*rig[n-i];
        res%=MOD;
    }

    res*=mod_inv(powm(2,m));
    res%=MOD;
    cout<<res<<endl;
    return 0;
}

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

typedef long long int llint;

int main()
{
  int cnt;
  int n, x;
  int m[101];
  
  cin>> n >> x;
  for(int i = 0; i < n; i++) cin>> m[i], x -= m[i];

  sort(m, m + n);

  cnt = n;
  cnt += x / m[0];

  cout<< cnt <<endl;
  
  return 0;
}


Measured memory_bytes: 256
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=1e5;
const int base=1e8;
const int limit=8;
char S[500008];

ll read()
{
    ll x=0,f=1;char ch=getchar();
    for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
    for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
    return x*f;
}

struct Bignum
{
    int v[maxn+8],len;
    void clear(){len=1,memset(v,0,sizeof(v));}
    void read()
    {
	scanf("%s",S);int n=strlen(S),now;
	reverse(S,S+n);len=-1;
	for (int i=0;i<n;i++)
	    {
		if (i%limit==0) len++,now=1;
		v[len]+=now*(S[i]-'0');
		now*=10;
	    }
	len++;
    }
    void write()
    {
	printf("%d",v[len-1]);
	for (int i=len-2;~i;i--) printf("%0*d",limit,v[i]);
	puts("");
    }
    int num()
    {
	int tmp=0;
	for (int i=0;i<len;i++)
	    for (int j=0,now=1;j<limit;j++,now*=10)
		tmp+=v[i]/now%10;
	return tmp;
    }
}A;

Bignum operator +(Bignum x,int y)
{
    Bignum z=x;
    z.v[0]+=y;
    for (int i=0;z.v[i]>=base;i++,z.len=max(z.len,i+1)) z.v[i+1]+=z.v[i]/base,z.v[i]%=base;
    return z;
}

Bignum operator *(Bignum x,int y)
{
    Bignum z;z.len=x.len+1;
    for (int i=0;i<=z.len;i++) z.v[i]+=x.v[i]*y,z.v[i+1]+=z.v[i]/base,z.v[i]%=base;
    while(z.len>1&&z.v[z.len]==0) z.len--;
    while(z.v[z.len]) z.v[z.len+1]+=z.v[z.len]/base,z.v[z.len++]%=base;
    return z;
}

bool check(int x)
{
    Bignum z=A+(9*x);
    return z.num()<=9*x;
}
int main()
{
    A.read();
    A=A*9;
    int l=1,r=500000,ans;
    while(l<=r)
	{
	    int mid=(l+r)>>1;
	    if (check(mid)) ans=mid,r=mid-1;
	    else l=mid+1;
	}
    printf("%d\n",ans);
    return 0;
}


Measured memory_bytes: 2176
h, w, K = map(int, raw_input().split())
dp = [[0]*w for i in range(h+1)]
dp[0][0] = 1
MOD = 10**9+7
def valid_mask():
    res = []
    for mask in range(1 << (w-1)):
        invalid = False
        for k in range(w-2):
            if (mask & 1 << k) > 0 and (mask & 1 << (k+1)) > 0:
                invalid = True
                break
        if not invalid:
            res.append(mask)
    return res

masks = valid_mask()

for i in range(h):
    for j in range(w):
        for mask in masks:
            if j < w-1 and (mask & 1 << j) > 0:
                dp[i+1][j+1] = (dp[i][j] + dp[i+1][j+1]) % MOD
            elif j - 1 >= 0 and (mask & (1 << (j-1))) > 0:
                dp[i+1][j-1] = (dp[i][j] + dp[i+1][j-1]) % MOD
            else:
                dp[i+1][j] = (dp[i][j] + dp[i+1][j])% MOD

print dp[h][K-1] % MOD

Measured memory_bytes: 2696
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
using namespace std;

#define INF (1ll<<60)


int main(int argc, char const *argv[]) {
    int n;cin>>n;
    map<long long, int> a;
    for(int i=0;i<n;++i){
        long long ai;
        cin>>ai;
        a[ai]++;
    }
    auto itr=a.end();
    int res=0;
    while(true){
        itr--;
        int ai=itr->first;
        long long k=1;
        while(k<ai)k*=2;
        if(k==ai){
            res+=itr->second/2;
        }
        else{
            if(a.count(k-ai)){
                int d = min(a[k-ai],itr->second);
                res+=d;
                a[k-ai]-=d;
            }
        }
        if(itr==a.begin())break;
    }
    cout<<res<<endl;
    return 0;
}

Measured memory_bytes: 12800
# encoding:utf-8
 
def main():
	k = int(input())
	n = 50
	div = k // n
	res = k % n
 
	temp = [i for i in range(n)]
	temp = [temp[i] + (i >= (50 - res)) + div for i in range(n)]
 
	out = [str(x) for x in temp]
	print(n)
	print(" ".join(out))
 
	#print(calc(temp))
 
def calc(temp):
	i = 0
	while len([x for x in temp if x >= len(temp)]) > 0:
		temp = query(temp)
		#print([x for x in temp if x >= len(temp)])
		i += 1
	return i
 
def query(temp):
	temp.sort()
	temp = [x + 1 for x in temp]
	temp[-1] = temp[-1] - 1 - len(temp)
	return temp
 
if __name__ == '__main__':
	main()

Measured memory_bytes: 3064
#include<iostream>
using namespace std;
int n,c;
main()
{
	cin>>n>>c;
	int M=c;
	for(int i=0;i<n;i++)
	{
		int a,b;cin>>a>>b;
		c+=a-b;
		if(c<0)
		{
			cout<<0<<endl;
			return 0;
		}
		M=M<c?c:M;
	}
	cout<<M<<endl;
}



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

#define rep(i, j) for (int i = 0; i < j; i++)
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<pii> vpii;

int main() {

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout << fixed;

    int m;
    cin >> m;


    map<int, pair<int, ll>> mp = {
        {1, {2, 1}},
        {2, {4, 1}},
        {3, {6, 1}},
        {4, {8, 1}},
        {5, {1, 2}},
        {6, {3, 2}},
        {7, {5, 2}},
        {8, {7, 2}},
        {9, {9, 2}}
    };

    auto f = [&](ll d, ll c){
        vll rem;

        ll res = 0;
        while (c > 1) {
            if(c % 2) {
                rem.push_back(d);
                c -= 1;
            }

            res += mp[d].second * c / 2;
            c /= 2;
            d = mp[d].first;
        }
        if (c > 0) rem.push_back(d);

        reverse(all(rem));
        d = rem[0];
        for (int i = 1; i < rem.size(); i++) {
            d += rem[i];
            res++;
            if (d >= 10) {
                d = d % 10 + d / 10;
                res++;
            }
        }

        return pair<int, ll>{d, res};
    };

    ll ans = 0;
    int sum = 0;
    rep(i, m) {
        ll d, c;
        cin >> d >> c;
        if (d == 0) ans += c;
        else {
            auto p = f(d, c);
            ans += p.second;
            sum += p.first;
            // cout << endl;
            // cout << " -- " << d << " " << c << " " << ans << endl;
            ans++;
            if (sum >= 10) {
                ans++;
                sum = sum % 10 + sum / 10;
            }
        }
    }

    cout << ans - 1 << endl;

    return 0;
}


Measured memory_bytes: 256
def main():
    n, t = map(int, input().split())
    a = list(map(int, input().split()))

    ans = 0
    for i in range(n-1):
        ans += min(t, a[i+1] - a[i])
    ans += t

    print(ans)

if __name__ == '__main__':
    main()


Measured memory_bytes: 25196
#include<iostream>
#define MAX 10000
#define NIL -1
using namespace std;

struct node{
	int p,l,r;
}; 

node T[MAX];
int n,D[MAX],H[MAX];

void setDepth(int u,int d){
	if(u==NIL){
		return ;
	}
	D[u]=d;
	setDepth(T[u].l ,d+1);
	setDepth(T[u].r ,d+1);
}

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

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

void print(int u){
	cout<<"node "<<u<<": ";
	cout<<"parent = "<<T[u].p<<", ";
	cout<<"sibling = "<<getSibling(u)<<", ";
	int deg=0;
	if(T[u].l !=NIL){
		deg++;
	}
	if(T[u].r !=NIL){
		deg++;
	}
	cout<<"degree = "<<deg<<", ";
	cout<<"depth = "<<D[u]<<", ";
	cout<<"height = "<<H[u]<<", ";
	if(T[u].p ==NIL){
		cout<<"root"<<endl;
	}else if(T[u].l ==NIL&&T[u].r ==NIL){
		cout<<"leaf"<<endl;
	}else{
		cout<<"internal node"<<endl;
	}
}

int main(){
	int v,l,r,root=0;
	cin>>n;
	for(int i=0;i<n;i++){
		T[i].p =NIL;
	}
	for(int i=0;i<n;i++){
		cin>>v>>l>>r;
		T[v].l =l;
		T[v].r =r;
		if(l!=NIL){
			T[l].p =v;
		}
		if(r!=NIL){
			T[r].p =v;
		}
		for(int i=0;i<n;i++){
			if(T[i].p ==NIL){
				root=i;
			}
		}
	}
	setDepth(root,0);
	setHeight(root);
	for(int i=0;i<n;i++){
		print(i);
	}
	return 0;
}


Measured memory_bytes: 3148
# -*- coding: utf-8 -*-
a,b,k = map(int,input().split())

"""
for i in range(a, b + 1):
    if a + k - 1 >= i or b - k + 1 <= i:
        print(i)
"""
"""
tmp = []
for i in range(k):
    if a + i <= b:
        print(a + i)
        tmp.append(a + i)
for i in range(k):
    i = k - i - 1
    if (b - i >= a) and (not((b - i) in tmp)):
        print(b - i)
"""

#くれないさんのわかりやすい解答
ans = []
for i in range(k):
    number = a + i
    if a <= a + i <= b:
        ans.append(number)
    number = b - i
    if a <= number <= b:
        ans.append(number)
ans = list(set(ans))
ans.sort()
for i in range(len(ans)):
    print(ans[i])


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

const int N = 100010;

int a[N];
int n;

int main(){
	scanf("%d", &n);
	int max = 0;
	for (int i = 0; i < n; ++ i){
		scanf("%d", &a[i]);
		max = std::max(a[i], max);
	}
	int cnt = 0;
	bool flag = true;
	for (int i = 0; i < n; ++ i){
		if (max != a[i]){
			flag = false;
			break;
		}
	}
	if (flag){
		if (max == n - 1){
			return printf("Yes\n"), 0;
		}
	}
	for (int i = 0; i < n; ++ i){
		if (a[i] != max && a[i] != max - 1){
			return printf("No\n"), 0;
		}
		cnt += a[i] == max;
	}
	int left = !!cnt, right = cnt >> 1;
	return printf(left + n - cnt > max || right + n - cnt < max ? "No\n" : "Yes\n"), 0;
}

Measured memory_bytes: 640
#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 A,B,T;cin>>A >> B >> T;
  T = T / A;
  B = B*T;
  cout << B;
  
}








Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define ll long long
#define P pair<int,int>
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()
#define pb push_back
#define eb emplace_back
template<class T>void chmax(T &a,T b){if(a<b)a=b;}
template<class T>void chmin(T &a,T b){if(a>b)a=b;}
constexpr int INF=1000000000000000000;
constexpr int mod=1000000007;
constexpr double eps=1e-8;
const double pi=acos(-1);
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int Random(int mi,int ma){
    random_device rnd;
    mt19937 mt(rnd());//32bit
    //[mi,ma]
    uniform_int_distribution<int>engine(mi,ma);
    return engine(mt);
}
int gcd(int a,int b){
    if(b==0)return a;
    return gcd(b,a%b);
}
int lcm(int a,int b){
    return a/gcd(a,b)*b;
}
bool prime(int a){
    if(a==1)return false;
    for(int i=2;i*i<=a;i++){
        if(a%i==0)return false;
    }
    return true;
}
int modpow(int a,int b){
    if(b==0)return 1;
    if(b%2)return modpow(a,b-1)*a%mod;
    int memo=modpow(a,b/2);
    return memo*memo%mod;
}
vector<int>kaijo,invkaijo;
void init_fact(int n){
    kaijo.resize(n+1);
    invkaijo.resize(n+1);
    kaijo[0]=1;
    for(int i=1;i<=n;i++){
        kaijo[i]=kaijo[i-1]*i;
        kaijo[i]%=mod;
    }
    rep(i,n+1)invkaijo[i]=modpow(kaijo[i],mod-2);
}
int comb(int a,int b){
    if(a<b)return 0;
    if(a<0||b<0)return 0;
    return kaijo[a]*invkaijo[a-b]%mod*invkaijo[b]%mod;
}
int inv(int x){
    x=modpow(x,mod-2);
    return x;
}

signed main(){
    cin.tie(0);ios::sync_with_stdio(false);
    int n;
    cin>>n;
    int ans=0;
    ans+=modpow(10,n);
    ans%=mod;
    ans-=modpow(9,n);
    ans+=mod;
    ans%=mod;
    ans-=modpow(9,n);
    ans+=mod;
    ans%=mod;
    ans+=modpow(8,n);
    ans%=mod;
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 3652
import heapq
from heapq import heapify

N, M = map(int,input().split())
A = list(map(int,input().split()))
L = []
for i in range(N):
  L.append([A[i],1])
for i in range(M):
  b, c = map(int,input().split())
  L.append([c,b])
heapq._heapify_max(L)

def _heappush_max(heap, item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappop_max(heap):
    lastelt = heap.pop()
    if heap:
        returnitem = heap[0]
        heap[0] = lastelt
        heapq._siftup_max(heap, 0)
        return returnitem
    return lastelt

ans = 0
for i in range(N):
  x = _heappop_max(L)
  if x[1] > 1:
    ans += x[0]
    x[1] -= 1
    _heappush_max(L,x)
  else:
    ans += x[0]
print(ans)

Measured memory_bytes: 36012
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define all(a) a.begin(), a.end()

signed main() {
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	int n, k;
	cin >> n >> k;

	vector <int> a(n + 10), ans(n + 10, INT_MAX);

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

	for(int i = 2; i <= n; i++) {
		int l = 0;
		for(int j = i - 1; j >= 1 and l < k; j--) {
			ans[i] = min(ans[i], ans[j] + abs(a[j] - a[i]));
			l++;
		}
	}

	cout << ans[n] << endl;

	return 0;
}

Measured memory_bytes: 4788
#include <stdio.h>
#include <math.h>
int main(){
  int n,i,j,s=0,a[100];
  scanf("%d",&n);
  for(i=0;i<n;i++)scanf("%d",&a[i]);
  for(i=0;i<n-1;i++)for(j=i;j<n;j++)if(abs(a[i]-a[j])>s)s=abs(a[i]-a[j]);
  printf("%d",s);
  return 0;
}

Measured memory_bytes: 128
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define pb push_back
const int maxn = 10000;
const int INF32 = 1'050'000'000;
const long long INF64 = 4'000'000'000'000'000'000;
const int MOD7 = 1'000'000'007;
const int MOD9 = 1'000'000'009;
const int mod = MOD7;

void ERROR(int num) { cout << "ERROR" << num << endl; }
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
int dx[8]={-1,0,1,0,1,1,-1,-1};
int dy[8]={0,-1,0,1,1,-1,1,-1};


struct mint{
    using ull=unsigned long long int;
    ull v;
    mint(ll vv=0){s(vv%mod+mod);}
    mint& s(ull vv){
        v=vv<mod?vv:vv-mod;
        return *this;
    }
    //オーバーロード
    mint operator-()const{return mint()-*this;}//符号反転
    mint&operator+=(const mint&val){return s(v+val.v);}
    mint&operator-=(const mint&val){return s(v+mod-val.v);}
    mint&operator*=(const mint&val){
        v=ull(v)*val.v%mod;
        return *this;
    }
    mint&operator/=(const mint&val){return *this*=val.inv();}
    mint operator+(const mint&val){return mint(*this)+=val;}
    mint operator-(const mint&val){return mint(*this)-=val;}
    mint operator*(const mint&val){return mint(*this)*=val;}
    mint operator/(const mint&val){return mint(*this)/=val;}
    mint pow(int n)const{
        mint res(1),x(*this);
        while(n){
            if(n&1)res*=x;
            x*=x;
            n>>=1;
        }
        return res;
    }
    mint inv()const{return pow(mod-2);}
    //拡張ユークリッドの互除法
    /* mint inv()const{
        int x,y;
        int g=extgcd(v,mod,x,y);
        assert(g==1);
        if(x<0)x+=mod;
        return mint(x);
    }*/
    friend ostream& operator<<(ostream&os,const mint&val){
        return os<<val.v;
    }//出力
    bool operator<(const mint&val)const{return v<val.v;}
    bool operator==(const mint&val)const{return v==val.v;}
    bool operator>(const mint&val)const{return v>val.v;}
};

mint Pow(ll m, ll n){
	if(n==0) return 1;
	mint x = Pow(m,n/2);
	x *= x;
	if(n%2==1) x *= m;
	return x; 
}

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

    ll N;
    cin >> N;
    vector<ll> A(N);
    rep(i,N) cin >> A[i];

    mint ans = 0;
    rep(i,60){
    	mint cnt1=0, cnt0=0;
    	rep(j,N){
    		if(A[j]>>i&1) cnt1+=1;
    		else cnt0+=1;
    	}
    	ans += cnt1*cnt0 * Pow(2,i);
    }
    cout << ans << endl;
}

Measured memory_bytes: 2560
import java.util.*;
import java.util.stream.Collectors;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class Main {

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		int N = Integer.parseInt(tokens[0]);
		int M = Integer.parseInt(tokens[1]);
		int[] A = new int[M];
		tokens = in.readLine().split(" ");
		for (int i = 0; i < M; ++i) {
			A[i] = Integer.parseInt(tokens[i]);
		}
		long sum = 0;
		for (int i = 0; i < M; ++i) {
			sum += A[i];
		}
		if (sum > N) {
			System.out.println("-1");
		} else {
			System.out.println((N - sum));
		}
	}

	void calc() {
	}

	void showResult() {
	}
}


Measured memory_bytes: 34384
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long Int;

int main(){
    map<int,Int> table;
    vector<int> ids;
    int n;
    bool flug;

    while(cin >> n, n != 0){
        flug = false;
        table.clear();
        ids.clear();

        for(int i=1; i<=n; i++){
            int id;
            Int tanka;
            Int number;
            cin >> id >> tanka >> number;
            table[id] += (tanka*number);
            vector<int>::iterator fIter = find(ids.begin(), ids.end(), id);
            if(fIter == ids.end()){
                ids.push_back(id);
            }
        }

        for(vector<int>::iterator Iter = ids.begin(); Iter != ids.end(); Iter++){
            if(table[*Iter] >= 1000000){
                cout << *Iter << endl;
                flug = true;
            }
        }

        if(flug == false){
            cout << "NA" << endl;
        }
    }

    return 0;
}

Measured memory_bytes: 0
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
#define INF 1e8
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
struct state{
	int cost;
	int x, y;
	int dir;
	state(int _x, int _y, int _dir, int _cost) : x(_x), y(_y), dir(_dir), cost(_cost){}
	bool operator < (const state &r)const{
		return cost > r.cost;
	}
};


int main(void){
	int w, h;
	while(cin >> w >> h, w){
		int mem[35][35][4];
		int grid[35][35];
		int c[4];
		for(int i = 0; i <= h; i++)
			for(int j = 0; j <= w; j++)
				for(int k = 0; k < 4; k++)
					mem[i][j][k] = INF;
		for(int i = 0; i <= h+1; i++)
			for(int j = 0; j <= w+1; j++)
				grid[i][j] = INF;
		
		for(int i = 1; i <= h; i++)
			for(int j = 1; j <= w; j++)
				cin >> grid[i][j];
		for(int i = 0; i < 4; i++) cin >> c[i];
		int mincost = INF;
		priority_queue<state> pq;
		pq.push(state(1,1,1,0));
		while(!pq.empty()){
			state P = pq.top(); pq.pop();
//			cout << P.x << ":" << P.y << "->" << P.cost << endl;
			if(P.y == h && P.x == w){
				mincost = min(mincost, P.cost);
				continue;
			}
			if(grid[P.y][P.x] == INF) continue;
			if(grid[P.y][P.x] != 4){
				int nd = (P.dir + grid[P.y][P.x]) % 4;
				int nx = P.x + dx[nd];
				int ny = P.y + dy[nd];
				if(mem[ny][nx][nd] > P.cost){
					mem[ny][nx][nd] = P.cost;
					pq.push(state(nx, ny, nd, P.cost));
				}
			}
			for(int i = 0; i < 4; i++){
				int nd = (P.dir + i) % 4;
				int nx = P.x + dx[nd];
				int ny = P.y + dy[nd];
				
				if(mem[ny][nx][nd] > P.cost + c[i]){
					mem[ny][nx][nd] = P.cost + c[i];
					pq.push(state(nx, ny, nd, P.cost + c[i]));
				}
			}
		}
		cout << mincost << endl;
	}
	return 0;
}

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

int main() {
  int N;
  cin >> N;
  
  vector<tuple<string, int, int>> R;
  for (int i = 0; i < N; i++){
    string s;
    int p;
    cin >> s >> p;
    R.push_back(make_tuple(s,100-p,i+1));
  }
  
  sort(R.begin(), R.end());
  
  for (tuple<string, int, int> r : R){
	string s; 
    int p;
    int n;
    tie(s, p, n) = r;
    cout << n << endl;
  }
}

Measured memory_bytes: 3580
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int main(void){
	ll x;
	scanf("%lld",&x);
	vector<ll> vec;
	for(ll i=1;i*i<=x;i++){
		if(x%i==0){
			vec.push_back(i);
			vec.push_back(x/i);
		}
	}
	for(ll i=0;i<vec.size();i++){
		ll tgt=x/vec[i];
		for(ll b=-300;b<=300;b++){
			ll a=b+vec[i];
			ll a2=a*a;
			ll a3=a*a*a;
			ll a4=a2*a2;
			ll b2=b*b;
			ll b3=b2*b;
			ll b4=b2*b2;
			ll sum=a4+a3*b+a2*b2+a*b3+b4;
			if(sum==tgt){
				printf("%lld %lld\n",a,b);
				return 0;
			}
		}
	}
	return 0;
}


Measured memory_bytes: 3792
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;

int A[1000000], n;

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

	int q, k, sum = 0;
	cin >> q;
	for (int i = 0; i < q; i++) {
		scanf("%d", &k);
		if (*lower_bound(A, A + n, k) == k)sum++;
	}

	cout << sum << endl;
	return 0;
}


Measured memory_bytes: 3528
N= [int(i) for i in input().split()][0]
Bn = [int(i) for i in input().split()]

An = [Bn[0]]
for n in range(1, N-1):
  An.append(min(Bn[n], Bn[n-1]))
An.append(Bn[-1])
print(sum(An))




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

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int x; cin >> x;
	if (x == 3 || x == 5 || x == 7) cout << "YES" << '\n';
	else cout << "NO" << '\n';
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <utility>
#include <functional>
#include <queue>
#include <cmath>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>      // std::setprecision
#include <boost/optional.hpp>
#include <boost/range/irange.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/numeric.hpp>
#include <boost/bimap/bimap.hpp>
#include <boost/bimap/multiset_of.hpp>
#include <boost/multi_array.hpp>
#include <boost/math/common_factor_ct.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/pending/disjoint_sets.hpp>
#include <boost/icl/interval_map.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/strong_components.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/kruskal_min_spanning_tree.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/numeric/ublas/vector.hpp>

#define REP(i, N) for(int i=0; i<(N); i++)
#define REC(f, ...) const auto f = [&](const auto f, __VA_ARGS__)
using namespace std;
using namespace boost::math;
using namespace boost::numeric;
namespace bst = boost;

int main() {
  int N;

  cin >> N;
  N--;
  vector<long long> B(N), A(N+1, 0);
  
  for (auto& i : B)
    cin >> i;
  B.push_back(1000000);
  
  long long r = 0;
  A[0] = B[0];
  for (auto i = 0; i < N; i++) {
    A[i] = min(A[i], B[i]);
    A[i+1] = B[i];
  }
  cout << bst::accumulate(A, 0LL) << endl;

  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define lld long long int
#define M 1000000007
#define fors(i,n) for(int i=0;i<n;++i)
#define fore(i,a,b) for(int i=a;i<=b;++i)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define vv vector<int>
#define endl "\n"
#define test(i) cout<<i<<endl
#define tests(i) cout<<"i: "<<i<<endl;
#define itrt int t; cin>>t; fors(i,t)
#define itr1 int t; cin>>t;
#define ff first
#define ss second
#define pb push_back
#define vmp make_pair
#define ppb pop_back
#define sorta(a,n) sort(arr,arr+n)
#define sortv(x) sort(x.begin(),x.end())
void file(){
    #ifdef LOCAL
    freopen("demo.in","r",stdin);
    freopen("demo.out","w",stdout);
    #endif
}
bool sortdes(const pair<int,int> &a, const pair<int,int> &b){return a.ss<b.ss;}
bool sortasc(const pair<int,int> &a, const pair<int,int> &b){return a.ss>b.ss;} 
int gcd(int x,int y){return __gcd(x,y);}

void test_case(){
    string s;
    cin>>s;
    if(s=="ABC"){
        test("ARC");
    }
    else{
        test("ABC");
    }
}

int main(){
    fastio;
    file();
    int T=1;
    // int T;
    // cin>>T;
    for(int nr=1;nr<=T;++nr) {
        // cout<<"Case #"<<nr<<": ";
		test_case();
    }
}

Measured memory_bytes: 3640
import math

def counter(l, n):
    num = list(range(1,n+1))
    index = 0
    order = 0
    for i in range(n):
        index = num.index(l[i])
        order += math.factorial(n-i-1)*index
        del num[index]
    
    return order


n =int(input())
ps = list(map(int, input().split()))
qs = list(map(int, input().split()))

print(abs(counter(ps,n)-counter(qs,n)))

Measured memory_bytes: 3064
#include <stdio.h>

int	main()
{
	int	N;
	long long ans, prev;

	scanf("%d\n", &N);
	prev = 2;
	ans = 1;
	for (int i = 2; i <= N; i++)
	{
		ans += prev;
		prev = ans - prev;
	}
	printf("%lld\n", ans);
	return (0);
}

Measured memory_bytes: 128
#include<functional>
#include<algorithm>
#include<iostream>
#include<utility>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,x,n) for(int i=x;i<n;i++)
#define pb push_back
#define pf push_front
#define int long long
using namespace std;
const int INF=1000000007;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};


signed main(){
  int n,a,b;
  cin>>n>>a>>b;
  if(n==1&&(a!=b))cout<<0<<endl;
  else if(n==1&&(a==b))cout<<1<<endl;
  else{
    if(a>b)cout<<0<<endl;
    else if(a==b)cout<<1<<endl;
    else{
      int summax=(n-1)*b+a;
      int summin=(n-1)*a+b;
      cout<<summax-summin+1<<endl;
    }
  }
  return 0;
}



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

using namespace std;

int main()
{
    vector<int> height(10);
    for (int i = 0; i < 10; i++) {
        cin >> height[i];
    }
    sort(height.begin(), height.end(), greater<int>());
    for (int i = 0; i < 3; i++) {
        cout << height[i] << endl;
    }
    return 0;
}

Measured memory_bytes: 3156
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define db(x) (cerr << #x << ": " << (x) << '\n')
#define cps CLOCKS_PER_SEC
#define tests(t) int t; cin >> t; while(t--)
#define iceil(n, x) (((n) + (x) - 1) / (x))
#define eb emplace_back
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define sz size()
#define all(v) (v).begin(), (v).end()
#define uni(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define fi first
#define se second
#define bitcount(x) __builtin_popcount(x)
#define bit(x) (1LL << x)
#define PI acos(-1.0)
#define EPS 1e-9

#define mod 1000000007
#define maxn 200015

using namespace std;

using namespace __gnu_pbds;
template<class key, class cmp = std::less<key>>
using ordered_set = tree<key, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
///All indexing is 0-based
///methods: find_by_order(k); & order_of_key(k);
///To make it an ordered_multiset, use pairs of (value, time_of_insertion)
///to distinguish values which are similar

typedef long long ll;
typedef double ld;
typedef pair<ll, ll> par;

ll N;

ll pot5(ll a)
{
    return a*a*a*a*a;
}
int main()
{
    #ifdef acm
    freopen("a.in","r",stdin);
    #endif // acm
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> N ;

    for(int i=-200;i<=200;i++)
        for(int j=-200;j<=200;j++)
            if( pot5(i) - pot5(j) == N){
                cout << i << " " << j;
                return 0;
            }

    return 0;

}


Measured memory_bytes: 3660
#include<stdio.h>
#define MAX 100000
#define NO -1

void ans(int);
void depth(int,int);

int p[MAX];
int l[MAX];
int r[MAX];

int n,Dp[MAX];

int main(){

  int d,v,c,le,ri;
  int i,j;

  scanf("%d",&n);

  for(i = 0; i < n; i++) p[i] = l[i] = r[i] = NO;

  for(i = 0; i < n; i++){
    scanf("%d%d",&v,&d);

    for(j = 0; j < d; j++){
      scanf("%d",&c);
      if(j == 0) l[v] = c;
      else r[le] = c;
      le = c;
      p[c] = v;
    }
  }

  for(i = 0; i < n; i++){
    if(p[i] == NO) ri = i;
  }

  depth(ri,0);

  for(i = 0; i < n; i++) ans(i);

  return 0;

}

void ans(int a){
  int i,b;
  printf("node %d: ",a);
  printf("parent = %d, ",p[a]);
  printf("depth = %d, ",Dp[a]);

  if(p[a] == NO) printf("root, ");
  else if(l[a] == NO) printf("leaf, ");
  else printf("internal node, ");

  printf("[");

  for(i = 0, b = l[a]; b != NO; i++, b = r[b]){
    if(i != 0) printf(", ");
    printf("%d",b);
  }

  printf("]\n");
}

void depth(int a, int i){
  Dp[a] = i;
  if(r[a] != NO) depth(r[a],i);
  if(l[a] != NO) depth(l[a],i+1);
}

Measured memory_bytes: 5300
#include<bits/stdc++.h>
#define forr(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = 1; i <= (n); i++)
#define ALL(a) (a.begin()),(a.end())
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll, ll> LP;
const ll LINF = 1LL<<60;
const int INF = 1001001001;
const int MOD = 1000000007;

/* --------------------------------------------------- */

int main() {
    string s;
    cin >> s;
    vector<bool> D(1000, false) ;
    rep(i, s.size()) {
        D[s[i]] = true;
    }
    if(D['N'] != D['S'] || D['W'] != D['E']) cout << "No" << endl;
    else cout << "Yes" << endl;
    
    return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <array>
#include <list>

using ll = long long;
using namespace std;

// GCD(最大公約数)
ll GCD(ll a, ll b){ return (b == 0) ? a : GCD(b, a%b); }

// LCM(最小公倍数)
ll LCM(ll a, ll b){ return a / GCD(a, b) * b; }

int main() {
	
	ll N;
	vector<ll> T;
	ll l;
	ll ans;
	
	// 入力
	scanf("%lld", &N);
	for(ll i = 0; i<N; i++){
		scanf("%lld", &l);
		T.push_back(l);
	}
	
	ans = 1LL;
	for(int i=0; i<N; i++){
		//printf("LCM(%lld, %lld) =", ans, T[i]);
		ans = LCM(ans, T[i]);
		//printf(" %lld\n", ans);
	}
	
	// 出力
	printf("%lld\n", ans);
	
	return 0;
	
}


Measured memory_bytes: 256
//May be wrong
#include <iostream>
#include <vector>
using namespace std;

int n;
int a[100000];
int flag[100000];		//-1?????????, 0?????¨????????¨, 1???????????????????????¨
bool visited[100000];	//visited[i] = ??????i????????¨???????????????

int main() {
	int i, j;
	
	cin >> n;
	for (i = 0; i < n; i++) { cin >> a[i]; flag[i] = -1; visited[i] = false; }
	
	for (i = 0; i < n; i++) {
		int pos = i;
		vector<int> path;
		
		if (flag[pos] != -1) { continue; }
		
		while (true) {
			if (flag[pos] != -1) { break; }
			if (visited[pos]) { break; }
			path.push_back( pos );
			visited[pos] = true;
			pos = (pos + a[pos]) % n;
		}
		
		if (visited[pos]) {			
			for (j = (int)path.size() - 1; j >= 0; j--) {
				flag[path[j]] = 1;
				visited[path[j]] = false;
				if ( path[j] == pos ) { break; }
			}
			for (j = j - 1; j >= 0; j--) {
				flag[path[j]] = 0;
				visited[path[j]] = false;
			}
		}
		else {
			for (j = (int)path.size() - 1; j >= 0; j--) { flag[path[j]] = 0; visited[path[j]] = false; }
		}
	}
	
	int ans = 0;
	for (i = 0; i < n; i++) {
		if (flag[i] == 1) {
			ans++;
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 4284
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a,b,c,d;
	cin>>a>>b>>c;
	if(b%a==0)
	{
		cout<<"OK";
		return 0;
	}
	d=b/a+1;
	if(d*a<=c)cout<<"OK";
	else
	cout<<"NG";
	return 0;
} 

Measured memory_bytes: 3636
n,m = map(int,input().split())
li = [list(map(int,input().split()))[1:] for _ in range(m)]
p = list(map(int,input().split()))
ans = 0

for i in range(1 << n):
    switch = set()
    for j in range(n):
        if (i >> j)&1:
            switch.add(j+1)
    for k in range(m):
        point = 0
        for l in li[k]:
            if l in switch:
                point += 1
        if point%2 != p[k]:
            break
    else:
        ans += 1

print(ans)

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

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int N,X,Y;
vint G[222222];
int par[222222];
int dep[222222];
void dfs(int v,int p,int d){
    dep[v]=d;
    par[v]=p;
    for(auto u:G[v]){
        if(u==p)continue;
        dfs(u,v,d+1);
    }
}
vint G2[222222];
bool win[222222];

int ans;
void dfs2(int v,int p,int d){
    if(dep[v]<d)return;
    chmax(ans,dep[v]*2);
    if(dep[v]==d)return;

    if(win[v])ans=INT_MAX;
    for(auto u:G2[v]){
        if(u!=p)dfs2(u,v,d+1);
    }
}
signed main(){
    cin>>N>>X>>Y;X--;Y--;
    vint A(N-1),B(N-1);
    rep(i,N-1)cin>>A[i]>>B[i],A[i]--,B[i]--;
    rep(i,N-1){
        int a,b;
        cin>>a>>b;
        a--;b--;
        G[a].pb(b);G[b].pb(a);
    }
    rep(i,N)sort(all(G[i]));

    dfs(Y,-1,0);

    rep(i,N-1){
        int a=A[i],b=B[i];
        bool ok=false;
        if(binary_search(all(G[a]),b))ok=true;
        if(par[a]!=-1&&binary_search(all(G[par[a]]),b))ok=true;
        if(par[b]!=-1&&binary_search(all(G[par[b]]),a))ok=true;
        if(ok){
            G2[a].pb(b);G2[b].pb(a);
        }
        else{
            win[a]=win[b]=true;
        }
    }

    dfs2(X,-1,0);

    if(ans==INT_MAX)cout<<-1<<endl;
    else cout<<ans<<endl;
    return 0;
}


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

typedef long long ll;

int h[100000] = {};
bool b[100000] = {};
ll mod = 1e9+7;

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

    int t[n], a[n];
    for(int i = 0; i < n; i++)  cin >> t[i];
    for(int i = 0; i < n; i++)  cin >> a[i];
    
    bool valid = true;

    b[0] = true;
    h[0] = t[0];

    for(int i = 1; i < n; i++){
        if(t[i] > t[i-1])   b[i] = true;
        h[i] = t[i];
    }

    if(b[n-1]){
        if(h[n-1] != a[n-1])    valid = false;
    }else{
        if(h[n-1] < a[n-1])   valid = false;
    }

    b[n-1] = true;
    h[n-1] = a[n-1];

    for(int i = n-2; i >= 0; i--){
        if(a[i] > a[i+1]){
            if(b[i] && h[i] != a[i])    valid = false;
            b[i] = true;
            h[i] = min(h[i], a[i]);
        }else if(b[i]){
            if(h[i] > a[i]) valid = false;
        }else{
            h[i] = min(h[i], a[i]);
        }
    }

    if(!valid){
        cout << 0 << endl;
        return 0;
    }

    ll ans = 1;
    for(int i = 0; i < n; i++){
        if(b[i])    continue;
        ans = ans*h[i]%mod;
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 1536
#include <bits/stdc++.h>
#include <algorithm>
#define _GLIBCXX_DEBUG
#define rep(i,n) for(int i=0;i<(n);++i)
#define all(x) (x).begin(), (x).end()
#define PB(x) push_back(x)
#define MP(x,y) make_pair(x,y)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

 
int main() {

  string s; cin >> s;
  string ans = "";
  for(int i = 0; i<s.size(); i++){
    if(s[i]=='0') ans+='0';
    if(s[i]=='1') ans+='1';
    if(s[i]=='B' && ans.size()>0) ans.pop_back();
  }
  cout << ans << endl;

 return 0;
}

Measured memory_bytes: 256
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        PrintWriter out = new PrintWriter(System.out);
        Scanner sc = new Scanner(System.in);
        TaskA task = new TaskA();
        task.solve(sc, out);
        out.flush();
        sc.close();
    }
    static class TaskA{
        void solve(Scanner sc, PrintWriter out){
            long n = nlong(sc);
            if (n%2==0){
                out.println(n);
            }else {
                out.println(n*2);
            }
        }
    }

    static int nint(Scanner sc){
        return Integer.parseInt(sc.next());
    }
    static long nlong(Scanner sc){
        return Long.parseLong(sc.next());
    }
    static double ndouble(Scanner sc){
        return Double.parseDouble(sc.next());
    }
    static float nfloat(Scanner sc){
        return Float.parseFloat(sc.next());
    }
    static String nstr(Scanner sc){
        return String.valueOf(sc.next());
    }
    static long[] longLine(Scanner sc, int size){
        long[] lLine = new long[size];
        for (int i = 0; i < size; i++) {
            lLine[i] = nlong(sc);
        }
        return lLine;
    }
    static int[] intLine(Scanner sc, int size){
        int[] iLine = new int[size];
        for (int i = 0; i < size; i++) {
            iLine[i] = nint(sc);
        }
        return iLine;
    }
    static String[] strLine(Scanner sc, int size){
        String[] strLine = new String[size];
        for (int i = 0; i < size; i++) {
            strLine[i] = nstr(sc);
        }
        return strLine;
    }
}



Measured memory_bytes: 21844
import java.util.Scanner;

public class Main {

	static Scanner sc = new Scanner(System.in);

	static int solve() {
		String s1 = sc.next();
		String s2 = sc.next();
		int ans = 0;
		for (int i = -s1.length() + 1; i < s2.length(); ++i) {
			int count = 0;
			for (int j = Math.max(0, -i); j < s1.length() && i + j < s2.length(); ++j) {
				if (s1.charAt(j) == s2.charAt(i + j)) {
					++count;
					ans = Math.max(ans, count);
				} else {
					count = 0;
				}
			}
		}
		return ans;
	}

	public static void main(String[] args) {
		while (sc.hasNext()) {
			System.out.println(solve());
		}
	}

}

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

int main()
{
    int n, m;

    cin >> n;
    vector<string> s(n);
    for (int i = 0; i < n; i++)
    {
        cin >> s.at(i);
    }

    cin >> m;
    vector<string> t(m);
    for (int i = 0; i < m; i++)
    {
        cin >> t.at(i);
    }
    int count=0;
    int wa=0;
    for(int i=0;i<n;i++){     
        for(int j=0;j<n;j++){
            if(s.at(i)==s.at(j)){
                count++;
            }
        }
        for(int j=0;j<m;j++){
            if(s.at(i)==t.at(j)){
                count--;
            }
        }
        wa=max(wa,count);
        count=0;
    }
    cout<<wa<<endl;
}

Measured memory_bytes: 256
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;


void main()
{
    auto nd = readln.split.to!(long[]);
    auto N = nd[0];
    auto A = nd[1];
    auto B = nd[2];
    auto C = nd[3];
    auto D = nd[4];

    foreach (m; 0..N-1) {
        if (C * (N-1-m) - D*m <= B-A && B-A <= D * (N-1-m) - C*m) {
            writeln("YES");
            return;
        }
    }
    writeln("NO");
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define rep(i,a,b) for(int i = a; i <= b; i++)
using namespace std;
const int N = 100010;
char s[N];
string a = "dream", b = "erase", c = "eraser", d = "dreamer";
bool check(int l, int r) {
	if (r - l + 1 == 5) {
		bool flag = true;
		rep(i,0,4) flag &= (s[l + i] == a[i]); 
		if (flag) return true;
		flag = true;
		rep(i,0,4) flag &= (s[l + i] == b[i]);
		return flag;
	} else if (r - l + 1 == 6) {
		bool flag = true;
		rep(i,0,5) flag &= (s[l + i] == c[i]);
		return flag;
	} else if (r - l + 1 == 7) {
		bool flag = true;
		rep(i,0,6) flag &= (s[l + i] == d[i]);
		return flag;
	} else return false;
}
bool f[N];
int main() {
	scanf("%s",s + 1); int l = strlen(s + 1);
	f[0] = 1;
	rep(i,1,l)
		f[i] = (i - 5 >= 0 && f[i - 5] && check(i - 5 + 1, i)) || (i - 7 >= 0 && f[i - 7] && check(i - 7 + 1, i)) || (i - 6 >= 0 && f[i - 6] && check(i - 6 + 1, i));
	if (f[l]) cout <<"YES"<<endl; else cout <<"NO"<<endl;
}

Measured memory_bytes: 512
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

void main()
{
    auto ab = readln.split.to!(int[]);
    writeln(max(ab[0] + ab[1], ab[0] - ab[1], ab[0] * ab[1]));
}

Measured memory_bytes: 256
#include<stdio.h>
int main()
{
    int n,k,a[51],len,i,j,l;
    scanf("%d %d",&n,&k);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=0;i<n;i++){
        for(j=i+1;j<n;j++){
            if(a[i]<a[j]){
                l=a[i];
                a[i]=a[j];
                a[j]=l;
            }
        }
    }
    for(i=0,len=0;i<k;i++){
        len=len+a[i];
    }
    printf("%d",len);
    return 0;
}


Measured memory_bytes: 128
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>

using namespace std;

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

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

#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

const double EPS = 1e-10;

int main() {
	int n;

	while(cin >> n, n) {
		int p = 1000-n;
		int m = 0;

		m += p/500;
		p = p%500;
		m += p/100;
		p = p%100;
		m += p/50;
		p = p%50;
		m += p/10;
		p = p%10;
		m += p/5;
		p = p%5;
		m += p;

		cout << m << endl;
	}

	return 0;
}

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

// in-short-use macros

#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007

//container-use 

typedef vector< ll > vl;
typedef vector< vl > vvl;
typedef pair< ll,ll > pll;
typedef vector<pair<ll,ll > > vpl;
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define rep(i,a,b) for (int i = a; i <= b; i++)

//main function here

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int a,b;
    cin>>a>>b;
    if(a<b)
        swap(a,b);
    while(a--)
        cout<<b;
  return 0;
 }

Measured memory_bytes: 256
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;

int main()
{
    char x[55][55];
    int h,w,i;
    scanf("%d%d",&h,&w);

    for(i=1; i<=h; i++)
        scanf("%s",&x[i][1]);



    int j,flag=1;
    for(i=1; i<=h; i++)
    {

        for(j=1; j<=w; j++)
        {
            if(x[i][j]=='#')
            {
                if(x[i+1][j]=='#'&&i+1<=h)
                    continue;
                if(x[i-1][j]=='#'&&i-1>=1)
                    continue;
                if(x[i][j+1]=='#'&&j+1<=w)
                    continue;
                if(x[i][j-1]=='#'&&j-1>=1)
                    continue;
                flag=0;
                break;
            }
            else
                continue;
        }
        if(!flag)
            break;
    }
        if(flag)
            printf("Yes");
        else
               printf("No");
    return 0;
}


Measured memory_bytes: 128
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll longinf=1LL<<60;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define F first
#define S second

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

  ll n,a,b,c,d;

ll rec(ll x, map<ll,ll> &mp){
  if(x==0) return mp[0]=0;
  if(x==1) return mp[1]=d;
  if(mp.count(x)) return mp[x];
  ll cur = longinf;
  for(ll i=-4; i<=4; i++){
    ll tmp = x+i;
    if(tmp<0) continue;
    if(tmp==0){
      cur = min(cur,d*abs(i)); continue;
    }

      int cnt=0; ll cul1=d, cul2=tmp;
      while(cul1>0){cnt++; cul1/=10;}
      while(cul2>0){cnt++; cul2/=10;}

    if(tmp%5==0 && tmp/5<x){
      if(cnt>12){
        cur = min(cur, rec(tmp/5, mp) + c + d*abs(i));
      } else {
        cur = min(cur, rec(tmp/5, mp) + min(c,d*(tmp/5LL)*4LL) + d*abs(i));
      }
    }
    if(tmp%3==0 && tmp/3<x){
      if(cnt>12){
        cur = min(cur, rec(tmp/3, mp) + b + d*abs(i));
      } else {
        cur = min(cur, rec(tmp/3, mp) + min(b,d*(tmp/3LL)*2LL) + d*abs(i));
      }
    }
    if(tmp%2==0 && tmp/2<x){
      if(cnt>12){
        cur = min(cur, rec(tmp/2, mp) + a + d*abs(i));
      } else {
        cur = min(cur, rec(tmp/2, mp) + min(a,d*(tmp/2LL)) + d*abs(i));
      }
    }
  }
  return mp[x] = cur;
}

int main(){
  int t; cin >> t;
while(t--){
  cin >> n >> a >> b >> c >> d;
  map<ll,ll> mp;
  cout << rec(n, mp) << endl;
}
  return 0;  
}

Measured memory_bytes: 6356
def prime(n):
    flag = [True]*(n+1)
    flag[0] = flag[1] = False
    for i in range(2,int(n**0.5)+1):
        if flag[i]:
            for j in range(i**2, n+1, i):
                flag[j] = False
    return flag
flag = prime(1000000)
while True:
    a,d,n = map(int,input().split())
    if n==0: break
    i,c = 0,0
    while c<n:
        if flag[a+d*i]: c+= 1
        i += 1
    print(a+d*(i-1))

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

int main(){

    string s;
    cin >> s;
    int queries;
    cin >> queries;
    bool rev = false;
    string beg="",end="";
    while (queries--)
    {
        int t;
        cin >> t;
        if(t==1){
            rev = !rev;
        }
        else
        {
            int f;
            string newchar;
            cin >> f >> newchar;

            if(f==1){
                if(rev) end+=newchar;
                else{
                    newchar+=beg;
                    beg=newchar;
                }
            }else
            {
                if(rev){
                    newchar+=beg;
                    beg=newchar;
                }else
                {
                    end+=newchar;
                }
                
            }
        }
        
    }
    string resultString = beg + s + end;
    if(rev) reverse(resultString.rbegin(),resultString.rend());
    cout << resultString << "\n";

    return 0;
}

Measured memory_bytes: 1420