akhauriyash/Code-Regression huggingface dataset:
mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.buffer.readline

    class Bit:
        def __init__(self, n):
            self.size = n
            self.tree = [0] * (n + 1)

        def sum(self, i):
            s = 0
            while i > 0:
                s += self.tree[i]
                i -= i & -i
            return s

        def add(self, i, x):
            while i <= self.size:
                self.tree[i] += x
                i += i & -i

        def lower_bound(self, w):
            if w <= 0:
                return 0
            x = 0
            k = 1 << (self.size.bit_length() - 1)
            while k:
                if x + k <= self.size and self.tree[x + k] < w:
                    w -= self.tree[x + k]
                    x += k
                k >>= 1
            return x + 1

    N, Q = map(int, input().split())
    A = list(map(int, input().split()))
    bit = Bit(N)
    for i, a in enumerate(A):
        bit.add(i+1, a)
    for _ in range(Q):
        q, x, y = map(int, input().split())
        if q == 0:
            bit.add(x+1, y)
        else:
            print(bit.sum(y) - bit.sum(x))


if __name__ == '__main__':
    main()


Measured memory_bytes: 126136
def f(a):
  if a%2==0:
    a = a//2
  else:
    a = a*3+1
  return a
s = int(input())
cnt = [0]*1000001
cnt[s] = 1
ans = 1
while 1:
  s = f(s)
  if cnt[s]==0:
    ans += 1
    cnt[s]=1
  elif cnt[s]==1:
    ans += 1
    print(ans)
    exit()

Measured memory_bytes: 10868
from bisect import bisect_left
from itertools import accumulate

N,M = map(int,input().split())
A = sorted(list(map(int,input().split())))
A_r = list(reversed(A))
B = [0] + list(accumulate(A_r))

def func(x):
    count = 0
    for Ai in A:
        idx = bisect_left(A,x-Ai)
        count += N-idx
    if count >= M:
        return True
    else:
        return False

MIN = 0
MAX = 2*10**5+1
while MAX-MIN > 1:
    MID = (MIN+MAX)//2
    if func(MID):
        MIN = MID
    else:
        MAX = MID

ans = 0
count = 0

for Ai in A_r:
    idx = bisect_left(A,MIN-Ai)
    ans += Ai*(N-idx) + B[N-idx]
    count += N-idx
print(ans-(count-M)*MIN)

Measured memory_bytes: 15920
#include <iostream>
#include <cstdint>
#include <queue>
#include <algorithm>
#include <vector>
#include <iterator>
#include <numeric>
#include <limits>

int main() {
  std::int_fast64_t n;
  std::cin >> n;
  std::vector<std::int_fast64_t> a(3 * n);
  std::copy_n(std::istream_iterator<std::int_fast64_t>(std::cin), 3 * n, a.begin());

  std::vector<std::int_fast64_t> left_sum(n + 1);
  left_sum[0] = std::accumulate(a.begin(), a.begin() + n, static_cast<std::int_fast64_t>(0));
  std::priority_queue<std::int_fast64_t, std::vector<std::int_fast64_t>, std::greater<std::int_fast64_t>> left_max_n(a.begin(), a.begin() + n);
  for (std::int_fast64_t i = 1; i <= n; ++i) {
    left_max_n.push(a[n + i - 1]);
    left_sum[i] = left_sum[i - 1] + a[n + i - 1] - left_max_n.top();
    left_max_n.pop();
  }

  std::vector<std::int_fast64_t> right_sum(n + 1);
  right_sum[0] = std::accumulate(a.rbegin(), a.rbegin() + n, static_cast<std::int_fast64_t>(0));
  std::priority_queue<std::int_fast64_t, std::vector<std::int_fast64_t>, std::less<std::int_fast64_t>> right_min_n(a.rbegin(), a.rbegin() + n);
  for (std::int_fast64_t i = 1; i <= n; ++i) {
    right_min_n.push(a[2 * n - i]);
    right_sum[i] = right_sum[i - 1] + a[2 * n - i] - right_min_n.top();
    right_min_n.pop();
  }

  std::int_fast64_t result = std::numeric_limits<std::int_fast64_t>::min();
  for (std::int_fast64_t i = 0; i <= n; ++i) {
    result = std::max(result, left_sum[i] - right_sum[n - i]);
  }
  std::cout << result << std::endl;
  return 0;
}

Measured memory_bytes: 6516
import java.util.*;
public class Main {
	public static void main(String args[]){
		ArrayList[] a;
		a=new ArrayList[10];
		a[1]=new ArrayList();
		for(int i=1;i<=9;i++){
			for(int j=1;j<=9;j++){
				System.out.printf("%dx%d=%d\n",i,j,i*j);
			}
		}
		
	}
}

Measured memory_bytes: 17956
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define max(a, b)	(((a) > (b)) ? (a) : (b))		/* ２個の値の最大値 */
#define min(a, b)	(((a) < (b)) ? (a) : (b))		/* ２個の値の最小値 */
#define ENTER		printf("\n")					/* 改行プリント */
int DBG = 1;										/* デバッグプリント 提出時は0 */
/* main *************************************************************/
int		a[200001];

int main()
{
	int		b,c,i,j,k,n,m1,m2,x,y,ans = 0;

	
	scanf("%d", &n);
	for(i=0;i<n;i++) scanf("%d", &a[i]);

	m1 = max(a[0],a[1]);
	m2 = min(a[0],a[1]);
	for (i=2; i<n; i++) {
		if (a[i] > m1) {
			m2 = m1;
			m1 = a[i];
		} else if (a[i] > m2) {
			m2 = a[i];
		}
	}

	for (i=0; i<n; i++) {
		if (a[i]==m1) {
			printf("%d\n",m2);
		} else {
			printf("%d\n",m1);
		}
	}

	return 0;
}


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

int main(void){
    int n, k;
    cin >> n >> k;
    int dis[n];
    for(int i = 0; i < n; i++)
        cin >> dis[i];
    int check[n-k+1];
    int ans = 2000000000;
    
    for(int i = 0; i < n-k+1; i++){
        if(dis[i] < 0 && dis[i+k-1] >= 0)
            check[i]=min(abs(2*dis[i])+dis[i+k-1], 2*dis[i+k-1]+abs(dis[i]));
        else if(dis[i+k-1]<0)
            check[i] = abs(dis[i]);
        else if(dis[i] >= 0 && dis[i+k-1] >= 0)
            check[i]=dis[i+k-1];
        ans = min(ans, check[i]);
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 768
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);

		int number = scanner.nextInt();
		long fizzBuzzSum = 0;

		for (int i = 0; i <= number; i++) {

			if (i % 3 != 0 && i % 5 != 0 && i % 15 != 0) {
				fizzBuzzSum = fizzBuzzSum + i;
			}

		}

		System.out.println(fizzBuzzSum);

		scanner.close();
	}

}


Measured memory_bytes: 35648
#include<iostream>
#include<bits/stdc++.h>
#define mod 1000000007
#define eps 1e-9
#define PI 3.14159265358979323846
#define F first
#define S second
#define ll long long
#define pb push_back
#define mp make_pair
#define Fr(i,a,b) for(int i=a;i<=b;i++)
#define RF(i,a,b) for(int i=a;i>=b;i--)
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair< int, int>
#define vpii vector<pii>
using namespace std;

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

    int t=1;
    //cin>>t;
    while(t--){
    	string a,b;
    	cin>>a>>b;
    	int n=a.length();
    	int m=b.length();
    	vvi dp(n+1,vi(m+1,0));
    	Fr(i,1,n){
    		Fr(j,1,m){
    			if(a[i-1]==b[j-1]){
    				dp[i][j]=dp[i-1][j-1]+1;
    			}
    			else{
    				dp[i][j]=max(dp[i][j-1],max(dp[i-1][j],dp[i-1][j-1]));
    			}
    			
    		}
    	}
    	//cout<<dp[n][m]<<endl;
    	int x=n;
    	int y=m;
    	string s="";
    	while(x!=0&&y!=0){
    		if(a[x-1]==b[y-1]){
    			s=a[x-1]+s;
    			x--;y--;
    		}
    		else if(dp[x][y]==dp[x-1][y]){
    			x--;
    		}
    		else if(dp[x][y]==dp[x][y-1]){
    			y--;
    		}
    		else{
    			x--;y--;
    		}
    	}
    	cout<<s<<endl;
    }
    return 0;
}


Measured memory_bytes: 35584
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const int MAX = 4e5 + 10;
template <class T> struct SegTree {
	int n, N; vector <T> tree, lazy;
	const T ID = 0;
	void init(int _n) { n = _n, N = 4 * _n; tree.assign(N, ID), lazy.assign(N, ID); }
	T comb(T a, T b) { return (a + b) % MOD; }
	void prop(int i, int st, int en) {
		tree[i] = comb(tree[i], lazy[i]);
		if(st != en) {
			lazy[2 * i] = comb(lazy[2 * i], lazy[i]);
			lazy[2 * i + 1] = comb(lazy[2 * i + 1], lazy[i]);
		}
		lazy[i] = 0;
	}
	void _update(int i, int st, int en, int l, int r, T val) {
		if(lazy[i]) prop(i, st, en);
		if(en < l || st > r) return;
		if(l <= st && en <= r) {
			lazy[i] = val;
			prop(i, st, en);
			return;
		}
		int mid = (st + en) / 2;
		_update(2 * i, st, mid, l, r, val);
		_update(2 * i + 1, mid + 1, en, l, r, val);
		tree[i] = comb(tree[2 * i], tree[2 * i + 1]);
	}
	T _query(int i, int st, int en, int l, int r) {
		if(lazy[i]) prop(i, st, en);
		if(en < l || st > r) return ID;
		if(l <= st && en <= r) return tree[i];
		int mid = (st + en) / 2;
		return comb(_query(2 * i, st, mid, l, r), _query(2 * i + 1, mid + 1, en, l, r));
	}
	T query(int l, int r) { return _query(1, 1, n, l, r); }
	void update(int l, int r, T val) { _update(1, 1, n, l, r, val); }
};
int main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	
	int n; cin >> n;
	int k; cin >> k;
	SegTree <ll> seg; seg.init(MAX);
	seg.update(1, 1, 1);
	vector <pair <int, int>> vp(k);
	for(auto &p : vp) cin >> p.first >> p.second;
	for(int pos = 1; pos <= n; pos ++) {
		ll cur = seg.query(pos, pos);
		for(int i = 0; i < k; i ++) {
			seg.update(pos + vp[i].first, pos + vp[i].second, cur);
		}
	}
	cout << seg.query(n, n);
	
	return 0;
}

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

int ans(int N, vector<int> H) {
  int a = 0;
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (a <= H.at(i)) {
      ans++;
      a = H.at(i);
    }
  }
  return ans;
}
 
int main() {
  int N;
  cin >> N;
  vector<int> H(N);
  for (int i = 0; i < N; i++) {
    cin >> H.at(i);
  }
  cout << ans(N, H) << endl;
}

Measured memory_bytes: 256
#include<algorithm>
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<cmath>
#include<list>
#include<set>
#include<map>
//#include<bits/stdc++.h>

using namespace std;
long long MOD = 1000000007LL;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\n"

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

	map<int, int> dp;
	for (int i = 1; i < N; ++i) {
		if (s[i] == 'W') {
			++dp[0];
		}
	}


	for (int i = 1; i < N; ++i) {
		dp[i] = dp[i - 1];
		if (s[i - 1] == 'E')++dp[i];
		if (s[i] == 'W')--dp[i];
	}

	int ans = N;
	for(auto i = dp.begin(); i != dp.end(); ++i){
		ans = min(ans, N - i->second - 1);
	}

	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 14596
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def main():
    n, m = map(int, readline().split())
    p = list(map(int, readline().split()))
    uf = UnionFind(n + 1)

    for _ in range(m):
        x, y = map(int, readline().split())
        uf.union(x, y)

    ans = 0
    for i, num in enumerate(p, 1):
        if uf.same(i, num):
            ans += 1

    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 84312
#include <algorithm>
#include <cstdio>
#include <map>

using namespace std;

int N, A[100005];
int aux[100005];
map<int,int> tbl;

int solve() {
    for (int i = 1; i <= N; i++) aux[i] = A[i];
    sort(aux+1, aux+N+1);
    for (int i = 1; i <= N; i++) tbl[aux[i]] = i;
    for (int i = 1; i <= N; i++) A[i] = tbl[A[i]];
    int mismatch = 0;
    for (int i = 1; i <= N; i++) {
        int sum = i+A[i];
        if (sum % 2) mismatch++;
    }
    return mismatch/2;
}

int main() {
    scanf("%d", &N);
    for (int i = 1; i <= N; i++) scanf("%d", A+i);
    int ans = solve();
    printf("%d\n", ans);
    return 0;
}

Measured memory_bytes: 5760
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <string>
#define int long long
#define P pair<int,int>
#define V vector<int>
#define rep(i,n) for(int i=0;i<n;i++)
#define vsort(vec) sort(vec.begin(),vec.end())
using namespace std;
signed main() {
	int n, x, a[100000], memo = 0, memo2 = 0;
	cin >> n >> x;
	rep(i, n) {
		cin >> a[i];
		memo += a[i];
		a[i] = min(a[i], x);
		if (i != 0) {
			int b = a[i - 1] + a[i];
			int c = min(a[i], max(0ll, b - x));
			a[i] -= c;
			a[i - 1] -= max(0ll, b - x - c);
			memo2 += a[i - 1];
		}
	}
	memo2 += a[n - 1];
	cout << memo - memo2 << endl;
	system("PAUSE");
	return 0;
}


Measured memory_bytes: 1272
import math
n = int(input())
hp = [int(x) for x in input().split()]
for i in range(n-1):
  a = hp[i]
  b = hp[i+1]
  r = a % b
  if r == 0:
    ans = b
    continue
  while r != 0:
    ans = r
    a = b
    b = r
    r = a % b
  hp[i+1] = ans
print(ans)
a = b * i + r

Measured memory_bytes: 14252
#include <bits/stdc++.h>
#define sz(arr) (int)(arr).size()
#define rng(arr) arr.begin(), arr.end()
#define show(x) cout << #x << " = " << x << endl;
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;
typedef long long int ll;

void _cin() {}
template <class Head, class... Tail>
void _cin(Head&& head, Tail&&... tail) {
  cin >> head;
  _cin(forward<Tail>(tail)...);
}

#define Cin(T, ...) \
  T __VA_ARGS__;    \
  _cin(__VA_ARGS__)

#define Cins(T, n, xs) \
  vector<T> xs(n);     \
  for (int i = 0; i < n; ++i) cin >> xs[i]

void Main();
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}

void Main() {
  Cin(int, n, m);
  vector<int> s(m);
  vector<char> c(m);
  rep(i, m) cin >> s[i] >> c[i];

  rep(i, m) {
    for (int j = i + 1; j < m; ++j) {
      if (s[i] == s[j] && c[i] != c[j]) {
        cout << -1 << endl;
        return;
      }
    }
  }

  string ans(n, '0');
  if (n != 1) ans[0] = '1';
  rep(i, m) ans[s[i] - 1] = c[i];
  if (n != 1 and ans[0] == '0') {
    cout << -1 << endl;
    return;
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
#include <stdio.h>
#define N 100
#define INFTY 100000
#define WHITE 0
#define BLACK 1

int n,color[N],A[N][N],A1[N],A2[N];

void prim(){
  int min,i,j;
  for(i=0;i<n;i++){
    A1[i]=INFTY;
    A2[i]=-1;
    color[i]=WHITE;
  }

  A1[0]=0;

  while(1){
    min=INFTY;
    for(i=0;i<n;i++){
      if(color[i]!=BLACK && A1[i]<min){
        min = A1[i];
        j=i;
      }
    }

    if(min == INFTY) break;
    color[j] = BLACK;
    for(i=0;i<n;i++){
      if(color[i]!=BLACK && A[j][i]<A1[i] && A[j][i]>=0){
        A2[i]=j;
        A1[i]=A[j][i];
      }
    }
  }
}

int main(){
  int i,j,temp=0;

  scanf("%d",&n);
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      scanf("%d",&A[i][j]);
    }
  }
  prim();
  for(i=0;i<n;i++) temp+=A1[i];
printf("%d\n",temp);
return 0;
}


Measured memory_bytes: 2088
#include <bits/stdc++.h>
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)
#define ALL(x) (x).begin(),(x).end() 
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define INF 1000000000
using namespace std;
typedef long long ll;

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

    if (X < A) cout << 0 << endl;
    else cout << 10 << endl;
}

Measured memory_bytes: 3636
import java.util.*;
public class Main {
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int d = sc.nextInt();
    int n = sc.nextInt();
    
    int num = (int)Math.pow(100,d);
        
    int ans = num * n;
    
    if(n==100){
      ans += num;
    }
    
    System.out.println(ans);
    
  }
}


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

public class Main {

	final static long MOD = 1000000007L;
	public static String[] Ws = null;
	public static int wsIndx = 0;
	public static BufferedReader in = null;
	public static ArrayList<int[]> list = new ArrayList<>();
	public static void main(String[] args) throws Exception {

		int N = nextInt();
		int M = nextInt();

		int[] num = new int[N+1];
		Arrays.fill(num, 1);
		boolean[] p = new boolean[N+1];
		Arrays.fill(p, false);
		p[1]=true;
		for(int i=0;i<M;i++){
			int x=nextInt();
			int y=nextInt();
			num[x]--;
			num[y]++;
			if(p[x]){
				p[y]=true;
			}
			if(num[x]==0&&p[x]){
				p[x]=false;
			}
		}
		long ans =0;

		for(int i=0;i<p.length;i++){
			if(p[i]){
				ans++;
			}
		}
		System.out.println(ans);

	}

	public static long[] disassemblyAlph(String s) {
		long[] alph = new long['z' - 'a' + 1];
		Arrays.fill(alph, 0);
		for (int i = 0; i < s.length(); i++) {
			alph[s.charAt(i) - 'a']++;
		}
		return alph;
	}

	public static String[] toStringArray(String s) {
		String[] tmp = new String[s.length()];
		char[] c = s.toCharArray();
		for (int i = 0; i < tmp.length; i++) {
			tmp[i] = String.valueOf(c[i]);
		}
		return tmp;
	}

	public static String concatStringArray(String[] s){
		StringBuffer sb = new StringBuffer();
		for(String x:s){
			sb.append(x);
		}
		return sb.toString();
	}

	public static void check() throws Exception{
		if(in == null){
			in = new BufferedReader(new InputStreamReader(System.in));
		}
		if(Ws==null || Ws.length<=wsIndx){
			Ws = in.readLine().split(" ");
			wsIndx=0;
		}
	}
	public static int nextInt()throws Exception{
		check();
		return Integer.parseInt(Ws[wsIndx++]);
	}

	public static long nextLong()throws Exception{
		check();
		return Long.parseLong(Ws[wsIndx++]);
	}

	public static String nextString()throws Exception{
		check();
		return Ws[wsIndx++];
	}

	public static int[] nextInts()throws Exception{
		check();
		int[] tmp = new int[Ws.length];
		for(int i=0;i<tmp.length;i++){
			tmp[i]=Integer.parseInt(Ws[i]);
		}
		wsIndx=Ws.length;
		return tmp;
	}

	public static long[] nextLongs()throws Exception{
		check();
		long[] tmp = new long[Ws.length];
		for(int i=0;i<tmp.length;i++){
			tmp[i]=Long.parseLong(Ws[i]);
		}
		wsIndx=Ws.length;
		return tmp;
	}

	public static String[] nextStrings()throws Exception{
		check();
		wsIndx=Ws.length;
		return Ws;
	}


}

Measured memory_bytes: 48584
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n;
  cin>>n;
int count=0,ans=0;
for(int i=0;i<n;i++){
  int l;
    cin>>l;
count=max(count,l);
ans+=l;
}
  if(count>=ans-count)
    cout<<"No"<<endl;
  else
    cout<<"Yes"<<endl;
  return 0;
}

Measured memory_bytes: 256
S = input()
num_cnt = len(S)
op_cnt = len(S) - 1  # すき間の個数
ope_sum = 0
for i in range(2 ** op_cnt):
    op = [""] * op_cnt  # あらかじめ ["-", "-", "-"] というリストを作っておく
    for j in range(op_cnt):
        if ((i >> j) & 1):
            op[op_cnt - 1 - j] = "+"  # フラグが立っていた箇所を "+" で上書き

    formula = ""
    for j in range(num_cnt):
        if j==num_cnt-1:
            formula = formula + S[j]
            ope_sum = ope_sum + eval(formula)
            break
        formula = formula + S[j] + op[j]

print(ope_sum)


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

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n), c(n), d(n);
  vector<pair<int, int>> cd;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> c[i] >> d[i];
    cd.push_back(make_pair(c[i], i));
  }

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

  int cnt = 0;
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    int bx = c[cd[i].second], by = d[cd[i].second];
    int mx      = -1;
    int mnIndex = -1;

    for (int j = 0; j < n; j++) {
      if (m[j]) continue;
      int rx = a[j], ry = b[j];
      if (rx < bx && ry < by) {
        if (mx < ry) {
          mx      = ry;
          mnIndex = j;
        }
      }
    }
    if (mnIndex != -1) {
      cnt++;
      m[mnIndex]++;
    }
  }
  cout << cnt << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int n,x;
    int count=0;
    while(1){
        cin >> n >> x;
        if(n==0 && x==0){
            break;
        }
        int i,j,k;
        for(i=1;i<(n-1);i++){
            for(j=i+1;j<n;j++){
                for(k=j+1;k<=n;k++){
                    if(i+j+k==x){
                        count++;
                        break;
                    }
                }
            }
        }
        cout << count << endl;
        count = 0;
    }

    return 0;
}



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

// iterator
#define REP(i,init, n) for(lli i=init;i<n;i++)
#define REPE(i,init, n) for(lli i=init;i<=n;i++)
#define REPIT(it,container) for(auto it = container.begin(); it != container.end(); it++)
#define REPIT_R(it,container) for(auto it = container.rbegin(); it != container.rend(); it++)

// input
#define cin1(x)             cin >> x
#define cin2(x, y)          cin >> x >> y
#define cin3(x, y, z)       cin >> x >> y >> z
#define ncin1(n, x)         REP(i, 0, n) {cin1(x[i]);}
#define ncin2(n, x, y)      REP(i, 0, n) {cin2(x[i], y[i]);}
#define ncin3(n, x, y, z)   REP(i, 0, n) {cin3(x[i], y[i], z[i]);}

// output
#define cout1(x)         cout << #x << ": " << x << endl;
#define cout2(x, y)      cout << #x << ": " << x << ", " << #y << ": " << y << endl;
#define cout3(x, y, z)   cout << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << endl;;
#define ncout1(n, x)     REP(i, 0, n) {cout << #x << "[" << i << "]: "<< x[i] << endl;}

#define coutp(p)         cout << #p << ":" <<  " (" << p.first << ", " << p.second << ")" << endl;

// sort
#define sort_r(x, y)        sort(x, y, greater<lli>()); // 降順(5,4,3,,,)

#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))

typedef long long int lli;
typedef pair<lli, lli> P;
typedef tuple<lli, lli, lli> tup;

int main() {
    lli a, b;
    cin2(a, b);

    a--;
    lli fa;
    if (a % 2 == 0) {
        fa = a;
        if ((a/2) % 2 == 1)
            fa++;
    }
    else {
        fa = 0;
        if ((((a+1) / 2) % 2 == 1))
            fa = 1;
    }

    lli fb;
    if (b % 2 == 0) {
        fb = b;
        if ((b/2) % 2 == 1)
            fb++;
    }
    else {
        fb = 0;
        if ((((b+1) / 2) % 2 == 1))
            fb = 1;
    }
    //cout1(fa);
    //cout1(fb);
    cout << (fa ^ fb) << endl;

    // 以下の解法は、O(log(B))
    //b++;
    //lli div = 2;
    //lli ans = 0;
    //while (true) {
    //    lli half = div/2;
    //    lli ap = a/div;
    //    lli aq = a%div;
    //    lli a_count = ap * (div/2);
    //    if (half < aq)
    //        a_count += aq- half;

    //    lli bp = b/div;
    //    lli bq = b%div;
    //    lli b_count = bp * (div/2);
    //    if (half < bq)
    //        b_count += bq- half;
    //    if ((b_count - a_count) % 2 == 1)
    //        ans += half;
    //    div *= 2;
    //    if (half > b)
    //        break;
    //}
    //cout << ans << endl;

}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <algorithm>  
#include <unordered_set>
#define _USE_MATH_DEFINES
#include <cmath>
#define ll long long

using namespace std;

const double pi = 3.14159265358979323846;

vector<int> g[100005];

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int N, M;

	cin >> N >> M;

	int a, b;
	queue<int> room;

	for (int i = 0; i < M; i++) {
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}

	int ans[100005] = { 0 };
	ans[1] = 1;
	room.push(1);

	while (!room.empty()) {

		int curr = room.front();
		room.pop();

		for (auto b : g[curr]) {
			if (!ans[b]) {
				ans[b] = curr;
				room.push(b);
			}
		}
	}

	cout << "Yes" << endl;

	for (int i = 1; i < N; i++) {
		cout << ans[i+1] << endl;
	}






	return 0;
}

Measured memory_bytes: 10372
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

axor=[0]*n
bxor=[0]*n

for i in range(n-1):
    axor[i] = a[i] ^ a[i+1]
    bxor[i] = b[i] ^ b[i+1]

axor[-1] = a[0] ^ a[-1]
bxor[-1] = b[0] ^ b[-1]

def MP(it):
    n = len(it)
    res = [0]*(n+1)
    res[0] = -1
    j = -1
    for i in range(n):
        while j >= 0 and it[i] != it[j]:
            j = res[j]
        j += 1
        res[i+1] = j
    return res

mp = MP(bxor)
txt = axor + axor
pattern = bxor
ans = list()
j = 0
for i in range(2*n-1):
    while j >= 0 and pattern[j] != txt[i]:
        j = mp[j]
    j += 1
    if j == n:
        k = i-j+1
        x = b[0] ^ a[k]
        ans.append([k,x])
        j = mp[j]


for k,x in ans:
    print(k,x)

Measured memory_bytes: 128080
#include <bits/stdc++.h>
 
#define rep(i,n) for(ll i=0;i<n;++i)
#define all_map(itr,mp) for(auto itr=mp.begin();itr!=mp.end();itr++)
#define ALL(a) (a).begin(),(a).end()

using namespace std;
using ll  = long long;
using pll = pair<ll, ll>;

const ll MOD  = 1e9 + 7;
const ll LINF = 1LL << 62;
const int INF = 1e9 + 7;

int main(){
    ll n, m;
    cin >> n >> m;

    for(ll i = m/n; i >= 1; --i){
        ll dif = m - i*n;
        if(dif % i == 0){
            cout << i << endl;
            return 0;
        }
    }
}

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

class Program
{
	public static void Main()
	{
		var n = int.Parse(ReadLine());
		var a = int.Parse(ReadLine());
		WriteLine(n * n - a);
	}
}


Measured memory_bytes: 17612
if __FILE__ == $0
  while line = STDIN.gets
    tokens = line.chomp!.split.map!(&:to_i)
    a, b= tokens[0], tokens[1]
    if a == b then
        puts "a == b"
    elsif a > b then
         puts "a > b"
    elsif a < b then
          puts "a < b"
    end
  end
end

Measured memory_bytes: 8652
// https://atcoder.jp/contests/jsc2019-qual/tasks/jsc2019_qual_d
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

fn deco(level: &mut Vec<Vec<i32>>, depth: i32, idx: Vec<usize>) {
    if idx.len() == 1 {
        return;
    }
    let n = idx.len();
    let mut a = vec![0; n/2];
    let mut b = vec![0; n-n/2];
    for i in 0..n/2 {
        a[i] = idx[i];
    }
    for i in n/2..n {
        b[i-n/2] = idx[i];
    }

    for &ai in a.iter() {
        for &bi in b.iter() {
            level[ai][bi] = depth;
            level[bi][ai] = depth;
        }
    }
    deco(level, depth+1, a);
    deco(level, depth+1, b);
}

fn print(level: Vec<Vec<i32>>) {
    let n = level.len();
    for i in 0..n-1 {
        for j in i+1..n {
            if j > i+1 {
                print!(" ");
            }
            print!("{}", level[i][j]);
        }
        println!("");
    }
}

fn main() {
    input! {
        n: usize
    };

    let mut level = dvec!(0; n, n);
    let mut idx = vec![0; n];
    for i in 0..n {
        idx[i] = i;
    }
    deco(&mut level, 1, idx);
    print(level);
}


Measured memory_bytes: 4604
#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;

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()


const int MAX_AB = 100;
int n, m, c;
int a[MAX_AB][MAX_AB], b[MAX_AB];
int ans = 0;
int main() {
    cin >> n >> m >> c;

    REP(i,m) cin >> b[i];

    REP(i,n) REP(j,m) cin >> a[i][j];


    REP(i,n)
    {
        int sum;
        sum = 0;
        REP(j,m)
        {
            sum += a[i][j] * b[j];
        }

        sum += c;

        if (sum > 0) 
            ans++;
    }

    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL<<62
#define inf 1000000007 

ll a[100010];

int main() {
	ll n;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	ll sum=0;
	bool sei=true;
	ll ans1=0;
	for(int i=0;i<n;i++){
		sum+=a[i];
		if(sei){
			sei=false;
			if(sum<=0){
				ans1+=abs(sum)+1;
				sum=1;
			}
		}
		else{
			sei=true;
			if(sum>=0){
				ans1+=abs(sum)+1;
				sum=-1;
			}
		}
	}
	ll ans2=ans1;
	ans1=0;sei=false;sum=0;
	for(int i=0;i<n;i++){
		sum+=a[i];
		if(sei){
			sei=false;
			if(sum<=0){
				ans1+=abs(sum)+1;
				sum=1;
			}
		}
		else{
			sei=true;
			if(sum>=0){
				ans1+=abs(sum)+1;
				sum=-1;
			}
		}
	}
	cout << min(ans1,ans2);
}

Measured memory_bytes: 1024
import sys

def main():
  N = int(input())
  S = list(input())
  if len(S) % 2 == 1:
    print('No')
    sys.exit()
  else:
    half = int(N / 2)
    for i in range(half):
      if S[i] != S[half + i]:
        print('No')
        sys.exit()
  print('Yes')

main()


Measured memory_bytes: 2940
#include <stdio.h>

int main(void)
{
    int a, b;
    scanf("%d %d", &a, &b);
    for (int i = 1; i <= 3; i++)
    {
        if ((a * b * i) % 2 == 1)
        {
            printf("Yes\n");
            return 0;
        }
    }
    printf("No\n");
    return 0;
}


Measured memory_bytes: 128
#include<stdio.h>
main(){
  int m,f,r,a,b;
  while(1){
    scanf("%d %d %d",&m,&f,&r);
    if(m==-1&&f==-1&&r==-1)break;
    a=m+f;
    if(m==-1||f==-1){
      printf("F\n");
    }
    else if(a>79){
      printf("A\n");
    }
    else if(a>64){
      printf("B\n");
    }
    else if(a>49){
      printf("C\n");
    }
    else if(a>29&&r>49){
      printf("C\n");
    }
    else if(a>29){
      printf("D\n");
    }
    else if(a<30){
      printf("F\n");
    }
  }
  return 0;
}

Measured memory_bytes: 584
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double db;
#define endl '\n'
#define fi first
#define se second
#define pi pair<ll,ll>
#define pii pair<ll,pi>
#define pb push_back
#define mk make_pair
int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int a, b, c;
	cin >> a >> b >> c;
	int k;
	cin >> k;
	int ans = 0;
	while (1) {
		if (b > a)break;
		b = 2 * b;
		ans++;
	}
	while (1) {
		if (c > b)break;
		c = 2 * c;
		ans++;
	}
	if (ans <= k)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
}


Measured memory_bytes: 3636
using System;
using System.Linq;

namespace Sample
{
	class Program
	{
		static void Main(string[] args)
		{
			var array = Console.ReadLine().Split().Select(s => int.Parse(s)).ToArray();
			var w = array[0];
			var h = array[1];
			var x = array[2];
			var y = array[3];
			var r = array[4];

			var result = (0 <= x - r) && (x + r <= w) && (0 <= y - r) && (y + r <= h);

			Console.WriteLine(result ? "Yes" : "No");
		}
	}
}



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

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
using ll = long long;
string char_to_string(char val) {
  return string(1, val);
}
int char_to_int(char val) {
  return val - '0';
}
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;
}
int vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance( vec.begin(), itr );
  if (index != vec.size()) { // 発見できたとき
    return 1;
  }
  else { // 発見できなかったとき
    return 0;
  }
}

int main() {
  int N, K; cin >> N >> K;
  ll ans = K * pow(K - 1, N -1);
  cout << ans << endl;
}


Measured memory_bytes: 384
#include <iostream>

using namespace std;

int main()
{
    int H, W, count;
    char a[128][128];
    bool row_white[128], col_white[128];

    cin >> H >> W;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> a[i][j];
        }
    }

    for (int i = 0; i < H; i++) {
        row_white[i] = true;
        for (int j = 0; j < W; j++) {
            if (a[i][j] == '#') {
                row_white[i] = false;
                break; 
            }
        }
    }
    for (int j = 0; j < W; j++) {
        col_white[j] = true;
        for (int i = 0; i < H; i++) {
            if (a[i][j] == '#') {
                col_white[j] = false;
                break;
            }
        }
    }

    for (int i = 0; i < H; i++) {
        if (row_white[i] == false) {
            for (int j = 0; j < W; j++) {
                if (col_white[j] == false) {
                    cout << a[i][j];
                }
            }
            cout << endl;
        }
    }
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define INF 1000000007
int main()
{
	long long a,cnt=1,no0=1,no9=1,no09=1,ans;
	cin>>a;
	for (long long i=0;i<a;i++)
	{
		cnt*=10;
		cnt%=INF;
		no0*=9;
		no0%=INF;
		no9*=9;
		no9%=INF;
		no09*=8;
		no09%=INF;
	}
	long long notneed=(no0+no9-no09)%INF;
	if (cnt>=notneed)
		ans=cnt-notneed;
	else
		ans=cnt+INF-notneed;
	cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 3632
// inputに入力データ全体が入る
function Main(input) {
    input = input.trim();
    input = input.split("\n");
    var N = input[0];
    var countA = 0;
    var countB = 0;
    var countAB = 0;
    var sumAB = 0;

    for(var i = 1; i <= N; i++){
        sumAB += input[i].split("AB").length-1;
        if(input[i].lastIndexOf("A") == input[i].length-1 && input[i].indexOf("B") != 0){
            countA++;
        }
        if(input[i].lastIndexOf("A") != input[i].length-1 && input[i].indexOf("B") == 0){
            countB++;
        }
        if(input[i].lastIndexOf("A") == input[i].length-1 && input[i].indexOf("B") == 0){
            countAB++;
        }
    }
    if(countAB == 0){
        sumAB += Math.min(countA,countB);
    }
    else if(countA+countB == 0){
        sumAB += countAB - 1;
    }
    else{
        sumAB += countAB + Math.min(countA,countB);
    }
    console.log(sumAB);
}
//*この行以降は編集しないでください（標準入出力から一度に読み込み、Mainを呼び出します）
Main(require("fs").readFileSync("/dev/stdin", "utf8"));

Measured memory_bytes: 23200
#include <bits/stdc++.h>
#define repr(i,from,to) for(int (i)=(from);(i)<(to);(i)++)
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
// const bool debug=true;
const bool debug=false;
#define DEBUG if(debug==true)
#define all(x) begin((x)), end((x))
#define vprint(x) for(auto a:(x)) cout << x << endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
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;
}

class ModCom {
    public:
        ModCom(int _n, long long _mod){
            assert(_n>=2);
            n = _n+1;
            mod = _mod;
            fac.assign(n,0);
            finv.assign(n,0);
            inv.assign(n,0);
            init();
        }

        long long com(int _n, int k){
            if(_n < k){return 0;}
            if(_n < 0 || k < 0){return 0;}
            return fac.at(_n) * (finv.at(k) * finv.at(_n - k) % mod) % mod;
        }

    private:
        int n;
        long long mod;
        vector<long long> fac;
        vector<long long> finv;
        vector<long long> inv;

        void init(void){
            fac.at(0) = fac.at(1) = 1;
            finv.at(0) = finv.at(1) = 1;
            inv.at(1) = 1;
            for(int i = 2;i < n; i++){
                fac.at(i) = fac.at(i - 1) * i % mod;
                inv.at(i) = mod - inv.at(mod % i) * (mod / i) % mod;
                finv.at(i) = finv.at(i - 1) * inv.at(i) % mod;
            }
            return;
        }
};
int main(void) {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ModCom com(201000,MOD);
    ll n,k;
    cin>>n>>k;
    ll res = 0;
    if (k==1) {
        cout << com.com(n,2) * 2 % MOD << endl;
        return 0;
    }
    rep(i,min(n,k+1)) {
        ll tmp = com.com(n,i) * com.com(n-1, i) % MOD;
        res += tmp;
        res %= MOD;
    }
    cout << res << endl;
    return 0;
}


Measured memory_bytes: 5120
#if 1
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <array>
#include <deque>
#include <algorithm>
#include <utility>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <numeric>
#include <assert.h>
#include <bitset>
#include <list>
#include <cmath>

auto & in = std::cin;
auto& out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI = 3.141592653589793238462643383279502884197169399375105820974944;

int32_t N;
int32_t A[100];
int32_t B[100];

int main()
{
	using std::endl;
	in.sync_with_stdio(false);
	out.sync_with_stdio(false);
	in.tie(nullptr);
	out.tie(nullptr);

	in >> N;
	for (size_t i = 0; i < N; i++)
	{
		in >> A[i];
		B[i] = A[i];
	}
	std::sort(B, B + N);
	int count = 0;
	for (size_t i = 0; i < N-1; i++)
	{
		if (B[i] != A[i]) {
			++count;
		}
	}
	if(count > 2)
	out << "NO\n";
	else out << "YES\n"; return 0;

	return 0;
}
#endif


Measured memory_bytes: 256
#include<stdio.h>

int main() {
    int X,i,flg;
    scanf("%d",&X);
    if(X == 2) {
	printf("%d\n", X);
	return 0;
    }	
    if(X%2 == 0) X++;
    while(1){
        flg = 0;	
	for(i = 3 ;i * i <= X; i+=2){
      	  if(X%i==0){
            flg = 1;
	    X+=2;
      	    break;
      	  }
        }
      if(flg == 0){	  
	  printf("%d\n",X);
        break;
      }
    }
    return 0;
}


Measured memory_bytes: 128
#include<iostream>
#include<vector>
#include<string>
#include<cmath>
using namespace std;
typedef long long ll;
int main()
{
	int n;
	cin>>n;
	int m;
	cin>>m;
	vector<int> v1,v2,v3,v4;
	for(int i=0;i<n;i++)
	{
		int a,b;
		cin>>a>>b;
		v1.push_back(a);
		v2.push_back(b);
	}
	for(int i=0;i<m;i++)
	{
		int a,b;
		cin>>a>>b;
		v3.push_back(a);
		v4.push_back(b);
	}
	for(int i=0;i<n;i++)
	{
		int maxi=1e9;
		int brojce=-1;
		for(int j=0;j<m;j++)
		{
			if(maxi>abs(v1[i]-v3[j])+abs(v2[i]-v4[j]))
			{
				maxi=abs(v1[i]-v3[j])+abs(v2[i]-v4[j]);
				brojce=j+1;
			}
		}
		cout<<brojce<<endl;
	}
		return 0;
}

Measured memory_bytes: 256
#include<stdio.h>

int main(void){
    int rows, columns;
    int a[100][100];
    scanf("%d %d", &rows, &columns);
    int i, j;
    for (i = 0; i < rows;i++){
        for (j = 0; j < columns;j++){
            scanf("%d", &a[i][j]);
        }
    }
    int sum;
    for (i = 0; i < rows; i++){
        sum = 0;
        for (j = 0; j < columns; j++){
            printf("%d ", a[i][j]);
            sum += a[i][j];
        }
        printf("%d\n", sum);
    }
    int Sumsum = 0;
    for (j = 0; j < columns;j++){
        sum = 0;
        for (i = 0; i < rows; i++){
            sum += a[i][j];
        }
        Sumsum += sum;
        printf("%d ", sum);
    }
    printf("%d\n", Sumsum);
    return 0;
}


Measured memory_bytes: 2100
#include <bits/stdc++.h>
#define int long long
#define lint long long
#define pii pair<int,int>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(c) (c).begin(),(c).end()
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MINF(a) memset(a,0x3f,sizeof(a))
#define POW(n) (1LL<<(n))
#define IN(i,a,b) (a <= i && i <= b)
using namespace std;
template <typename T> inline bool CHMIN(T& a,T b) { if(a>b) { a=b; return 1; } return 0; }
template <typename T> inline bool CHMAX(T& a,T b) { if(a<b) { a=b; return 1; } return 0; }
template <typename T> inline void SORT(T& a) { sort(ALL(a)); }
template <typename T> inline void REV(T& a) { reverse(ALL(a)); }
template <typename T> inline void UNI(T& a) { sort(ALL(a)); a.erase(unique(ALL(a)),a.end()); }
template <typename T> inline T LB(vector<T>& v, T a) { return *lower_bound(ALL(v),a); }
template <typename T> inline int LBP(vector<T>& v, T a) { return lower_bound(ALL(v),a) - v.begin(); }
template <typename T> inline T UB(vector<T>& v, T a) { return *upper_bound(ALL(v),a); }
template <typename T> inline int UBP(vector<T>& v, T a) { return upper_bound(ALL(v),a) - v.begin(); }
template <typename T1, typename T2> ostream& operator<< (ostream& os, const pair<T1,T2>& p) { os << p.first << " " << p.second; return os; }
template <typename T1, typename T2> istream& operator>> (istream& is, pair<T1,T2>& p) { is >> p.first >> p.second; return is; }
template <typename T> ostream& operator<< (ostream& os, const vector<T>& v) { REP(i,v.size()) { if (i) os << " "; os << v[i]; } return os; }
template <typename T> istream& operator>> (istream& is, vector<T>& v) { for(T& in : v) is >> in; return is; }
template <typename T = int> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...)); }
template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; }
template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for(auto &e : t) fill_v(e,v); }
const lint MOD = 1000000007;
const lint INF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-10;

int N;
int a[100010];
vector<int> g[100010];

int dfs(int v) {
    vector<int> L;
    for (int u : g[v]) {
        L.push_back(dfs(u));
    }
    if (L.empty()) return 0;
    SORT(L);
    int res = -1;
    REP(i, L.size()) {
        CHMAX(res, L[i] + (int)L.size() - i);
    }
    return res;
}

signed main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    cin >> N;
    FOR(i, 1, N) {
        cin >> a[i];
        g[a[i] - 1].push_back(i);
    }
    cout << dfs(0) << endl;

    return 0;
}

Measured memory_bytes: 14336
#include <stdio.h>

int main(int argc, char** argv)
{
	int i, j, k, h, l, cnt, n, x, result[1000];

	cnt = 0;
	
	memset(result, 0, sizeof(result));
	while (scanf("%d %d", &n, &x) != EOF) {
		int list_cnt = 0;
		int list[1500][3] = { { 0 } };
		for (i = 1; i <= n; i++) {
			for (j = 1; j <= n; j++) {
				if (i == j) {
					continue;
				}
				for (k = 1; k <= n; k++) {
					if (i == k || j == k) {
						continue;
					}
					if (i + j + k == x) {
						int ins_f = 1;
						for (h = 0; h < list_cnt; h++) {
							int match_cnt = 0;
							for (l = 0; l < 3; l++) {
								if (list[h][l] == i || list[h][l] == j || list[h][l] == k) {
									match_cnt++;
								}
							}

							if (match_cnt == 3) {
								ins_f = 0;
								break;
							}
						}

						if (ins_f) {
							list[list_cnt][0] = i;
							list[list_cnt][1] = j;
							list[list_cnt][2] = k;
							list_cnt++;
						}
						break;
					}
				}
			}
		}

		result[cnt] = list_cnt;

		if (n == 0 && x == 0) {
			break;
		}
		cnt++;
	}

	for (i = 0; i < cnt; i++) {
		printf("%d\n", result[i]);
	}
	return 0;
}

Measured memory_bytes: 632
#include <iostream>
#include <vector>
#include <climits>
#include <iomanip>
#include <cmath>
using namespace std;
using uint = unsigned int;

int main() {
	
	uint N;
	cin >> N;
	
	vector<double> X(N);
	vector<double> Y(N);
	
	double lx = 1000;
	double hx = 0;
	double ly = 1000;
	double hy = 0;
	
	for(uint i=0; i<N; i++) {
		cin >> X[i] >> Y[i];
		lx = min(lx, X[i]);
		hx = max(hx, X[i]);
		ly = min(ly, Y[i]);
		hy = max(hy, Y[i]);
	}
	
	double curX = (hx + lx) / 2;
	double curY = (hy + ly) / 2;
	double nxtX = 0;
	double nxtY = 0;
	
	double alfa = 0.1;
	uint idx = 0;
	
	while(alfa > pow(10, -9)) {	
		curX = nxtX;
		curY = nxtY;
		double maxD = 0;
		
		for(uint i=0; i<N; i++) {
			double d = pow(X[i] - curX, 2) + pow(Y[i] - curY, 2);
			
			if(maxD < d) {
				maxD = d;
				idx = i;
			}
		}
		
		nxtX = (1 - alfa) * curX + alfa * X[idx];
		nxtY = (1 - alfa) * curY + alfa * Y[idx];
		alfa = alfa * 0.9999;
	
	}
	
	double ans = sqrt(pow(X[idx] - curX, 2) + pow(Y[idx] - curY, 2));
	cout << setprecision(10) << ans;
	
	return 0;
}


Measured memory_bytes: 384
#include<algorithm>
#include<cstdio>
#define mxn 1000010
#define pii pair<int,int> 
#define fr first
#define sc second
#define mp make_pair
using namespace std;
int n,m,sl,fh,cnt,ans,cnt0,cnt1,col[710],a[710][710],dp[710][710];
pii s[710];
int t,h[710];
struct edge
{
	int to,nxt;
}e[mxn];
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;
}
void add(int u,int v) {e[++t]=(edge){v,h[u]};h[u]=t;}
void dfs(int u,int c)
{
	int v;col[u]=c;
	if(col[u]==-1) cnt0++;
	else cnt1++;
	for(int i=h[u];i;i=e[i].nxt)
		if(!col[v=e[i].to]) dfs(v,-c);
		else if(col[u]==col[v]) {puts("-1"); exit(0);}
}
int main()
{
	n=rd();m=rd();int x,y;
	for(int i=1;i<=m;++i) x=rd(),y=rd(),a[x][y]=a[y][x]=1;
	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j)
			if(i!=j&&!a[i][j])
				add(i,j);
	for(int i=1;i<=n;++i)
		if(!col[i])
			cnt0=cnt1=0,dfs(i,1),s[++cnt]=mp(cnt0,cnt1);
	dp[0][0]=1;
	for(int i=1;i<=cnt;++i)
	{
		for(int j=0;j<=n;++j)
			if(dp[i-1][j])
				dp[i][j+s[i].fr]=dp[i][j+s[i].sc]=1;
	}
	ans=n*n;
	for(int i=1;i<=n;++i)
		if(dp[cnt][i])
			ans=min(ans,i*(i-1)/2+(n-i)*(n-i-1)/2);
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 6528
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void scan(T...)(ref T a) {
    string[] ss = readln.split;
    foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;

void main() {
    int n, m; scan(n, m);

    int lo = 0;
    int hi = n;
    for (int i = 0; i < m; i++) {
        int l, r; scan(l, r);
        lo = max(lo, l);
        hi = min(hi, r);
    }
    writeln(max(0, hi - lo + 1));
}


Measured memory_bytes: 1276
#include<iostream>
using namespace std;
int main()
{
	int t,n,s,f,sum;
	
	while(1){
		sum = 0;
		cin >> t;
		if(t == 0) break;
		cin >> n;
		for(int i=0;i<n;i++){
			cin >> s >> f;
			sum += f - s;
		}
		if(sum >= t) cout << "OK" << endl;
		else cout << t - sum << endl;
	}
	return 0;
}

Measured memory_bytes: 1160
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll N=1e5+8;
#define  pb push_back
vector<ll> G[N];
ll n,m;
ll dp[N];
ll solve(ll i){
    if(dp[i]!=-1)return dp[i];
    dp[i]=0;
    for(ll p:G[i]){
        dp[i]=max(dp[i],1+solve(p));
    }
    return dp[i];
}
int main(){
    cin>>n>>m;
    for(ll i=0;i<m;i++){
        ll x,y;
        cin>>x>>y;
        G[x].pb(y);
    }
    memset(dp,-1,sizeof dp);
    ll ans=0;
    for(ll i=1;i<=n;i++){
        ans=max(ans,solve(i));
    }
    cout<<ans<<"\n";
    return 0;
}

Measured memory_bytes: 7808