akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> Vi;
typedef vector<ll> Vl;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;


//===

const ll mod = static_cast<ll>(1e9 + 7);

ll mpow(ll a, ll n) {
	ll res = 1;
	while (n != 0) {
		if (n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}

ll mfrac(ll a) {
	ll res = 1;
	for (ll i = 2; i <= a; ++i) res = res * i % mod;
	return res;
}

ll mcomb(ll n, ll k) {
	ll res;
	res = mfrac(n) * mpow(mfrac(k), mod - 2) % mod;
	res = res * mpow(mfrac(n - k), mod - 2) % mod;
	return res;
}


int main() {
	ll N, M, K;
	cin >> N >> M >> K;

	vector<Pll> cells;
	for (ll i = 0; i < N * M; ++i) {
		cells.emplace_back(i / M, i % M);
	}

	ll ans = 0;
	ll ans_x = 0, ans_y = 0;
	ll cmb = mcomb(N * M - 2, K - 2);
	for (ll i = 1; i < N; ++i) {
		ll tmp;
		tmp = (N - i) * mpow(M, 2) % mod;
		tmp = tmp * i % mod;
		ans_x = (ans_x + tmp) % mod;
	}
	for (ll i = 1; i < M; ++i) {
		ll tmp;
		tmp = (M - i) * mpow(N, 2) % mod;
		tmp = tmp * i % mod;
		ans_y = (ans_y + tmp) % mod;
	}


	ans = (ans_x + ans_y) * cmb % mod;

	cout << ans << endl;
}

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

using namespace std;

typedef unsigned long long ull;

int N,h[100001],cost[100001];

int main()
{
    cin>>N;
    for(ull i = 1; i<=N; i++){
        cin>>h[i];
    }

    cost[1] = 0;
    cost[2] = abs(h[2] - h[1]);

    for (ull i = 3; i<=N; i++){
        cost[i] = min(cost[i-1] + abs(h[i] - h[i-1]), cost[i-2] + abs(h[i] - h[i-2]));
    }

    cout<<cost[N]<<endl;
    return 0;
}


Measured memory_bytes: 4348
using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using static System.Console;
using static System.Math;

namespace AtCoder
{
    public class Program
    {
        public static void Main(string[] args)
        {
            new Program().Solve(new ConsoleInput(Console.In, ' '));
        }

        public void Solve(ConsoleInput cin)
        {
            var N = cin.ReadInt;
            var a = cin.ReadIntArray(N);

            var ans = 0;
            var val = 0;
            for (int i = 0; i < N; i++)
            {
                if (a[i] != val + 1)
                {
                    ans++;
                }
                else
                {
                    val++;
                }
            }

            if (ans == N)
            {
                ans = -1;
            }
            Console.WriteLine(ans);
        }
    }

    public class ConsoleInput
    {
        private readonly System.IO.TextReader _stream;
        private char _separator = ' ';
        private Queue<string> inputStream;
        public ConsoleInput(System.IO.TextReader stream, char separator = ' ')
        {
            this._separator = separator;
            this._stream = stream;
            inputStream = new Queue<string>();
        }
        public string Read
        {
            get
            {
                if (inputStream.Count != 0) return inputStream.Dequeue();
                string[] tmp = _stream.ReadLine().Split(_separator);
                for (int i = 0; i < tmp.Length; ++i)
                    inputStream.Enqueue(tmp[i]);
                return inputStream.Dequeue();
            }
        }
        public string ReadLine { get { return _stream.ReadLine(); }}
        public int ReadInt { get { return int.Parse(Read); }}
        public long ReadLong { get { return long.Parse(Read); }}
        public double ReadDouble { get { return double.Parse(Read); }}
        public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret;}
        public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret;}
        public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret;}
    }
}


Measured memory_bytes: 35452
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main () {
    vector<string> x;
    int N, cnt=0;
    cin >> N;
    for (int i=0;i<N;i++){
        string M;
        cin >> M;
        x.push_back(M);
        if (i>0){
            for (int j=0;j<i;j++){
                if (x[j]==x[i])
                    cnt++;
            }
            if (x[i-1][x[i-1].size()-1]!=x[i][0])
                cnt++;
        }
    }
    cnt==0 ? cout << "Yes" : cout << "No";
    return 0;
}

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

int main(){
    int n,m;
    cin  >>n>>m;
    int a[100][100];
    for(int i =0;i<n;i++)
       for(int j =0;j<m;j++) cin >>a[i][j];
        

    int b[100];
    for(int i = 0;i<m;i++) cin >>b[i];
    int c[100] = {};
    for (int i=0;i<n;i++)
         for(int j =0;j<m;j++)c[i] +=a[i][j]*b[j];
         
    for(int i = 0;i<n;i++) cout <<c[i]<<endl;

     return 0;
   }


Measured memory_bytes: 3120
s = list(input())
k = int(input())
ans=[]
for c in s:
    if (ord('a')-ord(c))%26 <= k:
        ans.append('a')
        k-=(ord('a')-ord(c))%26
    else:
        ans.append(c)
ans[-1]=chr(ord(ans[-1])+k%26)
print(''.join(ans))


Measured memory_bytes: 4920
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <numeric>
#include <random>
#include <algorithm>
#include <functional>
#include <cassert>

using namespace std;
typedef long long ll;

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

const int INF = (1 << 30) - 1;
const ll INFLL= (1LL << 61) - 1;
const int MOD = 1000000007;
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int A,B,C,D;
  bool taka;
  cin>>A>>B>>C>>D;
  REP(i,10000){
    if(i%2==0){
      C-=B;
      if(C<=0){
        taka=true;
        break;
      }
    }else{
      A-=D;
      if(A<=0){
        taka=false;
        break;
      }
    }
  }
  if(taka){
    cout<<"Yes"<<endl;
  }else{
    cout<<"No"<<endl;}
}


Measured memory_bytes: 3604
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
#include<map>

#define INF 1000000001
#define LINF 1000000000000000001

using namespace std;

typedef long long Int;
typedef pair<Int, Int> P;

Int gcd(Int x, Int y){
	if(y == 0)
		return x;
	return gcd(y, x % y);
}

Int lcm(Int x, Int y){
	return x * y / gcd(x, y);
}

int sx, sy, tx, ty;

int main(){
	cin >> sx >> sy >> tx >> ty;
	vector<char> v;
	for(int i = 0; i < ty - sy; i++)
		v.push_back('U');
	for(int i = 0; i < tx - sx; i++)
		v.push_back('R');
	for(int i = 0; i < ty - sy; i++)
		v.push_back('D');
	for(int i = 0; i < tx - sx; i++)
		v.push_back('L');
	v.push_back('L');
	for(int i = 0; i <= ty - sy; i++)
		v.push_back('U');
	for(int i = 0; i <= tx - sx; i++)
		v.push_back('R');
	v.push_back('D');
	v.push_back('R');
	for(int i = 0; i <= ty - sy; i++)
		v.push_back('D');
	for(int i = 0; i <= tx - sx; i++)
		v.push_back('L');
	v.push_back('U');
	for(int i = 0; i < v.size(); i++){
		cout << v[i];
	}
	cout << endl;
	
	return 0;
}

Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
#define Rep(i,n) for(int i=0;i<(int)(n);i++)
#define For(i,n1,n2) for(int i=(int)(n1);i<(int)(n2);i++)
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define RREP(i,n) for(ll i=((ll)(n)-1);i>=0;i--)
#define FOR(i,n1,n2) for(ll i=(ll)(n1);i<(ll)(n2);i++)
#define put(a) cout<<a<<"\n"
#define all(a)  (a).begin(),(a).end()
#define SORT(a) sort((a).begin(),(a).end())
#define oorret 0
#define oor(x) [&](){try{x;} catch(const out_of_range& oor){return oorret;} return x;}()
#define SZ(x) ((ll)(x).size())
typedef long long ll;
typedef pair<int, int> P;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){if(a>b){a=b;return 1;}return 0;}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){if(a<b){a=b;return 1;}return 0;}

int main(){
    int n;
    cin >> n;
    vector<ll> a(1);
    cin >> a[0];
    REP(i,n-1){
        int inp;
        cin >> inp;
        if(a.back()!=inp){
            a.push_back(inp);
        }
    }
    int res = 1;
    bool inc = false;
    bool dec = false;
    int m = a.size();
    REP(i,m-1){
        if(inc){
            if(a[i]>a[i+1]){
                inc = false;
                //dec = true;
                res++;
            }
        }else if(dec){
            if(a[i]<a[i+1]){
                dec = false;
                //inc = true;
                res++;
            }
        }else{
            if(a[i]<a[i+1]){
                inc = true;
            }else{
                dec = true;
            }
        }
    }
    put(res);
    return 0;
}


Measured memory_bytes: 1400
// "include" {{{
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <cstring>
#include <iomanip>
#include <tuple>
// }}}
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define reps(i,n) for(int i=1;i<=(n);++i)
#define all(x) (x).begin(),(x).end()
#define pii pair<int,int> 
#define int long long
#define setout(n,x) setw(n) << setfill(x)
#define Fixed fixed << setprecision(10)
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const long long mod = 1000000007;

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int x,y,z;
	cin >> x >> y >> z;
	int cnt = 0;
	x -= z;
	while(true){
		if(x - (y+z) < 0) break;
		x -= y+z;
		++cnt;
	}
	cout << cnt << '\n';
	return 0;
}


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

int main(){
  long long int ans[21];
  long long int bans[21];
  int n;
  cin >> n;
  int a; cin >> a;
  for(int i = 0; i <= 20; i++){
    ans[i] = 0;
        bans[i] = 0;
  }
  ans[a] = 1;
   int b;
  for(int j = 0; j < n-2; j++){
   
    cin >> b;
    for(int i = 0; i < 21; i++){
      if(ans[i] > 0){
        if(i-b >= 0)bans[i-b] += ans[i];
        if(i+b <= 20)bans[b+i] +=  ans[i];
      }
    }
    for(int i = 0; i <= 20; i++){
      ans[i] = bans[i];
      bans[i] = 0;
    }
  }

  cin >> b;

  cout << ans[b] << endl;
}



Measured memory_bytes: 3120
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
#define fi first
#define se second
#define endl '\n'
#define y1 holyshit
#define all(x) x.begin(),x.end()
const int inf=0x3f3f3f3f;

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	string S,T; cin>>S>>T;
	int ans=inf;
	for(int i=0;i<S.size()-T.size()+1;i++){
		int cnt=0;
		for(int j=0;j<T.size();j++) if(S[i+j]!=T[j]) cnt++;
		ans=min(ans,cnt);
	}
	cout<<ans;
	return 0;
}

Measured memory_bytes: 3616
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define F first
#define S second
#define endl "\n"
#define nl cout<<endl
#define pb push_back
#define f(i,a,b) for(ll i=a;i<b;i++)
#define Knucklehead ios_base::sync_with_stdio(false);cin.tie(NULL);
#define txti freopen("input.txt", "r", stdin);
#define txto freopen("output.txt", "w", stdout);
//#define mod 100000007

#define mat vector<vector<ll>>
mat cn(ll n, ll m){return vector< vector<ll> >(n, vector<ll>(m));}
bool compare(char &s1,char &s2){return s1 > s2;} 

bool sortmahstyle(const pair<ll,ll> &a,  const pair<ll,ll> &b) 
{ 
    if(a.second > b.second )
        return true;
    if( a.second==b.second && a.first > b.second )
       return true;
 
    return false;
 }
//me
ll const mod = 1e9+7;//998244353;
ll const inf = 1e3+1;

void solve()
{   
    string s,t; cin >> s >> t;
    ll n = s.size();
    ll m = t.size();
    ll ans = LLONG_MAX;
    f(i,0,n-m+1){
        ll cnt = 0;
        ll zz = i;
        f(j,0,m){

            if(s[zz++]!=t[j])cnt++;

        }
        ans = min(ans , cnt);
    }
    cout << ans << endl;
} 

int main() {
Knucklehead 
//*
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
/**/ 
        
        ll t;
        /**/t=1;
        /*/cin>>t;/**/
        f(testcases,0,t){
            solve();
        }
 
 
    return 0;
}


Measured memory_bytes: 3652
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int h, w, a[85][85], b[85][85], grid[85][85][16000], ans;

int min(int x,int y){
	if(x<y)return x;
	else return y;
}

void input(void) {
	scanf("%d%d",&h,&w);
	for (int i = 0; i < h; i++)for (int j = 0; j < w; j++)scanf("%d",&a[i][j]);
	for (int i = 0; i < h; i++)for (int j = 0; j < w; j++)scanf("%d",&b[i][j]);
	ans = 1000000000;
	memset(grid, -1, sizeof grid);
}

void solve(int x, int y, int diff) {
	if (x == h - 1 && y == w - 1) {
		ans = min(ans, abs(diff + (a[x][y] - b[x][y])));
		ans = min(ans, abs(diff - (a[x][y] - b[x][y])));
		return;
	}
	if (x > h || y > w)return;
	if (grid[x][y][diff] != -1)return;
	grid[x][y][diff] = 1;
	solve(x + 1, y, abs(diff + (a[x][y] - b[x][y])));
	solve(x + 1, y, abs(diff - (a[x][y] - b[x][y])));
	solve(x, y + 1, abs(diff + (a[x][y] - b[x][y])));
	solve(x, y + 1, abs(diff - (a[x][y] - b[x][y])));
}

int main() {
	input();
	solve(0, 0, 0);
	printf("%d\n",ans);
	return 0;
}

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

bool digit(const char c) { return '0' <= c && c <= '9'; }

bool f(const string s, int a) {
  rep(i, 0, a) if (!digit(s[i])) return false;

  if (s[a] != '-') return false;

  rep(i, a + 1, s.size()) if (!digit(s[i])) return false;

  return true;
}

int main() {
  int A, B;
  cin >> A >> B;
  string S;
  cin >> S;

  if (f(S, A))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}


Measured memory_bytes: 3608
#include <stdio.h>
#define N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n , M[N][N];
int color[N] , d[N] , f[N] , tt;

void dfs_visit(int u){
  int v;
  color[u] = GRAY;
  d[u] = ++tt;
  for(v = 0 ; v < n; v++){
    if(M[u][v] == 0 ) continue;
    if(color[v] == WHITE){
      dfs_visit(v);
    }
  }
  color[u] = BLACK;
  f[u] = ++ tt;
}

void dfs(){
  int u;
  for(u = 0 ; u < n ; u++){
    color[u] = WHITE;
  }
  tt = 0;
  for(u = 0 ; u < n ; u++){
    if(color[u] == WHITE) dfs_visit(u);
  }
  for( u = 0 ; u < n ; u++){
    printf("%d %d %d\n" , u+1 , d[u] , f[u]);
  }
}

int main(){
  int u , v , k , i , j;

  scanf("%d" , &n);
  for(i = 0 ; i < n ; i++){
    scanf("%d %d" , &u , &k);
    u--;
    for(j = 0 ; j < k ; j++){
      scanf("%d" , &v);
      v--;
      M[u][v] = 1;
    }
  }

  dfs();

  return 0;
}



Measured memory_bytes: 2728
N = int(input())
A = list(map(int, input().split()))
count = [0 for i in range(N)]
count[-1] = 3

MOD = 1000000007
ways = 1
for n in A:
    ways *= count[n-1]
    count[n-1] -= 1
    count[n] += 1
    ways %= MOD
print(ways)


Measured memory_bytes: 56556
import java.io.*;
import java.util.*;
public class Main{
    public static void main(String[] args) throws IOException{
        MyReader r = new MyReader();      
        int N, M;
        {int[] a = r.ii(); N = a[0]; M = a[1];}
        int[] road = new int[N];
        for(int i = 0; i < M; i++){
            int[] city = r.ii();
            road[city[0]-1]++;
            road[city[1]-1]++;
        }
        for(int rd : road)
            println(rd);        
    }
    static void print(Object o){
        System.out.print(o.toString());
    }
    static void println(Object o){
        System.out.println(o.toString());
    }
    static int Int(String s){
        return Integer.parseInt(s);
    }
    static long Long(String s){
        return Long.parseLong(s);
    }
    static class MyReader extends BufferedReader{
        MyReader(){
            super(new InputStreamReader(System.in));
        }
        String s() throws IOException{
            return readLine();
        }
        String[] ss() throws IOException{
            return s().split(" ");
        }
        int i() throws IOException{
            return Int(s());
        }
        int[] ii() throws IOException{
            String[] ss = ss();
            int size = ss.length;
            int[] ii = new int[size];
            for(int j = 0; j < size; j++) ii[j] = Int(ss[j]);
            return ii;
        }
        long l() throws IOException{
            return Long(s());
        }
        long[] ll() throws IOException{
            String[] ss = ss();
            int size = ss.length;
            long[] ll = new long[size];
            for(int j = 0; j < size; j++) ll[j] = Long(ss[j]);            
            return ll;
        }
    }
}

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

using namespace std;

typedef long long ll;

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

	int N;
	cin >> N;

	vector<ll> A(N);
	rep(i, N) cin >> A[i];

	ll INF = 1e13;
	ll mn = INF;
	ll mx = -INF;
	int cnt = 0;
	rep(i, N){
		if(A[i] < 0){
			mx = max(mx, A[i]);
			cnt++;
		}else{
			mn = min(mn, A[i]);
		}
	}

	ll ans = 0;
	if(cnt % 2 == 0){
		rep(i, N){
			ans += abs(A[i]);
		}

		cout << ans << endl;
	}else{
		rep(i, N){
			ans += abs(A[i]);
		}

		if(mn != INF){
			ans += mx;
			ans -= mn;
			ans -= min(abs(mx), abs(mn));
			ans += max(abs(mx), abs(mn));
		}else{
			ans += 2 * mx;
		}

		cout << ans << endl;
	}


}


Measured memory_bytes: 1024
#include <iostream>
#include <vector>
#include <cmath>
#include <ctime>
#include <cassert>
#include <cstdio>
#include <queue>
#include <set>
#include <map>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <numeric>

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("input.txt", "rt", stdin);
#endif

    int n;
    cin >> n;
    vi a(n);
    forn(i, n) cin >> a[i], --a[i];
    int ans = 0;
    forn(i, n) if (a[a[i]] == i) ++ans;
    cout << ans / 2 << '\n';

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}


Measured memory_bytes: 640
#include<cstdio>
#include<iostream>
using namespace std;
char a[1008][1008];
bool vis[2][1008];
int n,m;
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        for(int j=0;j<m;j++)
            if(a[i][j]=='#')
            {
                vis[0][i]=true;
                vis[1][j]=true;
            }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<m;j++)
            if(vis[0][i]&&vis[1][j])cout<<a[i][j];
        if(vis[0][i])cout<<endl;
    }
    return 0;
}


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

using namespace std;

int  h, w;
char G[2002][2002];
int  ctr[2][2002][2002];

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

    cin >> h >> w;
    h += 2;
    w += 2;
    string tmp;

    for (int i = 0; i < h; i++) {
        if (i == 0 || i == h - 1) {
            for (int j = 0; j < w; j++) {
                G[i][j] = '#';
            }
        } else {

            cin >> tmp;
            tmp = '#' + tmp + '#';
            for (int j = 0; j < w; j++) {
                G[i][j] = tmp[j];
            }
        }
    }

    int flag[2] = {0};

    for (int j = 0; j < w; j++) {
        for (int i = 1; i < h; i++) {
            if (G[i][j] == '#') {

                for (int k = 0; k < flag[0]; k++) {
                    ctr[0][i - k - 1][j] = flag[0];
                }
                flag[0] = 0;

            } else {
                flag[0]++;
            }
        }
    }

    flag[0] = 0;
    flag[1] = 0;

    for (int i = 0; i < h; i++) {
        for (int j = 1; j < w; j++) {
            if (G[i][j] == '#') {

                for (int k = 0; k < flag[1]; k++) {
                    ctr[1][i][j - k - 1] = flag[1];
                }
                flag[1] = 0;

            } else {
                flag[1]++;
            }
        }
    }

    int ans = 0;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            ans = max(ans, ctr[0][i][j] + ctr[1][i][j]);
        }
    }
    cout << ans - 1;

    return 0;
}


Measured memory_bytes: 35456
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

bool ok(int st,int ed,vector<int> d){
    bitset<20000> dp;
    dp[st+10000] = 1;
    rep(i,d.size()){
        dp = (dp<<d[i] | dp>>d[i]);
    }
    return dp[10000+ed];
}

int main(){
    string s;
    cin >> s;
    int x,y;
    cin >> x >> y;
    vector<int> dx,dy;
    int now = 0;
    int xx = -1;
    bool xxx = true;
    rep(i,s.size()){
        if(s[i]=='T'){
            if(xxx&&dy.size()==0) xx = now;
            else if(xxx) dx.push_back(now);
            else dy.push_back(now);
            now = 0;
            xxx = !xxx;
        }else{
            ++now;
        }
    }
    if(xxx) dx.push_back(now);
    else dy.push_back(now);

    if(xx==-1){
        if(dx[0]==x&&y==0){
            cout << "Yes" << endl;
        }else{
            cout << "No" << endl;
        }
    }else if(ok(0,y,dy)&&ok(xx,x,dx)){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }

    return 0;
}

Measured memory_bytes: 3648
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long int;

int main() {
    int S, T, D; cin >> S >> T >> D;
    vector<ll> W(D);

    const ll INF = 1LL << 60;
    ll sum = 0, mi = 0;
    for(int i=0; i<D; i++) cin >> W[i], sum += W[i], mi = min(mi, sum);

    if(S + mi > T and sum >= 0) {
        cout << -1 << endl;
    }
    else if(S + mi <= T) {
        ll ans = 0, weight = S;
        for(int i=0; i<2*D; weight += W[i%D], i++) {
            ll n_weight = weight + W[i%D];
            if(n_weight <= T) {
                ans += i + 1;
                break;
            }
        }
        cout << ans << endl;
    }
    else {
        ll dec = -sum;
        ll rem = S - T;
        ll loop = (rem + min<ll>(0, mi)) / dec;
        rem -= loop * dec;
        ll ans = loop * D;

        // fprintf(stderr, "dec = %lld, rem = %lld, ans = %lld\n", dec, rem, ans);
        ll weight = S - dec * loop;
        for(int i=0; i<2*D; weight += W[i%D], i++) {
            ll n_weight = weight + W[i%D];
            if(n_weight <= T) {
                ans += i + 1;
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}



Measured memory_bytes: 3556
#include <bits/stdc++.h>
#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 print(s) cout << s << endl
#define acc(v) accumulate(v.begin(), v.end(), 0)
#define cinv(n,v) rep(i,n) cin >> v[i]
using namespace std;
const int INF = 1e9;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vint;
typedef vector<string> vstr;
typedef vector<char> vchar;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;
// const lint MOD = 998244353;
int x_pos[4] = {1,0,-1,0}, y_pos[4] = {0,1,0,-1};
int ctoi(char c) {
	if (c >= '0' && c <= '9') {
		return c - '0';
	}
	return 0;
}

char upper(char c){
	return c-0x20;
}

char lower(char c){
	return c+0x20;
}

void unique_vector(vector<int>& v){
	sort(all(v));
	v.erase(unique(all(v)), v.end());
}

// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

ll modpow(ll a, ll p){
    if(p==0)return 1;
    if(p%2){
        return a * modpow(a,p-1)%MOD;
    }else{
        ll halfp = p/2;
        ll halfa = modpow(a,halfp);
        return halfa*halfa%MOD;
    }
}

ll calcComb(ll a, ll b){
    if(b > a - b)return calcComb(a,a-b);

    ll ansMul = 1,ansDiv = 1;
    rep(i,b){
        ansMul *= a-i;
        ansDiv *= b-i;
        ansMul %= MOD;
        ansDiv %= MOD;
    }
    ll ans = ansMul * modpow(ansDiv, MOD - 2) % MOD;
    return ans;
}

int main() {
    ll N,M;cin>>N>>M;
    
    ll Mnokori = M;
    ll ans = 1;
    for(int i = 2; i * i <= Mnokori; i++){
        if(Mnokori % i != 0)continue;
        int cnt = 0;
        while(Mnokori % i == 0){
            cnt++; Mnokori /= i; 
        }
        ans *= calcComb(N + cnt - 1, cnt);
        ans %= MOD;
    }
    if(Mnokori != 1){
        ans *= calcComb(N + 1 - 1, 1);
        ans %= MOD;
    }
    cout << ans << endl;
}



Measured memory_bytes: 256
while 1:
    n = int(input())
    if n == 0:
        break

    p = []
    for i in range(n):
        lst = list(map(int, input().split()))[1:]
        p.append(lst)

    l = list(map(int, input().split()))[1:]

    ans = 0
    id = -1
    for i in range(n):
        sum = 0
        for j in l:
            if p[i].count(j):
                sum += 1
        
        if sum == len(l):
            ans += 1
            id = i + 1
    
    if ans == 1:
        print(id)
    else:
        print(-1)



Measured memory_bytes: 5664
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <set>
#include <map>
#define rep(i,n) for(int (i) = 0;(i) < (n);(i)++)

using namespace std;
typedef long long int ll;
const ll INF = 20000000000;

int main(){
    int N;
    cin>>N;
    double ans = 0;
    double temp;
    string str;
    for(int i = 0; i < N; i++){
        cin>>temp>>str;
        if(str=="BTC"){
            temp *= 380000.0;
        }
        ans += temp;
    }
    printf("%10lf\n",ans);
}


Measured memory_bytes: 764
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG //GCC5.4.1で配列の範囲外アクセスをエラーにする

//cmdで g++ test.cpp とするとコンパイルできる
//制限1秒なら10^6は余裕 10^7は多分間に合う 10^8はよほど単純でないと厳しい
// https://github.com/atcoder/live_library
// cppdbg: test.exeのterminalに標準入力を打てる

#define ALL(x) x.begin(),x.end()
#define get_tp(t,i) get<i>(t)
#define len(x) (ll)(x.size())
#define ub(a,x) (upper_bound(ALL(a),x)-a.begin())
#define lb(a,x) (lower_bound(ALL(a),x)-a.begin())
typedef long long ll; //long long
typedef pair<int,int> pii; //pair<int,int>
typedef pair<long long,long long> pllll; //pair<ll,ll>
typedef vector<int> vi; //vector<int>
typedef vector<vector<int>> vvi; //vector<vector<int>>
typedef vector<ll> vll; //vector<long long>
typedef vector<vector<ll>> vvll; //vector<vector<long long>>
typedef priority_queue<ll, vector<ll>, greater<ll> > gpq; //逆順priority_queue
typedef priority_queue<ll> pq;

//const int MOD=998244353;
const int MOD=1000000007; //10^9+7
const long double PI=3.1415926535897932;

int main(){
    int a,b,c;cin>>a>>b>>c;
    if(a==b){
        cout<<c<<endl;
    }else if(b==c){
        cout<<a<<endl;
    }else{
        cout<<b<<endl;
    }
}

Measured memory_bytes: 3608
#include "bits/stdc++.h"

// Begin {{{
using namespace std;

#ifndef DEBUG
#define dump(...)
#endif

template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }

template <class T>
inline vector<T> make_v(const T &initvalue, intmax_t sz) {
    return vector<T>(sz, initvalue);
}

template <class T, class... Args>
inline auto make_v(const T &initvalue, intmax_t sz, Args... args) {
    return vector<decltype(make_v<T>(initvalue, args...))>(sz, make_v<T>(initvalue, args...));
}

constexpr int INF = 0x3f3f3f3f;
constexpr int64_t LINF = 0x3f3f3f3f3f3f3f3fLL;
// }}} End

vector<size_t> g[200010];
vector<size_t> counter;
vector<bool> seen;

void dfs(size_t now_node) {
    seen[now_node] = true;
    for (auto &next_node: g[now_node]) {
        if (!seen[next_node]) {
            counter[next_node] += counter[now_node];
            dfs(next_node);
        }
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    size_t N, Q; cin >> N >> Q;
    counter.resize(N), seen.resize(N);

    for (size_t i = 0; i < N - 1; ++i) {
        size_t a, b; cin >> a >> b;
        a--, b--;
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }

    while (Q--) {
        size_t root, x; cin >> root >> x;
        root--;
        counter[root] += x;
    }

    dfs(0);

    for (size_t i = 0; i < N; ++i) {
        cout << counter[i] << (i == N - 1)[" \n"];
    }

    return 0;
}


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

typedef long long ll;

int main()
{
    ll n,sum=0;
    cin>>n;
    ll arr[n];
    for(ll i=0;i<n;i++){
        cin>>arr[i];
        sum+=arr[i];
    }
    sort(arr,arr+n);
    if(arr[n-1]<sum-arr[n-1]) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
//template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
 
/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
    sizeは(int)とキャストしよう
    ごちゃごちゃ場合分けを考える前に全探索は考えましたか？
*/

int N; 
int dfs(string s){
    if(stoll(s) > N) return 0;

    int res = 0;
    int cnt3 = 0, cnt5 = 0, cnt7 = 0;
    for(int i=0; i<s.size(); i++){
        if(s[i] == '3') cnt3++;
        if(s[i] == '5') cnt5++;
        if(s[i] == '7') cnt7++;
    }
    if(cnt3 && cnt5 && cnt7) res++;
    
    res += dfs(s + '3');
    res += dfs(s + '5');
    res += dfs(s + '7');

    return res;
}

int main(){
    cin >> N;

    cout << dfs("0") << endl;
}

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

const int N = 2e5 + 5;
int arr[N];
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    vector<pair<ll, ll>> vec;
    for (int i = 0; i < n; i++) {
        ll a, b;
        cin >> a >> b;
        vec.push_back({b, a});
    }
    sort(vec.begin(), vec.end());
    ll curr = 0;
    for (int i = 0; i < n; i++) {
        curr += vec[i].second;
        if (curr > vec[i].first)
            return cout << "No", 0;
    }
    cout << "Yes";
    return 0;
}


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

int main() {
  int r, g, b;
  cin >> r >> g >> b;
  int x = 100 * r + 10 * g + b;
  if(x % 4) cout << "NO" << endl;
  else cout << "YES" << endl;
  return 0;
}

Measured memory_bytes: 3604
s=input()
k=int(input())
n=len(s)
z=5*10**15
if int(s[0])!=1:
  print(int(s[0]))
else:
  x=0
  for i in range(n):
    if s[i]=='1':
      x+=1
    else:
      break
  if k>x:
    print(int(s[x]))
  else:
    print(1) 

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

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

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

#define FF first
#define SS second

template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}
template<class T>
istream& operator>>(istream& is, vector<T>& xs){
  for(auto& x: xs)
	is >> x;
  return is;
}
template<class S, class T>
ostream& operator<<(ostream& os, const pair<S,T>& p){
  return os << p.FF << " " << p.SS;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T>& xs){
  for(unsigned int i=0;i<xs.size();++i)
	os << (i?" ":"") << xs[i];
  return os;
}
template<class T>
void maxi(T& x, T y){
  if(x < y) x = y;
}
template<class T>
void mini(T& x, T y){
  if(x > y) x = y;
}
void debug(istringstream&){}
template <char sep=',', class Head, class... Tail>
void debug(istringstream& iss, Head&& head, Tail&&... tail)
{
  string name;
  getline(iss, name, ',');
  cerr << sep << name << "=" << head;
  debug(iss, forward<Tail>(tail)...);
}
#define DEBUG(...) \
  do{ \
    istringstream ss(#__VA_ARGS__); \
    debug<' '>(ss, __VA_ARGS__); \
    cerr<<endl; \
  }while(0)

const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1e9+7;

LL dp[100000+10];

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

  int N, W;
  cin >> N >> W;

  REP(i,N){
    LL w, v;
    cin >> w >> v;
    for(LL k=W;k>=w;--k){
      maxi(dp[k], dp[k-w] + v);
    }
  }

  cout << *max_element(ALL(dp)) << endl;

  return 0;
}


Measured memory_bytes: 1024
#include <iostream>
#include <list>
#include <stack>
using namespace std;

int main()
{
	int n;
	cin >> n;
	char c;
	list<char> li;
	for(int i = 0; i < n; ++i){
		cin >> c;
		li.push_back(c);
	}

	stack<char> ch;
	for(list<char>::iterator it = li.begin(); it != li.end(); ++it){
		if( *it == '(' ){
			ch.push('(');
		} else if( *it == ')' ){
			if( ch.empty() ){
				li.push_front('(');
			} else {
				ch.pop();
			}
		}
	}

	if( !ch.empty() ){
		int s = ch.size();
		for(int i = 0; i < s; ++i){
			li.push_back(')');
		}
	}


	for(list<char>::iterator it = li.begin(); it != li.end(); ++it){
		cout << *it;
	}
	cout << endl;


	return 0;
}

Measured memory_bytes: 256
def comb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10 ** 9 + 7
N = 10**4
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )


N, K = map(int, input().split())

blue, red = K, N - K

p = 1 
d = 0

for i in range(1, blue + 1):
    print(int((comb(red + 1, p, mod) * comb(blue - 1, d, mod) % mod)))
    p += 1
    d += 1


Measured memory_bytes: 4596
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

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

    int n, k;
    cin >> n >> k;
    vector<int> v(n);
    for(int i = 0; i < n; ++i)  cin >> v[i];
    int ans = 0;
    for(int i = 0; i <= n; ++i){
        for(int j = 0; j <= n; ++j){
            if(i + j > n || i + j > k)  continue;
            priority_queue<int, vector<int>, greater<int>> que;
            for(int idx = 0; idx < i; ++idx){
                que.push(v[idx]);
            }
            for(int idx = 0; idx < j; ++idx){
                que.push(v[n - idx - 1]);
            }
            int rest = k - i - j;
            while(!que.empty() && rest > 0){
                if(que.top() >= 0)  break;
                que.pop();
                --rest;
            }
            int sum = 0;
            while(!que.empty()){
                sum += que.top();
                que.pop();
            }
            chmax(ans, sum);
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
#define p 1000000007
using namespace std;
namespace ywy{
	typedef struct _mx{
		ll v[3][3];_mx(){memset(v,0,sizeof(v));}
		friend _mx operator *(const _mx &a,const _mx &b){
			_mx c;for(register int i=0;i<3;i++){
				for(register int k=0;k<3;k++){
					for(register int j=0;j<3;j++)c.v[i][j]=(c.v[i][j]+a.v[i][k]*b.v[k][j])%p;
				}
			}return(c);
		}
	}mx;
	inline mx mi(mx a,int b){
		mx ans,tmp=a;ans.v[0][0]=ans.v[1][1]=ans.v[2][2]=1;while(b){
			if(b&1)ans=ans*tmp;tmp=tmp*tmp;b>>=1;
		}return(ans);
	}int ints[1000001];ll f[120001],fx[120001],fx2[120001];
	void ywymain(){
		f[0]=1;int n,m;cin>>n>>m;mx dk;dk.v[0][0]=2;dk.v[0][1]=dk.v[0][2]=1;
		dk.v[1][0]=2;dk.v[1][1]=1;dk.v[1][2]=2;dk.v[2][0]=1;dk.v[2][2]=1;
		for(register int i=1;i<=m;i++){
			cin>>ints[i];mx cjr=mi(dk,ints[i]-ints[i-1]-1);
			ll df=(f[i-1]*cjr.v[0][0]+fx[i-1]*cjr.v[1][0]+fx2[i-1]*cjr.v[2][0])%p;
			ll dfx=(f[i-1]*cjr.v[0][1]+fx[i-1]*cjr.v[1][1]+fx2[i-1]*cjr.v[2][1])%p;
			ll dfx2=(f[i-1]*cjr.v[0][2]+fx[i-1]*cjr.v[1][2]+fx2[i-1]*cjr.v[2][2])%p;
			f[i]=df;fx[i]=(dfx+df)%p;fx2[i]=(dfx2+2*dfx+df)%p;
		}mx cjr=mi(dk,n-ints[m]),ywy=mi(dk,n-ints[m]-1);
		ll df0=(f[m]*ywy.v[0][0]+fx[m]*ywy.v[1][0]+fx2[m]*ywy.v[2][0])%p;
		ll df1=(f[m]*cjr.v[0][0]+fx[m]*cjr.v[1][0]+fx2[m]*cjr.v[2][0])%p;
		cout<<(df1-df0+p)%p<<endl;
	}
}
int main(){
	ywy::ywymain();return(0);
}

Measured memory_bytes: 3968
#include <iostream>
using namespace std;
int main(void){
    int n,k;
    
    int M;
    int m;
    M=0;
    m=123;
    for(int i=1;i<=5;i++){
        cin>>n;
        if(M<=n){
            M=n;
            
        }
        if(m>=n){
            m=n;
        }
    }
   
    cin>>k;
    if(M-m>k){
        cout<<":("<<endl;
    }else{
        cout<<"Yay!"<<endl;
    }
    
    return 0;
}

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

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = mod * mod;
const int INF_N = 1e+9;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acos(-1.0);
//typedef vector<vector<ll>> mat;
typedef vector<int> vec;

//繰り返し二乗法
ll mod_pow(ll a, ll n, ll m) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * a%m;
		a = a * a%m; n >>= 1;
	}
	return res;
}

struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n%mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint &a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint &a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint &a, modint b) { a.n = ((ll)a.n*b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, int n) {
	if (n == 0)return modint(1);
	modint res = (a*a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

//逆元(Eucledean algorithm)
ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

const int max_n = 1 << 18;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
using mP = pair<modint, modint>;

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



void solve() {
	ll N, M; cin >> N >> M;
	ll res = min(N%M, M-(N%M));

	cout << res << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  //cout << fixed << setprecision(10);
  //init_f();
  //init();
  //int t; cin >> t; rep(i, t)solve();
  solve();
//   stop
    return 0;
}


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

int *mth, *spj, a2[100], a1[100]; 

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

LL DFS(LL res, LL l, LL r) {
	if (l <= r) {
		if (res == 0) {
			return l == r? 10: 1;
		} else {
			return 0;
		}
	} else {
		LL ret = 0, cur = l - r;
		l /= 10; r *= 10;
		for (int i = -9; i <= 9; i++) {
			if (abs(res - i * cur) < l * 11) {
				ret += mth[i] * DFS(res - i * cur, l, r);				
			}
		}
		return ret;
	}
}

int main() {
	//#ifdef DBG
	//freopen("11input.in", "r", stdin);
	//#endif
	mth = a1 + 50;
	spj = a2 + 50;
	for (int i = 0; i <= 9; i++) {
		for (int j = -9; j <= 0; j++) {
			mth[i + j]++;
		}
	}
	for (int i = 1; i <= 9; i++) {
		for (int j = -9; j <= 0; j++) {
			spj[i + j]++;
		}
	}
	LL ans = 0, D = -read();
	for (LL mx = 1e18; mx >= 10; mx /= 10) {
		LL delta = mx - 1;
		for (int i = -8; i <= 9; i++) {
			ans += spj[i] * DFS(D - i * delta, mx / 10, 10);
		}
	}
	cout<<ans<<endl;
	return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
  
using namespace std;
  
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second
  
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

ll getSum(ll x) {
    ll ret = 0;
    while(x > 0) {
        ret += x %10;
        x /= 10;
    }
    return ret;
}

ll dp[20];

ll ten(ll x) {
    if(x < 0) return 0;
    if(dp[x] != 0) return dp[x];
    ll ret = 1;
    rep(i, x) ret *= 10;
    return dp[x] = ret;
}

int main(){
    ll n;
    cin >> n;
    ll ans = getSum(n);
    REP(i, 1, 18) {
        // if(ten(i) > n) break;
        ll tmp = n / ten(i) * ten(i);
        ll k = (n%ten(i)) / ten(i-1);
        if(k > 0) {
            tmp += (k-1)*ten(i-1) + ten(i-1)-1;
        }
        else {
            tmp -= ten(i);
            tmp += ten(i) - 1;
        }
        if(tmp <= n && tmp >= 0) ans = max(ans, getSum(tmp));
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
import math
N = int(input())
x = [0 for i in range(N)]
y = [0 for i in range(N)]
for i in range(N):
    x[i], y[i] = map(int, input().split())


def mindis(t):
    j = 0
    for i in range(N):
        if x[i]*math.cos(t)+y[i]*math.sin(t)<x[j]*math.cos(t)+y[j]*math.sin(t):
            j = i
    return j


r = [0 for i in range(N)]
M = 250000
for i in range(M):
    k = mindis(2*math.pi*i/M)
    r[k] += 1
S = sum(r)
for i in range(N):
    print(r[i]/S)


Measured memory_bytes: 44572
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<map>
#include<algorithm>
#include<set>
#include<deque>
#include<queue>
#include<stack>
#include<bitset>
#include<list>
#include<vector>
#include<forward_list>
#include<array>
#define ll long long
#define ull unsigned long long
#define lowbit(x) (x&(-x))
#define inf 0x3f3f3f3f
#define IOS std::ios::sync_with_stdio(false)
#define undocin cin.tie(NULL)
using namespace std;
const int maxn=3e5+7;
const int maxm=2e6+7;
int Read(){
	int out=0,flag=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')flag=-1;c=getchar();}
	while(c>='0'&&c<='9'){out=out*10+c-'0';c=getchar();}
	return flag*out;
}
int count(int n)
{
	int cnt=0;
	while(n)
	{
		cnt+=(n&1);
		n>>=1;
	}
	return cnt;
}
int A[20],x[20][20],y[20][20];
int main()
{
	int n,ans=0;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>A[i];
		for(int j=0;j<A[i];j++)
		{
			cin>>x[i][j]>>y[i][j];
		}
	}
	for(int i=0;i<(1<<n);i++)
	{
		int ok=1;
		for(int j=0;j<n;j++)
		{
			if(!(i&(1<<j)))continue;
			for(int k=0;k<A[j];k++)
			{
				if(((i>>(x[j][k]-1))&1)^y[j][k])ok=0;
			}
			if(!ok)break;
		}
		if(ok)ans=max(ans,count(i));
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 384
{-# LANGUAGE OverloadedStrings #-}
module Main where

import           Data.List
import           Control.Monad
import           Debug.Trace
import qualified Data.ByteString.Char8         as BC
import           Data.Maybe                     ( fromJust )
import qualified Data.Set                      as S
import qualified Data.IntMap.Strict            as IM
import qualified Data.Map.Strict               as M
import           Data.Char
import Data.Bits


readTuple2 :: IO (Int, Int)
readTuple2 = do
    [a, b] <- map (fst . fromJust . BC.readInt) . BC.words <$> BC.getLine
    return (a, b)

readTuple3 :: IO (Int, Int, Int)
readTuple3 = do
    [a, b, c] <- map (fst . fromJust . BC.readInt) . BC.words <$> BC.getLine
    return (a, b, c)

readInt :: IO Int
readInt = fst . fromJust . BC.readInt <$> BC.getLine

readInts :: IO [Int]
readInts = map (fst . fromJust . BC.readInt) . BC.words <$> BC.getLine

split :: (a -> Bool) -> [a] -> [[a]]
split f [] = []
split f as =
    let (fo, la) = span f as
        las      = case la of
            [] -> []
            xs -> split f $ tail xs
    in  case fo of
            [] -> las
            xs -> xs : las

solve :: Int -> [Int] -> Int
solve n as = 
    let 
        amap = IM.fromList $ zip [1..] as
        findRight total right
            | right > n = (total, right)
            | newTotal /= newXor = (total, right)
            | otherwise = findRight newTotal $ right + 1
            where
                newVal = amap IM.! right
                newTotal = total + newVal
                newXor = total `xor` newVal

        go (right, total, ans) left =
            let
                (newTotal, newRight) = findRight total right
                newNum = newRight - left
            in
                (newRight, newTotal - amap IM.! left, ans + newNum)
        (_, _, ans) = foldl' go (1, 0, 0) [1..n]
    in 
    ans


main = do
    n <- readInt
    as <- readInts
    print $ solve n as




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

int main() {
	long long n,m,i,j,k,c,cur,ans1=0,ans2=0,mod=1000000007;
	cin >> n >> m;
	cur=0;
	for(i=1; i<=n; i++){
		cin >> c;
		cur=(cur+c)%mod;
		ans1=(ans1+c*i-cur)%mod;
	}
	cur=0;
	for(i=1; i<=m; i++){
		cin >> c;
		cur=(cur+c)%mod;
		ans2=(ans2+c*i-cur)%mod;
	}
	c=(ans1*ans2)%mod;
	if(c<0) c+=mod;
	cout << c;
	return 0;
}

Measured memory_bytes: 256