akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long int;

int main(){
    int n, q; cin >> n >> q;
    string s; cin >> s;
    vector<vector<char>> td(q);
    for(auto& v:td){
        char t, d; cin >> t >> d;
        v = vector<char>{t, d};
    }
    // Left Survive
    int l = -1, r = n;
    while(r - l > 1){
        int middle = (l + r) / 2;
        int tm = middle;
        int ok = true;
        for(int i=0; i<q; i++){
            if(tm >= n){break;}
            if(td[i][0] == s[tm]){
                if(td[i][1] == 'L') tm--;
                else tm++;
            }
            if(tm < 0){
                ok = false;
                break;
            }
        }
        if(ok){
            r = middle;
        }
        else{
            l = middle;
        }
    }
    int left_survive = r;
    // Right Survive
    l = -1, r = n;
    while(r - l > 1){
        int middle = (l + r) / 2;
        int tm = middle;
        int ok = true;
        for(int i=0; i<q; i++){
            if(tm < 0){break;}
            if(td[i][0] == s[tm]){
                if(td[i][1] == 'L') tm--;
                else tm++;
            }
            if(tm >= n){
                ok = false;
                break;
            }
        }
        if(ok){
            l = middle;
        }
        else{
            r = middle;
        }
    }
    int right_survive = l;
    cout << right_survive - left_survive + 1 << endl;
    return 0;
}

Measured memory_bytes: 11396
#include<bits/stdc++.h>
using namespace std;
#define ll long long
signed main(){
	ll n, i, sum=0;
	cin>>n;
	ll b[n-1];
	for(i=1; i<=n-1; i++){
		cin>>b[i];
	}
	ll a[10000];
	for(i=1; i<=n-1; i++){
		a[i+1]=min(b[i], b[i+1]);
		//cout<<a[i+1]<<" ";
	}
	a[1]=b[1]; a[n]=b[n-1];
	for(i=1; i<=n; i++){
		//cout<<a[i]<<" ";
		sum += a[i];
	}
	cout<<sum;
}

Measured memory_bytes: 256
#include <algorithm>
#include <climits>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define int long long int
constexpr int INF = std::numeric_limits<int>::max();
constexpr int NINF = std::numeric_limits<int>::min();
using namespace std;

void solve() {
  int L;
  cin >> L;
  L--;
  vector<int> v;
  vector<tuple<int, int, int>> m;

  for (int i = 0; (1 << i) - 1 <= L; i++) {
    v.push_back((1 << i) - 1);
  }

  for (int i = 1; i < v.size(); i++) {
    m.emplace_back(i, i + 1, 1 << (i - 1));
    m.emplace_back(i, i + 1, 0);
  }

  int x = 1 << (v.size() - 1);
  while (x <= L) {
    int i = 1;
    while (v[i] + x <= L) {
      i++;
    }

    m.emplace_back(i, v.size(), x);
    x = x + v[i - 1] + 1;
  }

  printf("%ld %ld\n", v.size(), m.size());
  for (auto &a : m) {
    printf("%lld %lld %lld\n", get<0>(a), get<1>(a), get<2>(a));
  }
}

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


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <iomanip>
#include <deque>
#include <cassert>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <chrono>
#include <ctime>
#include <random>
#include <stack>
#include <unordered_set>
#include <unordered_map>
#include <iterator>
#include <climits>
using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef long double ld;
#define INF 2001001001
#define MOD 1000000007

string S;
ll K;

void check(){
  char c=S[0];
  for (char c2:S)
    if (c2!=c)
      return;
  cout<<((ll)S.length()*(ll)K)/2<<endl;
  exit(0);
}

int main()
{
  //ios_base::sync_with_stdio(0);cin.tie(0);
  //freopen (".in","r",stdin);
  //freopen (".out","w",stdout);
  cin>>S>>K;
  check();
  ll ans=0;
  ll cur=0;
  vector<ll>segs;
  for (int i=0;i<(int)S.length();i++){
    cur++;
    if (S[i]!=S[i+1] || i+1==((int)S.length())){
      segs.push_back(cur);
      ans+=cur/2;
      cur=0;
    }
  }
  if (S[0]!=S.back()){
    ans*=K;
    cout<<ans<<endl;
    return 0;
  }
  ans=0;
  for (int i=1;i+1<(int)segs.size();i++)
    ans+=segs[i]/2;
  ans*=K;
  ans+=(segs[0]/2)+(segs.back()/2)+(segs[0]+segs.back())/2*(K-1);
  cout<<ans<<endl;
  return 0;
}


Measured memory_bytes: 256
n = int(input())
dp = [10**9]*(n+1)
L = [6**i for i in range(1,7)]
L += [9**i for i in range(1,6)]

dp[0] = 0

for i in range(1,n+1):
    dp[i] = dp[i-1]+1
    k = 6
    while i-k >=0:
        dp[i] = min(dp[i],dp[i-k]+1)
        k *=6
    k = 9
    while i-k >=0:
        dp[i] = min(dp[i],dp[i-k]+1)
        k *=9
    #print(dp)
print(dp[n])




Measured memory_bytes: 3828
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using stkll = vector<pll>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
#define rep(i, n) for (ll i = 0; i < (n); i++)
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
#ifndef ONLINE_JUDGE
    #define debug(x) cerr << #x << ": " << x << endl;
#else
    #define debug(x)
#endif

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

    ll k;
    string s;
    cin >> k >> s;
    if(s.size() <= k) cout << s << endl;
    else cout << s.substr(0, k) << "..." << endl;
}


Measured memory_bytes: 3656
#include <iostream>
#include <cstdint>
#include <vector>

using namespace std;

int main() {
  int64_t N, x;
  cin >> N >> x;
  vector<int64_t> as(N);
  int64_t cnt = 0;

  for (size_t i = 0; i < N; ++i) {
    int64_t n;
    cin >> n;
    if (n > x) {
      cnt += n - x;
      n = x;
    }
    as[i] = n;
  }
  for (size_t i = 1; i < N; ++i) {
    int64_t sum = as[i - 1] + as[i];
    if (sum > x) {
      cnt += sum - x;
      as[i] = as[i] - (sum - x);
    }
  }
  
  cout << cnt << endl;
  
  return 0;
}


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

using namespace std;

#define MAX_N 41

const double Pi = acos(-1);

double getAngle(double p, double q) {
  // assert(p < q);
  
  return (p-q)*2*Pi;
}

#define eps 1e-8

double getArea(double p, double q) {
  //cout<<getAngle(p,q)<<endl;
  //if(getAngle(p,q) > 0.5) return fabs(sin(1-getAngle(p, q))/2);
  return (sin(getAngle(p, q))/2);
}

int n, m;
double p[MAX_N];

double dp[MAX_N][MAX_N][MAX_N];

double solve(int idx, int curr, int start) {
  if(idx == m) {
    //if((1+p[start]-p[curr])-0.5 > 0) return -getArea(p[curr], p[start]);
    return getArea(p[start], p[curr]);
  }
  if(curr+1 == n) return -1e9;
  double &res = dp[idx][curr][start];
  if(res != -1) return res;
  for(int i = curr+1; i < n; i++) {
    res = max(res, solve(idx+1, i, start) + getArea(p[i], p[curr]));
  }
  return res;
}

int main() {
  while(cin >> n >> m, n || m) {
    for(int i = 0; i < n; i++) cin >> p[i];

    double ans = 0;
    for(int i = 0; i < MAX_N; i++)
      for(int j = 0; j < MAX_N; j++)
	for(int k = 0; k < MAX_N; k++)
	  dp[i][j][k] = -1;
    
    for(int i = 0; i < n; i++) {
      ans = max(ans, solve(1, i, i));
    }
    printf("%.6f\n", ans);
  }
  
  return 0;
}

Measured memory_bytes: 4000
n = int(input())
a = list(map(int, input().split()))

b = []
for i in range(n):
    count = 0
    while a[i] % 2 == 0:
        a[i] = a[i] // 2
        count += 1
    b.append(count)

n0 = 0
n2 = 0
n4 = 0
for i in range(n):
    if b[i] == 0:
        n0 += 1
    elif b[i] == 1:
        n2 += 1
    else:
        n4 += 1

if n2 > 0:
    if n4 >= n0:
        print("Yes")
    else:
        print("No")
else:
    if n4 >= n0 - 1:
        print("Yes")
    else:
        print("No")

Measured memory_bytes: 14252
#include <iostream>
#include<bitset>
using namespace std;
 
int main() {
	bitset<10001> line;
	line[0]=true;
	int N;
	cin>>N;
	for(int i=0;i<N;++i){
		int d;
		cin>>d;
		line|=line<<d;
	}
	for(int i=10000;i>=0;--i){
		if(line[i]&&(i%10!=0)||i==0){
			cout<<i<<endl;
			break;
		}
	}
	return 0;
}

Measured memory_bytes: 256
def rec(X, N, M, queries):
    if len(X) == N:
        ret = 0
        for a, b, c, d in queries:
            ret += (c == (X[b] - X[a])) * d
        return ret
    ret = 0
    start = X[-1] if len(X) > 0 else 1
    for i in range(start, M+1):
        X.append(i)
        ret = max(ret, rec(X, N, M, queries))
        X.pop()
    return ret


def main() -> None:
    N, M, Q = map(int, input().split())
    queries = []
    for _ in range(Q):
        a, b, c, d = map(int, input().split())
        queries.append((a-1, b-1, c, d))
    print(rec([], N, M, queries))


if __name__ == '__main__':
    main()


Measured memory_bytes: 9264
#pragma region
#include<bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = n; i > 0; i--)
#define repc(i, m, n) for (int i = m; i < n; i++)
#define reprc(i, m, n) for (int i = m; i > n; i--)
#define all(v) v.begin(), v.end
#define INF 1e9
#define EPS 1e-10
using namespace std;
typedef long long ll;
#pragma endregion



int main()
{
    int N,Q,c;
    cin >> N;
    int A[N];

    rep(i, N){
        cin >> A[i];
    }

    cin >> Q;
    int m[Q];
    bool ans[Q];

    rep(i, Q){
        cin >> m[i];
        ans[i] = false;
    }

    for(int bit = 0; bit < (1 << N); bit++){
        c = 0;
        rep(i, N){
            if(bit & (1 << i)){
                c += A[i];
            }
        }

        rep(i, Q){
            if(c == m[i]){
                ans[i] = true;
            }
        }
    }

    rep(i, Q){
        if(ans[i]){
            cout << "yes" << endl;
        }else{
            cout << "no" << endl;
        }
    }
}


Measured memory_bytes: 3120
typedef long long ll;
#include <bits/stdc++.h>
using namespace std;
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int main() {
    ll n,k;
    std::cin >> n>>k;
    
    map<ll,ll> m;
    for (int i = 0; i < n; i++) {
        ll tmp;
        std::cin >> tmp;
        m[tmp]++;
    }
    vector<ll> c;
    for (auto e : m) {
        c.push_back(e.second);
    }
    sort(c.begin(),c.end());
    ll now = m.size();
    ll in = 0;
    ll result = 0;
    while(now>k){
        result += c[in];
        now--;
        in++;
    }
    std::cout << result << std::endl;
}


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

int main(void){
    int k; cin >> k;
    int64_t ans = 0;
    
    
    for(int x = 1; x <= k; x++){
        for(int y = 1; y <= k; y++){
            for(int z = 1; z <= k; z++){
                int a = gcd(x, y);
                int b = gcd(a, z);
                ans += b;
            }
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 3620
#include <bits/stdc++.h>
using namespace std;
//#define MOD 1000000007
#define MOD 998244353
#define INF 100000000000000009
typedef long long ll;
#define REP(i,n) for(int i=0;i<(n);++i)
#define OREP(i,n) for(int i=1;i<=(n);++i)
#define ZREP(i,n) for(int i=1;i<(n);++i)
#define YES(s) s?cout << "YES" << endl:cout << "NO" << endl
#define Yes(s) s?cout << "Yes" << endl:cout << "No" << endl
#define out(s,t,u) s?cout << t << endl:cout << u << endl
#define int ll
#define Endl endl


signed main(){
    int N,K;
    char S[11],e;
    cin >> N;
    for(int i=1;i<=N;i++){
        cin >> S[i];
    }
    cin >> K;
    
    e=S[K];
    
    for(int i=1;i<=N;i++){
        if(S[i]==e){
            cout << S[i];
        }else{
            cout << '*';
        }
    }
    cout << endl;
    
    
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
using ll = long long;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {";  \
	for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) true
#define dump(x) true
#endif

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);		//DON'T USE scanf/printf/puts !!
	cout << fixed << setprecision(20);
	
	int N;ll K; cin >> N >> K;
	V<ll> A(N),B(N);
	rep(i,N) cin >> A[i];
	rep(i,N) cin >> B[i];
	sort(all(A));
	sort(all(B));
	auto can = [&](ll T){
		ll need = 0;
		rep(i,N){
			ll tar = T/B[i];
			need += max(A[N-1-i]-tar,0LL);
		}
		return need <= K;
	};
	ll lb = -1, ub = 1e12;
	while(ub-lb>1){
		ll m = (lb+ub)/2;
		if(can(m)) ub = m;
		else lb = m;
	}
	cout << ub << endl;
}


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

int main() {
    int A, B;
    std::cin >> A >> B;

    int price = 0;
    if (A >= 13) {
        price = B;
    } else if (A >= 6) {
        price = B / 2;
    } else {
        price = 0;
    }

    std::printf("%d\n", price);

    return 0;
}


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

using namespace std;

int main()
{
    int n,z,w,a[3000],ans;
    scanf("%d%d%d",&n,&z,&w);
    for (int i=0;i<n;i++) scanf("%d",&a[i]);
    if (n<2) ans=abs(w-a[n-1]);
    else ans=max(abs(a[n-1]-a[n-2]),abs(w-a[n-1]));
    printf("%d",ans);
    return 0;
}


Measured memory_bytes: 256
/*

*/
#include<bits/stdc++.h>

using namespace std;
using ll=long long;
#define rep2(i, a, n) for(int i = (a); i < (n); i++)
#define rep(i, n) rep2(i,0,n)
template<class T,class U>void chmin(T &t,U f){if(t>f)t=f;}
template<class T,class U>void chmax(T &t,U f){if(t<f)t=f;}

void in(){} template<typename Head,typename... Tail> void in(Head&& head,Tail&&... tail){cin>>head;in(forward<Tail>(tail)...);}

int main(){
  cin.tie(nullptr);ios_base::sync_with_stdio(false);
  int n,x,t;
  in(n,x,t);
  cout<<((n+x-1)/x)*t<<endl;
}

Measured memory_bytes: 3616
import sys

x,n=map(int,input().split())

if n==0:
    print(x)
    sys.exit()
    
P=set(map(int,input().split()))

if not x in P:
    print(x)
    sys.exit()

i=1
while True:
    if not x-i in P:
        print(x-i)
        break

    if not x+i in P:
        print(x+i)
        break

    i+=1






Measured memory_bytes: 9188
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil,pi,factorial
    from operator import itemgetter
    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def LI2(): return [int(input()) for i in range(n)]
    def MXI(): return [[LI()]for i in range(n)]
    def SI(): return input().rstrip()
    def printns(x): print('\n'.join(x))
    def printni(x): print('\n'.join(list(map(str,x))))
    inf = 10**17
    mod = 10**9 + 7
#main code here!
    def gcd(a, b):
        while b:
            a, b = b, a % b
        return a
    x=I()
    print(360//gcd(360,x))


                
    
    
    
            
if __name__=="__main__":
    main()


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

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep3(i,m,n) for(int (i)=m;(i)<=(n);(i)++)
#define rep3rev(i,m,n) for(int (i)=m;(i)>=(n);(i)--)
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

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

void Main(){
	int n;
	cin >> n;
	vi t(n+1), x(n+1), y(n+1);
	t[0] = x[0] = y[0] = 0;
	rep(i,n) {
		cin >> t[i+1] >> x[i+1] >> y[i+1];
		int dx = abs(x[i+1]-x[i]);
		int dy = abs(y[i+1]-y[i]);
		int dt = t[i+1]-t[i];
		if(dx + dy <= dt && (dx + dy + dt)%2 == 0) ;
		else {
			puts("No");
			return;
		}
	}
	puts("Yes");
	return;
}

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

Measured memory_bytes: 1408
#include <iostream> 
#include <math.h>

using namespace std; 
#define file freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
#define ll long long int
#define REPI(a, b, i, inc) for(ll i = a; i < b; i = i + inc)
#define REP(i, a, b) for(ll i = a; i < b; i++)
#define F first
#define S second
#define pb push_back
#define test ll t; cin>>t; while(t--)
#define testi ll t; cin>>t; REP(tc,1,t+1)
#define vec vector
#define veci vector<ll>
#define pi pair<ll,ll>
#define mp make_pair
#define all(v) v.begin(), v.end()
#define uset unordered_set
#define umap unordered_map

#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);


int solve( int n)
{
	int c=0;
	REP(i,1,sqrt(n))
	{
		if(i*i+ 5>n) break;
	REP(j,1,sqrt(n))
	{
		if(i*i + j*j + i*j + j + i+1> n)
			break;
	REP(k,1,sqrt(n)){
	if( i*i + j*j + k*k + k*j +i*j +i*k == n)
		c++;
	if( i*i + j*j + k*k + k*j +i*j +i*k > n)
		break;
}
}
}

	return c;
}


 
int main() {
	
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif
	int n;
	cin>>n;
	REP(i,1,n+1){
	cout<<solve(i)<<endl;
}
	



        return 0;
    }

Measured memory_bytes: 3060
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
#include <cassert>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;

int main() {
  int p,s;
  int ffff = 0;
  while(cin>>p,p) {
    cin >> s;
    if (ffff) cout << endl;
    else ffff = 1;
    int a[s+1][p+1];
    vector<int> vx;
    vector<int> vy;
    REP(i,p+1) {
      REP(j,s+1) {
        string str;
        cin >> str;
        if (str == "?") {
          a[j][i] = INF;
          vx.push_back(j);
          vy.push_back(i);
        } else a[j][i] = atoi(str.c_str());
      }
    }

    bool update = 1;
    while(update) {
      update = 0;
      REP(x, s) {
        int cnt = 0;
        int xx, yy;
        int sum = 0;
        REP(y, p) {
          if (a[x][y] == INF) {
            cnt++;
            xx = x; yy = y;
          } else
            sum += a[x][y];
        }
        if (cnt == 1) {
          //cout << xx << " " << yy << " " << sum << endl;
          a[xx][yy] = a[x][p] - sum;
          update = 1;
        }
      }
      REP(y, p) {
        int cnt = 0;
        int xx, yy;
        int sum = 0;
        REP(x, s) {
          if (a[x][y] == INF) {
            cnt++;
            xx = x; yy = y;
          } else
            sum += a[x][y];
        }
        //    cout << sum << endl;
        if (cnt == 1) {
          //cout << xx << " " << yy << endl;
          a[xx][yy] = a[s][y] - sum;
          update = 1;
        }
      }
    }
    vector<int> res;
    REP(i,vx.size()) {
      if (a[vx[i]][vy[i]] != INF) {
        res.push_back(a[vx[i]][vy[i]]);
      }
    }
    if (res.size() != vx.size())
      cout << "NO" << endl;
    else
      FOR(it,res)
        cout << *it << endl;
  }
}

Measured memory_bytes: 0
/* Author: Prasun Biswas */

#include <bits/stdc++.h>
using namespace std;

#define FIO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
typedef long long ll;

int32_t main() {
	FIO;
	int t;
	cin >> t;
	set <string> s; string str;
	while(t--){
		cin >> str;
		s.insert(str);
	}
	cout << s.size() << "\n";
}

Measured memory_bytes: 19196
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <string>
#include <cstdio>

using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(a) (a).begin(),(a).end()
typedef pair<int,int> pii;
#define INF 999999999;

int r,c;
string data[50]={};

int d[50][50];
bool used[50][50];

int solve(char ch){
    if(ch=='o')return 1;
    if(ch=='x')return INF;
    if(ch=='#')return INF;
    if(ch=='.')return 2;
    if(ch=='G')return 2;
    return -1;
}


int dijk(pii st,pii gl){
    rep(i,50)rep(j,50)d[i][j]=INF;
    rep(i,50)rep(j,50)used[i][j] = false;
    
    d[st.first][st.second]=0;
    
    while(1){
        int v=-1,w=-1;
        rep(u,r){
            rep(p,c){
                if(!used[u][p] && (v==-1||d[v][w]>d[u][p]))v=u,w=p;
            }
        }
        
        if(v==-1)break;
        used[v][w]=true;
        
        int dx[]={1,0,-1,0};
        int dy[]={0,1,0,-1};
        for(int i=0;i<4;i++){
//            cout<<i<<endl;
//            cout<<v+dy[i]<<" "<<w+dx[i]<<endl;
            if(v+dy[i]>=0 && v+dy[i]<r && w+dx[i]>=0 && w+dx[i]<c){
                d[v+dy[i]][w+dx[i]] = min(d[v+dy[i]][w+dx[i]],d[v][w]+ solve( data[v+dy[i]][w+dx[i]] ) );
            }
        }
        rep(i,4){
            rep(j,3){
                int y=v+dy[i]*(j+1);
                int x=w+dx[i]*(j+1);
                if(y>=0 && y<r && x>=0 && x<c && data[y][x]!='#'){
//                if(v+dy[i]*(j+1)>=0 && v+dy[i]*(j+1)<c && w+dx[i]*(j+1)>=0 && w+dx[i]*(j+1)<r){
                    d[y][x] = min(d[y][x] , d[v][w]+2+j+1);
                }
                else break;
            }
        }
    }
    return d[gl.first][gl.second];
}


int main(){
    cin>>r>>c;

    rep(i,r){
        cin>>data[i];
    }
    
    pii st,gl;
    rep(i,r){
        rep(j,c){
            if(data[i][j]=='S'){
                st = make_pair(i,j);
            }
            if(data[i][j]=='G'){
                gl = make_pair(i,j);
            }
        }
    }
    cout<<dijk(st,gl)<<endl;
}

Measured memory_bytes: 1220
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdlib>
#include <queue>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <string>
#include <stack>
#include <cstring>
#include <set>
#include <map>
#include <climits>
#include <cassert>
using namespace std;
 


int main() {
    stack<int> S;
    int a, b, x;
    string s;
 
    while (cin >> s) {
        if (s[0] == '+') {
            a = S.top(); S.pop();
            b = S.top(); S.pop();
            S.push(a + b);
        }
        else if (s[0] == '-') {
            b = S.top(); S.pop();
            a = S.top(); S.pop();
            S.push(a - b);
        }
        else if (s[0] == '*') {
            a = S.top(); S.pop();
            b = S.top(); S.pop();
            S.push(a*b);
        }
        else {
            S.push(atoi(s.c_str()));
        }
    }
    cout << S.top() << endl;
}

Measured memory_bytes: 3216
#include<bits/stdc++.h>
#include <array>
using namespace std;
using ULL = unsigned long long;
using UL = unsigned;
using LL = long long;
#define rep(i, n) for(UL i = 0; i < (n); i++)

template<class Ty>
using passive_queue = priority_queue<Ty, vector<Ty>, greater<Ty>>;

struct Problem {

	void Solve() {
		UL N, M; cin >> N >> M;
		cout << ((N * (N - 1) + M * (M - 1)) / 2) << endl;
	}

	Problem();
};
int main() {
	unique_ptr<Problem> p(new Problem());
	p->Solve();
	return 0;
}
Problem::Problem() {
	cout << fixed << setprecision(10);
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define RI register int
typedef long long LL;

#define FILEIO(name) freopen(name".in", "r", stdin), freopen(name".out", "w", stdout);

using namespace std;

char buf[1000000], *p1 = buf, *p2 = buf;
inline char gc() {
  if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin);
  return p1 == p2 ? EOF : *(p1++);
}
template <class T> inline void read(T &n) {
  n = 0; RI ch = gc(), f;
  while ((ch < '0' && ch > '9') && (ch != '-')) ch = gc();
  f = (ch == '-' ? ch = gc(), -1 : 1);
  while (ch >= '0' && ch <= '9') n = n * 10 + (ch ^ 48), ch = gc();
  n *= f;
}

int const MAXN = 1e5 + 5, INF = -0x7f7f7f7f;
struct Edges { int to, next; } e[MAXN * 2];
int head[MAXN], tot, d[MAXN];
int a[MAXN];
int size[MAXN], Sa[MAXN];
int f[MAXN], ans;

inline void addedge(int from, int to) {
  e[++tot] = (Edges){to, head[from]};
  head[from] = tot;
  e[++tot] = (Edges){from, head[to]};
  head[to] = tot;
  ++d[from], ++d[to];
}

void Dfs(int now, int fa) {
  size[now] = 1;
  Sa[now] = a[now];
  for (RI i = head[now]; i; i = e[i].next)
    if (e[i].to != fa) {
      Dfs(e[i].to, now);
      size[now] += size[e[i].to];
      Sa[now] += Sa[e[i].to];
    }
  if (Sa[now] == size[now]) --d[fa];
}

void Solve(int now, int fa) {
  int mx1 = 0, mx2 = 0;
  for (RI i = head[now]; i; i = e[i].next)
    if (e[i].to != fa && Sa[e[i].to] != size[e[i].to]) {
      Solve(e[i].to, now);
      int tmp = f[e[i].to];
      if (tmp > mx1) swap(tmp, mx1);
      if (tmp > mx2) swap(tmp, mx2);
    }
  int op;
  if (d[now] & 1)
    if (a[now]) op = 2;
    else op = 0;
  else
    if (a[now]) op = 0;
    else op = 2;
  f[now] = mx1 + op;
  ans = max(ans, mx1 + mx2 + op);
  ans = max(ans, mx1 + op);
}

int main() {

#ifdef LOCAL
  FILEIO("a");
#endif

  int n; read(n);
  for (RI i = 1, x, y; i < n; ++i)
    read(x), read(y), addedge(x, y);
  for (RI i = 1; i <= n; ++i) {
    char ch = gc();
    while (ch != 'W' && ch != 'B') ch = gc();
    a[i] = (ch == 'B');
  }
  int root = 0;
  for (RI i = 1; i <= n; ++i)
    if (!a[i]) root = i;
  if (root == 0) return puts("0"), 0;
  Dfs(root, 0);
  Solve(root, 0);
  int sum = 0;
  for (RI i = 1; i <= n; ++i)
    if (size[i] != Sa[i]) {
      sum += d[i] + ((d[i] & 1) ^ a[i] ^ 1);
      if (d[i] == 0) return printf("%d\n", sum), 0;
    }
  printf("%d\n", sum - ans);

  cerr << (double)(clock()) / CLOCKS_PER_SEC * 1000 << "ms" << endl;

  return 0;
}

// created by Daniel yuan

Measured memory_bytes: 11392
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
from fractions import gcd
import sys
def input(): return sys.stdin.readline().rstrip()
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = 10**18
# ----------------------------------------------------------- #

s = S()
ans = 0
for c in s:
    if c == '+':
        ans += 1
    else:
        ans -= 1
print(ans)


Measured memory_bytes: 63340
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pb push_back
#define f first
#define s second
#define MOD 1000000007
 
using namespace std;

int  A[200001];

void solve(){
	int n; cin>>n;
	for(int i=0; i<n; i++){
		int x; cin>>x;
		A[i] = x;
	}
	int t=1;
	for(int i=0; i<n; i++){
		if(A[i] == t){
			t++;
		}
	}
	if(t== 1) cout<<-1;
	else cout<<n - t +1;

}


int main()
{ 
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//cout<<fixed<<setprecision(12);
	
	int t =1; 
	//cin>>t;

	for(int i=1; i<=t; i++){
		solve();
		cout<<"\n";
	}
	return 0;

}

Measured memory_bytes: 1024
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>

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

const int maxn=200000;
const int maxv=99999999;
const double pi=acos(-1);
const double D=11.4159265358979323846264338328;
const double K=-4.2920367320510338076867830836;

struct point
{
  int x,y;

  bool operator <(const point &other) const
  {
    return x<other.x;
  }
};

// int n,a[maxn+10],f[maxn+10],sx,sy,tx,ty;
// point p[maxn+10];

// int main()
// {
//   sx=read();
//   sy=read();
//   tx=read();
//   ty=read();
//   n=read();
//   for(int i=1; i<=n; ++i)
//     {
//       p[i].x=read();
//       p[i].y=read();
//     }
//   if(sx>tx)
//     {
//       std::swap(sx,sy);
//       std::swap(tx,ty);
//     }
//   if(sy>ty)
//     {
//       sy=maxv-sy;
//       ty=maxv-ty;
//       for(int i=1; i<=n; ++i)
//         {
//           p[i].y=maxv-p[i].y;
//         }
//     }
//   std::sort(p+1,p+n+1);
//   int tot=0;
//   for(int i=1; i<=n; ++i)
//     {
//       if((p[i].x>=sx)&&(p[i].y>=sy)&&(p[i].x<=tx)&&(p[i].y<=ty))
//         {
//           a[++tot]=p[i].y;
//         }
//     }
//   int cnt=0;
//   memset(f,63,sizeof f);
//   for(int i=1; i<=tot; ++i)
//     {
//       int pos=std::lower_bound(f+1,f+cnt+1,a[i])-f;
//       f[pos]=a[i];
//       if(pos>cnt)
//         {
//           cnt=pos;
//         }
//     }
//   double ans=100.0*(tx-sx+ty-sy);
//   if(cnt==std::min(tx-sx,ty-sy)+1)
//     {
//       ans+=(cnt-1)*K+D;
//     }
//   else
//     {
//       ans+=cnt*K;
//     }
//   printf("%.15lf\n",ans);
//   return 0;
// }
int n,m,maxy,sx,sy,ex,ey,tot,c[maxn+10],pos[maxn+10],now; double ans;
struct pot{
    int X,Y; void gtin(){X=read(),Y=read(),maxy=std::max(maxy,Y+1);}
    bool ft(){return X>=sx&&X<=ex&&Y>=sy&&Y<=ey;}
    bool operator <(const pot&ot)const{return X<ot.X;}
}p[maxn+10],t[maxn+10];
int main(){
    sx=read(),sy=read(),ex=read(),ey=read(),maxy=std::max(sy,ey);
    if(sx>ex) std::swap(sx,ex),std::swap(sy,ey);
    n=read();
    for(int i=1;i<=n;i++) p[i].gtin();
    if(sy>ey){
        for(int i=1;i<=n;i++) p[i].Y=maxy-p[i].Y;
        sy=maxy-sy,ey=maxy-ey;
    } std::sort(p+1,p+n+1),ans=(ey+ex-sy-sx)*100.0;
    for(int i=1;i<=n;i++) if(p[i].ft()) t[++tot]=p[i];
    memset(c,0x3f,sizeof(c)),c[0]=now=0;
    for(int i=1;i<=tot;i++){
        int l=1,r=now,md,res=0;
        while(l<=r){md=((r+l)>>1);if(c[md]<t[i].Y) res=md,l=md+1;else r=md-1;}
        if(res==now) now++;
        c[res+1]=std::min(c[res+1],t[i].Y);
    }
    if(now<=ex-sx&&now<=ey-sy) ans+=now*K;
    else ans+=(now-1)*K+D;
    printf("%.13lf\n",ans);
    return 0;
}


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

int main()
{
    int n;
    cin >> n;
    vector <int> a(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i];
    }
    sort (a.rbegin(), a.rend());
    long sum = 0;
    for (int i = 0; i < n-1; ++i)
    {
        sum +=a[(i +1)/2];
    }
    cout << sum;
}


Measured memory_bytes: 4028
package main

import (
	"fmt"
)

func main() {
	var n, i int
	fmt.Scan(&n)
	a := make([]int32, n)
	var num int32
	for i = 0; i < n; i++ {
		fmt.Scan(&num)
		a[i] = num
	}

	flag := false
	for i = 0;; i++ {
		for j := 0; j < n; j++ {
			if a[j] % 2 == 0 {
				a[j] /= 2
			} else {
				flag = true
				break
			}
		}
		if flag {
			break
		}
	}

	fmt.Println(i)
}


Measured memory_bytes: 512
#include<iostream>
#include<string>
#define REP(i, n) for(int i; i<n; i++)

using namespace std;

int main(){
    string s;
    cin >> s;
    
    bool flag = true;
    REP(i, s.size()){
        if(s[i] != s[s.size()-i-1]){
            flag = false;
        }
    }

    if(flag){
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
}


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

int main(){

    int m,d;cin >> m >> d;

    int res = 0;
    for(int i = 1; i <= m ; i++){
        for(int j = 1; j <= d ; j++){
            int d1 = j%10;
            int d10 = j/10;
            if(d1 >= 2 && d10 >= 2 && d1*d10 == i)res++;
        }
    }

    cout << res << endl;


    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define int long long
#define mod 1000000007
#define trace(x) cerr<<#x<<": "<<x<<" "<<endl
#define trace2(x, y) cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z) cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define sa(a,n) for(int i =0 ; i<n ; i++) cin>>a[i]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define scan() int n; cin>>n ; int a[n]; for(int i = 0 ; i<n ; i++) cin>>a[i]
#define print(a,n) for(int i = 0 ; i < n ; i++) cout<<a[i]<<' '
using namespace std;

int32_t main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        int n;
        cin >> n;
        int i,a[n+1];
        for(i=1; i<=n; i++) cin >> a[i];
        int dp[n+1];
        dp[1]=0;
        dp[2]=abs(a[1]-a[2]);
        for(i=3; i<=n; i++){
            dp[i]=min(dp[i-1]+abs(a[i]-a[i-1]),dp[i-2]+abs(a[i]-a[i-2]));
        }
        cout << dp[n];
        

}

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

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

#define rep(i,cc,n) for(int i=cc;i<n;++i)
#define lrep(i,cc,n) for(long long i=cc;i<n;++i)
#define sqrep(i,cc,n) for(long long i=cc;i*i<=n;++i)
#define rrep(i,cc,n) for(long i=cc;i>n;--i)
#define pii pair<int, int>
#define pll pair<long long, long long>
 
using ll = long long;

const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};
const ll inf = 1001001001;
const ll e9  = 1000000000;
const ll mod = 1000000007;

int main() {
    int h, w, n;
    cin >> h >> w >> n;
    queue<int>v;
    vector<vector<int>>v2(h, vector<int>(w));
    rep(i,1, n+1){
        int a;
        cin >> a;
        rep(j, 0, a){
            v.push(i);
        }
    }
    rep(i, 0, h){
        rep(j, 0, w){
            int p = v.front();
            v.pop();
            v2[i][j] = p;
        }
        if(i%2==1)reverse(v2[i].begin(),v2[i].end());
    }
    rep(i, 0, h){
        rep(j, 0, w){
            cout << v2[i][j] << " ";
        }
        cout << endl;
    }
}

Measured memory_bytes: 3684
# -*- coding: utf-8 -*-
s = input()

flg = (s[0] == '1')
ans = 0
for v in s[1:]:
  if (flg and v == '1'):
    ans += 1
    flg = False
  elif (not flg and v == '0'):
    ans += 1
    flg = True
  else:
    flg = (v == '1')
print(ans)


Measured memory_bytes: 3188
import java.io.*;
import java.util.Scanner;

public class Main {
	public static void main(String args[]) throws IOException {
		//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		Scanner scan = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();


		
		int i,s;
		i = 0;

		while(true){
			i++;
			s = scan.nextInt();
			//s = Integer.parseInt(br.readLine());
			if(s == 0){break;}
			
			//str = str+ "Case " + Integer.toString(i) + ": " + str + "\n";
			
			sb.append("Case ");
			sb.append(Integer.toString(i));
			sb.append(": ");
			sb.append(Integer.toString(s));
			sb.append("\n");
			
			
			
		}

		System.out.print(new String(sb));

	}
}

Measured memory_bytes: 43472
import sys
from itertools import accumulate


def solve():
    N, K = map(int, sys.stdin.readline().split())
    cusum_p = (0,) + tuple(accumulate(map(int, sys.stdin.read().split())))
    return (max(i - j for i, j in zip(cusum_p[K:], cusum_p[:-K])) + K) / 2


if __name__ == "__main__":
    answer = solve()
    print(answer)


Measured memory_bytes: 25352
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
int n;
bool memo[400000]={0};
 
int main(void){
  int i,j=2,co=0;
  for(i=2;i<sqrt(246913);i++){
    if(memo[i]==0){
      j=2;
      while(i*j<=246913){
    memo[i*j]=1;
    j++;
      }
    }
  }
  while(1){
    scanf("%d",&n);
    if(n==0)break;
    if(n==1)co=1;
    else {
      co=0;
      for(i=n+1;i<=2*n;i++)if(memo[i]==0)co++;
    }
    printf("%d\n",co);
  }
  return 0;
}



Measured memory_bytes: 2096
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define endl "\n"
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define F first
#define S second
#define bitcnt(v) __builtin_popcount(v)
#define all(v) v.begin(), v.end()

vector<ll> a;
int n;

void solution(){
	cin >> n;
	a = vector<ll>(n); for(auto& x : a) cin >> x;
	// state -- only depends on the portion of the array which is remaining
	// dp[i][j] -- minimum cost to combine array from a[i]....a[j]
	// for calculating answer for each state, we consider k to be the position
	// between i and j where elements at k and k+1 will be the LAST ones to be merged,
	// so the cost in that case would be the sums of a[i]..a[k] + a[k+1]..a[j]
	// dp[i][j] = min over left + right + dp[i][k] + dp[k+1][j];
	vector<ll> pre(n,0);
	pre[0] = a[0];
	for(int i = 1; i < n; i++) pre[i] = pre[i-1]+a[i];
	vector<vector<ll>> dp(n,vector<ll>(n,1e18));
	for(int x = 0; x < n; x++){
		for(int y = 0; y+x < n; y++){
			int i = y, j = y+x;
			if(i == j) dp[i][j] = 0;
			else if(i+1 == j) dp[i][j] = a[i]+a[j];
			else{
				for(int k = i; k < j; k++){
					ll left = pre[k] - (i == 0? 0 : pre[i-1]);
					ll right = pre[j] - pre[k];
					dp[i][j] = min(dp[i][j], left + right + dp[i][k] + dp[k+1][j]);
				}
			}
		}
	}
	cout << dp[0][n-1] << endl;
	
}	


int main(){ 

	fio;
	int t;
	t = 1;
	// cin >> t;
	for(int i = 1; i <= t; i++){
		// cout << "Case #" << i << ": ";
		solution();
	} 
	return 0;
}


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

typedef ostringstream OSS; typedef istringstream ISS;
typedef long long LL;
typedef unsigned int  UI;
typedef pair<int, int> PII;  typedef pair<LL, LL> PLL;

template <typename T> using VT = vector<T>;
template <typename T> using VVT = VT<VT<T>>;
template <typename T> using VVVT = VT<VT<VT<T>>>;
template <typename T> using VVVVT = VT<VT<VT<VT<T>>>>;

#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define EB emplace_back 
#define ALL(x)  (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define RANGEBOX(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define RANGE(x, l, r) ((l) <= (x) && (x) <= (r))
#define rep(i, N)  for (int i = 0; i < (int)(N); i++)
#define rrep(i, N) for (int i = N - 1; i >= 0; i--)
#define REP(i, init, N)  for (int i = (init); i < (int)(N); i++)
#define RREP(i, N, last) for (int i = (init - 1); i >= last; i--)
#define MAXUD(orig, target) orig = max(orig, target)
#define MINUD(orig, target) orig = min(orig, target)
#define DUMP(x) cerr << #x << " = " << (x) << endl

template < typename T > inline T fromString(const string &s) { T res; ISS iss(s); iss >> res; return res; };
template < typename T > inline string toString(const T &a)   { OSS oss; oss << a; return oss.str(); };

template<typename T=int> inline void dump(vector<T> vs, bool ent=false) { rep(i, vs.size()) cout << vs[i] << (i+1==vs.size() ? '\n' : ' '); if (ent) cout << endl; }
template<typename T = int> inline void dump(vector<vector<T>> vs, bool ent=false) { rep(i, vs.size()) dump<T>(vs[i]); if (ent) cout << endl; }

const int    INF  = 0x3f3f3f3f;
const LL     INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int    DX[] = {1,  0, -1, 0};
const int    DY[] = {0, -1,  0, 1};
const double EPS  = 1e-12;
// const double PI   = acos(-1.0);

template<typename T = int> inline T in() { T x; cin >> x; return x; }
template<typename T = int> inline vector<T> in(int n) { vector<T> xs(n); rep(i, n) cin >> xs[i]; return xs; }
template<typename T = int> inline vector<vector<T>> in(int n, int m) { vector<vector<T>> xs(n, vector<T>(m)); rep(i, n) rep(j, m) cin >> xs[i][j]; return xs; }

int main(void) {
    int n, m, x;
    cin >> n >> m >> x;

    VT<bool> bs(n + 1);
    rep(i, m) {
        int a;
        cin >> a;
        bs[a] = true;
    }

    int ans = INF;
    int c1 = 0;
    int cur = x;
    while (cur != n) {
        if (bs[cur]) {
            ++c1;
        }
        ++cur;
    }

    ans = min(ans, c1);

    int c2 = 0;
    cur = x;
    while (cur != 0) {
        if (bs[cur]) {
            ++c2;
        }
        --cur;
    }

    ans = min(ans, c2);

    cout << ans << endl;


    return 0;
}



Measured memory_bytes: 256
/*************************************************************************
    > File Name: D.cpp
    > Author: Roundgod
    > Mail: wcysai@foxmail.com 
    > Created Time: 2018-12-02 19:50:11
 ************************************************************************/

#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define MAXN 5005
#define INF 1000000000000000000LL
#define MOD 1000000007
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
ll n;
void relax(ll &a,ll b) {a=min(a,b);}
struct box
{
    ll h,p;
};
bool cmp(box x,box y)
{
    return min(x.h,y.h-x.p)>min(y.h,x.h-y.p);
}
box a[MAXN];
ll dp[MAXN][MAXN];
int main()
{
    scanf("%lld",&n);
    for(ll i=1;i<=n;i++) scanf("%lld%lld",&a[i].h,&a[i].p);
    sort(a+1,a+n+1,cmp);
    for(ll i=0;i<=n;i++)
        for(ll j=0;j<=n;j++)
            dp[i][j]=INF;
    dp[0][0]=0;
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<=i;j++)
        {
            if(dp[i][j]==INF) continue;
            ll &cur=dp[i][j];
            relax(dp[i+1][j],cur);
            if(cur<=a[i+1].h) relax(dp[i+1][j+1],cur+a[i+1].p);
        }
    }
    for(ll i=n;i>=0;i--)
    {
        if(dp[n][i]!=INF) 
        {
            printf("%lld\n",i);
            break;
        }
    }
    return 0;
}



Measured memory_bytes: 195840
#include <bits/stdc++.h>
#include <numeric>
typedef long long ll;
#define rep(i,a,b) for(int i=a;i<b;++i)
#define rrep(i,a,b) for(int i=a;i>=b;--i)
using namespace std;

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

    int K, A, B;
    cin >> K >> A >> B;
    ll biscuit = 1;
    ll money = 0;

    if (A >= B-2) {
        biscuit += K;
        cout << biscuit << endl;
        return 0;
    }

    rep(turn, 0, K) {
        if (money > 0) {
            biscuit += B;
            --money;
        }
        else if (biscuit >= A and K-turn >= 2) {
            biscuit -= A;
            ++money;
        }
        else
            ++biscuit;
    }

    cout << biscuit << endl;
}

Measured memory_bytes: 256
n=int(input())
s=input()

maxco=0

for i in range(len(s)):
    co=0
    le=s[0:i]
    ri=s[i:len(s)]
    for j in range(i):
        if le[j] not in le[0:j] and le[j] in ri :
            co+=1
    maxco=max(maxco,co)

print(maxco)


Measured memory_bytes: 3060
Explosion = (-3..3).map{|i| [[i,0], [0,i]]}.flatten(1) - [[0,0]]

def explode(x,y)
  @map[x][y] = "0"
  Explosion.each do |dx,dy|
    i,j = x+dx, y+dy
    next if !(0..7).include?(i) || !(0..7).include?(j)
    if @map[i][j] == "1"
      explode(i,j)
    end
    @map[i][j] = "0"
  end
end

n = gets.to_i
n.times do |dataset|
  gets
  @map = 8.times.map{ gets.chomp.chars }.transpose
  x,y = 2.times.map{ gets.to_i - 1 }
  explode(x,y)
  puts "Data #{dataset+1}:"
  puts @map.transpose.map(&:join)
end

Measured memory_bytes: 6192
#include<iostream>
#include<string>
using namespace std;
int main(){
	string a;
	while(true){
	int count=0;
	cin>>a;
	if(a=="0")
		break;
	for(int i=0;i<a.size();i++){
		if(a[i]=='1')
			count+=1;
		if(a[i]=='2')
			count+=2;
		if(a[i]=='3')
			count+=3;
		if(a[i]=='4')
			count+=4;
		if(a[i]=='5')
			count+=5;
		if(a[i]=='6')
			count+=6;
		if(a[i]=='7')
			count+=7;
		if(a[i]=='8')
			count+=8;
		if(a[i]=='9')
			count+=9;
	}
	cout<<count<<endl;
	}
	return 0;
}

Measured memory_bytes: 1188
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
	long long int n,sum=1,k=pow(10,9)+7;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		sum*=i;
		if(sum>k)
		{
			sum=sum%k;
		}
	}
	
	cout<<sum<<endl;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define sz(x) (int)(x).size()
#define uni(x) x.erase(unique(rng(x)),x.end())
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define EPS (1e-10)
#define equals(a, b) (fabs((a)-(b)) < EPS)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;
  
int main() {
  int N;
  cin >> N;
  v(string) ss(N);
  rep(i,N) {
    cin >> ss[i];
  }
  vp inc,dec;
  vi eq;
  rep(i,N) {
    int dep = 0;
    int mi=0;
    rep(j,sz(ss[i])) {
      if(ss[i][j]=='(') {
        dep++;
      } else {
        dep--;
      }
      chmin(mi,dep);
    }
    int r=dep-mi;
    int l=-mi;
    if(l<r) {
      inc.emplace_back(l,r);
    } else if(l>r) {
      dec.emplace_back(r,l);
    } else {
      eq.emplace_back(l);
    }
  }
  sort(rng(inc));
  sort(rng(dec));
  sort(rng(eq));
  bool valid=true;
  int dep=0;
  for(P lr:inc) {
    if(dep<lr.fi) {
      valid=false;
      break;
    }
    dep-=lr.fi;
    dep+=lr.se;
  }
  for(int k:eq) {
    if(dep<k) {
      valid=false;
      break;
    }
  }
  int dep2=0;
  for(P rl:dec) {
    if(dep2<rl.fi) {
      valid=false;
      break;
    }
    dep2-=rl.fi;
    dep2+=rl.se;
  }
  if(dep!=dep2) {
    valid=false;
  }
  if(valid) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}


Measured memory_bytes: 42476
#include <iostream>
#include <list>
#include <string>
#include <iomanip>
#include <sstream>

using namespace std;

class City {
public:
	int index;
	int prefecture;
	long year;
	string s;

	City(int a, int b, long c) {
		index = a;
		prefecture = b;
		year = c;
	}

	bool operator<(const City &ref) const
	{
		return year < ref.year;
	}
};


class Output {
public:
	int index;
	string s;

	Output(int a, string b) {
		index = a;
		s = b;
	}

	bool operator<(const Output &ref) const
	{
		return index < ref.index;
	}
};

int main()
{
	int n, m;
	cin >> n >> m;
	list<City> *lst = new list<City>[n];
	
	for (int i = 0; i < m; i++) {
		int tempP, tempY;
		cin >> tempP >> tempY;
		lst[tempP - 1].push_back(City(i, tempP, tempY));
	}

	list<Output>result;
	for (int i = 0; i < n; i++) {
		lst[i].sort();
		int order = 1;
		for (auto itr = lst[i].begin(); itr != lst[i].end(); itr++) {
			stringstream ss;
			ss << setfill('0') << setw(6) << itr->prefecture << setfill('0') << setw(6) << order;
			itr->s = ss.str();
			result.push_back(Output(itr->index, ss.str()));
			order++;
		}
	}
	
	result.sort();
	for (auto itr = result.begin(); itr != result.end(); itr++) {
		cout << itr->s << endl;
	}
}

Measured memory_bytes: 21888