akhauriyash/Code-Regression huggingface dataset:
while True:
    H, W = map(lambda x: int(x), input().split(" "))
    if (H, W) == (0, 0):
        break
    for y in range(H):
        for x in range(W):
            if (x + y) % 2 == 0:
                print("#", end="")
            else:
                print(".", end="")
        print("")
    print("")



Measured memory_bytes: 7020
#include <iostream>
#include <queue>
#include <string>

class Baseball{
    private:
        std::queue<int> runner;
        int out_count;
        int score;
        
    public:
        void initRunner(){
            while(runner.empty() != true){
                runner.pop();
            }
            for(int i = 0; i < 3; ++i){
                runner.push(0);
            }
        }
        
        Baseball(){
            out_count = 0;
            score = 0;
            initRunner();
        }
        
        ~Baseball(){}
        
        void hit(){
            if(runner.front() == 1) ++score;
            runner.pop();
            runner.push(1);
        }
        
        void homerun(){
            do{
                if(runner.front() == 1) ++score;
                runner.pop();
            } while (runner.empty() != true);
            ++score;
            initRunner();
        }
        
        int getScore(){
            initRunner();
            std::string str;
            while(out_count != 3){
                std::cin >> str;
                if(str == "HIT") hit();
                if(str == "HOMERUN") homerun();
                if(str == "OUT") ++out_count;
            }
            return score;
        }
};

int main(void){
    int N;
    std::cin >> N;
    for(int i = 0; i < N; ++i){
        Baseball baseball;
        std::cout << baseball.getScore() << std::endl;
    }
}

Measured memory_bytes: 1216
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    s=SI()
    for t in ["eraser","erase","dreamer","dream"]:
        s=s.replace(t,"")
    if s:print("NO")
    else:print("YES")

main()

Measured memory_bytes: 3188
#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int arr[n+1];
    for(int i=1;i<=n;i++)
    {
        cin>>arr[i];
    }
    int result[n+1];
    result[1]=0;
    result[2]=abs(arr[2]-arr[1]);
    for(int i=3;i<=n;i++)
    {
        result[i]=min(result[i-1]+abs(arr[i]-arr[i-1]),result[i-2]+abs(arr[i-2]-arr[i]));
    }
    cout<<result[n]<<endl;
}

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

#define ll long long
#define vi vector<int>
#define vl vector<ll>
#define pii pair<int,int>
#define pll pair<ll,ll>
// pll pp; -> (pp.first,pp.second)
// vector<vl> pp(n,vl(2)); sort(pp.begin(),pp.end(),[](vl a,vl b){return a[0]<b[0];});

#define REP(i,n) for(ll (i)=0;(i)<(n);(i)++)
#define RREP(i,n) for(ll (i)=(n)-1;(i)>=0;(i)--)
#define FOR(i,a,b) for(ll (i)=(a);(i)<(b);(i)++)
#define FORR(i,a,b) for(ll (i)=(b)-1;(i)>=(a);(i)--)
#define DEBUG(x) cout << #x << ": " << x << endl
#define CHMAX(a,b) a=max((a),(b))
#define CHMIN(a,b) a=min((a),(b))
/*}}}*/
int main() {
  ll h,w;
  cin >> h >> w;
  vector<string> mp(h);
  REP(i,h) cin >> mp[i];
  vector<vl> score(h,vl(w,0));
  REP(i,h){
    ll beg = -1;
    REP(j,w){
      if(mp[i][j]=='.'){
        if(beg<0) beg = j;
      }
      if(mp[i][j]=='#'){
        if(beg>=0){
          FOR(k,beg,j) score[i][k] = j-beg;
          beg = -1;
        }
      }
    }
    if(beg>=0){
      FOR(k,beg,w) score[i][k] = w-beg;
      beg = -1;
    }
  }
  //REP(i,h){
  //  REP(j,w){
  //    cout << score[i][j] << " ";
  //  }
  //  cout << endl;
  //}
  //cout << endl;
  REP(j,w){
    ll beg = -1;
    REP(i,h){
      if(mp[i][j]=='.'){
        if(beg<0) beg = i;
      }
      if(mp[i][j]=='#'){
        if(beg>=0){
          FOR(k,beg,i) score[k][j] += i-beg-1;
          beg = -1;
        }
      }
    }
    if(beg>=0){
      FOR(k,beg,h) score[k][j] += h-beg-1;
      beg = -1;
    }
  }
  ll ans = -1;
  REP(i,h){
    REP(j,w){
      //cout << score[i][j] << " ";
      CHMAX(ans,score[i][j]);
    }
    //cout << endl;
  }
  cout << ans << endl;
  return 0;
}


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

int main(){
	int n;
  	cin>>n;
  	vector<int> a(n,0);
  	for(int i=1;i<n;i++){
    	int tmp;
      	cin>>tmp;
      	a[tmp-1]++;
    }
  	for(int i=0;i<n;i++){
    	cout<<a[i]<<endl;
    }
}

Measured memory_bytes: 4052
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll,ll> pll;
const ll mod=1e9+7;
//const ll mod=998244353;
const ll inf=1LL<<61;

int main() {
  ll n;
  cin >> n;
  mat w(2,vec(3));
  for(ll i=0;i<2;i++) {
    for(ll j=0;j<3;j++) {
      cin >>w[i][j]; 
    }
  }
  vec v(3);
  for(ll j=0;j<3;j++) {
    v[j]=w[1][j]-w[0][j];
  }
  vec dp1(n+1);
  for(ll i=0;i<3;i++) {
    for(ll j=0;j<n;j++) {
      dp1[j+1]=max(dp1[j+1],dp1[j]);
      if(j+w[0][i]<=n) dp1[j+w[0][i]]=max(dp1[j+w[0][i]],dp1[j]+v[i]);
    }
  }
  ll m=dp1[n]+n;
  vec dp2(m+1);
  for(ll i=0;i<3;i++) {
    for(ll j=0;j<m;j++) {
      dp2[j+1]=max(dp2[j+1],dp2[j]);
      if(j+w[1][i]<=m) dp2[j+w[1][i]]=max(dp2[j+w[1][i]],dp2[j]-v[i]);
    }
  }
  cout << m+dp2[m] << endl;
}

Measured memory_bytes: 195584
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
int T;
int ex_gcd(int a,int b,int &x,int &y)
{
	if(b==0)
	{
		x=1,y=0;
		return a;
	}
	int d=ex_gcd(b,a%b,x,y);
	int tmp=x;
	x=y,y=tmp-a/b*y;
	return d;
}
int getinv(int A,int MOD)
{
	int x,y;
	int d=ex_gcd(A,MOD,x,y);
	if(d!=1) return -1;
	return (x+MOD)%MOD;
}
int a,x,b,y,c;
int d;
int gcd(int a,int b)
{
	return b==0?a:gcd(b,a%b);
}
vector<pair<int,int> >pos;
vector<int>cnt;
void getpos()
{
	pos.clear(),cnt.clear();
	int W=c,H=d;
	int now=0;
	while(true)
	{
		int r=W/H;
		pos.push_back(make_pair(1LL*now*getinv(d,c)%c,now%c==0?c:(c-now+c)%c)),cnt.push_back(r); 
		now+=r*H;
		W%=H;
		if(W==0) break;
		H%=W;
		if(H==0) H=W;
	}
	pos.push_back(make_pair(c,0));
	return;
}
void solve()
{
	scanf("%d%d%d%d%d",&a,&x,&b,&y,&c);
	int G;
	G=gcd(a,b),a/=G,b/=G,c/=gcd(G,c);
	G=gcd(a,c),a/=G,c/=G,y/=G;
	G=gcd(b,c),b/=G,c/=G,x/=G;
	a%=c,b%=c;
	if(c==1)
	{
		printf("%d\n",x+y);
		return;
	}
	d=1LL*a*getinv(b,c)%c;
	getpos();
	int ans=0;
	for(size_t i=0;i<cnt.size();i++)
	{
		int xl=pos[i].first,yl=pos[i].second,xr=pos[i+1].first,yr=pos[i+1].second;
		int dx=(xr-xl)/cnt[i],dy=(yl-yr)/cnt[i];
		int l=0,r=x+y,res=0;
		while(l<=r)
		{
			int mid=((long long)l+r)/2;
			auto check=[=](int mid)
			{
				long long p=(x-1LL*xl*mid)/dx,q=(y-1LL*yr*mid)/dy;
				return (x-1LL*xl*mid)>=0&&(y-1LL*yr*mid)>=0&&p+q>=1LL*cnt[i]*mid;
			};
			if(check(mid)) res=mid,l=mid+1;
			else r=mid-1;
		}
		ans=max(ans,res);
	}
	printf("%d\n",ans);
	return;
}
int main()
{
	scanf("%d",&T);
	while(T--)
		solve();
	return 0;
}

Measured memory_bytes: 3844
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const long long INF = 1LL << 60;

int main()
{
    int n, ans = 0;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        ans += i + 1;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>

using namespace std;

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

int main() {
  int w, n, a[40][2], pos[40];
  cin >> w >> n;
  rep (i, n) scanf("%d,%d", a[i], a[i] + 1);
  rep (i, n) --a[i][0], --a[i][1];
  rep (i, w) pos[i] = i;
  rep (i, n) swap(pos[a[i][0]], pos[a[i][1]]);
  rep (i, w) cout << pos[i] + 1 << endl;
  return 0;
}

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

int main(){
  int n;
  cin >> n;
  int ans=0;
  int p=1;
  for(int i=0;i<n;i++){
    int a;
    cin >> a;
    if(a==p) p++;
    else ans++;
  }
  if(p==1) cout << -1 << endl;
  else cout << ans << endl;
}

Measured memory_bytes: 3648
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

#define d(x)  cerr << #x << " = " << (x) << endl;
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c,n) sort(c,c+n)
#define VSORT(c) sort((c).begin(),(c).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define INF 2000000000

const double EPS = 1e-10;
const double PI  = acos(-1.0);

int main()
{
	int n,a[260];cin>>n;
	FOR(i,1,n+1)cin>>a[i];
	FOR(i,1,n+1) 
	{
		int f=floor(i/2);
		cout<<"node "<<i<<": key = "<<a[i]<<", ";
		if(f!=0)cout<<"parent key = "<<a[f]<<", ";
		if(i*2<n+1)cout<<"left key = "<<a[i*2]<<", ";
		if(i*2+1<n+1)cout<<"right key = "<<a[i*2+1]<<", ";
		cout<<endl;
	}
	return 0;
}

Measured memory_bytes: 3072
/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(LL &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const LL &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
 #define DEBUG(...) {printf("# ");printf(__VA_ARGS__);puts("");}
#else
 #define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1e6+10;
LL f(LL h){
    if(h==1)return 1;
    return 2*f(h/2)+1;
}
int main(){
    LL h;
    R(h);
    W(f(h));
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main()
{
  int x,y,z;
  cin >> x >> y >> z;
  int temp;
  temp = x;
  x=y;
  y = temp;
  
  temp = x;
  x = z;
  z = temp;
  
  cout << x << " " << y << " " << z << endl;
  return 0;
}

Measured memory_bytes: 256
#define D(x) cout << #x " = " << x << endl
#define DD(x, y) cout << "(" #x ", " #y ") = (" << x << ", " << y << ")" << endl
#define DDD(x, y, z) cout << "(" #x ", " #y ", " #z ") = (" << x << ", " << y << ", " << z << ")" << endl
#define DV(v) for (auto __macro_vi : v){ cout << __macro_vi << " ";} cout << endl;
#define DA(a, n) for (int __macro_i = 0; __macro_i < n; ++__macro_i) { cout << a[__macro_i] << " "; } cout << endl;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define ALL(v) (v).begin(), (v).end()
#include<iostream>
#include<vector>
#include<cassert>
#include<cmath>
#include<string>

using namespace std;
const long long MOD = 1000000007;

long long mod(long long a, long long m = MOD)
{
    long long q = a % m;
    if (q < 0)
        q += m;
    return q;
}

long long modadd(long long a, long long b, long long m = MOD)
{
    return (a + b) % m;
}

long long modsub(long long a, long long b, long long m = MOD)
{
    return mod((a % m) - (b % m));
}

long long modmul(long long a, long long b, long long m = MOD)
{
    return (a * b) % m;
}

long long modpow(long long a, long long b, long long m = MOD)
{
    long long c = 1;
    while (b > 0)
    {
        if (b & 1)
            c = modmul(c, a, m);
        a = modmul(a, a, m);
        b >>= 1;
    }
    return c;
}

long long modinv(long long a, long long m = MOD)
{
    long long b = m, u = 1, v = 0;
    while (b)
    {
        long long t = a / b;
        long long tmp;
        a -= t * b;
        tmp = a;
        a = b;
        b = tmp;
        u -= t * v;
        tmp = u;
        u = v;
        v = tmp;
    }
    u %= m;
    if (u < 0)
        u += m;
    return u;
}

long long moddiv(long long a, long long b, long long m = MOD)
{
    return modmul(a, modinv(b, m), m);
}

string k;

int main()
{
    cin >> k;
    reverse(k.begin(), k.end());
    long long ans = 0;
    int cnt1 = 0;
    for (auto ki : k) {
        if (ki == '1') cnt1++;
    }
    ans = modadd(ans, modpow(2, cnt1));
    REP(p, k.size()) {
        if (k[p] == '0') continue;
        cnt1--;
        ans = modadd(ans, modmul(modpow(3, p), modpow(2, cnt1)));
    }
    cout << ans << endl;
}


Measured memory_bytes: 512
#!/snap/bin/pypy3

def main():
    N = int(input())
    S = input()

    whitestones = []
    tmpcount = 0
    for stone in S:
        if stone == '.':
            tmpcount += 1
        whitestones.append(tmpcount)

    totalwhite = whitestones[-1]

    print(min(min(i - wstone + totalwhite - wstone for i, wstone in enumerate(whitestones, start=1)), totalwhite))


if __name__ == '__main__':
    main()


Measured memory_bytes: 53600
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <string.h>
#include <utility>
#include <vector>
#include <climits>
#include <cmath>
#include <unordered_map>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    bool f=false;
    float h,a;
    cin>>h>>a;
    cout<<ceil(h/a);
    return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
const int maxn = 1000005;

vector<int> G[maxn];
int sg[maxn];

void dfs(int u, int fa)
{
    sg[u] = 0;
    for (auto v : G[u]) {
        if (v == fa)
            continue;
        dfs(v, u);
        sg[u] ^= sg[v] + 1;
    }
}

int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 1; i < n; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v), G[v].push_back(u);
    }
    dfs(1, 0);
    printf("%s\n", sg[1] ? "Alice" : "Bob");
    cin >> ws;
    return 0;
}

Measured memory_bytes: 35328
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    int n,a,b,c,d,j=0;
    cin>>n;
    a=n/1000;
    b=(n%1000)/100;
    c=(n%1000%100)/10;
    d=(n%1000%100)%10;
    if(a==2)
    j++;
    if(b==2)
    j++;
    if(c==2)
    j++;
    if(d==2)
    j++;
    cout<<j<<endl;
	return 0;
}

Measured memory_bytes: 256
#include <stdio.h>

int main(void){
    
    int n,max,sum;
    max=0;
    sum=0;
    
    scanf("%d",&n);
    
    int l[n];
    
    for(int i=0; i<n; i++){
        scanf("%d",&l[i]);
        
        if(l[i]>max)
            max=l[i];
        
        sum+=l[i];

    }

    if(max < sum-max)
        printf("Yes");
    else
        printf("No");
}


Measured memory_bytes: 128
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

using namespace std;

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

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

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                         \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
       << " " << __FILE__ << endl;

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

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

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

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

using Graph = vector<vector<int>>;
Graph G;
int n;
vector<P> vp;

void dfs(int v, int p, int rank) {
  vp.push_back(make_pair(rank, v));
  for (auto nv : G[v]) {
    if (nv == p) continue;
    dfs(nv, v, rank + 1);
  }
}

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

  cin >> n;
  G.assign(n, vector<int>());
  int a, b;
  rep(i, n - 1) {
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<int> c(n);

  rep(i, n) cin >> c[i];
  sort(c.rbegin(), c.rend());
  dfs(0, -1, 0);
  sort(all(vp));
  vector<int> res(n);
  int cnt = 0;
  lint sum = 0;
  for (auto v : vp) {
    res[v.second] = c[cnt];
    cnt++;
  }
  FOR(i, 1, n) sum += c[i];
  cout << sum << endl;
  rep(i, n) { cout << res[i] << (i == n - 1 ? "" : " "); }
  cout << endl;

  return 0;
}

Measured memory_bytes: 1920
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <math.h>
#include <cassert>
#define rep(i,n) for(int i = 0; i < n; ++i )
using namespace std;
using ll = long long;
using P = pair<int,int>;

int main() {
  int n;
  cin >> n;
  string ans = "ABC";
  if(n>999) ans = "ABD";
  cout << ans << endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<(n);++i)
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
int p,x,N,Q;
vector<int> to[200005];
vector<int> ans;

void dfs(int v,int p=-1){
  for(int u:to[v]){
    if(u==p) continue;
    ans[u]+=ans[v];
    dfs(u,v);
  }
}

signed main(){
  cin >> N >> Q;
  rep(i,N-1){
    int a,b;
    cin >> a >> b;
    a--,b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  ans.resize(N);
  rep(i,Q){
    cin >> p >> x;
    p--;
    ans[p]+=x;
  }
  dfs(0);
  rep(i,N) cout << ans[i] << endl;
  return 0;
}


Measured memory_bytes: 24320
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String s1 = sc.next();
        String s2 = sc.next();
        String s3 = sc.next();

        System.out.println(s1.substring(0, 1).toUpperCase() + s2.substring(0, 1).toUpperCase() + s3.substring(0, 1).toUpperCase());
    }
}

Measured memory_bytes: 22868
#!/usr/bin/env python3
import sys
from collections import deque
INF = float("inf")


def solve(N: int, S: str):
    X = ""
    counter = 0                 # counterは(の数
    for i, c in enumerate(S):
        if c == ')' and counter == 0:
            X = '(' + X + ')'
        elif c == ')' and counter > 0:
            X = X + ')'
            counter -= 1
        elif c == '(':
            X = X + '('
            counter += 1
    X += ')'*counter
    print(X)
    return


def main():

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


if __name__ == '__main__':
    main()


Measured memory_bytes: 3316
#include <iostream>
#include <bitset>

const int maxn = 50000+7;
const int maxm = 100000+7;

std::pair<int, int> balancer[maxm];
std::bitset<maxn> b[maxn];
int x[maxn], num[maxn];
int n, m, t;

int main(int argc, char *argv[]) {  
	std::ios::sync_with_stdio(false);
	std::cin.tie(0); 
	std::cin >> n >> m >> t;
	for (int i = 0; i < m; i++) {
		std::cin >> balancer[i].first >> balancer[i].second;
		balancer[i].first--;
		balancer[i].second--;
	}
	if (t == 1) {
		for (int i = 0; i < n; i++) {
			b[i].set(i);
		}
		for (int i = 0; i < m; i++) {
			b[balancer[i].first] = b[balancer[i].second] = b[balancer[i].first] | b[balancer[i].second];
		}
		for (int i = 0; i < n; i++) {
			if ((int)b[i].count() == n) {
				std::bitset<maxn> now;
				now.set(i);
				std::string ans(m, 'v');
				while (m--) {
					if (now[balancer[m].first]) {
						now.set(balancer[m].second);
						ans[m] = '^';
					} else {
						now[balancer[m].first] = now[balancer[m].second];
					}
				}
				std::cout << ans << '\n';
				exit(0);
			}
		}
		std::cout << "-1\n";
		exit(0);
	} else {
		if (n == 2) {
			std::cout << "-1\n";
			exit(0);
		}
		std::string ans(m, '^');
		for (int i = 0; i < n; i++) {
			x[i] = i;
			num[i] = 1;
		}
		for (int i = m - 1; i >= 0; i--) {
			if (num[x[balancer[i].first]] > 1) {
				num[x[balancer[i].first]]--;
				num[x[balancer[i].second]]++;
				x[balancer[i].first] = x[balancer[i].second];
				ans[i] = 'v';
			} else {
				num[x[balancer[i].first]]++;
				num[x[balancer[i].second]]--;
				x[balancer[i].second] = x[balancer[i].first];
			}
		}
		std::cout << ans << '\n';
	}
}

Measured memory_bytes: 306688
def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    a.append(0)
    a.insert(0,0)
    d_i=[]
    for i in range(n+1):
        d=abs(a[i]-a[i+1])
        d_i.append(d)
    tot_i=sum(d_i)
    for i in range(1,n+1):
        tot = tot_i - d_i[i-1] - d_i[i] + abs(a[i-1]-a[i+1])
        print(tot)
        
    
main()

Measured memory_bytes: 14048
n = gets.chomp.to_i.times do
  s,t = gets.split.map(&:to_i)
  
  if s < 6
    if t < 6
      a = (s < t ? (s..t).to_a : (t..s).to_a.reverse)
    else
      a = (s..t).to_a
    end
  else
    if t < 6
      a = (s..9).to_a + (t..5).to_a.reverse
      elsif s > t
      a = (s..9).to_a + (0..5).to_a.reverse + (1..t).to_a
    else
    a = (s..t).to_a
    end
  end
  puts a.join(" ")
end


Measured memory_bytes: 6872
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import permutations
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7


#############
# Main Code #
#############

N = getN()

next = [
[0, 1],
[0, 1, 2],
[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
[4, 5, 6],
[5, 6, 7],
[6, 7, 8],
[7, 8, 9],
[8, 9]
]

ans = []

def dfs(s):
    global ans
    if len(s) >= 11:
        return
    # 上限を超えない範囲で実行
    else:
        ans.append(int(s))
        # dfs
        s_last = int(s[-1])
        for i in next[s_last]:
            dfs(s + str(i))

# 始点は1~9
for i in range(1, 10):
    dfs(str(i))

ans.sort()
print(ans[N - 1])

Measured memory_bytes: 95804
#include<bits/stdc++.h>
#define FOR(i,n) for(int i=0;i<n;i++)
#define SFOR(i,a,b) for(int i=a;i<b;i++)
#define elif(a) else if(a)
#define INF INT_MAX
#define F first
#define S second
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
int n;
int a[200005];
int m1,m2;
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin>>n;
  FOR(i,n){
    cin>>a[i];
    if(m1==0) m1=a[i];
    else if(m1<a[i]){
      m2=m1;
      m1=a[i];
    }else if(m1>=a[i] && a[i]>m2){
      m2=a[i];
    }
  }
  FOR(i,n){
    if(m1!=a[i]) cout<<m1<<endl;
    else cout<<m2<<endl;
  }
  return 0;
}



Measured memory_bytes: 2432
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b){return (a*b)/gcd(a, b);}


int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    string s;
    cin>>s;
    for(auto l : s) cout<<"x";
}


Measured memory_bytes: 256
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) {
		FastScanner sc = new FastScanner(System.in);
		int k = sc.nextInt();
		long ans = 0l;
		for(int i = 1; i <= k; i++) {
			for(int j = 1; j <= k; j++) {
				for(int l = 1; l <= k; l++) {
					ans += gcd3(i, j, l);
				}
			}
		}
		System.out.println(ans);
	}
	
	static int gcd2(int x, int y) {
		if(x%y == 0) return y;
		else if(x < y) return gcd2(y, x);
		else return gcd2(y, x%y);
	}
	
	static int gcd3(int x, int y, int z) {
		int w = gcd2(x, y);
		return gcd2(w, z);
	}
	
	static class FastScanner {
		private BufferedReader reader = null;
	    private StringTokenizer tokenizer = null;
	    public FastScanner(InputStream in) {
	        reader = new BufferedReader(new InputStreamReader(in));
	        tokenizer = null;
	    }

	    public String next() {
	        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
	            try {
	                tokenizer = new StringTokenizer(reader.readLine());
	            } catch (IOException e) {
	                throw new RuntimeException(e);
	            }
	        }
	        return tokenizer.nextToken();
	    }

	    public String nextLine() {
	        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
	            try {
	                return reader.readLine();
	            } catch (IOException e) {
	                throw new RuntimeException(e);
	            }
	        }

	        return tokenizer.nextToken("\n");
	    }

	    public long nextLong() {
	        return Long.parseLong(next());
	    }

	    public int nextInt() {
	        return Integer.parseInt(next());
	    }

	    public double nextDouble() {
	         return Double.parseDouble(next());
	     }

	    public int[] nextIntArray(int n) {
	        int[] a = new int[n];
	        for (int i = 0; i < n; i++)
	            a[i] = nextInt();
	        return a;
	    }

	    public long[] nextLongArray(int n) {
	        long[] a = new long[n];
	        for (int i = 0; i < n; i++)
	            a[i] = nextLong();
	        return a;
	    }
	}
}


Measured memory_bytes: 33440
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <math.h>
#include <sstream>
#include <numeric>
#include <string>
#include <cassert>
#include <bitset>

using namespace std;
typedef long long ll;

int main() {
  ll N, A, B;
  cin >> N >> A >> B;

  ll X = N / (A + B);
  ll ans = X * A;
  N %= (A + B);
  ans += min(N, A);
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
object Main extends App {
  scala.io.Source.stdin.getLines.toList.map(_.split(" ")).takeWhile(_(1) != "?").map(x => (x(0).toInt, x(1), x(2).toInt)).map({
    case (a, "+", b) => a + b
    case (a, "-", b) => a - b
    case (a, "*", b) => a * b
    case (a, "/", b) => a / b
    case _ => -100100100
  }).map(println)
}

Measured memory_bytes: 44088
#include <bits/stdc++.h>
using namespace std;
using ll= long long;
using vi= vector<int>;
using vvi= vector<vi>;
using vd= vector<double>;
using vvd= vector<vd>;
using vc= vector<char>;
using vb= vector<bool>;
#define rep(i,x,n) for(int i=x; i<n; i++)
#define all(x) x.begin(), x.end()


int main(){
  int a,b,c,ans;
  cin >> a >> b >> c;

  if(b/a >= c){
    ans= c;
  }else{
    ans= b/a;
  }
  cout << ans << endl;
}


Measured memory_bytes: 256
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

 class Main  {
     public static void main(String[]args){
         Scanner scan = new Scanner(System.in);
         int x = scan.nextInt();
         String s[] = new String[x];
         for(int i=0;i<x;i++){
            s[i] = scan.next();  
         }
         int y = scan.nextInt();
         String s2[] = new String[y];
         for(int i=0;i<y;i++){
            s2[i] = scan.next();  
         } 
         ArrayList<Integer> a = new ArrayList<>();
         a.add(0);
         for(int i = 0 ; i<x ; i++){
             int yen = 0 ;
             for(int j=0; j<y ; j++){
                
                 if(s[i].equals(s2[j])){
                     yen--;
                 }
             } for(int j=0; j<x ; j++){
                
                 if(s[i].equals(s [j])){
                     yen++;
                 }
             }
             //System.out.println(yen);
             a.add(yen);
                     
         }
         System.out.println(Collections.max(a,null));
     }
}

Measured memory_bytes: 22868
#include <iostream>
#include <iomanip>
#include <cassert>

#include <utility>
#include <vector>
#include <limits>
#include <algorithm>
#include <map>
using namespace std;

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

void Main()
{
	int64_t H;
	cin >> H;

	int depth = 0;
	while (true)
	{
		if (H == 1) break;

		++depth;
		H /= 2;
	}

	int64_t count = 1;
	if (depth != 0)
	{
		rep(i, depth)
		{
			count = count * 2 + 1;
		}
	}

	cout << count << endl;
}

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

	Main();
	return 0;
}

Measured memory_bytes: 256
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def main():
    #K, X = [int(f) for f in input().split()]
    N = int(input())
    ana = {}
    for _ in range(N):
        key = ''.join(sorted(input()))
        if key in ana:
            ana[key] += 1
        else:
            ana[key] = 1

    counter = 0
    for key in ana:
        if ana[key] == 1:
            continue
        counter += cmb(ana[key], 2)
    print(counter)

if __name__ == "__main__":
    main()

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

using namespace std;

struct lr_t {
  int l;
  int r;
};

std::vector<int> solve(const std::string s, const std::vector<lr_t>& lrs) {
  int const n = s.length();
  int const q = lrs.size();
  std::vector<int> sums(n);
  int sum = 0;
  int i = 0;
  for (; i < n - 1; ++i) {
    if (s[i] == 'A' && s[i + 1] == 'C') {
      sum++;
    }
    sums[i] = sum;
  }
  sums[i] = sum;

  std::vector<int> rets(q);
  for (int i = 0; i < q; ++i) {
    auto const lr = lrs[i];
    auto const l = lr.l;
    auto const r = lr.r;
    int ret = sums[r - 1];
    if (l > 0) {
      ret -= sums[l - 1];
    }
    rets[i] = ret;
  }
  return rets;
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  int n;
  int q;
  std::cin >> n >> q;
  std::string s;
  std::cin >> s;
  std::vector<lr_t> lrs(q);
  for (auto& lr : lrs) {
    std::cin >> lr.l >> lr.r;
    lr.l--;
    lr.r--;
  }
  auto ret = solve(s, lrs);
  for (auto const r : ret) {
    std::cout << r << std::endl;
  }
  return 0;
}

Measured memory_bytes: 2248
/*input
1010
*/
#include <bits/stdc++.h>
#define pii pair<int, int>
#define F first
#define S second
#define int long long
using namespace std;
const int MX = 1e5 + 100;
vector<int> G[MX];
int ptr[MX];
main () {
	string s; cin >> s;
	int n = s.size();
	if(s[0] == '0' or s[n - 1] == '1') return cout << -1, 0;
	for(int i = 0; i + 1 < n; i ++) {
		if(s[i] != s[n - i - 2]) return cout << -1, 0;
	}
	int k = 1, ptr = 1;
	for(int i = 2; i <= n; i ++) {
		k ++;
		if(s[i - 2] == '0') {
			G[ptr].push_back(k);
			ptr = ptr;
		}
		else {
			G[ptr].push_back(k);
			ptr = k;
		}
	}
	for(int i = 1; i <= n; i ++) {
		for(auto it: G[i]) cout << i << ' ' << it << endl;
	}
}

Measured memory_bytes: 6912
#include<bits/stdc++.h>
using namespace std;
#define all(x) x.begin(),x.end()
#define EPS (int)1e-9
typedef long long ll;
typedef vector<int> vi;
#define sz(v)		((int)((v).size()))
#define OO 0x3f3f3f3f
#define  ed  '\n'
#define watch(x) cerr << '>' << #x << ':' << x << '\n';
#define in_range(x, y, r, c) ((x >= 0 && x < r && y >= 0 && y < c))
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
inline int two(int n) { return 1 << n; }
inline void set_bit(int & n, int b) { n |= two(b); }
inline void unset_bit(int & n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }
template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
#define IOS std::ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
const ll mod=1e17+9;
int main(){
IOS;
string s;
cin>>s;
vector<int> freq(27,0);
for(char x:s){
    freq[(x-'a')]++;
}
for(int i=0;i<26;i++){
    if((freq[i]%2)>0)return cout<<"No",0;
}
cout<<"Yes"<<endl;
return 0;
}


Measured memory_bytes: 764
#include <iostream>
#include <iomanip>
#include <cmath>
#include <queue>
#include <map>
#include <cstdlib>
#include <algorithm>
#include <utility>
#include <string>
#include <functional>
#define rep(i, a, b) for(int i=a;i<b;++i)
#define MOD 1000000007
using namespace std;

int cnt = 1;

int up(long long int a[], int i, int n)
{
	for (int j = i; j < n; ++j) {
		if (a[j] < a[j - 1]) {
			++cnt;
			return j;
		}
	}
	return n;
}

int down(long long int a[], int i, int n)
{
	for (int j = i; j < n; ++j) {
		if (a[j] > a[j - 1]) {
			++cnt;
			return j;
		}
	}
	return n;
}

int main(void)
{
	int n;
	long long int a[100000];
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	for (int i = 1; i < n; ++i) {
		if (a[i] == a[i - 1])
			;
		else if (a[i] > a[i - 1])
			i = up(a, i, n);
		else 
			i = down(a, i, n);
	}
	cout << cnt << endl;

	return 0;
}


Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
using p=pair<int,int>;
#define int long long
#define rep(i,N) for(int i=0;i<N;i++)

signed main(){
  string N;
  cin>>N;
  rep(i,2){
    if(N[i]=='9'){
      cout<<"Yes"<<endl;
      return 0;
    }
  }
  cout<<"No"<<endl;
  return 0;
}

Measured memory_bytes: 3548
#include <bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin>>s;
    for(int i=0;i<s.size()-1;i++){
		if(s[i]=='A'&&s[i+1]=='C'){
			cout<<"Yes"<<endl;
		    return 0;
		}
	}
	cout<<"No"<<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <math.h>
#include <map>
#include <bitset>
#include <cmath>
#include <set>
#include <utility>
#include <sstream>

#define INF 1000000000000
#define MOD 1000000007 //10^9+7:合同式の法

using namespace std;
typedef long long ll;

//マクロ
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)
#define ALL(x) x.begin(),x.end()
#define chmax(x,y) (x = max(x,y))
#define chmin(x,y) (x = min(x,y))

int main(){
    ll N,leftwhite=0,redcnt = 0;
    cin >> N;
    vector<char> c(N),d(N);

    for(ll i = 0;i < N;i++){
        cin >> c.at(i);
        d.at(i) = c.at(i);
    }

    for(ll i = 0;i < N-1;i++){
        if(d.at(i) == 'W' and d.at(i+1) == 'R'){
            leftwhite++;
            d.at(i+1) ='W';
            redcnt++;
        }else if(d.at(i) == 'R')
            redcnt++;
    }

    string hako;
    for(ll i = 0;i < redcnt;i++)
        hako += 'R';

    for(ll i = redcnt;i < N;i++)
        hako += 'W';

    ll cnt = 0;
    for(ll i = 0;i < N;i++){
        if(hako.at(i) != c.at(i))
            cnt++;
    }

    cnt /= 2;

    if(cnt <= leftwhite)
        cout << cnt << endl;
    else
        cout << leftwhite << endl;
}

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

using namespace std;

#define MAX 30
#define INF 1e9

class Dice{
private:
  int tmp;
public:
  vector<int> d;
  
  void init(){
    d.resize(6);
  }

  int getTop(){
    return d[0];
  }

  int getFront(){
    return d[1];
  }

  int getRight(){
    return d[2];
  }

  void rollN(){
    tmp = d[0];
    d[0] = d[1];
    d[1] = d[5];
    d[5] = d[4];
    d[4] = tmp;
  }
  
  void rollE(){
    tmp = d[0];
    d[0] = d[3];
    d[3] = d[5];
    d[5] = d[2];
    d[2] = tmp;
  }
 
  void rollS(){
    tmp = d[0];
    d[0] = d[4];
    d[4] = d[5];
    d[5] = d[1];
    d[1] = tmp;
  }
 
  void rollW(){
    tmp = d[0];
    d[0] = d[2];
    d[2] = d[5];
    d[5] = d[3];
    d[3] = tmp;
  }
};

int H,W,sx,sy;
int field[MAX][MAX];
bool visited[MAX][MAX][6][6][6][6];

struct State{
  int x,y,n,dist;
  Dice d;
  
  State(int x,int y,int n,int dist,Dice d) : 
    x(x),y(y),n(n),dist(dist),d(d) {}
};

int ctoi(char ch){
  switch(ch){
  case 'k':
    return -1;
  case 'r':
    return 0;
  case 'g':
    return 1;
  case 'b':
    return 2;
  case 'c':
    return 3;
  case 'm':
    return 4;
  case 'y':
    return 5;
  case 'w':
    return 6;
  }
  return -1;
}

bool inField(int x,int y){
  return 0 <= x && x < W && 0 <= y && y < H;
}

Dice init(){
  Dice d; d.init();
  d.d[0] = 0;
  d.d[1] = 4;
  d.d[2] = 2;
  d.d[3] = 5;
  d.d[4] = 1;
  d.d[5] = 3;
  return d;
}

void solve(Dice &dice){
  Dice initDice = init(); 
  int ans = INF;
  const int dx[4] = {-1,0,1,0};
  const int dy[4] = {0,-1,0,1};
  queue<State> Q;
  Q.push(State(sx,sy,0,0,initDice));
  memset(visited,false,sizeof(visited));
 
  while(!Q.empty()){
    State s = Q.front(); Q.pop();
    int x = s.x,y = s.y,n = s.n;
    Dice d = s.d;
    int t = d.getTop(),f = d.getFront(),r = d.getRight();

    if(visited[y][x][t][f][r][n]) continue;
    visited[y][x][t][f][r][n] = true;
    if(n == 6){ ans = s.dist; break; }

    for(int i = 0 ; i < 4 ; i++){
      int nx = x + dx[i], ny = y + dy[i];
      if(!inField(nx,ny)) continue;
      if(field[ny][nx] == -1) continue;
      Dice nd = d;
      if(i == 0){
        nd.rollW();
      }else if(i == 1){
        nd.rollN();
      }else if(i == 2){
        nd.rollE();
      }else{
        nd.rollS();
      }
      if(field[ny][nx] == nd.getTop() && field[ny][nx] == dice.d[n]){
        Q.push(State(nx,ny,n+1,s.dist+1,nd));
      }else if(field[ny][nx] == 6){
        Q.push(State(nx,ny,n,s.dist+1,nd));
      }
    }
  }
  if(ans == INF){
    cout << "unreachable" << endl;
  }else{
    cout << ans << endl;
  }
}

int main(){
  char in;
  while(cin >> W >> H, W){
    Dice dice; dice.init();
    for(int i = 0 ; i < H ; i++){
      for(int j = 0 ; j < W ; j++){
        cin >> in;
        if(in == '#'){
          in = 'w';
          sx = j; sy = i;
        }
        field[i][j] = ctoi(in);
      }
    }
    for(int i = 0 ; i < 6 ; i++){
      cin >> in;
      dice.d[i] = ctoi(in);
    }
    solve(dice);
  }
  return 0;
}

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

typedef long long ll;
typedef pair<ll, ll> P;

int A, B;

int main(){
    cin >> A >> B;

    int ans = 0;
    for(int i=A;i<=B;i++){
        if(i % 10 == (i / 10000) % 10 && (i / 10) % 10 == (i / 1000) % 10) ans++;
    }

    cout << ans << endl;
}

Measured memory_bytes: 256
S = input()

a = int(S[0]) * 10 + int(S[1])
b = int(S[2]) * 10 + int(S[3])

if a>=1 and a<=12:
    if b>=1 and b<=12:
        print("AMBIGUOUS")
    else:
        print("MMYY")

else:
    if b>=1 and b<=12:
        print("YYMM")
    else:
        print("NA")

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

long long N;

vector<pair<long long, long long> > prime_factorize(long long N) {
    vector<pair<long long, long long> > res;
    for (long long a = 2; a * a <= N; ++a) {
        if (N % a != 0) continue;
        long long ex = 0;

        while (N % a == 0) {
            ++ex;
            N /= a;
        }

        res.push_back({a, ex});
    }

    if (N != 1) res.push_back({N, 1});
    return res;
}


int main(){
    cin>>N;
    vector<pair<long long,long long>> p = prime_factorize(N);
    int cnt = 0;

    for(auto a : p){
        int have = a.second;
        int dec = 1;

        while(have >= dec){
            have -= dec;
            dec++;
            cnt++;
        }
    }
    


    cout<<cnt<<endl;

    return 0;
}

Measured memory_bytes: 3592
// input に入力データが全部入る
function Main(input) {
    // 1行目がinput[0], 2行目がinput[1], …に入る
    input = input.split("\n");
    tmp1 = input[1].split(" ");
    tmp2 = input[3].split(" ");
    
    // 文字列から10進数に変換するときはparseIntを使います
    var N = parseInt(input[0], 10);
    var D = new Array();
    for(var i = 0; i<N;i++){
        var tmp = parseInt(tmp1[i],10);
        D.push(tmp);
    }
/*
    for(var i = 0; i<N;i++){
        console.log(D.join());
    }
*/
    var M = parseInt(input[2], 10);
    var T = new Array();
    for(var i=0;i<M;i++){
        tmp = parseInt(tmp2[i], 10);
        T.push(tmp);
    }
    
    var hash = new Object();
    for(var i=0;i<N;i++){
        hash[D[i]] = 0;
    }
    for(var i=0;i<N;i++){
        hash[D[i]]++;
    }
/*    
    for (var key in hash) {
        document.write (key + "の値：" + hash[key] + "<br>");
    }
*/
    var flag = true;
    for(var i=0;i<M;i++){
        if(!hash[T[i]]){
            flag=false;
            break;
        }
        if(hash[T[i]]==0){
            flag = false;
            break;
        }
        else{
            hash[T[i]]--;
        }
    }

    // 出力
    if(flag){
        console.log("YES");
    }
    else console.log("NO");
}

// "実行する"ボタンを押した時に実行される関数 (デバッグ用)
function debug(){
	var input = document.getElementById("input").value;
	Main(input);
}

//* この行以降は編集しないでください（標準入出力から一度に読み込み、Mainを呼び出します）
Main(require("fs").readFileSync("/dev/stdin", "utf8"));

Measured memory_bytes: 102168
from collections import deque

n = int(input())
A = list(map(int, input().split()))
deq = deque([])

for i in range(1, n+1):
    if i % 2 == 1:
        deq.append(A[i-1])
    else:
        deq.appendleft(A[i-1])

if n % 2 == 0:
    print(*deq)
else:
    print(*reversed(list(deq)))


Measured memory_bytes: 25412
#include <iostream>

using namespace std;

int main(void) {
  int a,b,c;
  cin >> a >> b >> c;
  if(a + b == c) {
    cout << "Yes" << endl;
  } else if (a + c == b) {
    cout << "Yes" << endl;
  } else if (b + c == a) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

Measured memory_bytes: 256
count =0
while True:
    W = input()
    if (W==0): break
    work={0:0}
    count +=1
    N = input()
    for i in range(N):
        p,w = input()
        for w0 in sorted(work.keys(),reverse=True):
            w1 = w0+w
            p1 = work[w0]+p
            if w1>W or (work.has_key(w1) and work[w1]>=p1): continue
            else: work[w1] = p1

    m = max(work.values())
    tmp = work.values().index(m)
    n = work.keys()[tmp]
    print "Case %d:" %(count)
    print m
    print n

Measured memory_bytes: 4360
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>

#define REP(i,n) for(long long i=0;i<n;++i)
#define REPR(i,n) for(long long i=n;i>=0;--i)
#define REPI(itr,v) for(auto itr=v.begin();itr!=v.end();++itr)
#define REPIR(itr,v) for(auto itr=v.rbegin();itr!=v.rend();++itr)
#define FOR(i,a,b) for(long long i=a;i<b;++i)
#define SORT(v,n) sort(v, v+n)
#define SORTV(v) sort(v.begin(), v.end())
#define ALL(v) v.begin(),v.end()
#define llong long long
#define INF 999999999
#define MOD 1000000007
#define pb push_back
#define pf push_front
#define MP make_pair
#define SV(v) {for(long long sitr=0;sitr<v.size();++sitr){cin>>v[sitr];}}

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

using namespace std;

typedef pair<int,int> pii;

bool colors[100010];
bool used[100010] = {};

void solve(int n, vector<vector<pii>> &d){
  used[n] = true;
  //cout << n << endl;
  REPI(itr, d[n]){
    int idx = itr->first, cost = itr->second;
    if(!used[idx]){
      //cout << idx << " " << cost << endl;
      if(cost % 2 == 0){
        colors[idx] = colors[n];
      }else{
        colors[idx] = !colors[n];
      }
      solve(idx, d);
    }
  }
}

int main(){

  llong n;
  cin >> n;

  vector<vector<pii>> d(n);
  REP(i,n-1){
    int u, v, w;
    cin >> u >> v >> w;
    u--; v--;

    d[u].pb(MP(v,w));
    d[v].pb(MP(u,w));
  }

  solve(0,d);
  REP(i,n){
    if(colors[i]){
      cout << 1 << endl;
    }else{
      cout << 0 << endl;
    }
  }

  return 0;
}



Measured memory_bytes: 8064
#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 K; cin >> K;
  int ans = K%2 == 0 ? (K/2)*(K/2) : (K/2)*((K/2)+1);
  cout << ans << endl;
  return 0;
}

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

using namespace std;

int main(){
    int l;
    cin>>l;
    int n;
    vector<array<int,3>> ans;
    int i;
    for(i=21;i>-1;i--){
        if(l&(1<<i)){
            // l-=(1<<i);
            break;
        }
    }
    n=i+1;
    for(int i=1;i<n;i++){
        ans.push_back(array<int,3>({i,i+1,0}));
        ans.push_back(array<int,3>({i,i+1,(1<<(i-1))}));
    }
    int temp=(1<<i);
    for(--i;i>-1;i--){
        if(l&(1<<i)){
            ans.push_back(array<int,3>({i+1,n,temp}));
            temp+=(1<<i);
        }
    }
    cout<<n<<" "<<ans.size()<<endl;
    for(auto i:ans){
        cout<<i[0]<<" "<<i[1]<<" "<<i[2]<<endl;
    }
    return 0;
}

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

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

using namespace std;

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

int main() {
  int a, b, c;
  ll k;
  cin >> a >> b >> c >> k;

  ll maxValue = 1000000000000000000;

  if (abs(a - b) > maxValue) {
    cout << "Unfair" << endl;
  } else {
    if (k % 2 == 0) {
      cout << (a - b) << endl;
    } else {
      cout << (b - a) << endl;
    }
  }
  return 0;
}


Measured memory_bytes: 256