akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#include <algorithm>
#define mp make_pair
using namespace std;

int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
typedef pair<int,pair<int,int> > P;

int main(){
	int r,w,h,x,y,field[500][500],rank[500][500];
	while(cin >> r,r){
		map<int,int> sum;
		vector<int> vec[2][2];
		for(int i = 0;i < 2;i++){
			sum.clear();
			cin >> w >> h >> x >> y;
			for(int j = 0;j < h;j++){
				for(int k = 0;k < w;k++){
					cin >> field[j][k];
					rank[j][k] = -1;
				}
			}
			rank[y - 1][x - 1] = 1;
			priority_queue<P,vector<P>,greater<P> > que;
			que.push(mp(1,mp(y - 1,x - 1)));
			while(!que.empty()){
				P p = que.top();que.pop();
				int nowr = p.first,nowy = p.second.first,nowx = p.second.second;
				for(int j = 0;j < 4;j++){
					int ny = nowy + dy[j],nx = nowx + dx[j];
					if(ny >= 0 && ny < h && nx >= 0 && nx < w && rank[ny][nx] == -1){
						if(field[ny][nx] > nowr){
							rank[ny][nx] = field[ny][nx];
							que.push(mp(rank[ny][nx],mp(ny,nx)));
						}else{
							rank[ny][nx] = nowr;
							que.push(mp(nowr,mp(ny,nx)));
						}
					}
				}
			}
			for(int j = 0;j < h;j++){
				for(int k = 0;k < w;k++){
					sum[rank[j][k]]++;
				}
			}
			int s = 0;
			vec[i][0].push_back(0);
			vec[i][1].push_back(0);
			for(auto itr = sum.begin();itr != sum.end();itr++){
				s += itr->second;
				vec[i][0].push_back(s);
				vec[i][1].push_back(itr->first);
			}
		}
		int mi = 1e+9;
		for(int i = 0;i < vec[0][0].size();i++){
			int pos = lower_bound(vec[1][0].begin(),vec[1][0].end(),r - vec[0][0][i]) - vec[1][0].begin();
			if(pos < vec[1][0].size()) mi = min(mi,vec[0][1][i] + vec[1][1][pos]);
		}
		cout << mi << endl;
	}
	return 0;
}

Measured memory_bytes: 12288
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <queue>
#include <stack>
using namespace std;

int main(){
    string s;
    cin >> s;
    int c1 = 0;
    int c2 = 0;
    for(auto c: s){
        if(c == '0'){
            c1++;
        }else{
            c2++;
        }
    }
    cout << 2*min(c1, c2) << endl;
    return 0;
}


Measured memory_bytes: 1144
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

int main() {
    ios::sync_with_stdio(false);
    string s, t;
    cin >> s >> t;
    sort(s.begin(), s.end());
    sort(t.begin(), t.end()); reverse(t.begin(), t.end());
    if (s < t) cout << "Yes";
    else cout << "No";
}


Measured memory_bytes: 256
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a[] = new int[n + 1];
        int b[] = new int[n];
        for (int i = 0; i < n + 1; i++) {
            a[i] = sc.nextInt();
        }
        for (int i = 0; i < n; i++) {
            b[i] = sc.nextInt();
        }
        long ans = 0;

        for (int i = 0; i < n; i++) {
            if (a[i] < b[i]) {
                ans += a[i];
                // System.out.println(2);
                b[i] -= a[i];
                if (a[i + 1] < b[i]) {
                    ans += a[i + 1];
                    a[i + 1] = 0;
                } else {
                    a[i + 1] -= b[i];
                    ans += b[i];
                    // System.out.println(3);
                }
            } else {
                ans += b[i];
                // System.out.println(4);
            }
        }
        System.out.println(ans);
        sc.close();
    }
}


Measured memory_bytes: 63816
n = int(input())
t = list(map(int, input().split()))
m = int(input())
p_x = [list(map(int, input().split())) for _ in range(m)]

ans = []
for i in range(m):
    #どのドリンクを飲むか
    p, x = p_x[i]
    buf = 0
    for j in range(n):
        #何番の問題にかかる時間を加算するか
        if j == (p-1):
            buf += x
        else:
            buf += t[j]
    ans.append(buf)
for x in ans:
    print(x)

Measured memory_bytes: 3064
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int main(){
 	string a;
 	int i=0;
 		cin>>a;
	 if(a.length()==2)
	 	for(i=0;i<2;i++)
		 cout<<a[i]; 
	 else{
	 	for(i=2;i>=0;i--)
	 	cout<<a[i];
	 } 
}

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

int main()
{
     string Str;
     bool N, S;
     bool W,E;
    bool V,H, PRINT=true;
    
    N=S=W=E=V=H=false;
    
    cin>>Str;
    
    for(auto x : Str)
    { 
        if(x=='N'|| x=='S')
        {
            V=true;
         if(x=='N')
            N=true;
        else 
            S=true;   
        }else
        {
            H=true;
         if(x=='E')
            E=true;
         else 
            W=true;
         }
            
    }//end for
    
    if(V)
    {
        if(!N || !S)
           PRINT=false;
    }
        if(H && PRINT)
             if(!(E && W))
                    PRINT=false;
        
        if(PRINT)
            cout<<"Yes";
        else
             cout<<"No";
     
     return 0;
        
    
    
    
}

Measured memory_bytes: 256
n = int(input())
cnt = 0
judge = False
for i in range(n):
    a,b = map(int,input().split())
    if a == b:
       cnt += 1
    else:
        cnt = 0
    if cnt == 3:
        judge = True
if judge:
    print("Yes")
else:
    print("No")

Measured memory_bytes: 9180
// Created by Nandu Vinodan
#include <bits/stdc++.h>

#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define rep(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define vi vector<int>
#define pi pair<int, int>
#define INF INT_MAX
#define _INF INT_MIN
#define TC 0

using namespace std;
using ll = long long;


int h, n;

int cost[1005], hea[1005];
//int dp[1005][1005];

// int solve(int h, int n) {
// 	//db2(h, n);
// 	if (h <= 0)
// 		return 0;

// 	if (dp[h][n] != -1)
// 		return dp[h][n];
// 	if (n==0)
// 		return dp[h][n]=INT_MAX;

// 	// using n
// 	int a = cost[n-1] + solve(h-hea[n-1], n);
// 	int b = solve(h, n-1);

// 	return dp[h][n] = min (a, b);
// }

void testcase() {
    cin >> h >> n;

    rep (i, 0, n-1) {
    	cin >> hea[i] >> cost[i];
    }
    vector<int> cur(h+3), pre(h+3, INT_MAX);

    rep(i, 1, n) {
    	cur = pre;
    	cur[0] = 0;
    	rep (j, 1, h) {
    		int a = INT_MAX;
    		if (j-hea[i-1] < 0)
    			a = cost[i-1];
    		else if (cur[j-hea[i-1]] != INT_MAX)
    			a = cost[i-1] + cur[j-hea[i-1]];
    		int b = pre[j];
    		//db3(i, a, b);
    		cur[j] = min(a, b);
    	}

    	// db1(i);
    	// rep(i, 0, h)
    	// 	cout << cur[i] << " ";
    	// cout << endl;
    	pre = cur;
    }
    cout << cur[h] << endl;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    if (TC)
        cin>>t;
    while(t--) {
        testcase();
    }
    return 0;
}

Measured memory_bytes: 384
use std::cmp::{max, min};
use std::collections::{HashMap, HashSet, VecDeque};
use std::io::{stdin, Read};
use std::str::FromStr;

// https://qiita.com/tubo28/items/e6076e9040da57368845
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

#[allow(dead_code)]
fn main() {
    let n = read::<usize>();
    let m = read::<usize>();
    let ab = (0..m)
        .map(|_| (read::<usize>(), read::<usize>()))
        .collect::<Vec<_>>();
    let mut t = vec![vec![0; n]; n];
    for (a, b) in ab {
        t[a - 1][b - 1] = 1;
        t[b - 1][a - 1] = 1;
    }

    let mut node = vec![0; n];
    let ans = visit(0, &t, &mut node, n + 1, 1);
    println!("{}", ans);

    fn visit(i: usize, t: &Vec<Vec<i32>>, node: &mut Vec<i32>, prev: usize, count: usize) -> usize {
        if node[i] == 1 {
            return 0;
        }
        if count == node.len() {
            return 1;
        }

        node[i] = 1;
        let mut ans = 0;
        for (j, &x) in t[i].iter().enumerate() {
            if x == 1 {
                ans += visit(j, t, node, i, count + 1);
            }
        }
        node[i] = 0;
        ans
    }
}


Measured memory_bytes: 4352
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int t, n, a, h[];
		double h2[];
		
		n = sc.nextInt();
		t = sc.nextInt();
		a = sc.nextInt();
		
		h = new int[n];
		h2 = new double[n];
		
		for(int i = 0; i < n; ++i) {
			h[i] = sc.nextInt();
			h2[i] = (double)t - ((double)h[i] * 0.006);
			h2[i] = Math.abs((double)a-h2[i]);
		}
		
		int min = 0;
		for(int i = 0; i < n; ++i) {
			if(h2[min] > h2[i]) {
				min = i;
			}
		}
		
		System.out.println(min + 1);
		sc.close();
	}

}


Measured memory_bytes: 24020
s = gets.chomp
a = s[0, 2].to_i
b = s[2, 4].to_i
if a > 0 and a < 13 and b > 0 and b < 13 then
  puts "AMBIGUOUS"
elsif a > 0 and a < 13
  puts "MMYY"
elsif b > 0 and b < 13
  puts "YYMM"
else
  puts "NA"
end

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

// {U}{INT,LONG,LLONG}_{MAX,MIN}
#define ALPHABET    (26)
#define INF         INT_MAX
#define MOD         (1000000007LL)

using LL  = long long;
// }}}

int main() {
    std::ios::sync_with_stdio(false);
    int x, a, b;cin >> x >> a >> b;
    char ans = 'A';
    if(abs(x-b) < abs(x-a)) ans = 'B';
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

const ll P = 1000000007;
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

int main()
{
  cout << fixed << setprecision(10);
  string A,B,C;
  cin >> A >> B >> C;
  int a = A.size();
  int b = B.size();
  int c = C.size();
  rep(i,a/2)
  {
    swap(A[i],A[a-i-1]);
  }
  rep(i,b/2)
  {
    swap(B[i],B[b-i-1]);
  }
  rep(i,c/2)
  {
    swap(C[i],C[c-i-1]);
  }
  char next = 'a';
  rep(i,a+b+c)
  {
    if(next=='a')
    {
      int aa = A.size();
      if(aa==0)
      {
        cout << "A" << endl;
        return 0;
      }
      if(A[aa-1]=='a') next = 'a';
      else if(A[aa-1]=='b') next = 'b';
      else if(A[aa-1]=='c') next = 'c';
      A.pop_back();
    }
    else if(next=='b')
    {
      int bb = B.size();
      if(bb==0)
      {
        cout << "B" << endl;
        return 0;
      }
      if(B[bb-1]=='a') next = 'a';
      else if(B[bb-1]=='b') next = 'b';
      else if(B[bb-1]=='c') next = 'c';
      B.pop_back();
    }
    else if(next=='c')
    {
      int cc = C.size();
      if(cc==0)
      {
        cout << "C" << endl;
        return 0;
      }
      if(C[cc-1]=='a') next = 'a';
      else if(C[cc-1]=='b') next = 'b';
      else if(C[cc-1]=='c') next = 'c';
      C.pop_back();
    }
  }
  return 0;
}


Measured memory_bytes: 3624
# modなしの場合は，mod=1にする

class Combination:
    def __init__(self, size, mod=10**9 + 7):
        self.size = size + 2
        self.mod = mod
        self.fact = [1, 1] + [0] * size
        self.factInv = [1, 1] + [0] * size
        self.inv = [0, 1] + [0] * size

        for i in range(2, self.size):
            self.fact[i] = self.fact[i - 1] * i % self.mod
            self.inv[i] = -self.inv[self.mod % i] * (self.mod // i) % self.mod
            self.factInv[i] = self.factInv[i - 1] * self.inv[i] % self.mod

    def npr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * self.factInv[n - r] % self.mod

    def ncr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % self.mod) % self.mod

    def nhr(self, n, r):  # 重複組合せ
        return self.ncr(n + r - 1, n - 1)

    def factN(self, n):
        if n < 0:
            return 0
        return self.fact[n]

N, A, B = map(int, input().split())
MOD = 10**9 + 7

def calc(k):
    ret = 1
    for i in range(k):
        ret = ret * (N - i) * pow(i + 1, MOD - 2, MOD) % MOD
    return ret

ans = pow(2, N, MOD) - 1 - calc(A) - calc(B)
print(ans % MOD)

Measured memory_bytes: 3064
n, m = gets.split(' ')
n = n.to_i
m = m.to_i

f = 1
s, g = gets.split(' ')
s = s.to_i
g = g.to_i
(m-1).times do
    l, r = gets.split(' ')
    l = l.to_i
    r = r.to_i
    if g < l || r < s then
        f = 0
        break
    elsif l <= s then
        if r <= g then
            g = r
        end
    elsif s <= l then
        if r <= g then
            s = l
            g = r
        else
            s = l
        end
    end
end

if f == 0 then
    print 0
else
    print g-s+1
end


Measured memory_bytes: 1788
n,m = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(m)]
from collections import deque
e = [list() for _ in range(n)]
for i in range(m):
    e[ab[i][0]-1] += [ab[i][1]-1]
    e[ab[i][1]-1] += [ab[i][0]-1]

stack = deque()
visited = [0]*n

def dfs(x):
    num = 0
    stack.append(x)
    while stack:
        v = stack.pop()
        for k in e[v]:
            if visited[k]==0:
                num +=1
                visited[k]=1
                stack.append(k)
    return num


li = []

for i in range(n):
    li += [dfs(i)]

ans = max(li)
if ans ==0:
    print(1)
else:
    print(ans)

Measured memory_bytes: 140536
input = raw_input

from collections import Counter
n = int(input())
As = map(int, input().split(" "))

Bs = [i-A for i, A in enumerate(As)]

c = Counter(Bs)
out = 0
for i in range(n):
    key = As[i] + i 
    cur_out = c[key]
    out += cur_out

print(out)

Measured memory_bytes: 131664
#include <iostream>
#define rep(i, n) for (int i= 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main()
{
    int n, j = 0, k = 0;
    int s_1 = 0, s_2 = 0;
    cin >> n;
    int w[n];
    rep(i, n) cin >> w[i];
    while(j + k != n){
        if(s_1 <= s_2){
            s_1 += w[j];
            j ++;
        }
        else{
            s_2 += w[n-k-1];
            k++;
        }
    }
    if(s_1 > s_2) cout << s_1 - s_2 << endl;
    else cout << s_2 - s_1 << endl;
    return 0;
}


Measured memory_bytes: 256
// Package main provides
//
// File:  a.go
// Author: ymiyamoto
//
// Created on Fri Dec 28 23:08:46 2018
//
package main

import (
	"fmt"
	"sort"
)

func min(slice ...int) int {
	sort.Ints(slice)
	return slice[0]
}

func main() {
	var A, B, C, D int
	fmt.Scan(&A, &B, &C, &D)

	fmt.Println(min(A, B) + min(C, D))
}


Measured memory_bytes: 512
import java.lang.Math.abs
import java.lang.Math.min


fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    val h = readLine()!!.split(" ").map { it.toInt() }
    val dp = DP()
    println(dp.jump(h, 0))

}

class DP {
    val memo = mutableMapOf<Int, Int>()

    fun jump(h: List<Int>, now: Int): Int {
        val nr = memo[now]
        if (nr is Int) return nr

        if (now == h.size - 2) {
            val r = abs(h[now] - h[now + 1])
            memo[now] = r
            return r
        }
        if (now == h.size - 3) {
            val r = min(abs(h[now] - h[now + 1]) + abs(h[now + 1] - h[now + 2]), abs(h[now] - h[now + 2]))
            memo[now] = r
            return r
        }
        val r = min(abs(h[now] - h[now + 1]) + jump(h, now + 1), abs(h[now] - h[now + 2]) + jump(h, now + 2))
        memo[now] = r
        return r
    }
}


Measured memory_bytes: 85852
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int A = scanner.nextInt();
        int B = scanner.nextInt();

        if(A >= 13){
            System.out.println(B);
        }else if(6 <= A){
            System.out.println(B/2);
        }else{
            System.out.println(0);
        }
    }
}


Measured memory_bytes: 21716
n,w = list(map(int,input().strip().split()))

a = [[0] * 2 for i in [0] * n]

for i in range(n):
  a[i] = list(map(int,input().strip().split()))

weight_max = 0
for i in range(n):
  weight_max += a[i][0]

dp = [[0] * (w+1) for i in [0] * (n+1)]

for i in range(n):
  for sum_w in range(w+1):
    if sum_w >= a[i][0]:
      dp[i+1][sum_w] = max(dp[i][sum_w], dp[i][sum_w-a[i][0]]+a[i][1])
    dp[i+1][sum_w] = max(dp[i+1][sum_w],dp[i][sum_w])
print(dp[n][w])

Measured memory_bytes: 120940
#include<bits/stdc++.h>
#define Mx 1000000000005 
#define ll long long  
#define pi 3.141592653
#define vi vector<ll>
#define Mo 1000000007
#define mp make_pair
#define M map<ll,ll>
#define fr(i,a,b) for(ll i=a;i<b;i++)
#define vii vector<pair<ll,ll> > 
#define fi first
#define se second
#define pb push_back
#define ld long double
#define fill(a,x) memset(a,x,sizeof(a))
using namespace std;
bool comp(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
  return a.second<b.second;
}
/*vi b=v;
   sort(b.begin(), b.end());
  b.resize(unique(b.begin(), b.end()) - b.begin());
  ll t; cin>>t; fr(f,0,t){ 
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
 */
int main()
{
  ll n; cin>>n;
  ll a[n];
  fr(i,0,n)
    cin>>a[i];

  ll dp[n+1]={0};
  dp[2]=abs(a[1]-a[0]);

  fr(i,3,n+1)
  {
    dp[i]=min(dp[i-1]+abs(a[i-1]-a[i-2]), dp[i-2]+abs(a[i-1]-a[i-3]));
  }

  cout<<dp[n];
}

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

int main() {
#define rrep(i, n) for(int i = 1; i <= (int)(n); i++)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
  int N,M;
  cin>>N>>M;
  vector<int> a(M);
  rep(i,M){
    cin>>a[i];
    N-=a[i];
  }
  if(N<0)
    cout<<-1;
  else
    cout<<N;
}

Measured memory_bytes: 3640
#include<map>
#include<iostream>
#include<deque>
#include<algorithm>
#include<string>
#include<cctype>
#include<iomanip>
#include<vector>
#include<queue>
#include<math.h>
#include<bitset>
#include<random>
 
using namespace std;
#define REP(i,b,e) for(ll i=(ll)b;i<(ll)e;i++)
#define rep0(i,n) REP(i,0ll,n)
#define rep1(i,n) REP(i,1ll,n+1)
 
#define shosu setprecision(17)
 
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<char,P> Q;
//ll longinf=1ll<<60;
int inf=1<<29;
ll longinf=(1ll<<60);

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

signed main(){
    ll n,k;
    cin>>n>>k;
    ll h[n];
    rep0(i,n) cin>>h[i];
    ll a=max(n,k);
    ll b=min(n,k);
    ll dp[a];
    fill(dp,dp+a,0ll);
    dp[0]=0;
    for(int i=1;i<b;i++){
        ll tmp=longinf;
        for(int j=0;j<i;j++){
            tmp=min(tmp,dp[j]+abs(h[i]-h[j]));
        }
        dp[i]=tmp;
    }
    if(n>k){
        for(int i=k;i<n;i++){
            ll tmp=longinf;
            for(int j=i-k;j<i;j++){
                tmp=min(tmp,dp[j]+abs(h[i]-h[j]));
            }
            dp[i]=tmp;
        }
    }
    cout<<dp[n-1]<<endl;

    return 0;
}


Measured memory_bytes: 1792
#include <cstdio>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

std::string s, t;
bool f = false;
std::vector<std::string> v;

int main(){
	std::cin >> s >> t;
	for(int i=0; i<=(int)(s.size()-t.size()); ++i){
		bool b = true;
		for(int j=0; j<t.size(); ++j)
			if(s[s.size()-i-j-1] != '?' && s[s.size()-i-j-1] != t[t.size()-j-1])
				b = false;
		if(b){
			std::string ss = s;
			for(int j=0; j<ss.size()-t.size()-i; ++j)
				if(ss[j] == '?')
					ss[j] = 'a';
			for(int j=0; j<t.size(); ++j)
				ss[ss.size()-t.size()-i+j] = t[j];
			for(int j=ss.size()-i; j<ss.size(); ++j)
				if(ss[j] == '?')
					ss[j] = 'a';
			v.push_back(ss);
			f = true;
		}
	}
	if(!f) std::cout << "UNRESTORABLE" << std::endl;
	else{
		std::sort(v.begin(), v.end());
		std::cout << v[0] << std::endl;
	}
}

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

using namespace std;

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

    int n, k, arr[2000];
    cin >> n >> k;
    for (int i = 0; i < n; i++)
        cin >> arr[i];

    int cnt1 = 0;
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
            cnt1 = (cnt1 + (arr[i] > arr[j])) % MOD;

    int cnt2 = 0, a[4000];
    for (int i = 0; i < n; i++)
        a[i] = arr[i];
    for (int i = n; i < 2 * n; i++)
        a[i] = arr[i % n];
    for (int i = 0; i < n; i++)
        for (int j = n; j < 2 * n; j++)
            cnt2 = (cnt2 + (a[i] > a[j])) % MOD;

    cout << ((cnt1 * k % MOD) + ((k - 1) * k / 2 % MOD * cnt2)) % MOD << '\n';

    return 0;
}


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

int main() {
    int N,K,X,Y,ans;
    cin >> N >> K >> X >> Y;
    if(N > K){
        ans = K*X + (N-K)*Y;
    }
    else ans = N*X;

    cout << ans << endl;    
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<iomanip>
#define _USE_MATH_DEFINES
#include <math.h>
#include <functional>
#include<complex>
using namespace std;

#define rep(i,x) for(ll i=0;i<x;i++)
#define repn(i,x) for(ll i=1;i<=x;i++)

typedef long long ll;
const ll INF = 1e18+1e18;
const ll MOD = 1000000007;
const ll MAX = 4000001;
const long double eps = 1E-14;

ll max(ll a, ll b) {
	if (a > b) { return a; }
	return b;
}

ll min(ll a, ll b) {
	if (a > b) { return b; }
	return a;
}

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

ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}

struct edge {
	ll ind;
	ll fr;
	ll to;
	ll d;
};

class mint {
	long long x;
public:
	mint(long long x = 0) : x((x% MOD + MOD) % MOD) {}
	mint operator-() const {
		return mint(-x);
	}
	mint& operator+=(const mint& a) {
		if ((x += a.x) >= MOD) x -= MOD;
		return *this;
	}
	mint& operator-=(const mint& a) {
		if ((x += MOD - a.x) >= MOD) x -= MOD;
		return *this;
	}
	mint& operator*=(const  mint& a) {
		(x *= a.x) %= MOD;
		return *this;
	}
	mint operator+(const mint& a) const {
		mint res(*this);
		return res += a;
	}
	mint operator-(const mint& a) const {
		mint res(*this);
		return res -= a;
	}
	mint operator*(const mint& a) const {
		mint res(*this);
		return res *= a;
	}

	mint pow(ll t) const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}

	// for prime MOD
	mint inv() const {
		return pow(MOD - 2);
	}
	mint& operator/=(const mint& a) {
		return (*this) *= a.inv();
	}
	mint operator/(const mint& a) const {
		mint res(*this);
		return res /= a;
	}

	friend ostream& operator<<(ostream& os, const mint& m) {
		os << m.x;
		return os;
	}
};

mint pw(mint a, ll b) {
	if (b == 0) { return 1; }
	mint ret = pw(a, b >> 1);
	ret *= ret;
	if (b & 1) { ret *= a; }
	return ret;
}

typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;

typedef vector<mint> vmint;
typedef vector<vector<mint>> vvmint;
typedef vector<vector<vector<mint>>> vvvmint;

//////////////////////////////////////

int main() {
	ll N;
	cin >> N;
	map<ll, ll> mp;

	repn(i, N) {
		ll a;
		cin >> a;
		if (mp.count(a)) { cout << "NO" << endl; return 0; }
		mp[a] = 1;
	}

	cout << "YES" << endl;
}

Measured memory_bytes: 16096
/**
 *      purpose : 
 *      author  : kyomukyomupurin
 *      created : 
**/

// input/output
#include <iostream>
#include <fstream>
#include <sstream>
// container class
#include <array>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// math, algorithm
#include <algorithm>
#include <cmath>
#include <complex>
#include <numeric>
// etc
#include <bitset>
#include <cctype>
#include <chrono>
#include <iomanip>
#include <random>
#include <utility>
// using-directive
using namespace std;
// alias template
using int64 = int64_t;
using pii = pair<int, int>;
using pll = pair<int64_t, int64_t>;
// text macro replacement
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define print(x) cout << (x) << '\n'
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define fi first
#define se second

vector<string> split(const string &str, char sep) {
  vector<string> v;
  auto beg = str.begin();
  while(beg != str.end()) {
    auto end = beg;
    while(end != str.end() && *end != sep) ++end;
    v.push_back(string(beg, end));
    if(end != str.end()) ++end;
    beg = end;
  }
  return v;
}

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  
  string s; cin >> s;

  int ans = 1e8;

  for (char c = 'a'; c <= 'z'; ++c){
    string t = s;
    vector<string> v = split(t, c);
    int m = -1;
    for (int i = 0; i < (int) v.size(); ++i){
      m = max(m, (int) v[i].size());
    }
    ans = min(ans, m);
  }
  print(ans);
  
  return 0;
}

Measured memory_bytes: 256
program CADDiB2018D
    implicit none
    integer(8)N,C,i
    integer(8),allocatable::A(:)
    read*,N
    allocate(A(N))
    read*,A
    C=0
    do i=1,N
        if(mod(A(i),2)==1)then
            C=1
            exit
        end if
    end do
    if(C==0)print'(A)',"second"
    if(C==1)print'(A)',"first"
end program CADDiB2018D

Measured memory_bytes: 3808
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstring>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;

#define rep(i,n) for(int i=0; i<n; i++)
typedef long long ll;
typedef pair<int,int> pii;

int main() {
	ll N;
	while(cin >> N, N) {
		ll cnt = (N==1 ? 0 : 1);
		for(int i=2; i*i<=N; i++) {
			if(N%i == 0) {
				cnt += i + (N/i);
				if(i == N/i) cnt -= i;
			}
		}
		if(cnt == N) {
			cout << "perfect number" << endl;
		}
		else if(cnt < N) {
			cout << "deficient number" << endl;
		}
		else {
			cout << "abundant number" << endl;
		}
	}
	return 0;
}

Measured memory_bytes: 1156
#include <iostream>

using namespace std;

int linearSearch(int S[], int n, int key){
	int i = 0;
	S[n] = key;
	while( S[i] != key ) i++;
	return i != n;
}

int main(){
	int n,q,key,cnt = 0;
	int S[10001];
	
	cin >> n;
	for(int i = 0; i < n; i++) cin >> S[i];
	cin >> q;
	for(int i = 0; i < q; i++){
		cin >> key;
		if ( linearSearch(S,n,key) ) cnt++;
	}
	cout << cnt << endl;
	
	return 0;
}


Measured memory_bytes: 3120
import sys
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
 
n = ni()
a = [int(ns()) for i in range(n)]
pushed = {}

next_b = 1
count = 0
for _ in range(0,n):
  if next_b == 2:
    break
  count += 1
  next_b = a[next_b-1]
  if pushed.get(next_b) != None:
    count = -1
    break
  pushed[next_b] = True
  
if count == n:
  count = -1
print(count)
  

Measured memory_bytes: 53596
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>
#include <numeric>
#include <bitset>
#include <fstream>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a) { cout << #a << " = " << a << endl; }
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }

using namespace std;

// テ・ツ渉づィツ??
// http://d.hatena.ne.jp/kkishi/20090707/1246930309

bool forbid_0[10];
int coef[10];
int ASize;
bool used[10];
int sum;

int dfs(int idx) {
  int ret = 0;
  if(idx == ASize) return sum == 0;

  rep(digit, 10) {
    if(digit == 0 && forbid_0[idx]) continue;
    if(used[digit]) continue;
    used[digit] = 1, sum += coef[idx] * digit;
    ret += dfs(idx + 1);
    used[digit] = 0, sum -= coef[idx] * digit;
  }
  return ret;
}

int main() {
  int N;
  while(cin >> N && N) {
    vector<string> v(N);

    string alphs;
    rep(i, N) {
      cin >> v[i];
      alphs += v[i];
    }

    // ACM, IBM, ICPC => {A,B,C,I,M,P} テヲツ鳴?・ツュツ療・ツ按預lphsテ」ツ?ィテ」ツ?療」ツ?ヲテゥツ??」ツ?ォテ、ツクツヲテ」ツ?ケテ」ツつ?
    sort(all(alphs)); alphs.erase(unique(all(alphs)), alphs.end());

    /*
      テ・ツ青?ヲツ鳴?・ツュツ療」ツ?ョテ、ツソツづヲツ閉ーテ」ツつ津・ツ?暗」ツ?ォティツィツ暗ァツョツ療」ツ?凖」ツつ?
      ACM + IBM = ICPC テ」ツつ暗」ツつ甘」ツ??
      100*A+10*C+M + 100*I+10*B+M = 1000*I+101*C+10*P
      100*A +10*B -91*C -900*I +2*M -10*P = 0
      テ」ツ?禿」ツつ古」ツつ津ヲツコツ?」ツ?淌」ツ?凖」ツつ暗」ツ??」ツ?ェA, B, C, I, M, Pテ」ツ?ョテ・ツ?、テ」ツつ津ヲツアツづ」ツつ?」ツつ?
      */

    // alphsテ、ツクツ甘」ツ?ョテ、ツスツ催ァツスツョテ」ツつ津ァツ可ケテ・ツョツ堙」ツ?凖」ツつ?
    vector<vector<int>> pos(N);
    rep(i, N) rep(j, v[i].size()) pos[i].push_back(alphs.find(v[i][j]));

    rep(i, 10) coef[i] = 0;

    // テ・ツキツヲティツセツコテ」ツつ津ィツカツウテ」ツ??
    rep(i, N-1) rep(j, pos[i].size()) coef[pos[i][j]] += pow(10, (int)pos[i].size()-1-j);

    // テ・ツ渉ウティツセツコテ」ツつ津・ツシツ陛」ツ??
    rep(i, pos.back().size()) coef[pos.back()[i]] -= pow(10, (int)pos.back().size()-1-i);

    // 0テ」ツつ津・ツ渉姪」ツつ古」ツ?ェテ」ツ??」ツつ「テ」ツδォテ」ツδ陛」ツつ。テ」ツδ凖」ツδε」ツδ暗」ツつ津ァツ可ケテ・ツョツ堙」ツ?凖」ツつ?
    rep(i, 10) forbid_0[i] = 0;
    rep(i, N) if(pos[i].size() > 1) forbid_0[pos[i][0]] = 1;

    ASize = alphs.size();

    rep(i, 10) used[i] = 0;
    sum = 0;
    cout << dfs(0) << endl;

  }

  return 0;
}

Measured memory_bytes: 3336
a,b,k=map(int,input().split())

count=[]
if a<=b:
  for i in range(a,0,-1):
    if a%i==0 and b%i==0:
      count.append(i)
if a>b:
  for j in range(b,0,-1):
    if a%j==0 and b%j==0:
      count.append(j)
print(count[k-1])

Measured memory_bytes: 3064
#include <cstdio>
#include <algorithm>
using namespace std;
int main() {
	int N, M;
	scanf("%d %d", &N, &M);
	int A[N], tot = 0;
	for (int i = 0; i < N; ++i) scanf("%d", &A[i]), tot += A[i];
	sort(A, A + N);
	for (int i = 1; i <= M; ++i) {
		if (A[N - i] * 4LL * M < tot) {
			printf("No");
			return 0;
		}
	}
	printf("Yes");
	return 0;
}

Measured memory_bytes: 128
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	a := getNextInt(scanner)
	b := getNextInt(scanner)
	hh := make([]int, n)
	for i := 0; i < n; i++ {
		hh[i] = getNextInt(scanner)
	}
	sort.Ints(hh)
	l := (hh[n-1]-1)/a + 1
	r := (hh[n-1]-1)/b + 2
	diff := a - b
	for l < r {
		m := (l + r) >> 1
		c := 0
		for i := n - 1; i >= 0 && hh[i] > m*b; i-- {
			c += (hh[i]-m*b-1)/diff + 1
		}
		if c > m {
			l = m + 1
			continue
		}
		r = m
	}
	fmt.Fprintln(writer, l)
	writer.Flush()
}


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

using namespace std;

typedef long long ll;

ll MX(ll &a,ll b){return a=max(a,b);}

int main(){
  
  ll n,m,t;
  cin>>n>>m>>t;
  ll dp[20][1000],ans[1000];
  memset(ans,-1,sizeof(ans));
  memset(dp,-1,sizeof(dp));
  dp[0][0]=ans[0]=0;
  for(ll i=0,x;i<n;i++)
    scanf("%lld",&x),MX(dp[0][x%t],x);
  ll bt=0;
  while((1<<bt)<=m){
    if((1<<bt)&m){
      ll tp[1000];
      for(ll i=0;i<t;i++)tp[i]=ans[i];
      for(ll i=0;i<t;i++)
	for(ll j=0;j<t;j++)
	  if(dp[bt][i]!=-1&&ans[j]!=-1)
	    MX(tp[(i+j)%t],dp[bt][i]+ans[j]);
      for(ll i=0;i<t;i++)ans[i]=tp[i];
    }
    for(ll i=0;i<t;i++)
      for(ll j=0;j<t;j++)
	if(dp[bt][i]!=-1&&dp[bt][j]!=-1)
	  MX(dp[bt+1][(i+j)%t],dp[bt][i]+dp[bt][j]);
    bt++;
  }
  for(ll i=t-1;i>0;i--)
    ans[i-1]=max(ans[i-1],ans[i]);
  ll q;  
  cin>>q;
  while(q--){
    ll a,b;
    scanf("%lld%lld",&a,&b);
    if(ans[a]!=-1&&ans[a]/t>=b)cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }
  return 0;
}

Measured memory_bytes: 3384
#include<bits/stdc++.h>
#define rush() int T;scanf("%d",&T);int kase=1;while(T--)
#define pb(x) push_back(x)
#define pr pair<int,int>
#define mem(a) memset(a,0,sizeof(a))
#define  fi first
#define  se second
using namespace std;
typedef long long ll;
const ll maxn=2e6+5;
const ll mod=1e9+7;
ll quickpow(ll x,ll y,ll mod){ll ans=1;while(y){if(y&1)ans=ans*x%mod;x=x*x%mod;y>>=1;}return ans;}

ll a[200005]={0},b[200005]={0};

int main()
{
    int A,B,k;
    cin>>A>>B>>k;
    int res=0;
    for(int i=1;i<=A;i++)
    {
        cin>>a[i];
        a[i]=a[i]+a[i-1];
    }
    for(int i=1;i<=B;i++)
    {
        cin>>b[i];
        b[i]=b[i]+b[i-1];
    }
    for(int i=0,j=B;i<=A;i++)
    {
        while(j&&a[i]+b[j]>k) j--;
        if(a[i]+b[j]<=k)
        res=max(res,i+j);
    }
    cout<<res<<endl;
    return 0;
}


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

int main() {
    int N; cin >> N;
    int ans; ans = 0;
  	int s = 1e5 + 1;
    for (int i = 0; i < N-1; i++) {
        int a; cin >> a;
      	ans += min(s, a);
      	s = a;
    }
  	cout << ans + s << endl;
}

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

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n,k;
    cin>>n>>k;
    ll sum=0;
    int i=0;
    while(n)
    {
        sum+=n%k*pow(10,i);
        n/=k;
        i++;

    }
    cout<<i<<'\n';










}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <climits>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>

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

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

    string s,t;
    cin>>s>>t;
    vector<vector<int>> idx(26);
    for(int i=0; i<s.length(); i++){
        idx[s[i]-'a'].push_back(i);
    }
    ll answer=0;
    int cur=-1;
    int cycle=0;
    for(int i=0; i<t.length() && answer!=-1; i++){
        if(idx[t[i]-'a'].empty()) answer=-1;
        else{
            auto it=upper_bound(idx[t[i]-'a'].begin(),idx[t[i]-'a'].end(),cur);
            if(it==idx[t[i]-'a'].end()){
                cycle+=1;
                cur=idx[t[i]-'a'][0];
            }
            else cur=*it;
        }
    }
    if(answer!=-1) answer=(ll)s.length()*(ll)cycle+(ll)(cur+1);
    cout<<answer;

    return 0;
}


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

int n, q;
int s[1000010];
string query1[1010];
int query2[1010];

int s_sorted[1000010];
set<int> nowleader;

bool judge(vector<int> *leader_score, int x, int mid)
{
  int remain = n, previous = 2e9;
  for(int i = 0; i < leader_score->size(); i++){
    remain -= (upper_bound(s_sorted, s_sorted + n, min(previous - 1, leader_score->at(i))) - s_sorted)
      - (lower_bound(s_sorted, s_sorted + n, leader_score->at(i) - mid) - s_sorted);
    previous = leader_score->at(i) - mid;
  }
  return remain <= x;
}

void solve(int x)
{
  vector<int> leader_score;
  for(set<int>::iterator p = nowleader.begin(); p != nowleader.end(); p++){
    leader_score.push_back(s[*p]);
  }
  sort(leader_score.begin(), leader_score.end(), greater<int>());
  leader_score.erase(unique(leader_score.begin(), leader_score.end()), leader_score.end());

  if(x == n){
    cout << 0 << endl;
    return;
  }
  if(leader_score.empty() || n - (upper_bound(s_sorted, s_sorted + n, leader_score[0]) - s_sorted) > x){
    cout << "NA" << endl;
    return;
  }

  int rs = 0, re = 1e9;

  while(rs < re){
    int mid = (rs + re) / 2;
    if(judge(&leader_score, x, mid)){
      re = mid;
    }
    else{
      rs = mid + 1;
    }
  }
  
  cout << rs << endl;
  return;
}

signed main()
{
  cin >> n >> q;
  for(int i = 1; i <= n; i++){
    cin >> s[i];
    s_sorted[i - 1] = s[i];
  }
  for(int i = 1; i <= q; i++){
    cin >> query1[i] >> query2[i];
  }

  sort(s_sorted, s_sorted + n);

  for(int i = 1; i <= q; i++){
    if(query1[i] == "ADD"){
      nowleader.insert(query2[i]);
    }
    else if(query1[i] == "REMOVE"){
      nowleader.erase(query2[i]);
    }
    else{
      solve(query2[i]);
    }
  }

  return 0;
}

Measured memory_bytes: 16928
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using System.Security.Cryptography;

namespace AtCoder
{
	public class ABC
	{
		static public void Main(string[] args)
		{
			Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
			Solve(args);
			Console.Out.Flush();
		}

		const int mod = 1000000007;  // 10^9+7
		static int Mod(long x, int m) { return (int)((x % m + m) % m); }
		static long Mod(long x, long m) { return (x % m + m) % m; }

		static public void Solve(string[] args)
		{
			//string s = Console.ReadLine();
			//int n = int.Parse(Console.ReadLine());
			//long x = long.Parse(Console.ReadLine());
			//int[] vs = Console.ReadLine().Split().Select(int.Parse).ToArray();
			//long[] vs = Console.ReadLine().Split().Select(long.Parse).ToArray();

			long[] vs = Console.ReadLine().Split().Select(long.Parse).ToArray();
			long a = vs[0];
			long b = vs[1];
			long c = vs[2];
			long d = vs[3];

			long ans = a * c;
			ans = Math.Max(ans, a * d);
			ans = Math.Max(ans, b * c);
			ans = Math.Max(ans, b * d);
			Console.WriteLine(ans);

		}
	}
}


Measured memory_bytes: 28232
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            int a = scanner.nextInt();
            String op = scanner.next();
            int b = scanner.nextInt();

            switch (op) {
                case "+":
                    System.out.println(a + b);
                    break;
                case "-":
                    System.out.println(a - b);
                    break;
                case "*":
                    System.out.println(a * b);
                    break;
                case "/":
                    System.out.println(a / b);
                    break;
                case "?":
                    return;
            }
        }
    }
}



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

#define H 100
#define W 100

int h, w;
char a[H][W];

int main() {
	cin >> h >> w;

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

	int flag1;
	int flag;
	do {
		flag1 = 0;
		for( int i = 0; i < h; i++ ) {
			int flag = 0;
			for( int j = 0; j < w; j++ ) {
				if( a[i][j] == '#' ) {
					flag = 1;
				}
			}
			if( flag == 0 ) {
				for( ; i < h; i++ ) {
					for( int j = 0; j < w; j++ ) {
						a[i][j] = a[i + 1][j];
					}
				}
				h--;
				flag1 = 1;
			}
		}
	} while( flag1 == 1 );

	do {
		flag1 = 0;
		for( int i = 0; i < w; i++ ) {
			int flag = 0;
			for( int j = 0; j < h; j++ ) {
				if( a[j][i] == '#' ) {
					flag = 1;
				}
			}
			if( flag == 0 ) {
				for( ; i < w; i++ ) {
					for( int j = 0; j < h; j++ ) {
						a[j][i] = a[j][i + 1];
					}
				}
				w--;
				flag1 = 1;
			}
		}
	} while( flag1 == 1 );

	for( int i = 0; i < h; i++ ) {
		for( int j = 0; j < w; j++ ) {
			cout << a[i][j];
		}
		cout << endl;
	}

	return 0;
}


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

#define MOD 1000000007
#define rep(i, n) for(ll i=0; i < (n); i++)
#define ALL(v) v.begin(),v.end()

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<int, int> P;


ll mod(ll a, ll mod){
  ll res = a%mod;
  if(res<0)res=res + mod;
  return res;
}

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

ll modinv(ll a, ll mod){
  return modpow(a, mod-2, mod);
}

bool is_prime(ll n){
  ll i = 2;
  if(n==1)return false;
  if(n==2)return true;
  bool res = true;
  while(i*i <n){
    if(n%i==0){
      res = false;
    }
    i = i+1;
  }

  //if(i==1)res = false;
  if(n%i==0)res=false;
  return res;
}


/******************************************
** A main function is start from here  ****
*******************************************/
int main(){
  
  long double A,B,C;
  ll a, b, c;
  cin >> a >> b >> c;
  A = sqrt(a*b);
  if((c - a- b)*(c-a-b)>4*a*b and c-a-b>0)cout <<"Yes";
  else cout << "No";
  
  return 0;
}


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

using namespace std;
using namespace atcoder;

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

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()

#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;

template<class T>
ostream &operator<<(ostream &os, const vector <T> &v) {
  os << "[";
  REP(i, SZ(v)) {
    if (i) os << ", ";
    os << v[i];
  }
  return os << "]";
}

template<class T, class U>
ostream &operator<<(ostream &os, const pair <T, U> &p) {
  return os << "(" << p.first << " " << p.second << ")";
}

template<class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template<class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;

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

  int t; cin >> t;
  while(t--) {
    ll n, m, a, b; cin >> n >> m >> a >> b;
    cout << floor_sum(n, m, a, b) << endl;
  }

  return 0;
}

Measured memory_bytes: 3620
# util functions

# debug
$debugFlag = ( ARGV[0] != nil )
p "==== DEBUG MODE ====" if $debugFlag

def pd(str)
  p str if $debugFlag
end

def printd(str)
  print str if $debugFlag
end

# input
inputs = []
lc = 0
while l = $stdin.gets do
  if false
    inputs << l.chomp.split(" ").map(&:to_s)
  else
    inputs << l.chomp.split(" ").map(&:to_i)
  end
  lc += 1
end

nn = inputs[0][0]

# input check
if $debugFlag
  p "--- INPUT (begin) ---"
  inputs.each do |l_i|
    p l_i
  end
  p "--- INPUT (end) ---"
  print "\n"
end

# body
if $debugFlag
  p nn
end

ketasu = nn.to_s.length

cnt = 0
if ketasu % 2 == 1
  cnt += nn - 10**(ketasu-1) + 1
  ketasu -= 2
else
  ketasu -= 1
end


while ketasu >= 1 
  cnt += 9*(10**(ketasu-1))
  ketasu -= 2
end

puts cnt


Measured memory_bytes: 1788
#include<iostream>
#include<vector>
using namespace std;
 
int main()
{
    const int max_r = 100, max_c = 100;
    int a[max_r][max_c] = { 0 };
    int r, c;
    int SUM_ = 0;
    //input
    cin >> r >> c;
    for (int y = 0; y < r; y++) {
        for (int x = 0; x < c; x++) {
            cin >> a[y][x];
            SUM_ += a[y][x];
        }
    }
 
    //output
    for (int i = 0; i < r; i++) {
        int sum_r = 0;
        for (int j = 0; j < c; j++) {
            cout << a[i][j] << " ";
            sum_r += a[i][j];
        }
        cout << sum_r << endl;
    }
    for (int i = 0; i < c; i++) {
        int sum_c = 0;
        for (int j = 0; j < r; j++) {
            sum_c += a[j][i];
        }
        cout << sum_c << " ";
    }
    cout << SUM_ << endl;
    return 0;
}

Measured memory_bytes: 3116
#include <bits/stdc++.h>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define all(x) (x).begin(),(x).end()
#define INF 10000000
using ll = long long ;

using namespace std;

int main(){
    int w,a,b;
    cin >> w >> a >> b;
    int start = max(a,b);
    int end = min(a+w,b+w);
    if ((start-end)>0) cout << (start-end)<< endl;
    else cout << 0 << endl;
    
}

Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main(){
	int H;
	cin >> H;
	while (H){
		int B[10][5] = { 0 }, score = 0;
		for (int i = 0; i < H; i++){
			cin >> B[i][0] >> B[i][1] >> B[i][2] >> B[i][3] >> B[i][4];
		}
		int flag = 1;
		while (flag){
			flag = 0;
			for (int i = 0; i < H; i++){
				int connection = 0, j;
				for (j = 1; j < 5; j++){
					if (B[i][j] && B[i][j - 1] == B[i][j]){
						connection++;
					} else{
						if (connection >= 2){
							break;
						}
						connection = 0;
					}
				}
				if (connection >= 2){
					score += B[i][j - 1] * (connection + 1);
					for (int k = j - 1; k >= j - connection - 1; k--){
						B[i][k] = 0;
					}
					flag = 1;
				}
			}
			for (int i = 0; i < 5; i++){
				for (int j = H - 1; j > 0; j--){
					for (int k = H - 1; k > 0; k--){
						if (!B[k][i]){
							B[k][i] = B[k - 1][i];
							B[k - 1][i] = 0;
						}
					}
				}
			}
		}
		cout << score << endl;
		cin >> H;
	}
	return 0;
}

Measured memory_bytes: 1160
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
	int a, b;
	char op;
	while (true) {
		cin >> a >> op >> b;
		if (op == '?') break;
		switch (op) {
		case '+':
			printf("%d", a + b);
			break;
		case '-':
			printf("%d", a - b);
			break;
		case '/':
			printf("%d", a / b);
			break;
		case '*':
			printf("%d", a * b);
			break;
		}
		printf("\n");
	}

	return 0;
}


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

int main()
{
    int n,x;
    int l;
    int ans=0;
    int i;
    cin >> n >> x;
    for(i=0;i<n;i++){
        cin >> l;
        ans+=l;
        if(ans>x){
            break;
        }
    }

    cout << i+1 << endl;
}

Measured memory_bytes: 256
import java.util.Scanner;

class Main {

	public static void main(String[] args) {
		new Main().exec();
	}

	void exec() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int food[] = new int[m];

		for (int i = 0; i < n; i++) {
			int k = sc.nextInt();
			for (int j = 0; j < k; j++) {
				int personalF = sc.nextInt();
				++food[personalF-1];
			}
		}
		int total = 0;
		for (int i = 0; i < m; i++) {
			if(food[i] == n) total++;
		}
		System.out.println(total);
		sc.close();
	}


}


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

int main()
{
    int n;
    cin >> n;
    vector<ll> a(n);
    for(auto& ai:a) cin >> ai;
    
    unordered_map<int, ll> mp;
    rep(i,n) {
        if(mp.count(a[i]) == 0) mp.insert({a[i], 1});
        else mp[a[i]] += 1;
    }

    ll sum = 0;
    for(auto& mpi:mp) {
        ll cnt = mpi.second;
        sum += cnt*(cnt-1)/2;
    }

    rep(i,n) {
        ll cnt = mp[a[i]];
        cout << sum - cnt*(cnt-1)/2 + (cnt-1)*(cnt-2)/2 << endl;
    }

    return 0;
}

Measured memory_bytes: 9160