akhauriyash/Code-Regression huggingface dataset:
// #pragma GCC target("avx2")  // CPU 処理並列化
// #pragma GCC optimize("O3")  // CPU 処理並列化
// #pragma GCC optimize("unroll-loops")  // 条件処理の呼び出しを減らす
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<stdlib.h>
#include<cassert>
#include<time.h>
#include<bitset>
#include<numeric>
using namespace std;
const long long mod=1000000007;
const long long inf=mod*mod;
const long long d2=(mod+1)/2;
const double EPS=1e-10;
const double INF=1e+10;
const double PI=acos(-1.0);
const int C_SIZE = 3121000;
namespace{
	long long fact[C_SIZE];
	long long finv[C_SIZE];
	long long inv[C_SIZE];
	long long Comb(int a,int b){
	 	if(a<b||b<0)return 0;
	 	return fact[a]*finv[b]%mod*finv[a-b]%mod;
	}
	void init_C(int n){
		fact[0]=finv[0]=inv[1]=1;
		for(int i=2;i<n;i++){
			inv[i]=(mod-(mod/i)*inv[mod%i]%mod)%mod;
		}
		for(int i=1;i<n;i++){
			fact[i]=fact[i-1]*i%mod;
			finv[i]=finv[i-1]*inv[i]%mod;
		}
	}
	long long pw(long long a,long long b){
		if(a<0LL)return 0;
		if(b<0LL)return 0;
		long long ret=1;
		while(b){
			if(b%2)ret=ret*a%mod;
			a=a*a%mod;
			b/=2;
		}
		return ret;
	}
	int ABS(int a){return max(a,-a);}
	long long ABS(long long a){return max(a,-a);}
	double ABS(double a){return max(a,-a);}
	int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
}
// ここから編集しろ
int x[110];
int y[110];
vector<double>th;
int main(){
	int a;scanf("%d",&a);for(int i=0;i<a;i++)scanf("%d%d",x+i,y+i);
	for(int i=0;i<a;i++){
		if(x[i]==0&&y[i]==0)continue;
		double tr=atan2(y[i],x[i]);
		th.push_back(tr);
		for(int j=0;j<a;j++){
			if(i==j)continue;
			if(x[j]==0&&y[j]==0)continue;
			double ts=atan2(y[j],x[j]);
			th.push_back((tr+ts)/2);
		}
	}
	double ret=0;
	for(int i=0;i<th.size();i++){
		double X=0;
		double Y=0;
		double vx=cos(th[i]);
		double vy=sin(th[i]);
		for(int j=0;j<a;j++){
			double prod=vx*x[j]+vy*y[j];
			if(prod>=0){
				X+=x[j];
				Y+=y[j];
			}
		}
		ret=max(ret,sqrt(X*X+Y*Y));
	}
	printf("%.12f\n",ret);
}

Measured memory_bytes: 384
H, W = map(int, input().split())
field = [list(map(int, input().split())) for _ in range(H)]
ans = []
for i in range(H):
    for j in range(W):
        if field[i][j] % 2 == 0:
            continue
        found = False
        for di, dj in ((1, 0), (-1, 0), (0, 1), (0, -1)):
            ni, nj = i + di, j + dj
            if 0 <= ni < H and 0 <= nj < W and field[ni][nj] % 2:
                field[i][j] -= 1
                field[ni][nj] += 1
                found = True
                ans.append((i + 1, j + 1, ni + 1, nj + 1))
                break
        if not found:
            for di, dj in ((1, 0), (0, 1)):
                ni, nj = i + di, j + dj
                if 0 <= ni < H and 0 <= nj < W and field[ni][nj] % 2 == 0:
                    field[i][j] -= 1
                    field[ni][nj] += 1
                    ans.append((i + 1, j + 1, ni + 1, nj + 1))
                    break
print(len(ans))
[print(i, j, ni, nj) for i, j, ni, nj in ans]


Measured memory_bytes: 27404
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.solve();
    }
    public void solve() {
        Scanner scan = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int H = scan.nextInt();
        int W = scan.nextInt();
        int[][] move = {{-1,0},{0,-1},{1,0},{0,1}};
        char[][] map = new char[H][W];
        for (int i = 0; i < H; i++) {
            String s = scan.next();
            char[] d = s.toCharArray();
            map[i] = d;
        }
        int white_count = 0;
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                if (map[i][j] == '.') {
                    white_count += 1;
                }
            }
        }
        int[][] dp = new int[H][W];
        for (int i = 0; i < H; i++) {
            Arrays.fill(dp[i], Integer.MAX_VALUE);
        }
        dp[0][0] = 1;
        Deque<Integer> queue = new ArrayDeque<>();
        queue.add(0);
        while(!queue.isEmpty()) {
            int current = queue.poll();
            int i = current/100;
            int j = current%100;
            for (int k = 0; k < 4; k++) {
                int next_i = i + move[k][0];
                int next_j = j + move[k][1];
                if (next_i < 0 || H - 1 < next_i) {
                    continue;
                }
                if (next_j < 0 || W - 1 < next_j) {
                    continue;
                }
                if (map[next_i][next_j] == '#') {
                    continue;
                }
                if (dp[i][j]+1 < dp[next_i][next_j]) {
                    dp[next_i][next_j] = dp[i][j] + 1;
                    queue.add(next_i * 100 + next_j);
                }
            }
        }
        if (dp[H-1][W-1] == Integer.MAX_VALUE) {
            System.out.println(-1);
            return;
        }
        System.out.println(white_count - dp[H-1][W-1]);
    }
}


Measured memory_bytes: 22604
/*  -*- coding: utf-8 -*-
 *
 * e.cc: E - Rem of Sum is Num
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 200000;

/* typedef */

typedef long long ll;
typedef pair<int,int> pii;

/* global variables */

int as[MAX_N + 1];
pii ps[MAX_N + 1];

/* subroutines */

/* main */

/*
  ass[j]-ass[i] = (j - i) -> ass[j] - j = ass[i] - i
 */

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

  as[0] = 0;
  for (int i = 0; i < n; i++) {
    int ai;
    scanf("%d", &ai);
    as[i + 1] = (as[i] + ai) % k;
  }

  for (int i = 0; i <= n; i++)
    ps[i] = pii((as[i] + (k - i % k)) % k, i);
  sort(ps, ps + n + 1);

  ll sum = 0;
  for (int i = 0; i <= n; i++) {
    pii pj = ps[i];
    pj.second += k;
    int j = lower_bound(ps, ps + n + 1, pj) - ps;
    sum += j - i - 1;
  }

  printf("%lld\n", sum);
  return 0;
}


Measured memory_bytes: 2560
s = input()
count = 0
for i in range(0,len(s),2):
    if s[i] == "L":
        count+=1
for j in range(1,len(s),2):
    if s[j] == "R":
        count +=1
if count >0:
    print('No')
else:
    print('Yes')

Measured memory_bytes: 3060
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define MAX_DIGITS 80
#define RESULT_OVERFLOW "overflow"

int main()
{
    using namespace std;
    string num1, num2, result;
    vector<string> results;
    int count;
    cin >> count;
    for (int i = 0; i < count; ++i) {
        cin >> num1 >> num2;
        if (num1.size() > MAX_DIGITS || num2.size() > MAX_DIGITS) {
            result = RESULT_OVERFLOW;
        }
        else {
            int co = 0; // carryover
            reverse(num1.begin(), num1.end());
            reverse(num2.begin(), num2.end());
            size_t digits = min((int)max(num1.size(), num2.size())+1, MAX_DIGITS);
            result.resize(digits);
            for (int j = 0; j < digits; ++j) {
                string digit1, digit2;
                digit1 = j < num1.size() ? num1.substr(j, 1) : "0";
                digit2 = j < num2.size() ? num2.substr(j, 1) : "0";
                int tmp = atoi(digit1.c_str()) + atoi(digit2.c_str()) + co;
                co = tmp/10;
                // "integer to numeric character" conversion. see ascii table.
                result[j] = char(48 + tmp - co*10);
            }
            if (co == 1) result = RESULT_OVERFLOW;
            else {
                reverse(result.begin(), result.end());
                if (result[0] == '0') result = result.substr(1);
            }
        }
        results.push_back(result);
    }
    for (vector<string>::iterator itr = results.begin();
         itr != results.end(); ++itr) {
        cout << *itr << endl;
    }
}

Measured memory_bytes: 1228
import collections
N = int(input())
xy = [[int(i) for i in input().split()] for _ in range(N)]

diff = []

for i in range(N):
    for j in range(N):
        if i == j:
            continue
        else:
            if xy[i][0] > xy[j][0]:
                diff.append((xy[i][0] - xy[j][0], xy[i][1] - xy[j][1]))
            elif xy[i][0] == xy[j][0] and xy[i][1] >= xy[j][1]:
                diff.append((xy[i][0] - xy[j][0], xy[i][1] - xy[j][1]))
            elif xy[i][0] == xy[j][0] and xy[i][1] < xy[j][1]:
                diff.append((xy[j][0] - xy[i][0], xy[j][1] - xy[i][1]))
            else:
                diff.append((xy[j][0] - xy[i][0], xy[j][1] - xy[i][1]))

if N == 1:
    ans = 1
else:
    c = collections.Counter(diff)
    ans = N - sorted(list(c.values()))[-1] // 2
print(ans)


Measured memory_bytes: 3700
#include<bits/stdc++.h>
using namespace std;
#define inf INT_MAX
#define INF LLONG_MAX
#define ll long long
#define ull unsigned long long
#define M (int)(1e9+7)
#define P pair<int,int>
#define FOR(i,m,n) for(int i=(int)m;i<(int)n;i++)
#define RFOR(i,m,n) for(int i=(int)m;i>=(int)n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define int ll
#define vi vector<int>
#define IP pair<int,P>
#define PI pair<P,int>
#define PP pair<P,P>
#define Yes(f){cout<<(f?"Yes":"No")<<endl;}
#define YES(f){cout<<(f?"YES":"NO")<<endl;}
int Madd(int x,int y) {return (x+y)%M;}
int Msub(int x,int y) {return (x-y+M)%M;}
int Mmul(int x,int y) {return (x*y)%M;}



template< typename T >
struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};

template< typename T >
using Edges = vector< edge< T > >;
template< typename T >
using WG = vector< Edges< T > >;
using UG = vector< vector< int > >;
template< typename T >
using Matrix = vector< vector< T > >;



struct LCA{
  const int MAX_LOG_V = 50;

  vector<vector<int> > G,parent;
  int root=0,V;
  vector<int> depth;
  LCA(){}
  LCA(int V):V(V){init();}
  LCA(int V,WG<int> es):V(V){init();rep(i,V){for(auto e:es[i]){G[i].PB(e.to);}}}

  void init(){
    for(int i=0;i<(int)G.size();i++) G[i].clear();
    G.clear();
    for(int i=0;i<(int)parent.size();i++) parent[i].clear();
    parent.clear();
    depth.clear();
    G.resize(V);
    parent.resize(MAX_LOG_V,vector<int>(V));
    depth.resize(V);
  }

  void add(int u,int v){
    G[u].PB(v);
  }

  void dfs(int v,int p,int d){
    parent[0][v]=p;
    depth[v]=d;
    for(int i=0;i<(int)G[v].size();i++){
      if(G[v][i]!=p) dfs(G[v][i],v,d+1);
    }
  }

  void construct(){
    dfs(root,-1,0);
    for(int k=0;k+1<MAX_LOG_V;k++){
      for(int v=0;v<V;v++){
        if(parent[k][v]<0) parent[k+1][v]=-1;
        else parent[k+1][v]=parent[k][parent[k][v]];
      }
    }
  }

  int lca(int u,int v){
    if(depth[u]>depth[v]) swap(u,v);
    for(int k=0;k<MAX_LOG_V;k++){
      if((depth[v]-depth[u])>>k&1){
        v=parent[k][v];
      }
    }
    if(u==v) return u;
    for(int k=MAX_LOG_V-1;k>=0;k--){
      if(parent[k][u]!=parent[k][v]){
        u=parent[k][u];
        v=parent[k][v];
      }
    }
    return parent[0][u];
  }
};

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

  int n;
  cin>>n;
  WG<int> es(n);
  rep(i,n){
    int k;
    cin>>k;
    rep(j,k){
      int x;
      cin>>x;
      es[i].PB(edge<int>(x,1));
    }
  }

  LCA lca(n,es);
  lca.construct();
  int q;
  cin>>q;
  while(q--){
    int x,y;
    cin>>x>>y;
    cout<<lca.lca(x,y)<<endl;
  }


}


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

#ifdef DEBUG
#define PRINT(x)\
    cout<<"func "<<__func__<<": line "<<__LINE__<<": "<<#x<<" = "<<(x)<<endl;
#define PRINTA(a,first,last)\
    cout<<"func "<<__func__<<": line "<<__LINE__<<": "<<#a<<"["<<(first)<<", "<<(last)<<")"<<endl;\
    for (int i=(first);i<(last);++i){cout<<#a<<"["<<i<<"] = "<<(a)[i]<<endl;}
#define PRINTP(p)\
    cout<<"func "<<__func__<<": line "<<__LINE__<<": "<<#p<<" = ("<<(p.first)<<", "<<(p.second)<<")"<<endl;
#define PRINTI(a,i)\
    cout<<"func "<<__func__<<": line "<<__LINE__<<": "<<#a<<"["<<#i<<"] = "<<#a<<"["<<(i)<<"] = "<<(a)[i]<<endl;
#else
#define PRINT(x)
#define PRINTA(a,first,last)
#define PRINTP(p)
#define PRINTI(a,i)
#endif

#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);--i)
#define REP(i,n) for (int i=0;i<(n);++i)
#define RREP(i,n) for (int i=(n)-1;i>=0;--i)
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define MOD 1000000007

using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

template <class T, class U> void amax(T& x, U y) {if (x < y) x = y;}
template <class T, class U> void amin(T& x, U y) {if (x > y) x = y;}

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

int main() {
    int N;
    cin >> N;
    vector<int> a(N);
    REP(i, N) cin >> a[i];

    bool result = false;
    if (N % 3 == 0) {
        sort(all(a));
        bool b0 = all_of(a.begin(), a.begin() + N / 3, [&](int x) { return x == a[0]; });
        bool b1 = all_of(a.begin() + N / 3, a.begin() + 2 * N / 3, [&](int x) { return x == a[N / 3]; });
        bool b2 = all_of(a.begin() + 2 * N / 3, a.end(), [&](int x) { return x == a[2 * N / 3]; });
        int x = a[0], y = a[N / 3], z = a[2 * N / 3];
        result = b0 && b1 && b2 && x == (y ^ z) && y == (z ^ x) && z == (x ^ y);
    } else {
        result = all_of(all(a), [](int x) { return x == 0; });
    }
    cout << (result ? "Yes" : "No") << endl;

    return 0;
}


Measured memory_bytes: 640
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>

using namespace std;

void solve(string S) {
  int n = S.size();
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (S[i] == '+') {
      ans += 1;
    } else {
      ans -= 1;
    }
  }
  printf("%d\n", ans);
}

int main() {
  string S;
  cin >> S;
  solve(S);
  return 0;
}

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

int main(){
  int A,B,M,ans=1e9;cin>>A>>B>>M;
  vector<int> a(A,0),b(B,0);
  REP(i,A) cin>>a.at(i);
  REP(i,B) cin>>b.at(i);
  REP(i,M){
    int x,y,c;
    cin>>x>>y>>c;x--;y--;
    ans=min(ans,a.at(x)+b.at(y)-c);
  }
  sort(a.begin(),a.end());
  sort(b.begin(),b.end());
  ans=min(ans,a.at(0)+b.at(0));
  cout<<ans;
}

Measured memory_bytes: 1024
from collections import Counter
N = int(input())
A = sorted(list(map(int, input().split())))
count = Counter(A)

max_len_A = 10**6+1
data = [0]*max_len_A
A = list(set(A))

for a in A:
    if data[a]==0:
        for j in range(a+a, max_len_A, a):
            data[j]+=1


ans = len([a for a in A if data[a]==0 and count[a]==1])
print(ans)

Measured memory_bytes: 52432
#include<iostream>
#include<cmath>
using namespace std;
int n,a[100005],cnt,f=1e9;
long long ans;
main(){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
        if(f>abs(a[i]))f=abs(a[i]);
        if(a[i]<0)cnt++;
        ans+=abs(a[i]);
    }
    cout<<(cnt%2?ans-f*2:ans)<<endl;
}

Measured memory_bytes: 640
n = int(input())
t, a = map(int, input().split())
hs = list(map(int, input().split()))
best_sub = 100000
ans = -1
for i, h in enumerate(hs):
    t_ = t - h * 0.006
    if abs(a - t_) < best_sub:
        best_sub = abs(a - t_)
        ans = i + 1
print(ans)


Measured memory_bytes: 3188
#include <bits/stdc++.h>
#define ll long long
#define ls id << 1
#define rs id << 1 | 1
#define mem(array, value, size, type) memset(array, value, ((size) + 5) * sizeof(type))
#define memarray(array, value) memset(array, value, sizeof(array))
#define fillarray(array, value, begin, end) fill((array) + (begin), (array) + (end) + 1, value)
#define fillvector(v, value) fill((v).begin(), (v).end(), value)
#define pb(x) push_back(x)
#define st(x) (1LL << (x))
#define pii pair<int, int>
#define mp(a, b) make_pair((a), (b))
#define Flush fflush(stdout)
#define vecfirst (*vec.begin())
#define veclast (*vec.rbegin())
#define vecall(v) (v).begin(), (v).end()
#define vecupsort(v) (sort((v).begin(), (v).end()))
#define vecdownsort(v, type) (sort(vecall(v), greater<type>()))
#define veccmpsort(v, cmp) (sort(vecall(v), cmp))
using namespace std;
const int N = 500050;
const int inf = 0x3f3f3f3f;
const ll llinf = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
clock_t TIME__START, TIME__END;
void program_end()
{
#ifdef ONLINE
    printf("\n\nTime used: %.6lf(s)\n", ((double)TIME__END - TIME__START) / 1000);
    system("pause");
#endif
}
pair<int, int> p[N];
bool yes[N];

inline void solve()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        cin >> p[i].first >> p[i].second;
        if (p[i].first == p[i].second)
            yes[i] = 1;
    }
    for (int i = 1; i <= n - 2; ++i)
    {
        if (yes[i] && yes[i + 1] && yes[i + 2])
            return puts("Yes"), void();
    }
    puts("No");
}

int main()
{
    TIME__START = clock();
    int Test = 1;
    // scanf("%d", &Test);
    while (Test--)
    {
        solve();
        // if (Test)
        //     putchar('\n');
    }
    TIME__END = clock();
    program_end();
    return 0;
}

Measured memory_bytes: 3612
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define invrepr(i,a,b) for(int i=b-1;i>=a;i--)
#define invrep(i,n) invrepr(i,0,n)
#define repitr(itr,a) for(auto itr=a.begin();itr!=a.end();++itr)
const int MOD=1e9+7;



int main() {
    ios_base::sync_with_stdio(false);
    int n,m;
    cin >> n >> m;
    vector<int> t(n);
    rep(i,m) {
        int a,b;
        cin >> a >> b;
        ++t[a-1];
        ++t[b-1];
    }
    bool flag=1;
    rep(i,n) {
        if (t[i]%2==1) flag=0;
    }
    if (flag) cout << "YES" << endl;
    else cout << "NO" << endl;

    return 0;   
}


Measured memory_bytes: 640
#import pysnooper
#import numpy
#import os,re,sys,operator
#from collections import Counter,deque
#from operator import itemgetter
#from itertools import accumulate,combinations,groupby,combinations_with_replacement,permutations
from sys import stdin,setrecursionlimit
#from copy import deepcopy
#import heapq
#import math
#import string

setrecursionlimit(10**6)
input=stdin.readline

n=int(input().rstrip())
s=input().rstrip()

ans=0
now=s[0]
for i in range(1,n):
    if now!=s[i]:
        ans+=1
        now=s[i]
print(ans+1)

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

signed main(){
    double L;
    cin >> L;
    
    double i = (double)L/3;
    
    cout << setprecision(15) << (double)i*i*i << endl;
    return 0;
    
    return 0;
}



Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
  string s,t;
  cin>>s>>t;
  for(int i=0;i<(int)s.size()+(int)t.size();i++){
    if(i%2==0){
      cout<<s[i/2];
    }
    else{
      cout<<t[i/2];
    }
  }
  cout<<endl;
  return(0);
}

Measured memory_bytes: 256
n=input()
k=int(input())
l=len(n)
dp=[[[0 for _ in range(2)] for _ in range(5)] for _ in range(l+1)]
dp[0][1][1]=int(n[0])-1
dp[0][1][0]=1
for i in range(1,l):
  for j in range(4):
    b=i-1
    now=int(n[i])
    if now==0:
      dp[i][j][0]+=dp[b][j][0]
    else:
      dp[i][j][1]+=dp[b][j][0]
      dp[i][j+1][1]+=dp[b][j][0]*(now-1)
      dp[i][j+1][0]+=dp[b][j][0]
    dp[i][j][1]+=dp[b][j][1]
    dp[i][j+1][1]+=dp[b][j][1]*9
  dp[i][1][1]+=9
print(dp[l-1][k][0]+dp[l-1][k][1])
           


Measured memory_bytes: 9132
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <iomanip>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define repr(e,x) for(auto& e:x)
using namespace std;
typedef long long ll;
typedef long double ld;
//typedef pair<int,int> P;
//int const INF=1001001001;
//ll const INF=1001001001001001001;
ll const MOD=1000000007;

string s,t;
ll cnts[26];
ll cntt[26];
vector<ll> pos[26];

int main(){
    cin>>s>>t;

    rep(i,s.size()) cnts[s[i]-'a']++;
    rep(i,t.size()) cntt[t[i]-'a']++;
    rep(i,s.size()) pos[s[i]-'a'].push_back(i+1);

    rep(i,26){
        if(cnts[i]==0 && cntt[i]>0){
            cout<<-1<<endl;
            return 0;
        }
    }

    ll cnt=0;
    ll prv=0;
    ll ans=0;
    rep(i,t.size()){
        int c=t[i]-'a';
        ll idx=ans%s.size();
        auto p=upper_bound(pos[c].begin(),pos[c].end(),idx);
        if(p==pos[c].end()){
            p=pos[c].begin();
        }
        if(*p<=prv) cnt++;
        prv=*p;
        ans=cnt*s.size()+prv;
        //cout<<cnt<<' '<<prv<<' '<<ans<<endl;
    }
    
    
    cout<<ans<<endl;
}

Measured memory_bytes: 4576
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] A = new int[N];
        for(int i = 0; i < N; i++){
            A[i] = sc.nextInt() - (i+1);
        }
        sc.close();
        Arrays.sort(A);
        int med = N % 2 == 0 ? N/2 -1 : N/2;
        int b = A[med];
        long ans = 0;
        for(int a : A){
            ans += Math.abs(a - b);
        }

        System.out.println(ans);
    }

}


Measured memory_bytes: 88364
import java.io.*;
import java.util.*;

 
public class Main{
   public static void main(String[] args) {
      MyScanner sc = new MyScanner();
      out = new PrintWriter(new BufferedOutputStream(System.out));
      int N=sc.nextInt();
      
      out.println((long)N*(long)(N-1)/2L);

      out.close();
   }

   public static int binarySearch(Integer[] a,int x)
   {
       int right=a.length-1;
       int left=0;

       while(right-left>0){
           int center=(right+left)/2;

           if(x>a[center]){
               left=center+1;
           }else{
               right=center; 
           }
       }
       return right;
   }

   //-----------PrintWriter for faster output---------------------------------
   public static PrintWriter out;
      
   //-----------MyScanner class for faster input----------
   public static class MyScanner {
      BufferedReader br;
      StringTokenizer st;
 
      public MyScanner() {
         br = new BufferedReader(new InputStreamReader(System.in));
      }
 
      String next() {
          while (st == null || !st.hasMoreElements()) {
              try {
                  st = new StringTokenizer(br.readLine());
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          return st.nextToken();
      }
 
      int nextInt() {
          return Integer.parseInt(next());
      }
 
      long nextLong() {
          return Long.parseLong(next());
      }
 
      double nextDouble() {
          return Double.parseDouble(next());
      }
 
      String nextLine(){
          String str = "";
	  try {
	     str = br.readLine();
	  } catch (IOException e) {
	     e.printStackTrace();
	  }
	  return str;
      }
   }
}

Measured memory_bytes: 22484
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<map>
#include<bitset>
#include<iomanip>
#include<queue>
#include<functional>
#include<stack>
#include<tuple>
#include<cassert>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using P = pair<int, int>;

int main() {
  int N, K;
  int a[200001];
  cin >> N >> K;
  a[0] = 1;
  rep(i, N) {
    cin >> a[i+1];
  }
  for(int i = K; i < N; i++) {
    if(a[i+1] > a[i+1-K]) cout << "Yes" << endl;
    else cout << "No" << endl;
  }
  return 0;
}

Measured memory_bytes: 4416
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
using namespace std;

int main() {
    int n;
    while(cin >> n, n) {
        vector<int> S(n+1);
        string str;
        cin >> str;
        S[0] = 0;
        S[1] = str.size();
        for(int i=2; i<=n; ++i) {
            cin >> str;
            S[i] = S[i-1] + str.size();
        }
        int tmp[5] = {5, 7, 5, 7, 7};
        int ret = 0;
        bool finished = false;
        for(int i=0; i<=n; ++i) {
            if(finished) break;
            int current=i;
            int current_haiku = 0;
            int offset=1;
            ret = i;
            while(current+offset <= S.size()) {
                if(S[current+offset] - S[current] == tmp[current_haiku]) {
                    current += offset;
                    offset = 1;
                    current_haiku++;
                    if(current_haiku == 5) {
                        finished = true;
                        break;
                    }
                } else if(S[current+offset] - S[current] > tmp[current_haiku]) {
                    break;
                } else {
                    offset++;
                }
            }
        }
        cout << ret+1 << endl;
    }
}


Measured memory_bytes: 3112
def main():
    n = int(input())
    a = list(map(int, input().split(" ")))
    lim = 1e18
    ans = 1
    if 0 in a:
        print(0)
        return
    for i in a:
        ans *= i
        if ans >lim:
            print(-1)
            return
    print(ans)

if __name__ == "__main__":
    main()

Measured memory_bytes: 21640
#include <stdio.h>
#include <cmath>
#include <map>

using namespace std;

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

const int module = 1e9 + 7;

map<int, int> m;

void divisor(int n) {
    int a = 2;
    while (n >= a * a) {
        if (n % a == 0) {
            // printf("%ld * ", a);
            n /= a;
            decltype(m)::iterator it = m.find(a);
            if (it != m.end()) {
                m[a]++;
            } else {
                m.insert(make_pair(a, 1));
            }
        } else {
            a++;
        }
    }
    // printf("%ld\n", n);
    decltype(m)::iterator it = m.find(n);
    if (it != m.end()) {
        m[n]++;
    } else {
        m.insert(make_pair(n, 1));
    }
}

int main() {
    int N; // 1000
    scanf("%d", &N);

    REP(1, n, N) {
        divisor(n);
    }

    // REP(1, i, N) {
    //     if (m[i] > 0) printf("%d %d\n", i, m[i]);
    // }

    long ans = 1;
    for (decltype(m)::iterator it = m.begin(); it != m.end(); ++it) {
        if (it->first == 1) continue;
        if (it->second > 0) ans *= (it->second + 1);
        if (ans > module) ans = ans % module;
    }

    printf("%ld\n", ans);

    return 0;
}


Measured memory_bytes: 256
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
typedef long long ll;
#define rep(i,n) for (int i = 0; i < (n); i++)
#define repe(i,n) for (int i = 0; i <= (n); ++i)
#define repe1(i,n) for (int i = 0; i <= (n); ++i)
#define all(x) (x).begin(),(x).end()
#define pb(x) push_back(x)
#define eb(k,v) emplace_back(k,v)
#define ct(res) cout << res << "\n";
#define vi vector<int>
#define vl vector<ll>
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const int INF = 1e9;
const ll MOD = 1000000007;
const double pi = 3.141592653589;
using Graph = vector<vector<int>>;
ll gcd(ll a, ll b) {return (a%b) ? gcd(b, a%b) : b;}
ll modPow(ll x, ll n) {
    if(n==0) return 1;
    ll res = modPow(x*x%MOD, n/2);
    if(n&1) res=res*x%MOD;
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    while(true) {
        string s; cin >>s;
        if (s == "0") {
            return 0;
        }
        ll ans = 0;
        for(ll i=0; i<s.size(); ++i) {
            ans += s[i]-'0';
        }
        ct(ans);
    }
}


Measured memory_bytes: 3204
#include <bits/stdc++.h>
#define INF 1e9+7
using namespace std;
using ll=long long int;
using pii=pair<int,int>;



int main () {
    ios::sync_with_stdio(false); cin.tie(0);
    int r; cin>>r;
    cout<<2*r*3.141592653589793238<<endl;

}


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

template<class T> inline void chmin(T& a, T b) {if (a > b) a = b;}
template<class T> inline void chmax(T& a, T b) {if (a < b) a = b;}


int main() {
  int N,M;
  ll l,r,d;
  cin >> N >> M;

  vector<vector<vector<ll>>> q(N);
  rep(i,M) {
    scanf("%lld%lld%lld",&l,&r,&d);
    l--;r--;
    q[l].push_back({r,d});
    q[r].push_back({l,-d});
  }
  vector<ll> ans(N);
  vector<int> yet(N,1);
  queue<int> que;
  ans[0] = 0;
  yet[0] = false;
  que.push(0);

  int memo=1;
  bool flag = true;

  while (flag) {
    
    while (!que.empty()) {
      auto t = que.front();
      // cout << t << endl;
      que.pop();
      for (auto v:q[t]) {
        if (yet[v[0]]) {
          yet[v[0]] = 0;
          ans[v[0]] = ans[t] + v[1];
          que.push((int) v[0]);
        }
        else if (ans[v[0]]-ans[t]!=v[1]) {
          cout << "No\n";
          return 0;
        } 
      }
    }

    rep2(i,memo,N) {
      if (yet[i]) {
        ans[i] = 1;
        que.push((int) i);
        yet[i] = 0;
        memo = i;
        break;
      }
      if (i==N-1) flag = false;
    }
  }

  cout << "Yes" << endl;
} 


Measured memory_bytes: 29824
#include<cstdio>

int main(){
	int n,m;
	int t[100];
	int p[100],x[100];
	int sum = 0;
	scanf("%d",&n);
	for(int i=0;i<n;i++) scanf("%d",t+i);
	scanf("%d",&m);
	for(int i=0;i<m;i++){
		scanf("%d %d",p+i,x+i);
		p[i]--;
	}
	for(int i=0;i<n;i++) sum += t[i];
	for(int i=0;i<m;i++) printf("%d\n",sum+x[i]-t[p[i]]);
}


Measured memory_bytes: 128
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
// *start on @date: 2020-02-16 20:33 
using namespace std;
typedef long long ll;
const int maxn = 1000 + 5;
const int inf  = 0x3f3f3f3f;

map<string,int> m;

int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
int n;string s;
cin>>n;
int ma = -1;
while (n--)
{
    cin>>s;
    m[s]++;
    ma = max(m[s],ma);

}

for (map<string,int>::iterator it = m.begin(); it != m.end(); it++)
{
    if(it->second == ma){
        cout<<it->first<<endl;
    }

}




    
    return 0;
}

Measured memory_bytes: 23808
from collections import defaultdict

n=int(raw_input())
a=map(int,raw_input().split())
check=[]
d=defaultdict(int)
li=set([])
now=0
for i in xrange(n):
    if now<a[i]:
        now=a[i]
        check.append(i)
    d[a[i]]+=1
    li.add(a[i])
li=sorted(list(li))
b=[0]*(n+5)
i=0
idx=dict()
for key in li:
    b[i]=d[key]
    idx[key]=i
    i+=1
ans=[0]*(n+1)
for i in xrange(len(check)-2,-1,-1):
    cnt=0
    for j in xrange(idx[a[check[i]]]+1,idx[a[check[i+1]]]+1):
        ans[check[i+1]]+=b[j]*(li[j]-a[check[i]])
        cnt+=b[j]
    b[idx[a[check[i]]]]+=cnt
for i in xrange(1,n):
    if a[0]<a[i]:
        a[i]=a[0]
ans[0]=sum(a)
for i in xrange(n):
    print ans[i]


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

namespace AtCoderPractic0718
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            int[] nk = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();

            int N = nk[0];
            int K = nk[1];
            string S = Console.ReadLine();
            char[] cs = S.ToCharArray();

            if(cs[K-1] == 'A')
            {
                cs[K - 1] = 'a';
            }
            if (cs[K - 1] == 'B')
            {
                cs[K - 1] = 'b';
            }
            if (cs[K - 1] == 'C')
            {
                cs[K - 1] = 'c';
            }

            string ans = new string(cs);

            Console.WriteLine(ans);
            //k=1 hk=5 　k=2 h2=10 
        }
    }
}


Measured memory_bytes: 28060
#include<iostream>
#include<stack>
#include<cstdlib>
using namespace std;
typedef pair<int,char> P;


int main(){
  string s;
  stack<P> st;
  int cc,lc;
  while(getline(cin,s) && s != "."){
    cc = 0;
    lc = 0;
    for(int i=0;i<s.length();i++)
      {
	if(s[i] == '(')
	  {
	    st.push(P('c',cc));
	    cc++;
	  }
	else if(s[i] == '[')
	  {
	    st.push(P('l',lc));
	    lc++;
	  }
	else if(s[i] == ')')
	  {
	    if(st.empty()){
	      st.push(P('v',-1));
	      break;
	    }
	    if(st.top().first == 'c')
	      {
		cc--;
		st.pop();
	      }
	    else
	      {
		break;
	      }
	  }
	else if(s[i] == ']')
	  {
	    if(st.empty()){
	      st.push(P('t',-2));
	      break;
	    }
	    if(st.top().first == 'l')
	      {
		lc--;
		st.pop();
	      }
	    else 
	      {
		break;
	      }
	  }

      }   

    if(st.empty())
      {
	cout << "yes" << endl;
      }
    else
      {
	cout << "no" << endl;
	while(st.size())
	  {
	    st.pop();
	  }
      }
  

  }



}

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

int main(){
    int n,m;
    cin>>n>>m;
    vector<vector<int>> mat(n,vector<int>(m,0));
    vector<int> plus(m);
    vector<int> minus(m);
    for(int i=0;i<m;i++){
        int s,t;
        cin>>s>>t;
        s--,t--;
        mat[s][i]=1;
        plus[i]=s;
        mat[t][i]=-1;
        minus[i]=t;
    }

    for(int i=0;i<n;i++){
        int e=-1;
        for(int j=0;j<m;j++){
            if(mat[i][j]){
                e=j;
                break;
            }
        }
        if(e==-1) continue;
        int tar;
        if(mat[i][e]>0){
            tar=minus[e];
        }
        else{
            tar=plus[e];
        }
        if(tar<i) continue;
        for(int j=0;j<m;j++){
            mat[tar][j]+=mat[i][j];
            if(mat[tar][j]>0){
                plus[j]=tar;
            }
            else if(mat[tar][j]<0){
                minus[j]=tar;
            }
        }
    }
    int cnt=0;
    for(int i=0;i<n;i++){
        if(all_of(mat[i].begin(),mat[i].end(),[](int x){return x==0;})){
            cnt++;
        }
    }
    cout<<m-(n-cnt)<<endl;
    return 0;
}


Measured memory_bytes: 8684
#include<bits/stdc++.h>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long int
#define mod 1000000007
#define inf 1e18+42
#define endl "\n"
#define pi 3.1415926535897932384626433832795028841971693993751058
#define maxn 100005

#define out1(a) cout<<#a<<" "<<a<<endl
#define out2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<endl
#define out3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<endl

#define rep(i,a,b) for(int i=a;i<b;i++)
#define repr(i,a,b) for(int i=a;i>=b;i--)
#define fori(it,A) for(auto it=A.begin();it!=A.end();it++)

#define ft first
#define sd second
#define pb push_back
#define mp make_pair
#define pq priority_queue
#define all(x) (x).begin(),(x).end()
#define zero(x) memset(x,0,sizeof(x));
 
using namespace std;

int binpow(int a, int b) {
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

//START OF CODE ->->->->->->->

void solve()
{
    int n;
    cin>>n;

    vector<int> a(n+1);
    vector<int> b(n);

    rep(i,0,a.size())
    {
        cin>>a[i];
    }

    rep(i,0,b.size())
    {
        cin>>b[i];
    }
    int ans = 0;
    rep(i,0,b.size())
    {
        int x = min(a[i],b[i]);
        ans += x;
        a[i] -= x;
        b[i] -= x;
        int y = min(a[i+1],b[i]);
        ans += y;
        a[i+1] -= y;
        b[i] -= y;
    }

    cout<<ans<<endl;
}

//END OF CODE ->->->->->->->->

signed main()
{
    fast;
    int t = 1;
    // cin>>t;

    while(t--)
    {
        solve();
    }
    return 0;
}



Measured memory_bytes: 1792
#define _CRT_SECURE_NO_WARNINGS
#define forn(i, a, n) for(ll i = a; i < n; i++)

#include<iostream>
#include<string>
#include<vector>
#include<math.h>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<iomanip>
#include <cassert>
#include<stack>
#include<deque>
#include<list>
#include<sstream>
#include<unordered_map>
#include<time.h>
#pragma comment(linker, "/STACK:880999999")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef pair<pll, ll> ppll;
const ll mod = 1e9 + 7;
const ld eps = 1e-9;
const ll settled = 400007;

int main()
{
	ios_base::sync_with_stdio(false); cout.tie(NULL);
#ifndef overlord
	//freopen("qual.in", "r", stdin);
	//freopen("qual.out", "w", stdout);
#else
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ll a, b, c;
	cin >> a >> b >> c;
	if (a + b < c - 1)
		c = a + b + 1;
	cout << c + b;
	return 0;
}

Measured memory_bytes: 256
/*
				" اللهم صلي و سلم علي سيدنا محمد و علي اله وصحبه اجمعين "
*/


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define f for
#define ll long long
#define ull unsigned long long
#define endl "\n"
using namespace std;
#define BI  acos(-1)
#define all(a) a.begin(), a.end()
void Open()
{
#ifndef ONLINE_JUDGE
	freopen("Input.txt", "r", stdin);
	freopen("Output.txt", "w", stdout);
#endif
}
ll gcd(ll a, ll b)
{
	return b == 0 ? a : gcd(b, a % b);
}
void madesetprobblem()
{
	//$ $
	//less than or equal => $ 1 \leq 100 $
	//  $(1 \le A,B \le 10^5 )$
	// a[i] => $a_i$
	// a[i][j] => $a_{i,j}$
	// double qoutes ''yext''
	/*
		image
		\begine{center}
		\includegraphics{image.jpg}
		\end{center}
	*/
	/*
		list
		\begin{itemize}
			\item first one
			\item second one
		\end{itemize}
	*/
}

int n,k;
ll a[105][2];
vector<vector<ll>>dp(105, vector<ll>(100005,-1));
ll ans(ll c = 0, ll w = 0)
{
	if (c == n)
		return 0;
	if (dp[c][w] != -1)
		return dp[c][w];
	ll p1 = LLONG_MIN, p2 = LLONG_MIN;
	if (w + a[c][0] <= k)
	{
		p1 = a[c][1] + ans(c + 1, w + a[c][0]);
		p2 = ans(c + 1, w);
	}
	else
		p2 = ans(c + 1, w);
	return dp[c][w] = max(p1, p2);

}
int main()
{  
	// توكلت علي الله 
//Open();
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> n>>k;
	f(int i = 0; i < n; i++)
	{
		cin >>a[i][0]>>a[i][1] ;
	}
	cout << ans();
}

Measured memory_bytes: 83328
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <cmath>
using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64,i64> P;
#define rep(i,s,e) for(int i = (s);i <= (e);i++)

string a,b,c;

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

    string ans;
    ans += a.front();
    ans += b.front();
    ans += c.front();

    for(char c : ans)
    {
        cout << (char)(c - 'a' + 'A');
    }
    cout << endl;

}

Measured memory_bytes: 256
mod = 1000000007
l = input()
n = len(l)
dp1 = [1]
dp2 = [1]
for i in range(1, n + 1):
    dp1.append((dp1[i - 1] * 3) % mod)
    dp2.append(dp2[i - 1])
    if l[n - i] == '1':
        dp2[i] = (dp2[i] * 2 + dp1[i - 1]) % mod
print(dp2[n])

Measured memory_bytes: 11120
from collections import deque

def calcwidth(cks):
    if len(cks) == 1: return cks[0]*2
    width = cks[0] + cks[-1]
    for ck1,ck2 in zip(cks[:-1],cks[1:]):
        width += ((ck1+ck2)**2-(ck1-ck2)**2)**0.5
    return width

while True:
    try: W, *rs = list(map(float,input().split()))
    except: break
    rs = deque(sorted(rs))
    dp = [float('inf')]*len(rs)
    cs = deque([rs.popleft()])

    last_pick_small = -1
    # if -1: last pick up is smallest, if 0: last pick up is biggest
    while rs:
        if last_pick_small: nxt = rs.pop()
        else: nxt = rs.popleft()
        if abs(nxt-cs[0]) > abs(nxt-cs[-1]): cs.appendleft(nxt)
        else: cs.append(nxt)
        last_pick_small = -1-last_pick_small
    ret = calcwidth(list(cs))
    if ret <= W: print('OK')
    else: print('NA')




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

const int MAXH = 61;
const int MAXW = 61;
const int INF = 1<<28;
int w, h;
bool vis[MAXH][MAXW];
char g[MAXH][MAXW];

void dfs(int x, int y, char c, vector<pair<int, int> > &b, vector<pair<int, int> > &adj) {
  vis[y][x] = true;
  b.push_back(make_pair(x, y));
  for (int k = 0; k < 4; ++k) {
    int dx = (1 - k) % 2;
    int dy = (2 - k) % 2;
    int nx = x + dx;
    int ny = y + dy;
    if (nx < 0 || nx >= w) continue;
    if (ny < 0 || ny >= h) continue;
    if (vis[ny][nx]) continue;
    if (g[ny][nx] == '.') continue;
    if (g[ny][nx] != c) {
      if (k == 3) adj.push_back(make_pair(nx, ny));
      continue;
    }
    dfs(nx, ny, c, b, adj);
  }
}

vector<pair<int, int> > rec(int x, int y) {
  vector<pair<int, int> > b, adj;
  dfs(x, y, g[y][x], b, adj);
  int xl = INF, xr = -INF;
  for (int i = 0; i < b.size(); ++i) {
    int nx = b[i].first;
    int ny = b[i].second;
    if (ny+1 == h || g[ny+1][nx] != '.' && g[ny+1][nx] != g[ny][nx]) {
      xl = min(xl, nx*2);
      xr = max(xr, (nx+1)*2);
    }
  }
  for (int i = 0; i < adj.size(); ++i) {
    int nx = adj[i].first;
    int ny = adj[i].second;
    if (vis[ny][nx]) continue;
    vector<pair<int, int> > nb = rec(nx, ny);
    for (int j = 0; j < nb.size(); ++j) {
      b.push_back(nb[j]);
    }
  }

  int M = 0;
  for (int i = 0; i < b.size(); ++i) {
    M += b[i].first*2 + 1;
  }
  if (xl * b.size() < M && M < xr * b.size()) ; else {
    throw 0;
  }
  return b;
}

int main() {
  while (cin >> w >> h && (w|h)) {
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> g[i][j];
      }
    }
    memset(vis, 0, sizeof(vis));
    try {
      for (int j = 0; j < w; ++j) {
        if (g[h-1][j] != '.') {
          rec(j, h-1);
          break;
        }
      }
      cout << "STABLE" << endl;
    } catch(...) {
      cout << "UNSTABLE" <<endl;
    }
  }
  return 0;
}

Measured memory_bytes: 1272
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR(i,k,n) for(int(i)=(k);(i)<(n);++(i))
#define rep(i,n) FOR(i,0,n)
#define all(v) begin(v),end(v)
#define debug(x) std::cerr<<#x<<": "<<x<<"\n"
#define debug2(x,y) std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<"\n"
#define debug3(x,y,z) std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<", "<<#z<<": "<<z<<"\n"

using ll=long long;
using vi=std::vector<int>;
using vvi=std::vector<vi>;
using vll=std::vector<ll>;
using vvll=std::vector<vll>;
template<typename T> using vvec=std::vector<std::vector<T>>;
template<typename T>
auto make_v(size_t sz){return std::vector<T>(sz);}
template<typename T,typename... Ts>
auto make_v(size_t sz,Ts...ts){return std::vector<decltype(make_v<T>(ts...))>(sz,make_v<T>(ts...));}
template<typename T>
void fill_v(T&var,const T&x){var=x;}
template<typename V,typename T>
void fill_v(V&v,const T&x){for(auto&& w:v){fill_v(w,x);}}

template<typename T> std::ostream& operator<<(std::ostream&s,const std::vector<T>&v){
  int sz=v.size();s<<"\n";rep(i,sz){s<<v[i];if(i<sz-1){s<<"\t";}}s<<"\n";return s;}
template<typename T> std::ostream& operator<<(std::ostream&s,const std::vector<std::vector<T>>&v){
  for(auto&& w:v){s<<w;}return s;}
template<typename T> std::ostream& operator<<(std::ostream&s,const std::deque<T>&v){
  int sz=v.size();s<<"\n";rep(i,sz){s<<v[i];if(i<sz-1){s<<"\t";}}s<<"\n";return s;}
template<typename T> std::ostream& operator<<(std::ostream&s,const std::deque<std::deque<T>>&v){
  for(auto&& w:v){s<<w;}return s;}
template<typename T> std::ostream& operator<<(std::ostream&s, const std::set<T>&v){
  s<<"\n";for(auto&& elm:v){s<<elm<<"\t";}s<<"\n";return s;}

inline void scan(int&a){scanf("%d",&a);}
inline void scan(ll&a){scanf("%lld",&a);}
inline void scan(char&a){scanf(" %c",&a);}
inline void scan(double&a){scanf("%lf",&a);}
inline void scan(std::string&s){char BUF[3000000];scanf(" %s",BUF);s=std::string(BUF);}
template<typename T>
inline void scan(std::vector<T>&v){for(auto&& sv:v){scan(sv);}}
template<typename First,typename...Args>
inline void scan(First&f,Args&...args){scan(f);scan(args...);}
inline void print(int a){printf("%d\n",a);}
inline void print(ll a){printf("%lld\n",a);}
inline void print(double a){printf("%.12f\n",a);}
inline void print(std::string s){std::cout<<s<<"\n";}

using namespace std;

int main() {
  char c; scan(c);
  if (c == 'A') {
    print("T");
  }
  if (c == 'T') {
    print("A");
  }
  if (c == 'C') {
    print("G");
  }
  if (c == 'G') {
    print("C");
  }
  return 0;
}


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

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

	int a = (s[0] - '0') * 10 + s[1] - '0';
	int b = (s[2] - '0') * 10 + s[3] - '0';

	if (1 <= a && a <= 12)
	{
		if (b == 0 || 12 < b)
		{
			cout << "MMYY" << endl;
		}
		else if(1 <= b && b <= 12)
		{
			cout << "AMBIGUOUS" << endl;
		}
	}
	else
	{
		if (1 <= b && b <= 12)
		{
			cout << "YYMM" << endl;
		}
		else
		{
			cout << "NA" << endl;
		}
	}
}


Measured memory_bytes: 256
#include <iostream>
#include<vector>
#include<map>
using namespace std;
int main(void){
 string a,b;
 cin>>a>>b;
 
 int count=0;
 for(int i=0;i<a.length();i++)
    {
        if(a[i]!=b[i])
        count++;
    }
cout<<count;
}

Measured memory_bytes: 3448
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int main() {
	int k,count=0;
	cin >> k;
	for (int i = 1; i <= k; i+=2) {
		for (int j = 2; j <= k; j += 2) {
			count++;
		}
	}
	cout << count << endl;
}


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

#include <boost/range/adaptors.hpp>
#include <boost/range/irange.hpp>

using namespace std;
using namespace boost;
using namespace boost::adaptors;

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

    constexpr int64_t M = 'z' - 'a' + 1;

    unordered_map<int64_t, int64_t> m;
    m[0] = 0;
    bitset<M> bs;
    for (auto c : s) {
        auto i = c - 'a';
        bs.flip(i);

        if (!m.count(bs.to_ulong())) {
            m[bs.to_ulong()] = numeric_limits<int64_t>::max();
        }
        for (auto j : irange(0L, M)) {
            auto bs2 = bs;
            bs2.flip(j);
            if (m.count(bs2.to_ulong())) {
                m[bs.to_ulong()] = min(m[bs.to_ulong()], m[bs2.to_ulong()] + 1);
            }
        }

        // cerr << bs << ":" << m[bs.to_ulong()] << endl;
    }

    if (bs.none()) {
        cout << 1 << endl;
    } else {
        cout << m[bs.to_ulong()] << endl;
    }
}

Measured memory_bytes: 8780
import Queue
class Node:
    def __init__(self,v):
        self.v = v
        self.c = []

def bfs(n):
    d = {n.v:0}
    q = Queue.Queue()
    q.put(n)
    visited = set([n.v])
    while q.qsize() > 0:
        x = q.get()
        for c in x.c:
            if not c.v in visited:
                q.put(c)
                d[c.v] = d[x.v]+1
                visited.add(c.v)
    return d



n = int(raw_input())
nodes = {}
for _ in range(n):
    entry = map(int,raw_input().split(' '))
    nid = entry[0]
    if not nid in nodes: nodes[nid] = Node(nid)
    k = entry[1]
    if k > 0:
        children = entry[2:]
        for c in children:
            if not c in nodes: nodes[c] = Node(c)
            nodes[nid].c.append(nodes[c])

d = bfs(nodes[1])
for nid in nodes:
    if not nid in d:
        d[nid] = -1
for k in d:
    print k,d[k]

Measured memory_bytes: 6980
#include <iostream>
#include <stdio.h>

using namespace std;
typedef long long int lint;

void answer()
{
	int H, W;
	while (1) {
		cin >> H >> W;
		if (H == 0 && W == 0) break;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				cout << ((i == 0 || i == H - 1 || j == 0 || j == W - 1) ? '#' : '.');
			}
			cout << endl;
		}
		cout << endl;
	}
}

int main()
{
	answer();
	return 0;
}

Measured memory_bytes: 1152