akhauriyash/Code-Regression huggingface dataset:
object Main {
  def main(args: Array[String]): Unit = {
    val num     = scala.io.StdIn.readInt
    var factors = scala.io.StdIn.readLine.split( " " ).map{ _.toLong }

    val mod     = 1000000007
    var rate    = 0L
    var sum     = 0L

    ( 1 until num ).foreach{ i =>
      rate = ( rate + factors( i - 1 ) )%mod
      sum  = ( sum + rate*factors( i ) )%mod
    }
    println( sum )

  }
}

Measured memory_bytes: 84220
fun main(args: Array<String>) {
    var n = readLine()!!.toInt()
    var xs = readLine()!!.split(" ").map(String::toInt)
    var a1 = 0L
    var a2 = -1000000000000L
    for (i in 0 until n) {
        var t = Math.max(a1 + xs[i], a2 - xs[i])
        a2 = Math.max(a1 - xs[i], a2 + xs[i])
        a1 = t
    }
    println(a1)
}

Measured memory_bytes: 59816
input_int = lambda:int(input())
input_ints = lambda:map(int,input().split())
input_ints_list = lambda:list(input_ints())
input_str = lambda:input()
input_strs = lambda:input().split()
input_lines = lambda n,f:[f() for _ in range(n)]

import functools,fractions
import numpy as np
gcd = lambda a:functools.reduce(fractions.gcd,a) # 最大公約数（リスト）
lcm_base = lambda a,b:(a*b)//fractions.gcd(a,b) # 最小公倍数(2値)
lcm = lambda a:functools.reduce(lcm_base,a,1) # 最小公倍数(リスト)

init_dp = lambda v,n:[v]*(n+1)

import math
# 四捨五入はround
ceil = lambda a:math.ceil(a) # 切り上げ
floor = lambda a:math.floor(a) # 切り捨て

def solution():
    N,M = input_ints()
    As = input_ints_list()
    w = [0,2,5,5,4,5,6,3,7,6]

    dp = init_dp(-1,N)
    dp[0] = 0
    for i in range(N+1):
        for v in As:
            if i+w[v] < N+1:
                dp[i+w[v]] = max(dp[i+w[v]],dp[i]*10+v)
    print(dp[N])




if __name__ == '__main__':
    solution()

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

int main() {
    int N;
    int sum =0;
    cin >>N;
    vector<int> data(N);
    for (int i=0;i<N;i++){
        cin >> data.at(i);
        sum+=data.at(i);
    }
    sort(data.begin(),data.end());
    int sum2 = sum - data.at(N-1);
    if(data.at(N-1)<sum2)cout<< "Yes"<<endl;
    else cout << "No"<<endl;
}


Measured memory_bytes: 256
#include <cstdio>
#include <cmath>

class point{
    double x;
    double y;
public:
    point(double a, double b){x=a;y=b;}
    point(){x=0;y=0;}
    point operator+(point ob2);
    point operator-(point ob2);
    point operator/(double d);
    point rot(point p, double theta);
    void print(){printf("%f %f\n",x,y);}
};

point point::operator+(point ob2){
    point tmp;
    tmp.x = x + ob2.x;
    tmp.y = y + ob2.y;
    return tmp;
}

point point::operator-(point ob2){
    point tmp;
    tmp.x = x - ob2.x;
    tmp.y = y - ob2.y;
    return tmp;
}

point point::operator/(double d){
    point tmp;
    tmp.x = x / d;
    tmp.y = y / d;
    return tmp;
}

point point::rot(point p, double theta){
    point tmp;
    tmp.x = p.x + cos(theta) * (x - p.x) - sin(theta) * (y - p.y);
    tmp.y = p.y + sin(theta) * (x - p.x) + cos(theta) * (y - p.y);
    return tmp;
}

void recursion(double n, point p, point q){
    if(n==0)return;

    point r, s, t;

    r = p + (q-p)/3;
    t = q + (p-q)/3;
    s = t.rot(r,M_PI/3);

    recursion(n-1,p,r);
    r.print();
    recursion(n-1,r,s);
    s.print();
    recursion(n-1,s,t);
    t.print();
    recursion(n-1,t,q);

    return;
}

int main(void){
    int n;
    scanf("%d",&n);
    point p(0,0), q(100,0);
    p.print();
    recursion(n, p, q);
    q.print();
    return 0;
}



Measured memory_bytes: 2804
#include <iostream>
#include <vector>
#include <array>
#include <set>
#include <queue>


using namespace std;

const int N = 3;

struct Puzzle {
    array<array<int, N>, N> board;
    array<int, 2> empty;
    int turn;

    bool operator < (const Puzzle &p) const {
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < N; ++j) {
                if (board[i][j] != p.board[i][j])
                    return board[i][j] < p.board[i][j];
            }
        }
        return false;
    }
};


vector<array<int, 2>> dir = {
    {-1, 0}, // left
    {0, -1}, // up
    {1, 0},  // right
    {0, 1},  // down
};

bool solved(const Puzzle& p) {
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            int x = i == 2 && j == 2 ? 0 : i * 3 + j + 1;
            if (p.board[i][j] != x) {
                return false;
            }
        }
    }
    return true;
}

int main() {
    Puzzle start;
    start.turn = 0;
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            int x;
            cin >> x;
            if (x == 0) {
                start.empty = {i, j};
            }
            start.board[i][j] = x;
        }
    }

    Puzzle ans;
    set<Puzzle> visited;
    queue<Puzzle> Q;
    Q.push(start);
    while (!Q.empty()) {
        auto u = Q.front();
        Q.pop();
        if (solved(u)) {
            ans = u;
            break;
        }
        for (auto& d : dir) {
            auto tx = u.empty[0] + d[0];
            auto ty = u.empty[1] + d[1];
            if (tx < 0 || ty < 0 || tx >= N || ty >= N) {
                continue;
            }
            auto v = u;
            swap(v.board[v.empty[0]][v.empty[1]], v.board[tx][ty]);
            v.empty = {tx, ty};
            if (visited.find(v) == visited.end()) {
                ++v.turn;
                visited.insert(v);
                Q.push(v);
            }
        }
    }
    cout << ans.turn << endl;

    return 0;
}


Measured memory_bytes: 20548
#include <stdio.h>

int main(void) {
  char s[50];
  int count[26] = {0}, min[26];
  int n, i, j, k;
  
  for (i = 0; i < 26; i++) {
    min[i] = 51;
  }
  
  scanf("%d", &n);
  
  for (i = 0; i < n; i++) {
    scanf("%s", s);
    for (j = 0; j < strlen(s); j++) {
      for (k = 0; k < 26; k++) {
        if (s[j] == 'a'+ k){
          count[k]++;
        }
      }
    }
    for (j = 0; j < 26; j++) {
      if (count[j] < min[j]) {
        min[j] = count[j];
      }
      count[j] = 0;
    }
  }
    
  for (i = 0; i < 26; i++) {
    if (min[i] != 0 && min[i] != 51) {
      for (j = 0; j < min[i]; j++) {
        printf("%c",'a'+i);
      }
    }
  }
  printf("\n");
  
  return 0;
}


Measured memory_bytes: 128
void main() {
	auto ip = readAs!(long[]), N = ip[0], M = ip[1];
	alias Pair = Tuple!(long, "cost", long, "num");
	Pair[] pairs;
	ulong res;
	foreach(i; 0..N) {
		auto ip2 = readAs!(long[]), A = ip2[0], B = ip2[1];
		pairs ~= Pair(A, B);
	}
	pairs.sort!"a.cost < b.cost"();
	ulong k = 0;
	while(k < M) {
		auto p = pairs.front;
		pairs.popFront;
		if(p.num >= M - k) {
			res += p.cost * (M - k);
			break;
		} else res += p.cost * p.num;
		k += p.num;
	}
	res.writeln;
}

// ===================================

import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;

T readAs(T)() if (isBasicType!T) {
	return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
	return readln.split.to!T;
}

T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
	auto res = new T[][](height, width);
	foreach(i; 0..height) {
		res[i] = readAs!(T[]);
	}
	return res;
}

T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
	auto res = new T[][](height, width);
	foreach(i; 0..height) {
		auto s = rs;
		foreach(j; 0..width) res[i][j] = s[j].to!T;
	}
	return res;
}

int ri() {
	return readAs!int;
}

double rd() {
	return readAs!double;
}

string rs() {
	return readln.chomp;
}


Measured memory_bytes: 4860
def main():
    import sys
    input = sys.stdin.buffer.readline
    N, u, v = (int(i) for i in input().split())
    G = [[] for _ in range(N)]
    for i in range(N-1):
        a, b = (int(i) for i in input().split())
        G[a-1].append(b-1)
        G[b-1].append(a-1)

    d_from_u = [-1] * N
    d_from_v = [-1] * N

    def dfs(v, d):
        todo = []
        d[v] = 0
        todo.append(v)
        while todo:
            u = todo.pop()
            for next_v in G[u]:
                if 0 <= d[next_v]:
                    continue
                d[next_v] = d[u] + 1
                todo.append(next_v)

    u, v = u-1, v-1
    dfs(u, d_from_u)
    dfs(v, d_from_v)
    ans = 0
    for du, dv in zip(d_from_u, d_from_v):
        if du < dv and ans < dv - 1:
            ans = dv - 1
    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 59244
#include <bits/stdc++.h>
#define int long long
#define f first
#define s second
using namespace std;
int32_t main(){
    if(fopen("input.txt", "r"))
    freopen("input.txt", "r", stdin),
    freopen("output.txt", "w", stdout);
    ios::sync_with_stdio(0),cin.tie(0);
    int n,x,y;
    cin >> n >> x >> y;
    x--,y--;
    vector <int> g[n];
    for(int i=1;i<n;i++){
        g[i].push_back(i-1);
        g[i-1].push_back(i);
    }
    g[x].push_back(y);
    g[y].push_back(x);
    int a[n]={0};
    for(int i=0;i<n;i++){
        queue <int> q;
        q.push(i);
        int l[n];
        memset(l,-1,sizeof l);
        l[i]=0;
        while(!q.empty()){
            int v=q.front();
            q.pop();
            for(auto u:g[v]){
                if(l[u]!=-1)continue;
                l[u]=l[v]+1;
                q.push(u);
            }
        }
        for(int i=0;i<n;i++)
            a[l[i]]++;
    }
    for(int i=1;i<n;i++)
        cout << a[i]/2 << endl;
}

Measured memory_bytes: 384
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll,ll> pll;
const ll mod=1e9+7;
//const ll mod=998244353;
const ll inf=5e18;

int main() {
  ll h,w,a,b;
  cin >> h >> w >> a >> b;
  mat ans(h,vec(w));
  for(ll i=0;i<h;i++) {
    for(ll j=0;j<w;j++) {
      if((i<b&&j<a)||(i>=b&&j>=a)) {
        ans[i][j]=0;
      }
      else {
        ans[i][j]=1;
      }
    }
  }
  for(ll i=0;i<h;i++) {
    for(ll j=0;j<w;j++) {
      cout << ans[i][j];
    }
    cout << endl;
  }
}

Measured memory_bytes: 9088
#include"bits/stdc++.h"
using namespace std;

int main() {
	int H, W;
	cin >> H >> W;
	vector<string> a(H);
	for (int i = 0;i < H;++i) {
		cin >> a[i];
	}
	for (int i = 0;i < W + 2;++i) {
		putchar('#');
	}
	cout << endl;
	for (int i = 0;i < H;++i) {
		cout << '#' << a[i] << '#' << endl;
	}
	for (int i = 0;i < W + 2;++i) {
		putchar('#');
	}
	cout << endl;
	return 0;
}

Measured memory_bytes: 256
n,m = map(int,input().split())
ps = [list(input().split()) for _ in range(m)]
sub = [[0,0] for _ in range(n)]
ac = 0
wa = 0
for i in range(m):
  if ps[i][1] == "WA" and sub[int(ps[i][0])-1][0] == 0:
    sub[int(ps[i][0])-1][1] += 1
  else:
    sub[int(ps[i][0])-1][0] = 1
for j in range(n):
  if sub[j][0] == 1:
    ac += 1
    wa += sub[j][1]
print(ac,wa)

Measured memory_bytes: 80344
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int A[][] = new int[2][N];
		int point = 0;
		for (int i = 0; i < N; i++) {
			A[0][i] = sc.nextInt();
		}
		for (int i = 0; i < N; i++) {
			A[1][i] = sc.nextInt();
		}
		int score = 0;
		for (int i = 0; i < N; i++) {
			int temp = 0;
			for (int j = 0; j < N; j++) {
				if (i > j) {
					temp += A[0][j];
				} else if (i == j) {
					temp += A[0][j];
					temp += A[1][j];
				} else {
					temp += A[1][j];
				}
			}
			if (temp > score) {
				score = temp;
			}
		}
		System.out.println(score);
	}
}


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

int main() {
   string a, b, c;
   int ans;
   cin >> a;
   
   if (a == "111") { ans = 3; }
   if (a == "110") { ans = 2; }
   if (a == "101") { ans = 2; }
   if (a == "011") { ans = 2; }
   if (a == "100") { ans = 1; }
   if (a == "010") { ans = 1; }
   if (a == "001") { ans = 1; }
   if (a == "000") { ans = 0; }
   cout << ans << endl;
}

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

int GCD(int a, int b)
{
    if (b > a)
        swap(a, b);
    while (b!=0)
    {
        int r;
        r = a % b;
        a = b;
        b = r;
    }
    return a;
}

int main()
{
    int a, b;
    cin >> a >> b;
    cout << GCD(a, b) << endl;
}


Measured memory_bytes: 3084
#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>s;
			if(s=="snuke"){
				cout<<char(j-1+'A');
				cout<<i;
				return 0;
			}
		}
	}
	return 0;
}

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

using namespace std;

#define int long long
#define For(i, a, b) for(int i = a; i <= b; ++ i)
const int N = 5e5 + 5, mod = 1e9 + 7;

int l, q;
string s;
int _hash[N], Hash[N], Pow[N];
bool pre[N], suf[N];

int get_hash(int l, int r){
	return (_hash[r] - _hash[l - 1] * Pow[r - l + 1] + mod * mod ) % mod;
}
int get_hash2(int l, int r){
	return (Hash[l] - Hash[r + 1] * Pow[r - l + 1] + mod * mod ) % mod;
}
signed main(){
	ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);
	cin >> s;
	int len = s.length();
	s = "." + s;
	int ok = 0;
	For(i, 1, len) if(s[i] != s[1]){ ok = 1; break; }
	if(!ok){ cout << len << "\n" << 1; return 0; }
	Pow[0] = 1;
	For(i, 1, N - 1) Pow[i] = Pow[ i - 1] * 27 % mod;
	
	For(i, 1, len) _hash[i] = (_hash[i - 1] * 27 + s[i] - 'a' + 1) % mod;
	for(int i = len; i > 0; -- i) Hash[i] = (Hash[i + 1] * 27 + s[i] - 'a' + 1) % mod;
	
	For(i, 1, len){
		for(int j = i + 1; j + i - 1 <= len; j += i){
			if(_hash[i] != get_hash(j, j + i - 1)) break;
			pre[j + i - 1] = true;
		}
	}
	for(int i = len ; i > 0; -- i){
		int val = get_hash2(len - i + 1, len);
		for(int j = len - i; j - i + 1 > 0; j -= i){
			if(val != get_hash2(j - i + 1, j)) break;
			suf[j - i + 1] = true;
		}
	}
	if(!pre[len]){ cout << 1 << "\n" << 1; return 0; }
	int dem = 0;
	For(i, 1, len - 1) if(!pre[i] && !suf[i + 1]) dem ++;
	cout << 2 << "\n" << dem;
}

Measured memory_bytes: 13616
#include <cstdio>
#include <queue>
using namespace std;
static const int INFTY = 10001;
queue<int> lines[10];

int main() {
    int n, m, q, id, minidx, minnum;
    scanf("%d %d", &n, &m);
    for (int i = 0; i < m; i++) {
        scanf("%d %d", &q, &id);
        if (q == 1) {
            minnum = INFTY, minidx = -1;
            for (int j = 0; j < n; j++) {
                if (lines[j].size() < minnum) minnum = lines[j].size(), minidx = j;
            }
            lines[minidx].push(id);
        }
        else if (q == 0) {
            if (!lines[id-1].empty()) {
                printf("%d\n", lines[id-1].front()); lines[id-1].pop();
            }
        }
    }
    return 0;
}



Measured memory_bytes: 2748
import numpy as np

x, n = map(int, input().split())
num_lists = list(map(int, input().split()))

all_num = list(range(0, 102, 1))
lists = [i for i in all_num if i not in num_lists]

lists_arr = np.array(lists)
diff_arr = lists_arr - x
abs_arr = np.abs(diff_arr)
index = np.where(abs_arr == abs_arr.min())
answer_list = lists_arr[index]
print(answer_list[0])

Measured memory_bytes: 27228
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**9+7


Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

n = int(ri())
# n-=1

arr = []
while n > 0:
    temp = n%26
    arr.append((temp-1)%26)

    if temp == 0:
        n= (n-1)//26
    else:
        n = n//26

ans = []
for i in range(len(arr)):
    ans.append(chr(ord('a') + arr[i]))

ans  = ans[::-1]
print("".join(ans))


Measured memory_bytes: 63096
while True:
    H, W = [int(i) for i in input().split()]
    if H == W == 0:
        break
    
    for h in range(H):
        for w in range(W):
            if h == 0 or h == H-1 or w == 0 or w == W - 1:
                print('#', end='')
            else:
                print('.', end='')
        print()
    
    print()

Measured memory_bytes: 9028
#include <iostream>
#include <string>

using namespace std;

void  move(char t[100][100][2], int *x, int *y) {
  t[*y][*x][1] = 1;
  if (t[*y][*x][0] == '>')
    (*x)++;
  else if (t[*y][*x][0] == '<')
    (*x)--;
  else if (t[*y][*x][0] == '^')
    (*y)--;
  else if (t[*y][*x][0] == 'v')
    (*y)++;
}

int main(int argc, char *argv[]) {
  int h, w;
  char tile[100][100][2];
  while (true) {
    cin >> h >> w;
    if (h == 0 && w == 0)
      break;

    string tmp;
    // skip '\n'                                                                                                                                                                                            
    getline(cin, tmp);
    for (int i = 0; i < h; i++) {
      getline(cin, tmp);
      for (int j = 0; j < w; j++) {
        tile[i][j][0] = tmp[j];
        tile[i][j][1] = 0;
      }
    }
    /*                                                                                                                                                                                                      
    for (int i = 0; i < h; i++) {                                                                                                                                                                           
      for (int j = 0; j < w; j++)                                                                                                                                                                           
        cout << tile[i][j][0];                                                                                                                                                                              
      cout << endl;                                                                                                                                                                                         
    }                                                                                                                                                                                                       
    */
    int x = 0, y = 0;
    while (true) {
      move(tile, &x, &y);
      /*                                                                                                                                                                                                    
      cout << x << ' ' << y << endl;                                                                                                                                                                        
      */
      if (tile[y][x][0] == '.') {
        cout << x << ' ' << y << endl;
        break;
      } else if (tile[y][x][1] > 0) {
        cout << "LOOP" << endl;
        break;
      }
    }
  }
  return 0;
}

Measured memory_bytes: 1228
import sys

def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

def solve():
    s = [1 if hand == 'p' else 0 for hand in input()]
    n = len(s)
    t = [1 if i % 2 else 0 for i in range(n)]
    ans = 0

    for h1, h2 in zip(s, t):
        if h1 < h2:
            ans += 1
        elif h2 < h1:
            ans -= 1
        else:
            pass

    assert ans >= 0
    
    print(ans)

if __name__ == '__main__':
    solve()

Measured memory_bytes: 4760
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <functional>
#include <iomanip>
#include <bitset>
#include <numeric>
#include <queue>
#include <map>
#include <cstdint>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
const long long mod = 1000000007;
typedef long long int ll;
typedef pair<ll, ll> P;
const vector<int> di = { -1, 0, 1, 0 };
const vector<int> dj = { 0, 1, 0, -1 };


int main() {

	ll h, w;
	cin >> h >> w;

	ll ans = 0;

	if ((h * w) % 2 == 0) {
		ans = (h * w) / 2;
	}
	else {
		ans = (h * w) / 2 + 1;

	}
	if (h == 1 || w == 1) {
		ans = 1;
	}

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

Measured memory_bytes: 256
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = [0 for i in range(N + 1)]

for a in A:
    B[a] += 1
nums = [b for b in B if b > 0]
nums.sort()
n = len(nums)
print(0 if n<=K else sum(nums[:n - K]))


Measured memory_bytes: 114776
#include <iostream>
#include <algorithm>
#include <climits>
#include <unistd.h>
using namespace std;
int main(){
    int mcost = INT_MAX, T, n;
    cin >> n >> T;
    for (int i = 0; i < n; ++i){
        int x, y;
        scanf("%d %d", &x, &y);
        if (y <= T)
            mcost = min(mcost, x);
    }
    if (mcost == INT_MAX)
        cout << "TLE";
    else
        cout << mcost;

    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<deque>
#include<functional>
#include<iterator>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<utility>

using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define a first
#define b second
#define sz(x) (ll)((x).size())
#define pb push_back
#define mp make_pair
#define bg begin()
#define ed end()
#define all(x) (x).bg,(x).ed
#define rep(i,n) for(ll i=0;i<n;i++)
#define rrep(i,n) for(ll i=n-1;i>=0;i--)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define FOR(i,a,b) for(ll i=a;i<b;i++)
const ll MOD=1000000007;
const ll INF=1000000000000000;

int main(){
    ll N,M; cin>>N>>M;
    ll ans=0;
    priority_queue<P,vector<P>,greater<P> > que;
    priority_queue<ll,vector<ll> > caneat;
    vector<ll> A(N+1,0);
    vector<ll> B(N+1,0);
    rep1(i,N){
        cin>>A[i]>>B[i];
        que.push(mp(A[i],B[i]));
    }
    rep1(i,M){
        while(!que.empty()){
            if(que.top().a <= i){
                caneat.push(que.top().b);
                que.pop();
            }
            else break;
        }
        
        if(!caneat.empty()){
            ans+=caneat.top();
            caneat.pop();
        }
    }
    cout<<ans;
}


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

void compute(){
    int gy, gx, n, sx, sy, tx, ty;
    bool G[MAX+1][MAX+1][MAX+1][MAX+1];
    int T[MAX+1][MAX+1];
    rep(i,MAX+1) rep(j,MAX+1) rep(k,MAX+1) rep(m,MAX+1) G[i][j][k][m] = false;
    rep(i, MAX+1) rep(j, MAX+1) T[i][j] = 0;
    cin >> gx >> gy >> n;
    rep(i, n){
	cin >> sx >> sy >> tx >> ty;
	G[sy][sx][ty][tx] = true;
	G[ty][tx][sy][sx] = true;
    }
    T[0][0] = 1;
    rep(y, gy+1) rep(x, gx+1){
	if ( x < gx && !G[y][x][y][x+1]) T[y][x+1] += T[y][x];
	if ( y < gy && !G[y][x][y+1][x]) T[y+1][x] += T[y][x];
    }

    if ( T[gy][gx] == 0 ) cout << "Miserable Hokusai!" << endl;
    else cout << T[gy][gx] << endl;
}

main(){
    int tcase; cin >> tcase;
    for ( int i = 0; i < tcase; i++ ) compute();
}

Measured memory_bytes: 0
#include <stdio.h>
int main(void){
    long long n, x, i, max=-1000000, min=1000000, sum=0;
    
    scanf("%lld\n", &n);
    
    for(i=0; i<n; i++){
        scanf("%lld ",&x);
        
        sum = sum + x;
        
        if(max<=x){
            max = x;
        }
        
        if(min>=x){
            min = x;
        }
    }
    
    printf("%lld %lld %lld\n", min, max, sum);
    return 0;
}



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

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

int nextInt() { int x; scanf("%d", &x); return x;}
ll nextLong() { ll x; scanf("%lld", &x); return x;}

const int MAX_N = 212345;

int main2() {
  int H = nextInt();
  int W = nextInt();
  int N = nextInt();
  int sr = nextInt();
  int sc = nextInt();
  string S, T; cin >> S >> T;

  bool ans = true;
  int r1 = 1, r2 = H;
  int c1 = 1, c2 = W;
  for (int i = N - 1; i >= 0; i--) {
    if (i != N-1) {
      if (T[i] == 'L') c2++;
      if (T[i] == 'R') c1--;
      if (T[i] == 'U') r2++;
      if (T[i] == 'D') r1--;
      if (r1 < 1) r1 = 1;
      if (c1 < 1) c1 = 1;
      if (r2 > H) r2 = H;
      if (c2 > W) c2 = W;
    }
    if (S[i] == 'L') c1++;
    if (S[i] == 'R') c2--;
    if (S[i] == 'U') r1++;
    if (S[i] == 'D') r2--;
    if (r1 > r2 || c1 > c2) ans = false;
    // cout << r1 << " " << r2 << "  |  " << c1 << " " << c2 << endl;
  }
  if (r1 <= sr && sr <= r2 && c1 <= sc && sc <= c2) {

  } else {
    ans = false;
  }
  cout << (ans ? "YES":"NO") << endl;

  return 0;
}

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}


Measured memory_bytes: 776
#include<iostream>
#include<vector>
#include<limits.h>
#define ll long long
#define modulo 1000000007
using namespace std;
int main()
{
	int n;
	int i,j,k;
	
	cin>>n>>k;
	int a[n+1];
	bool dp[k+1];
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	for(i=1;i<=k;i++)
	{
		dp[i] = 0;
	}
	
	for(i=1;i<=n;i++)
	{
		dp[a[i]] = 1;
	}	
	
	for(i=1;i<=k;i++)
	{
		for(j=1;j<=n;j++)
		{
			bool flag = 1;
			if(i >= a[j])
			flag = dp[i - a[j]];
			
			if(flag==0)
			{
				dp[i] = 1;
				break;
			}
		}
	}
	
	if(dp[k])
	cout<<"First";
	else
	cout<<"Second";
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std ;
#define ll long long
inline int D(){
    int t ;
    scanf( "%d" , &t ) ;
    return t ;
}
inline ll LLD(){
    ll t ;
    scanf( "%lld" , &t ) ;
    return t ;
}


//#########################################

int main() {

long long n=LLD(),m=LLD();
long long x=m;
m-=(2*n);
if(m<1){
cout<<x/2;
return 0;
}


long long ans=(m/4)+n;
if(ans>0)
cout<<ans;
else
cout<<0;


}




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

using namespace std;
#define  ll long long
#define ld long double
#define f first
#define s second

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

    ll n,k;
    cin >> n >> k;
    vector<ll> v;
    ll ans=0;
    for(int i=1; i<=n; i++)
    {
        ll x;
        cin >> x;
        v.push_back(x);
    }

    sort(v.begin() , v.end() , greater<ll>() );
    while(k)
    {
        k--;
        ans += v.back();
        v.pop_back();
    }
    cout << ans;

    return 0;
}

Measured memory_bytes: 3656
#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <iomanip>
#include <map>
#include <queue>

using namespace std;

#define fori(x) for (int i = 0; i < x; ++i)
#define forj(x) for (int j = 0; j < x; ++j)

typedef long long ll;

const int INF = 2e9 + 5;
const int alength=100100;

int main() {
	int k,s;
	cin>>k>>s;

	int c=0;
	for(int x=0;x<=k;++x){
		for(int y=0;y<=k;++y){
			if(s-x-y>=0&&s-x-y<=k){
				++c;
			}
		}
	}
	cout<<c;
}

Measured memory_bytes: 256
def main():
    S = input()
    p = ''
    pp = ''
    for i, s in enumerate(S):
        if p == s:
            return (i, i + 1)
        if pp == s:
            return (i - 1, i + 1)
        pp, p = p, s
    return (-1, -1)

print(*main())


Measured memory_bytes: 8892
#include<stdio.h>

int main(void){
int n[100];
int i,j,s,t,u,v,w,x;
int sum=0;


i=0;
do{
scanf("%d",&n[i]);
i++;
}while(n[i-1]!=0);

for(i=0;n[i]!=0;i++){
	s=n[i];
	t=n[i]/6+1;

		for(j=1;j<t;j++){
			if(s%j==0) sum+=j;
		}
	if(n[i]%5==0){
		x=n[i]/5;
		if(s%x==0) sum+=x;
	}
	if(n[i]%4==0){
		w=n[i]/4;
		if(s%w==0) sum+=w;
	}	
	if(n[i]%3==0){
		u=n[i]/3;
		if(s%u==0) sum+=u;
	}
	if(n[i]%2==0){
		v=n[i]/2;
		if(s%v==0) sum+=v;
	}
	
    if(sum==n[i]) printf("perfect number\n");
    else if(sum>n[i]) printf("abundant number\n");
    else if(sum<n[i]) printf("deficient number\n");
    sum=0;
}

return 0;
}

Measured memory_bytes: 584
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#define stop(x) {cout<<x<<endl;exit(0);}
#define all(x) x.begin(),x.end()
#define szz(s) int(s.size())
#define endl '\n'
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
typedef long long ll;
using namespace std;

void file() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
    if (freopen("input.txt", "r", stdin));
    if (freopen("output.txt", "w", stdout));
#endif
}


int n , k =2 ;
vector < ll > v;
ll memo[100004][105];

ll solve(int i , int turn , int last) {
    if (i >= n && last == n-1)
        return 0;
    if (i >= n)
        return 1e10;
 
    ll& ret = memo[i][turn];  
    if (~ret) return ret;

    ret = 1e10;
    for(int e = 0 ; e < k ;e++)
        ret = min(ret,solve(i + e+1,e, i) + abs(v[i] - v[last]));

    return ret;
}
    
int main() {
    //file();

    cin >> n ;//>> k;
    memset(memo, -1, sizeof memo);
    v.resize(n);
    for (auto& e : v)cin >> e;
    cout << solve(0,0,0) << endl;

}

Measured memory_bytes: 89344
#include<iostream>
using namespace std;
int main(){
    int n, a[4][13];
    cin>>n;
    for(int i=0; i<4; i++){
        for(int j=0; j<13; j++){
            a[i][j]=0;
        }
    }
    char p;
    int q;
    for(int i=0; i<n; i++){
        cin>>p>>q;
        switch(p){
            case 'S':
                a[0][q-1] = 1;
                break;
            case 'H':
                a[1][q-1] = 1;
                break;
            case 'C':
                a[2][q-1] = 1;
                break;
            case 'D':
                a[3][q-1] = 1;
                break;
        }
    }
    for(int i=0; i<4; i++){
        for(int j=0; j<13; j++){
            if(a[i][j]==0){
                switch(i){
                    case 0:
                        cout<<"S"<<" "<<j+1<<endl;
                        break;
                    case 1:
                        cout<<"H"<<" "<<j+1<<endl;
                        break;
                    case 2:
                        cout<<"C"<<" "<<j+1<<endl;
                        break;
                    case 3:
                        cout<<"D"<<" "<<j+1<<endl;
                        break;
                }
            }
        }
    }
    
}



Measured memory_bytes: 3080
#include <stdio.h>
int main() {
  int n;
  int tnum = 1, i = 0, j = 0, ans = 0;
  int cbreak = 0;
  scanf ("%d", &n);
  int a[n];
  for (j = 0; j < n; j++) {
    if (j == n - 1)  scanf("%d\n", &a[j]);
    else scanf("%d", &a[j]);
  }

  while (i < n) {
    if (a[i] == tnum) {
      ans += i - cbreak;
      cbreak = i + 1;
      tnum++;
    }
    i++;
  }

  ans += n - cbreak;

  if (tnum != 1) printf ("%d\n", ans);
  else printf ("-1\n");
}


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

int main(void){
  int A,B;
  char S[10],T[10],U[10];
  
  scanf("%s %s", S,T);
  scanf("%d %d", &A,&B);
  scanf("%s", U);
  
  if(strcmp(S,U) == 0)
    A--;
  if(strcmp(T,U) == 0)
    B--;
  
  printf("%d %d\n", A,B);
  
  return 0;
}

Measured memory_bytes: 128
import java.io.*;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		
		while(true) {
			String[] str = (br.readLine()).split(" ");
			int x = Integer.parseInt(str[0]);
			int y = Integer.parseInt(str[1]);
			
			if(x==0 && y==0) {
				break;
			}else if(x<y) {
				sb.append(x).append(" ").append(y).append("\n");
			}else {
				sb.append(y).append(" ").append(x).append("\n");
			}
		}
		System.out.print(sb);
	}
}

Measured memory_bytes: 26232
extern crate core;

use std::io::{self, Read};

#[macro_use]
mod parser {
    macro_rules! input {
    ($s:expr=>$($t:tt)*) => {
        let mut lines=$s.split("\n");
        $(
            line_parse!(lines,$t);
        )*
    };
    }

    macro_rules! line_parse {
    ($lines:expr,($($name:ident:$t:tt)*)) => {
        let mut line=$lines.next().unwrap().split_whitespace();
        $(value_def!(line,$name,$t);)*
    };

    //複数行
    ($lines:expr,{$n:expr;$name:ident:$t:tt}) => {
        values_def!($lines,$n,$name,$t);
    };
    }

    macro_rules! value_def {
    ($line:expr,$name:ident,$t:tt) => {
        let $name=value!($line,$t);
    };
    }

    macro_rules! values_def {
    ($lines:expr,$n:expr,$name:ident,$t:tt) => {
        let $name={
        let mut vec=Vec::new();
        for i in 0..$n{
            let mut next=$lines.next().unwrap().split_whitespace();
            vec.push(value!(next,$t));
        }
        vec
        };
    };
    }

    macro_rules! value {
    //配列
    ($line:expr,[$t:tt]) => {
        $line.map(|x|{
        let mut iter=::std::iter::once(x);
        value!(iter,$t)
        }).collect::<Vec<_>>()
    };
    //タプル
    ($line:expr,($($t:tt),*)) => {
        ($(value!($line,$t),)*)
    };
    //文字列
    ($line:expr,#) => {
        $line.next().unwrap()
    };
    //単一値
    ($line:expr,$t:ty) => {
        $line.next().unwrap().parse::<$t>().unwrap()
    };
    }
}

fn main() {
    let mut input = String::new();
    io::stdin().read_to_string(&mut input).unwrap();
    let output = solve(input.trim().to_string());
    println!("{}", output);
}

fn solve(input: String) -> String {
    input!(input=>(n:usize)(list:[i64]));
    let mut list = list;
    list.sort();
    let a = list[n - 1];
    //listからmin(b,a-b)が最大の物を求める
    let b = list.into_iter()
        .filter(|&x| x != a)
        .max_by_key(|&b| std::cmp::min(b, a - b))
        .unwrap();
    format!("{} {}", a, b).to_string()
}

macro_rules! tests {
    ($($name:ident: $input:expr=>$output:expr,)*) => {
        mod tests {
            $(
                #[test]
                fn $name() {
                    assert_eq!($output.trim().to_string(),super::solve($input.trim().to_string()));
                }
            )*
        }
    }
}

tests! {
    test1: "5\n6 9 4 2 11" => "11 6",
    test2: "2\n100 0" => "100 0",
}


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

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

using ll = long long;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int k, n;
  cin >> k >> n;
  cout << (500 * k >= n ? "Yes" : "No");
  return 0;
}


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

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define pb push_back
#define mp make_pair

ll n;
ll a[123456];
ll ans[123456];
map<ll, ll> idx;
map<ll, ll> cnt;

struct Cmp{
    inline bool operator()(const ll &lhs, const ll &rhs) const{
        return lhs > rhs;
    }
};

set<ll, Cmp> q;

int main()
{
    ios_base::sync_with_stdio(0);
    cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < n; i++){
        q.insert(a[i]);
        if(idx[a[i]] == 0){
            idx[a[i]] = i + 1;
        }
        cnt[a[i]]++;
    }

    while(!q.empty()){
        int v = *q.begin();
        q.erase(q.begin());
        ll to = 0;
        if(!q.empty()) to = *q.begin();
        int index = idx[v];
        ans[index - 1] += ll(v - to) * (ll)cnt[v];
        cnt[to] += (ll)cnt[v];
        if(index < idx[to]) idx[to] = index;
    }

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

    return 0;
}


Measured memory_bytes: 19840
#include<stdio.h>
int main(){
char y[1001],z,x;
int a,b,c,e,f;
c=0; 
while(1){
	scanf("%s",y);
	if(y[0]=='0')break;
for(a=0;y[a]!='\0';a++){
	b=y[a]-'0';
	c=b+c;
}
printf("%d\n",c);
c=0;
}
return 0;
}


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

int main(){
    int n, a, b;
    cin >> n >> a >> b;
    string s;
    cin >> s;
    int all = 0;
    int over = 0;
    for (int i = 0; i < s.size(); i++){
        if (s[i] == 'a'){
            if (all < a + b) cout << "Yes" << endl, all++;
            else cout << "No" << endl;
        }
        else if (s[i] == 'b'){
            if (all < a + b && over < b){
                cout << "Yes" << endl;
                all++;
                over++;
            }
            else cout << "No" << endl;
        }
        else cout << "No" << endl;
    }
}

Measured memory_bytes: 896
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <algorithm>
#define rep(i,n) for(int i=0;i<(n);++i)
#define all(a) (a).begin(),(a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef long long ll;
//using Graph = vector<vector<pair<ll,ll>>>;
const int mod =1e+9+7;

const int dy[4]={0,1,0,-1};
const int dx[4]={1,0,-1,0};
const ll INF=1e10;

int main(){
    ll h,w; cin>>h>>w;
    vector<vector<ll>>a(h,vector<ll>(w));
    rep(i,h){
        rep(j,w){
            cin>>a[i][j];
        }
    }
    vector<vector<ll>>ans(h*w*10,vector<ll>(4));
    ll cnt=0;
    ll ny,nx;
    rep(i,h){
        if(i%2==0){
        rep(j,w){
            if(a[i][j]%2==0)continue;
            else if(h%2!=0&&i==h-1&&j==w-1)break;
            a[i][j]--;
            if(j+1<=w-1){
                a[i][j+1]++;
                ny=i;
                nx=j+1;
            }
            else{
                a[i+1][j]++;
                ny=i+1;
                nx=j;
            }
            ans[cnt][0]=i;
            ans[cnt][1]=j;
            ans[cnt][2]=ny;
            ans[cnt][3]=nx;
            cnt++;
        }
        }
        else{
            for(int j=w-1;0<=j;j--){
            if(a[i][j]%2==0)continue;
            else if(h%2==0&&i==h-1&&j==0)break;
            a[i][j]--;
            if(j-1>=0){
                a[i][j-1]++;
                ny=i;
                nx=j-1;
            }
            else{
                a[i+1][j]++;
                ny=i+1;
                nx=j;
            }
            ans[cnt][0]=i;
            ans[cnt][1]=j;
            ans[cnt][2]=ny;
            ans[cnt][3]=nx;
            cnt++;
        }
        }

    }
    /*rep(i,h){
        rep(j,w){
            cout<<a[i][j];
        }
        cout<<endl;
    }
    return 0;*/
    a.resize(cnt);
    cout<<cnt<<endl;
    rep(i,cnt){
        cout<<ans[i][0]+1<<" "<<ans[i][1]+1<<" "<<ans[i][2]+1<<" "<<ans[i][3]+1<<endl;
    }
}











Measured memory_bytes: 184028
N = int(input())
N_tmp = N

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

cnt = 0
divisors = make_divisors(N)
divisors_n1 = make_divisors(N-1)
divisors.extend(divisors_n1)
for K in divisors:
    tmp_N = N_tmp
    if K <= 1: continue
    while tmp_N % K == 0:
        tmp_N = tmp_N // K
    tmp_N = tmp_N % K
    if tmp_N == 1:
        cnt += 1

print(cnt)


Measured memory_bytes: 3188
#include <bits/stdc++.h>
using namespace std;
 
const int MAXL = 128;
const int Q = 850;
int L;
const string chars = 
		"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		"abcdefghijklmnopqrstuvwxyz"
		"0123456789";
vector<int> cnts(62);
int query(string s){
	cout<<"? "<<s<<endl;
	int v; cin>>v; return v;
}
bool is_subseq(string s){
	return (int)s.size()+query(s)==L;
}
string combine(const string& a, const string& b){
		string res;
		int ia = 0, ib = 0;
		while (ia < int(a.size()) && ib < int(b.size())) {
			if (is_subseq(a.substr(0, ia+1) + b.substr(ib))) {
				res += a[ia++];
			} else {
				res += b[ib++];
			}
		}
		while (ia < int(a.size())) res += a[ia++];
		while (ib < int(b.size())) res += b[ib++];
		return res;
	};
string solve(int l,int r){
	if(l==r) return string(cnts[l],chars[l]);
	int mid=(l+r)>>1;
	return combine(solve(l,mid),solve(mid+1,r));
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0);
	
	assert(chars.size() == 62);
	auto get_cnt = [&](char c) -> int {
		return MAXL - query(string(MAXL, c));
	};
	
	for (int i = 0; i < 62; i++) {
		cnts[i] = get_cnt(chars[i]);
		L += cnts[i];
	}
	string ans = solve(0, 61);
	cout << '!' << ' ' << ans << '\n';
}

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

int main(){
    int a, b;
    string op;
    int ans;
    while(1){
        cin >> a >> op >> b;
        if(op == "?") break;
        if(op == "+") ans = a+b;
        if(op == "-") ans = a-b;
        if(op == "*") ans = a*b;
        if(op == "/") ans = a/b;
        cout  << ans << endl;
    }
    return 0;
}


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

using namespace std;

long long pattern_num(long long am){
    return (am * (am - 1)) / 2;
}

int main()
{
    uint32_t nn;
    std::cin >> nn;

    vector<uint64_t> ball(nn + 1, 0);
    vector<uint64_t> pat(nn + 1, 0);
    long long temp_b = 0;
    long long ans = 0;

    for(int i = 1; i <= nn; i++){
        cin >> ball[i];
    }

    for(int i = 0; i <= nn; i++){
        pat[ball[i]]++;
    }

    for(int l = 0; l <= nn; l++){
        temp_b += pattern_num(pat[l]);
    }

//    cout << "ddd is " << temp_b << endl;

    for(int l = 1; l <= nn; l++){
		ans = temp_b;
		ans -= pattern_num(pat[ball[l]/*  - 1 */]);
		ans += pattern_num(pat[ball[l]/*  - 1 */] - 1);
		cout << ans << endl;
    }

    return 0;
}

Measured memory_bytes: 5760
#include<stdio.h>

int selectionSort(int A[],int N){
	int i,minj,j,x,cnt = 0;
	for(i = 0;i < N - 1;i++){
		minj = i;
		for(j = i;j < N;j++){
			if(A[j] < A[minj]){
				minj = j;
			}
		}
		x = A[i];
		A[i] = A[minj];
		A[minj] = x;
		
		if(i != minj) cnt++;
	}
	return cnt;
}
		
int main(void){		
	int A[100];
	int N,i,cnt;
	scanf("%d",&N);
	for(i = 0;i < N;i++){
		scanf("%d",&A[i]);
	}
	cnt = selectionSort(A,N);
	
	for(i = 0;i < N;i++){
		if(i > 0) printf(" ");
		printf("%d",A[i]);
	}
	printf("\n");
	printf("%d\n",cnt);
	
	return 0;
}

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

template<class T>
T gcd(T a, T b){
    if(b == 0) return a;
    return gcd(b, a%b);
}

// 最小公倍数を求める
template<class T>
T lcm(T a, T b){
    return a/gcd(a,b)*b;
}

int main(){
    int n;
    ll m; cin >> n >> m;
    ll a[n]; rep(i,n) cin >> a[i];
    rep(i,n) a[i] /= 2;
    ll a_lcm = a[0];
    rep(i,n-1) a_lcm = lcm(a_lcm, a[i+1]);
    rep(i,n){
        if((a_lcm/a[i]) % 2 == 0 ){
            cout << 0 << endl;
            return 0;     
        }
    }
    ll ans = m/a_lcm;
    if(ans%2 == 1) cout << ans/2+1 << endl;
    else cout << ans/2 << endl;
}

Measured memory_bytes: 1024
#include<string>
#include<iostream>
using namespace std;
string a;
int x,y,ans;
int main(){
    cin>>a;
    for (int i=0;i<a.size();++i){
    	if (x==y) {x++; if(a[i]=='p') ans--;}    
    	else {y++; if(a[i]=='g') ans++;}     
    }
	cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 512
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<cstring>
#include<math.h>
#include<bitset>
#include<queue>
#include<set>
#include<iomanip>
#include<math.h>
#include<assert.h>
#include<string>
#include<cstdlib>
#include<cctype>
#include<unordered_map>
#include<sstream>
#include<numeric>
// #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
constexpr ll INF = 1LL<<60;
const double INFD = 1e100;
const ll mod = 1000000007;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
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やllをstringに変換
template<typename T> inline string toString(const T &a){
  ostringstream oss;
  oss << a;
  return oss.str();
};
// ----------------------------------------------------------------------------

int main(){
  int N;
  ll D,A;
  cin >> N >> D >> A;
  D = D*2;
  vector<pair<ll,ll>> p(N);
  for(int i=0; i<N; i++){
    cin >> p[i].first >> p[i].second;
  }
  sort(p.begin(),p.end());
  queue<pair<ll,ll>> que;
  ll total = 0;
  ll ans = 0;
  for(int i=0; i<N; i++){
    ll x,h;
    x = p[i].first;
    h = p[i].second;
    while(!que.empty() && que.front().first < x){
      total -= que.front().second;
      que.pop();
    }
    h -= total;
    if(h<=0) continue;
    ll num = (h+A-1)/A;
    ll power = num * A;
    total += power;
    que.emplace(x+D,power);
    ans += num;
  }
  cout << ans << endl;
  return 0;
}


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

struct Node {
  int key;
  Node *parent, *left, *right;
};

Node* NIL;

int m;
Node* root;

void init() {
  NIL = new Node;
  NIL->parent = NIL;
  NIL->left = NIL;
  NIL->right = NIL;
  root = NIL;
}

void insert(int k) {
  Node* z = new Node {k, NIL, NIL, NIL};
  Node* y = NIL;
  Node* x = root;
  while (x != NIL) {
    y = x;
    if (z->key < x->key) x = x->left;
    else x = x->right;
  }
  z->parent = y;

  if (y == NIL) root = z;
  else if (z->key < y->key) y->left = z;
  else y->right = z;
}

void preParse(Node* u) {
  if (u == NIL) return;
  cout << " " << u->key;
  preParse(u->left);
  preParse(u->right);
}

void inParse(Node* u) {
  if (u == NIL) return;
  inParse(u->left);
  cout << " " << u->key;
  inParse(u->right);
}

Node* find(int k) {
  Node* c = root;
  while (c != NIL) {
    if (c->key == k) return c;
    else if (k < c->key) c = c->left;
    else c = c->right;
  }
  return NIL;
}

Node* getMinimum(Node* x) {
  while (x->left != NIL) x = x->left;
  return x;
}

Node* getSucessor(Node* x) {
  if (x->right != NIL) return getMinimum(x->right);

  Node* y = x->parent;
  while (y != NIL && x == y->right) {
    x = y;
    y = y->parent;
  }
  return y;
}

void deleteNode(Node* z) {
  Node* y;
  if (z->left == NIL | z->right == NIL) y = z;
  else y = getSucessor(z);

  Node* x;
  if (y->left != NIL) x = y->left;
  else x = y->right;

  if (x != NIL) x->parent = y->parent;

  if (y->parent == NIL) {
    root = x;
  } else if (y == y->parent->left) {
    y->parent->left = x;
  } else {
    y->parent->right = x;
  }

  if (y != z) z->key = y->key;
}

int main() {
  cin >> m;
  rep(i, m) {
    string com;
    cin >> com;
    int key;
    if (com == "insert") {
      cin >> key;
      insert(key);
    } else if (com == "print") {
      inParse(root);
      cout << endl;
      preParse(root);
      cout << endl;
    } else if (com == "find") {
      cin >> key;
      if (find(key) != NIL) cout << "yes" << endl;
      else cout << "no" << endl;
    } else if (com == "delete") {
      cin >> key;
      deleteNode(find(key));
    } else {
      cout << "error" << endl;
    }
  }
  return 0;
}



Measured memory_bytes: 21816