akhauriyash/Code-Regression huggingface dataset:
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;

public class Main implements Runnable {

    private static int MOD = 1_000_000_007;

    public static void main(String[] args) {
        // Run with 32MB stack
        Thread thread = new Thread(null, new Main(), "", 32 * 1024 * 1024);
        thread.start();
    }

    @Override
    public void run() {
        final Scanner scanner = new Scanner(System.in);
        solve(scanner);
    }

    static void solve(Scanner scanner) {
        String s = scanner.next();
        LinkedList<Character> l = new LinkedList();
        for(char c: s.toCharArray()) {
            l.add(c);
        }
        
        int Q = Integer.parseInt(scanner.next());
        boolean rev = false;
        for (int i = 0; i < Q; i++) {
            int q = Integer.parseInt(scanner.next());
            if (q == 1) {
                rev = !rev;
            } else {
                int t = Integer.parseInt(scanner.next());
                char c = scanner.next().charAt(0);
                if (t == 1) {
                    if (!rev) {
                        l.addFirst(c);
                    } else {
                        l.addLast(c);
                    }
                } else {
                    if (!rev) {
                        l.addLast(c);
                    } else {
                        l.addFirst(c);
                    }
                }
            }
        }

        StringBuilder sb = new StringBuilder();
        Iterator<Character> ite;
        if (rev) {
            ite = l.descendingIterator();
        } else {
            ite = l.iterator();
        }
        while (ite.hasNext()) {
            sb.append(ite.next());
        }

        System.out.println(sb.toString());
    }

}



Measured memory_bytes: 98240
n = int(input())
d = [dict() for _ in range(n)]
for i in range(n):
    a = int(input())
    for _ in range(a):
        x, y = map(int, input().split())
        d[i][x - 1] = y

def count_honest(bits):
    honest = [(bits >> i) & 1 for i in range(n)]
    for i in range(n):
        if not honest[i]:
            continue
        for k, v in d[i].items():
            if honest[k] != v:
                return 0
    return sum(honest)


print(max([count_honest(i) for i in range(2**n)]))

Measured memory_bytes: 3444
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
long dp[N][3];
int main(){
  long n,a,b,c;
  cin >> n >> a >> b >> c;
  dp[0][0] = a;
  dp[0][1] = b;
  dp[0][2] = c;
  for(int i = 1; i < n; ++i){
  	cin >> a >> b >> c;
    dp[i][0] = max(dp[i-1][1],dp[i-1][2])+a;
  	dp[i][1] = max(dp[i-1][0],dp[i-1][2])+b;
  	dp[i][2] = max(dp[i-1][1],dp[i-1][0])+c;
  }
  cout << max(dp[n-1][0],max(dp[n-1][1],dp[n-1][2]));
}

Measured memory_bytes: 2560
import sys
N = int(input())
S = list(map(int,input().split()))
d = {}

for i in range(1,N+1):
  d[i] = S[i-1]


d2 = sorted(d.items(), key = lambda x:x[1])

for i in range(0,len(d2)):
  print(d2[i][0])

Measured memory_bytes: 25608
// SeeAlso: https://atcoder.jp/contests/abc123/tasks/abc123_a

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <limits>

using namespace std;

#define MAX 100

int main() {
    int a, b, c, d, e, k;

    cin >> a;
    cin >> b;
    cin >> c;
    cin >> d;
    cin >> e;
    cin >> k;

    if ((e - a) <= k) {
        cout << "Yay!" << endl;
    } else {
        cout << ":(" << endl;
    }

    return 0;
}


Measured memory_bytes: 256
#include <string>
#include<iostream>
#include<vector>
#include <bits/stdc++.h>
#include <stdio.h>
#include <numeric>
#include<math.h>
#include <ctype.h>
#include <algorithm> // sort
#include <map> // pair
using namespace std;
#define nin(n) int n; cin >> n;
#define kin(k) int k; cin >> k;
int main() {
  int a,b,c,n;
  cin >> a >>b >> c >>n;
  int ans=0;
  int B;
  for(int i=0;i<=n;i++){
    for(int j=0;j<=n;j++){
      if(n-i*a-j*c>=0){ 
        if((n-i*a-j*c)%b==0){
          ans++;
        }
      }
    }
  }
  cout << ans << endl;
}

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

void solve()
{
  int a,b;
  cin >> a >> b;
  if(a+b >= 10) {
    cout << "error" << endl;
  } else {
    cout << a+b << endl;
  }
}

int main(){cin.tie(0);ios_base::sync_with_stdio(false);solve();return 0;}


Measured memory_bytes: 256
#!/usr/bin/env python3
import math

def solve(a: int, b: int, h: int, m: int) -> float:
    th1 = (h * 60 + m) / (12 * 60) * 2 * math.pi
    th2 = m / 60 * 2 * math.pi
    dy = a * math.sin(th1) - b * math.sin(th2)
    dx = a * math.cos(th1) - b * math.cos(th2)
    return math.sqrt(dy ** 2 + dx ** 2)

# generated by online-judge-template-generator v4.1.0 (https://github.com/kmyk/online-judge-template-generator)
def main():
    A, B, H, M = map(int, input().split())
    thk = solve(A, B, H, M)
    print(thk)

if __name__ == '__main__':
    main()


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

n, q = map(int, input().split())
g = [ [] for _ in range(n) ]
for _ in range(n-1):
    a, b = map(int, input().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1)

pl = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    pl[p-1] += x


def dfs(curr, pare):
    for chi in g[curr]:
        if chi == pare: continue
        pl[chi] += pl[curr]
        dfs(chi, curr)

def main():
    dfs(0,-1)
    print(*pl)

if __name__ == "__main__":
    main()

Measured memory_bytes: 230960
A, V = map(int, input().split())
B, W = map(int, input().split())
T = int(input())

Aleft = A-V*T
Aright = A+V*T
Bleft = B-W*T
Bright = B+W*T

if Aleft <= Bleft and Bright <= Aright:
    print("YES")
else:
    print("NO")

#print(A-V*T, A+V*T)
#print(B-W*T, B+W*T)

Measured memory_bytes: 61588
#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	string a;
	cin>>a;
	int se[n],sw[n];
	for(int i=0;i<n;++i){
		se[i]=0;
		sw[i]=0;
	}
	if(a[0]=='E')
	    se[0]=1;
	else
		sw[0]=1;
	for(int i=0;i<n;i++){
		if(a[i]=='E'){
			se[i]=se[i-1]+1;
			sw[i]=sw[i-1];
		}
		else {
			sw[i]=sw[i-1]+1;
			se[i]=se[i-1];
		}
	}
	int minv=300000000;
	for(int i=0;i<n;++i){
		int mmm=0;
		if(i==0)
			mmm=se[n-1]-se[i];
		else if(i==n-1)
			mmm=sw[n-2];
		mmm=sw[i-1]+(se[n-1]-se[i]);
		minv=min(minv,mmm);
	}
	cout<<minv<<endl;
	return 0;
}


Measured memory_bytes: 3076
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Scanner;

public class Main {
    public static PrintWriter out;

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

    private static void run() {
        out = new PrintWriter(new BufferedOutputStream(System.out));
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next()); // マス目の数
        int m = Integer.parseInt(sc.next()); // 料金所の数
        int x = Integer.parseInt(sc.next()); // 初期地点
        // 料金所マス
        HashSet<Integer> a = new HashSet<>(m);
        for (int i = 0; i < m; i++) {
            a.add(Integer.parseInt(sc.next()));
        }
        // 全てのマス
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n + 1; i++) {
            // 料金所マスは1、それ以外は0とする
            if (a.contains(i)) {
                sb.append(1);
            } else {
                sb.append(0);
            }
        }
        // 初期地点から左右の料金所マスの数を計算
        String left = Main.deleteZero(sb.substring(0, x));
        String right = Main.deleteZero(sb.substring(x + 1));
        int answer = Math.min(left.length(), right.length());
        out.println(answer);
        out.close();
        sc.close();
    }

    /**
     * 引数の文字列から"0"を削除した値を返却
     * @param pstr
     * @return
     */
    private static String deleteZero(String pstr) {
        String str = pstr.replaceAll("0", "_");
        return str.replaceAll("_", "");
    }
}


Measured memory_bytes: 35692
#include<iostream>
#include<string>
using namespace std;
int main(){
  string s;
  cin>>s;
  if(s[2]==s[3]&&s[4]==s[5]){
    cout<<"Yes"<<endl;
  }
  else{
   cout<<"No"<<endl; 
  }
  
  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0);

using namespace std;

namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};}
template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";}
template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";}
void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);}
typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9;

int main(){
	cin.tie(0); cin.sync_with_stdio(0);
	string s; cin >> s;
	int n = s.size();
	vi ans(n);
	rep(i, n - 1){
		if(s[i] == 'R' && s[i + 1] == 'L'){
			
			int r = 0, l = 0;
			for(int j = i; j >= 0 && s[j] == 'R'; j--) r++;
			for(int j = i + 1; j < n && s[j] == 'L'; j++) l++;
			
			ans[i] += (r + 1) / 2 + l / 2;
			ans[i + 1] += r / 2 + (l + 1) / 2;
		}
	}
	rep(i, n) cout << ans[i] << (i==n-1?"\n":" ");
	return 0;
}

Measured memory_bytes: 1104
# -*- coding: utf-8 -*-
"""
Created on Sun May  3 20:56:43 2020
"""

import sys
#import numpy as np

sys.setrecursionlimit(10 ** 9)
#def input():
#    return sys.stdin.readline()[:-1]
mod = 10**9+7

#N = int(input())
N, M = map(int,input().split())
H = list(map(int,input().split()))

graph = [[] for _ in range(N+1)]
for _ in range(M):
    a, b = map(lambda x : int(x)-1, input().split())
    graph[a].append(b)
    graph[b].append(a)

ans = 0
for i in range(N):
    flg = True
    for r in graph[i]:
        if H[i] <= H[r]:
            flg = False
    if flg:
        ans += 1
#    print(i,flg,ans,graph[i],H[i])
print(ans)




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

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = std::int64_t;
using P = std::tuple<int,int>;

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

    int A, B;
    std::cin >> A >> B;

    int res;
    if(B % A == 0){
        res = A + B;
    }else{
        res = B - A;
    }

    std::cout << res << std::endl;
}


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

int a, b;

int main () {
    cin >> a >> b;
    if (a==b) {
        cout << "Draw" << endl;
    } else if (a==1 || (a>b && b!=1)) {
        cout << "Alice" << endl;
    } else if (b==1 || (a<b && a!=1)) {
        cout << "Bob" << endl;
    }
    return 0;
}


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

#define lli long long int
#define ld long double
//初期化
#define DEFO(n) lli n = 0;
//整数最大
#define MAX(n) lli n = 9000000000000000000;
//整数最小
#define MIN(n) lli n = -9000000000000000000;
//少数最大
#define MAX_D(n) ld n = 4500;
//少数最小
#define MIN_D(n) ld n = -4500;
//整数入力
#define LLI(n) \
  lli n;       \
  cin >> n;
//少数入力
#define LD(n) \
  ld n;       \
  cin > n;
//整数配列入力
#define LLIV(a, n)            \
  vector<lli> a(n);           \
  for (lli i = 0; i < n; i++) \
  {                           \
    cin >> a.at(i);           \
  };
//２列整数配列入力
#define LLIV2(a, b, n)         \
  vector<lli> a(n), b(n);      \
  for (lli i = 0; i < n; i++)  \
  {                            \
    cin >> a.at(i) >> b.at(i); \
  };
//３列整数配列入力
#define LLIV3(a, b, c, n)                 \
  vector<lli> a(n), b(n), c(n);           \
  for (lli i = 0; i < n; i++)             \
  {                                       \
    cin >> a.at(i) >> b.at(i) >> c.at(i); \
  };
//少数配列入力
#define LDV(a, n)             \
  vector<ld> a(n);            \
  for (lli i = 0; i < n; i++) \
  {                           \
    cin >> a.at(i);           \
  };
//文字列入力
#define STR(n) \
  string n;    \
  cin >> n;
//文字列配列入力
#define STRV(a, n)            \
  vector<string> a(n);        \
  for (lli i = 0; i < n; i++) \
  {                           \
    cin >> a.at(i);           \
  };
//配列の合計
#define SUM(n) accumulate(n.begin(), n.end(), 0);
//配列を昇順
#define SORT(n) sort(n.begin(), n.end());
//配列を降順
#define REVE(n) reverse(n.begin(), n.end());
//Yesと表示
#define yes              \
  cout << "Yes" << endl; \
  return 0;
//Noと表示
#define no              \
  cout << "No" << endl; \
  return 0;
//Noneと表示
#define none              \
  cout << "None" << endl; \
  return 0;
//四捨五入
#define C45(n) (n + 1) / 2
//nを出力
#define OUT(n) cout << n << endl;
//文字列からcを探す
#define SRC(n, c)                    \
  for (lli i = 0; i < s.size(); i++) \
  {                                  \
    if (s.at(i) == c)                \
    {                                \
      n++;                           \
    }                                \
  };

//素数
bool IsPrime(lli num)
{
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false;
  long double sqrtNum = sqrt(num);
  for (lli i = 3; i <= sqrtNum; i += 2)
  {
    if (num % i == 0)
      return false;
  }
  return true;
}
//各桁の和
lli KSum(lli n)
{
  lli sum = 0;
  if (n < 0)
    return 0;
  while (n > 0)
  {
    sum += n % 10;
    n /= 10;
  }
  return sum;
}

//数値反転
bool KReve(lli n)
{
  int reverse = 0;
  int remaind;
  int tmp = n;
  while (tmp != 0)
  {
    remaind = tmp % 10;
    reverse = reverse * 10 + remaind;
    tmp /= 10;
  }
  if (reverse == n)
    return true;
  else
    return false;
}

int main(void)
{
  ios::sync_with_stdio(false);
  LLI(N) LLI(Y)
  for(lli i = 0;i <= N;i++) {
    for(lli u = 0;u <= N - i;u++) {
      if(i * 10000 + u * 5000 + (N - i - u) * 1000 == Y) {
        cout << i << " " << u << " " << N - i - u << endl;
        return 0;
      }
    }
  }
  cout << "-1 -1 -1" << endl;
}


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

int main(){
	int m, d;
	scanf("%d%d", &m, &d);
	int cnt = 0;
	for(int a = 10; a <= d; a++){
		if(a%10 >= 2 && a/10 >= 2 && (a%10) * (a/10) <= m){
			cnt++;
		}
	}
	printf("%d\n", cnt);
	return 0;
} 

Measured memory_bytes: 256
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <vector>

const int MOD = 1e9 + 7;
const int iINF = 2147483647 / 2;
const long long int llINF = 9223372036854775807 / 2;

using namespace std;
using ll = long long int;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;

typedef pair<ll, ll> pll;
bool paircomp(const pll &a, const pll &b) {
  if (a.first == b.first)
    return a.second < b.second;
  return a.first < b.first;
}

#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define AUTO(i, m) for (auto &i : m)
#define ALL(a) (a).begin(), (a).end()
#define MAX(vec) *std::max_element(vec.begin(), vec.end())
#define MIN(vec) *std::min_element(vec.begin(), vec.end())
#define ARGMAX(vec)                                                            \
  std::distance(vec.begin(), std::max_element(vec.begin(), vec.end()))
#define ARGMIN(vec)                                                            \
  std::distance(vec.begin(), std::min_element(vec.begin(), vec.end()))
#define REV(T) greater<T>()
#define PQ(T) priority_queue<T, vector<T>, greater<T>>
#define VVL(a, b, c) vector<vector<ll>>(a, vector<ll>(b, c))
#define VV(T, a, b, c) vector<vector<T>>(a, vector<T>(b, c))
#define VVVL(a, b, c, d)                                                       \
  vector<vector<vector<ll>>>(a, vector<vector<ll>>(b, vector<ll>(c, d)))
#define VVV(T, a, b, c, d)                                                     \
  vector<vector<vector<T>>>(a, vector<vector<T>>(b, vector<T>(c, d)))
#define SP(a) setprecision(a)
#define SQRT(a) sqrt((long double)(a))
#define DPOW(a, b) pow((long double)(a), (long double)(b))
#define UNIQUE(vec)                                                            \
  do {                                                                         \
    sort(ALL((vec)));                                                          \
    (vec).erase(std::unique(ALL((vec))), (vec).end());                         \
  } while (0)

ll POW(ll n, ll m) {
  if (m == 0) {
    return 1;
  } else if (m % 2 == 0) {
    ll tmp = POW(n, m / 2);
    return (tmp * tmp);
  } else {
    return (n * POW(n, m - 1));
  }
}

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll H, W, h, w;
  cin >> H >> W >> h >> w;
  ll poyo = (H / h) * (W / w);
  ll yo = 1e9 / (h * w);
  if ((H * W - poyo) * yo - ((poyo * (h * w - 1) * yo) + poyo) > 0) {
    cout << "Yes" << endl;
    FOR(i, 1, H + 1) {
      FOR(j, 1, W + 1) {
        if (i % h == 0 && j % w == 0)
          cout << -(h * w - 1) * yo - 1 << " ";
        else
          cout << yo << " ";
      }
      cout << endl;
    }
  } else
    cout << "No" << endl;

  return 0;
}


Measured memory_bytes: 2816
import os
import sys
from io import BytesIO, IOBase
def main():
    startHour, startMin, endHour, endMin, studyTime = map(int, input().rstrip().split())
    diff = (endHour-startHour)*60 - startMin + endMin
    if diff <= studyTime:
        print(0)
    else:
        print(diff-studyTime)

##############################################################################################################
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")



if __name__ == "__main__":
    main()

Measured memory_bytes: 62604
#include <iostream>
#include <math.h>
#include <numeric>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <tuple>
#include <functional>

#define PI 3.14159265359
#define INF 1e9
#define LINF 1e18
#define irep(i,n) for(int  i = 0; i < n; i++)
#define irep2(i,a,n) for(int i = a; i < n; i++)
#define lrep(i,n) for(long long  i = 0; i < n; i++)
#define lrep2(i,a,n) for(long long i = a; i < n; i++)
#define in(a) cin >> a
#define out(a) cout << a << endl
typedef long long ll;

typedef std::vector<int> v_int;
typedef std::vector<std::vector<int> > v2_int;
typedef std::vector<ll> v_ll;
typedef std::vector<std::vector<ll> > v2_ll;

using namespace std;

int main() 
{   
    int a,b,c;
    cin >> a >> b >> c;

    if(a+b < c) {
        cout << "No" << endl;
    } else {
        cout << "Yes" << endl;
    }
    return 0;
}
    



Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <algorithm>
#include <queue>
using namespace std;
long long int ketawa(long long int n){
	long long int sum = 0; 
	while(n > 0){
		sum += n % 10; 
		n /= 10; 
	}
	return sum; 
}
int main(){
	int K; 
	cin >> K;
	vector<long long int> snuke; 
	long long int D[20] = {}; 
	D[0] = 1; 
	for(int i = 1; i < 19; i++){
		D[i] = 10 * D[i-1]; 
	}
	for(int i = 1; i < 10; i++){
		snuke.push_back(i); 
	}
	for(int i = 1; i < 10; i++){
		snuke.push_back(i * 10 + 9); 
	}
	for(int i = 1; i < 10; i++){
		snuke.push_back(i * 100 + 99); 
	}
	for(int i = 10; i < 100; i++){
		snuke.push_back(i * 100 + 99); 
	}
	for(int d = 5; d <= 15; d++){
		for(int i = 1001; i <= 10000; i++){
			snuke.push_back(i * D[d-4] - 1);
		}
	}
	int L = snuke.size(); 
	long double S[114514]; 
	for(int i = L-2; i >= 0; i--){
		S[i] = (long double) snuke.at(i) / ketawa(snuke.at(i));
	}
	long double saishou = snuke.at(L-1) / ketawa(snuke.at(L-1)); 
	vector<long long int> final; 
	final.push_back(snuke.at(L-1)); 
	for(int i = L-2; i >= 0; i--){
		S[i] = (long double) snuke.at(i) / ketawa(snuke.at(i)); 
		if(S[i] <= saishou){final.push_back(snuke.at(i));
			saishou = S[i]; 
			}	
	}
	int M = final.size(); 
	for(int i = 0; i < K; i++){
		cout << final.at(M - 1 - i) << endl; 
	}
}

Measured memory_bytes: 1400
n = gets.to_i
n.times do
  stop1, stop2 = gets.split.map(&:to_i)
  route = (stop1 < stop2 ? (stop1..stop2) : (stop2..stop1)).to_a
  case stop1
  when 0..5
    route.reverse! if stop2 < stop1
  when 6..9
    if (0..5).include?(stop2)
      route = (stop1..9).to_a + (stop2..5).to_a.reverse!
    elsif stop2 < stop1
      route = (stop1..9).to_a + (0..5).to_a.reverse! + (1..4).to_a + (5..stop2).to_a
    end
  end

  puts route.join(' ')
end

Measured memory_bytes: 6112
#include <cstdio>
using namespace std;

bool u[400][400];
bool die[400];
int x[100005], y[100005];
int n,m;

int main()
{
  scanf("%d%d", &n, &m);  
  for (int i = 0; i < m;i++) {
    scanf("%d%d", x+i, y+i);
  }

  for (int i = 0; i < n;i++) u[i][i] = true;

  for (int i = m-1; i>=0; i--) {
    int p = x[i] - 1, q = y[i] - 1;
    for (int j = 0; j < n; j++) {
      die[j] |= (u[p][j] && u[q][j]);
      u[p][j] = u[q][j] = (u[p][j] || u[q][j]);
    }
  }

  int ans = 0;
  for (int i = 0; i < n; i++)
  for (int j = i+1; j < n; j++) if (!die[i] && !die[j]) {
    ans++;
    for (int k = 0; k < n; k++) if (u[k][i] && u[k][j]) {
      ans--;
      break;
    }
  }
  printf("%d\n", ans);
  return 0;
}

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

int main() {
	long long int a,b;
	cin >> a >> b;
	if(a%b == 0){
	    cout << -1;
	    return 0 ;
	}
	long long ans = a;
	while(ans%b == 0){
	    ans += a;
	}
	cout << ans;
	return 0;
}

Measured memory_bytes: 256
#include<cstdio>
#include<vector>
int p[5005];
std::vector<int>g[5005];
int vis[5005];
long long f[5005], invf[5005];
long long size[5005];
long long dp[5005][5005];
const long long mod = 1e9+7;
const long long sign = mod-1;
long long cycle = 0;

long long power(long long a,long long b){
    long long res = 1;
    while(b){
        if(b&1) res = res*a%mod;
        a = a*a%mod;
        b /= 2;
    }
    return res;
}

long long inv(long long u){ return power(u,mod-2); }

void preWork(int n){
    f[0] = invf[0] = 1;
    for(int i = 1; i <= n; i++){
        f[i] = f[i-1]*(long long)i%mod;
        invf[i] = inv(f[i]);
    }
}

long long getC(int n,int m){
    if(m>n || m<0) return 0;
    else{
        long long res = f[n];
        res = res*invf[m]%mod;
        res = res*invf[n-m]%mod;
        return res;
    }
}

void dfs(int u,int t){
    vis[u] = t;
    for(int i = 0; i < (int)g[u].size(); i++){
        int v = g[u][i];
        if(!vis[v]) dfs(v,t);
    }
}

int getAmount(int n){
    int tag = 0, total = 0;
    for(int i = 1; i <= n; i++){
        if(!vis[i]){
            dfs(i,++tag);
            int find = -1, curSize = 0;
            for(int j = 1; j <= n; j++){
                if(vis[j]==tag){
                    curSize++;
                    if(p[j]==-1) find = 1;
                }
            }
            if(find==-1) cycle++;
            else size[++total] = curSize;
        }
    }
    return total;
}

int main(){
    long long n;
    scanf("%lld",&n);
    preWork(n);
    long long k = 0;
    for(int i = 1; i <= n; i++){
        int u;
        scanf("%d",&u);
        p[i] = u;
        if(u!=-1){
            g[u].push_back(i);
            g[i].push_back(u);
        }
        else k++;
    }
    int total = getAmount(n);
    //printf("total = %d, cycle = %lld\n",total,cycle);
    long long ans = power(n-1,k)*(n-cycle)%mod;
    //printf("ans = %lld\n",ans);
    dp[0][0] = 1;
    for(int i = 1; i <= total; i++){
        for(int j = 0; j <= total; j++){
            dp[i][j] = dp[i-1][j];
            if(j!=0) dp[i][j] = (dp[i][j]+dp[i-1][j-1]*size[i]%mod)%mod;
            //printf("dp[%d][%d] = %lld\n",i,j,dp[i][j]);
        }
    }
    dp[total][1] = 0;
    for(int i = 1; i <= total; i++) dp[total][1] = (dp[total][1]+(size[i]-1))%mod;
    for(int i = 1; i <= total; i++){
        //printf("i = %d\n",i);
        long long minus = power(n-1,k-i)*f[i-1]%mod;
        minus = minus*dp[total][i]%mod;
        minus = minus*sign%mod;
        //printf("minus = %lld\n",minus);
        ans = (ans+minus)%mod;
    }
    printf("%lld\n",ans);
    return 0;
}


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

int main()
{
	double t1, t2;
	string s;

	while (cin >> t1 >> t2)
	{
		if (t1 < 35.50 && t2 < 60 + 11) s = "AAA";
		else if (t1 < 37.50 && t2 < 60 + 17) s = "AA";
		else if (t1 < 40.0 && t2 < 60 + 23) s = "A";
		else if (t1 < 43.0 && t2 < 60 + 29) s = "B";
		else if (t1 < 50.0 && t2 < 60 + 45) s = "C";
		else if (t1 < 55.0 && t2 < 60 + 56) s = "D";
		else if (t1 < 60 + 10 && t2 < 60 * 2 + 28) s = "E";
		else s = "NA";

		cout << s << endl;
	}
}

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

int main() {
	int AC = 0, WA = 0, TLE = 0, RE = 0, count;
	cin >> count;
	string stat;
	for (int i = 0; i < count; i++) {
		cin >> stat;
		if (stat == "AC") {
			AC += 1;
		}
		else if (stat == "WA") {
			WA += 1;
		}
		else if (stat == "TLE") {
			TLE += 1;
		}
		else if (stat == "RE") {
			RE += 1;
		}
	}

	cout << "AC x " << AC << endl;
	cout << "WA x " << WA << endl;
	cout << "TLE x " << TLE << endl;
	cout << "RE x " << RE << endl;

	return 0;
}

Measured memory_bytes: 3556
#include <iostream>
#include <algorithm>

using namespace std;

int main() {
	int ans = 0;

	int p[5] , q[5], s[5], t =9;

	for(int i=0;i<5;i++)
		cin >> p[i];

	for (int i = 0; i < 5; i++) {
		q[i] = p[i] + 9;
		int k = q[i] / 10;
		ans += k * 10;

		s[i] = q[i] % 10;
		t = min(t, s[i]);
	}


	cout << ans -(9 - t);
}

Measured memory_bytes: 256
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <functional>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cassert>
#include <exception>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<P> vp;
#define rep(i,a,n) for(ll i = (a);i < (n);i++)
#define per(i,a,n) for(ll i = (a);i > (n);i--)
#define lep(i,a,n) for(ll i = (a);i <= (n);i++)
#define pel(i,a,n) for(ll i = (a);i >= (n);i--)
#define clr(a,b) memset((a),(b),sizeof(a))
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define sz size()
#define print(X) cout << (X) << endl
const ll INF = 1e+9+7;
ll n,m,l;
string s,t;
ll d[200010],dp[550][550];

int main(){
  cin >> n;
  vi vec;
  rep(i,0,n){
    cin >> m;
    vec.pb(m);
  }
  sort(all(vec));
  rep(i,0,vec.sz){
    rep(j,i+1,vec.sz){
      if(abs(vec[i]-vec[j]) % (n-1) == 0){
        cout << vec[i] << " " << vec[j] << endl;
        return 0;
      }
    }
  }
  return 0;
}


Measured memory_bytes: 3216
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep_lr(i,l,r) for(int i=(l);i<(r);i++)
#define all(x) (x).begin(),(x).end()
#define V vector
typedef V<int> vi;
typedef V<vi> vvi;
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<int, int, int> T;
constexpr int INF = INT_MAX >> 1;
constexpr ll LINF = 5000000000000000LL;
constexpr int MOD = 1000000007;

int main() {

	int n;
	cin >> n;
	vi w(n);
	rep(i, n)cin >> w[i];

	int ans = INF;
	rep(i, n - 1) {
		int l = 0, r = 0;
		rep_lr(j,0,i + 1)l += w[j];
		rep_lr(j, i + 1, n)r += w[j];
		ans = min(ans, abs(l - r));
	}
	cout << ans << endl;
}

Measured memory_bytes: 3620
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Main {
	InputStream is;
	PrintWriter out;
	String INPUT = "";

	void solve() {
		int a = ni(), b = ni();
		for(int i = 1; i <= 10000; i++) {
			if(i * 8 / 100 == a && i * 10 / 100 == b) {
				out.println(i);
				return;
			}
		}
		out.println(-1);
	}

	void run() throws Exception {
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);

		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if (!INPUT.isEmpty())
			tr(System.currentTimeMillis() - s + "ms");
	}

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

	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;

	private int readByte() {
		if (lenbuf == -1)
			throw new InputMismatchException();
		if (ptrbuf >= lenbuf) {
			ptrbuf = 0;
			try {
				lenbuf = is.read(inbuf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (lenbuf <= 0)
				return -1;
		}
		return inbuf[ptrbuf++];
	}

	private boolean isSpaceChar(int c) {
		return !(c >= 33 && c <= 126);
	}

	private int skip() {
		int b;
		while ((b = readByte()) != -1 && isSpaceChar(b))
			;
		return b;
	}

	private double nd() {
		return Double.parseDouble(ns());
	}

	private char nc() {
		return (char) skip();
	}

	private String ns() {
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	private char[] ns(int n) {
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while (p < n && !(isSpaceChar(b))) {
			buf[p++] = (char) b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}

	private char[][] nm(int n, int m) {
		char[][] map = new char[n][];
		for (int i = 0; i < n; i++)
			map[i] = ns(m);
		return map;
	}

	private int[] na(int n) {
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = ni();
		return a;
	}

	private int ni() {
		int num = 0, b;
		boolean minus = false;
		while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
			;
		if (b == '-') {
			minus = true;
			b = readByte();
		}

		while (true) {
			if (b >= '0' && b <= '9') {
				num = num * 10 + (b - '0');
			} else {
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private long nl() {
		long num = 0;
		int b;
		boolean minus = false;
		while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
			;
		if (b == '-') {
			minus = true;
			b = readByte();
		}

		while (true) {
			if (b >= '0' && b <= '9') {
				num = num * 10 + (b - '0');
			} else {
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private static void tr(Object... o) {
		System.out.println(Arrays.deepToString(o));
	}
}

Measured memory_bytes: 23124
#include<cstdio>
#include<cmath>
#include<climits>
#include<algorithm>
#include<sstream>
#include<iostream>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<list>
#include<map>
#include<vector>

using namespace std;


#define rep(i,n) for(int i=0; i<n; i++)
#define rept(i,a,b) for(int i=a; i<b; i++)
#define ll long long
#define MOD 1000000007

#define IMIN INT_MIN;
#define IMAX INT_MAX;


int main(){
    ll N, K;
    cin >> N >> K;
    vector<ll> H(N);
    rep(i, N) cin >> H[i];
    sort(H.begin(), H.end());
    ll ans=0;
    rep(i, N-K) ans += H[i];
    cout << ans << endl;


	return 0;
}



Measured memory_bytes: 1792
#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=1LL<<61;

int main() {
  ll n;
  cin >> n;
  ll f=0;
  ll tmp=n;
  while(tmp) {
    f+=tmp%10;
    tmp/=10;
  }
  if(n%f==0) cout << "Yes" << endl;
  else cout << "No" << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
using namespace std;
int main(){
	int H, W, d;
	cin >> H >> W >> d;
	if (d % 2 == 1){
		for (int i = 0; i < H; i++){
			for (int j = 0; j < W; j++){
				if ((i + j) % 2 == 0){
					cout << 'R';
				} else {
					cout << 'Y';
				}
			}
			cout << endl;
		}
	} else {
		for (int i = 0; i < H; i++){
			for (int j = 0; j < W; j++){
				int i2, j2;
				if ((i + j) % 2 == 0){
					i2 = i;
					j2 = j;
				} else {
					i2 = i;
					j2 = j + 1;
				}
				int A = ((i2 + j2) / 2 % d + d) % d;
				int B = ((i2 - j2) / 2 % d + d) % d;
				if (A < d / 2){
					if (B < d / 2){
						cout << 'R';
					} else {
						cout << 'Y';
					}
				} else {
					if (B < d / 2){
						cout << 'G';
					} else {
						cout << 'B';
					}
				}
			}
			cout << endl;
		}
	}
}

Measured memory_bytes: 3640
#define _USE_MATH_DEFINES

#include <cmath>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <limits>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <bitset>

using namespace std;

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

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


int main() {
    /*
    ios::sync_with_stdio(false);
    cin.tie(0);
     */
    int n;
    string s;
    cin >> n >> s;
    int sum = 0, l = 0;
    for(auto &i : s){
        if (i == '(') ++sum;
        else if (sum == 0) ++l;
        else --sum;
    }
    for (int i = 0; i < l; ++i) {
        cout << '(';
    }
    cout << s;
    for (int i = 0; i < sum; ++i) {
        cout << ')';
    }
    cout << endl;
}

Measured memory_bytes: 256
// ※※※ 解答不能 ※※※
// pekempey氏.
// https://atcoder.jp/contests/abc133/submissions/6313403
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)

using namespace std;
using ll = long long;

struct HLD {
  vector<int> label, parent, head;

  HLD(const vector<vector<int>> &g) : label(g.size()), parent(g.size()), head(g.size()) {
    const int n = g.size();
    vector<int> size(n, 1);
    auto dfs = [&](auto dfs, int u, int p) -> void {
      for (int v : g[u]) if (v != p) {
        dfs(dfs, v, u);
        size[u] += size[v];
      }
    };
    dfs(dfs, 0, -1);
    int k = 0;
    auto dfs2 = [&](auto dfs, int u, int p, int h) -> void {
      label[u] = k++;
      head[u] = h;
      parent[u] = p;
      pair<int, int> heavy(-1, -1);
      for (int v : g[u]) if (v != p) heavy = max(heavy, {size[v], v});
      if (heavy.first != -1) dfs(dfs, heavy.second, u, h);
      for (int v : g[u]) if (v != p && v != heavy.second) dfs(dfs, v, u, v);
    };
    dfs2(dfs2, 0, -1, 0);
  }

  int lca(int u, int v) {
    for (;;) {
      if (label[u] > label[v]) swap(u, v);
      if (head[u] == head[v]) return u;
      v = parent[head[v]];
    }
  }
};

#define getchar getchar_unlocked
#define putchar putchar_unlocked

int i32() {
  int n, c;
  while ((c = getchar()) < '0') if (c == EOF) return -1;
  n = c - '0';
  while ((c = getchar()) >= '0') n = n * 10 + c - '0';
  return n;
}

void print(int n) {
  int res[11], i = 0;
  do { res[i++] = n % 10, n /= 10; } while (n);
  while (i) putchar(res[--i] + '0');
  putchar('\n');
}

int main() {
  int n = i32();
  int q = i32();
  vector<vector<int>> g(n);
  struct edge {
    int v, c, w;
  };
  vector<vector<edge>> g2(n);
  rep(i, n-1) {
    int u = i32() - 1;
    int v = i32() - 1;
    int c = i32();
    int w = i32();
    g[u].push_back(v);
    g[v].push_back(u);
    g2[u].push_back((edge){v, c, w});
    g2[v].push_back((edge){u, c, w});
  }
  HLD hld(g);
  vector<ll> ans(q);
  struct query {
    int x, y;
    int coeff;
    int i;
  };
  vector<vector<query>> qs(n);
  rep(i, q) {
    int x = i32();
    int y = i32();
    int u = i32() - 1;
    int v = i32() - 1;
    qs[u].push_back((query){x, y, 1, i});
    qs[v].push_back((query){x, y, 1, i});
    qs[hld.lca(u, v)].push_back((query){x, y, -2, i});
  }
  ll dist = 0;
  vector<ll> distc(n);
  vector<ll> cnt(n);
  auto dfs = [&](auto dfs, int u, int p) -> void {
    for (query q : qs[u]) {
      ans[q.i] += q.coeff * (dist - distc[q.x] + cnt[q.x] * q.y);
    }
    for (edge e : g2[u]) {
      if (e.v == p) continue;
      distc[e.c] += e.w;
      cnt[e.c]++;
      dist += e.w;
      dfs(dfs, e.v, u);
      dist -= e.w;
      cnt[e.c]--;
      distc[e.c] -= e.w;
    }
  };
  dfs(dfs, 0, -1);
  rep(i, q) {
    print(ans[i]);
  }
}

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

int main() {
	ll a,b,c;
	cin>>a>>b>>c;
	if(c-a-b<=0){
		cout << "No";
	}
	else if(4*a*b>=c*c+a*a+b*b-2*a*c+2*a*b-2*b*c){
		cout << "No";
	}
	else{
		cout << "Yes";
	}
	// your code goes here
	return 0;
}

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

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
//#pragma GCC optimize ("-O3") 
#ifdef YANG33
#include "mydebug.hpp"
#else
#define DD(x) 
#endif
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  __MAKE_TIME__  Problem: __PROBLEM__ / Link: __CONTEST_URL__  ----- */
/* ------問題------



-----問題ここまで----- */
/* -----解説等-----



----解説ここまで---- */

LL ans = 0LL;

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

	int H, W; cin >> H >> W;
	FOR(i, 0, H) {
		string s; cin >> s;
		cout << s << endl;
		cout << s << endl;
	}

	return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2")
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
typedef long long ll;

int n;
string stat[5] = {"", "AC", "WA", "TLE", "RE"};
int cnt[5];

int main()
{
	ios::sync_with_stdio(false);
    cin.tie(0);
	cout.tie(0);
	cin >> n;
	
	string s;
	
	for (int i = 1; i <= n; i++)
	{
		cin >> s;
		if (s == "AC") cnt[1]++;
		if (s == "WA") cnt[2]++;
		if (s == "TLE") cnt[3]++;
		if (s == "RE") cnt[4]++;
	}
	
	for (int i = 1; i <= 4; i++)
		cout << stat[i] << " x " << cnt[i] << endl;

	return 0;
}


Measured memory_bytes: 3656
#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef priority_queue<int> PQ;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }

#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 8223372036854775807
#define mod 1000000007
//vector<vector<int> > dp;
//vector<vector<vector<int> > > vvvi;
//dp=vector<vector<int> >(N, vector<int>(M,0));
//vector<pair<int,int> > v;
//v.push_back(make_pair(x,y));
//priority_queue<int,vector<int>, greater<int> > q2;

int N;
VI X;
int all, ave, amari;
int size[5010], sum[5010];
int dp[5010][5010];
int sdp[5010][5010];
vector<VI>G;

void solve(int now, int pa) {
	VI chs;
	size[now] = 1;
	sum[now] = X[now];

	fore(ch, G[now]) {
		if (ch == pa)continue;
		solve(ch, now);
		chs.push_back(ch);
		size[now] += size[ch];
		sum[now] += sum[ch];
	}
	eREP(i, chs.size())eREP(j, size[now])sdp[i][j] = LLINF;
	
	sdp[0][0] = 0;
	int cur = 0;

	REP(i, chs.size()) {
		int ch = chs[i];
		eREP(j, cur) {
			eREP(k, size[ch]) {
				chmin(sdp[i + 1][j + k], sdp[i][j] + dp[ch][k]);
			}
		}
		cur += size[ch];
	}

	eREP(k, size[now]) {
		chmin(dp[now][k], sdp[chs.size()][k] + abs(ave*size[now] + k - sum[now]));
		chmin(dp[now][k + 1], sdp[chs.size()][k] + abs(ave*size[now] + k + 1 - sum[now]));
	}
}






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

	cin >> N;
	X.resize(N);
	G.resize(N);
	all = 0;
	REP(i, N) {
		cin >> X[i];
		all += X[i];
	}
	ave = all / N;
	amari = all % N;

	REP(i, N - 1) {
		int a, b; cin >> a >> b; a--; b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	REP(i, N)eREP(j, N)dp[i][j] = LLINF;
	solve(0,-1);
	cout << dp[0][amari] << endl;

	return 0;
}




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

int main()
{

	int H, W;
	cin >> H >> W;
	char s[50][50];
	for (int i = 0; i < H; i++)
	{
		for (int j = 0; j < W; j++)
		{
			cin >> s[i][j];
		}
		cout << endl;
	}
	int ans[50][50] = {};//配列の宣言と初期化
	for (int i = 0; i < H; i++)
	{
		for (int j = 0; j < W; j++)
		{
			if (s[i][j] == '.')
			{
				if (i > 0)
				{
					if (s[i - 1][j] == '#')
					{
						ans[i][j]++;
					}
				}
				if (i > 0 && j > 0)
				{
					if (s[i - 1][j - 1] == '#')
					{
						ans[i][j]++;
					}
				}
				if (i > 0 && j < W - 1)
				{
					if (s[i - 1][j + 1] == '#')
					{
						ans[i][j]++;
					}
				}
				if (i < H - 1)
				{
					if (s[i + 1][j] == '#')
					{
						ans[i][j]++;
					}
				}
				if (i<H-1&&j>0)
				{
					if (s[i+1][j-1]=='#')
					{
						ans[i][j]++;
					}
				}
				if (i < H - 1 && j<W-1)
				{
					if (s[i + 1][j + 1] == '#')
					{
						ans[i][j]++;
					}
				}
				if (j > 0)
				{
					if (s[i][j - 1] == '#')
					{
						ans[i][j]++;
					}
				}
				if (j < W - 1)
				{
					if (s[i][j + 1] == '#')
					{
						ans[i][j]++;
					}
				}
			}
			else
			{
				ans[i][j] = 0;
			}
			//
			if (s[i][j]=='.')
			{
			cout << ans[i][j];
			}
			else
			{
				cout << '#';
			}
		}
		cout << endl;
	}
}

Measured memory_bytes: 256
a, b, c, d, e = map(int, input().split())
x = 0
if a == 0:
    x =1
if b == 0:
    x =2
    
if c == 0:
    x =3
    
if d == 0:
    x =4
    
if e == 0:
    x =5
    
print(x)

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

int main(){
  int N;
  int G;
  cin >> N >> G;
  int n_problems = 100000000;
  vector<pair<int,int>> pairs(N);
  rep(i,N){
    cin >> pairs[i].first >> pairs[i].second;
    //cout << pairs[i].first << pairs[i].second;
  }
  //cout << endl;
  //cout << pairs[0].first<< endl;
  for(int bit=0;bit<(1<<N);bit++){
    int sum = G;
    int n_prob2=0;
    for(int i=0;i<N;i++){
      if(bit&(1<<i)){
      	n_prob2+=pairs[i].first;
      	sum-= pairs[i].first*100*(i+1)+pairs[i].second;
        //cout << i <<endl << pairs[0].first << endl;
      }
    }
    //cout << sum << endl;
    if(sum>=0){
      for(int i=N-1;i>=0;i--){
        //cout << i << endl;
        if(!(bit&(1<<i))){
          //cout << i << endl;
          int a = pairs[i].first;
          while(sum>0 && a>1){
            	sum -= (i+1)*100;
            	a--;
            	n_prob2++;
              //cout << "問題残り　" << a << "SUM" << sum<< "nprob" << n_prob2 << endl;
            }//cout << sum << endl;
          }
        }
      }
      //cout << sum << " " << n_prob2 << endl;
      if(sum<=0){
        n_problems = min(n_problems,n_prob2);
      }
    }
  
  cout << n_problems << endl;

  return 0;
}

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

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

 
   if (N % 500 <= A) {
    cout << "Yes" << endl;
   }
  else if (N % 500 >= A) {
    cout << "No" << endl;
   }
  
}

Measured memory_bytes: 256
from collections import namedtuple

Shop = namedtuple('Shop', ('a', 'b'))

def main():
    n, m = map(int, input().split())
    shops = []
    for _ in range(n):
        a, b = map(int, input().split())
        shops.append(Shop(a, b))
    
    print(solve(shops, m))

def solve(shops, m):
    cost = 0
    count = 0

    for shop in sorted(shops):
        if count + shop.b >= m:
            cost += shop.a * (m - count)
            return cost
        
        count += shop.b
        cost += shop.a * shop.b

main()


Measured memory_bytes: 18896
a, b = map(int, input().split())
if a == 1 and b == 1:
    print('Draw')
else:
    if a == 1:
        print('Alice')
    elif b == 1:
        print('Bob')
    else:
        if a == b:
            print('Draw')
        elif a < b:
            print('Bob')
        else:
            print('Alice')


Measured memory_bytes: 2940
#include<stdio.h>
#include<memory.h>
#define N 100001

typedef struct{
  int A;
  char B;
} card;

card array[N], arrayMarge[N];

int Partition(card *,int, int);
void Quicksort(card *, int, int);
void Merge(card *,int,int,int);
void MergeSort(card *, int, int);

int n;

int main(){

  int i;

  scanf("%d\n",&n);

  for(i = 0; i < n; i++){
    scanf("%c %d\n",&array[i].B,&array[i].A);
  }

  memcpy(arrayMarge,array,sizeof(array));
  Quicksort(array, 0, n-1);
  MergeSort(arrayMarge, 0, n);
  
  if (memcmp(arrayMarge,array,sizeof(array)) == 0) printf("Stable\n");
  else printf("Not stable\n");
  
  for(i = 0; i < n; i++){
    printf("%c %d\n",array[i].B,array[i].A);

  }

   return 0;
	
}

int Partition(card *A, int p, int r){

  int i,j,x;
  card T;
 
  x = A[r].A;
  i = p-1;

  for(j = p; j < r; j++){
    
    if(A[j].A <= x){
		i = i + 1;
		T = A[i]; 
		A[i] = A[j];
		A[j] = T;
	}
	  
  }
      T = A[i+1]; 
      A[i+1] = A[r];
	  A[r] = T;
      return i + 1;
	
}


void Quicksort(card *A, int p, int r){
  
  int q;  
  
  if(p < r){
    q = Partition(A, p, r);
    Quicksort(A, p, q-1);
    Quicksort(A, q+1, r);
  }
  
} 
 
void Merge(card *T,int left,int mid,int right){
    
  int i, j = 0, k;
  int n1 = mid - left;
  int n2 = right - mid;
  card L[N], R[N];
   
  for (i = 0; i < n1; i++){
    L[i] = T[left + i];
  }
   
  for(i = 0; i < n2; i++){
    R[i] = T[mid + i];
  }

  L[n1].A=1000000000;
  R[n2].A=1000000000;
  
	i = 0;
	j = 0;
	
  for(k = left; k < right; k++){
    if (L[i].A <= R[j].A){
      T[k] = L[i];
      i = i + 1;
    } else {
      T[k] = R[j];
      j = j + 1;
    }
  }
   
}
 
void MergeSort(card *A, int left, int right){

  int mid = 0;

  if(left + 1 < right){
    mid = (left + right)/2;
    MergeSort(A, left, mid);
    MergeSort(A, mid, right);
    Merge(A, left, mid, right);
  }
   
}

Measured memory_bytes: 2776