akhauriyash/Code-Regression huggingface dataset:
N = int(input())
x = [int(x) for x in input().split()]
dp = [[0 for _ in range(2)] for _ in range(N+1)]
dp[0][0] = 0
dp[0][1] = -10**9
for i in range(N):
  dp[i+1][0] = max(dp[i][0]+x[i],dp[i][1]-x[i])
  dp[i+1][1] = max(dp[i][0]-x[i],dp[i][1]+x[i])
print(dp[N][0])

Measured memory_bytes: 27028
#include <bits/stdc++.h>
using namespace std;
#define int long long   // aaaaaaaaaaaaaaaaaa
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
#define in(s,x) s.find(x) != s.end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define SIZE(buff) (sizeof(buff)/sizeof(buff[0]))
typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<pair<int,int>> VPII;
const ll mod=1000000007;
const ll INF= 1LL<<60-1;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
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; }
template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}
template<typename T, typename U>ostream& operator << (ostream& os, pair<T, U>& pair_var)
{os << pair_var.first << " " << pair_var.second ;return os;}
template<class tContainer>
long long sum(tContainer const& iContainer) {return  std::accumulate(std::begin(iContainer), std::end(iContainer), 0LL);}

// head


int m,n,k,res,tmp,tmp2,tmp3,h,w,x,y;
VI a;
deque<PII> que;
string s;
//VI b;
vector<PII> g[2100600];
int b[2100000];
int c[210] = {};
int rui[210000];
set<int> ch;
VI ab;
VI bb;

signed main() {
	cin.tie(0);
  ios::sync_with_stdio(false);
  cin>>b[0]>>b[1]>>b[2]>>b[3];
  x = b[1]-(b[1]/b[2] + b[1]/b[3]-b[1]/(lcm(b[2],b[3])));
  b[0]-=1;
  y = b[0]-(b[0]/b[2] + b[0]/b[3]-b[0]/(lcm(b[2],b[3])));
  cout << x-y << '\n';
}

Measured memory_bytes: 51200
#include <iostream>
#include <vector>
#include <string>
#include <atcoder/string>

using namespace std;

int main(){
    string S; cin >> S;
    const int N = S.size();
    auto sa = atcoder::suffix_array(S);
    auto lcp = atcoder::lcp_array(S, sa);
    long long res = N - sa[0];
    for(int i=0;i<N-1;i++) res += N - sa[i+1] - lcp[i];
    cout << res << endl;
}

Measured memory_bytes: 15668
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    public static void main(String args[]) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n =Integer.parseInt(br.readLine());
        List<Integer> a = new ArrayList<>();
        List<Integer> b = new ArrayList<>();
        for(int i=0;i<n;i++){
            String[] tokens = br.readLine().split(" ");
            a.add(Integer.parseInt(tokens[0]));
            b.add(Integer.parseInt(tokens[1]));
        }
        int medA = median(a);
        int medB = median(b);
        System.out.println(Math.abs(medB-medA)+1);
    }

    public static int median(List<Integer> list){
        Collections.sort(list);
        if(list.size()%2==1){
            return list.get(list.size()/2);
        }else {
            return list.get(list.size()/2)  + list.get(list.size()/2 -1);
        }
    }



}

Measured memory_bytes: 56364
import math
N = int(input())
A = sorted(list(map(int, input().split())))
if N == 2:
    print(max(A))
    exit()
result = []
num = 0
for a in A[1:]:
    if a % A[0] != 0:
        num += 1
        if num == 2:
            break
else:
    result.append(A[0])
for n in range(2, int(math.sqrt(A[0])) + 1):
    if A[0] % n != 0:
        continue
    num = 0
    x = A[0] // n
    for a in A[1:]:
        if a % x != 0:
            num += 1
            if num == 2:
                break
    else:
        result.append(x)
    num = 0
    for a in A[1:]:
        if a % n != 0:
            num += 1
            if num == 2:
                break
    else:
        result.append(n)
num = 0
B = A[0:1] + A[2:]
for a in B:
    if a % A[1] != 0:
        num += 1
        if num == 2:
            break
else:
    result.append(A[1])
for n in range(2, int(math.sqrt(A[1])) + 1):
    if A[1] % n != 0:
        continue
    num = 0
    x = A[1] // n
    for a in B:
        if a % x != 0:
            num += 1
            if num == 2:
                break
    else:
        result.append(x)
    num = 0
    for a in B:
        if a % n != 0:
            num += 1
            if num == 2:
                break
    else:
        result.append(n)
if len(result) != 0:
    print(max(result))
else:
    print(1)


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

int32_t main()
{
  int n; cin>>n;
  string s,t; cin>>s>>t;
  for(int i = 0 ; i< n; i++) cout<<s[i]<<t[i];
//}
//cout<<ans;
return 0;
}


Measured memory_bytes: 256
N,M = map(int,input().split())
H_li = list(map(int,input().split()))
ans = 0
route = [[] for i in range(N)]
for i in range(M):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    route[a].append(b)
    route[b].append(a)
for i in range(N):
    i_h = H_li[i]
    ikeru = route[i]
    if ikeru == []:
        ans += 1
        continue
    ikeru_h = [H_li[j] for j in ikeru]
    if max(ikeru_h) < i_h:
        ans += 1
print(ans)





Measured memory_bytes: 29940
#include<stdio.h>
int select(int,int);
int n;
int ns[20];
int m;
int ms;
int main(){
  scanf("%d",&n);
  m=n;
  while(m){
    scanf("%d",&ns[n-m]);
    m--;
  }
  scanf("%d",&m);
  while(m){
    scanf("%d",&ms);
     printf(select(0,ms)?"yes\n":"no\n");
    m--;
  }
  return 0;
}
int select(int i,int s){
  if(s==0)return 1;
  if(i>=n)return 0;
  select(i+1,s)||select(i+1,s-ns[i]);
}



Measured memory_bytes: 2044
#include <stdio.h>

int main(void) {

	int a, b, ans;
	char op;
	
	while (scanf("%d %c %d", &a, &op, &b),op != '?'){
		switch (op){
			case '+':
				ans = a+b;
				break;
			
			case '-':
				ans = a-b;
				break;
				
			case '*':
				ans = a*b;
				break;
				
			case '/':
				ans = a/b;
				break;
		}
			
		printf("%d\n", ans);
		
	}
		
	return 0;
}

Measured memory_bytes: 600
#include <iostream>
#pragma endregion //#include
using namespace::std;

signed main(void){
	int A,B;
	cin >> A >> B;
	if( A%3==0 || B%3==0 || (A+B)%3==0){
		cout << "Possible" << endl;
	}else{
		cout << "Impossible" << endl;
	}
}

Measured memory_bytes: 256
n, k = map(int,input().split())
w = [0]*n

for i in range(n):
    w[i] = int(input())

minP = max(max(w), sum(w)//k)
maxP = sum(w)
    
left = minP
right = maxP

while left < right:
    mid = (left + right) // 2
    load = 0
    cnt_track = 1
    flag = 1
    for i in range(n):
        load += w[i]
        if load > mid:
            load = w[i]
            cnt_track += 1

        if cnt_track > k:
                flag = 0
                break
    if flag:
        right = mid
    else:
        left = mid + 1
            
print(left)


Measured memory_bytes: 9416
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = a; i < (int)(b); i++)

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

  int x[n][d];
  rep(i, n) rep(j, d) cin >> x[i][j];

  set<int> s;
  rep(i, 130) s.insert(i*i);

  int cnt = 0;
  rep(i, n-1) repi(j, i+1, n){
    int l = 0;
    rep(k, d){
      l += (x[i][k] - x[j][k]) * (x[i][k] - x[j][k]);
    }
    if(s.count(l)) cnt++;
  }

  cout << cnt << endl;

}


Measured memory_bytes: 256
#include <stdio.h>

int main(void)
{
	int stack[10]={0}, buf, crpos=0;
	while(scanf("%d", &buf) != EOF){
		if(buf == 0){
			printf("%d\n", stack[--crpos]);
		}else{
			stack[crpos++] = buf;
		}
	}
	return 0;
}

Measured memory_bytes: 600
#include <bits/stdc++.h>
#include <boost/range/algorithm.hpp>
#include <boost/range/numeric.hpp>
#include <boost/integer/common_factor.hpp>
#include <boost/integer/common_factor_rt.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>

using std::string;
using std::vector;
using std::set;
using std::multiset;
using std::unordered_set;
using std::map;
using std::multimap;
using std::unordered_map;
using std::pair;
using std::cin;
using std::cout;

using boost::multiprecision::cpp_int;
using cpp_dec_float_1000 = boost::multiprecision::number<boost::multiprecision::cpp_dec_float<1000>>;

typedef uintmax_t    ull;
typedef intmax_t     ll;
typedef uint64_t     ul;
typedef uint32_t     ui;
typedef uint8_t      uc;

constexpr char CRLF  = '\n';
constexpr char SPACE = ' ';
constexpr char VECTOR_COUT_SEPARATOR = SPACE;
constexpr ll INF = 1000'000'007;
constexpr int MOD = 1000'000'007;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> std::ostream& operator<< (std::ostream& os, const std::vector<T>& vc) { for(auto it = vc.begin(); it != vc.end(); ++it) { if (std::next(it) == vc.end()) os << *it; else os << *it << VECTOR_COUT_SEPARATOR; } return os; }
template<class T1, class T2> inline std::ostream & operator<< (std::ostream & os, const std::pair<T1, T2> & p) { return os << p.first << ' ' << p.second; }
template<class T> T modinv(T a, T m) { T b = m, u = 1, v = 0; while (b) { T t = a / b; a -= t * b; boost::swap(a, b); u -= t * v; boost::swap(u, v); } u %= m; if (u < 0) u += m; return u; }

struct Tmp
{
public:
  Tmp(int N);
};

Tmp::Tmp(int N){}

void solve(void)
{
  string l; cin >> l;

  vector<std::array<cpp_int, 2>> dp(l.size());
  dp[0][0] = 2;
  dp[0][1] = 1;
  for (size_t i = 1; i < l.size(); ++i) {
    char c = l[i];
    if (c == '0') {
      dp[i][0] = dp[i-1][0];
      dp[i][1] = dp[i-1][1] * 3;
    }
    else if (c == '1') {
      dp[i][0] = dp[i-1][0] * 2;

      dp[i][1] = dp[i-1][0] + dp[i-1][1] * 3;
    }
    else {
      assert(false);
    }
    dp[i][0] %= MOD;
    dp[i][1] %= MOD;
  }
  cpp_int ans = dp[l.size()-1][0] + dp[l.size()-1][1];
  ans %= MOD;
  cout << ans.str() << CRLF;
  return;
}

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

  solve();

  return 0;
}


Measured memory_bytes: 6736
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <deque>
#include <queue>
#include <array>
#include <set>
#include <map>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cstdint>
#include <cassert>
#include <random>

using namespace std;
using i64 = int64_t;
using i32 = int32_t;
template<class T, class U> void init_n(vector<T>& v, size_t n, U x) 
{ v = vector<T>(n, x); }
template<class T> void init_n(vector<T>& v, size_t n) { init_n(v, n, T()); }
template<class T> void read_n(vector<T>& v, size_t n, size_t o = 0) 
{ v = vector<T>(n+o); for (size_t i=o; i<n+o; ++i) cin >> v[i]; }
template<class T> void read_n(T a[], size_t n, size_t o = 0)
{ for (size_t i=o; i<n+o; ++i) cin >> a[i]; }
template<class T> T gabs(const T& x) { return max(x, -x); }
#define abs gabs

i64 x, y, a[] = {0, 300000, 200000, 100000};
int main() {
  cin >> x >> y;
  i64 ans = 0;
  if (x <= 3) ans += a[x];
  if (y <= 3) ans += a[y];
  if (x == 1 && y == 1) ans += 400000;
  cout <<ans << endl;
  return 0;
}

Measured memory_bytes: 256
def check(h):
    prev = h[0]
    for i, height in enumerate(h):
        if prev < height:
            h[i] -= 1
        elif prev > height:
            return False
        prev = h[i]
    return True

n = int(input())
h = list(map(int, input().split()))
print('Yes' if check(h) else 'No')

Measured memory_bytes: 14396
n, m, l = map(int, input().split())
A = list(list(map(int, input().split())) for _ in range(n))
B = list(list(map(int, input().split())) for _ in range(m))
C = [[0]*l for _ in range(n)]

for i in range(n):
    for j in range(l):
        tmp_sum = 0
        for k in range(m):
            tmp_sum += A[i][k] * B[k][j]
        C[i][j] = tmp_sum

for c in C:
    print(*c)


Measured memory_bytes: 7052
#include<bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int (i)=(a);(i)<(int)(b);(i)++)
#define RREP(i,a,b) for(int (i)=(a);(i)>=(int)(b);(i)--)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;



int main(){
    string s;
    cin >> s;
    int n = s.size();

    REP(i,0,n - 1){
        if (s[i] == s[i + 1]){
            cout << i + 1 << " " << i + 2 << endl;
            return 0;
        }
    }

    REP(i,0,n - 2){
        if (s[i] == s[i + 2]){
            cout << i + 1 << " " << i + 3 << endl;
            return 0;
        }
    }

    cout << -1 << " " << -1 << endl;
    
    return 0;
}

Measured memory_bytes: 512
#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef priority_queue<int> PQ;
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; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
//vector<vector<int> > dp;
//vector<vector<vector<int> > > vvvi;
//dp=vector<vector<int> >(N, vector<int>(M,0));
//vector<pair<int,int> > v;
//v.push_back(make_pair(x,y));
//priority_queue<int,vector<int>, greater<int> > q2;



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

	int N;
	cin >> N;
	VI A(N);
	REP(i, N)cin >> A[i];
	SORT(A);
	bool F = true;
	REP(i, N - 1)if (A[i] == A[i + 1])F = false;

	cout << (F ? "YES" : "NO") << endl;

	return 0;
}



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

int main() {
  int a,b,k;
  cin>>a>>b>>k;
  int d=0,s;
  
    for(int i=min(a,b);i>0;i--){
      if(a%i==0&&b%i==0){
        d++;
        s=i;
      }
      if(d==k){
        break;
      }
    }
  cout<<s<<endl;
}

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

int N;
int h[100010];
int ans=0;

int main()
{
    cin>>N;
    for(int i=1;i<=N;i++) cin>>h[i];
    int cur=0;
    for(int i=2;i<=N;i++)
    {
        if(h[i]<=h[i-1]) cur++;
        else
        {
            ans=max(cur,ans);
            cur=0;
        }
    }
    ans=max(cur,ans);
    cout<<ans;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int n,next;
  cin >> n;
  vector <int> B(n);
  vector <int> F(n);
  
  for (int i = 0;i<n;++i){
   cin >> B.at(i); 
  }
  next = 0;
  for (int j = 0;j<n;++j){
  if (B.at(next) == 2){
   cout << j+1 << endl;
    break;
  }
    else if(F.at(next) == 1){
      cout << -1 << endl;
      break; 
    }
    else {
     F.at(next) = 1;
      next = B.at(next)-1;
      continue;
    }
  }
}

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

int triple_max(int x, int y, int z);

int main(){
    int n;
    cin >> n;
    cout << "ABC" << n << endl;


    return 0;
}

int triple_max(int x, int y, int z){
    int ans = x;
    if (y > x && y > z) ans = y;
    else if (z > x && z > y) ans = z;

    return ans;
}


Measured memory_bytes: 256
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


# aとbの最大公約数
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)

# aとbの最小公倍数
def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


def main():
    N, M = map(int, input().split())

    dp = [0] * (10**5 + 100)
    for _ in range(M):
        L, R = map(int, input().split())
        dp[L] += 1
        dp[R + 1] -= 1

    for i in range(len(dp) - 1):
        dp[i + 1] += dp[i]

    ans = 0
    for i in range(len(dp)):
        ans += dp[i] == M
    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 7784
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<sstream>
#include<cmath>
#include<numeric>
#include<map>
#include<stack>
#include<queue>
using namespace std;
int mod = 1e9+7;

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


int main() {

  int m; cin >> m;
  cout << 24 + 24 - m << endl;

  return 0;
}


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

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

using namespace std;

using ll = long long;

using P = pair<int,int>;
int main() {

int n ;
cin >>n  ;
vector<int> X(n) ;
rep(i,n){
    cin>>X[i] ;
}
sort(X.begin(), X.end())  ;
int p=X[0] ;
int q=X[n-1] ;

ll ans = 1e9 ;
for(int i=p ; i< q+1 ; i++){
    ll sum=0 ;
    rep(j,n){
   
      sum +=  (X[j]-i)*(X[j]-i) ; 
}
  ans=min(ans ,sum) ;


}
cout << ans <<endl ;


  return 0;
}

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

#ifdef LOCAL
#include "../debug.h"
#else
#define debug(x...) 141
#endif
#define forn(i,x,n) for(int i = x; i < n; ++i)
#define forit(it, m) for(auto it = m.begin(); it!=m.end(); ++it)
typedef long long ll;

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

    cin>>n;
    cout<<(n[0]>='a'&&n[0]<='z'?"a":"A")<<'\n';
    
    return 0;
}

Measured memory_bytes: 3648
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; ++i)
typedef long long ll;

int main()
{
	int n;cin >> n;
	vector<string> s(n);
	rep(i,n)cin >> s[i];
	int count = 0;
	int B = 0;
	int A = 0;
	int BA = 0;
	rep(i,n)
	{
		for(int j=1;j<s[i].size();++j)
		{
			if(s[i][j-1]=='A'&&s[i][j]=='B')count++;
		}
		if(s[i][0]=='B'&&s[i][s[i].size()-1]=='A')BA++;
		else if(s[i][s[i].size()-1]=='A')A++;
		else if(s[i][0]=='B')B++;
	}
	int ans=0;
	int a=BA-A,b=BA-B;


	if(BA==0)ans+=(A<B?A:B);
	else if(A+B>0)ans+=BA+min(A,B);
	else if(A+B==0)ans+=BA-1;

	if(ans==n)ans--;
	ans+=count;
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 768
#include <stdio.h>
int main()
{
    int x, i;
    i = 1;
    x = 0;
    
    while (i > 0) {
        scanf ("%d", &x);
        if (x == 0 || x > 10000) {
            break;
        }
        printf ("Case %d: %d\n", i, x);
        i++;
    }
    
    return 0;
}

Measured memory_bytes: 596
X,K,D = map(int,input().split())
X = abs(X)
if X > K*D:
    print(X-K*D)
else:
    if X%D != 0:
        ans1 = X%D
        ans2 = (-X)%D
    else:
        ans1 = X%D
        ans2 = D
    if ((X-ans1) //D)&1 == K&1:
        print(ans1)
    else:
        print(ans2)

Measured memory_bytes: 61884
#include <stdio.h>

int main(void){

    int cou = 0;
    long n, k, r = 1;

    scanf("%ld %ld", &n,&k);

    while(1){

        r *=k;
        
        if(r <= n){
            cou++;
        }else{
            printf("%d\n", cou+1);
            break;
        }
    }
    return 0;
}

Measured memory_bytes: 128
N=int(input())

import itertools
l=[]
for i in range(1,N+1):
    l.append(i)
L=list(itertools.permutations(l,N)) #Lの中から2個
L.sort()
#print(L)
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
a=10**9
b=10**9
for i in range(len(L)):
    if L[i]==P:
        a=i
    if L[i]==Q:
        b=i
print(abs(a-b))

Measured memory_bytes: 79552
#include"bits/stdc++.h"
using namespace std;

int main() {
	int N, K;
	cin >> N >> K;
	vector<int> x(N);
	int ans = 0;
	for (int i = 0;i < N;++i) {
		cin >> x[i];
		ans += min(x[i], K - x[i]) * 2;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <tuple>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <numeric>
#include <string.h>
#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define repn(i, x) for (int i = 1; i <= (int)(x); i++)
#define repr(i, x) for (int i = ((int)(x) - 1); i >= 0; i--)
#define reprn(i, x) for (int i = ((int)(x)); i > 0; i--)
#define all(x) (x).begin(),(x).end()
typedef long long int64;
using namespace std;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1<<29;
const long long INFL = 1e18;
const int MOD = 1000000007;
const int MAX = 210000;

int gcd(int a, int b) {
    return b != 0 ? gcd(b, a % b) : a;
}

//逆元を求める関数
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; 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;
    }
}
//組み合わせ
long long COM (int n, int k) {
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n-k] % MOD) % MOD;
}

void solve() {
		int n; cin >> n;
		vector<int> b(n - 1);
		rep(i, b.size()) {
			cin >> b[i];		
		}
		vector<int> a(n);
		//a数列の初項と末項にはb数列の初項と末項を格納する
		a[0] = b[0];
		a[n - 1] = b[n - 2];
		//初項と末項以外はb数列のi番目とi+1番目の最小値を格納すれば良い
		for (int i = 0; i < b.size() - 1; ++i) {
			a[i + 1] = min(b[i], b[i + 1]);
		}
		auto sum = [&](vector<int> b) {
			int ret = 0;
			rep (i, a.size()) {
				ret += a[i];	
			}
			return ret;
		};
		cout << sum(a) << endl;
    return;
}

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



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

int main(){
    long long N,D;
    cin >> N >> D;
    long long X,Y;
    long long ans=0;
    for(long long i=0; i<N; i++){
        cin >> X >> Y;
        if(X*X+Y*Y<=D*D){
            ans++;
        }
    }
    cout << ans << endl;
    system("pause");
    return 0;
}

Measured memory_bytes: 3684
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
typedef long long ll;

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

    int flag=0;    //0:なし, 1:減少, 2:増加
    ll preNum, cnt=1;
    cin >> preNum;

    for(int i=1; i<n; i++){
        ll Num;
        cin >> Num;
        if(Num == preNum) continue;
        if(flag==0){
            if(preNum < Num){
                flag = 2;
            }else{
                flag = 1;
            }
        }else if(flag == 1){
            if(preNum < Num){
                flag = 0;
                cnt++;
            }
        }else{
            if(preNum > Num){
                flag = 0;
                cnt++;
            }
        }
        preNum = Num;
    }

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

Measured memory_bytes: 256
from collections import deque
n = int(input())
a = sorted(int(input()) for _ in range(n))
d = deque(a)
l = deque([])
bit = 0
if len(d)%2==0:
    while d:
        dmi = d.popleft()
        dma = d.pop()
        if bit==1:
            l.appendleft(dmi)
            l.append(dma)
            bit = 0
        else:
            l.append(dmi)
            l.appendleft(dma)
            bit = 1
else:
    while len(d)!=1:
        dmi = d.popleft()
        dma = d.pop()
        if bit==1:
            l.appendleft(dmi)
            l.append(dma)
            bit = 0
        else:
            l.append(dmi)
            l.appendleft(dma)
            bit = 1
    last = d.pop()
    if abs(l[0]-last)<abs(l[-1]-last):
        l.append(last)
    else:
        l.appendleft(last)
ans = 0
for i in range(len(l)-1):
  ans += abs(l[i+1] - l[i])
  
print(ans)

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

static const int INF=1e9;
static const int MOD=1000000007;
typedef long long ll;
typedef pair<int,int> P;

int main(){
    int N; cin >> N;
    int a[N];
    int sum=0;
    for(int i=0;i<N;i++){
        cin >> a[i];
        sum=sum^a[i];
    }
    for(int i=0;i<N;i++){
        int ans=sum^a[i];
        cout << ans << " ";
    }
}


Measured memory_bytes: 4376
#include <iostream>
#include <string>

using namespace std;

void solve()
{
	string str;
	while(cin >> str)
	{
		int n = str.size();
		for(int i = 0; i < n; ++i)
		{
			if(str[i] == '@')
			{
				int length = str[i + 1] - '0';
				for(int j = 0; j < length; ++j)
				{
					cout << str[i + 2];
				}
				i += 2;
			}
			else
			{
				cout << str[i];
			}
		}
		cout << endl;
	}
}

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

Measured memory_bytes: 0
#include <stdio.h>
#define N 8
#define Free -1
#define nFree 1
void recursive(int);
void printBoard();


int row[N], col[N], dpos[2*N-1], dneg[2*N-1];
int b[N][N], k;
int main(){
        int i, j, r, c;

        for(i=0; i<N; i++) {
                row[i]=Free, col[i]=Free;
        }
        for(j=0; j<2*N-1; j++) {
                dpos[j]=Free, dneg[j]=Free;
        }
        scanf("%d", &k);

        for(i=0; i<k; i++) {
                scanf("%d%d", &r, &c);
                b[r][c]=nFree;
        }

        recursive(0);

        return 0;
}

void recursive(int i){
        int j;
        if(i==N) {
                printBoard();
                return;
        }

        for(j=0; j<N; j++) {
                if(nFree==col[j]||nFree==dpos[i+j]||nFree==dneg[i-j+N-1])
                        continue;
                row[i]=j;
                col[j]=dpos[i+j]=dneg[i-j+N-1]=nFree;
                recursive(i+1);
                row[i]=col[j]=dpos[i+j]=dneg[i-j+N-1]=Free;
        }
}

void printBoard(){
int i, j;

  for(i=0; i<N; i++){
    for(j=0; j<N; j++){
      if(b[i][j]==nFree){
        if(row[i]!=j)return;
      }
    }
  }
for(i=0; i<N; i++){
  for(j=0; j<N; j++){
if(row[i]==j) printf("Q");
else printf(".");
  }
  printf("\n");
}
}

Measured memory_bytes: 572
#include<iostream>
#include<algorithm>
using namespace std;
int N,t,x,y;
main()
{
  cin>>N;
  for(int i=0;i<N;i++)
  {
    int tt,tx,ty;cin>>tt>>tx>>ty;
    if((tt-t)%2!=abs(x-tx+y-ty)%2||tt-t<abs(tx-x)+abs(ty-y))
    {
      cout<<"No"<<endl;
      return 0;
    }
    t=tt,x=tx,y=ty;
  }
  cout<<"Yes"<<endl;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
int main () {
	int a,b,c,d,e,f,g,h,kane;
	char ai[100];
	kane=100000.0;

	scanf("%d",&a);

	while(a--){
		kane=kane*1.05;
		kane=kane+999;
		kane=kane/1000;
		kane=kane*1000;

	}
	printf("%d\n",kane);





	//scanf("%d",&a);
	return 0;}

Measured memory_bytes: 592
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional> 
#include <iomanip>
#include <unordered_map>
#include <memory.h>
#include <cstring>
#include <fstream>

using namespace std;

pair <int, int> key[1001];
int n, m;
int res = 1e9;
int dp[1001][5000];

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	cin >> n >> m;

	for (int i = 0; i < m; i++)
	{
		int a, b, c;
		int val = 0;
		cin >> a >> b;
		for (int j = 0; j < b; j++)
		{
			cin >> c;
			c--;
			val = (val | (1 << c));
		}
		key[i] = make_pair(a, val);
	}

	for (int i = 0; i <= 1000; i++)
	{
		fill(dp[i], dp[i] + 5000, 1e9);
	}

	dp[0][0] = 0;

	for (int i = 1; i <= m; i++)
	{
		for (int j = 0; j <= (1 << n) - 1; j++)
		{
			dp[i][j] = min(dp[i][j], dp[i - 1][j]);
			if ((j | key[i-1].second) <= (1 << n) - 1)
			{
				dp[i][(j | key[i-1].second)] = min(dp[i][(j | key[i-1].second)], dp[i - 1][j] + key[i-1].first);
			}
		}
	}

	if (dp[m][(1 << n) - 1] == 1e9)
	{
		cout << -1 << '\n';
	}
	else
	{
		cout << dp[m][(1 << n) - 1] << '\n';
	}

	return 0;
}


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

int main() {
	int n;
	cin>>n;
	for(int i=1;i<10;i++){
	    if(n%i==0){
	        if(n/i<10){
	            cout<<"Yes"<<endl;
	            return 0;
	        }
	    }
	}
	cout<<"No"<<endl;
	return 0;
}


Measured memory_bytes: 3552
//#include <bits/stdc++.h>
#include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <climits>
#include <iomanip>

#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define ROF(i, j, k) for(int i = (int)(j); i >= (int)(k); --i)
#define FORLL(i, n, m) for(long long i = n; i < (long long)(m); i++)
#define SORT(v, n) sort(v, v+n)
#define REVERSE(v) reverse((v).begin(), (v).end())

using namespace std;
using ll = long long;
const ll MOD=1000000007LL;
typedef pair<int, int> P;

ll ADD(ll x, ll y) { return (x+y) % MOD; }
ll SUB(ll x, ll y) { return (x-y+MOD) % MOD; }
ll MUL(ll x, ll y) { return x*y % MOD; }
ll POW(ll x, ll e) { ll v=1; for(; e; x=MUL(x,x), e>>=1) if (e&1) v = MUL(v,x); return v; }
ll DIV(ll x, ll y) { /*assert(y%MOD!=0);*/ return MUL(x, POW(y, MOD-2)); }

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(a>b){a=b;return 1;}return 0;}

const int MAXN = 200001;

int x[MAXN], v[MAXN];

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

  int n;
  cin >> n;
  REP(i, n) {
    cin >> x[i];
    v[i] = x[i];
  }

  sort(v, v+n);

  int mid1 = v[n/2-1], mid2 = v[n/2];

  //cout << mid1 << " "  << mid2 << endl;

  REP(i, n){
    if(x[i] <= mid1){
      cout << mid2 << endl;
    }
    else if(x[i] >= mid2){
      cout << mid1 << endl;
    }
  }
  return 0;
}


Measured memory_bytes: 3712
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	static int head = 0;
	static int tail = 0;
	static Process[] queue = new Process[100005];

	public static void main(String[] args) {
		try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
			String nq[] = br.readLine().split(" ");
			int n = Integer.parseInt(nq[0]);
			int q = Integer.parseInt(nq[1]);
			int total = 0;
			for (int i = 0; i < n; i++) {
				String[] tmp = br.readLine().split(" ");
				String name = tmp[0];
				int time = Integer.parseInt(tmp[1]);
				Process p = new Process(name, time);
				enqueue(p);
			}
			while (head != tail) {
				Process cur = dequeue();
				if (cur.time > q) {
					cur.time -= q;
					total += q;
					enqueue(cur);
				} else {
					total += cur.time;
					System.out.println(cur.name + " " + total);
				}

			}

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	static void enqueue(Process p) {
		if ((tail + 1) % queue.length == head) {
			System.err.println("キューが満タンです");
			return;
		}
		queue[tail] = p;
		tail = (tail + 1) % queue.length;
	}

	static Process dequeue() {
		Process ret = null;
		if (head == tail) {
			System.err.println("キューが空です");
		} else {
			ret = queue[head];
			head = (head + 1) % queue.length;
		}

		return ret;
	}

}

class Process {
	String name;
	int time;

	public Process(String name, int time) {
		this.name = name;
		this.time = time;
	}
}


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

const int M = 1001001;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i,n) cin >> a[i];
  vector<int> cnt(M);
  for(int x : a) {
    if(cnt[x] != 0) {
      cnt[x] = 2;
      continue;
    }
    for(int i = x; i < M; i += x) cnt[i]++;
  }
  int ans = 0;
  for(int x : a) {
    if(cnt[x] == 1) ans++;
  }
  cout << ans << endl;
  return 0;
}

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

using  ll = long long;
using ull = unsigned long long;
using namespace std;
#define pb push_back
int dy[]={0, 0, 1, -1, 1, 1, -1, -1};
int dx[]={1, -1, 0, 0, 1, -1, -1, 1};
constexpr ll kINF = 1152921504606846976;//2^60
constexpr double kEPS = 1e-6;

#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 rep(i,n) REP(i,n)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep(i,n) RREP(i,n)
#define mp make_pair
#define fi first
#define sc second

ll N,W;
ll w[110],v[110],dp[110][100010];

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

int main(void) {
  cin >> N >> W;

  rep(i, N) cin >> w[i] >> v[i];

  rep(i,N) {
    rep(j, W + 1) {
      chmax(dp[i + 1][j], dp[i][j]);
      if (j + w[i] <= W)
        chmax(dp[i + 1][j + w[i]], dp[i][j] + v[i]);
    }
  }

  cout << dp[N][W] << endl;
}

Measured memory_bytes: 79360
#include <bits/stdc++.h>
#include <boost/multi_array.hpp>
#include <boost/optional.hpp>
#include <boost/range/irange.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/algorithm/string/classification.hpp> // is_any_of
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/math/tools/minima.hpp>

const int MOD = 1e9+7;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = (n); i >= 0; --i)

namespace adaptor = boost::adaptors;
using namespace std;
using boost::math::tools::brent_find_minima;
using boost::irange;
using boost::algorithm::join;
using boost::algorithm::split;
using boost::algorithm::replace_all;
using boost::multiprecision::cpp_int;
using ll = long long;
using P = pair<int, int>;

// TOP
void	Main()
{
	bool	flag;
	flag = true;
	vector<vector<int>>	V(3, vector<int>(3));
	rep(i, 3)
	{
		rep(j, 3)
			cin >> V.at(i).at(j);
	}
	rep(i, 2)
	{
			int x, y, z;
			x = V.at(i + 1).at(0) - V.at(i).at(0);
			y = V.at(i + 1).at(1) - V.at(i).at(1);
			z = V.at(i + 1).at(2) - V.at(i).at(2);
			if (x != y || y != z)
			{
				flag = false;
				break ;
			}
	}
	rep(j, 2)
	{
			int x, y, z;
			x = V.at(0).at(j + 1) - V.at(0).at(j);
			y = V.at(1).at(j + 1) - V.at(1).at(j);
			z = V.at(2).at(j + 1) - V.at(2).at(j);
			if (x != y || y != z)
			{
				flag = false;
				break ;
			}
	}
	if (V.at(0).at(0) + V.at(2).at(2) != V.at(2).at(0) + V.at(0).at(2))
		flag = false;
	if (flag)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
}
// END

int		main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	Main();
}


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

#define int long long

int gcd(int m, int n){
    int tmp;
    if(m < n){
        tmp = n;
        n = m;
        m = tmp;
    }

    while(1){
        if(n == 0) return m;
        tmp = m % n;
        m = n;
        n = tmp;
    }
}

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

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

    int gcd1[100010];

    gcd1[0] = a[0];
    for(int i = 1;i < n;i++){
        gcd1[i] = gcd(a[i], gcd1[i-1]);
    }

    int gcd2[100010];
    gcd2[n-1] = a[n-1];
    for(int i = n-2;i >= 0;i--){
        gcd2[i] = gcd(gcd2[i+1], a[i]);
    }

    int ans = 0;
    for(int i = 1;i < n-1;i++){
        ans = max(ans, gcd(gcd1[i-1], gcd2[i+1]));
    }

    cout << max(ans, max(gcd1[n-2], gcd2[1])) << endl;

    return 0;
}


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

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

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

const double EPS = 1e-9;
const double PI  = acos(-1.0);
const int INT_INF = 2147483647;
const long long LL_INF = 1LL<<60;
const long long MOD = 1000000007;

#define CLR(a) memset((a), 0 ,sizeof(a))

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

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

int main(void){
    string S; cin >> S;
    int Q; cin >> Q;
    string top = "", back = "";
    int T = 0;
    REP(i,Q){
        int t; cin >> t;
        if(t == 1){
            T++;
        }
        else{
            int f; char c; cin >> f >> c;
            if(T%2 == 0){
                if(f == 1){
                    top = c + top;
                }
                else{
                    back += c;
                }
            }
            else{
                if(f == 1){
                    back += c;
                }
                else{
                    top = c + top;
                }
            }
        }
    }
    string ans = top + S + back;
    if(T%2 == 1) REVERSE(ans);
    cout << ans << endl;
    return 0;
}

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

#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()

using namespace std;
using ll = long long;

const double PI = 3.14159265358979;

void solve()
{
  ll n;
  cin >> n;
  int ans= 0;
  for(int c = 1; c < n; ++c)
  {
    int num = n - c;
    for(int i = 1; i * i <= num; ++i)
    {
      if (num % i == 0) ans += 2;
      if (i * i == num) ans--;
    }
  }
  cout << ans;
}

int main()
{
  fastio;
  solve();

  return 0;
}

Measured memory_bytes: 3644
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<int,int>;
/* ちゃんと考えてわかって実装 */

const int INF = 10000000;
vector<string> s;
int h, w;
int di[4] = {1, -1, 0, 0};
int dj[4] = {0, 0, 1, -1};
int d[55][55];

int bfs(){
    h--; w--;
    queue<P> que;
    for(int i=0; i<=h; i++){
        for(int j=0; j<=w; j++){
            d[i][j] = INF;
        }
    }
    que.push(P(0, 0));
    d[0][0] = 0;
    while(que.size()){
        P now = que.front();
        que.pop();
        // goalに到達したら終わり
        // もう１つの終了条件はqueがからになること
        if(now.first == h && now.second == w){
            break;
        }
        for(int k=0; k<4; k++){
            int ni = now.first + di[k];
            int nj = now.second + dj[k];
            // それが到達可能な状態ならqueに追加する
            // d[ni][nj] != INF だと訪問済みの状態(すでに最短経路確定済み)
            if(0 <= ni && ni <= h && 0 <= nj && nj <= w){
                if(d[ni][nj] == INF && s[ni][nj] == '.'){
                    que.push(P(ni, nj));
                    d[ni][nj] = d[now.first][now.second] + 1;
                }
            }
        }
    }
    return d[h][w];
}

int main(void){
    
    cin >> h >> w;
    for(int i=0; i<h; i++){
        string tmp_s;
        cin >> tmp_s;
        s.push_back(tmp_s);
    }

    // whiteのcount
    int white = 0;
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            if(s[i][j] == '.') white++;
        }
    }

    // bfs
    int dist = bfs();
    if(dist == INF) cout << -1 << endl;
    else cout << white - dist - 1 << endl;
    return 0;
}

Measured memory_bytes: 3648
/*author* Priyanshu Shrivastav (from IIT Palakkad) *
 * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
 * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
 * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
 * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
When I wrote this, only God and I understood what I was doing
 ** * * * * * * * Now, only God knows * * * * * * */
#include         <bits/stdc++.h>
using namespace std;
#pragma GCC      optimize ("Ofast")
#pragma GCC      optimize ("unroll-loops")
#pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
#define PREC     cout.precision (10); cout << fixed
#define bg(x)    " [ " << #x << " : " << (x) << " ] "
#define x        first
#define y        second
using ll = long long;
using ff = long double;
using pii = pair<int,int>;

#define debug(args...) { \
/* WARNING : do NOT compile this debug func calls with following flags: // \
 * // -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2*/ \
   string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
   stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); \
}
void err(istream_iterator<string> it) { it->empty();
   cerr << " (Line : " << __LINE__ << ")" << '\n';
}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << fixed << setprecision(15) << " [ " <<  *it << " : " << a  << " ] "<< ' ';
	err(++it, args...);
}

const int N = 22, MOD = (int)1e9 + 7;

int dp[N][1 << N];
int ar[N][N];
int n;

inline int add (int a, int b) { return ((a%MOD) + (b%MOD)) % MOD; };

void solve() {
   for (int i = 1; i <= n; ++i) {
      for (int j = 0; j < (1 << n); ++j) {
         dp[i][j] = 0;
      }
   }

   for (int k = 0; k < n; ++k) {
      if (ar[1][k+1]) dp[1][1 << k] = 1;
   }

   for (int i = 2; i <= n; ++i) {
      for (int j = 0; j < (1 << n); ++j) {
         if (__builtin_popcount(j) != i) continue;
         for (int k = 0; k < n; ++k) {
            if (ar[i][k+1]) dp[i][j] = add(dp[i][j], dp[i-1][j - (1 << k)]);
         }
      }
   }
   int total = 0;
   for (int j = 0; j < (1 << n); ++j) {
      if (__builtin_popcount(j) != n) continue;
      total = add(total, dp[n][j]);
   }
   cout << total << '\n';
}

signed main() {
   IOS; PREC;
   cin >> n;
   for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j)
         cin >> ar[i][j];
   solve();

   return EXIT_SUCCESS;
}


Measured memory_bytes: 215296
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
#define ll long long int
#define MOD 1000000007
#define oo 1000000000000000000
#define forr(i,n) for(ll i=0;i<n;i++)
#define fastio ios_base::sync_with_stdio(false); cin.tie(0) ; cout.tie(0);
#define all(x) x.begin(),x.end()
#define eb emplace_back
#define pb push_back
#define vect(x) vector<x>
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
 
using namespace __gnu_pbds; 
using namespace std;

ll valueOfIndex(ordered_set&s , ll i){ return *(s.find_by_order(i)); }
ll indexOfValue(ordered_set&s , ll x){ return s.order_of_key(x); }

ll add(ll a, ll b,ll p=MOD) { a%=p; b%=p; return (a+b + p)%p;}
ll mul(ll a, ll b,ll p=MOD) { a%=p; b%=p; return (a*b + p)%p;}

ll power(ll x,ll n,ll p=MOD){ if(x==0) return 0; if(n==0 || x==1) return 1LL;
    ll r = (power(x,n/2,p))%p; if(n&1) return mul(mul(r,r,p) , x,p); else return mul(r,r,p);
}

ll a[410];
ll n;

ll dp[410][410];

ll sum(ll i,ll j)
{
    ll ans=0;
    for(ll k=i;k<=j;k++) ans+=a[k];
    return ans;
}

ll get(ll s, ll e)
{
    if(s+1>e) return 0;
    if(s+1==e) return a[s]+a[e];
    
    if(dp[s][e]!=-1) return dp[s][e];
    
    ll ans = oo;
    for(ll j=s;j<e;j++)
    {
        ans = min( ans , sum(s,e) + get( s , j ) + get(j+1,e));
    }
    return dp[s][e] = ans;
}

void solve()
{
    cin >> n;
    forr(i,n) cin >> a[i];
    memset(dp,-1,sizeof(dp));
    cout << get( 0 , n-1 );
    
    
    
    return;
}


int main()
{
    
    fastio
    
    ll tc=1;
  //  read(tc);
    while(tc--)
    {
        solve();
    }
    
    return 0;
}

Measured memory_bytes: 1664
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE BinaryLiterals #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE NumDecimals #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}

import Control.Applicative
import Control.Arrow
import Control.Monad
import Control.Monad.ST

import Data.Char
import Data.List
import Data.Maybe

import qualified Data.ByteString.Char8 as B

import Data.Array.ST.Safe
import Data.STRef

import Debug.Trace
import Text.Printf

import Data.Int

readInt = readLn :: IO Int
readInteger = readLn :: IO Integer
readInts = map ( fst . fromJust . B.readInt ) . B.words <$> B.getLine
readIntegers = map ( fst . fromJust . B.readInteger ) . B.words <$> B.getLine

which a b f = if f then a else b
mp [ a, b ] = ( a, b )

modifyArray a i f = writeArray a i =<< f <$> readArray a i

main = do
	getLine
	bs <- map fromIntegral <$> readInts
	print $ sum $ solve bs

solve bs = runST $ do
	let
		n = succ $ length bs
	as <- newArray ( 0, n - 1 ) 1000000 :: ST s ( STUArray s Int Int64 )
	forM_ [ 0 .. n - 2 ] $ \i -> do
		modifyArray as i ( min $ bs !! i )
		modifyArray as ( i + 1 ) ( min $ bs !! i )
	getElems as

Measured memory_bytes: 764
import java.util.*;

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

      char[] a = A.toCharArray();
      char[] b = B.toCharArray();
      char[] c = C.toCharArray();
     
  
      if(a[a.length-1] != b[0]){
          System.out.println("NO");
      }else if(b[b.length-1] != c[0]){
          System.out.println("NO");
      }else{
          System.out.println("YES");
      }
    }
}

Measured memory_bytes: 35620
main :: IO ()
main = do
    a <- readLn
    b <- readLn
    c <- readLn
    x <- readLn :: IO Int
    let combinations = [i * 500 + j * 100 + k * 50 | i <- [0..a], j <- [0..b], k <- [0..c]]
    print . length $ filter (== x) combinations

Measured memory_bytes: 508
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
const Int MOD = 998244353;
array<Int, 1000010> P2;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    P2[0] = 1;
    for (int i = 1; i < P2.size(); i++) P2[i] = P2[i - 1] * 2 % MOD;
    int N; cin >> N;
    string X; cin >> X;
    string Y; Y.reserve(N);
    vector<int> A(N + 1);
    Int ans = 0;
    for (int n = 1; n <= N; n++) if (N % n == 0 && N % (2 * n) != 0) {
        Int cnt = 0;
        for (int i = 0; i < n; i++) {
            cnt = (cnt + P2[n - 1 - i] * (X[i] - '0')) % MOD;
        }
        Y.resize(2 * n);
        for (int i = 0; i < n; i++) Y[i] = X[i];
        for (int i = n; i < Y.size(); i++) Y[i] = '0' ^ '1' ^ X[i % n];
        bool ok = true;
        for (int i = 0; i < N; i++) {
            if (Y[i % Y.size()] == X[i]) continue;
            ok = Y[i % Y.size()] < X[i];
            break;
        }
        if (ok) cnt = (cnt + 1) % MOD;
        cnt = (cnt + MOD - A[n]) % MOD;
        for (int i = n + n; i < A.size(); i += n) {
            A[i] = (A[i] + cnt) % MOD;
        }
        ans = (ans + cnt * 2 * n) % MOD;
    }
    cout << ans << endl;
    return 0;
}


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

int	main() {
	long K, A, B;
	cin >>K >>A >>B;
	long S=0;
	if (B-A<=2) {
		S=K+1;
	} else if (K<=A-1) {
		S=K+1;
	} else {
		K -=A-1;
		if (0==K%2) {
			K/=2;
			S=A+(B-A)*K;
		} else {
			K/=2;
			S=A+(B-A)*K+1;
		}
	}
	cout <<S <<endl;
	return 0;
};


Measured memory_bytes: 256