akhauriyash/Code-Regression huggingface dataset:
HW = list(map(int,input().split()))
H=HW[0]
W=HW[1]
S = [list(input()) for i in range(H)]
cnt=0
for i in range(H):
    for j in range(W):
            if S[i][j]=='#':
                continue
            try:
                if S[i-1][j-1]=='#' and i-1>=0 and j-1>=0:
                    cnt=cnt+1            
            except:
                None
                          
            try:
                if S[i-1][j]=='#' and i-1>=0:
                    cnt=cnt+1
            except:
                None
            try:
                if S[i-1][j+1]=='#' and i-1>=0:
                    cnt=cnt+1
            except:
                None
            try:
                if S[i][j-1]=='#' and j-1>=0:
                    cnt=cnt+1
            except:
                None
                
            try:
                if S[i][j+1]=='#':
                    cnt=cnt+1
            except:
                None
            try:
                if S[i+1][j-1]=='#' and j-1>=0:
                    cnt=cnt+1
            except:
                None
            try:
                if S[i+1][j]=='#':
                    cnt=cnt+1
            except:
                None
            try:
                if S[i+1][j+1]=='#':
                    cnt=cnt+1
            except:
                None
            S[i][j]=cnt
            cnt=0

for a in S:
        print(*a, sep='')



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

int main() {
  string s, t;
  cin >> s >> t;
  
  int ans = 1e9;
  for(int i = 0; i < s.size(); i++) {
  	if(i+t.size() > s.size()) break;
    int cnt = 0;
    for(int j = 0; j < t.size(); j++) {
      if(s[i+j]!= t[j]) cnt++;
    }
    ans = min(ans, cnt);
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 3632
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        try (Scanner in = new Scanner(System.in)) {
            int H = in.nextInt();
            int W = in.nextInt();
            char[][] a = new char[H][W];
            for (int r = 0; r < H; r++) {
                String line = in.next();
                a[r] = line.toCharArray();
            }

            boolean[] is = new boolean[H];
            for (int r = 0; r < H; r++) {
                boolean iswhite = true;
                for (int c = 0; c < W; c++) {
                    iswhite &= (a[r][c] == '.' || a[r][c] == 'W');
                }
                is[r] = iswhite;
                if (iswhite) {
                    for (int c = 0; c < W; c++) {
                        a[r][c] = 'W';
                    }
                }
            }

            for (int c = 0; c < W; c++) {
                boolean iswhite = true;
                for (int r = 0; r < H; r++) {
                    iswhite &= (a[r][c] == '.' || a[r][c] == 'W');
                }
                if (iswhite) {
                    for (int r = 0; r < H; r++) {
                        a[r][c] = 'W';
                    }
                }
            }

            StringBuilder res = new StringBuilder();
            for (int r = 0; r < H; r++) {
                if (is[r]) {
                    continue;
                }
                for (int c = 0; c < W; c++) {
                    if (a[r][c] != 'W') {
                        res.append(a[r][c]);
                    }
                }
                res.append("\n");
            }

            System.out.println(res.toString());
        }
    }
}


Measured memory_bytes: 29892
import java.util.Scanner;


public class Main {
	static long  sieve[]=new long[9910000];
	public static void bal()
	{
	    int i,a,b,j;
	    double n = Math.sqrt(2000010.0);
	    for(i=2; i<=(int)n; i++)
	    {
	        if(sieve[i]==0)
	        {
	            for(a=i; a*i<=2000010; a++)
	            {
	                sieve[a*i]=1;
	            }
	        }

	    }
	}
	public static void main(String args[])
	   {
		 long i,j,k,n,c=0;
		 bal();
		    Scanner ui = new Scanner (System.in);
		    while(ui.hasNext()){
		    	n=ui.nextLong();
		    	c=0;
		    for(i=2;i<=n;i++)
		    {
		       
		        if(sieve[(int) i]!=1)
		        {
		          c++;
		        }
		    }
		    System.out.println(c);}
	   }}

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

typedef unsigned long long ull;

#define REP(i, N) for (int i = 0; i < N; i++)
#define ANS(a) cout << a << endl;
#define SORT(V) sort(V.begin(), V.end())
#define RSORT(V) sort(V.rbegin(), V.rend())

ull
gcd(ull a, ull b)
{
  if (a < b) { ull tmp = a; a = b; b = tmp; }
  if (a % b) return gcd(b, a % b); else return b;
}

ull
lcm(ull a, ull b)
{
  return a * (b / gcd(a, b));
}

int
main()
{
  ull N; cin >> N;
  vector<ull> A(N);
  REP(i, N) cin >> A[i];
  
  ull ans = 0;
  REP(i, N) {
    ans += A[i] - 1;
  }

  ANS(ans);

  return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
int main(){
  char s[10];
  scanf("%s",s);
  
  int i=0, ans=0;
  //for(;i<3;++i) ans += s[i]-'0';
  for(;i<3;++i){
    if(s[i]=='1') ans++;
  }
  
  printf("%d\n",ans);
  return 0;
}


Measured memory_bytes: 1700
#include <iostream>
#include <algorithm>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <cstring>
#include <utility>
#include <vector>
#include <complex>
#include <valarray>
#include <fstream>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <random>

#define _overload(a, b, c, d, ...) d
#define _rep1(X, A, Y) for (int (X) = (A);(X) <= (Y);++(X))
#define _rep2(X, Y) for (int (X) = 0;(X) < (Y);++(X))
#define rep(...) _overload(__VA_ARGS__, _rep1, _rep2)(__VA_ARGS__)
#define rrep(X,Y) for (int (X) = Y-1;(X) >= 0;--(X))
#define all(X) (X).begin(),(X).end()
#define len(X) ((int)(X).size())
#define mod(n, m) (((n)%(m)+(m))%m)
#define fi first
#define sc second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
const int INFINT = 1 << 30;                          // 1.07x10^ 9
const ll INFLL = 1LL << 60;                          // 1.15x10^18
const double EPS = 1e-10;
const int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

const int MAX_N = 100000;
ll N, X;
ll b[MAX_N], l[MAX_N], u[MAX_N];
ll sum[MAX_N];
ll accum[MAX_N];

int main() {
  cin >> N >> X;
  rep(i, N) cin >> b[i] >> l[i] >> u[i];

  for (int i = 0; i < N; ++i) {
    sum[i] = b[i]*l[i] + (X-b[i])*u[i];
  }

  vector<Pll> vp;
  rep(i, N) vp.push_back(Pll(sum[i], i));
  sort(all(vp));
  reverse(all(vp));

  accum[0] = vp[0].fi;
  for (int i = 1; i < N; ++i) {
    accum[i] = accum[i-1] + vp[i].fi;
  }
  ll D = 0;
  rep(i, N) D += b[i]*l[i];
  ll lb = -1, ub = X*N, mid;
  while ((ub-lb)>1) {
    mid = (ub+lb)/2;
    ll q = mid/X;
    ll r = mid - q*X;
    ll tmp = 0;
    if (r == 0 && q != 0) {
      tmp = accum[q-1];
    } else if (q == 0) {
      for (int i = 0; i < N; ++i) {
        tmp = max(tmp, min(r, b[i])*l[i] + max(0ll, r-b[i])*u[i]);
      }
    } else {
      for (int i = 0; i < N; ++i) {
        ll tmp2 = 0;
        ll id = vp[i].sc;
        if (i < q) {
          tmp2 += accum[q];
          tmp2 += - vp[i].fi + min(r, b[id])*l[id] + max(0ll, r-b[id])*u[id];
        } else {
          tmp2 += accum[q-1];
          tmp2 += min(r, b[id])*l[id] + max(0ll, r-b[id])*u[id];
        }
        tmp = max(tmp, tmp2);
      }
    }
    if (tmp >= D) {
      ub = mid;
    } else {
      lb = mid;
    }
  }
  cout << ub << endl;
  return 0;
}


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

struct UnionFind {
	vector<int> par;
	vector<int> rank;
	vector<int> size;
	
	UnionFind(int N) : par(N), rank(N), size(N){
		rep(i,N){
			par[i] = i;
			rank[i] = 0;
			size[i] = 1;
		}
	}
	
	int root(int x) {
		if (par[x] == x) return x;
		par[x] = root(par[x]);
		return par[x];
	}
	
	void unite(int x, int y){
		int rx = root(x);
		int ry = root(y);
		if(rx == ry) return;
		if(rank[rx] < rank[ry]){
			par[rx] = ry;
			size[ry] += size[rx];
		}else{
			par[ry] = rx;
			size[rx] += size[ry];
			if(rank[rx] == rank[ry]) rank[rx]++;
		}
	}
	
	bool same(int x, int y){
		int rx = root(x);
		int ry = root(y);
		return rx == ry;
	}
};

int main(){
	int n,m;
	cin >> n >> m;
	UnionFind uf(n+m);
	rep(i,n){
		int k;
		cin >> k;
		rep(j,k){
			int l;
			cin >> l;
			l--;
			uf.unite(i, n+l);
		}
	}
	
	bool ok = true;
	int a = uf.root(0);
	
	rep(i,n){
		if(a != uf.root(i)) ok = false;
	}
	if(ok){
		cout << "YES" << endl;
	}else{
		cout << "NO" << endl;
	}
	
    return 0;
}



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

using namespace std;

#define REP(i,m) for(int i=0; i<m; i++)
#define FOR(i,n,m) for(long long i=n; i<m; i++)
#define INF 1000000001
#define INFL (1LL << 60)
#define MOD 998244353
#define ALL(v) v.begin(),v.end()
#define pb push_back
#define ll long long int
#define P pair<int, int>

int main() {
  int n;
  cin >> n;
  int ans = 0;
  int tmp = 0;
  int cur = 0;
  REP(i, n){
    int a;
    cin >> a;
    if(cur != a){
      cur = a;
      ans += tmp/2;
      tmp = 0;
    }
    tmp++;
  }
  ans += tmp/2;
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
import java.util.*;

public class Main {

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

  public static void main(String[] args) {
    int A = sc.nextInt();

    for (int i=1; i<10; i++){
      if(A%i==0 && A/i<10){
        System.out.println("Yes");
        return;
      }
    }
    System.out.println("No");
  }
}


Measured memory_bytes: 21716
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector> 
using namespace std;
set<int> st;

int main(){
	int n,ans=0;
	scanf("%d",&n);
	//memset(f,false,sizeof(f));
	for(int i=1;i<=n;i++){
		int x;
		scanf("%d",&x);
		if(st.find(x)==st.end()) st.insert(x);
		else st.erase(x);
	}
	printf("%d\n",st.size());
	return 0;
}
/*
B


int main(){
	int n,ans=0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int a,b;
		scanf("%d%d",&a,&b);
		ans+=b-a+1;
	}
	printf("%d\n",ans);
	return 0;
}
*/

/*
A
int main(){
	int n;
	scanf("%d",&n);
	if(n%10==9||n/10==9) printf("Yes\n");
	else printf("No\n");
	return 0;
} 
*/


Measured memory_bytes: 4864
use std::io;
fn main() {
        let mut input = String::new();
        io::stdin().read_line(&mut input);
        let mut it = input.split_whitespace();
        let a = it.next().unwrap().parse::<i32>().unwrap();
        if a < 1200 { println!("ABC"); }
        else { println!("ARC"); }
 }


Measured memory_bytes: 4352
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

typedef long long ll;

int main(){

    ll n;
    cin >> n;

    ll digit = 0;
    ll n_tmp = n;
    while(n_tmp > 0){
        digit += n_tmp % 10;
        n_tmp = n_tmp / 10;
    }

    if(n % digit == 0) cout << "Yes" << endl;
    else cout << "No" << endl;

}

Measured memory_bytes: 256
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <bitset>
#include <algorithm>
#include <functional>
#include <utility>
#include <iomanip>

#define int long long int
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() )

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9+7;

template <typename T>
using vector2 = vector<vector<T>>;
template <typename T>
vector2<T> initVec2(size_t n0, size_t n1, T e = T()){
    return vector2<T>(n0, vector<T>(n1, e));
}

template <typename T>
using vector3 = vector<vector<vector<T>>>;
template <typename T>
vector3<T> initVec3(size_t n0, size_t n1, size_t n2, T e = T()){
    return vector3<T>(n0, vector2<T>(n1, vector<T>(n2, e)));
}

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

    int a, b;
    cin >> a >> b;
    cout << (a + b - 3) / (a - 1) << endl;

    return 0;
}


Measured memory_bytes: 256
import java.util.*;

/**
 * ICPCScoreTotalizerSoftware
 */
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            int n = sc.nextInt();
            if (n == 0)
                break;

            List<Integer> list = new ArrayList<Integer>();

            for (int i = 0; i < n; i++) {
                int num = sc.nextInt();
                list.add(num);
            }

            Collections.sort(list);
            list.remove(0);
            list.remove(list.size() - 1);
            // 最初と最後をカットして平均切り捨てしゅつりょく
            int ans = 0;
            for (int i : list) {
                ans += i;
            }

            System.out.println(ans / list.size());
        }
    }
}


Measured memory_bytes: 29120
def get_format(a, b):
  if 0 < a < 13 and 0 < b < 13:
    return 'AMBIGUOUS'
  elif 0 < a < 13:
    return "MMYY"
  elif 0 < b < 13:
    return "YYMM"
  else:
    return 'NA'
 
s = input()
s1 = int(s[:2])
s2 = int(s[2:])
print(get_format(s1, s2))

Measured memory_bytes: 2940
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()

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

    vector<pair<int,int>> r(n),b(n);
    rep(i,n){
        int a,b;
        cin>>a>>b;
        r[i]={a,b};
    }
    rep(i,n){
        int c,d;
        cin>>c>>d;
        b[i]={c,d};
    }

    sort(all(b));
    vector<bool> used(n,false);
    int ans=0;
    rep(i,n){
        int xb=b[i].first;
        int yb=b[i].second;
        int ind=-1;
        int ma=-1;
        rep(j,n){
            int xr=r[j].first;
            int yr=r[j].second;

            if(used[j]) continue;
            if(xr>xb || yr>yb) continue;
            if(ma<yr){
                ind=j;
                ma=yr;
            }
        }
        if(ind!=-1){
            ans++;
            used[ind]=true;
        }
        //cout<<i<<" "<<ind<<endl;
    }
    //cout<<used[0]<<endl;
    cout<<ans<<endl;
}


 

Measured memory_bytes: 256
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
#define rep(i,x) for(ll i = 0; i < (ll)(x); i++)
#define rrep(i,x) for(ll i = (ll)(x)-1;0 <= i; i--)
#define reps(i,x) for(ll i = 1; i < (ll)(x)+1; i++)
#define rreps(i,x) for(ll i = (ll)(x); 1 <= i; i--)
#define debug(x) cerr << #x << ": " << (x) << "\n";
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll,ll> Pll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
const ll INF = numeric_limits<ll>::max()/4;
const int n_max = 1e5+10;

void print() {
    cout << endl;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail) != 0) cout << ' ';
    print(forward<Tail>(tail)...);
}

template <class T>
void print(vector<T> &vec) {
    for (auto& a : vec) {
        cout << a;
        if (&a != &vec.back()) cout << ' ';
    }
    cout << endl;
}

template <class T>
void print(vector<vector<T>> &df) {
    for (auto& vec : df) {
        print(vec);
    }
}

template<class T, class U>
void print(pair<T,U> &p){
    print(p.first, p.second);
}


template<class T>
bool chmax(T &a, T b){if(a < b){a = b; return true;} return false;}
template<class T>
bool chmin(T &a, T b){if(a > b){a = b; return true;} return false;}

ll dfs(vvl &graph, ll pos = 0){
    priority_queue<ll, vector<ll>, greater<ll>> pq;
    for(auto i : graph[pos]){
        pq.emplace(dfs(graph, i));
    }
    ll ans = 0;
    while(!pq.empty()){
        chmax(ans, pq.top());
        pq.pop();
        ans++;
    }

    return ans;
}

int main(){
    ll n; cin >> n;
    vector<vector<ll>> graph(n);
    rep(i,n-1){
        ll a; cin >> a;
        a--;
        graph[a].emplace_back(i+1);
    }
    // print(graph);
    // rep(i,n){
    //     debug(i);debug(dfs(graph, i));
    // }
    cout << dfs(graph) << endl;
}

Measured memory_bytes: 13568
N, Q = map(int, input().split())
s = '#' + input() + '#'
T = []
D = []
for t, d in [input().split() for _ in range(Q)]:
  T.append(t)
  D.append(-1 if d=='L' else 1)

#そのゴーレムがどちらかの端にたどり着けるかの関数
def canReachEnd(iL, dir):
  nowL = iL
  nowt = s[iL]
  for t, d in zip(T, D):
    if t == nowt:
      nowL += d
      nowt = s[nowL]
  if dir == 1 and nowL == N+1:
    return True
  elif dir == -1 and nowL == 0:
    return True
  else:
    return False

#1.iは左に辿り着くがi+1は辿り着かない iを探す
left = 0
right = N+1
while right > left+1:
  check = (left+right)//2
  if canReachEnd(check, -1):
    left = check
  else:
    right = check
leftborder = left

#2.jは右に辿り着くがj-1は辿り着かない jを探す
left = leftborder
right = N+1
while right > left+1:
  check = (left+right)//2
  if canReachEnd(check, 1):
    right = check
  else:
    left = check
rightborder = right

dropcnt = 0
dropcnt += leftborder
dropcnt += (N-(rightborder-1))
print(N-dropcnt)

Measured memory_bytes: 42600
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#include <list>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
//#define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream& operator<<(ostream& os, pair<T, S> a) {
    os << '(' << a.first << ',' << a.second << ')';
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
    for (auto x : v) os << x << ' ';
    return os;
}
void debug() {
    cerr << '\n';
}
template <typename H, typename... T>
void debug(H a, T... b) {
    cerr << a;
    if (sizeof...(b)) cerr << ", ";
    debug(b...);
}
int main(){
    int n;
    cin >> n;
    string s;
    cin >> s;
    rep(i,s.size()){
        cout << (char)((s[i]-'A'+n)%26+'A');
    }
}


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

int main(){
  int N=0 ;
  cin>>N;
  int d[N];
  int HP=0;
  for(int i=0;i<N;i++){
    cin >>d[i];
  }
  
  for(int i=0;i<N;i++){
    for(int j=i+1;j<N;j++){
      HP=HP+d[i]*d[j];
    }
  }
  
  cout<< HP;
  
  return 0;
}

      

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

using namespace std;

/*  テンプレここから */
typedef long long ll;

#define rep(i, n) for(int i = 0; i < n; ++i)
#define repi(i, start, end) for(int i = start; i < end; ++i)
#define all(x) (x).begin(), (x).end()
#define INF (1e9)
#define INFL (1e18)
#define add_mod(a, b, x) ( (ll)(a) + (ll)(b) ) % (ll)(x)
#define mul_mod(a, b, x) ( ((ll)(a) % (ll)(x)) * ((ll)(b) % (ll)(x)) ) % (ll)(x)
#define out(x) cout << (x) << endl;
#define outyes(bool_val) cout << (bool_val ? "yes" : "no") << endl;
#define outYes(bool_val) cout << (bool_val ? "Yes" : "No") << endl;
#define outYES(bool_val) cout << (bool_val ? "YES" : "NO") << endl;
#define outPossible(bool_val) cout << (bool_val ? "Possible" : "Impossible") << endl;
#define outPOSSIBLE(bool_val) cout << (bool_val ? "POSSIBLE" : "IMPOSSIBLE") << endl;

template<typename A, size_t N, typename T>
void array_fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

ll sub_mod(ll a, ll b, ll x){
    ll tmp = (a - b) % x;
    if(tmp < 0) tmp += x;
    return tmp;
}
ll gcd(ll a, ll b){
    if(b == 0) return a;
    else return gcd(b, a % b);
}

int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
/*  テンプレここまで */

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

    int r;
    cin >> r;
    cout << r*r << endl;
}

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



void solve(){
    int n;
    cin >> n;
    int a[n+10], b[n+10], c[n+10];
    rep(i,n){
        cin >> a[i];
    }
    rep(i,n){
        cin >> b[i];
    }
    rep(i,n-1){
        cin >> c[i];
    }
    int sum = b[0];
    for(int i=1; i<n; i++){
        if(a[i]-a[i-1]==1){
            sum += c[a[i-1]-1];
        }
        sum += b[i];
    }
    cout << sum << endl;
}

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

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<deque>
#include<functional>
#include<iterator>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<utility>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define a first
#define b second
#define sz(x) (ll)((x).size())
#define pb push_back
#define mp make_pair
#define bg begin()
#define ed end()
#define all(x) (x).bg,(x).ed
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rep1(i,n) for(ll i=1;i<=(n);i++)
#define rrep(i,n) for(ll i=(n)-1;i>=0;i--)
#define rrep1(i,n) for(ll i=(n);i>=1;i--)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
const ll MOD=1000000007;
const ll INF=1000000000000000;
template<class T> inline bool chmin(T& a, T b){if(a>b){a=b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b){if(a<b){a=b;return true;}return false;}
ll maxx(ll x,ll y,ll z){return max(max(x,y),z);}
ll minn(ll x,ll y,ll z){return min(min(x,y),z);}
ll gcd(ll x,ll y){if(x%y==0) return y;else return gcd(y,x%y);}
ll lcm(ll x,ll y){return x*(y/gcd(x,y));}
ll digsz(ll x){if(x==0) return 1;else{ll ans=0;while(x){x/=10;ans++;}return ans;}}
ll digsum(ll x){ll sum=0;while(x){sum+=x%10;x/=10;}return sum;}
vector<ll> pw2(62,1);vector<ll> pw10(19,1);

ll fac[500001],finv[500001],inv[500001];
 
void table() {
    fac[0]=fac[1]=1;
    finv[0]=finv[1]=1;
    inv[1]=1;
    for(ll i=2;i<500001;i++){
        fac[i]= fac[i-1]*i%MOD;
        inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;
        finv[i]=finv[i-1]*inv[i]%MOD;
    }
}

int main(){
    table();
    {rep1(i,61) pw2[i]=2*pw2[i-1];}
    {rep1(i,18) pw10[i]=10*pw10[i-1];}
    
    ll N,M; cin>>N>>M;
    
    if(abs(N-M)>=2){
        cout<<0<<endl;
    }
    else{
        if(N==M){
            cout<<((fac[M]*fac[N])*2)%MOD<<endl;
        }
        else{
            cout<<(fac[M]*fac[N])%MOD<<endl;
        }
    }
}



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

using namespace std;

#define FOR(i, n) for(int i = 0; i < (int) n; i++)
#define PB push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define F first
#define S second

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

const int MOD = 998244353;

int dp[3007][3007];

int binExp(int x, int p){
	int ans = 1;
	while(p){
		if(p&1)
			ans = 1LL * ans * x % MOD;
		x = 1LL * x * x % MOD;
		p >>= 1;
	}
	return ans;
}

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

	int a, b, c, d; cin >> a >> b >> c >> d;

	dp[a][b] = 1;

	for(int i = a; i <= c; i++){
		for(int j = b; j <= d; j++){
			if(i == a && j == b) continue;
			else if(i == a) dp[i][j] = 1LL*i*dp[i][j-1] % MOD;
			else if(j == b) dp[i][j] = 1LL*j*dp[i-1][j] % MOD;
			else{
				dp[i][j] = (1LL*i*dp[i][j-1]+1LL*j*dp[i-1][j]) % MOD;
				dp[i][j] = (dp[i][j] - 1LL*(i-1)*(j-1)*dp[i-1][j-1] % MOD + MOD) % MOD; 
			}
		}
	}

	cout << dp[c][d] << '\n';

	return 0;
}


Measured memory_bytes: 38792
#include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
#include<numeric>
#include<iostream>
#include<array>
#include<string>
#define _USE_MATH_DEFINES

#include<math.h>
#include<unordered_map>

#define min(a,b) (a)>(b)?(b):(a)
#define max(a,b) (a)>(b)?(a):(b)

using namespace std;

int main()
{
	double a, b, C;

	cin >> a >> b >> C;

	double c = sqrt(a*a + b*b - 2 * a*b*cos(C / 180.0*M_PI));

	printf("%lf\n", 0.5*a*b*sin(C / 180.0*M_PI));
	printf("%lf\n", a + b + c);
	printf("%lf\n", b*sin(C / 180.0*M_PI));
}

Measured memory_bytes: 3420
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <clocale>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define FOR(i, s, n) for(int i = (s), i##_len=(n); i < i##_len; ++i)
#define FORS(i, s, n) for(int i = (s), i##_len=(n); i <= i##_len; ++i)
#define VFOR(i, s, n) for(int i = (s); i < (n); ++i)
#define VFORS(i, s, n) for(int i = (s); i <= (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FORS(i, 0, n)
#define VREP(i, n) VFOR(i, 0, n)
#define VREPS(i, n) VFORS(i, 0, n)
#define RFOR(i, s, n) for(int i = (s), i##_len=(n); i >= i##_len; --i)
#define RFORS(i, s, n) for(int i = (s), i##_len=(n); i > i##_len; --i)
#define RREP(i, n) RFOR(i, n, 0)
#define RREPS(i, n) RFORS(i, n, 0)
#define ALL(v) (v).begin(), (v).end()
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(ALL(v), greater<decltype(v[0])>())
#define SZ(x) ((int)(x).size())
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())

using ll = long long;
using ui = unsigned int;
using ull = unsigned long long;
using Pi_i = pair<int, int>;
using VB = vector<bool>;
using VC = vector<char>;
using VD = vector<double>;
using VI = vector<int>;
using VLL = vector<ll>;
using VS = vector<string>;
using VSH = vector<short>;
using VULL = vector<ull>;

const int MOD = 1000000007;
const int INF = 1000000000;
const int NIL = -1;
const double EPS = 1E-10;

template<class T, class S>
bool chmax(T &a, const S &b){
    if(a < b){
        a = b; return true;
    }
    return false;
}
template<class T, class S>
bool chmin(T &a, const S &b){
    if(b < a){
        a = b; return true;
    }
    return false;
}



int main(){
    int N, M; cin >> N >> M;
    int ac = 0;
    VI wa(N);
    VB prob(N, false);
    REP(i, M){
        int p; cin >> p; --p;
        string S; cin >> S;
        if(prob[p]) continue;
        if(S == "AC"){
            prob[p] = true;
            ++ac;
        }else{
            ++wa[p];
        }
    }
    int waCnt = 0;
    REP(i, N){
        if(prob[i]) waCnt += wa[i];
    }
    cout << ac << " " << waCnt << endl;
    return 0;
}


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

#ifdef _DEBUG
#define debug(x) cerr << "line: " << __LINE__ << ", func: " << __func__ << " ->  " << #x << " = " << x << endl
#else
#define debug(x)
#endif

#define all(s) begin(s), end(s)
#define rall(s) rbegin(s), rend(s)
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = ((a)-1); i >= (b); i--)
#define pb push_back
#define sz(a) int((a).size())
#define put(a) ((cout) << (a) << (endl))
#define putf(a, n) ((cout) << (fixed) << (setprecision(n)) << (a) << (endl))
#define deg2rad(x) (((x)*PI) / (180.0))
#define rad2deg(x) (((x) * (180.0)) / PI)

using namespace std;

using ll = long long;
using ull = unsigned long long;
using i_i = pair<int, int>;
using ll_ll = pair<ll, ll>;
using d_ll = pair<double, ll>;
using ll_d = pair<ll, double>;
using d_d = pair<double, double>;
template <class T> using vec = vector<T>;

static constexpr ll LL_INF = 1LL << 60;
static constexpr int I_INF = 1 << 28;
static constexpr double PI = static_cast<double>(3.14159265358979323846264338327950288);
static constexpr double EPS = numeric_limits<double>::epsilon();

static map<type_index, const char* const> scanType = {
    {typeid(int), "%d"}, {typeid(ll), "%lld"}, {typeid(double), "%lf"}, {typeid(char), "%c"}};

template <class T> static void scan(vector<T>& v);
[[maybe_unused]] static void scan(vector<string>& v, bool isWord = true);
template <class T> static inline bool chmax(T& a, T b);
template <class T> static inline bool chmin(T& a, T b);
template <class T> static inline T gcd(T a, T b);
template <class T> static inline T lcm(T a, T b);
template <class A, size_t N, class T> static void Fill(A (&arr)[N], const T& val);
template <class T> T mod(T a, T m);

int main(int argc, char* argv[]) {
  ll n, k;
  cin >> n >> k;
  vec<ll_ll> a(n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i].first);
    scanf("%lld", &a[i].second);
  }

  sort(all(a));
  rep(i, 0, n) {
    k -= a[i].second;
    if (k <= 0) {
      put(a[i].first);
      return 0;
    }
  }
  return 0;
}

template <class T> static void scan(vector<T>& v) {
  auto tFormat = scanType[typeid(T)];
  for (T& n : v) {
    scanf(tFormat, &n);
  }
}

static void scan(vector<string>& v, bool isWord) {
  if (isWord) {
    for (auto& n : v) {
      cin >> n;
    }
    return;
  }

  int i = 0, size = v.size();
  string s;
  getline(cin, s);

  if (s.size() != 0) {
    i++;
    v[0] = s;
  }

  for (; i < size; ++i) {
    getline(cin, v[i]);
  }
}

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

template <class T> inline T gcd(T a, T b) { return __gcd(a, b); }

template <class T> inline T lcm(T a, T b) {
  T c = min(a, b), d = max(a, b);
  return c * (d / gcd(c, d));
}

template <class A, size_t N, class T> void Fill(A (&arr)[N], const T& val) {
  std::fill((T*)arr, (T*)(arr + N), val);
}

template <class T> T mod(T a, T m) { return (a % m + m) % m; }


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

int main(){
int r;
cin >> r;
double l=2*r*3.14159265358979;
cout << fixed << setprecision(10);
cout << l << endl;


}

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

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

int nextInt() { int x; scanf("%d", &x); return x;}

const int MAX_N = 112345;

int L[MAX_N];
int R[MAX_N];

int main2() {
  int N = nextInt();
  vector< pair<int,int> > P(N);
  REP(i, N) {
    P[i] = { nextInt(), i+1 };
  }
  sort(ALL(P));

  for (int i = 0; i <= N; i++) R[i] = i+1;
  R[N+1] = N+1;
  for (int i = 1; i <= N+1; i++) L[i] = i-1;
  L[0] = 0;

  ll ans = 0;
  for (auto pi : P) {
    ll val = pi.first;
    int pos = pi.second;
    int l1 = L[pos];
    int l2 = L[L[pos]];
    int r1 = R[pos];
    int r2 = R[R[pos]];
    ans += val * ((ll)(l1 - l2)*(r1 - pos) + (ll)(pos - l1)*(r2 - r1));

    R[l1] = r1;
    L[r1] = l1;
  }

  cout << ans << endl;
  return 0;
}

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}


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

using ll = long long;

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

    array<ll, 26> counts{};
    for (const char c : A)
        counts[c-'a']++;

    const ll N = ll(A.length());
    ll answer = N * (N-1) / 2 + 1;
    for (const ll x : counts)
        answer -= x * (x-1) / 2;
    cout << answer << endl;
}


Measured memory_bytes: 640
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <fstream>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;

double K, R, L, P, E, T;

struct state {
	int d, m;
	double r, l;
	state(int d, double r, double l, int m) : d(d), r(r), l(l), m(m) {};
};

bool ok(double r, double l) {
	if(T-E <= r && l <= T+E) {
		return true;
	} else {
		return false;
	}
}

bool out(double r, double l) {
	if(l <= T-E || T+E <= r) {
		return true;
	} else {
		return false;
	}
}

bool ok2(double r, double l) {
	double H = (r+l)/2;
	if(abs(T-H) < E) {
		return true;
	} else {
		return false;
	}
}

int main() {
	cin >> K >> R >> L >> P >> E >> T;

	stack<state> S;
	S.push(state(0, R, L, 0));
	double ans = 0;
	while(!S.empty()) {
		state st = S.top();
		S.pop();
		double H = (st.r+st.l)/2;

		if(ok(st.r, st.l)) {
			double p = pow(1-P, st.d-st.m)*pow(P, st.m);
			ans += p;
			continue;
		}

		if(out(st.r, st.l)) {
			continue;
		}

		if(st.d == K) {
			if(ok2(st.r, st.l)) {
				double p = pow(1-P, st.d-st.m)*pow(P, st.m);
				ans += p;
				continue;
			}
			continue;
		}

		if(T <= H) {
			S.push(state(st.d+1, st.r, H, st.m));
			S.push(state(st.d+1, H, st.l, st.m+1));
			continue;
		} else {
			S.push(state(st.d+1, st.r, H, st.m+1));
			S.push(state(st.d+1, H, st.l, st.m));
			continue;
		}
	}

	printf("%.6f\n", ans);
	return 0;
}

Measured memory_bytes: 1252
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define MOD 1000000007
#define INF (1<<30)
#define LINF (lint)(1LL<<56)
#define endl "\n"
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define reprev(i,n) for(int (i)=(n-1);(i)>=0;(i)--)
#define Flag(x) (1<<(x))
#define Flagcount(x) __builtin_popcount(x)
#define pint pair<int,int>
#define pdouble pair<double,double>
#define plint pair<lint,lint>
#define fi first
#define se second
typedef long long lint;
int dx[8]={1,1,0,-1,-1,-1,0,1};
int dy[8]={0,1,1,1,0,-1,-1,-1};

int main(void){
    int N,M;
    cin >> N >> M;
    int s[M],c[M];
    rep(i,M) cin >> s[i] >> c[i];
    if(N==1){
        for(int i=0;i<=9;i++){
            bool check=true;
            rep(j,M) if(c[j]!=i) check=false;
            if(check){
            cout << i << endl;
            return 0;
            }
        }
    }
    if(N==2){
        for(int i=10;i<=99;i++){
            bool check=true;
            int one=i/10,two=i%10;
            rep(j,M){
                if(s[j]==1) if(c[j]!=one) check=false;
                if(s[j]==2) if(c[j]!=two) check=false;
            }
            if(check){
                cout << i << endl;
                return 0;
            }
        }
    }
    if(N==3){
        for(int i=100;i<=999;i++){
            bool check=true;
            int now=i;
            int one=i/100,three=i%10;
            now-=one*100;
            int two=now/10;
            rep(j,M){
                if(s[j]==1) if(c[j]!=one) check=false;
                if(s[j]==2) if(c[j]!=two) check=false;
                if(s[j]==3) if(c[j]!=three) check=false;
            }
            if(check){
                cout << i << endl;
                return 0;
            }
        }
    }
    cout << -1 << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <array>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <sstream>
#include <stdio.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <list>
#include <numeric>
#include <stack>
#include <iomanip>

// using namespace std;

#define Rep(i,a,b) for(int i = a; i < b; i++)
#define rep(i,b) Rep(i,0,b)
#define allof(a) (a).begin(), (a).end()

typedef long long ll;
const int inf = 1e9 + 7;
const ll infll = 1ll << 60ll;
const ll mod = 1e9 + 7;
// 0~3までは右左下上 4~7までは斜め
constexpr int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
constexpr int dy[] = { 0, -1, 0, 1, 1, -1, -1, 1 };

namespace {  // 名前なし名前空間
	// gcd, lcm は C++17 でstdに標準入りしたので言語のバージョンに注意
	// 最大公約数
	ll gcd(ll a, ll b) {
		if (b == 0) return a;
		return gcd(b, a % b);
	}

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

int main() {

	int n;
	std::cin >> n;
	std::vector<int> s(n);
	rep(i, n) std::cin >> s[i];

	int sum = std::accumulate(allof(s), 0);
	// dp[i][j]:= i番目まで採点したときにj点はあり得るか
	std::vector<std::vector<bool>> dp(n + 1, std::vector<bool>(sum + 1, false));
	rep(i, n) dp[i][0] = true;

	rep(i, n) {
		rep(j, sum + 1) {
			if(j + s[i] <= sum)
				dp[i + 1][j + s[i]] = (dp[i + 1][j + s[i]] || dp[i][j]);
			dp[i + 1][j] = (dp[i + 1][j] || dp[i][j]);
		}
	}

	for (int i = sum; i >= 0; i--) {
		if (i % 10 && dp[n][i]) {
			std::cout << i << std::endl;
			return 0;
		}
	}
	std::cout << 0 << std::endl;

	return 0;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
#define pb push_back
#define mp make_pair
#define x first
#define y second
using namespace std;

template<typename T>
void read(T& n){
	char ch; int sign = 1;
	while (!isdigit(ch = getchar())) if (ch == '-') sign = -1;
	n = ch - '0';
	while (isdigit(ch = getchar())) n = n * 10 + ch - '0';
	n *= sign;
}
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9 + 7;
const int N = 211111;
int n;
int p[N], v[N], f[N], c0[N], c1[N];
struct node{
	int c0, c1, x;
	bool operator <(const node rhs) const{
		return (ll)c0 * rhs.c1 < (ll)rhs.c0 * c1;
	}
};
priority_queue<node> heap;

int find(int x){return x == f[x] ? x : f[x] = find(f[x]);}

int main(){
	read(n);
	rep(i, 2, n) read(p[i]);
	rep(i, 1, n) read(v[i]);
	rep(i, 1, n){
		f[i] = i, c0[i] = !v[i], c1[i] = v[i];
		heap.push((node){c0[i], c1[i], i});
	}
	ll ans = 0;
	while(!heap.empty()){
		node h = heap.top(); heap.pop();
		while(!heap.empty() && (c0[h.x] != h.c0 || c1[h.x] != h.c1))
			h = heap.top(), heap.pop();
		if (c0[h.x] != h.c0 || c1[h.x] != h.c1) break;
		if (h.x == 1) continue;
		int pa = find(p[h.x]);
		ans += (ll)c1[pa] * c0[h.x];
		c0[pa] += c0[h.x], c1[pa] += c1[h.x];
		f[h.x] = pa;
		heap.push((node){c0[pa], c1[pa], pa});
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 8436
#include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int w,h;//幅と高さ
int solve(int board[80][80]){
	int d[100][100] ={0};
	d[1][1] = 1;
	pii sp(1,1);
	queue<pii> q;
	q.push(sp);
	while(!q.empty()){
		pii p = q.front();
		q.pop();
		int x = p.first;
		int y = p.second;
		int cnt =d[y][x];
		if(x==w*2-1&&y==h*2-1){
			break;
		}
		for(int i = 0;i < 4;i++){
			int mx = x+dx[i];
			int my = y+dy[i];
			int nx =mx +dx[i];
			int ny = my+dy[i];
			if( nx < 0 || ny < 0 || nx > w*2 || ny > h*2 ) continue;
			if( board[my][mx] == 0 && d[ny][nx] == 0 ){
				pii np( nx , ny );
				q.push( np );
				d[ny][nx] = cnt+1;
			}
		}
	}
	return d[h*2-1][w*2-1];
}
int main(){
	while(cin>>w>>h,w||h){
		int board[80][80]={0};
		for(int x = 0; x<=w*2;x++){
			board[0][x] =board[h*2][x] = 1;
		}
		for(int y = 0; y<=h*2;y++){
			board[y][0] =board[y][w*2] = 1;
		}
		for(int y = 0; y<(h*2-1);y++){
			if(y%2==0){
				for(int x= 0; x<w-1;x++){
					//(2x+2,y+1)
					cin>>board[y+1][2*x+2];
				}
			}else{
				for(int x = 0; x<w;x++){
					//(2x+1,y+1)
					cin>>board[y+1][2*x+1];
				}
			}
		}
		cout <<solve(board)<<endl;
	}
	return 0;
}

Measured memory_bytes: 1280
// OPTIMIZATIONS
//#pragma GCC optimize("O3")
//(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)
//#pragma comment(linker, "/stack:200000000")
//(UNCOMMENT WHEN NEEDED)
//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
// OPTIMIZATIONS
#include "bits/stdc++.h"
typedef long long ll;
typedef unsigned long long uu;
typedef long long int lll;
typedef unsigned long long int uuu;
using namespace std;

#if defined(DEBUG)
#include "prettyprint.hpp"
#define _GLIBCXX_DEBUG
#define dbga(x, n) cerr << "\n" << (#x) << " is " << pretty_print_array(x, n) << endl
#define dbg(x) cerr << "\n" << (#x) << " is " << x << endl
#endif

#if !defined(DEBUG)
#define dbga(x, n)
#define dbg(x)
#endif

#define cel(x, y) 1 + ((x - 1) / y)
const double PI = 3.141592653589793238463;
const int MOD = 1000000007;
const long long int INF = 0x3f3f3f3f3f3f3f3f;

/*
$alil03

URL: url

Solution Begins here
*/

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    // freopen("file.in", "r", stdin);
    // freopen("file.out", "w", stdout);
      bool x;
  cin >> x;
  cout << !x;
}

Measured memory_bytes: 3668
#include <stdio.h>

int main(){
        int N,A,B;
        scanf("%d %d %d", &N, &A, &B);
        int PA=0,PB=0;
        PA = A*N;
        PB = B;
        if(PA>=PB){
                printf("%d\n", PB);
        }
        if(PA<PB){
                printf("%d\n", PA);
        }
        return 0;
}


Measured memory_bytes: 128
using System;

class Program
{
    static void Main(string[] args) {
        int N = int.Parse(Console.ReadLine());
        string[] v = Console.ReadLine().Split(' ');

        int[] A = new int[N];
        for (int i = 0; i < N; i++) {
            A[i] = int.Parse(v[i]);
        }

        int cnt = selectionSort(A);
        for (int i = 0; i < A.Length; i++) {
            if (i > 0) { Console.Write(" "); }
            Console.Write(A[i]);
        }
        Console.WriteLine("");
        Console.WriteLine(cnt);
    }

    static int selectionSort(int[] A) {
        int cnt = 0;

        for (int i = 0; i < A.Length; i++) {
            int minj = i;

            for (int j = i; j < A.Length; j++) {
                if (A[j] < A[minj]) {
                    minj = j;
                }
            }

            if (minj != i) {
                int wk = A[minj];
                A[minj] = A[i];
                A[i] = wk;
                cnt++;
            }
        }

        return cnt;
    }
}



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

using namespace std;

int main(){
  int n,k;
  cin >> n >> k;
  
  vector<int> d(k);
  for(int i=0;i<k;i++)
    cin >> d.at(i);
  
  for(int ans=n;ans<n*10;ans++){
    string s=to_string(ans);
    bool jg=false;
    for(auto x:d){
      char c=x+'0';
      if(s.find(c)!=-1){
        break;
      }
      if(x==d.at(d.size()-1)){
        jg=true;
        break;
      }
    }
    if(jg){
      cout << ans << endl;
      return 0;
    }
  }
}

        


Measured memory_bytes: 256
import java.util.*;


/**
 * Created by Seiya on 2016/10/16.
 */

public class Main {

    static int[] map = new int[104];
    static int max = 0;
    static int N;

    static int[][] memo = new int[30000][104];

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        N = scanner.nextInt();

        for (int i = 0; i < N; i++) {
            map[i] = scanner.nextInt();
        }


        dfs(0,0);

        System.out.println(max);


    } // mainMethod

    static void dfs(int to, int n){
        if(memo[to][n] != 0){
            return;
        }else{
            memo[to][n] = 1;
        }
        if(n == N){
            if(to % 10 == 0)return;
            max = Math.max(max, to);

            return;
        }

        dfs(to + map[n], n + 1);
        dfs(to, n + 1);


    }



} // MainClass


Measured memory_bytes: 34388
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii =  pair<int, int>;
using pll =  pair<long long, long long>;
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007LL;
constexpr long long INF = 1000000009LL;
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
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; }



int main() {
    int n; cin >> n;
    vector<double> X(n), Y(n); rep(i, n) cin >> X[i] >> Y[i];

    vector<vector<double>> dist(n, vector<double>(n));
    rep(i, n){
        rep(j, n){
            dist[i][j] = sqrt(pow(X[i]-X[j], 2) + pow(Y[i]-Y[j], 2));
        }
    }
    
    vector<vector<int>> per;
    vector<int> array; rep(i, n) array.push_back(i);
    do{
        per.push_back(array);
    }while(next_permutation(all(array))); 

    double res = 0;
    for(vector<int> p: per){
        rep(i, p.size()-1){
            int v = p[i], nv = p[i+1];
            res += dist[v][nv];  
        }  
    }

    cout << fixed << setprecision(10);
    cout << res / per.size() << ln;
}


Measured memory_bytes: 3384
#include <bits/stdc++.h>
using namespace std;
int main(){
  random_device rnd;
  mt19937 mt(rnd());
  int D;
  cin >> D;
  vector<int> c(26);
  for (int i = 0; i < 26; i++){
    cin >> c[i];
  }
  vector<vector<int>> s(D, vector<int>(26));
  for (int i = 0; i < D; i++){
    for (int j = 0; j < 26; j++){
      cin >> s[i][j];
    }
  }
  vector<int> last(26, -1);
  for (int i = 0; i < D; i++){
    int t = min(6, D - i);
    vector<vector<int>> L(t, vector<int>(26, 0));
    for (int j = 0; j < t; j++){
      for (int k = 0; k < 26; k++){
        L[j][k] = c[k] * (i + j - last[k]);
      }
    }
    /*
    for (int j = 0; j < t; j++){
      for (int k = 0; k < 26; k++){
        cout << L[j][k] << ' ';
      }
      cout << endl;
    }
    */
    int mx = 0;
    int a = 0;
    vector<int> best(t);
    for (int j = 0; j < 15000; j++){
      vector<vector<int>> L2 = L;
      vector<int> next(t, 0);
      if (j < 7500){
        for (int k = 0; k < t; k++){
          next[k] = mt() % 26;
        }
      } else if (j < 14000){
        next = best;
        for (int k = 0; k < 3; k++){
          int pos = mt() % t;
          int upd = mt() % 26;
          next[pos] = upd;
        }
      } else {
        next = best;
        for (int k = 0; k < 2; k++){
          int pos = mt() % t;
          int upd = mt() % 26;
          next[pos] = upd;
        }
      }
      //calculate diff
      int diff = 0;
      for (int k = 0; k < t; k++){
        diff += s[i + k][next[k]];
        for (int l = k; l < t; l++){
          diff += L2[l][next[k]] - (l - k) * c[next[k]];
          L2[l][next[k]] = (l - k) * c[next[k]];
        }
      }
      if (diff > mx){
        mx = diff;
        best = next;
        /*
        cout << "best: " << endl;
        for (int k = 0; k < t; k++){
          cout << best[k] << ' ';
        }
        cout << endl;
        cout << "mx = " << mx << endl;
        */
      }
    }
    cout << best[0] + 1 << endl;
    last[best[0]] = i;
  }
}

Measured memory_bytes: 3700