akhauriyash/Code-Regression huggingface dataset:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
  int n;
  char c[101];
  int min = 0;
  int sum = 0;
  bool head = false;

  cin >> n;
  for(int i=0;i<n;i++){
    cin >> c[i];
    if(c[i] == '('){
      sum++;
    }
    else{
      sum--;
    }
    if(sum < min)
      min = sum;
  }

  for(int i=0;i<-min;i++)
    cout << '(';
  for(int i=0;i<n;i++)
    cout << c[i];
  for(int i=0;i<(sum-min);i++)
    cout << ')';
  cout << endl;

  return 0;
}

Measured memory_bytes: 256
def solve(N, A):
    max_candies = 0
    # どこで下に降りるかの考え方がNケースあるだけ
    for j in range(1, N + 1):
        candies = sum(A[0][:j]) + sum(A[1][j - 1:])
        if candies > max_candies:
            max_candies = candies
    return max_candies


N = int(input())
A = [list(map(int, input().split())) for _ in range(2)]

print(solve(N, A))

Measured memory_bytes: 3060
N, Q= map(int, input().split())
S = input()
lr = [[int(i) for i in input().split()] for i in range(Q)] 

T = "AC"
t = [0] * (N+1)

for val in range(N):
	if S[val:val+2] == "AC":
		t[val + 1] = t[val] + 1
	else:
		t[val + 1] = t[val]

for val in range(Q):

	print(t[lr[val][1]-1] - t[lr[val][0]-1])

Measured memory_bytes: 24472
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
		
		Scanner keyboard = new Scanner(System.in); 
		
		String SA = keyboard.nextLine();	
		String SB = keyboard.nextLine();	
		String SC = keyboard.nextLine();	
		
		String input = SA.substring(0,1);

		keyboard.close();	
              System.out.print(solve(SA,SB,SC,input));

	}


public static String solve(String A, String B, String C, String input2) {
		String output ;

			if(input2.equals("a")) {
				if(A.length() != 0) {
					output = A.substring(0,1);
					return solve(A.substring(1), B, C, output);
				}else {
					return "A";
				}
			}else if(input2.equals("b")) {
				if(B.length() != 0) {
					output = B.substring(0,1);
					return solve(A, B.substring(1), C, output);
				}else {
					return "B";
				}
			}else{ 	//(input2.equals("c")) 
				if(C.length() != 0) {
					output = C.substring(0,1);
					return solve(A, B, C.substring(1), output);
				}else {
					return "C";
				}
			}
		}
}


Measured memory_bytes: 21716
import itertools

n = int(input())
happiness = [list(map(int, input().split())) for _ in range(n)]
dp = [[0, 0, 0] for _ in range(n + 1)] 

tpls = tuple((i, j) for i, j in itertools.product(range(3), repeat=2) if i != j)
for x in range(n):
    for i, j in tpls:
        dp[x + 1][j] = max(dp[x + 1][j], dp[x][i] + happiness[x][i])

print(max(dp[n]))


Measured memory_bytes: 78552
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
	vector<int> v(4);
	cin >> v[0] >> v[1] >> v[2] >> v[3];
	sort(v.begin(), v.end());
	cout << (v == vector<int>({ 1, 4, 7, 9 }) ? "YES" : "NO") << endl;
	return 0;
}

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

int check(long long p, int n, int k, int w[])
{
    int j = 0;
    int r = 0;
    for (int i = 0; i < k; i++)
    {
        long long sum = 0;
        while (sum + w[j] <= p)
        {
            sum += w[j];
            j++;
            if (j == n)
                return n;
        }
    }
    return j;
}

long long solve(int n, int k, int w[])
{
    long long left = 0;
    long long right = 10000 * 100000;
    while (right - left > 1)
    {
        long long mid = (right + left) / 2;
        if (check(mid, n, k, w) >= n)
            right = mid;
        else
            left = mid;
    }

    return right;
}

int main()
{
    int n, k;
    cin >> n >> k;
    int w[n];
    for (int i = 0; i < n; i++)
        scanf("%d", &w[i]);
    cout << solve(n, k, w) << endl;
}


Measured memory_bytes: 3576
S=gets.chomp.bytes
n=S.size
L=[nil]*(n+1)
l=[n]*26
(n-1).downto(0){|i|
	L[i]=l.dup
	l[S[i]-97]=i
}
L[-1]=l
q=[-1]
z={-1=>[nil,0]}
while q[0]&&q[0]!=n
	v=q.shift;l=L[v];26.times{|c|!z[l[c]]&&(z[l[c]]=[v,c];q<<l[c])}
end
r=''
(n,c=z[n];r<<(c+97))while n
$><<r.reverse[1..-1]

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

int main() {
 int n;
 cin >> n;
 vector<int> a(n);
  long long sum = 0;
 for(int i = 0; i < n; i++){
   cin >> a[i];
   sum += a[i];
 }
 int max = a[0];
 long long aftersum = a[0];
 for(int i = 1; i < n; i++){
   if(a[i] > max) max = a[i];
   if(a[i] < max){
     a[i] = max;
   }
   aftersum += a[i];
 }
  cout << aftersum-sum << endl;
}


Measured memory_bytes: 3996
#include<bits/stdc++.h>
using namespace std;
#define pfi(a) printf("%d",a);
#define pfl(a) printf("%lld",a);
#define pfin(a) printf("%d\n",a);
#define pfln(a) printf("%lld\n",a);
#define sfi(a) scanf("%d",&a);
#define sfl(a) scanf("%lld",&a);
#define pb push_back
#define f(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define endl "\n"
const int MAXN=2e3+5;
const int M= 1e9+7;
int n;

ll dp[MAXN][MAXN];
vector<pair<ll,int>> v(MAXN);

ll solve(int i,int j){
	if(i>j)
		return 0;
	int ind=(n-1)-(j-i);

	if(dp[i][j]!=-1)
		return dp[i][j];

	dp[i][j]=max(abs(v[ind].second-i)*v[ind].first+solve(i+1,j),abs(v[ind].second-j)*v[ind].first+solve(i,j-1));

	return dp[i][j];
}

int main(){

	
	sfi(n)

	f(i,0,n){
		f(j,0,n){
			dp[i][j]=-1;
		}
	}

	f(i,0,n){
		cin>>v[i].first;
		v[i].second=i;
	}

	sort(v.rbegin(),v.rend());

	ll ans=solve(0,n-1);

	pfl(ans)
	return 0;
}

Measured memory_bytes: 35328
//In the name of He
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
const bool DEBUG = false;
#define cerr if(DEBUG)cerr
#define bit(num,i) ((num>>i)&1)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<pii, pii> point;

const long long maxn = 3e5 + 5 + 1;
const long long INF = 4e18 ;
const long long M = 1e9 + 7;
const int lg = 21;

#pragma GCC Optimize("Ofast")

string s;
set<char> C;
int a[26];

int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> s;
    string t = s;
    for(int i=0;i<26;i++){
        C.insert('a'+i);
    }
    for(int i=0;i<s.size();i++){
        C.erase(s[i]);
        a[s[i]-'a'-1]=-1;
    }
    for(int i=24;i>=0;i--){
        a[i]+=a[i+1]+1;
        //cout << i << " : " << a[i] << '\n';
    }
    if(s.size()==26){
        if(next_permutation(s.begin(), s.end())){
            while(s.substr(0,s.size()-1) > t){
                s = s.substr(0,s.size()-1);
            }
            return cout << s, 0;
        }
        return cout << "-1", 0;
    }
    s += *C.begin();
    /*for(int i=s.size();i>=0;i--){
        if(a[s[i]-'a']){
            for(auto c : C){
                if(c > s[i]){
                    s[i] = c;
                    C.erase(c);
                    break;
                }
            }
            for(i = i+1;i<s.size();i++){
                char c = *C.begin();
                C.erase(C.begin());
                s[i] = c;
            }
            break;
        }
        for(int j = s[i]-'a'-1; j>=0; j--){
            a[j]++;
        }
    }
    if (s == t){
        s += *C.begin();
    }*/
    cout << s;
	return 0;
}


Measured memory_bytes: 256
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n = f()[0]

    rs = []
    for i in range(n):
        ss = input().split()
        rs.append((ss[0], int(ss[1]), i+1))
    
    
    rs = sorted(rs, key = lambda x:(x[0],-x[1]))
    for i in rs:
        print(i[2])



solve()


Measured memory_bytes: 3060
'''
5
H4 C9 S4 D2 C3
'''
N = int(input())
C1 = list(map(str, input().split()))
C2 = [None] * N

for i in range(N):
  C2[i] = C1[i]

def bubble(C, N):
  #count = 0
  for i in range(0, N-1, 1):
    for j in range(N-1, i, -1):
      #print("j:{} C[j-1]:{} C[j]:{}".format(j, C[j-1], C[j]))
      if (int(C[j-1][1]) > int(C[j][1])):
        C[j-1], C[j] = C[j], C[j-1]
        #count += 1
        #print("Swapped. C:{}".format(C))

  print(str(C).replace('[', '').replace(']', '').replace(',', '').replace('\'', ''))
  print("Stable") # because buddle sort is stable sort

def selection(C, N):
  for i in range(N):
    minj = i
    for j in range(i+1, N):
      if (int(C[minj][1]) > int(C[j][1])):
        minj = j
    C[minj], C[i] = C[i], C[minj]

  print(str(C).replace('[', '').replace(']', '').replace(',', '').replace('\'', ''))

def main():
  bubble(C1, N)
  selection(C2, N)

  if (C1 == C2):
    print("Stable")
  else:
    print("Not stable")

main()



Measured memory_bytes: 5628
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;

class Point{
public:
  bool n[20][20]; 
  int x,y,d;
};

int main(){
  int i,j,w,h;
  while(1){
    int a,gx,gy,ans = -1;
    Point u,v;
    queue <Point> Q;
    cin >> w >> h;
    if(w == 0 && h == 0) break;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	cin >> a;
	if(a == 2){
	  u.y = i;
	  u.x = j;
	  u.n[i][j] = 0;
	}
	else if(a == 3){
	  gy = i;
	  gx = j;
	  u.n[i][j] = 0;
	}
	else if(a == 1){
	  u.n[i][j] = 1;
	}
	else{
	  u.n[i][j] = 0;
	}
      }
    }

    u.d = 0;
    Q.push(u);
    while(!Q.empty()){
      u = Q.front();
      Q.pop();
      u.d++;

      if(u.d <= 10 && u.x < w - 1 && u.n[u.y][u.x+1] == 0){
	for(j=u.x+1;j<w;j++){
	  if(u.n[u.y][j] == 1) break;
	  if(u.y == gy && j == gx) break;
	}
	if(u.y == gy && j == gx){
	  ans = u.d;
	  break;
	}
	if(j < w){
	  v = u;
	  v.n[v.y][j] = 0;
	  v.x = j - 1;
	  Q.push(v);
	}
      }

      if(u.d <= 10 && u.x > 0 && u.n[u.y][u.x-1] == 0){
	for(j=u.x-1;j>=0;j--){
	  if(u.n[u.y][j] == 1) break;
	  if(u.y == gy && j == gx) break;
	}
	if(u.y == gy && j == gx){
	  ans = u.d;
	  break;
	}
	if(j >= 0){
	  v = u;
	  v.n[v.y][j] = 0;
	  v.x = j + 1;
	  Q.push(v);
	}
      }

      if(u.d <= 10 && u.y < h - 1 && u.n[u.y+1][u.x] == 0){
	for(j=u.y+1;j<h;j++){
	  if(u.n[j][u.x] == 1) break;
	  if(j == gy && u.x == gx) break;
	}
	if(j == gy && u.x == gx){
	  ans = u.d;
	  break;
	}
	if(j < h){
	  v = u;
	  v.n[j][v.x] = 0;
	  v.y = j - 1;
	  Q.push(v);
	}
      }

      if(u.d <= 10 && u.y > 0 && u.n[u.y-1][u.x] == 0){
	for(j=u.y-1;j>=0;j--){
	  if(u.n[j][u.x] == 1) break;
	  if(j == gy && u.x == gx) break;
	}
	if(j == gy && u.x == gx){
	  ans = u.d;
	  break;
	}
	if(j >= 0){
	  v = u;
	  v.n[j][v.x] = 0;
	  v.y = j + 1;
	  Q.push(v);
	}
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

char s[4];
int c = 0;

int main(){
  if(scanf("%s", s)==1);  
  
  if(s[0]==s[1] && s[2]==s[3] && s[0]!=s[2])c=1;
  if(s[0]==s[2] && s[1]==s[3] && s[0]!=s[1])c=1;
  if(s[0]==s[3] && s[1]==s[2] && s[0]!=s[1])c=1;
  
  if(c==1)printf("Yes");
  if(c==0)printf("No");
  
  return 0;
}

Measured memory_bytes: 128
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
#define REP(var, a, b) for (int var = (a); var < (b); var++)
#define rep(var, n) for (int var = 0; var < (n); ++var)
#define ALL(c) (c).begin(), (c).end()
#define rALL(c) (c).rbegin(), (c).rend()
ll MOD = 1000000007;

int main() {
  //
  ll n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  ll cnt = 0;
  rep(i, n - 1) {
    if (s[i] == s[i + 1]) cnt++;
  }
  int diff = n - 1 - cnt;
  cout << min(n - 1, cnt + 2 * k) << endl;
  return 0;
}


Measured memory_bytes: 512
object Main extends App {
	val strAry = io.StdIn.readLine().split(' ')
	
	val num1 = strAry(0).toInt
	val num2 = strAry(1).toInt
	
	// 面積
	val area = num1 * num2
	
	// 周の長さ
	val l = num1 * 2 + num2 * 2
	
	println(area + " " + l)
}


Measured memory_bytes: 43764
#include <cstdio>

int main()
{
	int x, y, z;
	scanf("%d %d %d", &x, &y, &z);

	int a[3] = {x ,y ,z}, buff = 0;

	while (1){
		for(int i = 0; i < 3; i++){
			for(int j = i; j < 3; j++){
				if(a[i] > a[j]) {
					buff = a[j];
					a[j] = a[i];
					a[i] = buff;
				}
			}
		}
		if (a[0] <= a[1] && a[1] <= a[2]) break;
	}

	printf("%d %d %d\n", a[0], a[1], a[2]);
	return 0;
}

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

int main(int argc, char const *argv[])
{
    int n[3];
    for (int i=0; i<3; i++) cin >> n[i];
    int min_dist = n[0] + n[1];
    if (n[0] + n[2] < min_dist) {
        min_dist = n[0] + n[2];
    }
    if (n[1] + n[2] < min_dist) {
        min_dist = n[1] + n[2];
    }
    cout << min_dist << endl;
    return 0;
}


Measured memory_bytes: 256
/*
hey stop, don't skip the shit till it's solved
*/
#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
int main()
{
string s;
cin>>s;
string ans="Yes";
for(int i=0;i<s.length();i++)
{
if((i+1)%2==0 && s[i]=='R')
{
ans="No";
}
if((i+1)%2!=0 && s[i]=='L')
{
ans="No";
}
}
cout<<ans<<endl;
}

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

namespace Sample
{
	class Program
	{
		static void Main(string[] args)
		{
			var array = Console.ReadLine().Split().Select(s => int.Parse(s)).ToArray();
			var a = array[0];
			var b = array[1];

			var sign = (a > b) ? ">" : (a == b) ? "==" : "<";
			Console.WriteLine($"a {sign} b");
		}
	}
}



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

using namespace std;

int string2int(const string s) {
    if(s == "A") 
        return 10;
    else if (s == "B")
        return 11;
    else if (s == "C")
        return 12;
    else if (s == "D")
        return 13;
    else if (s == "E")
        return 14;
    else if (s == "F")
        return 15;
}

int main() {
    string x, y;
    cin >> x >> y;
    if(string2int(x) < string2int(y)) {
        cout << "<" << endl;
    }
    else if(string2int(x) > string2int(y)) {
        cout << ">" << endl;
    }
    else {
        cout << "=" << endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <string.h>
  
char dict[30600000];
  
char word[26];
  
long encode(const char *key){
  int n=strlen(key),i;
  long sum=0;
  long keta=1;
  for(i=0;i<n;i++){
    int keyi=key[i];
    sum = sum + keta * word[keyi];
    keta=(keta<<2)+keta;
  }
  return (sum);
}
  
void insert(const char *key2) {
  long key=encode(key2);
  long keta=key/8;
  dict[keta]=(dict[keta]|(1<<(key%8)));
}
  
int find_h(const char *key2) {
  long key=encode(key2);
  return (dict[key/8] & 1<<(key%8));
}
  
int main(void) {
  long n,i;
  word['A'] = 1;
  word['C'] = 2;
  word['G'] = 3;
  word['T'] = 4;
  scanf("%ld\n",&n);
  for(i=0;i<n;i++){
    char cmd[8], str[14]; scanf("%s %s\n",cmd, str);
    if(cmd[0]=='i') {
      insert(str);
    }
    else {
      if(find_h(str)) printf("yes\n");
      else printf("no\n");
    }
  }
  return 0;
}

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

const int N = 3010;
const int MOD = 1e9 + 7;
int n, q;
int F[N][N], G[N], A[N], inv2 = (MOD + 1) / 2, sum = 1, res;
int main()
{
    cin >> n >> q;
    for (int i = 1; i <= n; ++ i)
        cin >> A[i];
    for (int i = 1; i <= n; ++ i)
        for (int j = 1; j <= n; ++ j)
            if (A[i] < A[j]) F[i][j] = 1;
    while (q --)
    {
        int a, b;
        cin >> a >> b; sum = 2ll * sum % MOD;
        for (int i = 1; i <= n; ++ i) if ((i != a) && (i != b))
            F[a][i] = F[b][i] = 1ll * (F[a][i] + F[b][i]) * inv2 % MOD,
            F[i][a] = F[i][b] = 1ll * (F[i][a] + F[i][b]) * inv2 % MOD;
        F[a][b] = F[b][a] = 1ll * (F[a][b] + F[b][a]) * inv2 % MOD;
    }
    for (int i = 1; i <= n; ++ i)
        for (int j = 1; j < i; ++ j)
            res = (res + F[i][j]) % MOD;
    cout << 1ll * sum * res % MOD << endl;
}


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

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


int main() {
  int x;
  cin >> x;
  rep2(i,x,100004){
    if(is_prime(i)){
      cout << i << endl;
      break;
    }
  }
}

Measured memory_bytes: 256
import math
n=int(input())
ans=0
if n>1:
    data=[]
    for _ in range(n-1):
        data_=list(map(int,input().split()))
        data.append(data_)
    if n>2:
        for i in range(0,n-2):
            time=data[i][1]
            for j in range(i,n-2):
                arrive=time+data[j][0]
                s=data[j+1][1]
                if arrive<=s:
                    time=s
                else:
                    f=data[j+1][2]
                    time=s+f*(math.ceil((arrive-s)/f))
            time+=data[n-2][0]
            print(time)
    time=data[n-2][0]+data[n-2][1]
    print(time)
print(ans)

Measured memory_bytes: 3188
import sys
def input():
    return sys.stdin.readline()[:-1]

def main():
    N, K = map(int, input().split())
    h = [0] + list(map(int, input().split())) # 便宜上、先頭に[0]

    dp = [1000000007 for _ in range(N+1)] # dp[i]：足場iに移動する最小コスト
    dp[1] = 0 # 初期位置のため、0
    dp[2] = abs(h[2]-h[1]) # 足場2への移動は1パターンのみのため

    for dst in range(3, N+1):
        for k in range(1,K+1):
            src = dst-k
            if src < 1:
                break
            dp[dst] = min(dp[dst], dp[src] + abs(h[dst]-h[src]))
    print(dp[N])

if __name__ == "__main__":
    main()

Measured memory_bytes: 57312
/*
 *  Created on: 26 / 4 / 2020
 *  Author:     Eslam Waheed
 */
#include <bits/stdc++.h>
//#include <bits/BIGINT.h>
using namespace std;

#define _CRT_SECURE_NO_WARNINGS
#define __USE_MINGW_ANSI_STDIO
#define Eslam__Waheed ios_base::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);

#define ll   long long
#define ull  unsigned ll
#define dd   double
#define ld   long dd
#define ss   string

#define pb      push_back
#define all(c)  c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define stp(n)  cout << fixed << setprecision(n)
#define iq(v)   v.resize(unique(v.begin(),v.end())-v.begin())
#define sz(s)   (ll)(s.size())
#define endl    '\n'

#define loop(i,m,n)  for(ll i=m;i<n;i++)

ll gcd(ll a, ll b) { return !b ? abs(a) : gcd(b, a % b); }
ll lcm(ll a, ll b) { return abs(a / gcd(a, b)) * b; }

//const ll mod = 1e9 + 7;
//__int128
/*
 you can ^_^ (•‿•)

( إن الله وملائكته يصلون على النبي يا أيها الذين آمنوا صلوا عليه وسلموا تسليما )
*/
int main()
{
    //stp(20);
    Eslam__Waheed
        ll a, b; cin >> a >> b;
    if (b >= a)
    {
        cout << "unsafe";
    }
    else
    {
        cout << "safe";
    }
}

Measured memory_bytes: 3652
#include<bits/stdc++.h>
using namespace std;
using ll =long long;
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define SORT(i) sort((i).begin(),(i).end())
constexpr int INF = 2000000000;
constexpr int mod = 1000000007;

int main() {
	int a, b; cin >> a >> b;
	if (a / 10 > 0 || b / 10 > 0) {
		cout << "-1" << "\n";
	}
	else cout << a * b << "\n";

	return 0;
}

Measured memory_bytes: 256
fun main() {
    fun readInt():Int{ return readLine()!!.toInt()}
    fun readLong():Long{return readLine()!!.toLong()}
    fun readString():String{return readLine()!!}
    fun readIntList():List<Int>{return readLine()!!.split(" ").map { it.toInt() }}
    fun readLongList():List<Long>{return readLine()!!.split(" ").map { it.toLong() }}
    fun readStringList():List<String>{return readLine()!!.split(" ")}
    fun readIntLists(n: Int, sorted: Boolean=false):List<List<Int>>{return if(sorted){(0 until n).map { readIntList().sorted() }}else{(0 until n).map { readIntList()}}}
    fun readLongLists(n:Int, sorted: Boolean=false):List<List<Long>>{return if(sorted){(0 until n).map { readLongList().sorted() }}else{(0 until n).map { readLongList()}}}
    fun readStringLists(n:Int):List<List<String>>{return (0 until n).map { readStringList() }}
    val n=readInt()
    val a=readLongList()
    val m:Long=1000000000+7
    var ans:Long=0
    var tmp:Long=0
    var sum:Long=0
    for (i in a){
        sum=(sum+i)%m
    }
    tmp=sum
    for (i in a){
        tmp=(tmp-i+m)%m
        ans=((tmp*i)%m+ans)%m
    }

    println(ans)

}


Measured memory_bytes: 65268
def asindex(c):
    return ord(c) - ord('a')


def asstr(i):
    return chr(i + ord('a'))


def main():
    n = int(input())
    S = [[0 for _ in range(n)] for _ in range(26)]

    for i in range(n):
        for c in input():
            S[asindex(c)][i] += 1

    ans = ''
    for i in range(26):
        freq = min(S[i])
        
        for _ in range(freq):
            ans += asstr(i)

    print(ans)


if __name__ == "__main__":
    main()

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

public class Main {

	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int n = sc.nextInt();
		int[] integers = new int[n];
		for(int i = 0; i < n; i++) {
			integers[i] = sc.nextInt();
		}
		boolean distinct = true;
		Arrays.sort(integers);
		for(int i = 0; i < n-1; i++) {
			if(integers[i] == integers[i+1]) {
				distinct = false;
				break;
			}
		}
		if(distinct) pw.print("YES");
		else pw.print("NO");
		pw.close();
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public Scanner(String file) throws FileNotFoundException {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

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

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

		public String nextLine() throws IOException {
			return br.readLine();
		}

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

		public boolean ready() throws IOException {
			return br.ready();
		}

	}
}


Measured memory_bytes: 48408
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    A = [0] + list(int(input()) for _ in range(n)) + [0]

    res = 0
    cnt = 0
    for i in range(n + 2):
        if A[i] == 0:
            res += cnt // 2
            cnt = 0
        else:
            cnt += A[i]

    print(res)


if __name__ == '__main__':
    resolve()


Measured memory_bytes: 7888
c_dif = ord('A')-ord('a')
s = input()
for c in s:
    if ord('a')<= ord(c) and ord(c) <= ord('z'): 
        print(chr(ord(c)+c_dif) ,end='')
    elif ord('A')<= ord(c) and ord(c) <= ord('Z'): 
        print(chr(ord(c)-c_dif) ,end='')
    else:
        print(c, end='')
print()

Measured memory_bytes: 7448
s=[ord(ch)-97 for ch in input()]
t=[ord(ch)-97 for ch in input()]

# minimize
mnmz={ch for ch in s} & {ch for ch in t}
if len(mnmz)==0:
    print("")
    exit()
s=[ z for z in s if z in mnmz ]
t=[ z for z in t if z in mnmz ]
if len(s)>len(t): s, t = t, s

# init
dp=[[0]*len(s) for _ in range(len(t))]

#dp frame init
for i in range(len(s)): 
    if s[i]==t[0]:
        for j in range(i, len(s)):
            dp[0][j]=1
        break
for i in range(len(t)): 
    if s[0]==t[i]:
        for j in range(i, len(t)):
            dp[j][0]=1
        break
#dp
for i in range(1,len(t)):
    for j in range(1, len(s)):
        if s[j]==t[i]:
            dp[i][j]=dp[i-1][j-1]+1
        else: dp[i][j]=(dp[i][j-1] if dp[i][j-1]>dp[i-1][j] else dp[i-1][j])

#recover string
res=[]
j=len(s)-1
i=len(t)-1
while i>=0 and j>=0:
    if s[j]==t[i]: #and dp[i][j]==dp[i-1][j-1]+1:
        res.append(s[j])
        i=i-1
        j=j-1
        continue
    if i==0 and j==0: break
    if j>0 and dp[i][j]==dp[i][j-1]:
        j=j-1
    else: i=i-1
# last letter (first acually)


print("".join(chr(i+97) for i in reversed(res)))


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

using namespace std;

const int maxn = 5e2 + 10, maxc = 40;
int n, c, cnt[3][maxc], d[maxc][maxc], ans;

int main(){
	cin >> n >> c;
	for (int i = 1; i <= c; ++i)
		for (int j = 1; j <= c; ++j)
			cin >> d[i][j];
	for (int i = 0; i < n; ++i)
		for (int j = 0, a; j < n; ++j) {
			cin >> a;
			cnt[(i + j) % 3][a]++;
		}
	ans = 1e9;
	for (int i = 1; i <= c; ++i)
		for (int j = 1; j <= c; ++j)
			if (i != j)
				for (int k = 1; k <= c; ++k)
					if (k != i && k != j) {
						int val = 0;
						for (int t = 1; t <= c; ++t) {
							val += d[t][i] * cnt[0][t];
							val += d[t][j] * cnt[1][t];
							val += d[t][k] * cnt[2][t];
						}
						ans = min(ans, val);
					}
	cout << ans << '\n';
	return 0;
}


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

int main(){
    int h,w;
    cin >> h >> w;
    rep(i, h){
        string c;
        cin >> c;
        cout << c << endl;
        cout << c << endl;
    }
}

Measured memory_bytes: 3608
#include<stdio.h>
#include<string.h>
#include <math.h>
#include <stdlib.h>
#define MAX 10000

#define N 100
#define INF 1000
#define WHITE 0

#define GRAY 1

#define BLACK 2


void cal(void);
void print(void);
void bfs(void);
int n, A[N][N], d[N];

int main() {
	int i, j, l, m, data;

	cal();

	scanf("%d", &n);
	for (i = 0; i < n; i++) {
	  
		scanf("%d%d", &l, &m);
		for (j = 0; j < m; j++) {
			scanf("%d", &data);
			
			A[l - 1][data - 1] = 1;
		}
	}

	bfs();
	print();

	return 0;
}
void bfs(void) {
	int i, v;
	int head, tail, que[N*N], z[N];

	head = -1;
	tail = 1;
	for (i = 0; i < n; i++) {
		d[i] = INF;
		z[i] = WHITE;
	}
	z[0] = GRAY;
	que[++head] = 0;
	d[0] = 0;

	while (head != tail) {
		v = que[head++];
		for (i = 0; i < n; i++) {
			if (A[v][i] == 1 && z[i] == WHITE) {
				z[i] = GRAY;
				que[tail++] = i;
				d[i] = d[v] + 1;
			}
		}
		z[v] = BLACK;
	}
}
void cal(void) {
	int i, j;

	for (i = 0; i < n; i++) {
		for (j = 0; j < n; j++) {
			A[i][j] = 0;
		}
	}
	return ;
}
void print(void) {
	int i;
	for (i = 0; i < n; i++) {
		printf("%d ", i + 1);
		if (d[i] == INF) printf("-1\n");
		else			 printf("%d\n", d[i]);
	}
	return ;
}



Measured memory_bytes: 2140
from itertools import accumulate
from operator import itemgetter
N = int(input())
AB = [tuple(map(int, input().split())) for _ in range(N)]
AB.sort(key=itemgetter(1))
def g():
    acc = 0
    for a, b in AB:
        acc += a
        yield acc <= b
print("Yes" if all(g()) else "No")

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

#define endl '\n'
#define ll long long
#define rint register int

#define re register
#define pb(x) push_back(x);
#define ce(x) cout << x << '\n';

using db = long double;
using pll = pair < ll, ll >;
#define scan(a, n) 		for(int i = 0; i < n; i++)cin >> a[i];

#define rep(i,x,n,inc) for(i=x ; i<n ; i+=inc)
#define repr(i,x,n,inc) for( i=x ; i>n ; i+=inc)
#define all(a)      (a).begin(),(a).end()
#define unique_sort(x) sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))

#define mp(a,b) make_pair(a,b)
#define ff first
#define ss second
#define hell 1000000007
#define infl LLONG_MAX

#define Foxen(i,s) for (i=s.begin(); i!=s.end(); i++)
#define Fill(s,v) memset(s,v,sizeof(s))
#define cout_p(x, p) cout << fixed << setprecision((p)) << x << endl     //print with precision

#define tc(tt) \
    ll tt;    \
    cin >> tt; \
    for (ll _tt = 0; _tt < tt; _tt++) // testcase 

int32_t main() {
	ios::sync_with_stdio(0); 		cin.tie(0); cout.tie(0);

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ll t, i, x, j, y, 	z, k, n;
	string s;
	cin >> s;

	ce(s.substr(0, 3))

}


Measured memory_bytes: 3612
import java.util.Scanner;

class Main {

	public static void main(String[] args){

		Scanner scan = new Scanner(System.in);
		int[] num = new int[3];
		for(int i=0; i<3; i++){
			num[i] = scan.nextInt();
		}
		scan.close();

		for(int s=0; s<num.length; s++){
			for(int t=s+1; t<num.length; t++){
				if(num[t]<num[s]){
					int tmp = num[s];
					num[s] = num[t];
					num[t] = tmp;
				}
			}
		}


		System.out.println(num[0] + " " + num[1] + " " + num[2]);
	}
}

Measured memory_bytes: 17360
#include<stdio.h>

int main(){
  int A,B;
  scanf("%d %d",&A,&B);
  
  if(A > B){
    printf("%d\n",2*A-1);
  }if(A==B){
    printf("%d\n",A+B);
  }else if(A < B){
    printf("%d\n",2*B-1);
  }
}

Measured memory_bytes: 128
# https://onlinejudge.u-aizu.ac.jp/problems/1149


class Cake:
    def __init__(self, w, d):
        self.w = w
        self.d = d

    def area(self):
        return self.w * self.d

    def cut(self, s):
        perimeter = self.w * 2 + self.d * 2
        s %= perimeter
        if s < self.w:
            return [Cake(s, self.d), Cake(self.w - s, self.d)]
        elif s < self.w + self.d:
            l = s - self.w
            return [Cake(self.w, l), Cake(self.w, self.d - l)]
        elif s < self.w + self.d + self.w:
            l = s - (self.w + self.d)
            return [Cake(self.w - l, self.d), Cake(l, self.d)]
        else:
            l = s - (self.w + self.d + self.w)
            return [Cake(self.w, self.d - l), Cake(self.w, l)]

    def __repr__(self):
        return '(w {}, d {})'.format(self.w, self.d)


def solve(Q, n, w, d):
    cakes = [Cake(w, d)]
    for p, s in Q:
        # print(cakes)
        cut_cake = cakes.pop(p)
        cake1, cake2 = cut_cake.cut(s)
        if cake1.area() > cake2.area():
            cake2, cake1 = cake1, cake2
        cakes.append(cake1)
        cakes.append(cake2)

    # print(cakes)
    cakes.sort(key=lambda cake: cake.area())
    areas_str = []
    for cake in cakes:
        area_str = str(cake.area())
        areas_str.append(area_str)
    print(' '.join(areas_str))


if __name__ == "__main__":
    while True:
        n, w, d = list(map(int, input().split()))
        if n == 0 and w == 0 and d == 0:
            break
        Q = []
        for _ in range(n):
            p, s = list(map(int, input().split()))
            Q.append((p-1, s))
        solve(Q, n, w, d)



Measured memory_bytes: 5628
#include <iostream>
#include <vector>

using namespace std;

const int mod=1e9+7;
long long dp[200003];
int last[200003];
int levi[200003];
vector<int> veki;

int main (){
	int n;
	cin>>n;
	veki.push_back(0);
	for(int i=1;i<=n;i++){
		int a;
		cin>>a;
		if(a==veki[veki.size()-1]) continue;
		veki.push_back(a);
	}
	for(int i=1;i<veki.size();i++){
		if(last[veki[i]]!=0){
			levi[i]=last[veki[i]];
		}
		last[veki[i]]=i;
	}
	dp[1]=1;
	for(int i=2;i<veki.size();i++){
		dp[i]=(dp[i-1]+dp[levi[i]])%mod;
	}
	cout<<dp[veki.size()-1];





return 0;
}



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

using namespace std;
typedef long long ll;

#define REP(i,n) for(int i=0;i<n;++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#  define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#  define DBG(fmt, ...)
#endif

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const int MOD = 1000000007;
#define MAX_N 100010

int N;
int prime[MAX_N];           // i 番目の素数
bool is_prime[MAX_N + 1];   // is_prime[i] が true なら i は素数

// prime と is_prime のテーブルを [0, n] の範囲で作成
// n 以下の素数の数を返す
int sieve(int n) {
    int p = 0;
    for(int i = 0; i <= n; ++i) {
        is_prime[i] = true;
    }
    is_prime[0] = is_prime[1] = false;
    for(int i = 1; i <= n; ++i) {
        if(is_prime[i]) {
            prime[p++] = i;
            for(int j = 2 * i; j <= n; j += i) {
                is_prime[j] = false;
            }
        }
    }
    return p;
}

signed main()
{
    cin >> N;
    int cnt = sieve(55555);
    int ans = 0;
    REP(i, cnt) {
        if(prime[i] % 10 == 1) {
            printf("%d", prime[i]);
            ans++;
            if(ans >= N) { break; }
            printf(" ");
        }
    }
    printf("\n");
    return 0;
}


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

int main() {
	
	int n;
	int point[51000]; //データセットごとの点数
	int count = 0;
	
	while (1)
	{
		
		cin >> n;
		
		if(n == 0) break; // 0を入力されたら終了
		
		int a[n];
		count++;
		
		for (int i = 0; i < n; i++) std::cin >> a[i];
		
		// 配列を大きい順に並べ替える
		sort(a, a+n, greater<int>());
		
		int proa = 0;
		int d;
		//データセットごとの作業
		for (int i = 0; i < n; i++) {
			
			// 隣のデータとの差
			d = abs(proa - a[i]);
			
			// 初期値は最大点
			if(i == 0) point[count - 1] = a[0];
			
			if(d < point[count - 1]) point[count - 1] = d;
			
			proa = a[i];
		}
	}
	
	for (int i = 0; i < count; i++) {
		cout << point[i] << endl;
	}
	
	return 0;
}


Measured memory_bytes: 3088
#include <bits/stdc++.h>
using namespace std;
#define Int int64_t
#define dump(x) cout << (x) << endl
#define fi first
#define se second
Int mod = 1e9+7;
//Int mod = 998244353;
Int INF = 1e18;
double EPS = 0.00000001;

Int res[501][501];

void f(Int b, Int e, Int d) {
    if (b == e) return;
    Int mid = (b + e) / 2;
    for (Int i = b; i <= mid; i++) {
        for (Int j = mid + 1; j <= e; j++) {
            res[i][j] = d;
        }
    }
    f(b, mid, d + 1);
    f(mid + 1, e, d + 1);
}

int main() {
    Int n;
    cin >> n;
    f(1, n, 1);
    for (Int i = 1; i <= n - 1; i++) {
        for (Int j = i + 1; j <= n; j++) {
            cout << res[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

Measured memory_bytes: 2432
/*---------------Go Code GO---------------*/

#include<bits/stdc++.h>
using namespace std;

#define     PI              acos(-1.0)
#define     O_O             ios_base::sync_with_stdio(false); cin.tie(NULL)
#define     precision(a)    fixed<<setprecision(a)
#define     endl            '\n'
#define     Flush           cout<<flush
#define     LLMX            0x3fffffffffffffff
#define     PW2(a)          (long long)1<<a


int main()
{
    long long i,j,k,l,m,n,o,p,q,tst,csin=0;
    cin>>n;
    string s;
    map<pair<int, int>, long long> mp;
    for(i=n; i>=0; i--)
    {
        s=to_string(i);

        if(s[s.size()-1]!='0')
        {
            mp[{s[0],s[s.size()-1]}]++;
        }
    }

    m=0;
    for(i=n; i>=0; i--)
    {
        s=to_string(i);

        if(s[s.size()-1]!='0')
        {
            m+=mp[{s[s.size()-1],s[0]}];
        }
    }

    cout<<m<<endl;





    return 0;
}




Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
string ans;
const int MAXN = 3002;

void lcs(string s, string t, int n, int m, string res)
{
    if (n == 0 || m == 0)
    {
        if (ans.length() < res.length())
        {
            ans = res;
        }
        return;
    }
    if (s[n - 1] == t[m - 1])
        lcs(s, t, n - 1, m - 1, string(1, s[n - 1]) + res);
    else
    {
        lcs(s, t, n - 1, m, res);
        lcs(s, t, n, m - 1, res);
    }
}

string lcs(const string &s, const string &t)
{
    int n = s.length();
    int m = t.length();
    int dp[n + 1][m + 1];
    for(int i = 0;i<=n;i++) dp[i][0] = 0;
    for(int j = 0;j<=m;j++) dp[0][j] = 0;
    
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            if (s[i - 1] == t[j - 1])
            {
                dp[i][j] = dp[i - 1][j - 1] + 1;
            }
            else
            {
                dp[i][j] = max(dp[i - 1][j], dp[i][j-1]);
            }
        }
    }
/*
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
            cout << dp[i][j] << " ";
        cout << "\n";
    }
  */  
    string ans = "";
    
    int i = n;
    int j = m;
    int len = dp[n][m];
    while (i > 0 && j > 0 && len)
    {
        while (dp[i][j] == dp[i - 1][j - 1]) //go diagonally up
            --i, --j;
        while (dp[i][j] == dp[i - 1][j]) //go vertically up
            --i;
        while (dp[i][j] == dp[i][j - 1]) //go horizontally left
            --j;
        if (s[i - 1] == t[j - 1])
        {
            ans += s[i - 1];
            --i, --j;
            --len;
        }
    }
    
    reverse(ans.begin(), ans.end());
    return ans;
}

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

    cout << lcs(s, t) << "\n";
    return 0;
}

Measured memory_bytes: 35456
#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> n(4);
    for (int i = 0; i < 4; i++) {
        std::cin >> n[i];
    }

    std::sort(n.begin(), n.end());

    std::vector<int> right = { 1, 4, 7, 9 };

    if (n == right) {
        std::cout << "YES" << std::endl;
    } else {
        std::cout << "NO" << std::endl;
    }
}


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

using namespace std;
int main (){
    long long a,b,k;
    cin >> a >> b >> k;
    if(a<k){
        k-=a;
        a=0;
    }
    else{
        a-=k;
        k=0;
    }
    if(b<k){
        k-=b;
        b=0;
    }
    else{
        b-=k;
        k=0;
    }
    cout << a << " " << b << "\n";
}

Measured memory_bytes: 256
import sys
input = sys.stdin.readline

def main():
    N = int(input())
    p = list(map(int, input().split()))

    m = [p[0]] * N
    for i in range(1, N):
        m[i] = min(m[i-1], p[i])
    
    ans = 0
    for i in range(N):
        if m[i] == p[i]:
            ans += 1
    print(ans)


if __name__ == "__main__":
    main()

Measured memory_bytes: 25696
//
// Created by 海老晃行 on 2019-07-25.
//

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

int main(){
    char  a,b;
    cin >> a >> b;
    if(a == 'H'){
        if(b == 'H'){
            cout << 'H' << endl;
        }else{
            cout << 'D' << endl;
        }
    }else{
        if(b == 'H'){
            cout << 'D' << endl;
        }else {
            cout << 'H' << endl;
        }
    }
    return 0;
}


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

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

	int x = 0 , ans = 0;
	char key = 'I';
	for(int i=0;i<N;++i){
		char t = S[i];
		if(t == key){
			x += 1;
		}
		else{
			x -= 1;
		}
		if(ans <x) ans=x;
		
	}
	cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rgi register int
#define rgl register long long
#define ll long long
#define il inline

using namespace std;

const int N = 2000 + 10;

ll n, ans, max_size = -5000;
ll l[N], book[N], s[N];

il ll read() {
	rgl x = 0, f = 0, ch;
	while(!isdigit(ch = getchar()))	f |= ch == '-';
	while(isdigit(ch))	x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

int main() {
	n = read();
	for(rgi i = 1; i <= n; ++i) l[i] = read();
	sort(l + 1, l + n + 1);
	for(rgi i = 1; i <= n; ++i)	for(rgi j = i + 1; j <= n; ++j) {
		int k = upper_bound(l + j + 1, l + n + 1, l[i] + l[j] - 1) - (l + 1);
		ans += k - j;
	}
	printf("%lld", ans);
	return 0;
}
/*
6
1 1 1 2 2 2

*/

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<string>
#include<algorithm>
#include<utility>

#define rep(i, a) for (int i = 0; i < a; i++)
#define reps(i, s, a) for (int i = s; i < a; i++)
#define llint long long int
using namespace std;

const int N_MAX = 505;
const llint INF = 999999999999999;

int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    int m = a;
    if (m < b) m = b;
    if (m < c) m = c;

    cout << m * 10 + a + b + c - m;
}

Measured memory_bytes: 256
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroint-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define ll long long
#define f(i,a,b) for(int i=a;i<b;i++)
#define mod 1000000007
#define pi 3.14159265359
#define mp make_pair
#define ff first
#define ss second
#define rf(i,a,b) for(int i=a;i>=b;i--)
#define sc(a) scanf("%intd",&a)
#define pf printf
#define sz(a) (int)(a.size())
#define psf push_front
#define ppf pop_front
#define ppb pop_back
#define pb push_back
#define pq priority_queue
#define all(s) s.begin(),s.end()
#define sp(a) setprecision(a)
#define rz resize
#define ld long double
#define inf 1e18
#define ub upper_bound
#define lb lower_bound
#define bs binary_search
#define eb emplace_back
#define vvlli vector<vector<ll> >
#define vlli vector<ll>
#define vi vector<int>
#define vvi vector<vector<int> >
#define vstr vector<string>
#define vvstr vector<vector<string> >
ll binpow(ll a, ll b){ll res=1;while(b!=0){if(b&1)res*=a;a*=a;b>>=1;}return res;}

using namespace std;

ll d,dp[10001][2][2][100];
string l,r;

inline ll fn(int level, int tlo, int thi, int rem)
{
    if(level>=sz(l))
    {
        if(rem==0)
            return 1;
        return 0;
    }
    if(dp[level][tlo][thi][rem]==-1)
    {
        ll ans=0;
        int blo = 0,bhi = 9;
        if(tlo)
            blo = l[level]-'0';
        if(thi)
            bhi = r[level]-'0';
        f(i,blo,bhi+1)
        {
            int ntlo = tlo, nthi = thi;
            if(i > (l[level]-'0'))
                ntlo = 0;
            if(i < r[level]-'0')
                nthi = 0;
            ans += fn(level+1,ntlo,nthi,(rem+i)%d);
            ans%=mod;
        }
        dp[level][tlo][thi][rem] = ans;
    }
    return dp[level][tlo][thi][rem];
}

void solve()
{
    memset(dp,-1,sizeof(dp));
    cin>>r>>d;
    l="1";
    while(sz(l)<sz(r))
        l+='0';
    reverse(all(l));
    cout<<fn(0,1,1,0)<<"\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif
    solve();
}

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

// template {{{  0 
// using {{{ 1
using ll = long long int;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vii = vector<pii>;
using vll = vector<pll>;
// }}} 1
// definition {{{ 1
// scaning {{{ 2
#define Scd(x) scanf("%d", &x)
#define Scd2(x,y) scanf("%d%d", &x, &y)
#define Scd3(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define Scll(x) scanf("%lld", &x)
#define Scll2(x,y) scanf("%lld%lld", &x, &y)
#define Scll3(x,y,z) scanf("%lld%lld%lld", &x, &y, &z)
#define Scc(c) scanf("%c", &c);
#define Scs(s) scanf("%s", s);
#define Scstr(s) scanf("%s", &s);
// }}} 2
// constants {{{ 2
#define EPS (1e-7)
#define INF (2e9)
#define PI (acos(-1))
// }}} 2
// systems {{{ 2
#define Repe(x,y,z) for(ll x = z; x < y; x++)
#define Rep(x,y) Repe(x,y,0)
#define RRepe(x,y,z) for(ll x = y-z-1; x >= 0; x--)
#define RRep(x,y) RRepe(x,y,0)
// }}} 2
// output {{{ 2
#define YesNo(a) (a)?printf("Yes\n"):printf("No\n")
#define YESNO(a) (a)?printf("YES\n"):printf("NO\n")
// }}} 2
// }}} 1
// input {{{ 1
// }}} 1
// }}} 0

int main() {

    int N;
    Scd(N);
    vii a(N);
    vii b(N);
    int x,y;
    Rep(i,N){
        Scd2(x,y);
        a[i] = {x,i};
        b[i] = {y,i};
    }

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

    vector<vii> e(N);
    int u,v,c;
    Repe(i,N,1){
        u = a[i-1].second; v = a[i].second; c = abs(a[i-1].first - a[i].first);
        e[u].push_back({v,c}); e[v].push_back({u,c});
        u = b[i-1].second; v = b[i].second; c = abs(b[i-1].first - b[i].first);
        e[u].push_back({v,c}); e[v].push_back({u,c});
    }

    vi id(N,-1);
    priority_queue<pii,vector<pii>,greater<pii>> q;

    q.push({0,0});
    int ans = 0;
    while( q.size() ){
        int from = q.top().second;
        int cost = q.top().first;
        q.pop();
        if( id[from] != -1 ) continue;
        id[from] = 0;
        ans += cost;
        for( pii tow : e[from] ){
            q.push({tow.second,tow.first});
        }
    }

    printf ("%d\n", ans );

    return 0;

}



Measured memory_bytes: 12020
#include<stdio.h>
int MIN(int a,int b){return a<b?a:b;}
int main(){
  int n,m,s,t,u,i,j,k,d[1010][1010];
  while(scanf("%d %d %d %d %d",&n,&m,&s,&t,&u),n){
    for(i=0;i<1010*1010;i++)d[i/1010][i%1010]=1e8;
    for(i=0;i<1010;i++)d[i][i]=0;
    while(m--){
      scanf("%d %d %d",&i,&j,&k);
      d[i][j]=k;
    }
    for(i=1;i<=n;i++){
      for(j=1;j<=n;j++){
	for(k=1;k<=n;k++)d[j][k]=MIN(d[j][k],d[j][i]+d[i][k]);
      }
    }
    j=1e9;
    for(i=1;i<=n;i++)j=MIN(j,d[s][i]+d[i][t]+d[i][u]);
    printf("%d\n",j);
  }
  return 0;
}
  

Measured memory_bytes: 4584
#include <stdio.h>

p[2<<19],a,b,m,i,j;

x(i,j){if(a)p[i]=60,p[i+1]=62,a--;}
y(i,j){if(b)p[i]=94,p[i+m]=118,b--;}
#define c for(i=n%2;i<n;i+=2)
#define d for(j=m%2;j<m;j+=2)
#define o i*m+j

main(n) {
	scanf("%d%d%d%d",&n,&m,&a,&b);

	if(n&1)d x(j);
	if(m&1)c y(i*m);
	if(n>2&m>2&n&m&a&a>0&b>0)a++,x(0),x(2*m+1),y(2);
	c d if(p[o+1]);else if(a)x(o),x(o+m);else y(o),y(o+1);

	if(a|b)puts("NO");
	else{
		puts("YES");
		for(i=0;i<n;puts(""),i++)
			for(j=0;j<m;)putchar(p[o++]?:46);
	}
}

Measured memory_bytes: 5248
import itertools
n, a, b, c = map(int, input().split())
take = [int(input()) for i in range(n)]

ans = 100000000
for i in itertools.product([0, 1, 2, 3], repeat = n):
    x = 0
    y = 0
    z = 0
    for j in range(n):
        if i[j] == 1:
            x += take[j]
        elif i[j] == 2:
            y += take[j]
        elif i[j] == 3:
            z += take[j]

    if x * y * z == 0:
        continue

    x, y, z = sorted([x, y, z], reverse = True)
    tmp = 0
    tmp += (i.count(1) + i.count(2) + i.count(3) - 3)*10
    tmp += abs(x-a) + abs(y-b) + abs(z-c)
    ans = min(ans, tmp)

print(ans)


Measured memory_bytes: 3064
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define ALL(v) v.begin(), v.end()
ll mod = 1e9 + 7;
ll mul(ll a, ll b)
{
	return (a*b) % mod;
}
ll power(ll a, ll b)
{
	ll  re = 1;
	while(b)
	{
		while(!(b&1))a=mul(a,a),b>>=1;;
		re = mul(re,a);
		b--;
	}
	return re;
}
vector<ll> vec(1000010);
ll ncr(ll a, ll b)
{
	return (mul(vec[a], power(mul(vec[a-b], vec[b]), mod -2 )) );
}
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	

	ll n,d;
	cin >> n >> d;
	d = d*2LL + 1;
	cout << n / d + (n%d != 0? 1 : 0) ;
}

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

int main(){
	string s;
	cin>>s;
	string ans(s.size(),2);
	int p=0;
	
	for(int i=0;i<s.size();i++){
		if(s[i]=='B'){
			p--;
			if(p<0)p=0;
		}else{
			ans[p]=s[i];
			p++;
		}
	}
	
	for(int i=0;i<p;i++){
		cout<<ans[i];
	}
	cout<<endl;
	
	return 0;
}

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

// GNU C Compiler
//template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }

// type
using ll = int_fast64_t;
using ull = uint_fast64_t;
using LL = long long;
using ULL = unsigned long long;
using PII = pair<int, int>;
//using pair<T1, T2> = p12;

//container
#define PB emplace_back
#define MP make_pair 
#define SZ(a) int((a).size())
#define ALL(v) v.begin(),v.end()

//repetition
#define REP(n) for(int i=0; i<(n); i++)
#define REPj(n) for(int j=0; j<(n); j++)
#define REPinit(i,a,n) for(int i=(a); i<(n); i++)
#define REPinitj(j,a,n) for(int j=(a); j<(n); j++)

//algorithm
//#define SORT(c) sort((c).begin(),(c).end())

//constant

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//Optimizer
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

/* 
	add utility Algorithm
*/

bool apperance[1000000001];

int main(int argc, char const *argv[])
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    ull N, A, B;
    cin >> N >> A >> B;

    if (A == 0) {
        cout << 0 << endl;
    }
    else {
        ull pairAB = N / (A + B);
        ull extraAB = min(A, N % (A + B));
        cout << (pairAB * A + extraAB) << endl;
    }

    return 0;
}


Measured memory_bytes: 3648
def resolve():
    n = int(input())
    d = {}
    for _ in range(n):
        s = input()
        if s not in d:
            d[s] = 1
        else:
            d[s] += 1
    d = sorted(d.items(), key=lambda x:x[1], reverse=True)
    ans = []
    before = d[0][1]
    for t in d:
        if t[1] != before:
            break
        else:
            ans.append(t[0])
    ans = list(sorted(ans))
    print(*ans, sep="\n")
resolve()

Measured memory_bytes: 47528