akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
using namespace std;
bool agari(int num[]){
 for(int head=1;head<10;head++){
  if(num[head]>=2){
   int hands[12]={0};
   for(int i=1;i<10;i++)hands[i]=num[i];
   hands[head]-=2;
   bool agari=true;
   for(int i=0;i<10;i++)
    while(hands[i]%3)
     for(int j=0;j<3;j++){
      hands[i+j]--;
      if(hands[i+j]<0)agari=false;
     }
   if(agari)return true;
  }
 }
 return false;
}
int main(){
 long long hand;
 while(cin>>hand){
  int num[10]={0};
  for(;hand>0;hand/=10)num[hand%10]++;
  bool tenpai=false;
  for(int i=1;i<10;i++)
   if (num[i]<4){
    num[i]++;
    if(agari(num)){
     if(tenpai)cout<<" ";
     cout<<i;tenpai=true;
    }
    num[i]--;
   }
  if(!tenpai)cout<<0;
  cout<<endl;
 }
}

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

signed main() {
	int n, q;
	cin >> n >> q;
	string s;
	cin >> s;
	int now = 0;
	vector<int> rui{0};
	rep(i, s.size() - 1) {
		if(s[i] == 'A' && s[i + 1] == 'C') {
			now++;
		}
		rui.push_back(now);
	}
	rep(i, q) {
		int l, r;
		cin >> l >> r;
		cout << rui[r - 1] - rui[l - 1] << endl;
	}
}

Measured memory_bytes: 1908
#include<stdio.h>
#include<string.h>
int main()
{
  char s[1000000],t[1000000];
  scanf("%s",s);
  scanf("%s",t);
  int count=0;
  int s1=strlen(s);
  for(int i=0;i<s1;i++)
  {
    if(s[i]!=t[i])
    {
      count++;
    }
  }
  printf("%d",count);
  return 0;
}

Measured memory_bytes: 2128
import Control.Applicative( (<$>))

main = do
    [q_, h_, s_, d_] <- map read . words <$> getLine
    n_	<- readLn::IO Integer
    let
	min'::Integer->Integer
	min' 0 = 0
	min' 1 = q_
	min' 2 = min (2*min' 1) h_
	min' 3 = minimum [  min' 2 + min' 1 ]
	min' 4 = minimum [  min' 3 + min' 1 ,
			    2*min' 2	    ,
			    s_		    ]
	min' 5 = minimum [  min' 4 + min' 1	, 
			    min' 3 + min' 2	]
	min' 6 = minimum [  min' 5 + min' 1	    ,
			    min' 4 + min' 2	    ,
			    2*min' 3		    ]
	min' 7 = minimum [  min' 6 + min' 1,
			    min' 5 + min' 2,
			    min' 4 + min' 3]
	min' 8 = minimum [  d_,
			    min' 7 + min' 1 ,
			    min' 6 + min' 2 ,
			    min' 5 + min' 3 ,
			    2*min' 4	    ]
	units = 4*n_
	(d,m) = units `quotRem` 8
    print $ d*(min' 8) + min' m


Measured memory_bytes: 508
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define y1 zzz
#define fi first
#define se second
#define PII pair<int,int>
#define PII2 pair<int,PII >
#define PII3 pair<PII,int >
#define PII4 pair<PII,PII >
#define all(x) (x).begin(),(x).end()
#define pb push_back
PII p[1000010];
vector<PII> up,down;
int n;
string s;
void solve()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>s;
        int res=0,tmp=0;
        for(char ch:s)
        {
            if(ch=='(')res++;
            if(ch==')')res--;
        }
        if(res>=0)
        {
            res=0;
            for(int i=0;i<s.size();i++)
            {
                if(s[i]=='(')res++;
                if(s[i]==')')res--;
                tmp=min(tmp,res);
            }
            up.pb({tmp,res});
        }
        if(res<0)
        {
            res=0;
            for(int i=s.size()-1;i>=0;i--)
            {
                if(s[i]=='(')res--;
                if(s[i]==')')res++;
                tmp=min(tmp,res);
            }
            down.pb({tmp,res});
        }
    }
    sort(all(up),greater<PII>());
    sort(all(down),greater<PII>());
    int res_up=0,res_down=0;
    for(PII it:up)
    {
        if(res_up+it.fi<0)
        {
            cout<<"No";
            return;
        }
        res_up+=it.se;
    }
    for(PII it:down)
    {
        if(res_down+it.fi<0)
        {
            cout<<"No";
            return;
        }
        res_down+=it.se;
    }
    if(res_up==res_down)cout<<"Yes";
    else cout<<"No";
}
int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    //freopen("CUBE.inp","r",stdin);
    //freopen("CUBE.out","w",stdout);
    //int t;cin>>t;while(t--)
    solve();
    return 0;
}


Measured memory_bytes: 19076
#include<stdio.h>
main()
{
	int a,b,c,x,y,z,i,j;
	for(int t=1;;t++)
	{
		scanf("%d %d",&i,&j);
		if(i==0&&j==0) break;
		for(x=1;x<=i;x++)
		{
			for(y=1;y<=j;y++)
			{
			   if((x%2==1&&y%2==0)||(x%2==0&&y%2==1))printf(".");
			   else printf("#");
			}
			printf("\n");
		}
		printf("\n");
	}
	return 0;
}

Measured memory_bytes: 1000
#include<stdio.h>
int data[10] = {0}, data2[100] = {0}, i = 0, j = 0;
void push(int n){
        data[i] = n;
}
void pop(int n){
        data2[j] = data[i];
        j++;
        data[i+1] = 0;
}
int main(){
        int n, k;
        while(scanf("%d", &n) != EOF){
                if(n == 0){
                        i--;
                        pop(n);
                }else{
                        push(n);
                        i++;
                }
        }
        for(k = 0; k < j; k++){
                printf("%d\n",data2[k]);
        }

}

Measured memory_bytes: 1740
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCoder_Grand_Contest_026_B
{
    class Program
    {
        static void Main(string[] args)
        {
            int T = int.Parse(System.Console.ReadLine());
            string[] line;
            long A, B, C, D;
            long X;

            for( int i = 0; i < T; i++)
            {
                line = Console.ReadLine().Split(' ');
                A = long.Parse(line[0]);
                B = long.Parse(line[1]);
                C = long.Parse(line[2]);
                D = long.Parse(line[3]);

                string ans = "Yes";

                if( A < B )
                {
                    ans = "No";
                }
                else if( B > D )
                {
                    ans = "No";
                }
                else if( C < (B - gcd( B, D ) + ( A % gcd( B, D ) ) ) )
                {
                    ans = "No";
                }

                Console.WriteLine(ans);

            }
            
        }

         static long gcd(long A, long B)
        {
            if (B == 0)
            {
                return A;
            }
            else
            {
                return gcd(B, A % B);
            }
            

        }
    }

}


Measured memory_bytes: 13140
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <map>
#include <list>
#include <set>
#include <numeric>
#include <queue>
#include <stack>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <climits>
#include <cfloat>
#include <ctime>
#include <complex>
#include <cassert>
#include <array>
#include <bitset> 
#include <unordered_map>
#include <random>

using namespace std;
typedef long long LL;
typedef pair<int,int> P;

int main() {
	int K,N;
	cin >> K >> N;
	if(K%2==0){
		cout << K/2;
		for(int i=1;i<N;i++){
			cout << " " << K;
		}
		cout << endl;
		return 0;
	}
	vector<int> vs;
	for(int i=0;i<N;i++){
		vs.push_back((K+1)/2);
	}
	for(int i=0;i<N/2;i++){
		if(vs.back()==1){
			vs.pop_back();
		}
		else{
			vs.back()--;
			while(vs.size()<N){
				vs.push_back(K);
			}
		}
	}
	auto sp="";
	for(auto v:vs){
		cout << sp << v;
		sp=" ";
	}
	cout << endl;

	return 0;
}



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

using namespace std;

string s;

int main(void) {

	cin >> s;

	int N = s.size();

	int m=0;
	for (int i = 0; i < N; i++) {
		if (s[i] == s[0]) { m++; }
	}

	int num;

	if (s[0] != s[N - 1]) { num = N - 2 * m; }
	else { num = N - 2 * m + 1; }

	if (num % 2 == 1) { cout << "First" << endl; }
	else { cout << "Second" << endl; }

	//system("pause");
	return 0;
}

Measured memory_bytes: 512
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define MOD 1000000007
#define INF (1<<29)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;


Int gcd(Int a, Int b){
  if(a==0)return b;
  return gcd(b%a, a);
}
Int lca(Int a, Int b){
  return a / gcd(a,b)* b;
}
bool isPrime[1080000];
vector<Int> primes;
void init(){
  fill(isPrime, isPrime + 1080000, true);
  isPrime[0] = isPrime[1] = false;
  for(Int i = 2;i < 1080000;i++){
    if(!isPrime[i])continue;
    primes.push_back(i);
    for(Int j = i * i;j < 1080000;j += i)isPrime[j] = false;
  }
}

set<Int> used;
Int n, A[540][540], p = 2;
int main(){
  init();
  cin >> n;
  for(Int i = 0;i < n;i++){
    for(Int j = 0;j < n;j++){
      if((i+j)%2){
	int k = (i+j)/2;
	Int pk, pl;
	if(k%2)pk = primes[k/2];
	else pk = primes[n-1-k/2];
	int l = (i+n-1-j)/2;
	if(l%2) pl = primes[l/2 + n];
	else pl = primes[n-1-l/2 + n];
	A[i][j] = pk*pl;
      }
    }
  }
  
  for(int i = 0;i < n;i++){
    for(int j = 0;j < n;j++){
      if((i + j) % 2 == 0){
	Int prod = 1;
	if(i)prod = lca(prod, A[i-1][j]);
	if(j)prod = lca(prod, A[i][j-1]);
	if(i<n-1)prod = lca(A[i+1][j], prod);
	if(j<n-1)prod = lca(A[i][j+1], prod);
	A[i][j] = prod+1;
	while(used.count(A[i][j]))A[i][j] += prod;
	used.insert(A[i][j]);
      }
    }
  }
  for(int i = 0;i < n;i++){
    for(int j = 0;j < n;j++){
      cout<< A[i][j] << "  ";
    }cout << endl;
  }
  return 0;
}

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

int main(){
    int n;
    cin>>n;
    double ans=0;
    for(int i=0;i<n;i++){
        double a;
        cin>>a;
        ans+=1/a;
    }
    cout<<setprecision(16)<<1/ans<<endl;
}

Measured memory_bytes: 256
// inputに入力データ全体が入る
function Main(input) {
  // 1行目がinput[0], 2行目がinput[1], …に入る
  input = input.split("\n");
  tmp0 = input[0].split(" ");
  R = Number(tmp0[0]);
  G = Number(tmp0[1]);
  B = Number(tmp0[2]);
  N = Number(tmp0[3]);

  var ans = 0;
  for (var r = 0; r <= 3000; r++) {
    for (var g = 0; g <= 3000; g++) {
      var b = N - (r * R + g * G);
      if (b >= 0) {
        // console.log(`r: ${r}, g: ${g}, b: ${b}`);
        // console.log(b % B);
        // console.log();
        if (b % B === 0) {
          ans++;
        }
      }
    }
  }
  console.log(ans);
}
//*この行以降は編集しないでください（標準入出力から一度に読み込み、Mainを呼び出します）
Main(require("fs").readFileSync("/dev/stdin", "utf8"));
// Main("13 1 4 3000\n");


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

#include <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::cpp_int;

cpp_int ModPow(cpp_int x, int n){
  cpp_int res = 1;
  while (n > 0) {
    if (n % 2 == 0) {
      x *= x;
      n /= 2;
    } else {
      res *= x;
      --n;
    }
  }
  return res;
}

cpp_int rec(int n, const cpp_int& x) {
  if (n == 0) return x >= 0;
  if (x == 0) return 0;
  if (x == ModPow(2, n + 1) - 2) return ModPow(2, n);
  if (x < ModPow(2, n + 1) - 2) return rec(n - 1, x - 1);
  return ModPow(2, n) + rec(n - 1, x - ModPow(2, n + 1) + 1);
}

int main() {
  int n;
  cin >> n;
  cpp_int x;
  cin >> x;
  cout << rec(n, x - 1) << endl;
}


Measured memory_bytes: 256
c1, c2, c3 = map(int, input().split())
c4, c5, c6 = map(int, input().split())
c7, c8, c9 = map(int, input().split())

if c1-c4 == c2-c5 == c3-c6 and \
   c1-c7 == c2-c8 == c3-c9 and \
   c4-c7 == c5-c8 == c6-c9:
    print('Yes')
else:
    print('No')

Measured memory_bytes: 3060
#include<stdio.h>
#include<string.h>

char stra[100], str[100], len;
void zure(int i){
  int j;
  for(j = 0;j < len;j++){
    if(stra[j] < 'a' || stra[j] > 'z')str[j] = stra[j];
    else str[j] = (stra[j] - 'a' + i) % 26 + 'a';
  }
  str[j] = '\0';
}

int main(){
  int x, j, i;
  while(scanf(" %[^\n]", stra) != EOF){
    len = strlen(stra);
    for(i = 0;i < 26;i++){
      int ok = 0;
      zure(i);
      for(j = 0;j < len - 4;j++){
	if(str[j] == 't' &&  str[j + 1] == 'h' && str[j + 2] == 'a' && str[j + 3] == 't'){
	  ok = 1;
	  break;
	}
	if(str[j] == 't' &&  str[j + 1] == 'h' && str[j + 2] == 'i' && str[j + 3] == 's'){
	  ok = 1;
	  break;
	}
	if(str[j] == 't' &&  str[j + 1] == 'h' && str[j + 2] == 'e'){
	  ok = 1;
	break;
	}
	if(str[j + 1] == 't' && str[j + 2] == 'h' && str[j + 3] == 'e'){
	  ok = 1;
	  break;
	}
      }
      if(ok == 1){
	puts(str);
	break;
      }
    }
  }
  return 0;
}

Measured memory_bytes: 524
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;

const int mod=1000000007;
const int N=2005;

int n,m,Q;
char s[N][N];

int f[N][N];
int g[N][N];
int h[N][N];
int p[N][N];

int calP(int x1,int y1,int x2,int y2) {
    if(x1>x2||y1>y2) return 0;
    return p[x2][y2]-p[x2][y1-1]-p[x1-1][y2]+p[x1-1][y1-1];
}

int main() {
    scanf("%d%d%d",&n,&m,&Q);
    for(int i=1;i<=n;++i) scanf("%s",s[i]+1);
    for(int i=1;i<=n;++i) {
        for(int j=1;j<=m;++j) {
            f[i][j]=f[i-1][j]+f[i][j-1]-f[i-1][j-1]+(s[i][j]=='1');
            p[i][j]=p[i-1][j]+p[i][j-1]-p[i-1][j-1];
            if(s[i][j]=='1') {
                if(i<n&&s[i+1][j]=='1') ++p[i][j];
                if(j<m&&s[i][j+1]=='1') ++p[i][j];
            }
        }
    }
    for(int i=1;i<=n;++i) {
        for(int j=1;j<=m;++j) {
            g[i][j]=g[i][j-1];
            if(s[i][j]=='1'&&j<m&&s[i][j+1]=='1') ++g[i][j];
        }
    }
    for(int j=1;j<=m;++j) {
        for(int i=1;i<=n;++i) {
            h[i][j]=h[i-1][j];
            if(s[i][j]=='1'&&i<n&&s[i+1][j]=='1') ++h[i][j];
        }
    }
    while(Q--) {
        int x1,x2,y1,y2;
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        int ans=f[x2][y2]-f[x2][y1-1]-f[x1-1][y2]+f[x1-1][y1-1];
        ans-=calP(x1,y1,x2-1,y2-1);
        ans-=g[x2][y2-1]-g[x2][y1-1];
        ans-=h[x2-1][y2]-h[x1-1][y2];
        printf("%d\n",ans);
    }
}



Measured memory_bytes: 68480
// g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<unordered_map>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define FOR(i,a) for(auto i:a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
#define show1d(v) {rep(_,v.size())cout<<" "<<v[_];puts("");}
#define show2d(v) {rep(__,v.size())show1d(v[__]);}
using namespace std;
//kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
#define mt make_tuple
typedef tuple<int,int,int,int> tp;
typedef vector<tp> vt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
//template<class C>constexpr int size(const C &c){return (int)c.size();}
//template<class T,size_t N> constexpr int size(const T (&xs)[N])noexcept{return (int)N;}
const double PI=acos(-1);
const double EPS=1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10;
// int dx[]={0,1,0,-1};
// int dy[]={1,0,-1,0};//RDLU
#define yes puts("YES")
#define no puts("NO")

int dp[10010][110][2];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	int n;
	cin>>n;
	vvi in(n,vi(3));
	rep(i,n)rep(j,3)cin>>in[i][j];

	sort(all(in),[](vi a,vi b){
		return a[0]+a[1]<b[0]+b[1];
	});
	vi dp(30000,-inf);
	dp[0]=0;
	rep(i,n)for(int j=in[i][1];j>=0;j--)if(dp[j]+inf)
		cmax(dp[j+in[i][0]],dp[j]+in[i][2]);
	int out=0;
	rep(i,30000)cmax(out,dp[i]);
	cout<<out<<endl;
}


Measured memory_bytes: 512
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);
  //a
  int a, b;
  cin >> a >> b;
  if (a < 10 && b < 10 && a > 0 && b > 0) cout << a * b << '\n';
  else cout << -1 << '\n';
  return 0;
}

Measured memory_bytes: 256
N = gets.to_i
S = gets.chomp
cand = []
1.upto(N - 1) do |i|
  l = S[0..(i - 1)].split('')
  r = S[i..(N - 1)].split('')
  cand.push((l & r).uniq.size)
end
puts cand.max

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

using namespace std;

int n, m, a, b, ini, fim;

int main() {
	
	scanf("%d %d", &n, &m);
	
	ini = 1, fim = n;
	for (int i=0; i<m; ++i) {
		scanf("%d %d", &a, &b);
		
		if (a>ini) 
			ini = a;
		if (b<fim)
			fim = b;
	}
	if (fim >= ini)
		printf("%d\n", fim-ini+1);
	else 
		puts("0");
		
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int,int>
#define VI vector<int>
#define VPII vector<pair<int,int> >
#define PLL pair<long long,long long>
#define VPLL vector<pair<long long,long long> >
#define F first
#define S second
typedef long long LL;
using namespace std;
const int MOD = 1e9+7;
const int SIZE = 1e6+10;

int main(){
    DRII(K,n);
    int ma=0;
    REP(i,n){
        DRI(x);
        ma=max(ma,x);
    }
    printf("%d\n",max(0,K-1-(K-ma)*2));
    return 0;
}


Measured memory_bytes: 256
X,N = map(int,input().split())
n = list(map(int,input().split()))
xn = list(range(0,102))
answer = 0
for i in range(N):
  xn.remove(n[i])
mindis = 100
for i in range(len(xn)):
  dis = abs(X-xn[i])
  if dis < mindis:
    mindis = dis
    answer = xn[i]
print(answer)

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


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

	if (k % 2 == 0) {
		cout << k / 2 * k / 2 << endl;

	}

	else {
		cout << (k - 1) / 2 * (k - 1) / 2 + (k - 1) / 2 << endl;
	}

	return 0;

}




Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, e) for (int i = (int)(s); i <= (int)(e); i++)
#define printYesNo(is_ok) puts(is_ok ? "Yes" : "No");
#define SORT(v) sort(v.begin(), v.end());
#define RSORT(v) sort(v.rbegin(), v.rend());
#define REVERSE(v) reverse(v.begin(), v.end());

template <typename T>
void printlnVector(T v)
{
  rep(i, v.size())
  {
    cout << v[i] << endl;
  }
}

template <typename T>
void printVector(T v)
{
  rep(i, v.size())
  {
    cout << v[i] << " ";
  }
  cout << endl;
}

int main()
{
  int N;
  cin >> N;
  vector<long long> A(N);
  rep(i, N)
  {
    cin >> A[i];
  }
  vector<long long> left(N + 1), right(N + 1);
  rep(i, N)
  {
    left[i + 1] = left[i] + A[i];
  }
  map<long long, long long> count;
  rep(i, N + 1)
  {
    count[left[i]]++;
  }
  long long ans = 0;
  for (pair<long long, long long> P : count)
  {
    if (P.second < 1)
      continue;
    ans += P.second * (P.second - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 17408
def c_time3_or_div2(N, A):
    # Aの各要素aにおける因数2の個数の和
    # aを2進数として下位ビットから見て、最初に1が現れた桁数が因数2の個数
    return sum((bin(a)[::-1].index('1') for a in A))

N = int(input())
A = [int(i) for i in input().split()]
print(c_time3_or_div2(N, A))

Measured memory_bytes: 4148
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <cmath>
#include <numeric>
#include <iomanip>
#include <deque>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <sstream>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define FOR(i, a, done) for (int i = (a); i < (done); ++i)
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define pb push_back
#define pf push_front

namespace mp = boost::multiprecision;
using cpp_int = mp::cpp_int;
using ll = long long;
using namespace std;

ll M = 1000000007;
typedef pair<int, int> P;

int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    if(a<10&&b<10){
        cout<<a*b<<endl;
    }else{
        cout<<-1<<endl;
    }
    
}


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

int H, W;

int main() {
    int maximum, map[10][10], tmp_sea_max, tmp_wall_min, tmp;

    while (true) {
        scanf("%d %d", &H, &W);
        if (H == 0 && W == 0) break;

        for (int i = 0; i < H; i++) {
            for (int k = 0; k < W; k++) scanf("%d", &map[i][k]);
        }

        maximum = 0;

        for (int sea_left = 1; sea_left <= W - 2; sea_left++) {
            for (int sea_right = sea_left; sea_right <= W - 2; sea_right++) {
                for (int sea_top = 1; sea_top <= H - 2; sea_top++) {
                    for (int sea_bottom = sea_top; sea_bottom <= H - 2; sea_bottom++) {
                        tmp_sea_max = 0;
                        for (int row = sea_top; row <= sea_bottom; row++) {
                            for (int col = sea_left; col <= sea_right; col++) {
                                tmp_sea_max = max(tmp_sea_max, map[row][col]);
                            }
                        }

                        tmp_wall_min = 100;
                        for (int col = sea_left - 1; col <= sea_right + 1; col++) tmp_wall_min = min(tmp_wall_min, map[sea_top - 1][col]);
                        for (int col = sea_left - 1; col <= sea_right + 1; col++) tmp_wall_min = min(tmp_wall_min, map[sea_bottom + 1][col]);
                        for (int row = sea_top - 1; row <= sea_bottom + 1; row++) tmp_wall_min = min(tmp_wall_min, map[row][sea_left - 1]);
                        for (int row = sea_top - 1; row <= sea_bottom + 1; row++) tmp_wall_min = min(tmp_wall_min, map[row][sea_right + 1]);

                        if (tmp_sea_max >= tmp_wall_min) continue;

                        tmp = 0;
                        for (int row = sea_top; row <= sea_bottom; row++) {
                            for (int col = sea_left; col <= sea_right; col++) {
                                tmp += tmp_wall_min - map[row][col];
                            }
                        }

                        maximum = max(maximum, tmp);
                    }
                }
            }
        }

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

    return 0;
}


Measured memory_bytes: 3140
// https://yamakasa.net/atcoder-agc-039-a/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for(ll i = 0; i < (ll)n; i++)
int main() {
    string S;
    ll K;
    cin >> S >> K;
    ll n = S.length();
    if(n == 1) {
        cout << K / 2 << "\n";
        return 0;
    }
    ll cnt = 1;
    REP(i, n - 1) {
        if(S[i] == S[i + 1]) {
            cnt++;
        }
    }
    if(cnt == n) {
        cout << K * n / 2 << endl;
        return 0;
    }
    ll l = 0;
    ll r = 0;
    for(int i = 0; i < n; i++) {
        if(S[0] == S[i]) {
            l++;
        } else {
            break;
        }
    }
    if(l == n) {
        cout << n * K / 2 << "\n";
        return 0;
    }
    for(int i = n - 1; i >= 0; i--) {
        if(S[n - 1] == S[i]) {
            r++;
        } else {
            break;
        }
    }
    if(l + r == n) {
        // S[0] != S[n - 1]
        ll ans = l / 2 * K + r / 2 * K;
        cout << ans << "\n";
        return 0;
    }
    ll ans = 0;
    ll c = 1;
    for(int i = l; i < n - r; i++) {
        if(S[i] == S[i + 1]) {
            c++;
        } else {
            ans += c / 2 * K;
            c = 1;
        }
    }
    ans += c / 2 * (K - 1);

    if(S[0] == S[n - 1]) {
        ans += l / 2 + (l + r) / 2 * (K - 1) + r / 2;
    } else {
        ans += l / 2 * K + r / 2 * K;
    }
    // cout << l << " " << r << "\n";
    cout << ans << "\n";
    return 0;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
 
int main(){
  int A,B,C,D,x,y;
  cin >> A >> B >> C >> D;
  y=C+B-1;
  x=A+D-1;
  if(x/D<y/B){
    cout << "No" <<endl;
  }
  else{
    cout << "Yes" <<endl;
  }
}

Measured memory_bytes: 3588
#include "bits/stdc++.h"
using namespace std;
const int MOD = 1e9 + 7;
typedef long long lint;
const int INF = 1e7;
#define rep(i, n) for (lint i = 0; i < (n); ++i)
#define repi(i, k, n) for (int i = (k); i < (int)(n); ++i)
typedef pair<int, int> P;
typedef vector<lint> vi;
typedef vector<vi> vvi;
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define N 45000
vi s(N + 1, 1);
void eli(int p)
{
    for (int i = p + p; i <= N; i = i + p)
    {
        s[i] = 0;
    }
}
void eratos()
{
    s[0] = s[1] = 0;
    rep(i, N + 1)
    {
        if (s[i] == 1)
        {
            eli(i);
        }
    }
}
int main()
{
    eratos();
    vi v;
    rep(i, N + 1)
    {
        if (s[i])
        {
            v.push_back(i);
        }
    }
    lint l, r;
    cin >> l >> r;
    vi num(r - l + 1);
    rep(i, r - l + 1)
    {
        num[i] = i + l;
    }
    vi count(r - l + 1, 0);
    for (lint i : v)
    {
        for (lint j = (l + i - 1) / i * i; j <= r; j += i)
        {
            while (num[j - l] % i == 0)
            {
                num[j - l] /= i;
                count[j - l]++;
            }
        }
    }
    lint ans = 0;
    rep(i, r - l + 1)
    {
        if (num[i] != 1)
        {
            count[i]++;
        }
        if(s[count[i]]){
            ans++;
        }
   }
    cout << ans << endl;
    return 0;
}



Measured memory_bytes: 19004
#include<bits/stdc++.h>
using namespace std;
#define MS(x, i) memset(x, i, sizeof x)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define MOD 1000000007
#define NOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int maxn = 1e3 + 10;
const int inf = 0x3f3f3f3f;
char F[200];

int nums[maxn];

ll n, aa, bb;

inline ll read() {
    ll ret = 0, sgn = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if(ch == '-') sgn = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9'){
        ret = ret*10 + ch - '0';
        ch = getchar();
    } 
    return ret*sgn;
}

inline void write( ll x ) {
    ll tmp = x > 0 ? x : -x;
    if(x < 0) putchar('-');
    int cnt = 0;
    while(tmp > 0) {
        F[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while(cnt > 0) putchar(F[--cnt]);
}

ll qpow(ll a, ll b)
{
    ll base = a;
    ll ans = 1;
    while(b)
    {
        if(b & 1)
        {
            ans = (ans * base) % MOD;
            b--;
        }
        b >>= 1;
        base = (base * base) % MOD;
    }
    return ans;
}

ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0)
    {
        x=1,y=0;
        return a;
    }
    ll res=exgcd(b,a%b,y,x);
    y-=a/b*x;
    return res;
}

ll Inv(ll a)
{
    ll d,x,y;
    d=exgcd(a,MOD,x,y);
    if(d==1)
      return (x%MOD+MOD)%MOD;
    return -1;
} 

ll C(ll n,ll m)
{
    if(m > n)
        return 0ll;
    if(m == n)
        return 1ll;
    ll ans1=1,ans2=1;
    for(int i=n,j=1;j<=m;j++,i--)
    {
        ans1=ans1*i%MOD;
        ans2=ans2*j%MOD;
    }
    return (ll)(ans1*Inv(ans2)%MOD);
}

int main()
{
    NOS
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    cin >> n >> aa >> bb;

    cout << ((qpow(2, n) - 1 - C(n, aa) + MOD) % MOD - C(n, bb) + MOD) % MOD << endl;

    return 0;
}

Measured memory_bytes: 256
N,M,X,Y=map(int,input().split())
x=[int(i) for i in input().split()]
y=[int(i) for i in input().split()]
for z in range(X+1,Y,1):
    if z>max(x):
        if z<=min(y):
            print('No War')
            break
else:
    print('War')

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

bool solve() {
  int n, m; cin >> n >> m;
  vector<bool> a(n), b(n);
  while(m--) {
    int u, v; cin >> u >> v; u--, v--;
    if (u > v) swap(u, v);
    if (u == 0) a[v] = true;
    if (v == n-1) b[u] = true;
  }
  for (int i = 1; i < n-1; i++) {
    if (a[i] && b[i]) return true;
  }
  return false;
}

int main() {
  if (solve()) cout << "POSSIBLE" << endl;
  else cout << "IMPOSSIBLE" << endl;
}

Measured memory_bytes: 256
#include <algorithm>  
#include <iostream>  
#include <sstream>  
#include <string>  
#include <cstring>
#include <vector>  
#include <queue>  
#include <set>  
#include <map>  
#include <cstdio>  
#include <cstdlib>  
#include <cctype>  
#include <cmath>  
#include <list>  
#include <cassert>
#include <ctime>
#include <climits>
using namespace std;  

#define PB push_back  
#define MP make_pair  
#define SZ(v) ((int)(v).size())  
#define FOR(i,a,b) for(int i=(a);i<(b);++i)  
#define REP(i,n) FOR(i,0,n)  
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)  
#define REPE(i,n) FORE(i,0,n)  
#define FORSZ(i,a,v) FOR(i,a,SZ(v))  
#define REPSZ(i,v) REP(i,SZ(v))  
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); }

const int MAXN=100000;
const int MOD=1000000007;

int n[2];
int x[2][MAXN];

int calc(int z) {
	ll sum=0; ll ret=0;
	REP(i,n[z]) {
		ret=(ret+(ll)i*x[z][i]-sum)%MOD;
		sum=(sum+x[z][i])%MOD;
	}
	ret%=MOD; if(ret<0) ret+=MOD; return ret;
}

void run() {
	scanf("%d%d",&n[0],&n[1]);
	REP(i,2) REP(j,n[i]) scanf("%d",&x[i][j]);
	int ret=(ll)calc(0)*calc(1)%MOD;
	printf("%d\n",ret);
}

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


Measured memory_bytes: 1024
import copy

def main():
    _data = copy.deepcopy(data)
    while 1 < len(_data):
        d1, v1 = _data[0]
        d2, v2 = _data[1]
        l = f1(v1, v2)
        v = l
        m1, m2 = list(f2(l, v1, v2))
        d = f1(d1*m1, d2*m2)
        for i in range(2): del _data[0]
        _data.append([d, v])
    else:
        f3(_data[0])

def f1(a, b):
    x, y = (a, b) if a >= b else (b, a)
    g = gcd(x, y)
    return lcm(x, y, g)
    
def f2(l, *s):
    for v in s:
        yield l/v
        
def f3(_data):
    d1, v1 = _data
    for d2, v2 in data:
        ans = str((float(d1)/v1)/(float(d2)/v2))
        print int(ans[:-2])
        
def gcd(x, y):
    while y != 0:
        x = x-(x/y)*y
        x, y = y, x
    else:
        return x
    
def lcm(x, y, g):
    return (x*y)/g

while True:
    n = int(raw_input())
    if n == 0: break
    data = [map(int, raw_input().split()) for i in range(n)]
    main()

Measured memory_bytes: 4336
import java.util.Scanner;

/**
 * Deque
 */
public class Main {

    public static void main(String[] args) throws Exception {
        try (Scanner sc = new Scanner(System.in)) {
            int N = sc.nextInt();
            long[] A = new long[N];
            for (int i = 0; i < N; i++) {
                A[i] = sc.nextLong();
            }

            long[][] dp = new long[N][N];

            for (int i = 0; i < N; i++) {
                if (N % 2 == 1) {
                    dp[i][i] = A[i];
                } else {
                    dp[i][i] = -A[i];
                }
            }

            for (int k = 1; k < N; k++) {
                for (int i = 0; i < N; i++) {
                    if (i + k >= N) {
                        break;
                    }

                    if ((N - k) % 2 == 1) {
                        dp[i][i + k] = Math.max(dp[i][i + k - 1] + A[i + k], dp[i + 1][i + k] + A[i]);
                    } else {
                        dp[i][i + k] = Math.min(dp[i][i + k - 1] - A[i + k], dp[i + 1][i + k] - A[i]);
                    }
                }
            }

            System.out.println(dp[0][N - 1]);
        }
    }

}


Measured memory_bytes: 129488
#include <iostream>
#include <set>
#include <map>
using namespace std;

int main() {
	int l;
	while(cin >> l && l){
		int n, m, sum = 0;
		bool flag = false;
		for(int i = 0; i < 12; ++i){
			cin >> m >> n;
			sum += (m - n);
			if(sum >= l && !flag){
				cout << i + 1 << endl;
				flag = true;
			}
		}
		if(!flag)
			cout << "NA" << endl;
	}
	return 0;
}

Measured memory_bytes: 1160
import java.util.Scanner;

public class Main {

	static Scanner scanner;

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

	    int N=gi();
	    int M=gi();
	    int[] S=new int[M];
	    int[] C=new int[M];
	    StringBuilder sb=new StringBuilder();
	    for (int i=0; i<N; i++) {
	    	sb.append("X");
	    }

	    for (int i=0; i<M; i++) {
	    	S[i]=gi();
	    	C[i]=gi();
	    	if(sb.charAt(S[i]-1) == 'X') {
	    		sb.setCharAt(S[i]-1, String.valueOf(C[i]).charAt(0));
	    	} else if (sb.charAt(S[i]-1) != String.valueOf(C[i]).charAt(0)) {
	    		System.out.println("-1");
	    		return;
	    	}
	    }

	    for (int i=0; i<N; i++) {
	    	if(sb.charAt(i) == 'X') {
	    		if (N!=1 && i==0) {
	    		    sb.setCharAt(i, '1');
	    		} else {
	    			sb.setCharAt(i, '0');
	    		}
	    	}
	    }

        //System.out.print(x);
	    String s=sb.toString();
        if (s.charAt(0)=='0' && N!=1) {
        	System.out.println(-1);
        }else {
        	System.out.println(s);
        }
	}

	// 文字列として入力を取得
	public static String gs() {
		return scanner.next();
	}

	// intとして入力を取得
	public static int gi() {
		return Integer.parseInt(scanner.next());
	}

	// longとして入力を取得
	public static long gl() {
		return Long.parseLong(scanner.next());
	}

	// doubleとして入力を取得
	public static double gd() {
		return Double.parseDouble(scanner.next());
	}
}

Measured memory_bytes: 35556
#include <algorithm>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <unordered_set>
#include <utility>
#include <vector>
using std::cin;
using std::cout;
using std::endl;

struct Node {
  std::vector<int> child;
  int parentcount = 0;
};

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

  Node node[100010] = {};
  int n, m;
  cin >> n >> m;
  int parent[100010] = {};
  for (int i = 1; i <= n - 1 + m; i++) {
    int m1, m2;
    cin >> m1 >> m2;
    node[m1].child.push_back(m2);
    node[m2].parentcount++;
  }

  std::queue<int> q;

  for (int i = 1; i <= n; i++) {
    if (node[i].parentcount == 0) {
      q.push(i);
      parent[i] = 0;
      break;
    }
  }

  while (!q.empty()) {
    int target = q.front();
    q.pop();
    for (auto x : node[target].child) {
      if (node[x].parentcount == 1) {
        q.push(x);
        parent[x] = target;
      } else {
        node[x].parentcount--;
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    cout << parent[i] << endl;
  }
  return 0;
}


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

int main() {

  vector<int> ABC(3);
  int K;
  cin >> ABC[0] >> ABC[1] >> ABC[2] >> K;

  sort(ABC.begin(), ABC.end(), std::greater<int>());

  for (int i = 0; i < K; i++) {
    ABC[0] *= 2;
  }

  cout << ABC[0] + ABC[1] + ABC[2] << endl;

}

Measured memory_bytes: 256
function Main(input){
    i=input.split("/").map(Number);
    if(i[0]<2019)console.log("Heisei");
    else if(i[0]===2019&&i[1]<4)console.log("Heisei")
    else if(i[0]===2019&&i[1]===4&&i[2]<=30)console.log("Heisei")
    else console.log("TBD")
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));

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

typedef long long ll;
typedef long double ld;

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

ll gcd(ll a, ll b){return b ? gcd(b, a % b) : a;}

int main(){

    string N;

    cin >> N;

    cout << (N[0] == '9' || N[1] == '9' ? "Yes" : "No") << endl;

    return 0;
}

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

int main()
{
    int x, y;
    cin >> x >> y;

    rep (i, x)
        if (2 * i == y - 2 * x)
        {
            cout << "Yes" << endl;
            return 0;
        }

    rep (i, x)
        if (2 * i == 4 * x - y)
        {
            cout << "Yes" << endl;
            return 0;
        }

    cout << "No" << endl;
    return 0;
}

Measured memory_bytes: 3636
#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <cmath>
#include <utility>

using namespace std;

int main()
{
	int h, w;
	cin >> h >> w;
	for (int i = 0; i < h; i++)
	{
		vector<char> symbols(w);
		for (int j = 0; j < w; j++)
		{
			cin >> symbols[j];
		}
		for (int j = 0; j < w; j++)
		{
			cout << symbols[j];
		}
		cout << endl;
		for (int j = 0; j < w; j++)
		{
			cout << symbols[j];
		}
		cout << endl;
	}
}

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

int gcd(int a, int b){ //a>=0,b>=0,￢(a=b=0)
  while(min(a,b)>0) {if(a<b) swap(a,b); a = a%b;} return max(a,b);
}

int main(){
  int n;
  cin>>n;
  string s;
  cin >> s;
  rep(i,s.length()){
    s[i]='A'+(s[i]-'A'+1+n)%26-1;
    if(s[i]=='@') s[i] = 'Z';
  }

  cout << s << endl;;
 
  return 0;
}

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

using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
signed main(){
  int h;cin>>h;
  int k=0;
  int buf=h;
  while(buf>0){
    buf/=2;
    k++;
  }
  int res=1;
  while(k>0){
    k--;
    res*=2;
  }
  res--;
  cout<<res<<endl;
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std; 
 
#define NOT_FOUND -1
#define rep(i,n) for(int i=0;i<(int)(n); i++)
/*for文のマクロ定義
rep(i,回数){
    処理
}
*/
 
void swap(int *a, int *b){
	*b=*a-*b;
	*a-=*b;
	*b+=*a;
}
 
int sosu_judge(int N){
	/*素数ならば1
	  素数でなければ0を返す
	*/
	int i,j;
 
	if(N==2)   return 1;
	if(N%2==0) return 0;
 
	for(i=3;i<N && N%i!=0;i+=2);
 
	if(i>=N) return 1;
	
	return 0;
}
 
int factorial(int N){  //Nの階乗を求める関数
	int ans=1;
 
	while(N--) ans*=(N+1);
	return ans;
}
 
//クイックソートをする関数
void q_sort(int a[], int first, int last){
    int i,j;
    int x,t;
 
    x = a[(first + last) / 2];
    i = first; j=last;
 
    while(1){
        while( a[i] < x) i++;
        while( x < a[j]) j--;
        
        if( i >= j) break;
        
        swap(&a[i],&a[j]);
        i++; j--;
    }
    if(first < i-1) q_sort(a, first,i-1 );
    if(last > j+1)  q_sort(a, j+1,  last);
}
 
int b_search(int target, int array[],int right, int left){
    /*二分探索を行う関数である。
      戻り値は探索したい値の格納されている位置である。
      もし値が存在しない場合、NOT_FOUND(-1)を返す
    */
    int mid;
    while(left<right){
        mid=(right+left)/2;
        if(array[mid]<target) left =mid+1;
        else                  right=mid;
    }
    if(array[mid]==target) return mid;
    return NOT_FOUND;
}
 
unsigned long combination(int n, int r){
    /*nCrの値を求める関数である。
      値が巨大になる可能性があるため、戻り地はunsigned longである
    */
    int i,j;
    unsigned long a[17];
 
    if(n-r<r) r=n-r;
    if(r==0 ) return 1;
    if(r==1 ) return n;
    if(r>17 ) return 0;
    for(i=1;i<r;i++) a[i]=i+2;
    for(i=3;i<=n-r+1;i++){
        a[0]=i;
        for(j=1;j<r;j++)a[j]+=a[j-1];
    }
    return a[r-1];
}

int main(){
    long long N,K,i,ans;
    vector<long long> dp;

    cin>>N>>K;

    if(N%K==0){
        cout<<"0"<<endl;
        return 0;
    }
/*
    dp.push_back(N);

    for(i=0;;i++){
        dp.push_back(min(dp[i],llabs(N-K)));
        if(dp[i+1]==dp[i]) break;
        N-=K;
    }
*/
    ans=N%K;
    ans=min(ans,llabs(K-ans));
    cout<<ans<<endl;
    
    
//    cout<<dp[i]<<endl;
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
using namespace std;
void watch(int);
int main() {
  int S;
  cin >> S;
  watch(S);
}
void watch(int S) {
  int h, m, s;
  h = S / 3600;
  m = (S % 3600) / 60;
  s = (S % 3600) % 60;
  cout << h << ":" << m << ":" << s << endl;
}

Measured memory_bytes: 1160
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		// 整数の入力
		int n = sc.nextInt();
		// スペース区切りの整数の入力
		List<Integer> a = new ArrayList<Integer>();
		for (int i = 0; i < n; i++) {
			a.add(sc.nextInt());
		}
		Collections.sort(a);

		int outCounter = 0;
		int tmp = 0;
		int now = 0;
		for (Integer i : a) {
			if (now != i) {
				if (now < tmp) {
					outCounter = outCounter + tmp - now;
				}else if (now > tmp) {
					outCounter = outCounter + tmp;
				}

				now = i;
				tmp = 0;
			}
			tmp++;
		}
		if (now < tmp) {
			outCounter = outCounter + tmp - now;
		}else if (now > tmp) {
			outCounter = outCounter + tmp;
		}

		System.out.println(outCounter);
	}
}


Measured memory_bytes: 58952
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    vector<long double> v(n);
    long double sum=0;
    for(int i=0;i<n;i++){
        cin>>v[i];
        v[i]=1/v[i];
        sum+=v[i];
    }
    
    cout<<setprecision(10)<<1/sum;
    return 0;
}

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

int main(void){
    
    int N,max=0;
    cin >> N;
    int data[2][N];
    
    for(int i=0;i<N;i++) cin >> data[0][i];
    for(int i=0;i<N;i++) cin >> data[1][i];
    
    for(int i=0;i<N;i++){
        int num = 0;
        int key = 0;
        bool flag = true;
        for(int j=0;j<N;j++){
            num += data[key][j];
            if(j == i && flag){
                key++;
                j--;
                flag = false;
            }
        }
        if(max < num) max = num;
    }
    cout << max << endl;
}

Measured memory_bytes: 3568
# -*- coding: utf-8 -*-

N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
BC = [list(map(int, input().split())) for i in range(M)]

A2 = [[a, 1] for a in A]
for bc in BC:
    A2.append([bc[1], bc[0]])

A2.sort(reverse=True)

count = N
ans = 0
for a2 in A2:
    if count > a2[1]:
        ans += a2[0]*a2[1]
        count -= a2[1]
    else:
        ans += a2[0]*count
        break
        
print(ans)

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

using namespace std;

typedef long long ll;
typedef long double ld;


int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n,a,b;
    cin>>n>>a>>b;
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        int x,y;
        cin>>x>>y;
        if(x>=a && y>=b)
        {
            ans++;
        }
    }
    cout<<ans<<"\n";
    return 0;
}
/**

**/


Measured memory_bytes: 256
import sys
from collections import Counter
readline = sys.stdin.readline
MOD = 10**9+7
N = int(readline())

CC = Counter()
CC[0] = 1
CC[1] = 2
def calc(x):
    res = 0
    if CC[x]:
        res = CC[x]
    elif x == 1:
        res = 2
    elif x%2 == 0:
        res = 2*calc(x//2-1) + calc(x//2)
    else:
        res = 2*calc(x//2) + calc(x//2-1)
    CC[x] = res%MOD
    return res%MOD
print(calc(N))

Measured memory_bytes: 3316
#include<cmath>
#include<algorithm>
#include<iostream>
#include<vector>
#include<climits>

using namespace std;

double EPS = 1e-10;
const double PI = acos(-1);

double add(double a, double b){
  if(abs(a+b) < EPS * (abs(a)+abs(b)))return 0;
  return a+b;
}

struct point{
  double x, y;
  point(){}
  point(double x,double y) : x(x) , y(y){}

  point operator + (point p){
    return point(add(x,p.x), add(y,p.y));
  }

  point operator - (point p){
    return point(add(x,-p.x), add(y,-p.y));
  }

  point operator * (double d){
    return point(x*d,y*d);
  }

  point operator / (double d){
    return point(x/d,y/d);
  }
  
  bool operator == ( const point &p ) const {
    return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;
  }
};

struct segment{
  point a,b;
};

double cross(point a, point b) {
  return (a.x * b.y - a.y * b.x);
}

double dist(point a,point b){
  return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
}

int is_intersected_ls(point a1, point a2, point b1, point b2) {
  return ( cross(a2-a1, b1-a1) * cross(a2-a1, b2-a1) < EPS ) &&
         ( cross(b2-b1, a1-b1) * cross(b2-b1, a2-b1) < EPS );
}

point intersection_ls(point a1, point a2, point b1, point b2) {
  point b = b2-b1;
  double d1 = abs(cross(b, a1-b1));
  double d2 = abs(cross(b, a2-b1));
  double t = d1 / (d1 + d2);

  return a1 + (a2-a1) * t;
}

int main(void){
  segment s[3];

  while(true){
    for(int i=0;i<3;i++){
      cin >> s[i].a.x >> s[i].a.y >> s[i].b.x >> s[i].b.y;
      if( s[i].a.x==0 && s[i].a.y==0 && s[i].b.x==0 && s[i].b.y==0)return 0;
      }
    
    int fg[3]={0};
    for(int i=0;i<3;i++){
      for(int j=0;j<3;j++){
	if(i!=j)
	  fg[i]+=is_intersected_ls(s[i].a,s[i].b,s[j].a,s[j].b);
      }
    }

    if(fg[0]==2 && fg[1]==2 && fg[2]==2){
      point p[3];
      for(int i=0;i<2;i++)
	    p[i]=intersection_ls(s[i].a,s[i].b,s[i+1].a,s[i+1].b);
      p[2]=intersection_ls(s[2].a,s[2].b,s[0].a,s[0].b);

      double z=(dist(p[0],p[1])+dist(p[1],p[2])+dist(p[2],p[0]))/2;
      double res=sqrt(z*(z-dist(p[0],p[1]))*(z-dist(p[1],p[2]))*(z-dist(p[2],p[0])));

      if(res>=1900000)cout <<"dai-kichi"<<endl;
      else if(res>=1000000 && res <1900000)cout <<"chu-kichi"<<endl;
      else if(res>=100000 && res<1000000)cout <<"kichi"<<endl;
      else if(res>0 && res<100000)cout << "syo-kichi"<<endl;
      else cout <<"kyo"<<endl;
    }
    else cout <<"kyo"<<endl;
  }
  return 0;
}

Measured memory_bytes: 0
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define vi vector<int>
#define pb push_back
#define pi pair<int,int>
#define vp vector<pair<int,int>>
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define MEMSET(a) memset(a,0,sizeof(a))
#define inf (1ll<<60)
#define Yes(f) cout<<(f?"Yes":"No")<<endl
#define yes(f) cout<<(f?"yes":"no")<<endl
#define YES(f) cout<<(f?"YES":"NO")<<endl
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v), greater<int>())

using namespace std;

const int mod=1e9+7;

void run();

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

signed main(){
    init();
    run();
    return 0;
}

void run(){
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    cout<<max(0ll,min(b,d)-max(a,c))<<endl;
}

Measured memory_bytes: 256