akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#ifdef DEMETRIO
#define deb(...) fprintf(stderr,__VA_ARGS__)
#define deb1(x) cerr << #x << " = " << x << endl
#else
#define deb(...) 0
#define deb1(x) 0
#endif
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define fore(i,a,b) for(int i=a,ThxDem=b;i<ThxDem;++i)
#define SZ(x) ((int)x.size())
using namespace std;
typedef long long ll;

int dist(int x, int y){
	int r=abs(x-y);
	return min(r,24-r);
}

int q[32];
int w[32];
int n,r;

void go(int k){
	if(k==13){
		int rr=32;
		fore(i,0,32){
			if(w[i]>1){rr=0;break;}
			if(w[i])rr=min(rr,dist(0,i));
			fore(j,i+1,32){
				if(w[i]&&w[j])rr=min(rr,dist(i,j));
			}
		}
		r=max(r,rr);
		return;
	}
	if(q[k]==0)go(k+1);
	else if(q[k]==2){
		w[k]++;
		w[24-k]++;
		go(k+1);
		w[k]--;
		w[24-k]--;
	}
	else {
		w[k]++;
		go(k+1);
		w[k]--;
		w[24-k]++;
		go(k+1);
		w[24-k]--;
	}
}

int main(){
	scanf("%d",&n);
	fore(i,0,n){
		int d;
		scanf("%d",&d);
		q[d]++;
	}
	if(q[0]){puts("0");return 0;}
	if(*max_element(q,q+32)>2){puts("0");return 0;}
	r=0;
	go(0);
	printf("%d\n",r);
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define fr first
#define se second
#define r_0 return 0
#define ll long long
#define pb push_back
#define pf push_front
#define TIME ios_base::sync_with_stdio(0);
using namespace std;

const long long INF = 1e14;
const long long N = 3e6 + 1;
const long long mod = 1e9 + 7;
const long double eps = 1E-7;

ll n, m, q, a[N], cnt;
string s;
int main()
{
    TIME;
    cin >> n >> m >> q;
    cout << max( 0ll, (q - ( n - m )));
}


Measured memory_bytes: 256
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include <bits/stdc++.h>
using namespace std;
template<class t> inline t read(t &x){
	char c=getchar();bool f=0;x=0;
	while(!isdigit(c)) f|=c=='-',c=getchar();
	while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	if(f) x=-x;return x;
}
template<class t,class ...A> inline void read(t &x,A &...a){
	read(x);read(a...);
}
template<class t> inline void write(t x){
	if(x<0) putchar('-'),write(-x);
	else{if(x>9) write(x/10);putchar('0'+x%10);}
}

#define x first
#define y second

const int N=1e5+5,C=5;
int n,a[N],ans[N];
set<int> rest;
map<int,int> du;

int check(){
	if(du.size()==2){
		for(auto i:du)if(i.y==rest.size()-1&&rest.count(i.x)) return i.x;
		return 0;
	}else{
		return 0;
	}
}

void dfs(int x,int last,set<int> rest){
	if(x>n){
		for(int i=1;i<=n;i++) write(ans[i]),putchar(' ');
		exit(0); 
	}
	for(auto i:rest) if(i!=last){
		ans[x]=i; set<int> tp=rest; tp.erase(i);
		dfs(x+1,a[i],tp);
	}
}

signed main(){
	read(n);
	for(int i=1;i<=n;i++) read(a[i]),rest.insert(i),du[a[i]]++; 
	if(n==2) return write(-1),0;
	int last=0; 
	for(int i=1;i<=n-C;i++){
		int t=check();
		if(t){
			ans[i]=t; 
		}else{
			auto it=rest.begin();
			if(*it==last)it++;
			ans[i]=*it;
		}
		if(--du[a[ans[i]]]==0)du.erase(a[ans[i]]);
		rest.erase(ans[i]);
		last=a[ans[i]];
	}
	dfs(max(1,n-C+1),last,rest);
	write(-1);
}

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

using namespace std;
using ll = long long;
using pint = pair<int, int>;

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

	if (S[S.size() - 1] == 's') {
		S += "es";
	}
	else {
		S += "s";
	}

	cout << S << endl;
	return 0;
}

Measured memory_bytes: 3624
import java.util.*;
import java.io.*;
import java.awt.geom.*;
import java.math.*;

public class Main {

	static final Scanner in = new Scanner(System.in);
	static final PrintWriter out = new PrintWriter(System.out,false);
	static boolean debug = false;

	static boolean solve() {
		int m = in.nextInt();
		int n = in.nextInt();
		if (m + n == 0) return false;

		int N = 10000000;
		boolean[] f = new boolean[N];
		int cnt = 0;
		Arrays.fill(f, true);
		for (int i=m; i<N; i++) {
			if (!f[i]) continue;
			cnt++;
			if (cnt > n) {
				out.println(i);
				return true;
			}
			for (int j=i+i; j<N; j+=i) {
				f[j] = false;
			}
		}

		return true;
	}

	public static void main(String[] args) {
		debug = args.length > 0;
		long start = System.nanoTime();

		while(solve());
		out.flush();

		long end = System.nanoTime();
		dump((end - start) / 1000000 + " ms");
		in.close();
		out.close();
	}

	static void dump(Object... o) { if (debug) System.err.println(Arrays.deepToString(o)); }
}

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

using namespace std;

#define NO "UNRESTORABLE"

int main() {
  string s, w;
  cin >> s >> w;
  bool fuck = false;
  if (s.size() < w.size()) {
    puts(NO);
    return 0;
  }
  int ss = s.size(), ws = w.size();
  string ans;
  for (int i = 0; i < ss; i++) {
    ans += 'z';
  }
  for (int i = 0; i < ss; i++) {
    if (i == ss - ws + 1) {
      break;
    }
    string t = s;
    for (int j = 0; j < ws; j++) {
      t[i + j] = w[j];
    }
    bool eq = true;
    for (int j = 0; j < ss; j++) {
      if (s[j] == '?') {
        continue;
      } 
      if (s[j] != t[j]) {
        eq = false;
        break;
      }
    }
    if (eq) {
      fuck = true;
      for (int j = 0; j < ss; j++) {
        if (t[j] == '?') {
          t[j] = 'a';
        }
      }
      ans = min(ans, t);
    }
  }
  if (fuck) {
    cout << ans << endl;
  } else {
    puts(NO);
  }
  return 0;
}

Measured memory_bytes: 256
n, k = map(int, input().split())
P = list(map(int, input().split()))

C = [(1 + p) / 2 for p in P]

ans = tmp = sum(C[: k])
for i in range(k, n):
    tmp += (C[i] - C[i - k])
    ans = max(tmp, ans)

print(ans)

Measured memory_bytes: 25060
import sys
import math
from collections import Counter, defaultdict, deque
from functools import reduce

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)


def I():
    return int(input())


def MI():
    return map(int, input().split())


def LI():
    return list(MI())


def LIN(n: int):
    return [I() for _ in range(n)]


inf = float("inf")
mod = 10 ** 9 + 7


def main():
    k = I()

    def gcd(a, b, c):
        return math.gcd(math.gcd(a, b), c)

    sum = 0
    for a in range(1, k + 1):
        for b in range(1, k + 1):
            for c in range(1, k + 1):
                if a == b or a == c:
                    sum += math.gcd(b, c)
                elif b == c:
                    sum += math.gcd(a, c)
                else:
                    sum += gcd(a, b, c)

    print(sum)


if __name__ == "__main__":
    main()


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

int main() {
  int H,W;
  cin >> H >> W;
  //vector<vector<char>> before(H,vector<char>(W));
  vector<string> before(H);
  
  for(int i = 0 ; i < H ; i++){
    cin >> before.at(i);
  }
  
  vector<int> H_num(0);
  vector<int> W_num(0);
  
  for(int i = 0 ; i < H ; i++){
    bool flag = true;
    for(int j = 0 ; j < W ; j++){
      if(before.at(i).at(j) == '#'){
        flag = false;
        break;
      }
    }
    if(!flag){
      H_num.push_back(i);
    }
  }
  
  for(int i = 0 ; i < W ; i++){
    bool flag = true;
    for(int j = 0 ; j < H ; j++){
      if(before.at(j).at(i) == '#'){
        flag = false;
        break;
      }
    }
    if(!flag){
      W_num.push_back(i);
    }
  }
  
  for(int i = 0 ; i < H_num.size() ; i++){
    for(int j = 0 ; j < W_num.size() ; j++){
      cout << before.at(H_num.at(i)).at(W_num.at(j));
    }
    cout << endl;
  }
  
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define pb push_back
#define ers erase
#define ins insert
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x)  cerr << #x << " = " << x << endl
#define kill(x) return cout << x, 0;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vl;


const ld Pi = 3.14159265359;
const ll MOD = 1000 * 1000 * 1000 + 7;
const ll MAXN = 2e5 + 10;
const ll INF = 1e18;
ll n, w[MAXN], v[MAXN], W;
ll dp[110][MAXN];
int main(){
	IOS;
	cin >> n >> W;
	memset(dp, 63, sizeof dp);
	for (int i = 1; i <= n; i++) cin >> w[i] >> v[i];
	dp[1][v[1]] = w[1];
	dp[1][0] = 0;
	for (int i = 2; i <= n; i++){
		for (int j = 0; j < MAXN; j++){
			if (j >= v[i]){
				dp[i][j] = min(dp[i - 1][j - v[i]] + w[i], dp[i - 1][j]);
			}else{
				dp[i][j] = dp[i - 1][j];
			}
		}
	}
	ll ans = 0;
	for (int j = 0; j < MAXN; j++){
		if (dp[n][j] <= W) ans = j;
	}
	cout << ans;
	
	
	
	return 0;
}






/*
     ,---,           ___                 ,--,
    '  .' \        ,--.'|_             ,--.'|                             ,--,
   /  ;    '.      |  | :,'            |  | :                           ,--.'|    ,---.        ,---,
  :  :       \     :  : ' :            :  : '                 .--.--.   |  |,    '   ,'\   ,-+-. /  |
  :  |   /\   \  .;__,'  /    ,--.--.  |  ' |     ,--.--.    /  /    '  `--'_   /   /   | ,--.'|'   |
  |  :  ' ;.   : |  |   |    /       \ '  | |    /       \  |  :  /`./  ,' ,'| .   ; ,. :|   |  ,"' |
  |  |  ;/  \   \:__,'| :   .--.  .-. ||  | :   .--.  .-. | |  :  ;_    '  | | '   | |: :|   | /  | |
  '  :  | \  \ ,'  '  : |__  \__\/: . .'  : |__  \__\/: . .  \  \    `. |  | : '   | .; :|   | |  | |
  |  |  '  '--'    |  | '.'| ," .--.; ||  | '.'| ," .--.; |   `----.   \'  : |_|   :    ||   | |  |/
  |  :  :          ;  :    ;/  /  ,.  |;  :    ;/  /  ,.  |  /  /`--'  /|  | '.'\   \  / |   | |--'
  |  | ,'          |  ,   /;  :   .'   \  ,   /;  :   .'   \'--'.     / ;  :    ;`----'  |   |/
  `--''             ---`-' |  ,     .-./---`-' |  ,     .-./  `--'---'  |  ,   /         '---'
                            `--`---'            `--`---'                 ---`-'
*/




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

int main(){
  long long N;
  cin >> N;
  vector<string> s(N);
  
  rep(i,N) cin >> s.at(i);
  
  int AC=0,WA=0,TLE=0,RE=0;
  rep(i,N){
    if(s.at(i)=="AC") AC++;
    if(s.at(i)=="WA") WA++;
    if(s.at(i)=="TLE") TLE++;
    if(s.at(i)=="RE") RE++;
  }
  
    cout << "AC x " << AC << endl;
    cout << "WA x "<< WA << endl;
    cout << "TLE x " << TLE << endl;
    cout << "RE x "<< RE << endl;
}


Measured memory_bytes: 6324
#include <iostream>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <sstream>
#include <list>
#include <functional>
#include <algorithm>

using namespace std;

int main(int, char**){
    string str;
    cin >> str;
    for(auto it=str.rbegin();it!=str.rend();++it) cout << *it;
    cout << endl;
    return 0;
}

Measured memory_bytes: 1176
//BROWNIE TK

#include <bits/stdc++.h>
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ldb;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define pb push_back
#define popb pop_back()
#define pf push_front
#define popf pop_front()
#define si size()
#define be begin()
#define en end()
#define all(v) v.be, v.en
#define le length()
#define mp make_pair
#define mt make_tuple
#define F first
#define S second

#define forz(i, n) for (int i = 0; i < n; i++)
#define forzm(i, m, n) for (int i = m; i < n; i++)
#define rforz(i, n) for (int i = n - 1; i >= 0; i--)
#define rforzm(i, m, n) for (int i = n - 1; i >= m; i--)
#define deci(n) fixed << setprecision(n)
#define high(n) __builtin_popcount(n)
#define parity(n) __builtin_parity(n)
#define ctz(n) __builtin_ctz(n)
#define lb lower_bound
#define ub upper_bound
#define er equal_range
#define maxe *max_element
#define mine *min_element
#define mod 1000000007
#define mod2 998244353
#define kira ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)

#define endl "\n"
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl

#define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define osetlli tree<lli, null_type, less<lli>, rb_tree_tag, tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
#define ofk order_of_key
#define fbo find_by_order
using namespace std;

/*STD fucntions*/
lli power(lli x, lli y, lli p)
{
    lli res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % p;
        //y must be even now
        y = y >> 1; //y=y/2
        x = (x * x) % p;
    }
    return res;
}
lli gcd(lli a, lli b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
lli lcm(lli a, lli b)
{
    return a * b / gcd(a, b);
}
lli modi(lli a, lli m)
{
    // fermat little thm where m is prime
    return power(a, m - 2, m);
}
/*CODE BEGINS*/
int main()
{
    kira;
    lli n;
    cin>>n;
    string s;
    map<string,lli>m;
    forz(i,n)
    {
        cin>>s;
        sort(all(s));
        m[s]++;
    }
    lli c=0;
    for(auto x:m)
    {
        c+=(x.S*(x.S-1))/2;
    }
    cout<<c;
    return 0;
}

Measured memory_bytes: 11264
x, y, a, b, c = gets.split.map &:to_i
ps = [0] + gets.split.map(&:to_i).sort[-x, x]
qs = [0] + gets.split.map(&:to_i).sort[-y, y]
rs = [0] + gets.split.map(&:to_i).sort

sum = 0
(x + y).times do
  sum += [ps, qs, rs].max_by(&:last).pop
end
p sum

Measured memory_bytes: 28116
#include <stdio.h>
int def(int x, int y)
{
	if (y == 0)
	{
		return x;
	}
	else
	{
		return def(y, x%y);
	}
}
int main(void)
{
	int W, H, C,ans,kouyakusu;
	scanf("%d %d %d", &W, &H, &C);
	if (W > H)
		kouyakusu = def(W, H);
	else
		kouyakusu = def(H, W);
	ans = (W / kouyakusu)*(H / kouyakusu)*C;
	
	printf("%d\n", ans);
	return 0;
}

Measured memory_bytes: 596
def gcd(a,b):
    if b == 0:
        return abs(a)
    else:
        return gcd(b, a%b)


N,St = map(int, input().split())
X = list(map(int,input().split()))

X.append(St)
X = sorted(X)

del_X = []
for i in range(N):
    del_X.append(X[i+1] - X[i])
del_X = sorted(list(set(del_X)))

# print(X)
# print(del_X)

for i in range(len(del_X)):
    if i == 0:
        tmp = gcd(del_X[i],0)
    else:
        tmp = gcd(tmp, del_X[i])
print(tmp)


Measured memory_bytes: 14252
import java.io.*;
public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str = br.readLine();
		String[] strs = str.split(" ");
		int a = Integer.parseInt(strs[0]);
		int b = Integer.parseInt(strs[1]);
		int c = Integer.parseInt(strs[2]);
		if(a < b){
			if(b < c){
				System.out.println("Yes");
			}else{
				System.out.println("No");
			}
		}else{
			System.out.println("No");
		}//if
		br.close();
	}
}

Measured memory_bytes: 17088
#include <bits/stdc++.h>
#define rep3(i, s, n, a) for (int i = (s); i < (int)(n); i += a)
#define rep2(i, s, n) rep3(i, s, n, 1)
#define rep(i, n) rep2(i, 0, n)

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;

int main(){
    string s;
    cin >> s;
    int seq=0, cnt=0;
    rep(i, 3){
        if(s[i]=='R'){
            seq++;
        }else{
            cnt = max(cnt, seq);
            seq = 0;
        }
    }
    cnt = max(cnt, seq);
    cout << cnt << endl;
    return 0;
}

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

int dp[110][10010];
int main(){
  int N, W;
  cin>>N>>W;
  vector<int> v(N),w(N);
  rep(i,N) cin>>v[i]>>w[i];
  rep(j,W+1) dp[0][j] = 0;
  rep(i,N){
    rep(j,W+1){
      if (j >= w[i]) dp[i+1][j] = max(dp[i][j-w[i]] + v[i], dp[i][j]);
      else dp[i+1][j] = dp[i][j];
    }
  }
  cout<< dp[N][W] << endl;
}



Measured memory_bytes: 7036
import java.util.*

fun main(args : Array<String>) {
    val n = readLine()!!.toInt()
    val s = HashMap<String, Int>(n)
    var maxc = 1
    (1..n).forEach {
        val si = readLine()!!
        val cnt = s[si]
        if (cnt == null) {
            s[si] = 1
        } else {
            s[si] = cnt + 1
            maxc = Math.max(maxc, cnt + 1)
        }
    }
    val ans = TreeSet<String>()
    for ((k, v) in s) {
        if (v == maxc) ans += k
    }
    ans.forEach(::println)
}


Measured memory_bytes: 86984
n = int(input())
L = list(map(int, input().split()))

cnt = 0
for i in range(n-1):
    for j in range(n-1-i):
        if L[j] > L[j+1]:
            L[j], L[j+1] = L[j+1], L[j]
            cnt += 1

print(' '.join(str(x) for x in L))
print(cnt)


Measured memory_bytes: 5604
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
const int MAXINT = 1e9;
int h[MAXN], dp[MAXN];
int main ()
{
    for(int i = 3; i <= MAXN - 5; ++i) dp[i] = MAXINT;
    int n, k; cin >> n >> k;
    for(int i = 1; i <= n; ++i) cin >> h[i];
    dp[2] = abs(h[1] - h[2]);
    for(int i = 1; i <= n ; ++i){
        for(int j = 1; j <= k and j + i <= n; ++j)
            dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
    }
    cout << dp[n] << " ";
    return 0;
}
///dp[i] += (dp[i - 1] < dp[i - 2]) ? dp[i - 1] + abs(h[i - 1] - h[i]) : dp[i - 2] + abs(h[i - 2] - h[i]);


Measured memory_bytes: 1024
#include <iostream>
#include <string>
using namespace std;
int a[3];

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

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define per(i, n) for (int i = (int)(n-1); i >= 0; i--)
#define sz(x) int(x.size())
#define all(x) (x).begin(), (x).end()
#define INF 2e9
typedef long long ll;
typedef pair<int, int> P;

int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a%b);
}

//3項以上の場合
int gcd(vector<int>a) {
  int ans = a[0];
  for (int i=1; i<sz(a); i++) {
    ans = gcd(ans, a[i]);
  }
  return ans;
}

int main() {
    string s; cin >> s;
    int n = sz(s);
    int ans;
    int cnt1 = 0, cnt2 = 0;
    rep(i, n) { //0スタート
        if (i%2==0) {
            if (s[i]!='0') cnt1++;
        }
        else {
            if (s[i]!='1') cnt1++;
        }
    }
    rep(i, n) { //1スタート
        if (i%2==0) {
            if (s[i]!='1') cnt2++;
        }
        else {
            if (s[i]!='0') cnt2++;
        }
    }
    ans = min(cnt1, cnt2);
    cout << ans << endl;
}

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 = sc.nextInt();
		int modSum = 0;
		int answer = 0;

		for (int i = 1; i <= N; i = i + 2) {
			modSum = 0;
			for (int j = 1; j < i / 2; j = j + 2) {
				if (i % j == 0) modSum++;
			}
			if (modSum + 1 == 8) answer++;
		}

		System.out.println(answer);
	}
}


Measured memory_bytes: 21844
#include <cmath>
#include <cstdio>
#include <limits>
#include <tuple>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <numeric>
#include <string>
#include <array>
#include <iomanip>
#include <set>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = n-1; i >= 0; --i)
#define REP(i, s, n) for (int i = s; i< (int)(n); ++i)
#define all(x) x.begin(), x.end()

using namespace std;
using ll = long long;
ll inf = ll(1e18);
using ll1d = vector<ll>;
using ll2d = vector<ll1d>;
using ll3d = vector<ll2d>;

struct Edge {
  ll fr, to;
  Edge(ll fr, ll to) {
    this->fr = fr;
    this->to = to;
  };

  bool operator<(const Edge &edge) const {
    return fr != edge.fr ? edge.fr < fr : to < edge.to;
  }
};

bool dfs(const ll& v, const ll& c, vector<ll> &col, ll2d& g) {
  col[v] = c;
  rep(i, g[v].size()) {
    if (col[g[v][i]] == c) return false;
    if (col[g[v][i]] == 0 and !dfs(g[v][i], -c, col, g)) return false;
  }
  return true;
}

void solve(istream &ccin = cin, ostream &ccout = cout) {
  ll n, m;
  ccin >> n >> m;
  ll2d g(n);
  rep(i, m) {
    ll a, b;
    ccin >> a >> b;
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  ll1d col(n, 0);
  if (dfs(0, 1, col, g)) {
    ll b1 = 0, b2 = 0;
    rep(i, n) {
      if (col[i] == 1) ++b1;
      else ++b2;
    }
    ccout << b1 * b2 - m << endl;
  } else
    ccout << n * (n - 1) / 2 - m << endl;

}

int main() {
  ios::sync_with_stdio(false);
  solve();
  return 0;
}

Measured memory_bytes: 7680
#include "bits/stdc++.h"
#define _ ios_base::sync_with_stdio(0);cin.tie(0);

using namespace std;

const int maxn=100005;
vector<pair<int, int> > graph[maxn];
bool vis[maxn];
int pos[maxn];

bool dfs(int x, int p) {
    if (vis[x]) {
        if (p != pos[x]) {
            return false;
        } else {
            return true;
        }
    }
    vis[x] = true;
    pos[x] = p;
    for (pair<int, int> pair : graph[x]) {
        bool res = dfs(pair.first, pair.second + p);
        if (!res) return res;
    }
    return true;
}

int main() { _
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int l, r, d;
        cin >> l >> r >> d;
        // vr - vl = d
        graph[l].emplace_back(r, d);
        // vl - vr = -d
        graph[r].emplace_back(l, -d);
    }
    for (int i = 1; i <= n; i++) {
        if (!vis[i]) {
            if (!dfs(i, 0)) {
                cout << "No" << endl;
                return 0;
            }
        }
    }
    cout << "Yes" << endl;
    return 0;
}



Measured memory_bytes: 11136
n,k=map(int,input().split())
res=(n+2)*(n+1)**2/2+n+1-(n+1)*(n+2)*(2*n+3)/6
re=(n+1)*k*(k-1)/2+k-1-k*(k-1)*(2*k-1)/6
res=(res-re)%(10**9+7)
print(int(res))

Measured memory_bytes: 9196
S = input()

gLen = 0
lLen = 0
greater = True
ans = 0
for c in S:
    if greater:
        if c == '>':
            gLen += 1
        else:
            if gLen > lLen:
                ans += lLen * (lLen-1) // 2
                ans += gLen * (gLen+1) // 2
            else:
                ans += lLen * (lLen+1) // 2
                ans += gLen * (gLen-1) // 2
            lLen = 1
            gLen = 0
            greater = False
    else:
        if c == '<':
            lLen += 1
        else:
            greater = True
            gLen = 1
if gLen > lLen:
    ans += lLen * (lLen-1) // 2
    ans += gLen * (gLen+1) // 2
else:
    ans += lLen * (lLen+1) // 2
    ans += gLen * (gLen-1) // 2
print(ans)

Measured memory_bytes: 4212
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double lf;
typedef pair<ll,ll> ii;
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define RST(i,n) memset(i,n,sizeof i)
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(),a.end()
#define X first
#define Y second
#define mkp make_pair
#define pb push_back
#define eb emplace_back
#define pob pop_back
#ifdef cold66
#define debug(...) do{\
    fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);\
    _do(__VA_ARGS__);\
}while(0)
template<typename T>void _do(T &&_x){cerr<<_x<<endl;}
template<typename T,typename ...S> void _do(T &&_x,S &&..._t){cerr<<_x<<", ";_do(_t...);}
template<typename _a,typename _b> ostream& operator << (ostream &_s,const pair<_a,_b> &_p){return _s<<"("<<_p.X<<","<<_p.Y<<")";}
template<typename It> ostream& _OUTC(ostream &_s,It _ita,It _itb)
{
    _s<<"{";
    for(It _it=_ita;_it!=_itb;_it++)
    {
        _s<<(_it==_ita?"":",")<<*_it;
    }
    _s<<"}";
    return _s;
}
template<typename _a> ostream &operator << (ostream &_s,vector<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s,set<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a,typename _b> ostream &operator << (ostream &_s,map<_a,_b> &_c){return _OUTC(_s,ALL(_c));}
template<typename _t> void pary(_t _a,_t _b){_OUTC(cerr,_a,_b);cerr<<endl;}
#define IOS()
#else
#define debug(...)
#define pary(...)
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#endif // cold66
//}
template<class T> inline bool chkmax(T &a, const T &b) { return b > a ? a = b, true : false; }
template<class T> inline bool chkmin(T &a, const T &b) { return b < a ? a = b, true : false; }
template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;

const ll MAXn=1e5+5,MAXlg=__lg(MAXn)+2;
const ll MOD=1000000007;
const ll INF=0x3f3f3f3f;

vector<ll> stk;
int main(){
  IOS();
  string s;
  cin >> s;
  ll ans = 0;
  REP (i,SZ(s)) {
    ll tmp = s[i]-'0';
    if (!SZ(stk)) stk.eb(tmp);
    else {
      if (stk.back() == tmp) stk.eb(tmp);
      else {
        stk.pob();
        ans += 2;
      }
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 1480
N, M, Q =  map(int, input().split())
List = [list(map(int, input().split())) for _ in range(Q)]
A = [1 for _ in range(N)]
ans = 0

nCr = {}
def cmb(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    return nCr[(n,r)]

def new_A(A):
  for i in range(N):
    if A[N-i-1] < M:
      A[N-i-1] += 1
      break
    elif A[N-i-1] == M and A[N-i-2] < M:
      A[N-i-2] += 1
      for x in range(N-i-1, N):
        A[x] = A[N-i-2]
      break
  return A

for j in range(cmb(N+M-1, N)):
  sum = 0
  for k in range(Q):
    if A[List[k][1]-1] - A[List[k][0]-1] == List[k][2]:
      sum += List[k][3]
  if ans < sum:
    ans = sum
  A = new_A(A)
print(ans)

Measured memory_bytes: 68884
#include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll long long
#define endl "\n"
#define print_array(name,n) for(ll o=0;o<n;o++) cout<<name[o]<<" "
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define inf 2e18
#define pll pair<ll,ll>
#define pii pair<int,int>
bool sortbysecond(const pair<int,int> &a,const pair<int,int> &b){
	return (a.ss>b.ss);
}
 
ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b) ;} 

template <typename T>
string NumberTostring(T Number )
{
	ostringstream ss;
	ss<<Number;
	return ss.str();
}
 

 
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
 	ll n,k;
 	cin>>n>>k;
 	vector<ll> vec(n);
 	ll mx = 0;
 	for(int i=0;i<n;i++){
 		cin>>vec[i];
 		if(mx<vec[i]){
 			mx = vec[i];
 		}
 	}   
 	ll low = 1;
 	ll high = mx;
 	ll ans = 0;
 	while(low<=high){
 		ll mid = low + (high-low)/2;
 		ll temp = 0;
 		for(int i=0;i<n;i++){
 			temp += ceil(double(vec[i])/mid) - 1;
 			
 		}
 		// cout<<mid<<" "<<temp<<endl;
 		if(temp<=k){
 			high = mid-1;
 			ans = mid;
 		}
 		else{
 			low = mid+1;
 		}
 	}
 	cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 4664
#[allow(unused)]
use std::io::Write;

#[allow(unused)]
macro_rules! debug {
    ($($format:tt)*) => (write!(std::io::stderr(), $($format)*).unwrap());
}
#[allow(unused)]
macro_rules! debugln {
    ($($format:tt)*) => (writeln!(std::io::stderr(), $($format)*).unwrap());
}

fn main() {
    let n: usize = {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).unwrap();
        let s = s.trim_right().to_owned();
        s.parse().unwrap()
    };

    let s = {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).unwrap();
        let s = s.trim_right().to_owned();
        s
    };

    let chars: Vec<_> = s.chars().collect();

    let mut dp = vec![vec![0; n + 5]; n + 5];

    for i in 0..n {
        for j in 0..n {
            dp[i][j] = if chars[i] == chars[j] {
                if i == 0 || j == 0 {
                    1
                } else {
                    dp[i - 1][j - 1] + 1
                }
            } else {
                0
            };
        }
    }

    let mut ans = 0;
    for i in 0..n {
        for j in (i + 1)..n {
            let now = std::cmp::min(dp[i][j], j - i);
            ans = std::cmp::max(now, ans);
        }
    }
    println!("{}", ans);
}


Measured memory_bytes: 231676
#include <iostream>
using namespace std;
int main (){
    int a, b, c[300][300];
    int i , k;
    while (1) {
        cin >> a >> b;
        if (a == 0 && b == 0) break;
        for (i = 0; i < a; i++){
            for (k = 0; k < b; k++){
                if (i == 0 || k == 0 ||i == a-1 || k == b - 1){
                    c[i][k] = 1;
                } else {
                    c[i][k] = 0;
                }
            }
        }
        for (i = 0; i < a; i++) {
            for (k = 0;k < b; k ++ ){
                if (c[i][k] == 1) {
                    cout << "#";
                } else {
                    cout << "." ;
                }
            }
            cout << endl;
        }
        cout << endl;
    }
}


Measured memory_bytes: 3304
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
 
int main (void)
{
    int n,i,cnt=0;
    scanf("%d",&n);
  
    char s[n][11];
    for(i=0;i<n;i++){
        scanf("%s",s[i]);
    }
  
    qsort(s,n,11,strcmp); 
  
    for(i=0;i<n-1;i++){
        if(strcmp(s[i],s[i+1])!=0)
            cnt++;
    } 
  
  cnt++;
  
    printf("%d",cnt);
  return 0;
}

Measured memory_bytes: 5712
#include <stdio.h>
#include <string.h>
#include <math.h>
#define rep(i,l,n) for(i=l;i<n;i++)

int main(void){
	int n,i,m=100000;

	scanf("%d",&n);
	rep(i,0,n){
		m=m*105/100;
		if(m%1000) m=m-m%1000+1000;
	}
	printf("%d\n",m );
	return 0;
}

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

int32_t main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int N , M;
	cin >> N >> M;
	int tot = 0;
	int tab[N];
	for (int i = 0 ; i < N ; i++)
	{
		 cin >> tab[i];
		 tot += tab[i];
	}
	sort(tab , tab  + N);
	for (int i = 0 ; i < M ; i++)
	{
		if (tot > tab[N - i - 1] * 4 * M)
		{
			cout<<"No"<<endl;
			return 0;
		}
	}
	cout<<"Yes"<<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <algorithm>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define stdi(in) freopen(in, "r", stdin)
#define stdo(out) freopen(out, "w", stdout)
#define stdio(in, out) stdi(in); stdo(out)
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define MAX 100007
#define MOD 1000000007
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
#define ordered_set tree<int , null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

const int N = 3007;

ll dp[N][N], pref[N][N];

int main(){
    /*#ifndef ONLINE_JUDGE
        stdio("in", "out");
    #endif*/
    fast;
    int n;
    string s;
    cin>>n;
    cin>>s;

    dp[1][1] = 1;
    pref[1][1] = 1;

    for(int l=2;l<=n;l++){
    	for(int i=1;i<l;i++){
    		pref[l-1][i] = pref[l-1][i-1]+dp[l-1][i];
    		if(pref[l-1][i]>=MOD) pref[l-1][i]-=MOD;
    	}

    	for(int curr=1;curr<=l;curr++){
    		int _l,r;
    		if(s[l-2]=='<'){
    			_l=1;
    			r=curr-1;
    		} else {
    			_l = curr;
    			r = l-1;
    		}

    		dp[l][curr] = (dp[l][curr] + pref[l-1][r] - pref[l-1][_l-1] + MOD) % MOD;
    	}

    }

    ll ans = 0;
    for(int i=1;i<=n;i++){
    	//cerr<<"i, dp[n][i] == "<<i<<' '<<dp[n][i]<<'\n';
    	ans+=(dp[n][i]);
    	if(ans>=MOD){
    		ans-=MOD;
    	}
    }

    cout<<ans<<'\n';

    return 0;   
}

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

typedef unsigned long long int ull;
typedef long double ldb;
typedef long long int ll;

#define MOD1        1000000007
#define MOD2        998244353
#define PI          3.1415926535897932384626433832795
#define FILL(a, value) memset(a, value, sizeof(a))
#define BOOST       ios_base::sync_with_stdio(false); cin.tie(NULL)
#define what_is(x)  cerr << #x << " is " << x << endl;
#define ASSERT(left,operator,right) { if(!((left) operator (right))){ std::cerr << "ASSERT FAILED: " << #left << #operator << #right << " @ " << __FILE__ << " (" << __LINE__ << "). " << #left << "=" << (left) << "; " << #right << "=" << (right) << std::endl; } }

#define rep01(k,n)      for (ll i=k; i<n; i++)
#define rep02(k,n)      for (ll j=k; j<n; j++)
#define rep03(k,n)      for (ll k=k; k<n; k++)
#define rep11(k,n)      for (ll i=k; i<=n; i++)
#define rep12(k,n)      for (ll j=k; j<=n; j++)
#define rep13(k,n)      for (ll k=k; k<=n; k++)
#define repIT(c,it)     for(typeof(c.begin()) it = c.begin(); it != c.end(); ++it)
#define all(c)      c.begin(), c.end()

using namespace std;

const double pi = acos(-1.0);
const double epsilon = 1e-9;        // Add epsilon when casting double to int to avoid rounding pitfall issues, eg, 0.94*8178 in int
const int maxn = 1e6+10;


void solve()
{
    ll n;
    cin >> n;
    
    ll lim = 50000/1.08;

    //cout << lim << "\n";

    for(int i = 1; i <= lim+1; i++)
    {
        if((int)(i*1.08) == n)
        {
            cout << i;
            return;
        }
    }
    cout << ":(";

}

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

    cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl;
    return 0;
}

Measured memory_bytes: 3776
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

int main(void){
    int X, A, B;
    cin >> X >> A >> B;
    if(abs(X-A) >= abs(X-B))
        cout << 'B' << endl;
    else
        cout << 'A' << endl;
    return 0;
}

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

const int N = 1000010;
const int moder = 998244353;
const int inv2 = (moder + 1) / 2;
using ll = long long;

int inv[N], mu[N], min[N];
ll sum[N], sum2[N];
std::vector <int> prime;

int main(){
    inv[1] = 1;
    for (int i = 2; i < N; ++ i){
        inv[i] = moder - 1ll * (moder / i) * inv[moder % i] % moder;
    }
    mu[1] = 1;
    for (int i = 2; i < N; ++ i){
        if (!min[i]){
            min[i] = i;
            mu[i] = -1;
            prime.push_back(i);
        }
        for (auto u : prime){
            if (i * u >= N) break;
            min[i * u] = u;
            if (i % u == 0) break;
            mu[i * u] = -mu[i];
        }
    }
    int n;
    scanf("%d", &n);
    for (int i = 0, x; i < n; ++ i){
        scanf("%d", &x);
        sum[x] += x;
        sum2[x] += 1ll * x * x;
    }
    for (int i = 1; i < N; ++ i){
        for (int j = i + i; j < N; j += i){
            sum[i] += sum[j];
            sum2[i] += sum2[j];
        }
    }
    for (int i = 1; i < N; ++ i){
        sum[i] %= moder;
        sum2[i] %= moder;
        sum[i] = (1ll * sum[i] * sum[i] - sum2[i]) % moder;
    }
    int ans = 0;
    for (int i = 1; i < N; ++ i){
        int tot = 0;
        for (int j = i; j < N; j += i){
            tot = (tot + 1ll * mu[j / i] * sum[j]) % moder;
        }
        ans = (ans + 1ll * inv[i] * tot) % moder;
    }
    ans = 1ll * ans * inv2 % moder;
    ans += ans < 0 ? moder : 0;
    printf("%d\n", ans);
    return 0;
}


Measured memory_bytes: 28024
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
    std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
    std.typecons, std.container, std.format;
static import std.ascii;

// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on

void main()
{
    long N, K;
    scan(N, K);
    auto G = new long[][](N);
    foreach (i; 0 .. N - 1)
    {
        long a, b;
        scan(a, b);
        a--, b--;
        G[a] ~= b;
        G[b] ~= a;
    }

    auto visited = new bool[](N);
    auto C = Combination_mod(K + 1);

    // writeln(C._fact);

    long dfs1(long p, long v)
    {
        if (visited[v])
            return 1;
        visited[v] = true;
        long x = (p == -1) ? C(K - 1, G[v].length) : C(K - 2, (cast(long) G[v].length) - 1);
        // writefln("%s %s %s %s", v, G[v].length, x, C._fact[G[v].length - 1 - min(0, p)]);
        x *= C._fact[G[v].length - 1 - min(0, p)];
        x %= MOD;
        foreach (w; G[v])
            if (w != p)
            {
                x *= dfs1(v, w);
                x %= MOD;
            }
        return x;
    }

    writeln(K * dfs1(-1, 0) % MOD);
}

/// Number of k-combinations % m (precalculated)
alias Combination_mod = Combination_modImpl!long;
struct Combination_modImpl(T)
{
    T _n, _m;
    T[] _fact, _factinv;
    this(T maxnum, T mod = 10 ^^ 9 + 7)
    {
        _n = maxnum, _m = mod, _fact = new T[](_n + 1), _factinv = new T[](_n + 1), _fact[0] = 1;
        foreach (i; 1 .. _n + 1)
            _fact[i] = _fact[i - 1] * i % _m;
        T powmod(T x, T n, T m)
        {
            if (n < 1)
                return 1;
            if (n & 1)
            {
                return x * powmod(x, n - 1, m) % m;
            }
            T tmp = powmod(x, n / 2, m);
            return tmp * tmp % m;
        }

        foreach (i; 0 .. _n + 1)
            _factinv[i] = powmod(_fact[i], _m - 2, _m);
    }

    T opCall(T n, T k, T dummy = 10 ^^ 9 + 7)
    {
        return n < k ? 0 : ((_fact[n] * _factinv[n - k] % _m) * _factinv[k] % _m);
    }
}


Measured memory_bytes: 23292
#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T1,typename T2>
ostream &operator<<(ostream &os,const pair<T1,T2>&p){
    os<<p.first<<" "<<p.second;
    return os;
}
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}

template<typename T1,typename T2>
istream &operator>>(istream &is,pair<T1,T2>&p){
    is>>p.first>>p.second;
    return is;
}

template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

struct UnionFind{
    vector<int> par,siz;
    int con;
    UnionFind(int n){
        par.resize(n);
        iota(begin(par),end(par),0);
        siz.assign(n,1);
    }
    int root(int x){
        return (par[x]==x?x:(par[x]=root(par[x])));
    }
    bool sameroot(int x,int y){
        return root(x)==root(y);
    }
    bool unite(int x,int y){
        x=root(x);y=root(y);
        if(x==y) return false;
        if(siz[x]<siz[y])swap(x,y);
        siz[x]+=siz[y];
        par[y]=x;
        con--;
        return true;
    }
    int size(int x){
        return siz[root(x)];
    }
};


signed main(){
    int n;cin>>n;
    using P=pair<int,int>;
    using T=tuple<int,int,int>;
    vector<T> v(n);
    rep(i,n){
        int x,y;cin>>x>>y;
        v[i]=T(x,y,i);
    }

    sort(ALL(v));

    priority_queue<P> que;// height, uf root

    UnionFind uf(n);

    for(int i=n-1;i>=0;i--){
        auto [x,y,me]=v[i];
        int maxheight=y;
        while(!que.empty() and que.top().first>y){
            auto [height, id]=que.top();que.pop();
            chmax(maxheight,height);
            uf.unite(id,me);
        }
        que.push(P(maxheight, uf.root(me)));
    }
    rep(i,n){
        cout<<uf.size(i)<<endl;
    }
    return 0;
}


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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = (1<<30) - 1;
const ll LINF = (1LL<<60) - 1;

int main(){
    ll n, k;
    cin >> n >> k;
    cout << min(n % k, k - n % k) << endl;
}

Measured memory_bytes: 3592
#include<iostream>
#include<cstdio>
#define MN 5000
#define MM 200
#define ll long long
using namespace std;
inline int read()
{
    int x=0;char ch=getchar();
    while (ch<'0'||ch>'9')ch=getchar();
    while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x;
}
int n,m,B[MN+5][MM+5];
ll d[MN+5],ans,Add[MN+5],f[MN+5][MN+5],q[MN+5],top,la[MN+5];
int main()
{
    n=read();m=read();
    for(int i=2;i<=n;++i) d[i]=d[i-1]+read();
    for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) B[i][j]=read();
    for(int i=1;i<=m;++i)
    {
        top=0;
        for(int j=1;j<=n;++j)
        {
            while(top&&B[q[top]][i]<=B[j][i])
                f[la[q[top]]][j]-=B[q[top]][i],f[q[top]+1][j]+=B[q[top]][i],--top;
            la[j]=q[top]+1;f[la[j]][j]+=B[j][i];f[j+1][j]-=B[j][i];q[++top]=j;
        }
    }
    for(int i=1;i<=n;++i) for(int j=1;j<=n;++j)
    {
        f[i][j]+=f[i-1][j]+f[i][j-1]-f[i-1][j-1];
        if(i<=j) ans=max(ans,f[i][j]-d[j]+d[i]);
    }
    cout<<ans;
    return 0;
}


Measured memory_bytes: 200064
#include <bits/stdc++.h>
using namespace std;
int n,k,r,s,p,ans=0,i;
string t;
int main()
{
    cin.tie(0);
	ios::sync_with_stdio(false);
    cin>>n>>k>>r>>s>>p>>t;
    for(i=0;i<n;i++){
        if(i>=k&&t[i]==t[i-k])t[i]=0;
        if(t[i]=='s')ans+=r;
        if(t[i]=='p')ans+=s;
        if(t[i]=='r')ans+=p;
    }
  	cout<<ans<<endl;
}


Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  string S, T; cin >> S >> T;
  int s = S.size(), t = T.size();
  int count = 0;
  for(int i = 0; i <= s - t; i++) {
    int cnt = 0;
    for(int j = 0; j < t; j++) {
      if(S.at(i + j) == T.at(j)) cnt++;
    }
    if(count < cnt) count = cnt;
  }
  cout << t - count << endl;
}


Measured memory_bytes: 3628
N = int(input())
for h in range(1, 3501):
  for n in range(1, 3501):
    a = N*h*n
    b = 4*h*n - N*n - N*h
    if b == 0:
      continue
    if a % b:
      continue
    if a//b < 0:
      continue
    print(h, n, a//b)
    exit()


Measured memory_bytes: 66964
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define irep(i, n) for (int i = (n); i >= 0; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1 << 25;
const int MOD = 1e9+7;
 
int main() {
	int a, b;
	cin >> a >> b;
	cout << 6 - a - b;
	
	return 0;
}

Measured memory_bytes: 3592
import java.util.*;
import java.io.*;
import java.awt.geom.*;
import java.math.*;

public class Main {

	static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static final PrintWriter out = new PrintWriter(System.out,false);

	static void solve() throws IOException{
		String str;
		while ((str = br.readLine()) != null) {
			ArrayDeque<Double> stack = new ArrayDeque<Double>();
			String[] s = str.split(" ");
			for (int i=0; i<s.length; i++) {
				if ("+".equals(s[i])) {
					double x = stack.pollLast();
					double y = stack.pollLast();
					stack.addLast(x+y);
				} else if ("-".equals(s[i])) {
					double x = stack.pollLast();
					double y = stack.pollLast();
					stack.addLast(y-x);
				} else if ("*".equals(s[i])) {
					double x = stack.pollLast();
					double y = stack.pollLast();
					stack.addLast(x*y);
				} else if ("/".equals(s[i])) {
					double x = stack.pollLast();
					double y = stack.pollLast();
					stack.addLast(y/x);
				} else {
					stack.addLast(Double.parseDouble(s[i]));
				}
			}

			out.println(stack.pollLast());
		}
	}

	public static void main(String[] args) throws Exception {
		long start = System.currentTimeMillis();

		solve();
		out.flush();

		long end = System.currentTimeMillis();
		//trace(end-start + "ms");
		br.close();
		out.close();
	}

	static void trace(Object... o) { System.out.println(Arrays.deepToString(o));}
}

Measured memory_bytes: 24428
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.buffer.readline

'''
dp[i,j]=部分文字列が一致する最長 s[:i],t[:j]文字目まで考慮したとき

chmax(dp[i+1,j],dp[i,j])
chmax(dp[i,j+1],dp[i,j])
if s[i]==t[j]:
    chmax(dp[i+1,j+1],dp[i,j]+1)

完成したdpテーブルの終端からからやればおk
s[i]とv[j]が同じ文字だったら斜め上(i-1,j-1)にもどってそれ以外はdp[i-1,j]ordp[i,j-1]のうちにdp[i,j]と値が同じ方に戻ることにする
'''

import numpy as np
from functools import partial
array = partial(np.array, dtype=np.int64)
zeros = partial(np.zeros, dtype=np.int64)
full = partial(np.full, dtype=np.int64)

S = array(list(read()[:-1]))
T = array(list(read()[:-1]))

from numba import njit


@njit('(i8[:],i8[:],)', cache=True)
def solve(S, T):
    s = len(S)
    t = len(T)
    dp = np.zeros((len(S) + 1, len(T) + 1))
    for i in range(1, s + 1):
        for j in range(1, t + 1):
            dp[i, j] = max(dp[i, j], dp[i - 1, j])
            dp[i, j] = max(dp[i, j], dp[i, j - 1])
            if S[i - 1] == T[j - 1]:
                dp[i, j] = max(dp[i, j], dp[i - 1, j - 1] + 1)
    # 以下復元
    i, j = s, t
    ans = []
    while dp[i, j] != 0:
        if S[i - 1] == T[j - 1]:
            ans.append(S[i - 1])
            i -= 1
            j -= 1
            continue
        if dp[i, j] == dp[i - 1, j]:
            i -= 1
        else:
            j -= 1
    return ans


ret = solve(S, T)
print(''.join(map(chr, ret[::-1])))


Measured memory_bytes: 176868
#include <bits/stdc++.h>
#define REP(i, n) for(ll i = 0; i < (ll)n; i++)
#define FOR(i, a, b) for(ll i = (a); i < (ll)b; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF (1ll << 60)
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
using vl = vector<ll>;
using vvl = vector<vl>;
typedef double db;
typedef string str;
typedef pair<ll, ll> p;
constexpr int MOD = 1000000007;
using ll = long long;
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

void print(const std::vector<int> &v) {
    std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
    std::cout << std::endl;
}

int main() {
    ll A, B;
    int K;
    cin >> A >> B >> K;
    REP(i, K) {
        if(i % 2 == 0) {
            if(A % 2 == 1) {
                A--;
            }
            B += A / 2;
            A = A / 2;
        } else {
            if(B % 2 == 1) {
                B--;
            }
            A += B / 2;
            B = B / 2;
        }
    }
    cout << A << " " << B << endl;
    return 0;
}

Measured memory_bytes: 256
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
#define REP(var, a, b) for (int var = (a); var < (b); var++)
#define rep(var, n) for (int var = 0; var < (n); ++var)
#define ALL(c) (c).begin(), (c).end()
#define rALL(c) (c).rbegin(), (c).rend()
ll MOD = 1000000007;
ll INF = 1000000000;

int main() {
  //
  ll n, m;
  cin >> n >> m;
  vvl edges(n);
  vl cnt(n, 1);
  set<ll> pts;
  pts.insert(0);
  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    x--, y--;
    if (pts.find(x) != pts.end()) {
      if (cnt[x] == 1) {
        pts.erase(pts.find(x));
      }
      pts.insert(y);
    }
    cnt[x]--;
    cnt[y]++;
  }
  cout << pts.size() << endl;
  return 0;
}


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

int main() {
  int A,B,C,D,E;
  cin >> A >> B >> C >> D >> E;
  
  if (A == 0) {
    cout << 1 << endl;
  }
  if (B == 0) {
    cout << 2 << endl;
  }
  if (C == 0) {
    cout << 3 << endl;
  }
  if (D == 0) {
    cout << 4 << endl;
  }
  if (E == 0) {
    cout << 5 << endl;
  }
}

Measured memory_bytes: 3588