akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,a,n) for(int i=(a); i<(n); i++)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9+7;

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

    ll n; cin >> n;
    vector<ll> vec(n);
    map<ll, pair<ll, ll>> mp;
    ll total = 0;
    rep(i, n) {
        ll x; cin >> x;
        total -= mp[x].second;
        vec[i] = x;
        ll num = mp[x].first+1;
        mp[x] = make_pair(num, num*(num-1)/2);
        total += mp[x].second;
    }
    rep(i, n) {
        ll num = vec[i];
        ll ans = total;
        ans -= mp[num].second;
        ans += (mp[num].first-1)*(mp[num].first-2)/2;
        cout << ans << endl;
    }
}


Measured memory_bytes: 11136
#include <cstdio>
#include <complex>
#include <iostream>
using namespace std;

double cross(complex<double> z1, complex<double> z2) {
	return z1.real() * z2.imag() - z1.imag() * z2.real();
}

int main() {
	double x1, y1, x2, y2, x3, y3, x4, y4;
	while (scanf("%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4) != EOF) {
		complex<double> a(x1, y1), b(x2, y2), c(x3, y3), d(x4, y4);

		double ab, bc, cd, da;
		ab = cross(b - a, c - b);
		bc = cross(c - b, d - c);
		cd = cross(d - c, a - d);
		da = cross(a - d, b - a);

		if ((ab > 0 && bc > 0 && cd > 0 && da > 0) || (ab < 0 && bc < 0 && cd < 0 && da < 0)) {
			cout << "YES";
		}else {
			cout << "NO";
		}
		cout << endl;
	}
	return 0;
}

Measured memory_bytes: 3220
#include "bits/stdc++.h"
using namespace std;
#define INF 1e9
#define rep(i, n) for(int i=0;i<n;i++)
#define ALL(x) (x).begin(), (x).end()  //昇順
#define RALL(x) (x).rbegin(), (x).rend() // 降順
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
const long long mod = 1e9+7;
typedef long long ll;  //ll とdoubleは違う
typedef priority_queue<int> PQ;
typedef vector<long long> VL;
typedef vector<bool> VB;
typedef vector<int> VI;  // VI a(n);
typedef vector<double> VD;  
typedef vector<string> VS;  
typedef vector<char> VC; 
typedef vector<VS> VSS;
typedef vector<VC> VCC;    
typedef vector<VI> VII;   // VII a(n,vector<int>(m)) n * m 
typedef vector<VL> VLL;
typedef pair<int, int> PII;
typedef map<int, int> MP;   //MP a;
typedef vector<pair<ll, ll> > PS;
template <class T, class U>  // chmax(max, a);
bool chmax(T &a, U b){
    if (a < b){ a = b;return 1;}
    return 0;
}
template <class T, class U> //chmin(min,a)
bool chmin(T &a, U b){
    if (a > b){ a = b; return 1;}
    return 0;
}
template<typename T>   // operator << (cout,a);
ostream& operator << (ostream& os, vector<T>& v) {
    os << "{";
    rep(i, (int)v.size()) { os << v[i] << (i < v.size() - 1 ? ", " : ""); }
    os << "}";
    return os;
}
// 素因数分解 O(√n)以下
// (素因数,個数)
template<typename T>
vector<pair<T, int>> prime_factorization(T n) {
    vector<pair<T, int>> res;
    for (T i = 2; i*i <= n; i++) {
        int cnt = 0;
        while (n % i == 0) {
            n /= i;
            cnt++;
        }
        if (cnt > 0) res.push_back({i,cnt});
    }
    if (n > 1) res.push_back({n,1});
    return res;
}

// g++ -std=c++11 prac.cpp 
int main(){
    ll x,y,z,n,m=0,k,h,w,sum=0,ans=0,Max=-1e9,count=0,Min=1e9+1;
    string s,t,u;
    bool ok=true;
    cin >> n;
    int v=1e9;
    x = (v - n%v)%v; 
    y = (n+x)/v;
    cout<<"0 0 1000000000 1 "<< x <<" "<< y <<endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <random>

using namespace std;
 
#define mod 1000000007
#define FOR(x,to) for(int x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define long long long
inline int rei(){int x;cin>>x;return x;}
inline long rel(){long x;cin>>x;return x;}
inline string res(){string x;cin>>x;return x;}
//------------------------------------------------------- 
void Calc(){
	int N = rei();
	for(int i=0;i<N-1;i++){
		for(int j=i+1;j<N;j++){
			int k = 0;
			while(1){
				if(((1 << k) & i) != 0 && ((1 << k) & j) == 0){
					cout << k+1 << " ";
					break;
				}
				if(((1 << k) & i) == 0 && ((1 << k) & j) != 0){
					cout << k+1 << " ";
					break;
				}
				k++;
			}
		}
		cout << "\n";
	}
}
int main(int argc,char** argv){
	ios::sync_with_stdio(false), cin.tie(0);
	cout.tie(0); Calc(); return 0;
}

Measured memory_bytes: 512
#include <iostream>
#include <map>
#include <cmath>
#include <iomanip>


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

using namespace std;

typedef long long ll; 



int main(){

    int N,K;
    scanf("%d %d",&N,&K);

    map<int,double> mp;

    int r;

    for(int i=0;pow(2,i)<K;++i){

        if(K%((int)pow(2,i))==0){
            r=K/pow(2,i)-1;
        }else{
            r=K/pow(2,i);
        }
        mp.insert(std::make_pair(r,1/pow(2,i+1)));
    }
    if(N>=K){
         mp.insert(make_pair(N,1));

    }
   /*
    if(N>=K){
    mp.insert(std::make_pair(N,1.0));
    }
    for(map<int,double>::iterator it=mp.begin();it!=mp.end();++it){
        //cout<<it->first<<":"<<it->second<<endl;

    }*/

    double sum=0;
    int prev=0;

    for(map<int,double>::iterator it=mp.begin();it!=mp.end();++it){
        //cout<<it->first<<":"<<it->second<<endl;
        if((it->first)<=N){

            double a=(1.0/N)*(it->first-prev)*(it->second);
          //  cout<<"a:"<<a<<endl;
            sum+=a;
            

            prev=(it->first);
            //cout<<prev<<endl;

        
        }else{
            double b=(1.0/N)*(N-prev)*(it->second);
            //cout<<"b:"<<b<<endl;

            sum+=b;
            break;
            
        }


    }
    cout<<std::fixed<<std::setprecision(14)<<sum<<endl;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <cmath>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <iostream>
#include <random>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include <fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
#include <cctype>
#include <bitset>
using namespace std;

using ll = long long;
using vll = vector<long long>;
using pll = pair<long long, long long>;
#define rep(i,n) for(long long i(0);(i)<(n);(i)++)


void solve(string S, long long K){
    char ans;
    rep(i, S.length()){
        if(S.at(i)=='1') 
            K--;
        else
        {
            ans = S.at(i);
            break;
        }
    }
    if(K>0)
        cout << ans << endl;
    else
    {
        cout << 1 << endl;
    }
    

}

int main(){
    string S;
    cin >> S;
    long long K;
    scanf("%lld",&K);
    solve(S, K);
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin>>n;
  string s;
  cin>>s;
  string t=s;
  sort(s.begin(),s.end());
  int cnt=0;
  for(int i=0;i<s.size();i++){
    if(s[i]!=t[i])cnt++;
  }
  cout<<cnt/2<<endl;
  return 0;
}

Measured memory_bytes: 3828
//
//  main.cpp
//  sss
//
//  Created by on 2019/09/04.
//  Copyright © 2019 . All rights reserved.
//

#include <iostream>


using namespace std;

int main(){
    char b;
    cin >> b;
    if (b == 'A'){
        cout << 'T' << endl;
    }
    if (b == 'T'){
        cout << 'A' << endl;
    }
    if (b == 'C'){
        cout << 'G' << endl;
    }
    if (b == 'G'){
        cout << 'C' << endl;
    }
    }




Measured memory_bytes: 256
#include <stdio.h>
#include <iostream>
#include <set>
#include <utility>
using namespace std;
#define F first
#define S second
int main(){
    int n;
    string s;
    set<string> dic;
    scanf("%d",&n);
    while(n--){
        cin>>s;
        if(s[0]=='i'){
            cin>>s;
            dic.insert(s);
        }
        else{
            cin>>s;
            if(dic.find(s)==dic.end())printf("no\n");
            else printf("yes\n");
        }
    }
}


Measured memory_bytes: 32664
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const int inf = 1<<30;
const LL maxn = 1e5+10;

LL a[maxn], b[maxn], n;
int main()
{
    int n;
    cin >> n;
    for(int i = 1; i <= n; ++i){
        cin >> a[i];
        if(i&1) b[1] += a[i];
        else b[1] -= a[i];
    }
    for(int i = 2; i <= n; ++i)
        b[i] = 2*a[i-1]-b[i-1];
    for(int i = 1; i <= n; ++i){
        if(i==1) cout << b[i];
        else cout << " " << b[i];
    }
    cout << endl;

	return 0;
}



Measured memory_bytes: 2816
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define puts(i) cout<<i<<endl
#define pi 3.14159265358979323846264338
#define pb push_back
#define mp make_pair
#define inf 10000
using ll = long long;
using P = pair<int,int>;
using Pll = pair<ll,ll>;

ll divisor=1000000007;

int main(){
	string s;
	cin>>s;
	rep(i,size(s)){
		if(s[i]=='?'){
			s[i]='D';
		}
	}
	int a=0;
	rep(i,size(s)){
		if(i==0&&s[0]=='D')a++;
		if(s[i]=='D'){
			a++;
			if(s[i-1]=='P')a++;
		}
	}
	puts(s);
}

Measured memory_bytes: 3748
import java.util.*;

public class Main {

    public static void main(String[] args) {
        // write your code here
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        int ans = 0;

        for (int i = 0; i < n; i++) {
            ans += sc.nextInt() - 1;
        }

        System.out.println(ans);
    }
}

Measured memory_bytes: 25968
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct rest
{
	int n;
	string name;
	int score;
};

bool cmp(rest a, rest b)
{
	if (a.name == b.name)
	{
		return a.score > b.score;
	}
	else
	{
		return a.name < b.name;
	}
}

int main()
{
	int N;
	cin >> N;
	
	vector<rest> res;

	char buff[32];

	for (int i = 0; i < N; i++)
	{
		int p;
		cin >> buff >> p;
		string s(buff);

		rest r = { i+1, s, p };
		res.push_back(r);
	}

	sort(res.begin(), res.end(), cmp);

	for (int i = 0; i < N; i++)
	{
		cout << res[i].n << endl;
	}

	return 0;
}



Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1061109567;
const ll LINF = 4557430888798830399ll;

/*---*/

int a[5];

void TestCase() {
    for(int i = 1; i <= 3; ++i)
        scanf("%d", &a[i]);
    sort(a + 1, a + 1 + 3);
    if(a[1] != 5 || a[2] != 5 || a[3] != 7)
        puts("NO");
    else
        puts("YES");
}

int main() {
#ifdef KisekiPurin
    freopen("KisekiPurin.in", "r", stdin);
    //freopen("KisekiPurin.out", "w", stdout);
#endif // KisekiPurin
    //InitCase();
    int t = 1;
    //scanf("%d", &t);
    for(int i = 1; i <= t; ++i) {
        //printf("Case #%d:", i);
        TestCase();
    }
    return 0;
}


Measured memory_bytes: 256
import sys

def input():
    return sys.stdin.readline().rstrip()

class Node:
    def __init__(self, value, prev_node, next_node):
        self.value = value
        self.prev_node = prev_node
        self.next_node = next_node

class LinkedList:
    def __init__(self):
        self.current_node = Node(None, None, None)
        self.front_node = self.current_node
        self.end_node = self.current_node

    def insert(self, value):
        current_node = self.current_node
        prev_node = current_node.prev_node
        if prev_node == None:
            newNode = Node(value, None, current_node)
            current_node.prev_node = newNode
            self.current_node = newNode
            self.front_node = newNode
        else:
            newNode = Node(value, prev_node, current_node)
            prev_node.next_node = newNode
            current_node.prev_node = newNode
            self.current_node = newNode

    def remove(self):
        prev_node = self.current_node.prev_node
        next_node = self.current_node.next_node
        if prev_node == None:
            next_node.prev_node = None
            self.front_node = next_node
            self.current_node = next_node
        else:
            next_node.prev_node = prev_node
            prev_node.next_node = next_node
            self.current_node = next_node

    def move(self, diff):
        if diff > 0:
            for _ in range(diff):
                self.current_node = self.current_node.next_node
        elif diff < 0:
            for _ in range(-diff):
                self.current_node = self.current_node.prev_node

    def print(self):
        node = self.front_node
        while node != self.end_node:
            print(node.value)
            node = node.next_node

input()
a = LinkedList()
while True:
    line = input()
    if not line:
        break
    if line[0] == '0':
        a.insert(int(line[2:]))
    elif line[0] == '1':
        a.move(int(line[2:]))
    else:
        a.remove()

a.print()



Measured memory_bytes: 89072
#include <bits/stdc++.h>
#define ll long long
using namespace std ;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0) ;
    cout.tie(0) ;
    long long yan , show ;
    cin >> yan ;
    show = (ceil(yan/1000.0) * 1000) - yan;
    cout << show << "\n" ;
    return 0 ;
}


Measured memory_bytes: 3648
#include <bits/stdc++.h>
//#define int long long

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

#define FOR(i, a, n) for(int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)

#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()

const int INF = (int)1e9;
const LL INFL = (LL)1e15;
const int MOD = 1e9 + 7;

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

int n, m, l, k, a, h;
vector<vector<P>> graph;
vector<vector<int>> dist;
vector<bool> isFreeze;

void dijkstra(int firstNode) {
    using Tup = tuple<int, int, int>;
    priority_queue<Tup, vector<Tup>, greater<Tup>> pq;

    pq.push(make_tuple(0, firstNode, m));

    while (!pq.empty()) {
        int cost, currentEdge, t;
        tie(cost, currentEdge, t) = pq.top(); pq.pop();
        if (dist[currentEdge][t] < cost) continue;

        for (auto tmp : graph[currentEdge]) {
            int nextEdge = tmp.first, time = tmp.second;
            if(isFreeze[currentEdge]){
                for(int i = 0; i <= m; i++){
                    if(t + i > m) break;
                    if(t + i - time < 0) continue;
                    if(cost + i + time < dist[nextEdge][t + i - time]){
                        dist[nextEdge][t + i - time] = cost + i + time;
                        pq.push(make_tuple(dist[nextEdge][t + i - time], nextEdge, t + i - time));
                    }
                }
            }else{
                if(t - time < 0) continue;
                if(cost + time < dist[nextEdge][t - time]){
                    dist[nextEdge][t - time] = cost + time;
                    pq.push(make_tuple(dist[nextEdge][t - time], nextEdge, t - time));
                }
            }
        }
    }
}

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

    while(1){
        cin >> n >> m >> l >> k >> a >> h;
        if(n+m+l+k+a+h == 0) break;
        dist.assign(105, vector<int>(105, INF));
        graph.resize(105);
        isFreeze.assign(105, false);
        REP(i, l){
            int x;
            cin >> x;
            isFreeze[x] = true;
        }
        REP(i, k){
            int x, y, t;
            cin >> x >> y >> t;
            graph[x].pb(P(y, t));
            graph[y].pb(P(x, t));
        }
        
        dijkstra(a);
        int ans = INF;
        REP(i, m+1) ans = min(ans, dist[h][i]);
        if(ans == INF) cout << "Help!" << endl;
        else cout << ans << endl;
        REP(i, 105) graph[i].erase(all(graph[i]));
    }
}



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

#define int long long
#define fast_io(); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define loop(i,a,b) for(int i=a;i<b;i++)
#define all(x) x.begin(),x.end()
#define us unordered_set
#define um unordered_map
#define ms multiset
#define mm multimap
#define pb push_back
#define pf push_front
#define ins insert
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define _max max_element
#define _min min_element
#define tc(T) int T; cin>>T; while(T--)
#define q(q) int q; cin>>q; while(q--)

const int pinf=((int)2e18);
const int ninf=((int)-2e18);
const int mod=1000000007;

using namespace std;

int power(int x,int n,int M)
{
    if(n==0)
        return 1;
    else if(n%2 == 0)
        return power((x*x)%M,n/2,M);
    else
        return (x*power((x*x)%M,(n-1)/2,M))%M;
}

bool check_prime(int n)
{
    if(n<2)
        return false;
    for(int i=2;i<=sqrt(n);i++)
    {
        if(n%i==0)
            return false;
    }
    return true;
}

int setbits(int n)
{
    int count=0;
    while(n)
    {
        if(n%2==1)
            count++;
        n>>=1;
    }
    return count;
}

int lcm(int a,int b)
{
    return (a*b)/__gcd(a,b);
}

vector <pair <int,int> > adj[100000+5];
vector <bool> vis(100000+5);
vector <int> a(100000+5);
int c;
vector <int> dist(100000+5);
int n,x,y,w;

void dijkstra(int src)
{
    dist[src]=0;

    // First is distance, second is node index
    multiset <pair <int,int> > ms;
    ms.insert(make_pair(0,src));

    while(not ms.empty())
    {
        pair <int,int> p=(*ms.begin());
        ms.erase(ms.begin());

        // If the best distance of the node is greater than the distance inside queue then skip
        if(dist[p.second]<p.first)
            continue;
        for(int i=0;i<adj[p.second].size();i++)
        {
            if(dist[adj[p.second][i].first]>dist[p.second]+adj[p.second][i].second)
            {
                dist[adj[p.second][i].first]=dist[p.second]+adj[p.second][i].second;
                ms.insert(make_pair(dist[adj[p.second][i].first],adj[p.second][i].first));
            }
        }

    }
}

int32_t main()
{
    fast_io();

    //FILE *fptr1=freopen("in.txt","r",stdin);
    //FILE *fptr2=freopen("out.txt","w",stdout);
    cin>>n;
    vector <int> deg(n+1);
    loop(i,1,n+1) a[i]=-1;

    loop(i,0,n-1)
    {
        cin>>x>>y>>w;
        adj[x].pb(mp(y,w));
        adj[y].pb(mp(x,w));
        deg[x]++;
        deg[y]++;
    }
    vector <pair <int,int> > v;
    loop(i,1,n+1)
    {
        v.pb(mp(deg[i],i));
    }
    sort(all(v));

    int leaf=v[0].se;

    loop(i,1,n+1)
    dist[i]=pinf;
    dijkstra(leaf);

    loop(i,1,n+1)
    {
        a[i]=dist[i]%2;
    }



    loop(i,1,n+1) cout<<a[i]<< "\n";



    return 0;
}


Measured memory_bytes: 12916
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n = int(input())
A = [int(input()) for i in range(n)]
AS=sorted(A)
C = collections.Counter()
for i,ai in enumerate(AS):
  C[ai] = i
odd = 0
even = 0
for i,ai in enumerate(A):
  if C[ai] % 2 != i % 2:
    # print(C[ai],i)
    if i % 2 == 0:
      odd += 1
    else:
      even += 1
m = min(odd,even)
odd -= m
even -= m
count += m
count += max(odd,even)
# print(C.items())
print(count)

Measured memory_bytes: 88268
#include <bits/stdc++.h>
#define PI 3.1415926535897
using namespace std;
typedef long long ll;
const int INF = 1000000000;
const ll LINF = 1000000000000000000; //1e18
const double EPS = 1e-10;

int main(void)
{
    int N, L;
    cin >> N >> L;

    vector<string> S(N);
    for (int i = 0; i < N; i++)
    {
        cin >> S[i];
    }
    sort(S.begin(), S.end());
    for (int i = 0; i < N; i++)
    {
        cout << S[i];
    }
    printf("\n");
    return (0);
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i < (n); i++)
#define RREP(i, s, n) for (int i = s; i < (n); i++)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
using namespace std;
using ll = long long;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007;
const ll INF = MOD * MOD;
const int inf = (1<<29);

int main() {

    string S;
    cin >> S;
    
    int cnt = 0;
    for(int i = 0; i < S.size(); i++) {
        if (S[i] == 'p') cnt ++;
    }

    cout << S.size() / 2 - cnt << endl;


    return 0;
}

Measured memory_bytes: 512
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n =  Integer.parseInt(sc.next());
		int k =  Integer.parseInt(sc.next());
		
		int ans = 0;
		for (int i = 0 ; i < n ; i++) {
			if(k <= Integer.parseInt(sc.next())) {
				ans++;
			}
			
		}
		
		System.out.println(ans);
	}



}

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

using namespace std;
#define IOS cin.sync_with_stdio(0); cin.tie(0); cout.tie(0);

using ll = long long;
using ld = long double;

#define pb push_back
#define pii pair<int,int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

const int mod = 1e9+7;


const int N = 1e5+ 10;

vector<int> g[N];
int in_deg[N], dist[N];
bool visited[N];

void dfs(int v){
    visited[v] = true;

    for(int c : g[v]){
        dist[c] = max(dist[c], dist[v] + 1);
        --in_deg[c];
        if(in_deg[c] == 0){
            dfs(c);
        }
    }
}


int main(){
    IOS

    int n, m; cin>>n>>m;

    for(int i = 0; i < m; i++){
        int x, y; cin>>x>>y; 
        g[x].pb(y);
        ++in_deg[y];
    }

    for(int i = 1; i <= n; i++){
        if(!visited[i] && in_deg[i]==0)
            dfs(i);
    }

    int ans = 0;
    for(int i = 1; i <= n; i++){
        ans = max(ans, dist[i]);
    }


    cout<<ans<<"\n";



    
}


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

#define F first
#define S second
#define MP make_pair
#define PB emplace_back

using namespace std;

const int N = 1e6+100;

long long int wop(long long int x, long long int y) {
    long long int ret=1;
    for(;y>=1;y/=2) {
        if(y&1)ret*=x;
        x*=x;
    }
    return ret;
}

bool PA(string s) {
    for(int i = 0 ; i < s.size() ; i++ ) {
        if(s[i]!=s[s.size()-i-1])return 0;
    }
    return 1;
}

int main () {
    string s;
    cin >> s;
    string s1, s2;
    s1.clear();
    s2.clear();
    for(int i = 0 ; i < s.size()/2 ; i++ ) {
        s1=s1+s[i];
    }
    for(int i = (s.size()/2)+1 ; i < s.size() ; i++ ) {
        s2=s2+s[i];
    }
    if(PA(s)&PA(s1)&PA(s2))cout << "Yes";
    else cout << "No";
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
    int d[5], k = 0, i = 0;
    for(i = 0; i < 5; i++){
        cin >> d[i];
    }
    cin >> k;
    if(d[4]-d[0] > k){
        cout << ":(" << endl; 
    }
    else{
        cout << "Yay!" << endl;
    }
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define allOf(x) x.begin(), x.end()
typedef std::pair<int,int>pii;
const int MN=2004,MM=2004;
int N,M,ans;
char S[MM];
std::vector<pii>X,Y;
int main() {
  scanf("%d%d",&N,&M);
  for(int i=0;i<N;++i) {
	scanf("%s",S);
    for(int j=0;S[j];++j) {
      if(S[j]=='#') {
      	X.emplace_back(i,j);
        Y.emplace_back(j,i);
      }
    } 
  }
  std::sort(allOf(X));
  std::sort(allOf(Y));
  for(int i=0;i<N;++i) {
   	for(int j=0;j<M;++j) {
     	// binary search
      	auto it1 = std::lower_bound(allOf(X), std::make_pair(i, j));
      	if(it1 != X.end() && *it1 == std::make_pair(i, j)) continue;
      	int x0 = 0;
      	if(it1 == X.begin() || std::prev(it1)->first!=i) x0 = -1;
      	else x0 = std::prev(it1)->second;
      	int x1 = N-1;
      	if(it1 == X.end() || it1->first!=i) x1 = M;
      	else x1 = it1->second;
      	auto it2 = std::lower_bound(allOf(Y), std::make_pair(j, i));
      	if(it2 != Y.end() && *it2 == std::make_pair(j, i)) continue;
      	int y0 = 0;
      	if(it2 == Y.begin() || std::prev(it2)->first!=j) y0 = -1;
      	else y0 = std::prev(it2)->second;
      	int y1 = M-1;
      	if(it2 == Y.end() || it2->first!=j) y1 = N;
      	else y1 = it2->second;
        // printf("We are at %d %d with %d %d %d %d\n", i, j, x0, x1, y0, y1);
      	ans = std::max(ans, y1-y0 + x1-x0 - 3);
        // printf("We are at %d %d with ans = %d\n", i, j, ans);
    }
  }
  printf("%d\n",ans);
}

Measured memory_bytes: 64836
#include <iostream>
#include <math.h>
#include <stack>
#include <vector>
#include <algorithm>

using namespace std;
#define n_max 100

int main()
{
	int n;
	int A[n_max];
	int ans, diff;

	cin >> n;

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

	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
			
			if(i == j) { continue; }
			diff = abs(A[i] - A[j]); 
			ans = max(ans, diff);
		}
	}

	cout << ans << endl;

    return 0;
}


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

using namespace std;

#ifdef SG
	#include <debug.h>
#else
	#define show(...)
	#define debug(...)
	#define deepen(...)
	#define timer(...)
#endif

#define ARG4(_1,_2,_3,_4,...) _4

#define forn3(i,l,r) for (int i = int(l); i < int(r); ++i)
#define forn2(i,n) forn3 (i, 0, n)
#define forn(...) ARG4(__VA_ARGS__, forn3, forn2) (__VA_ARGS__)

#define ford3(i,l,r) for (int i = int(r) - 1; i >= int(l); --i)
#define ford2(i,n) ford3 (i, 0, n)
#define ford(...) ARG4(__VA_ARGS__, ford3, ford2) (__VA_ARGS__)

#define ve vector
#define pa pair
#define tu tuple
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define fs first
#define sc second
#define all(a) (a).begin(), (a).end()
#define sz(a) ((int)(a).size())

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef unsigned char uc;
typedef pa<int, int> pii;
typedef pa<int, ll> pil;
typedef pa<ll, int> pli;
typedef pa<ll, ll> pll;
typedef ve<int> vi;

const ld pi = 3.1415926535897932384626433832795l;

template<typename T> inline auto sqr (T x) -> decltype(x * x) {return x * x;}
template<typename T1, typename T2> inline bool umx (T1& a, T2 b) {if (a < b) {a = b; return 1;} return 0;}
template<typename T1, typename T2> inline bool umn (T1& a, T2 b) {if (b < a) {a = b; return 1;} return 0;}

int main () {
	//std::ios::sync_with_stdio(false);
	cout.setf(ios::showpoint | ios::fixed);
	cout.precision(20);
#ifdef SG
	freopen((problemname + ".in").c_str(), "r", stdin);
//	freopen((problemname + ".out").c_str(), "w", stdout);
#endif
    ll a, b, c, k;
    cin >> a >> b >> c >> k;
    ll ans = a - b;
    if (k & 1) {
        ans *= (-1);
    }
    cout << ans << endl;

	return 0;
}

Measured memory_bytes: 256
#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(i,v.size())cout<<" "<<v[i];cout<<endl<<endl;
#define show2d(v) rep(i,v.size()){rep(j,v[i].size())cout<<" "<<v[i][j];cout<<endl;}cout<<endl;
using namespace std;
typedef long long 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;
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;}
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};
int main(int argc, char *argv[])
{
  int ans = 0;
  string s, t="CODEFESTIVAL2016";
  cin >> s;
  rep(i, s.size()) {
    if(s[i] != t[i]) ans++;
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
n, m, c = gets.split.map(&:to_i)
aryB    = gets.split.map(&:to_i)
ans     = 0

n.times do |i|
  sum = 0
  aryA = gets.split.map(&:to_i)
  m.times do |j|
    sum += aryA[j] * aryB[j]
  end
  if sum + c > 0
    ans += 1
  end
end

puts ans


Measured memory_bytes: 3836
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
 
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
 
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
 
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
 
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
 
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
 
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001; //check the limits, dummy
 
int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);    
	
    int N, K; cin >> N >> K;
    map<int, ll> cnts;
    ll ans = 0;
    cnts.insert({0, 1});
    ll pre = 0;
    ll prefs[N+1]; prefs[0] = 0;
    F0R(i, N) {
        int A; cin >> A; A--;
        pre += A; pre %= K;
        prefs[i+1] = pre;
        if (i >= K-1) {
            cnts[prefs[i-K+1]]--;
        }
        if (cnts.count(pre)) {
            ans += cnts[pre]; cnts[pre]++;
        } else {
            cnts.insert({pre, 1});
        }
    }

    cout << ans << endl;
	
	return 0;
}
 
// read the question correctly (ll vs int)
// template by bqi343

Measured memory_bytes: 14336
fn get_input() -> String {
    let mut word = String::new();
    std::io::stdin().read_line(&mut word).unwrap();
    word.trim().to_owned()
}

fn main() {
    let line = get_input();
    let mut split = line.split_whitespace();
    let n: i32 = split.next().unwrap().parse().unwrap();
    let a: i32 = split.next().unwrap().parse().unwrap();
    let b: i32 = split.next().unwrap().parse().unwrap();
    
    let ans = std::cmp::min(n*a, b);
    
    println!("{}", ans);

}

Measured memory_bytes: 4352
//  2020 05 03
// ABC 166 A A?C
#include <algorithm>
#include <complex>
#include <string>
#include <deque>
#include <iostream>
#include <set>
#include <stack>
#include <string>
#include <queue>
#include <utility>
#include <vector>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
using namespace std;
using ll = long long;
using P = pair<int, int>;


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

    if (s == "ABC") {
        cout << "ARC" << endl;
    } else {
        cout << "ABC" << endl;
    }

    return 0;
}

Measured memory_bytes: 3620
#include<stdio.h>
#include<algorithm>
using namespace std;
int main(void)
{
	int n,m,a,b,c,i,k[1000],h[1000],r[1000],t[1000],w[301],K,H,T;
	while(1){
		scanf("%d %d %d",&a,&b,&c);
		if(a==0&&b==0&&c==0) break;
		scanf("%d",&n);
		for(i=0;i<n;i++){
			scanf("%d %d %d %d",&k[i],&h[i],&t[i],&r[i]);
		}
		for(i=1;i<=a+b+c;i++) w[i]=2;
		for(i=0;i<n;i++){
			if(r[i]==1){
				w[k[i]]=1; w[h[i]]=1; w[t[i]]=1;
			}
		}
		for(i=0;i<n;i++){
			K=0;H=0;T=0;m=0;
			if(w[k[i]]==1) K=k[i],m++;
			if(w[h[i]]==1) H=h[i],m++;
			if(w[t[i]]==1) T=t[i],m++;
//			printf("a %d\n",m);
			if(m>=2){
				w[k[i]+h[i]+t[i]-K-H-T]=0;
			}
		}
		for(i=1;i<=a+b+c;i++){
			printf("%d\n",w[i]);
		}
	}
	return 0;
}

Measured memory_bytes: 2684
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, b = LI()
    print((a-1)*(b-1))
    return

#B
def B():
    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    A()


Measured memory_bytes: 38512
#define _CRT_SECURE_NO_DEPRECATE
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include "bits/stdc++.h"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define int long long int
#define SYNC std::ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define FRE freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
typedef long double ld;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
typedef vector<ii>   vii;
//typedef tree<int, null_type, less<int>, rb_tree_tag, 
//             tree_order_statistics_node_update> 
//    ost;
#define rep(i,l,r)   for (int i = (l); i < (r); i++)
#define here cout << " Hey!!\n";
#define  pb push_back
#define  F  first
#define  S  second
#define all(v) (v).begin(),(v).end()
#define sz(a) (int)((a).size())
#define sq(x) ((x)*(x))
template <typename T>
void etch(T V) {
	for (auto x : V)	cout << x << ' ' ;
	cout << '\n';
}
vector<string> vec_splitter(string s) {
	s += ',';
	vector<string> res;
	while(!s.empty()) {
		res.push_back(s.substr(0, s.find(',')));
		s = s.substr(s.find(',') + 1);
	}
	return res;
}
void debug_out(
vector<string> __attribute__ ((unused)) args,
__attribute__ ((unused)) int idx, 
__attribute__ ((unused)) int LINE_NUM) { cerr << endl; } 
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
	if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") ";
	stringstream ss; ss << H;
	cerr << args[idx] << " = " << ss.str();
	debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifndef ONLINE_JUDGE
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif
const int MOD = 1e9+7;
const int MOD1 = 998244353;
const int N = 3e5+5;
const int INF = 1000111000111000111LL;
const ld EPS = 1e-12;
const ld PI = 3.141592653589793116;

int32_t main()
{
	SYNC
	int h, w, m; cin >> h >> w >> m;
	int ans = 0;
	vi col(N, 0), row(N, 0);
	map<int, vi> dp;
	multiset<int> st;
	rep(i,0,m) {
		int x, y; cin >> x >> y;
		row[x]++;
		col[y]++;
		dp[x].pb(y);
	}
	for (auto it : col) {
		st.insert(it);	
	}
	for (auto it : dp) {
		vi vec = it.S;
		int x = it.F;
		int cnt = sz(vec);
		for (auto c : vec) {
			ans = max(ans, cnt + col[c]-1);
			st.erase(st.find(col[c]));
		}
		ans = max(ans, cnt + *st.rbegin());
		for (auto c : vec) {
			st.insert(col[c]);
		}
	}
	cout << ans;
	return 0;
}

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

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define MIN(a) *min_element(all(a))
#define MAX(a) *max_element(all(a))
#define SUM(a, b) accumulate(all(a), b)
#define REP(i, n) for(int (i)=0; (i)<(n); (i)++)
#define RREP(i, n) for(int (i)=(n)-1; (i)>=0; (i)--)
#define FOR(i, m, n) for(int (i)=(m); (i)<(n); i++)
#define FORR(i, m, n) for(int (i)=(n)-1; (i)>=(m); i--)
#define ios() cin.tie(0), ios::sync_with_stdio(false)
#define debug(x) cerr << #x << " = " << x << endl
//#define int long long

typedef long long ll;
typedef unsigned long long ull;
//ll const MOD = 998244353;
ll const MOD = 1e9+7;
ll const INF = 1e18;

template<typename T> int size(const T& a){return (int)a.size();}
template<typename T> T Div(T a, T b){return (a + b - 1) / b;}
template<typename T> bool chmin(T& a, const T& b){if(a > b){a = b; return true;} return false;}
template<typename T> bool chmax(T& a, const T& b){if(a < b){a = b; return true;} return false;}



signed main(){
    int a, b;
    cin >> a >> b;
    cout << a - b + 1 << endl;
    return 0;
}


Measured memory_bytes: 256
N = gets.to_i

def f(v, b)
  res = 0
  while v.positive?
    v, r = v.divmod(b)
    res += r
  end
  res
end

ans = 1.0 / 0
0.upto(N) do |k|
  ans = [ans, f(k, 6) + f(N - k, 9)].min
end

puts ans


Measured memory_bytes: 1788
list=[]
a=int(input())
b=int(input())
c=int(input())
d=int(input())
e=int(input())
if a<40:
    a=40
if b<40:
    b=40
if c<40:
    c=40
if d<40:
    d=40
if e<40:    
    e=40
list.append(a)
list.append(b)
list.append(c)
list.append(d)
list.append(e)    
ave=sum(list)//len(list)
print(str(ave))


Measured memory_bytes: 5592
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;


int main()
{
    int a,b;
    cin >> a >> b;
    string s;
    cin >> s;
    for(int i = 0; i < a; i++){
        if(s[i] >= 48 && s[i] <= 57)continue;
        else{
            cout << "No";
            return 0;
        }
    }
    for(int i = a+1; i < s.size(); i++){
        if(s[i] >= 48 && s[i] <= 57)continue;
        else{
            cout << "No";
            return 0;
        }
    }
    if(s[a] == '-' && s.size() == a + b + 1){
        cout << "Yes";
    }
    else cout << "No";
        
}

Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder();

            while (true)
            {
                int n = int.Parse(Console.ReadLine());

                if (n == 0) break;

                int[,] table = new int[n + 1, n + 1];
                int sumH = 0;

                for (int i = 0; i < n; i++)
                {
                    string[] input = Console.ReadLine().Split(' ');
                    int sumW = 0;

                    for (int j = 0; j < n; j++)
                    {
                        int temp = int.Parse(input[j]);

                        table[i, j] = temp;
                        table[n, j] += temp;
                        sumW += table[i, j];
                    }
                    table[i, n] = sumW;
                    sumH += sumW;
                }
                table[n, n] = sumH;

                for (int i = 0; i <= n; i++)
                {
                    for (int j = 0; j <= n; j++)
                    {
                        string s = string.Format("{0,5}", table[i, j]);
                        sb.Append(s);
                    }
                    sb.Append("\n");
                }
            }
            Console.Write(sb);
        }
    }
}

Measured memory_bytes: 9404
#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
int H,W;
string s[2000];
int a[2000][2000];
int h[2000][2000];
int ans;
main()
{
	cin>>H>>W;ans=max(H,W);
	for(int i=0;i<H;i++)cin>>s[i];
	for(int i=1;i<H;i++)
	{
		for(int j=1;j<W;j++)
		{
			a[i-1][j-1]=(s[i-1][j-1]==s[i-1][j])==(s[i][j-1]==s[i][j]);
		}
	}
	for(int i=0;i<H-1;i++)
	{
		for(int j=0;j<W-1;j++)
		{
			if(a[i][j]==0)h[i][j]=0;
			else
			{
				if(i==0)h[i][j]=1;
				else h[i][j]=h[i-1][j]+1;
			}
		}
	}
	for(int i=0;i<H-1;i++)
	{
		stack<pair<int,int> >S;
		S.push(make_pair(0,0));
		for(int j=0;j<W;j++)
		{
			int now=0;
			while(S.top().first>h[i][j])
			{
				now+=S.top().second;
				ans=max(ans,(now+1)*(S.top().first+1));
				S.pop();
			}
			S.push(make_pair(h[i][j],now+1));
		}
	}
	cout<<ans<<endl;
}

Measured memory_bytes: 35712
s = input()
a = [-1]
b = 0
for n in range(len(s)):
    if s[n] != "A" and s[n] != "C" and s[n] != "G" and s[n] != "T":
        a.append(n)
a.append(len(s))
for n in range(len(a)-1):
    b = max(b, a[n+1]-a[n]-1)
print(b)

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

int main()
{
    int n = 0;
    cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    int max = *max_element(a.begin(), a.end());
    int min = *min_element(a.begin(), a.end());
    cout << (max - min) << "\n";

    return 0;
}

Measured memory_bytes: 256
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.NoSuchElementException;

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    public double nextDouble() { return Double.parseDouble(next());}
}

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		FastScanner fs = new FastScanner();
		Long N = fs.nextLong();
		Long K = fs.nextLong();

		Long num = N / K;

		N = N - K * num;

		ArrayList<Long> history = new ArrayList<>();
		history.add(N);
		Long x = N;
		Long min = N;
		while(true) {
			x = Math.abs(x-K);
			if(history.contains(x)) break;
			history.add(x);

			if(min > x ) {
				min = x;
			}
			if(min == 0) break;


		}
		System.out.println(min);

	}

}


Measured memory_bytes: 21204
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll ara[100003],ara2[100003];

int main()
{
    ll n,i,s,r;
    cin>>n;
    for(i=0; i<n+1; i++)scanf("%lld",&ara[i]);
    for(i=0; i<n; i++)scanf("%lld",&ara2[i]);
    s=0;
    for(i=0; i<n; i++)
    {
        r=min(ara[i],ara2[i]);
        ara[i]-=r;
        ara2[i]-=r;
        s+=r;
        if(ara2[i]>0 && ara[i+1]>0)
        {
            r=min(ara2[i],ara[i+1]);
            s+=r;
            ara2[i]-=r;
            ara[i+1]-=r;
        }
    }
    cout<<s<<endl;
    return 0;
}


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

typedef long long   signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)
#define  inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define  dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))

#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define PQ priority_queue

#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
#define  FOR(it, v) for(auto it =  v.begin(); it !=  v.end(); ++it)
#define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)

template<typename T> bool   setmin(T & a, T b) { if(b <  a) { a = b; return true; } else { return false; } }
template<typename T> bool   setmax(T & a, T b) { if(b >  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }
template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

int n, a[200000];

bool f(int v) {
	if(v == 1) {
		int ma = -1;
		inc(i, n) { if(! setmax(ma, a[i])) { return false; } }
		return true;
	}
	
	map<int, int> mp;
	inc(i, n) {
		int p = (i == 0 ? 0 : a[i - 1]);
		if(p >= a[i]) {
			while(! mp.empty() && mp.rbegin() -> FI >= a[i]) { mp.erase(mp.rbegin() -> FI); }
			decID(j, -1, a[i]) {
				if(j < 0) { return false; }
				mp[j]++;
				if(mp[j] == v) { mp[j] = 0; } else { break; }
			}
		}
	}
	
	return true;
}

int main() {
	cin >> n;
	inc(i, n) { cin >> a[i]; }
	
	int L = 0, H = n;
	while(H - L > 1) {
		int M = (L + H) / 2;
		(f(M) ? H : L) = M;
	}
	
	cout << H << endl;
	
	return 0;
}


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

int main() {
  string S;
  cin >> S;
  bool B[26];
  bool c = true;
  for (int i = 0; i < 26; i++) {
    B[i] = false;
  }
  for (int i = 0; i < S.size(); i++) {
    B[S[i] - 'a'] = true;
  }
  for (int i = 0; i < 26; i++) {
    if (B[i]) {
      continue;
    }
    else {
      cout << (char)('a' + i) << endl;
      c = false;
      break;
    }
  }
  if (c) {
    cout << "None" << endl;
  }
}

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

#define REP(i,n) for(int i = 0; i < n; i++)
#define PER(i,n) for(int i = n-1; i >= 0; i--)
#define FOR(i,l,r) for(int i = l; i <= r; i++)
#define ROF(i,l,r) for(int i = r; i >= l; i--)
#define DEBUG(x) cout << #x << "=" << x << endl;
#define SHOW1(A,n) { REP(i,n) cout<<A[i]<<(i==n-1?'\n':' '); }
#define SHOW2(A,m,n) { REP(j,m) SHOW1(A[j],n) }
#define pb push_back
#define fi first
#define se second
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)((x).size())
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int INF = 0x3f3f3f3f, MOD = 1000000007;
const double PI = acos(-1), EPS = 1e-15;
const int MAXN = 5e5+9, MAXM = 2e5+9;



int main()
{
#ifdef LOCAL
    //freopen("i.txt", "r", stdin);
    //freopen("o.txt", "w", stdout);
#endif //LOCAL

    LL K,A,B;
    while (cin>>K>>A>>B) {
        LL ans=1;
        if (A+1<B) {
            if (K>A-1) {
                K-=A-1;
                ans=A;
                ans+=K/2*(B-A);
                if (K&1) ans++;
            } else ans+=K;
        } else ans+=K;
        cout<<ans<<endl;
    }

    return 0;
}
































Measured memory_bytes: 256