akhauriyash/Code-Regression huggingface dataset:
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vll = vector<vector<ll>>;
using pii = pair<int,int>;
#define FOR(i,a,b) for(ll i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(obj) (obj).begin(), (obj).end()
#define pb push_back
const double PI = acos(-1);
const double EPS = 1e-10;
const ll MOD = 1000000007;
vl a1(5);
vl sousuu,pat;
void cioacc(){//accelerate cin/cout
  cin.tie(0);
  ios::sync_with_stdio(false);
}
ll solve(ll a,ll x){
  if(x==0) return 0;
  if(a < 2) return a1[x];
  if(x <= sousuu[a-2]) return solve(a-1,x-1);
  else if(x == sousuu[a-2]+1) return pat[a-2] + 1;
  else if(x == sousuu[a-1]-1) return pat[a-1];
  else return solve(a-1,x-sousuu[a-2]-2) + pat[a-2] + 1;
}
int main(){
  ll n,x;
  cin >> n >> x;
  sousuu.assign(n,0),pat.assign(n,0);
  sousuu[0] = 5;
  pat[0] = 3;
  a1[0] = 0;
  a1[1] = 1;
  a1[2] = 2;
  a1[3] = 3;
  a1[4] = 3;
  REP(i,n-1) {
    sousuu[i+1] = 2 * sousuu[i] + 3;
    pat[i+1] = 2 * pat[i] + 1;
  }
  cout << solve(n,x-1) << endl;
}


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

int main() {
  int a,b,c,x;
  cin>>a>>b>>c>>x;
  int ans=0,i,j,k,sum;
  
  for(i=0;i<=a;i++){
    for(j=0;j<=b;j++){
      for(k=0;k<=c;k++){
        sum=500*i+100*j+50*k;
        if(sum==x)
          ans++;
      }
    }
  }
  cout<<ans;
}


Measured memory_bytes: 256
let array_of_string s = Array.init (String.length s) (fun i -> s.[i])

let () =
  read_line ()
  |> array_of_string
  |> Array.to_list
  |> List.map (fun c -> Char.code c - Char.code '0')
  |> (fun (d :: n) -> List.fold_left (fun candidates d ->
      List.concat @@ List.map (fun (sum, x) -> [(sum + x, d); (sum, 10 * x + d)]) candidates) [(0, d)] n)
  |> List.map (fun (sum, x) -> sum + x)
  |> List.fold_left ( + ) 0
  |> Printf.printf "%d\n"

Measured memory_bytes: 512
import java.io.BufferedReader
import java.util.*

fun main() = abc138e()

@OptIn(ExperimentalStdlibApi::class)
fun abc138e() = SimpleFastIOWithToken_abc138e().exec {
    val s = readString()
    val t = readString()
    fun Char.toIndex() = this - 'a'

    val nextAppearDist = Array(26) { IntArray(s.length) }
    val repeatedS = s.repeat(2)

    for (tChar in t.toSet()) {
        var distToNext = 1
        for (i in repeatedS.indices.reversed()) {
            if (i in s.indices) nextAppearDist[tChar.toIndex()][i] = distToNext
            if (repeatedS[i] == tChar) distToNext = 1 else distToNext++
        }
        if (distToNext - 1 == repeatedS.length) return@exec println(-1)
    }

    var answer = 0L

    for ((index, tChar) in t.withIndex()) {
        val nextDist = nextAppearDist[tChar.toIndex()][(answer % s.length).toInt()]
        val moveDist = if (index == 0) s.indexOf(tChar) else nextDist
        answer += moveDist
    }

    println(++answer)
}

// region kokokara template dayo (^o^)
@Suppress("unused", "HasPlatformType", "MemberVisibilityCanBePrivate", "ClassName")
private class SimpleFastIOWithToken_abc138e(private val separator: String = System.lineSeparator()) {
    private val br: BufferedReader = System.`in`.bufferedReader()
    private var st: StringTokenizer = StringTokenizer("")
    private val sb: StringBuilder = StringBuilder()
    private fun prepareNext() = run { if (!st.hasMoreTokens()) st = StringTokenizer(br.readLine()) }

    fun readString(): String = run { prepareNext() }.run { st.nextToken() }
    fun readStringList(size: Int): List<String> = List(size) { readString() }
    fun readInt(): Int = Integer.valueOf(readString())
    fun readIntList(size: Int): List<Int> = List(size) { readInt() }
    fun readLong(): Long = java.lang.Long.valueOf(readString())
    fun readLongList(size: Int): List<Long> = List(size) { readLong() }
    inline fun <reified T> readArray(size: Int, init: () -> T) = Array(size) { init() }
    inline fun <T> readList(size: Int, init: () -> T) = List(size) { init() }

    fun println(): Unit = run { sb.append(separator) }
    fun print(any: Any): Unit = run { sb.append(any) }
    fun println(any: Any): Unit = print(any.toString() + separator)

    fun exec(action: SimpleFastIOWithToken_abc138e.() -> Unit) = run {
        Thread(null, { action() }, "(^o^)", 128 * 1024 * 1024).apply { start() }.join()
    }.run { kotlin.io.print(sb) }

    fun readLine(): Nothing = error("readLine is disabled.")
}
// endregion kokomade template dayo (^o^)


Measured memory_bytes: 51720
#define _CRT_SECURE_NO_WARNINGS
//#pragma GCC optimize("O3")
#include <vector>
#include <algorithm>
#include <vector>
//#include <random>
#include <fstream>
#include <map>
#include <string>
#include <deque>
#include <set>
#include <ctime>
#include <exception>
#include <iostream>
#include <queue>
using namespace std;

//const int size2 = 6e5;
const int size1 = 1e6 + 500;
const long long INF = 1e9 + 10;
const long long mod = 1e9 + 9;
const long double eps = 1e-7;
const long double pi = 3.1415926535;
const int erat = 1e6 + 500;


int main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	vector < string > s(n);
	for (int i = 0; i < n; i++)
	{
		cin >> s[i];
	}
	int ans = 0;
	for (int razn = -(n - 1); razn <= (n - 1); razn++)
	{
		int a = n;
		int b = a + razn;
		bool res = true;
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (s[i][(((j + b) % n) + n) % n] != s[j][(((i + b) % n) + n) % n])
				{
					res = false;
				}
			}
		}
		if (res)
		{
			ans += n - abs(razn);
		}
	}
	cout << ans;
	return 0;
}

Measured memory_bytes: 384
#include <stdio.h>
#include <stdlib.h>

typedef struct dice{
  int pip[6];
  int top, front, right;
}Dice;

Dice *createDice(int *a);          /* ?????????????????? */
void rollDice(Dice *D, char c);    /* ?????????????????¢?????? */


int main(void){
  char compass[101];
  Dice *D;
  int i, a[6];

  for(i = 0; i< 6; i++){
    scanf("%d", &a[i]);
  }
  D = createDice(a);
  scanf(" %s", compass);
  for(i = 0; compass[i] != '\0'; i++){
    rollDice(D, compass[i]);
  }
  printf("%d\n", D->pip[D->top]);
  free(D);
  return 0;
}


Dice *createDice(int *a){
  Dice *p;
  int i;

  p = (Dice *)malloc(sizeof(Dice));
  if(p == NULL){
    printf("?????¢????¢??????¨??????\n");
    return NULL;
  }
  for(i = 0; i < 6; i++){
    p->pip[i] = a[i];
  }
  p->top = 0;
  p->front = 1;
  p->right = 2;
  return p;
}


void rollDice(Dice *D, char c){
  int tmp;

  switch(c){
  case 'N':
    tmp = D->top;
    D->top = D->front;
    D->front = 5 - tmp;
    break;
  case 'E':
    tmp = D->right;
    D->right = D->top;
    D->top = 5 - tmp;
    break;
  case 'S':
    tmp = D->front;
    D->front = D->top;
    D->top = 5 - tmp;
    break;
  case 'W':
    tmp = D->top;
    D->top = D->right;
    D->right = 5 - tmp;
    break;
  }
}

Measured memory_bytes: 596
# ABC070d
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

n = int(input())
cost = [10**10]*n
edge = [dict() for _ in range(n)]
for _ in range(n-1):
    a, b, c = map(int, input().split())
    edge[a-1].update({b-1: c})
    edge[b-1].update({a-1: c})

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


def dfs(i, prev, d):
    cost[i] = d
    for j in edge[i].keys():
        if j == prev:
            continue
        dfs(j, i, d+edge[i][j])


dfs(K-1, -1, 0)

for _ in range(Q):
    x, y = map(int, input().split())
    print(cost[x-1]+cost[y-1])

Measured memory_bytes: 140276
import math
import queue
import bisect
import heapq
import time
import itertools

mod = int(1e9+7)

def swap(a,b):
    return (b,a)

def gcd(a,b): #最大公約数
    if (a<b):
        a,b = swap(a,b)
    if (b==0):
        return a
    else:
        return gcd(b,a%b)

def lcm(a,b): #最小公倍数
    return a/gcd(a,b)*b

def divisors(a): # 約数列挙
    divisors = []
    for i in range(1,int(a**0.5)+1):
        if a%i==0:
            divisors.append(i)
            if i!=a//i:
                divisors.append(a//i)
    return divisors

def is_prime(a): #素数判定
    if a<2:
        return False
    elif a==2:
        return True
    elif a%2==0:
        return False
    sqrt_num = int(a**0.5)
    for i in range(3,sqrt_num+1,2):
        if a%i==0:
            return False
    return True

def prime_num(a): #素数列挙
    pn = [2]
    for i in range(3,int(a**0.5),2):
        prime = True
        for j in pn:
            if i%j==0:
                prime = False
                break
        if prime:
            pn.append(i)
    return pn

def prime_fact(a): #素因数分解
    sqrt = math.sqrt(a)
    res = []
    i = 2
    if is_prime(a):
        res.append(a)
    else:
        while a!=1:
            while a%i==0:
                res.append(i)
                a //= i
            i += 1
    return res

def main():
    s1,s2,s3 = input().split()
    ans = [s1[0],s2[0],s3[0]]
    print(''.join(ans).upper())
    return

if __name__=='__main__':
    main()


Measured memory_bytes: 38764
#include <iostream>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

void solve(std::string S){
    int ans = 0;
    REP(i, S.size()) {
        if (S[i] == '+') ans++;
        else ans--;
    }
    cout << ans << '\n';
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    std::string S;
    std::cin >> S;
    solve(S);
    return 0;
}


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

int f(int n){
	int sum = 0;
	while(n>0){
		sum += n%10;
		n /= 10;
	}
	return sum;
}

int main(){
  int n,a,b;
  cin >> n >> a >> b;
  int total = 0;
  for(int i = 1; i <= n; ++i){
	  int sum = f(i);
	  if(sum >= a && sum <= b) total += i;
  }
  cout << total << endl;
  return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define ll long long
using namespace std;
ll a,b;
int main()
{
	scanf("%lld%lld",&a,&b);
	if (a+b==15) puts("+");
		else if (a*b==15) puts("*");
			else puts("x");
	return 0;
}


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

int main() {
    string s;
    cin>>s;
    bool app[26];
    for (int i=0;i<26;i++){
      app[i]=false;
    }
    for(int i=0;i<s.size();i++){
      app[s[i]-'a']=true;
    }
    for(int i=0;i<26;i++){
      if(!app[i]){
        char h=i+'a';
        cout<<h<<endl;
        return 0;
      }
    }
    cout<<"None"<<endl;
    return 0;
}

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

int main(void)
{
	char str[21];
	int i, len;
	
	scanf("%s", str);
	
	len = strlen(str);
	
	for(i = len - 1;i >= 0;i--){
		printf("%c", str[i]);
	}
	printf("\n");
	
	return 0;
}
	

Measured memory_bytes: 524
#include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<string>
#include<queue>
#include<utility>
#include<cmath>
#include<sstream>
#include<istream>

using namespace std;


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

  int p;
  string S;

  int getP[N] = {0};
  int W[N] = {0};

  int Ccnt = 0;
  int Wcnt = 0;
  for(int i=0; i<M; i++){
    cin >> p >> S;
    if(getP[p-1]==0){
      if(S == "AC"){
        Ccnt++;
        getP[p-1]=1;
        Wcnt += W[p-1];
      }
      else if(S == "WA"){
        W[p-1] += 1;
      }
    }
  }

  cout << Ccnt << " " << Wcnt << endl;

  return 0;
}


Measured memory_bytes: 1024
#include<stdio.h>
#include<iostream>
#include<iomanip>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<string>
#include<math.h>
using namespace std;

const int MAX = 3000;

int main(){
    int N;
    string S;

    cin >> N >> S;

    if(N % 2){
        cout << "No" << endl;
        return 0;
    }
    
    for(int i = 0;i < N / 2;i++){
        if(S[i] != S[i + N / 2]){
            cout << "No" << endl;
            return 0;
        }
    }

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

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

int main() {
    long N, M;
    cin >> N >> M;
    vector<int> h(M);
    rep(i, M) cin >> h.at(i);
    
    long cnt = 0;
    rep(i,M) cnt += h.at(i);
    
    if (cnt <= N) cout << N - cnt << endl;
    else cout << -1 << endl;
}


Measured memory_bytes: 3668
s = int(input())
L = [s]
cnt = 1
f = s
while(True):
    cnt += 1
    if(f % 2 == 0):
        f = f / 2
    else:
        f = 3 * f + 1
    if(f in L):
        print(cnt)
        break
    else:
        L.append(f)

Measured memory_bytes: 38384
#include<iostream>

using namespace std;

int main() {
    int K, X;
    cin >> K >> X;

    int min = X - K + 1;
    int max = X + K - 1;
    if (min > max) {
        int stash = min;
        max = min;
        min = stash;
    }

    for (int i = min; i <= max; i++) {
        cout << i << " ";
    }
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
using ll  = long long;
using vl  = vector<ll>;
using vi  = vector<int>;
#define _GLIBCXX_DEBUG
#define IO_STREAM cin.tie(0);ios::sync_with_stdio(false)
#define all(x) x.begin(),x.end()
#define rep(i,sta,end) for(int i=sta;i<end;++i)
#define lcm(a,b) (a)/__gcd((a),(b))*(b)
#define pb push_back
const ll INF = 1000000000000000;
const ll MOD = 1000000007;
const double PI = acos(-1);
//||
#define DBG(a,b,c,d) //cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<endl
//DBG("","","","");

signed main(){
	IO_STREAM;
	int N,K;
	cin>>N>>K;
	vl H(N);
	rep(i,0,N){
		cin>>H[i];
	}
	sort(all(H));

	ll ans=0;
	if(N<=K){
		ans=0;
	}
	else{
		rep(i,0,N-K){
			ans+=H[i];
		}
	}
	cout<<ans<<endl;
	return 0;
}


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

int main()
{
    char s[110];
    int c[26],i,t;

    memset(c, 0, 26*sizeof(c[0]));

    gets(s);
    i=0;
    while(s[i]!='\0')
    {
        t=s[i] - 'a';
        c[t]++;
        i++;
    }

    for(i=0;i<26;i++)
    {
        if(c[i]%2!=0)
        {
            printf("No\n");
            return 0;
        }
    }
    printf("Yes\n");
    return 0;


}


Measured memory_bytes: 128
def main():
    N, K = map(int, input().split())
    S = input()
    print(replace(S, K))


def replace(S, K):
    if S[K-1] == 'A':
        return S[:K-1] + 'a' + S[K:]
    elif S[K-1] == 'B':
        return S[:K-1] + 'b' + S[K:]
    else:
        return S[:K-1] + 'c' + S[K:]


if __name__ == '__main__':
    main()


Measured memory_bytes: 3060
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <limits>
typedef long long ll;
#define REP(i,n)   for(int i=0; i<(int)(n); ++i)
#define FOR(i,b,e) for(int i=(b); i<=(int)(e); ++i)

#define in cin
#define out cout
#define pb push_back
using namespace std;
int main(){
	int n, m,a[200],b[200];

	in >> n >> m;
	REP(i, 200) { a[i] = i + 1; }
	REP(i, m) {
		int aa;
		in >> aa;
		if (aa==0) {
			int ai = 0,bi=0;
			REP(j, n*2) {
				if(j%2)b[j] = a[n+bi++];
				else b[j] = a[ai++];
			}
		}
		else {
			int bi = 0;
			for(int j=aa;j<2*n;++j) {
				b[bi++] = a[j];
			}
			for (int j = 0; j < aa; ++j) {
				b[bi++] = a[j];
			}
		}
		copy(b, b + 200, a);
	}
	REP(i, n*2)out << a[i] << endl;
	in >> n
		;
	return 0;
}

Measured memory_bytes: 3048
def frog(h):
    N = len(h)
    if N == 1:
        return 0

    c = [0 for _ in range(N)]
    c[1] = abs(h[1] - h[0])
    for i in range(2, N):
        if2 = c[i - 2] + abs(h[i] - h[i - 2])
        if1 = c[i - 1] + abs(h[i] - h[i - 1])
        c[i] = min(if1, if2)
    return c[-1]

N = int(raw_input())
h = map(int, raw_input().split())
print(frog(h))


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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  if (fopen("input.in", "r")) {
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
  }

  int H, W;
  cin >> H >> W;
  vector<vector<char>> grid(H, vector<char>(W));
  for (int i = 0; i < H; i++) {
    string s; cin >> s;
    for (int j = 0; j < W; j++) {
      grid[i][j] = s[j];
    }
  }

  if (grid[0][0] == '#' || grid[H-1][W-1] == '#') {
    cout << 0 << endl;
    return 0;
  }

  vector<vector<int>> dp(H, vector<int>(W, 0));
  dp[0][0] = 1;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (i==0&&j==0) continue;
      if (grid[i][j] == '#') continue;
      if (i-1 >= 0) dp[i][j] += dp[i-1][j];
      dp[i][j] %= (int)1e9+7;
      if (j-1 >= 0) dp[i][j] += dp[i][j-1];
      dp[i][j] %= (int)1e9+7;
    }
  }

  cout << dp[H-1][W-1] << endl;

  return 0;
}

Measured memory_bytes: 5248
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <math.h>
#include <utility>
#include <vector>
#include <map>
#include <unordered_map>
#include <cstdlib>
#define div1 1000000007
const long long INF = 1LL << 60;
using namespace std;
// a>b -> a==b;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
// a<b -> a==b;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

int main(void)
{
    long long N, t[100001], x[100001], y[100001], i, judge=1, d, td;

    cin >> N;

    t[0] = 0;
    x[0] = 0;
    y[0] = 0;

    for(i=1; i<=N; i++){
        cin >> t[i] >> x[i] >> y[i];
    }

    for(i=0; i<=N-1; i++){
        d = abs(x[i+1]-x[i]) + abs(y[i+1]-y[i]);
        td = t[i+1]-t[i];

        if(d > td || d%2 != td%2){
            judge = 0;
        }
    }

    if(judge==1) cout << "Yes" << endl;
    else cout << "No" << endl;

    return 0;
}

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

using namespace std;

typedef long long int ll;
typedef pair<int, int> P;
#define INF 10000000000000
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep1(i, n) for(int i = 1; i < (int)(n); i++)
#define repN_1(i, n) for(int i = 0; i < (int)(n - 1); i++)
#define repM(i, n) for(int i = (int)(n - 1); i >= 0; i--)
template<class T>bool chmax(T & a, const T & b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

int main()
{
	ll n, k;
	ll ans, min, max;
	ans = 0;

	cin >> n >> k;

	for (ll i = k; i <= n + 1; i++)
	{
		if (i % 2 == 0)
		{
			min = (i - 1) * (i / 2);
			max = (n + n - i + 1) * (i / 2);
		}
		else
		{
			min = (i - 1) * ((i - 1) / 2) + ((i - 1) / 2);
			max = (n + n - i + 1) * ((i - 1) / 2) + ((n + n - i + 1) / 2);
		}
		ans = (ans + (max - min + 1)) % 1000000007;
	}

	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 3636
import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
        int n = sc.nextInt();
        long [] a = new long[n]; long [] b = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextLong(); b[i] = sc.nextLong();
        }
        boolean ok = true; long sum = 0;
        for (int i = 0; i <n; i++) {
            if (a[i] != b[i]) ok = false;
            sum += a[i];
        }
        if (ok) {
            out.println(0);
        } else {
            long res = 0;
            for (int i = 0; i < n; i++) {
                if (a[i] > b[i]) {
                    res = Math.max(res, sum - b[i]);
                }
            }
            out.println(res);
        }
        out.close();
    }


    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

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

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }


    }

}

Measured memory_bytes: 41892
#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
#include <queue>
using namespace std;

int main(int argc, char* argv[]){
	long long N, K;

	scanf("%lld %lld", &N, &K);
	long long possible_all=0;

	long long min=(K-1)*K/2;
	//m個選ぶときに作れる最小の和:sum(0~m-1):初期値m=K
	long long max=(N*(N+1)-(N-K)*(N-K+1))/2;
	//m個選ぶときに作れる最大の和:sum(N-m+1~N):初期値m=K

//	printf("%lld %lld\n", min, max);
	for(long long m=K; m<=N+1; m++){

		long long possible_m=max -min + 1; //m個選ぶときに作れる和の個数
		possible_all+=possible_m % 1000000007;
		possible_all = possible_all % 1000000007;

		min+=m;
		max+=N-m;
	}

	printf("%lld\n", possible_all);

	return 0;
}





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



signed main(){
    int n,m,a,b;
    cin >>n>>m;
    vector<int> hen[n];
    for(int i=0;i<m;i++){
        cin >>a>>b;
        hen[a-1].push_back(b-1);
        hen[b-1].push_back(a-1);

    }
    pair<int,int> dp[11][n],ans[n];
    for(int i=0;i<11;i++)for(int j=0;j<n;j++)dp[i][j]=make_pair(0,0);
    for(int i=0;i<n;i++)ans[i]=make_pair(0,0);
    int q,v,d,c;
    cin >>q;
    for(int i=0;i<q;i++){
        cin >>v>>d>>c;
        dp[d][v-1]=make_pair(i+1,c);
    }
    for(int i=10;i>0;i--){
        for(int j=0;j<n;j++){
            if(ans[j].first<dp[i][j].first)ans[j]=dp[i][j];
            for(int k=0;k<hen[j].size();k++){
                if(dp[i-1][hen[j][k]].first<dp[i][j].first)dp[i-1][hen[j][k]]=dp[i][j];
            }
        }
    }
    for(int j=0;j<n;j++){
            if(ans[j].first<dp[0][j].first)cout <<dp[0][j].second<<endl;
            else cout << ans[j].second<<endl;
    }

       

    return 0;
}

Measured memory_bytes: 25588
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
    static void Main(string[] args)
    {
            string sS = System.Console.ReadLine().Trim();
            int iR = int.Parse(sS);

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();

            // 解析
            int iOut = iR * iR;

            // 出力
            System.Console.WriteLine("{0}", iOut.ToString().Trim());

            sw.Stop();
#if DEBUG
            Console.WriteLine();
            Console.WriteLine("{0}", sw.Elapsed.ToString());
            Console.WriteLine("続行するには何かキーを押してください．．．");
            Console.ReadKey();
#endif
    }

}


Measured memory_bytes: 13268
#include<bits/stdc++.h>
using namespace std;
int main()
{  
    int a,b,c,d;cin>>a>>b>>c>>d;
    while(1)
    {
    	c-=b;
    	if(c<=0)
    	break;
    	a-=d;
    	if(a<=0)break;
	}
	if(a>0)cout<<"Yes"<<endl;
	else
	cout<<"No"<<endl; 
	return 0;
 } 

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

#define min(A, B) ((B) > (A) ? (A) : (B))

/*
int min(int a, int b)
{
    return (b > a ? a : b);
}
*/

int main(void)
{
    int i, j, k;
    int n;
    int div[10001];
    int dp[2][5001][2];
    
    scanf("%d", &n);
    
    for (i = 1; i <= n - 1; i++){
        scanf("%d", div + i);
    }
    memset(dp, 1, sizeof(dp));
    dp[0][0][0] = dp[0][0][1] = 0;
    
    for (i = 0; i <= n / 2; i++){
        for (j = 0; j <= n / 2; j++){
            if (i){
                dp[i & 1][j][0] = dp[(i - 1) & 1][j][0];
                dp[i & 1][j][1] = dp[(i - 1) & 1][j][0] + div[i + j];
            }
            if (j){
                dp[i & 1][j][0] = min(dp[i & 1][j][0], dp[i & 1][j - 1][1] + div[i + j]);
                dp[i & 1][j][1] = min(dp[i & 1][j][1], dp[i & 1][j - 1][1]);
            }
        }
    }
     
    printf("%d\n", min(dp[0][n / 2][0], dp[0][n / 2][1]));
     
    return (0);
}

Measured memory_bytes: 484
import collections
def f(n,ais):
	cc,extra = collections.Counter(),0
	for ai in ais:
		if ai/400 < 8: cc[ai/400] +=1
		else: extra +=1
	print len(cc) + (1 if (len(cc) == 0 and extra) else 0), min(10000, len(cc) + extra)
	#print min(cc.values() or [0]), max(cc.values() or [0]) + extra
f(int(raw_input()), map(int, raw_input().split()))

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

#define ld long double
#define ll long long int
#define ull unsigned long long int
#define vi vector<int>
#define vl vector<ll>
#define vvi vector< vector<int> >
#define vvl vector< vector<ll> >
#define repd(i, a, b) for (int i=(a);i<(b);i++)
#define rep(i, n) repd(i,0,n)
#define ALL(v) v.begin(), v.end()
#define INF 1e9

using namespace std;

/**
 * calculate GCD(greatest common divisor)
 * @param a
 * @param b
 * @return
 */
unsigned euclidean_gcd(unsigned a, unsigned b) {
    if (a < b) return euclidean_gcd(b, a);
    unsigned r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

/**
 * change minimum
 * @tparam T
 * @param a
 * @param b
 * @return bool
 */
template<class T>
inline bool changeMinimum(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

/**
 * change maximum
 * @tparam T
 * @param a
 * @param b
 * @return bool
 */
template<class T>
inline bool changeMaximum(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

/**
 * output answer
 * @tparam T
 * @param answer
 * @return
 */
template<typename T>
T output(T answer) {
    cout << answer << endl;
    return 0;
}

/** ----from here ---------------------------------------------------------- */

int main() {
    int N;
    string S;
    cin >> N >> S;
    for (int i = 0; i < S.size(); ++i) {
        int number = S[i] - 'A';
        int added = (number + N) % 26;
        cout << (char) ('A' + added);
    }
    cout << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define mxn 1000010
#define db double
#define LL long long
#define ldb long double
#define ull unsigned long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define pul pair<ull,LL> 
#define pii pair<int,int> 
#define mp make_pair
#define fr first
#define sc second
#define x1 faqx1
#define y1 faqy1
#define x2 faqx2
#define y2 faqy2
using namespace std;
const int mod=998244353;
template<typename T> void chkmin(T &x,T y) {if(y<x) x=y;}
template<typename T> void chkmax(T &x,T y) {if(y>x) x=y;}
template<typename T> void upd(T &x,T y) {if((x+=y)>=mod) x-=mod;}
string S,S1,S2;
int n,m,sl,fh;
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
int main()
{
	cin>>S;n=S.size();m=(n-1)/2;
	for(int i=0;i<n;++i)
		if(S[i]!=S[n-i-1])
			return puts("No"),0;
	S1=S.substr(0,(n-1)/2);
	for(int i=0;i<m;++i)
		if(S1[i]!=S1[m-i-1])
			return puts("No"),0;
	S2=S.substr((n+1)/2);
	for(int i=0;i<m;++i)
		if(S2[i]!=S2[m-i-1])
			return puts("No"),0;
	puts("Yes");
	return 0;
}

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

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

int main() {
    long long N; cin >> N;
    long long res = 0;

    auto div = divisor(N);
    for(long long d : div) {
        if(d == 1) continue;
        long long n = N;
        while(n % d == 0) n = n / d;
        if(n % d == 1) ++res;
    }

    div = divisor(N - 1);
    res += div.size() - 1;

    cout << res << endl;
}

Measured memory_bytes: 384
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include<queue>
#include<set>
#include<vector>
#include<cstdio>

using namespace std;
/*
int find(int x, vector<int> &p) {
if (p[x] == x)return x;
return p[x] = find(p[x], p);
}

void uni(int x, int y, vector<int> &p) {
x = find(x, p);
y = find(y, p);
p[x] = y;
}

int same(int x, int y, vector<int> p) {
if (find(x, p) == find(y, p))return 1;
return 0;
}
*/

//long long a[1002];


int main()
{
	long long N, cnt = 0, tmp, ans = 0;

	cin >> N;

	vector<long long> a(N, 0);
	for (int i = 0; i < N; i++) {
		cin >> a[i];
	}

	sort(a.begin(), a.end());

	long long sum = 0;
	vector<long long> b(N);
	for (int i = 0; i < N / 2; i++) {
		b[N / 2 - 1 - 2 * ((i + 1) / 2 * (int)pow(-1, i))] = a[i];
		b[N / 2 + 2 * ((i + 1) / 2 * (int)pow(-1, i))] = a[N - 1 - i];
		//cout << N / 2 - 1 - 2 * ((i + 1) / 2 * (int)pow(-1, i)) << ", " << N / 2 + 2 * ((i + 1) / 2 * (int)pow(-1, i)) << endl;
		//cout << i << ", " << N -1 -i << endl;
	}


	if (N % 2 == 0) {
		for (int i = 0; i < N - 1; i++) {
			sum += abs(b[i] - b[i + 1]);
		}
		cout << sum;
	}
	else {
		for (int i = 0; i < N - 2; i++) {
			sum += abs(b[i] - b[i + 1]);
		}
		sum += max(abs(a[N/2] - b[0]), abs(a[N / 2] - b[N-2]));
		cout << sum;
	}

	char qq;
	cin >> qq;
	return 0;
}

Measured memory_bytes: 2048
#include <bits/stdc++.h>
using namespace std;
string s,t;
long long ans;
int main(){
	cin>>s;
	long long cnt=count(s.begin(),s.end(),'1');
	for(long long i=0;i<=cnt;++i){
		ans+=(i+1)/2;
	}
	for(long long i=0;i<s.size();++i){
		t+=s[i];
		long long sz=t.size();
		if(sz>=2&&t[sz-1]=='1'&&t[sz-2]=='1'){
			t.pop_back();
			t.pop_back();
			ans+=s.size()-cnt;
		}
	}
	s=t;
	cnt=count(s.begin(),s.end(),'1');
	long long c=0;
	for(long long i=0;i<s.size();++i){
		if(s[i]=='1'){
			ans+=s.size()-cnt-((i+1)/2-(c+1)/2);
			c++;
		}
	}
	cout<<ans<<'\n';
	return 0;
}

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

int main(){
    double n,k,p=0;
    cin >> n >> k;

    for(int i = 1;i <= n;i ++){
        double cnt=0,point=i;

        while(point<=k-1){
            point *= 2.0;
            cnt ++;
        }

        p += pow(0.50,cnt)/n;
    }

    cout << setprecision(15) << p << endl;
    return 0;
}

Measured memory_bytes: 384
def main():
    N,A,B,C = map(int, input().split())
    l = [int(input()) for _ in range(N)]
    
    INF = 1<<20
    
    def perm(n, a, b, c):
        if n == N:
            if all([a, b, c]):
                return abs(A-a)+abs(B-b)+abs(C-c)-30
            else:
                return INF
        return min(
            perm(n+1,a,b,c),
            perm(n+1,a+l[n],b,c)+10,
            perm(n+1,a,b+l[n],c)+10,
            perm(n+1,a,b,c+l[n])+10
            )
    
    print(perm(0,0,0,0))

main()

Measured memory_bytes: 3064
using System;
using System.Collections.Generic;
using System.Linq;
using static Input;

public class Prog
{
    private const int INF = 1000000007;
    private const long INFINITY = 9223372036854775807;
    private struct Pair { public int x, y; }
    public static void Main()
    {
        //input
        int N = NextInt();
        List<int> a = new List<int>();
        for (int i = 0; i < N; i++) a.Add(NextInt());
        a.Sort();
        int ans_n = a[N - 1];
        a.RemoveAt(N - 1);
        int r = ans_n / 2;
        int sf1 = LessSearch(a, r);
        int sf2 = MoreSearch(a, r);
        int ans_r = 0;
        if (sf1 == -1) ans_r = a[sf2];
        else if (sf2 == N - 1) ans_r = a[sf1];
        else ans_r = (r - a[sf1] < a[sf2] - r ? a[sf1] : a[sf2]);
        Console.WriteLine(ans_n + " " + ans_r);
    }
    private static int MoreSearch(List<int> list, int n)
    {
        int a = 0, b = list.Count();
        if (list[a] >= n) return a;
        else if (list[b - 1] < n) return b;
        while (b - a != 1)
        {
            int x = (a + b) / 2;
            if (list[x] >= n) b = x;
            else a = x;
        }
        return b;
    }
    private static int LessSearch(List<int> list, int n)
    {
        int a = 0, b = list.Count();
        if (list[b - 1] <= n) return b - 1;
        else if (list[a] > n) return -1;
        while (b - a != 1)
        {
            int x = (a + b) / 2;
            if (list[x] <= n) a = x;
            else b = x;
        }
        return a;
    }
}

public class Input
{
    private static Queue<string> q = new Queue<string>();
    private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); }
    public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); }
    public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); }
    public static char NextChar() { Confirm(); return char.Parse(q.Dequeue()); }
    public static string NextString() { Confirm(); return q.Dequeue(); }
    public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); }
    public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
    public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
    public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); }
    public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); }
}


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

int main() {
  int A, B, C, D, L, R;

  cin >> A >> B >> C >> D;
  L = A + B;
  R = C + D;

  if (L == R) {
  	cout << "Balanced" << endl;
  }
  else if (L > R) {
  	cout << "Left" << endl;
  }
  else if (L < R) {
  	cout << "Right" << endl;
  }
}

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

const int N = 55;

const int mod = 1e9+7;

int n;
int A;
int a[N];
long long mem[N][N][N*N];

long long go(int idx , int lft , int sum){
    if(idx == n+1){
        return sum == 0 && lft == 0;
    }
    long long &ret = mem[idx][lft][sum];
    if(~ret)
        return ret;
    ret = 0;
    if(a[idx] <= sum && lft)
        ret += go(idx + 1 , lft - 1 , sum - a[idx]);
    ret += go(idx + 1 , lft , sum);
    return ret;
}

int main(){
    scanf("%d%d" , &n , &A);
    for(int i = 1; i <= n; i++){
        scanf("%d" , &a[i]);
    }
    memset(mem , -1 , sizeof mem);
    long long ans = 0;
    for(int len = 1; len <= n; len++){
        ans += go(1 , len , A*len);
    }
    printf("%lld\n" , ans);
}


Measured memory_bytes: 75296
def v():
 for e in iter(input,'0 0'):
  r=int(e.split()[0])
  d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
  a=0
  for m in range(1<<~-r):
   t=0
   for s in d:
    c=bin(m^s).count('1')
    t+=c if c>r//2 else r-c
   if a<t:a=t
  print(a)
if'__main__'==__name__:v()


Measured memory_bytes: 6916
S=input()
A=0 #Aの数
B=0 #ABの数
C=0 #ABCの数
n=1 #通り数
mod=10**9+7
for s in S:
    if s=="?":
        C=3*C+B
        B=3*B+A
        A=3*A+n
        n*=3
    elif s=="A":
        A+=n
    elif s=="B":
        B+=A
    else:
        C+=B
    A%=mod
    B%=mod
    C%=mod
    n%=mod
print(C)

Measured memory_bytes: 3188
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int main(void) {
  int N;
  cin >> N;
  vector<string> S(2);
  cin >> S[0] >> S[1];

  const int64_t mod = 1e9 + 7;
  int64_t answer = 0;
  int start;
  bool prev_same;
  if (S[0][0] == S[1][0]) {
    answer = 3;
    start = 1;
    prev_same = true;
  } else {
    answer = 3 * 2;
    start = 2;
    prev_same = false;
  }
  for (int i = start; i < N; ++i) {
    if (S[0][i] == S[1][i]) {
      if (prev_same) {
        // a b
        // a b
        answer = (answer * 2) % mod;
      } else {
        // aa c
        // bb c
        answer = answer;  // no choise
      }
      prev_same = true;
    } else {
      if (prev_same) {
        // a bb
        // a cc
        answer = (answer * 2) % mod;
      } else {
        // aa cc
        // bb dd
        answer = (answer * 3) % mod;
      }
      ++i;
      prev_same = false;
    }
  }
  cout << answer << endl;
  return 0;
}

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

using namespace std;

#define M 1000000007

int main() {
	string s;
	cin >> s;
	long long int sz = s.size();
	vector<long long int> a(sz, 0);
	vector<long long int> c(sz, 0);
	vector<long long int> qa(sz, 0);
	vector<long long int> qc(sz, 0);

	long long int q = 0;

	for (long long int i = 0; i < sz; i++) {
		if (s[i] == '?') {
			q++;
		}
	}

	long long int q1, q2, q3, qq;
	q1 = q2 = q3 = qq = 1;

	for (long long int i = 0; i < q; i++) {
		qq *= 3;
		qq %= M;
	}

	for (long long int i = 0; i < q-1; i++) {
		q1 *= 3;
		q1 %= M;
	}

	for (long long int i = 0; i < q-2; i++) {
		q2 *= 3;
		q2 %= M;
	}

	for (long long int i = 0; i < q-3; i++) {
		q3 *= 3;
		q3 %= M;
	}

	for (long long int i = 0; i < sz; i++) {
		if (i != 0) {
			a[i] += a[i - 1];
			qa[i] += qa[i - 1];
		}
		if (s[i] == 'A') {
			a[i]++;
		}
		if (s[i] == '?') {
			qa[i]++;
		}
	}
	for (long long int i = sz - 1; i >= 0; i--) {
		if (i != sz - 1) {
			c[i] += c[i + 1];
			qc[i] += qc[i + 1];
		}
		if (s[i] == 'C') {
			c[i]++;
		}
		if (s[i] == '?') {
			qc[i]++;
		}
	}
	long long int ans = 0;
	for (long long int i = 1; i < sz - 1; i++) {
		if (s[i] == 'B') {
			ans += a[i] * c[i] * qq;
			ans %= M;
			ans += qa[i] * c[i] * q1;
			ans %= M;
			ans += a[i] * qc[i] * q1;
			ans %= M;
			ans += qa[i] * qc[i] * q2;
			ans %= M;
		}
		if (s[i] == '?') {
			ans += a[i] * c[i] * q1;
			ans %= M;
			ans += qa[i-1] * c[i] * q2;
			ans %= M;
			ans += a[i] * qc[i+1] * q2;
			ans %= M;
			ans += qa[i-1] * qc[i+1] * q3;
			ans %= M;
		}
		//cout << ans << endl;
	}

	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 3584
#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <sstream>
#include <iomanip>
using namespace std;

int main(void){
    long long int h;
    cin >> h;
    long long int ans = 1;
    long long int count = 0;
    while(h > 1){
        h /= 2;
        count++;
        ans += pow(2, count);
       //cout << pow(2, count) << endl;
    }

    cout << ans << endl;
}

Measured memory_bytes: 256
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>
#include <queue>

using namespace std;

#define SIZE 400000
#define SIZE0 200000
#define INF 1000000000

typedef long long ll;

struct Dinic{

  struct edge{
    int to, cap, rev;
    edge(int a, int b, int c): to(a), cap(b), rev(c){}
  };

  vector<vector<edge>>G;
  vector<int> level, itr;
  int MAX_V;

  Dinic(int max_v): G(max_v), MAX_V(max_v) {}

  int add_edge(int from, int to, int cap) {
    int id = G[from].size();

    G[from].push_back(edge(to, cap, G[to].size()));
    G[to].push_back(edge(from, 0, G[from].size() - 1));

    return id;
  }

  void bfs(int s) {
    queue<int> q;
    level.assign(MAX_V, -1);
    level[s] = 0;
    q.push(s);
    while(q.size()) {
      int v = q.front(); q.pop();
      for(edge e: G[v])
        {
          if(e.cap > 0 && level[e.to] < 0) {
            level[e.to] = level[v] + 1;
            q.push(e.to);
          }
        }
    }
  }

  int dfs(int v, int t, int f) {
    if(v == t) return f;
    for(int &i = itr[v]; i < G[v].size(); i++) {
      edge &e = G[v][i];
      if(e.cap > 0 && level[v] < level[e.to]) {
        int d = dfs(e.to, t, min(f, e.cap));
        if(d > 0) {
          e.cap -= d;
          G[e.to][e.rev].cap += d;
          return d;
        }
      }
    }

    return 0;
  }

  int solve(int s, int t) {
    int flow = 0, f;

    while(1) {
      bfs(s);
      if(level[t] < 0) return flow;
      itr.assign(MAX_V, 0);
      while((f = dfs(s, t, INF)) > 0) flow += f;
    }
  }

};


struct UF{
  vector<int> data;
  UF(int s): data(s, -1) {}

  int root(int x) {
    if(data[x] < 0) return x;
    return data[x] = root(data[x]);
  }

  bool set(int x, int y) {
    x = root(x); y = root(y);
    if(x == y) return false;
    data[x] += data[y];
    data[y] = x;
    return true;
  }

  bool check(int x, int y) {
    return root(x) == root(y);
  }
};

int h[SIZE], w[SIZE];
int x[SIZE], y[SIZE];

int main() {
  int N, M;

  scanf("%d%d", &N, &M);

  int ans1 = 0, ans2 = 0;

  for(int i=0; i<1000; i++) h[i] = w[i] = -1;

  UF uf(M);

  for(int i=0; i<M; i++) {
    scanf("%d%d", x+i, y+i);
    x[i]--; y[i]--;

    if (h[y[i]] >= 0)
      ans1 += uf.set(h[y[i]], i);

    if (w[x[i]] >= 0)
      ans1 += uf.set(w[x[i]], i);

    h[y[i]] = i;
    w[x[i]] = i;
  }

  int K = 1000;

  Dinic dinic(K * 2 + M + 2);

  for(int i=0; i<M; i++) {
    dinic.add_edge(x[i], K*2 + i, 1);
    dinic.add_edge(K*2 + i, y[i] + K, 1);
  }

  for(int i=0; i<K; i++) {
    dinic.add_edge(K*2 + M, i, 1);
    dinic.add_edge(i + K, K*2 + M + 1, 1);
  }

  //cerr << dinic.solve(K*2+M, K*2+M+1) << endl;

  ans2 = M - dinic.solve(K*2+M, K*2+M+1);

  printf("%d %d\n", ans2, ans1);
}



Measured memory_bytes: 3500
#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
LL n,rnk[100001],cnt;
int m,p[100001],b[100001],a[100001];
int fnd(int a){
	if(p[a]!=a)
		return p[a]=fnd(p[a]);
	return a;
}
int main(){
	vector<LL> ans;
	LL o=0;
	cin>>n>>m;
	cnt=(n-1)*n/2;
	ans.push_back(cnt);
	for(int i=0;i<n;i++){
		rnk[i]=1;
		p[i]=i;
	}
	for(int i=0;i<m;i++)
		scanf("%d%d",a+i,b+i);
	reverse(a,a+m);
	reverse(b,b+m);

	for(int i=0;i<m-1;i++){
		a[i]--;b[i]--;
		int pa=fnd(a[i]);
		int pb=fnd(b[i]);
		if(pa==pb){
			ans.push_back(cnt-o);
			continue;
		}
		o+=rnk[pa]*rnk[pb];
		ans.push_back(cnt-o);
		rnk[pa]+=rnk[pb];
		rnk[pb]=0;
		p[pb]=pa;
	}
	reverse(ans.begin(),ans.end());
	for(auto a:ans)
		cout<<a<<"\n";

}



Measured memory_bytes: 4212
#include <cstdio>
#include <cstring>
#include <cassert>
#include <algorithm>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define FOREACH(i, c) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

int sum[1024];

inline unsigned next_set(unsigned x)
{
  unsigned smallest = x & -x;
  unsigned ripple = x + smallest;
  unsigned new_smallest = ripple & -ripple;
  unsigned ones = ((new_smallest / smallest) >> 1) - 1;
  return ripple | ones;
}

int main()
{
  memset(sum, 0, sizeof(sum));
  REP(i, 1024)
    REP(j, 10)
      if (i & (1 << j))
        sum[i] += j;

  int n, s;
  while (scanf("%d%d", &n, &s), n|s) {
    int count = 0;
    for (int S = (1<<n)-1; S < 1<<10; S = next_set(S))
      if (sum[S] == s)
        ++count;
    printf("%d\n", count);
  }
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
out << "(" << a.first << "," << a.second << ")";
return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " |";
__f(comma + 1, args...);
}
#define rep(i,n) for(int i=0; i<(n); i++)
using ll = long long;
#define int long long
using P = pair<int,int>;
//#########################################
signed main(){
    string s;cin >> s;
    int ans = 0;
    int n = s.size();
    for(int i = 0,j;i < n;i = j){
        int now = i;
        for(;i < n && s[i] == s[0];++i);
        ans++;
        if(i == n)break;
        for(j = i;j < n && s[j] !=s[0];j++);
        ans++;
    }
    cout << ans-1 << endl;

    
    return 0;
}


Measured memory_bytes: 512
#include<bits/stdc++.h>
#define INF     1e9
#define ld      long double
#define lli     long long int
#define PI 	    acos(-1)
#define read    freopen("in.txt","r",stdin)
#define uu		first
#define vv		second
#define write   freopen("out.txt","w",stdout)
#define pii     pair<int,int>
using namespace std;
int n;
int main()
{
    cin>>n;
    cout<<"ABC"<<n<<endl;
    return 0;
}


Measured memory_bytes: 256
#include<stdio.h>

void QSort(long x[ ], int left, int right)
{
    int i, j;
    int pivot;

    i = left;                      /* ソートする配列の一番小さい要素の添字 */
    j = right;                     /* ソートする配列の一番大きい要素の添字 */

    pivot = x[(left + right) / 2]; /* 基準値を配列の中央付近にとる */

    while (1) {                    /* 無限ループ */

        while (x[i] < pivot)       /* pivot より大きい値が */
            i++;                   /* 出るまで i を増加させる */

        while (pivot < x[j])       /* pivot より小さい値が */
            j--;                   /*  出るまで j を減少させる */
        if (i >= j)                /* i >= j なら */
            break;                 /* 無限ループから抜ける */

        Swap(x, i, j);             /* x[i] と x[j]を交換 */
        i++;                       /* 次のデータ */
        j--;
    }

    if (left < i - 1)              /* 基準値の左に 2 以上要素があれば */
        QSort(x, left, i - 1);     /* 左の配列を Q ソートする */
    if (j + 1 <  right)            /* 基準値の右に 2 以上要素があれば */
        QSort(x, j + 1, right);    /* 右の配列を Q ソートする */
}

  /* 配列の要素を交換する */
void Swap(long x[ ], int i, int j)
{
    long temp;

    temp = x[i];
    x[i] = x[j];
    x[j] = temp;
}


int main(){
  int n,i;
  long x[200000],xx[200000];
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%ld",&x[i]);
    xx[i]=x[i];
  }
  QSort(xx, 0, n - 1);
  for(i=0;i<n;i++){
    if(x[i]>xx[n/2-1])
      printf("%ld\n",xx[n/2-1]);
    else
      printf("%ld\n",xx[n/2]);
  }
}

Measured memory_bytes: 5248
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin >> n;
  int k;
  cin >>k;
  int dis =0;
  for(int i = 0;i<n;i++){
    int x;
    cin >> x;
    if(k>=x){
      if(2*x > 2*(k-x)){
        dis += 2*(k-x);
      }else{
        dis+=2*x;
      }
    }else{
      if(2*x > 2*(x-k)){
        dis += 2*(x-k);
      }else{
        dis+=2*x;
      }
    }
  }
  cout << dis << endl;
}
        

Measured memory_bytes: 256
#include <iostream>
using namespace std;
typedef long long int ll;
ll n, c, m = 1e9 + 7, a[500], b[500], sum[500][500], dp[500][500], pow1[500];
void init()
{
	for (int i = 0; i <= 400; i++)
	{
		sum[i][0] = 0;
		for (int j = 1; j <= 400; j++)
		{
			if (i == 0)
				pow1[j] = 1;
			else
			{
				pow1[j] *= j;
				pow1[j] %= m;
			}
			sum[i][j] = sum[i][j - 1] + pow1[j];
		}
	}

	dp[n][0] = 1;
}
int main()
{
	cin >> n >> c;
	for (int i = 0; i < n; i++)
		cin >> a[i];

	for (int i = 0; i < n; i++)
		cin >> b[i];

	init();

	for (int i = n - 1; i >= 0; i--)
	{
		for (int j = 0; j <= c; j++)
		{
			for (int k = 0; k <= j; k++)
			{
				dp[i][j] += ((sum[k][b[i]] - sum[k][a[i]-1]) % m)*dp[i+1][j-k];
				//cout << i << " " << j << " " << dp[i][j] << endl;
				dp[i][j] %= m;
			}
		}
	}
	ll ans = dp[0][c];
	cout << ans << endl;
}

Measured memory_bytes: 3328