akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1'000'000'007;
const ld PI = acos(-1);  
const ld EPS = 0.0000000001;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define rep(i, n) for(ll i=0; i<(ll)(n); i++)
#define repd(i, n) for(ll i=n-1; 0<=i; i--)
#define FOR(i, a, b) for(ll i=a; i<(ll)(b); i++)
#define FORD(i, a, b) for(ll i=a-1; (ll)(b)<=i; i--)
#define ALL(x) x.begin(), x.end()
#define rALL(x) x.rbegin(), x.rend()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main(){
    string s;   cin >> s;
    ll n;       cin >> n;
    vector<ll> v;
    ll cnt=0;
    FOR(i, 1, s.size()){
        if(s[i-1]==s[i]) cnt += 1;
        else{
            v.push_back(cnt+1);
            cnt = 0;
        }
    }
    if(0<cnt) v.push_back(cnt+1);
    
    ll res = n/2;
    if(1<v.size()){
        res = 0;
        if(s[0]!=s.back()){
            rep(i, v.size()) res += v[i]/2*n;
        }else{
            res = *v.begin()/2+v.back()/2 + (*v.begin()+v.back())/2*(n-1);
            FOR(i, 1, v.size()-1) res += v[i]/2*n;
        }
        
    }else if(0<v.size()) res = v.back()*n/2;

    cout << res << endl;
}

Measured memory_bytes: 3588
n,k,c=gets.split.map &:to_i
s=gets.chomp
idxs=[]
s.each_char.with_index{|c,i|
  idxs.push(i) if c==?o
}
cs=[0]
t,nidx=0,n-1
idxs.reverse_each{|e|
  if e<=nidx
    cs.unshift(t+=1)
    nidx=e-c-1
  else
    cs.unshift(t)
  end
}
nidx=0
idxs.each_with_index{|e,i|
  next if e<nidx
  puts e+1 if cs[i]<=k && cs[i]>cs[i+1]
  break if k==1
  k-=1
  nidx=e+c+1
}

Measured memory_bytes: 7160
#pragma GCC optimize ("O2")
#pragma GCC optimize ("tree-vectorize")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define FFOR(i, a, b) FOR(i, a, b+1)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FOR(i, 1, n+1)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define LEN(x) (int)(x).size()
#define DUMP(x) cerr<<__LINE__<<' '<<#x<<"="<<(x)<<endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair

using namespace std;
using lint = long long;
using pii =  pair<int, int>;
using pll =  pair<lint, lint>;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vector<T>>;
template <typename T> inline bool chmax(T &a, T b){if(a<b){a = b; return true;} return false;}
template <typename T> inline bool chmin(T &a, T b){if(a>b){a = b; return true;} return false;}

const double PI = acos(-1);
constexpr lint ten(int n) {return n==0 ? 1 : ten(n-1)*10;}

class Task{
public:
    void solve(istream& in, ostream& out){
        int X, Y, Z, K;
        in>>X>>Y>>Z>>K;
        vc<lint> A(X), B(Y), C(Z);
        for(auto& a : A) in>>a;
        for(auto& b : B) in>>b;
        for(auto& c : C) in>>c;
        sort(RALL(A));
        sort(RALL(B));
        sort(RALL(C));
        vc<lint> vec;
        REP(i, X) REP(j, Y) REP(k, Z){
            if(i*j*k>K) break;
            vec.push_back(A[i]+B[j]+C[k]);
        }
        sort(RALL(vec));
        REP(i, K) out<<vec[i]<<'\n';
    }
};

int main(){
    istream& in(cin);
    ostream& out(cout);
    ios_base::sync_with_stdio(false);
    in.tie(nullptr), out.tie(nullptr);
    Task solver;
    solver.solve(in, out);
    return 0;
}

Measured memory_bytes: 36228
#include <stdio.h>

int main(void)
{
    int i , j , k , count;
    int n , m;

        while ( 1 ) {
                scanf("%d %d" , &n , &m);
                count = 0;

                if ( n == 0 && m == 0 )
                    break;

                    for ( i = 1 ; i <= n ; i ++ ) {
                        for ( j = 1 ; j <= n ; j++ ) {
                            for ( k = 1 ; k <= n ; k++ ) {

                                if ( i + j + k == m && i !=  j && j != k && i != k )
                                    count++;

                            }
                        }
                    }

                printf("%d\n" , count / 6);

        }  

    return 0;

}



Measured memory_bytes: 2124
#include <iostream>
#include <bits/stdc++.h>
#include <math.h>

using namespace std;

int main(){
    int a,b,c,x,y;
    cin>>a>>b>>c>>x>>y;
    int amari=a;
    if(x<y) amari=b;
    
    cout<<min(min(max(x,y)*2*c,a*x+b*y),min(x,y)*2*c+amari*abs(x-y))<<endl;
    
    return 0;
}

Measured memory_bytes: 256
// AUTHOR: ROSHAN SRIVASTAVA
// WEBSITE: https://www.youtube.com/channel/UC6uQdd7kLLOdlHSVklhV7Cw

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007

// ll n,k,a,b; 
// ll arr[100009];
// ll m[100009];

int main(){

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	// memset(m, 0, sizeof m);

	// int t;
	// cin>>t;
	// while(t--) {
		ll n;
		cin>>n;
		if(n == 1){
			cout<<'a'<<endl;
			return 0;
		}
		char c;
		int d;
		// n--;
		string ans = "";
		while(n>0){
			c = 'a';
			d = n % 26;
			c += (d - 1) < 0 ? 25 : (d - 1);
			ans = c + ans;
			if(n % 26 == 0) {
				n /= 26;
				n --;
			} else {
				n /= 26;
			}
		}

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

Measured memory_bytes: 3628
#include<bits/stdc++.h> 
using namespace std;
char k;
int main()
{
    for(int i=1;i<=12;i++)
    {
        cin>>k;
        if(i<=3 || i>4)cout<<k;
    	else cout<<k<<" ";
    }
	cout<<endl;
    return 0;
}

Measured memory_bytes: 256
n = int(input())
lis = list(map(int,input().split()))
cou = 0
co = 0
ans1 = 0
ans2 = 0
for i in range(n):
  cou += lis[i]
  co += lis[i]
  if cou <= 0 and i % 2 == 0:
    ans1 += (1 -cou)
    cou = 1
  if cou >= 0 and i % 2 == 1:
    ans1 += (cou +1)
    cou = -1
  if co >= 0 and i % 2 == 0:
    ans2 += (co +1)
    co = -1
  if co <= 0 and i % 2 == 1:
    ans2 += (1 -co)
    co = 1
print(min(ans1,ans2))

Measured memory_bytes: 14468
//aryan_singh
#include<bits/stdc++.h>
using namespace std;
using ll=long long int ;
#define FOR0(n) for(ll i=0;i<n;i++)
int main()
{
  ll n,c1=0,c2=0,c3=0,c4=0;
  cin>>n;

  for(int i=0;i<n;i++)
  {
      string r;
      cin>>r;

      if(r=="AC")
      {
          c1++;
      }
      else if(r=="WA")
      {
          c2++;
      }
      else if(r=="TLE")
      {
          c3++;
      }
      else
      {
          c4++;
      }
  }

      cout<<"AC"<<" "<<"x"<<" "<<c1<<endl;
       cout<<"WA"<<" "<<"x"<<" "<<c2<<endl;
        cout<<"TLE"<<" "<<"x"<<" "<<c3<<endl;
         cout<<"RE"<<" "<<"x"<<" "<<c4<<endl;
  return 0;
}


Measured memory_bytes: 3628
N = str(input())
num=N
S=[int(c) for c in N]

#while int(num) > 0:
#    S.append(int(num) % 10)
#    num = int(int(num) / 10)
#S.reverse()

hako=[]
eq_1=0 # siki
eq_2=0
ans=0


for num in range(1<<(len(S)-1)):
    eq_1 = S[0]
    eq_2 = S[0]
    list_1=[]
    for i in range(len(S)-1):
        if num & (1 << i):
            eq_1+=S[i+1]
            list_1.append("+")
        else:
            eq_1-=S[i+1]
            list_1.append("-")
    if eq_1==7:
        print("{}{}{}{}{}{}{}=7".format(S[0],list_1[0],S[1],list_1[1],S[2],list_1[2],S[3]))
        break


Measured memory_bytes: 3064
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;

typedef unsigned long long ll;

struct Rail {
  int from;
  int to;
  int yen;
  int sunuke;
  ll cost;

  bool operator<(const Rail &e) const {
    return this->cost>e.cost;
  }
};

const int N = 100001;
vector<Rail> adj_list[N];

vector<ll> dijkstra(int start, bool changed) {
  vector<ll> res(N);
  bool visited[N];
  memset(visited, 0, sizeof(visited));

  priority_queue<Rail> q;
  Rail r = { start, start, 0, 0, 0 };
  q.push(r);
  while (!q.empty()) {
    Rail e = q.top();
    q.pop();

    int now_city = e.to;
    ll cost = e.cost;
    if (visited[now_city])
      continue;
    visited[now_city] = true;
    res[now_city] = cost;

    //cout << now_city+1 << ' ' << cost << endl;

    vector<Rail> v = adj_list[now_city];
    for (int i=0; i<v.size(); i++) {
      Rail r = v[i];
      int to = r.to;
        
      r.cost = cost + (changed ? r.sunuke : r.yen);
      q.push(r);
    }
  }

  return res;
}

int main() {
  int n, m, start, to;
  cin >> n >> m >> start >> to;
  start--;
  to--;
  for (int i=0; i<m; i++) {
    int u, v, yen, sunuke;
    cin >> u >> v >> yen >> sunuke;
    u--;
    v--;

    Rail r = { u, v, yen, sunuke, 0 };
    adj_list[u].push_back(r);
    Rail r2 = { v, u, yen, sunuke, 0 };
    adj_list[v].push_back(r2);
  }
  vector<ll> yen_min_path = dijkstra(start, false);
  vector<ll> sunuke_min_path = dijkstra(to, true);

  vector<ll> res;
  ll max = 0;
  for (int i=0; i<n; i++) {
    int j = n-i-1;
    ll use_yen = yen_min_path[j];
    ll use_sunuke = sunuke_min_path[j];
    ll s = 1000000000000000 - use_yen - use_sunuke;
    if (s>max) {
      max = s;
    }
    res.push_back(max);
  }
  for (int i=0; i<n; i++) {
    cout << res[n-i-1] << endl;
  }
  return 0;
}



Measured memory_bytes: 23820
fn main() {
    loop {
        let (n, m) = base();
        if (n, m) == (0, 0) {
            break;
        }

        let mut sum = vec![0; n];
        for _ in 0..m {
            let p = input();
            for (i, &x) in p.iter().enumerate() {
                sum[i] += x;
            }
        }

        println!("{}", sum.iter().max().unwrap());
    }
}

fn base() -> (usize, usize) {
    let v = input();

    (v[0] as usize, v[1] as usize)
}

fn input() -> Vec<i32> {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();

    let v: Vec<i32> = s.trim().split_whitespace().map(|x| x.parse().unwrap()).collect::<_>();

    v
}



Measured memory_bytes: 5036
#include<stdio.h>
int sort(int a[],int n)
{
	int i,j,t;
	for(i=0;i<n;i++)
	{
		for(j=0;j<n-1-i;j++)
		if(a[j]<a[j+1]) {t=a[j];a[j]=a[j+1];a[j+1]=t;}
 	}
}
int main()
{
	int i,n,k,a[100],s=0;
	scanf("%d%d",&n,&k);
	for(i=0;i<n;i++)
	   scanf("%d",&a[i]);
	sort(a,n);
	for(i=0;i<k;i++)
	  s=s+a[i];
	printf("%d\n",s);
}

Measured memory_bytes: 128
#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <functional>
#include <string>
#include <stack>
#include <map>

using namespace std;
using ll = long long;
ll mod = 1e9 + 7;


int main()
{
	int min = 123;
	int max = 0;

	for (int i = 0; i < 5; i++)
	{
		int c;
		cin >> c;
		if (c < min)
			min = c;
		if (c > max)
			max = c;
	}

	int k;
	cin >> k;

	if (max - min > k)
		cout << ":(" << endl;
	else
		cout << "Yay!" << endl;

	return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
double X, Y;
int main() {
	while (~scanf("%lf%lf", &X, &Y)) {
		if (X < 35.5 && Y < 71.0) printf("AAA\n");
		else if (X < 37.5 && Y < 77.0) printf("AA\n");
		else if (X < 40.0 && Y < 83.0) printf("A\n");
		else if (X < 43.0 && Y < 89.0) printf("B\n");
		else if (X < 50.0 && Y < 105.0) printf("C\n");
		else if (X < 55.0 && Y < 116.0) printf("D\n");
		else if (X < 70.0 && Y < 148.0) printf("E\n");
		else printf("NA\n");
	}
	return 0;
}

Measured memory_bytes: 2568
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll a,b,c;
    cin>>a>>b>>c;
    if(c==1000000000)
    {
      for(ll i=0;i<a;i++)
      {
        if(i>b-1)
        cout<<1<<" ";
        else cout<<c<<" ";
      }
    }
    else
    {
      for(ll i=0;i<a;i++)
      {
        if(i>b-1)cout<<c+1<<" ";
        else cout<<c<<" ";
      }
    }

  }


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

#define lint long long
#define P pair<int, int>
#define LLP pair<long long, long long>
#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)
#define reps(i, n) for(int i = 1, i##_len = (int)(n) ; i <= i##_len ; ++i)
#define rrep(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)
#define rreps(i, n) for(int i = (int)(n) ; i > 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())

const int IINF = (1 << 30) - 1;
const long long LLINF = 1LL << 61;
const long long MOD = 1000000007LL;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

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

    int n, k;
    cin >> n >> k;
    cout << n - k + 1 << endl;

    return 0;
}

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

int main(){
    string S;
    cin>>S;
    for(int i=0;i<S.size()-1;i++){
        if(S[i]==S[i+1]){
            cout<<i+1<<' '<<i+2<<endl;
            return 0;
        }
    }
    for(int i=0;i<S.size()-2;i++){
        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<cstdio>
#include<algorithm>
#include<functional>
using namespace std;
int main(void)
{
	int n,m,l,k,i,j,a[101][101],b[101][101];
	long long c[101][101];
	scanf("%d %d %d",&n,&m,&l);
	for(i=1;i<=n;i++)	{
		for(j=1;j<=m;j++)	{
			scanf("%d",&a[i][j]);
		}
	}
	for(i=1;i<=m;i++)	{
		for(j=1;j<=l;j++)	{
			scanf("%d",&b[i][j]);
		}
	}
	for(i=1;i<=n;i++)	{
		for(j=1;j<=l;j++)	{
			c[i][j]=0;
			for(k=1;k<=m;k++)	{
				c[i][j]+=a[i][k]*b[k][j];
			}
		}
	}
	for(i=1;i<=n;i++)	{
		printf("%lld",c[i][1]);
		for(j=2;j<=l;j++)	{
			printf(" %lld",c[i][j]);
		}
		printf("\n");
	}
	return 0;
}


Measured memory_bytes: 2904
#include <stdio.h>
#define M(a, b) (a < b ? b : a)
#define m(a, b) (a > b ? b : a)

int main() {
  int n, a, mi = 1000, ma = 0;
  scanf("%d", &n);
  for(int i = 0; i < n; i++) {
    scanf("%d", &a);
    ma = M(ma, a);
    mi = m(mi, a);
  }
  printf("%d\n", ma - mi);
}

Measured memory_bytes: 128
#include <stdio.h>
int main(void)
{
    int a, b, c;
    scanf("%d%d", &a, &b);
    int i, max, min;
    if (a >= b)
    {
        max = a;
        min = b;
    }
    else
    {
        max = b;
        min = a;
    }
  //printf("%d%d",min,max);
    for (i = 0; i < max; i++)
    {
      //printf("%d   %d\n",max-i,min + i);
        if (max - i == min+i)
        {
            printf("%d", max-i);
            return 0;
        }
    }
    printf("IMPOSSIBLE");
    return 0;
}

Measured memory_bytes: 128
// 4/25
// Greedy

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    // char S[50];
    // char T[50];
    string S;
    string T;
    cin >> S >> T;

    int ns = S.size();
    int nt = T.size();
    if (ns < nt) {
        cout << "UNRESTORABLE" << endl;
        return 0;
    }

    // bool b;

    // char ans[ns];
    bool find_match = false;

    for (int i = ns - 1; i >= nt - 1; i--) {
        // 既に見つけていたら、?だったらaに置換。
        // これなし。最後にチェックする。
        // if (find_match) {
        //     if (S[i] == '?') ans[i] = 'a';
        // }

        // まだ見つけていなければ、チェック。
        bool match = true;
        for (int j = 0; j < nt; j++) {
            if (S[i - j] != '?' && S[i - j] != T[nt - j - 1]) {
                match = false;
                break;
            }
        }

        if (match) {
            // cout << i << endl;
            for (int j = 0; j < nt; j++) {
                S[i - j] = T[nt - j - 1];
            }
            find_match = true;
            break;
        }
        // else {}
    }

    if (find_match) {
        for (int i = 0; i < ns; i++) {
            if (S[i] == '?') S[i] = 'a';
        }
        cout << S << endl;
    } else {
        cout << "UNRESTORABLE" << endl;
    }

    return 0;
}

Measured memory_bytes: 256
#include<iostream>

int main(){
	int pas[2],juice,min1,min2;
	std::cin>>min1>>pas[0]>>pas[1]>>min2>>juice;
	for(int i=0;i<2;i++)if(min1>pas[i])min1=pas[i];
	if(min2>juice)min2=juice;
	std::cout<<min1+min2-50<<std::endl;
	return 0;
}

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

using namespace std;

#define ll long long
#define fi first
#define se second

ll NAX=1e10;

void test_case(){
	ll h,n;
	scanf("%lld%lld",&h,&n);
	vector<ll> a(n),b(n);
	for(int i=0;i<n;i++)scanf("%lld%lld",&a[i],&b[i]);
	
	vector<vector<ll> > dp(1001,vector<ll>(10001,NAX));
	
	dp[0][0]=0;
	for(int i=1;i<=n;i++){
		for(int j=0;j<=h;j++){
			if(j>=a[i-1]){
				dp[i][j]=min(dp[i][j],dp[i-1][j-a[i-1]]+b[i-1]);
				dp[i][j]=min(dp[i][j-a[i-1]]+b[i-1],dp[i][j]);
			}
			else{
				dp[i][j]=min(dp[i-1][j],b[i-1]);
			}
			dp[i][j]=min(dp[i][j],dp[i-1][j]);
		}
		
	}
	printf("%lld\n",dp[n][h]);

}

int main(){
	int t=1;
	//scanf("%d",&t);
	
	while(t--)
		test_case();

}


Measured memory_bytes: 78592
#!usr/bin/env python3
from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import itertools
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return list(sys.stdin.readline())
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
mod = 1000000007

#C
def C():
    N = I()
    F = LIR(N)
    P = LIR(N)
    ans = -1*float("INF")
    kaiten = list((itertools.product([0,1], repeat=10)))
    del kaiten[0]
    for patern in kaiten:
        i = list(patern)
        anskaiten = [0 for i in range(N)]
        for youbi, n in enumerate(i):
            if n == 1:
                for nn,f in enumerate(F):
                    if f[youbi] == 1:
                        anskaiten[nn] += 1
        ansb = 0
        #print(anskaiten, end="")
        for k, p in enumerate(P):
            ansb += p[anskaiten[k]]
        ans = max(ans, ansb)
    print(ans)
        
if __name__ == '__main__':
    C()




Measured memory_bytes: 43740
#include<bits/stdc++.h>
using namespace std;
int main(){
	long double n,a,x,r=0;
	cin>>n>>a>>x;
	while(n){
		r+=a+(2*n-1)/2*x;
		a=((2*n+2)*a+5*x)/(2*n);
		x=(n+2)*x/n;
		n--;
	}
	printf("%.10Lf",r);
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <iostream>
#include <functional>
#include <vector>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <math.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define rep1(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define per1(i,a,n) for (int i=a;i>n;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define endl "\n"
#define pi 3.14159265358979323846264338327950288419716939937510
#define IOS cin.sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll mod=1000000007;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
ll qpow(ll a,ll b){ll s=1;while(b>0){if(b%2==1){s=s*a;}a=a*a;b=b>>1;}return s;}
ll qpowmod(ll a, ll b, ll c){ll res, t;res=1;t=a%c;while(b){if(b & 1){res=res*t%c;}t=t*t%c;b>>=1;}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;}
inline long long read(){long long k=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}return k*f;}
inline void write(long long x){if(x<0)x=-x,putchar('-');if(x>9)write(x/10);putchar(x%10+'0');}
// head
ll n,k,l,r,mid;
int main()
{
    //IOS;
    cin>>n>>k;
    if(n>k)
    {
        n=n-(n/k)*k;
        cout<<min(abs(n-k),abs(abs(n-k)-k))<<endl;
    }
    else if(n==k)
    {
        cout<<0<<endl;
    }
    else
    {
        cout<<min(abs(n-k),abs(abs(n-k)-k))<<endl;
    }
    return 0;
}
/*

*/


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <map>

#define mp(x, y) make_pair(x, y)
#define ALL(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define each(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)

using namespace std;

typedef long long LINT;

const LINT MAX = 10;

int N, S;
map<int, LINT> d[1 << MAX][MAX];
map<int, int> ans[MAX+1];

int bit ( int n )
{
    int res = 0;
    for (int i = 0; i < MAX; ++i) {
        if (n & (1 << i)) { ++res; }
    }
    return res;
}

int main ( void )
{
    for (int i = 0; i < MAX; ++i) {
        int state = 1 << i;
        d[state][i].insert(mp(i, 1));
        ans[1][i] = 1;
    }
    for (int state = 1, I = 1 << MAX; state < I; ++state) {
        int b = bit(state);
        for (int i = 0; i < MAX; ++i) {
            if (!(state & (1 << i))) {
                int next = state | (1 << i);
                for (int j = 0; j < MAX; ++j) {
                    if (state & (1 << j)) {
                        each(d[state][j], p) {
                            LINT num = i * (b+1) + (*p).first;
                            d[next][i][num] += (*p).second;
                            ans[b+1][num] += (*p).second;
                        }
                    }
                }
            }
        }
    }
    while (cin >> N >> S) {
        int out;
        if (N > 10 || S > 330) { out = 0; }
        else { out = ans[N][S]; }
        cout << out << endl;
    }
    return 0;
}

Measured memory_bytes: 11648
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define int long long
using namespace std;
int n,m;
int a[222222],b[222222];
set<int>st;
signed main(){
	cin>>n>>m;
	rep(i,m){
		cin>>a[i]>>b[i];
		if(b[i]==n)st.insert(a[i]);
	}
	rep(i,m){
		if(a[i]==1&&st.count(b[i])){
			puts("POSSIBLE");
			return 0;
		}
	}
	puts("IMPOSSIBLE");
	return 0;	
}


Measured memory_bytes: 8064
//おまじない
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#define INF 1e9+7
#define roop(i,m,n) for(int i=m;i<n;i++)
#define mroop(i,m,n) for(int i=m;i<n;i--)
#define NO cout<<"NO"<<endl;
#define YES cout << "YES"<<endl;
#define No cout << "No"<<endl;
#define Yes cout << "Yes"<<endl;
using namespace std;
typedef long long int ll;

//(int)'a'は97 (int)'A'は65
//おまじない



int main() {

	char c[11];
	char ans[11];
	cin >> c;


	for (int i = 0; c[i] != '\0'; i++) {
		if (c[i] == 'B') {
			c[i] = '3';
			for (int j = i-1; j >= 0; j--) {
				if (c[j] != '3') {
					c[j] = '3';
					break;
				}
			}
		}
	}

	
	for (int i = 0; c[i] != '\0'; i++) {
		if (c[i] != '3') { cout << c[i]; }
	}

	cout << endl;

	return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pi 3.141592653589793238
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
ll MOD=1e9+7;

ll f(pair<string,ll>&x ,pair<string,ll>&y) 
{
	if(x.first == y.first) 
	{
		return x.second>y.second;
	}
	else 
	{
		return x.first<y.first;
	}
}
 
void solve()
{
	ll a,b;
	cin>>a>>b;
	cout<<max(a+b,2*max(a,b)-1);
	return;
}

int main()
{
        fast;
        ll q;
        q=1;
        // cin>>q;
        while(q--)
        	solve();
        
        return 0;
}


Measured memory_bytes: 3600
#include <cstdio>
using namespace std;
const long long NR=110;
char s[NR];
long long l;
bool check(long long zuo,long long you)
{
	for(long long i=zuo;i<=you;i++)
	{
		if(s[i]!=s[zuo+you-i])
		{
			return false;
		}
	}
	return true;
}
int main()
{
	scanf("%s",s);
	while(s[l])
	{
		l++;
	}
	if(check(0,l-1)&&check(0,l/2-1)&&check(l/2+1,l-1))
	{
		printf("Yes");
	}
	else
	{
		printf("No");
	}
	return 0;
}

Measured memory_bytes: 128
#pragma region

#include "bits/stdc++.h"
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define _rep3(i, begin, end) for (register int i = (begin), i##_end = (end); i < i##_end; i++)
#define _irep3(i, begin, end) for (register int i = (end)-1, i##_begin = (begin); i >= i##_begin; i--)
#define _rep2(i, n) _rep3(i, 0, n)
#define _irep2(i, n) _irep3(i, 0, n)
#define _overload3(_1, _2, _3, name, ...) name
#define rep(...) _overload3(__VA_ARGS__, _rep3, _rep2, )(__VA_ARGS__)
#define irep(...) _overload3(__VA_ARGS__, _irep3, _irep2, )(__VA_ARGS__)
#define out(x) cout << (x) << '\n';
#define debug(x) cerr << #x << " = " << (x) << " (line: " << __LINE__ << ") " << endl;
typedef long long ll;

void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
}

template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec) is >> v;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "vec[ ";
  rep(i, vec.size()) i == 0 ? os << vec[i] : os << ", " << vec[i];
  os << " ]";
  return os;
}

#pragma endregion

#define MOD 1000000007
#define INF 2147483647

int main() {
  init();

  int N;
  cin >> N;
  vector<int> P(N);
  cin >> P;

  int ans = 0;
  int min = INF;
  rep(i, N) {
    if (P[i] < min) {
      ans++;
      min = P[i];
    }
  }

  out(ans);
}


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

int main(){
 
int n ;
cin>>n ;
double t,a ;
cin>>t>>a ;
double arr[n] ;
double mind = 100000000.00 ,id = -1 ;
for(int i = 0 ; i< n ; i++){
  cin>>arr[i] ;
  arr[i] = t-0.006*arr[i] ;
  if(abs(arr[i]-a) <mind){
   mind = abs(arr[i]-a) ;
   id = i+1 ; 
 }
}
cout<<id<<endl ;
return 0 ;
}

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

#define FOR(i, a, b) for (int i = (a); i < int(b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= int(a); --i)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, int(n) + 1)
#define RREP(i, n) RFOR(i, 0, n)
#define RREP1(i, n) RFOR(i, 1, int(n) + 1)
#define ALL(c) begin(c), end(c)

int _ = (
#ifndef LOCAL
    std::cin.tie(nullptr), std::ios::sync_with_stdio(false),
#endif
    std::cout.precision(10), std::cout.setf(std::ios::fixed));

using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <typename T> using vec = std::vector<T>;
using namespace std;

int main() {
  ll Q, H, S, D, N;
  cin >> Q >> H >> S >> D >> N;
  const ll h = min(H, 2 * Q);
  const ll s = min(S, 2 * h);
  const ll d = min(D, 2 * s);
  const ll a = (N / 2) * d + (N % 2) * s;
  cout << a << endl;
  return 0;
}


Measured memory_bytes: 3616
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#include<math.h>
#define INFINI 100000001
int gcd(int a,int b)
{
    if(b==0)return a;
    else return (b,a%b);

}
int  compareint(const void *a,const void *b)
{

    return *(int*)(a)-*(int*)(b);

}



int main(){

int t[26]={0};

char s[100001];
scanf("%s",s);

int i,lenght=strlen(s);

for(i=0;i<lenght;i++)
   t[ s[i]-'a']=1;
   int l=0;
for(i=0;i<26;i++)
if(t[i]==0){
    printf("%c",i+'a');
    l=1;
    break;
}
if(l==0)printf("None");

}


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

int main() {
  int N,K;
  cin>>N>>K;
  vector<int> a(N);
  rep(i,N){
    cin >> a.at(i);
  }
  rep(i,N-K){
    if(a.at(K+i)>a.at(i)){
      cout<<"Yes"<<endl;
    }
    else{
      cout<<"No"<<endl;
    }
  }
}

Measured memory_bytes: 4044
#include <iostream>
#include <queue>
#include <map>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstdarg>
#include <set>
#include <limits>
#include <iomanip>

using namespace std;
#define INF 2e9 + 9
#define MOD 1000000007
#define ALL(v) v.begin() , v.end()
#define Int int64_t
#define pll pair<Int,Int>
template<class T> using pql = priority_queue<T, vector<T>>;
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
#define ll(n) Int n; cin >> n;
#define LL(...) Int __VA_ARGS___; cin >> __VA_ARGS___;
#define OUT(...) cout << __VA_ARGS___;
#define RP(n, i, s) for(Int i = s ;i < n ;i++)
#define REP(n, i) for (Int i = 0; i < n; i++)
#define REPe(n, i) for (Int i = 1; i <= n; i++)
#define RPe(n, i, s) for (Int i = s; i <= n; i++)
#define upd_max(maxer, updater) if (maxer < updater) maxer = updater;

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

void ansYES(bool flag){
    if (flag) cout << "YES" << endl; else  cout << "NO" << endl;
}

struct node {
    Int to;
    Int length;
};


vector<Int> divisor(Int n) {
    vector<Int> ret;
    for (Int i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    return (ret);
}

/*
vector<vector<int>> to;
vector<int> flag;
vector<int> c;


Int dfs(int from, int mae) {
    flag[from] = 1;
    Int mini = 0;
    for (auto next : to[from]) {
        if (next == mae) continue;
        if (flag[next] == 1) {
            return -1;
        } else {
            mini = min(mini, dfs(next, from));
        }
    }
    return mini;
}*/

int dx8[8] = {0, -1, -1, -1, 0, 1, 1, 1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

struct datas{
    Int max;
    Int size;
};


int main(void) {
    Int a, b;
    cin >> a >> b;
    Int ans = - 100000;
    ans = max(ans, a * b);
    ans = max(ans, a + b);
    ans = max(ans, a- b);
    cout << ans << endl;
}




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


void solve(long long P, long long Q, long long R){
    cout << P + Q + R - max(P, max(Q, R)) << endl;
}

int main(){
    long long P;
    scanf("%lld",&P);
    long long Q;
    scanf("%lld",&Q);
    long long R;
    scanf("%lld",&R);
    solve(P, Q, R);
    return 0;
}


Measured memory_bytes: 256
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);

        int N = in.nextInt();
        int M = in.nextInt();
        long[][] edge = new long[N][N];
        long[][] edge_2 = new long[N][N];
        for (int i=0;i<N;i++) {
            for (int j=0;j<N;j++) {
                if (i!=j) {
                    edge[i][j]=1_000_000_000_000L;
                    edge_2[i][j]=1_000_000_000_000L;
                }
            }
        }
        for (int i=0;i<M;i++) {
            int a = in.nextInt()-1;
            int b = in.nextInt()-1;
            int c = in.nextInt();
            edge[a][b]=c;
            edge[b][a]=c;
            edge_2[a][b]=c;
            edge_2[b][a]=c;
        }

        for (int k=0;k<N;k++) {
            for (int i=0;i<N;i++) {
                for (int j=0;j<N;j++) {
                    edge_2[i][j] = Math.min(edge_2[i][j], edge_2[i][k]+edge_2[k][j]);
                }
            }
        }

        int ans = 0;
        for (int i=0;i<N;i++) {
            for (int j=i;j<N;j++) {
                if (edge[i][j]!=1_000_000_000_000L && edge[i][j]!=edge_2[i][j]) {
                    ans++;
                }
            }
        }
        out.println(ans);
        // out.println(Arrays.deepToString(edge));
        // out.println(Arrays.deepToString(edge_2));

        out.close();
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

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

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

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

    }
}


Measured memory_bytes: 22996
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <map>
#include <string>

#define reps(i,s,n) for(int (i) = (s); (i) < (n); (i)++)
#define rep(i,n) reps(i,0,n)
using namespace std;
using ll = long long;

int main(){
    int length;
    string str;
    cin >> length;
    cin >> str;
    
    int cnt = 1;
    rep(i,str.length()-1){
        if(str[i] != str[i+1]){
            cnt++;
        }
    }

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

Measured memory_bytes: 512
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
#define INF 1000000000000000000
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;

struct edge {
    ll to;
    ll cost;
};

int N;
vector<vector<edge>> G;
vector<ll> d(150);

void dijkstra() {
    d.assign(N, INF);
    d[0] = 0;
    vector<bool> seen(N, false);
    while (true) {
        ll minv = INF;
        int v = -1;
        rep(i, N) {
            if (!seen[i] && minv > d[i]) {
                v = i;
                minv = d[i];
            }
        }

        if (v == -1)
            break;
        seen[v] = true;
        for (int i = 0; i < G[v].size(); i++) {
            edge e = G[v][i];
            if (seen[e.to])
                continue;
            if (d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
            }
        }
    }
}

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

    cin >> N;
    G.resize(N);
    rep(i, N) {
        ll a, b;
        cin >> a >> b;
        rep(j, b) {
            ll c, d;
            cin >> c >> d;
            G[a].push_back({c, d});
        }
    }

    dijkstra();
    rep(i, N) { cout << i << " " << d[i] << endl; }
}


Measured memory_bytes: 3248
#include <math.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <assert.h>

#include <vector>
#include <numeric>
#include <iostream>
#include <algorithm>

#define nl                std::cout << "\n"
#define in(x)             std::cin  >> (x)
#define out(x)            std::cout << (x) << " "
#define err(x)            std::cerr << "DBG: " << #x << " " << (x) << "\n"

#define inn(x)            int x; in(x);
#define rep(i, n)         for (int i =  0 ; i < (n); ++i)
#define repr(i, l, r)     for (int i = (l); i < (r); ++i)
#define rrep(i, n)        for (int i = (n) - 1; i > -1; --i)

#define no                puts("NO")
#define yes               puts("YES")

using ll  = long long int;
using vi  = std::vector<int>;
using ii  = std::pair<int, int>;
using iii = std::pair<ii, int>;
using vii = std::vector<ii>;

const int N = 1010101;
const int MD = 1e9 + 7;

void solve() {
    inn(p); inn(q); inn(r);

    out(std::min(std::min(p + q, p + r), q + r));
}

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

    //int t; in(t); repr(tc, 1, t + 1)
    solve();

    return 0;
}


Measured memory_bytes: 384
// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <climits>
#include <complex>
#include <cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
//#define dump(x) cerr << #x << "=" << (x) << endl;
using namespace std;
const int INF = (int)2e9 + 1;
const long long LINF = (long long)9e18 + 1;

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

  int N;
  cin >> N;
  vector<int> seq(N);
  for (int i = 0; i < N - 1; i++) {
    cin >> seq[i];
  }
  seq[N - 1] = INF;

  int ans = seq[0];
  for (int i = 0; i < N - 1; i++) {
    ans += min(seq[i], seq[i + 1]);
  }

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


Measured memory_bytes: 256
main(){float D,a,b,c,d,r;for(;~scanf("%f%f%f%f%f",&D,&a,&b,&c,&d);D&&printf(fabs(a*d-b*c)||D<r?"impossible\n":"%f\n",r))r=hypot(a,b),r=a*c+b*d<0?r:2-r;}

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

public class Main {
    public static void main(String[] args) {
        new Main().solve();
    }

    void solve() {
        try (Scanner in = new Scanner(System.in)) {
            int n = in.nextInt();
            long a[] = new long[n];
            for (int i = 0; i < n; ++i) {
                a[i] = in.nextLong();
            }
            Arrays.sort(a);
            int min = -1, max = n;
            while (max - min > 1) {
                int mid = (max + min) / 2;
                if (ok(a, mid)) max = mid;
                else min = mid;
            }
            System.out.println(n - max);
        }
    }

    boolean ok(long a[], int x) {
        long s = a[x];
        for (int i = 0; i < a.length; ++i) {
            if (i == x) continue;
            if (s * 2 < a[i]) return false;
            s += a[i];
        }
        return true;
    }

    void tr(Object... o) {
        System.err.println(Arrays.deepToString(o));
    }
}


Measured memory_bytes: 51240
#include <iostream>
#include <vector>

using namespace std;

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

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

  vector<int> A(N);
  vector<int> count(1e6 + 1);
  Rep(n, N) {
    int a;
    cin >> a;
    A[n] = a;
    if (count[a]) {
      count[a]++;
      continue;
    }
    for (int i = a; i < 1e6 + 1; i += a) {
      count[i]++;
    }
  }

  int answer = 0;
  for (auto a : A) {
    if (count[a] <= 1) answer++;
  }
  cout << answer << endl;
  return 0;
}


Measured memory_bytes: 8004
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
#include<sstream>
using namespace std;
typedef  long long int lint;
typedef pair<int, int> IP;
typedef pair<lint, lint> LLP;
typedef pair<char, char>CP;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
#define upper(v,hoge) upper_bound(v.begin(),v.end(),hoge)
#define lower(v,hoge) lower_bound(v.begin(),v.end(),hoge)
#define llower(v,hoge) *lower_bound(v.begin(), v.end(), hoge)
#define lupper(v,hoge) *upper_bound(v.begin(), v.end(), hoge)



int main() {
	lint N, A, B;
	cin >> N >> A >> B;
	if (A > B) {
		cout << 0 << endl;
	}
	else if (N == 1) {
		if (A == B) {
			cout << 1 << endl;
		}
		else {
			cout << 0 << endl;
		}
	}
	else {
		lint X = (N - 1) * B + A;
		lint Y = (N - 1) * A + B;
		cout << X - Y + 1 << endl;
	}
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1<<30;

int main(){
  int n;cin>>n;
  int m=INF, M=-1;
  for(int i=0;i<n;i++){
    int a;cin>>a;
    m=min(m, a);M=max(M, a);
  }
  cout << M-m << endl;
}


Measured memory_bytes: 256
N = int(input())
As = list(map(int, input().split()))

sorted_iAs = sorted(map(tuple, enumerate(As)), key = lambda t: (t[1], t[0]), reverse = True)

#if As = 1 2 1 3 2 4 2 5 8 1
#then sorted_iAs is:
#   i | 8 7 5 3 6 4 1 9 2 0
#   A | 8 5 4 3 2 2 2 1 1 1
#I'll look at:
#       x x x x     x     x
#where the next A decreases (or doesn't exist).

ans = [0] * N
youngest_index = 10 ** 9
for index, (original_index, A) in enumerate(sorted_iAs):
    youngest_index = min(original_index, youngest_index)
    if index == N - 1:
        ans[youngest_index] += A * (index + 1)
    elif sorted_iAs[index + 1][1] < A:
        next_A = sorted_iAs[index + 1][1]
        ans[youngest_index] += (A - next_A) * (index + 1)

print(*ans, sep = '\n')

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

int main() {
    int n;
    string s;
    cin >> n >> s;
    int e[n+2] = {0}, w[n+2] = {0};
    for(int i = 1; i <= n; ++i) {
        if(s[i-1] == 'W') {
            w[i]++;
        }
        w[i+1] += w[i];
    }
    for(int i = n; i > 0; --i) {
        if(s[i-1] == 'E') {
            e[i]++;
        }
        e[i-1] += e[i];
    }
    int ans = 1000000;
    for(int i = 1; i <= n; ++i) ans = min(ans, w[i] + e[i] - 1);
    cout << ans << '\n';

    return 0;
}


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

int main() {
	string s;
	cin >> s;
	char op1[2] = {'0','1'};
	char op2[2] = {'1','0'};
	int ans1=0,ans2=0;
	for(int i=0;i<s.size();i++){
		ans1 += s[i] != op1[i&1];
		ans2 += s[i] != op2[i&1];
	}
	cout << min(ans1,ans2) << '\n';
	return 0;
}

Measured memory_bytes: 512
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<list>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<ctime>
using namespace std;

typedef long long ll;
bool debug = false;
const int NIL = -1;
const int INF = 1000000000;
const int NUM = 1410;
clock_t START, END;
int G[NUM][NUM], dp[NUM][NUM];

int main(void)
{
	if (debug) {
		START = clock();
		freopen("in29.txt", "r", stdin);
		freopen("out.txt", "w", stdout);
	};
	int H, W, maxWidth = 0;
	cin >> H >> W;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			cin >> G[i][j];
			int tmp = !G[i][j];
			maxWidth = max(maxWidth, tmp);
			if (i == 0 && G[i][j])
				dp[i][j] = 0;
			else
				dp[i][j] = 1;
		}
		if (G[i][0])
			dp[i][0] = 0;
		else
			dp[i][0] = 1;
	}
	for (int i = 1; i < H; i++) {
		for (int j = 1; j < W; j++) {
			if (G[i][j])
				dp[i][j] = 0;
			else {
				dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1;
				maxWidth = max(maxWidth, dp[i][j]);
			}
		}
	}
	cout << maxWidth * maxWidth << endl;
	if (debug) {
		END = clock();
		double endtime = (double)(END - START) / 1000;
		printf("total time = %lf s", endtime);
	}
	return 0;
}


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

void testCase(){
    string s, t;
    cin>>s>>t;
    t = t.substr(0, t.length()-1);
    if(s == t) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}

int main(){
    int t;
    t = 1;
    // cin>>t;
    while(t--){
        testCase();
    }
}


Measured memory_bytes: 3632
#include <iostream>
#include <string>
using namespace std;
string s;
int main(){
    cin >> s;
    bool a,b;
    if((s[0] == '0'&&s[1]!='0')||(s[0]=='1'&&(s[1] == '1'||s[1] == '2'))){
        a = true;
    }
    else{
        a = false;
    }

    if((s[2] == '0'&&s[3]!='0')||(s[2]=='1'&&(s[3] == '1'||s[3] == '2'))){
        b = true;
    }
    else{
        b = false;
    }
    if(a&&b){
        cout << "AMBIGUOUS"<<endl;
    }
    else if(a&&(!b)){
        cout << "MMYY" << endl;
    }
    else if((!a)&&b){
        cout << "YYMM" << endl;
    }
    else{
        cout << "NA" << endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
 
using namespace std;
 
int main() {
  int n;
  while( cin >> n, n ) {
    vector<int> vec(n);
    for(int i=0;i<n;++i) cin >> vec[i];
    sort(vec.begin(),vec.end());
    int mini = INT_MAX;
    for(int i=0;i<n-1;++i) {
      mini = min(mini,vec[i+1]-vec[i]);
    }
    cout << mini << endl;
  }
  return 0;
}


Measured memory_bytes: 3152
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <string>

using namespace std;

long long pascal[100][100];

void make_table()
{
    pascal[0][0] = 1;
    pascal[1][0] = 1;
    pascal[1][1] = 1;
    for (int i = 2; i < 60;i++)
    {
        pascal[i][0] = 1;
        pascal[i][i] = 1;
        for (int j = 1; j < i; j++)
        {
            pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j];
        }
    }
}

int main()
{
    long long en = 0, on = 0;
    long long n, p;
    make_table();
    cin >> n >> p;
    long long ensum = 1;
    long long onsum1 = 0;
    long long onsum2 = 0;
    for (long long i = 0; i < n;i++)
    {
        long long a;
        cin >> a;
        if(a % 2 == 0)
        {
            en++;
        }
        else
        {
            on++;
        }
    }
    for (int i = 1; i <= en; i++)
    {
        ensum += pascal[en][i];
    }

    for (int i = 1; i <= on; i+=2)
    {
        onsum1 += pascal[on][i];
    }
    for (int i = 0; i <= on; i += 2)
    {
        onsum2 += pascal[on][i];
    }
    long long ans = ensum * (p == 1 ? onsum1 : onsum2);

    cout << ans << endl;

    return 0;
}

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

int main() {
  string s; cin >> s;
  string ans = "AC";
  int C_cnt = 0;
  
  for (int i = 0; i < s.size(); ++i) {
    if (i == 0 ) {
      if (s[i] != 'A') ans = "WA";
    }
    else if (2 <= i && i <= s.size() - 2) {
      if (s[i] == 'C') ++C_cnt;
    }
    else {
      if (isupper(s[i])) ans = "WA";
    }
  }
  
  if (C_cnt != 1) ans = "WA";
  cout << ans << endl;
   
  return 0;
}

Measured memory_bytes: 3600
#include<iostream>
#include<math.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
const ll INF=1e9;

main(){
  ll n;
  cin >> n;
  ll W;
  cin >> W;
  ll weight[n],value[n];
  for(ll ii=0;ii<n;ii++)cin >> weight[ii] >> value[ii];
  ll dp[n+2][W+2];
  for(ll ii=0;ii<=n;ii++){
    for(ll jj=0;jj<=W;jj++){
      dp[ii][jj]=0;
    }
  }
  for(ll ii=0;ii<n;ii++){
    for(ll jj=0;jj<=W;jj++){
      if(jj-weight[ii]>=0)dp[ii+1][jj]=max(dp[ii][jj],dp[ii][jj-weight[ii]]+value[ii]);
      else dp[ii+1][jj]=dp[ii][jj];
    }
  }
  cout << dp[n][W] << endl;
}


Measured memory_bytes: 79872
// Created by sz
#include <bits/stdc++.h>
using namespace std;

int cnt[26];

int main(){
#ifdef LOCAL
    freopen("./input.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    string s;
    cin>>s;
    for(auto i:s){
        cnt[i-'a'] ++;
    }
    bool  ok = true;
    for(auto i:cnt){
        if( (i&1) != 0){cout<<"No"<<endl;ok = false;break;}
    }
    if(ok)cout<<"Yes"<<endl;
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<stack>
#include<algorithm>
#include<string>
#include<cstdio>
using namespace std;

double a,b,n,z;
string s;
stack < double > stk;

void solve(char c){
  b = stk.top();
  stk.pop();
  a = stk.top();
  stk.pop();

  if(c == '+'){
    n = a + b;
    stk.push(n);
  }
  else if(c == '-'){
    n = a - b;
    stk.push(n);
  }
  else if(c == '*'){
    n = a * b;
    stk.push(n);
  }
  else if(c == '/'){
    n = a / b;
    stk.push(n);
  }

}

int main(){

  while(getline(cin,s)){
    z = 0;

    for(int i=0;i<s.size();i++){
      if(s[i] >= '0' && s[i] <= '9'){
	if(z < 0){
	  z *= 10;
	  z -= s[i]-'0';
	}
	else {
	  z *= 10;
	  z += s[i]-'0';
	}
	if(s[i-1] == '-') z *= -1;
      }

      else if(s[i] == '-' && s[i+1] >= '0' && s[i+1] <= '9');

      else if(s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/') solve(s[i]);
      
      else if(s[i] == ' ' && (s[i-1] >= '0' && s[i-1] <= '9')){
	stk.push(z);
	z = 0;
      }
      
    }

    if(s.size() == 1){
      z = s[0]-'0';
      printf("%.6f\n",z);
    }

    else printf("%.6f\n",stk.top());

    while(!stk.empty()) stk.pop();

  }
}

Measured memory_bytes: 1236
from collections import deque
def gridbfs(grid,sy,sx):
    INF = 10**18

    gridy = len(grid)
    gridx = len(grid[0])

    dist = [[INF]*gridx for _ in range(gridy)]
    dist[sy][sx] = 0
    q = deque([(sy,sx)])

    while q:
        y,x = q.popleft()
        for dy,dx in [(-1,0),(1,0),(0,-1),(0,1)]:
            ny,nx = y+dy, x+dx
            if not (0 <= ny < gridy and 0 <= nx < gridx):
                continue
            if grid[ny][nx] == '#':
                continue
            if dist[ny][nx] != INF:
                continue
            dist[ny][nx] = dist[y][x] + 1
            q.append((ny,nx))

    return dist

H,W = map(int,input().split())

Grid = [input() for _ in range(H)]

cnt = 0

for i in range(H):
    cnt += Grid[i].count('.')

d = gridbfs(Grid,0,0)

if d[H-1][W-1] == 10**18:
    print(-1)
else:
    print(cnt - d[H-1][W-1] -1)



Measured memory_bytes: 3316