akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
typedef pair<int, int> P;
typedef pair<P, int> T;
#define INF (1 << 30)

vector<T> coner;
int ax, ay, bx, by, n, r, p;
int round, area, left_side, right_side, bottom, top;
int imos[3][10500];
int main(){
    while(cin >> n >> r, n | r){
	for(int i = 0;i < 3;i++)
	    fill(imos[i], imos[i] + 10500, 0);
		
		
	round = area = 0;
	p = 0;
	left_side = bottom = 10500;
	right_side = top = 1;
	coner.clear();
	for(int i = 0;i < n;i++){
	    cin >> ax >> ay >> bx >> by;
	    left_side = min(left_side, ++ax);
	    right_side = max(right_side, ++bx);
	    bottom = min(bottom, ++ay);
	    top = max(top, ++by);
	    
	    coner.push_back(T(P(ax, ay), 1));
	    coner.push_back(T(P(ax, by), -1));
	    coner.push_back(T(P(bx, ay), -1));
	    coner.push_back(T(P(bx, by), 1));
	}
	coner.push_back(T(P(INF, INF), 0));
	sort(coner.begin(), coner.end());
	for(int i = left_side;i < right_side + 2;i++){
	    for(int j = bottom;j < top + 2;j++){
		imos[i % 3][j] = imos[i % 3][j - 1];
		while(coner[p].first == P(i, j)){
		    imos[i % 3][j] += coner[p++].second;
		}
	    }
	    for(int j = bottom;j < top + 2;j++){
		imos[i % 3][j] += imos[(i - 1) % 3][j];
		if(imos[i % 3][j])area++;
		if(!!imos[i % 3][j] != !!imos[i % 3][j - 1])round++;
		if(!!imos[i % 3][j] != !!imos[(i - 1) % 3][j])round++; 
	    }

	}
	cout << area << endl;
	if(r == 2)cout << round << endl;
    }
    return 0;
}

Measured memory_bytes: 2164
def count(x1,y1,x2,y2):
  ret=acum[y2][x2]
  if x1!=0:
    ret-=acum[y2][x1-1]
  if y1!=0:
    ret-=acum[y1-1][x2]
  if x1!=0 and y1!=0:
    ret+=acum[y1-1][x1-1]
  return ret

h,w,k=map(int,input().split())
board=[list(input()) for _ in range(h)]
acum=[[0]*(w+1) for _ in range(h)]
for i in range(h):
  for j in range(w):
    if board[i][j]=='1':
      acum[i][j]+=1
    if i!=0:
      acum[i][j]+=acum[i-1][j]
    if j!=0:
      acum[i][j]+=acum[i][j-1]
    if i!=0 and j!=0:
      acum[i][j]-=acum[i-1][j-1]
    if j==w:
      acum[i][j]=acum[i][j-1]
ans=10**18
for i in range(2**(h-1)):
  hlist=[]
  flag=format(i,'b')
  if len(flag)!=(h-1):
    flag='0'*((h-1)-len(flag))+flag
  for j in range((h-1)):
    if flag[j]=='1':
      hlist.append(j)
  hlist.append(h-1)
  prevx=0
  tmp=len(hlist)
  yoko=tmp-1
  tate=0
  for j in range(w):
    prevy=0
    for l in range(tmp):
      if count(prevx,prevy,j,hlist[l])>k:
        tate+=10**18
      if count(prevx,prevy,j,hlist[l])<=k and count(prevx,prevy,j+1,hlist[l])>k:
        tate+=1
        prevx=j+1
        break
      prevy=hlist[l]+1
  ans=min(ans,yoko+tate)
print(ans)

Measured memory_bytes: 50648
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main 
{
	public static void main(String[] Arguments) throws IOException 
	{
		BufferedReader BR = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer ST = new StringTokenizer(BR.readLine());
		int cSum = 0, N = Integer.parseInt(ST.nextToken()), M = Integer.parseInt(ST.nextToken()), Count = 0, itemVotes[] = new int[N];
		double Result = 0.00, Barrier = (double)1 / (double)100001;
		ST = new StringTokenizer(BR.readLine());
		for (int i = 0; i < N; i++)
		{
			itemVotes[i] = Integer.parseInt(ST.nextToken());
			cSum += itemVotes[i];
		}
		Result = (double)1 / (double)(4 * M);
		Result *= (double)cSum;
		for (int i = 0; i < N; i++)
		{
			if (itemVotes[i] - Result >= (double)0.000000)
			{
				Count++;
			}
		}
		System.out.println((Count >= M) ? "Yes" : "No");
	}
}


Measured memory_bytes: 21460
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack> // s.top()
#include <bitset>
#include <queue> // s.front()
using namespace std;

typedef long long Int;
typedef pair<Int,Int> P;
#define print(x) cout<<(x)<<endl

#define INF10 10000000000
#define INF5 100000
#define EPS 1e-10
#define MOD 1000000007


int a[10];

int cost(int i, int j) {
	return abs(a[i] - a[j]);
}

int main() {
	int r;
	cin >> r;
	if (r < 1200)print("ABC");
	else if (r < 2800)print("ARC");
	else print("AGC");




	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define IO std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL);
#define FIN freopen("entrada.txt", "r", stdin);
#define FOUT freopen("saida.sol", "w", stdout);

#define MOD 1000000007
#define ull unsigned long long int
#define ll  long long int
#define pii pair<int, int>
#define pb push_back
#define MAX int(5*10e5+2)

const int INF = 0x3f3f3f3f;
using namespace std;

int caso=1;

int main() {

   IO

   int n;
   string a;
   cin >> n >> a;

   if(a.size()>n)
      a=a.substr(0, n)+"...";

   cout << a << "\n";

   return 0;
}


Measured memory_bytes: 3644
using System;
using System.Collections.Generic;
using System.Linq;

public static class Program
{
    public static void Main(string[] args)
    {
        var n = int.Parse(Console.ReadLine());
        var cnt = 0;
        foreach (var item in Enumerable.Range(1, n))
        {
            var val = item.ToString().ToCharArray().Length;
            cnt = val % 2 == 1 ? cnt + 1 : cnt;
        }
        Console.WriteLine(cnt);
    }
}

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

using ll = long long;

ll gcd(ll n,ll k)
{
    if(k==0)return n;
    else return gcd(k,n%k);
}

ll lcm(ll n,ll k)
{
    ll g = gcd(n,k);
    return n/g*k;
}

int main()
{
    int n;
    cin>>n;
    ll arr[n+5];
    for(int i=0;i<n;i++)cin>>arr[i];

    ll sum=1;
    for(int i=0;i<n;i++)sum=lcm(sum,arr[i]);

    cout<<sum<<endl;
}


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

int main(void){
    int a = 0, b = 0, c = 0, d = 0; 

    scanf("%d%d%d%d", &a, &b, &c, &d);

    while(a > 0 || c || 0){
        c -= b;
        if(c <= 0){
            printf("Yes\n");
            break;
        }
        a -= d;
        if(a <= 0){
            printf("No\n");
            break;
        }
    }

    return 0;

} 


Measured memory_bytes: 3712
import bisect
N = int(input())
A = sorted(list(map(int,input().split())))
B = sorted(list(map(int,input().split())))
C = sorted(list(map(int,input().split())))
count = 0
for j in range(N):
    c = N-bisect.bisect_right(C,B[j])
    b = bisect.bisect_right(A,B[j]-1)
    count += b*c
print(count)

Measured memory_bytes: 23092
#include <bits/stdc++.h>
using namespace std;
int main(){
  string s;
  cin >> s;
  int ans;
  if(s == "SUN")ans = 7;
  else if(s == "MON")ans = 6;
  else if(s == "TUE")ans = 5;
  else if(s == "WED")ans = 4;
  else if(s == "THU")ans = 3;
  else if(s == "FRI")ans = 2;
  else if(s == "SAT")ans = 1;
  cout << ans << endl;
}

Measured memory_bytes: 384
import std.stdio;
import std.string;
import std.conv;
import std.array;

void main()
{
    auto reader = readln.split;
    int N = reader[0].to!int;
    int K = reader[1].to!int;
    string S = readln.chomp;

    int[] cnt;
    int beforeI = 0;
    if (S[0] == '0') cnt ~= 0;
    foreach (i; 0..N){
        if (S[i] != S[beforeI]){
            cnt ~= i - beforeI;
            beforeI = i;
        }
    }
    cnt ~= N - beforeI;
    if (S[N - 1] == '0') cnt ~= 0;

    ulong cntLen = cnt.length;
    int sumRange = K * 2 + 1;
    int sum = 0;
    foreach (i; 0..sumRange){
        if (i >= cntLen) break;
        sum += cnt[i];
    }

    int ans = sum;
    for (uint i = 2; i + sumRange - 1 < cntLen; i += 2){
        sum -= cnt[i - 2];
        sum -= cnt[i - 1];
        sum += cnt[i + sumRange - 2];
        sum += cnt[i + sumRange - 1];

        if (sum > ans) ans = sum;
    }

    writeln(ans);
}



Measured memory_bytes: 2556
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main(){
  ll n;
  cin >>n;
  ll a[n];
  for(ll i=0;i<n;i++) cin >>a[i];
  ll sum=0;
  ll ans=0,ans1=0;
  for(ll i=0;i<n;i++){
    if(i%2==0){
      sum+=a[i];
      if(sum==0) {
        sum=1;
        ans++;
      }
      if(sum<0) {
        ans+=(abs(sum)+1);
        sum=1;
      }
    }
    else{
      sum+=a[i];
      if(sum==0) {
        sum=-1;
        ans++;
      }
      if(sum>0) {
        ans+=(abs(sum)+1);
        sum=-1;
      }
    }
  }
  sum=0;
  for(ll i=0;i<n;i++){
    if(i%2==1){
      sum+=a[i];
      if(sum==0) {
        sum=1;
        ans1++;
      }
      if(sum<0) {
        ans1+=(abs(sum)+1);
        sum=1;
      }
    }
    else{
      sum+=a[i];
      if(sum==0) {
        sum=-1;
        ans1++;
      }
      if(sum>0) {
        ans1+=(abs(sum)+1);
        sum=-1;
      }
    }
  }
  cout <<min(ans,ans1)<<endl;
}

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

int main(){
  int A,B,C;
  cin>> A >> B >> C;
  if((A==7&&B==5&&C==5) | (A==5&&B==7&&C==5) | (A==5&&B==5&&C==7)){
    cout<<"YES"<<endl;
  }
  else{
    cout<<"NO"<<endl;
  }
}

Measured memory_bytes: 256
n,m=map(int,input().split())
memo={}
def primes(n):
    primefac = []
    d = 2
    while d*d <= n:
        while (n % d) == 0:
            if d in memo:
                memo[d]+=1
            else:
                memo[d]=1
            n //= d
        d += 1
    if n > 1:
        if n in memo:
            memo[n]+=1
        else:
            memo[n]=1
primes(m)

def cmb(n,r,mod):
    if (r<0 or r>n):
        return 0
    r = min(r,n-r)
    return g1[n]*g2[r]*g2[n-r]%mod

mod = 10**9+7
N = 10**5*2
g1 = [1,1]
g2 = [1,1]
inverse = [0,1]

for i in range(2,N+1):
    g1.append((g1[-1]*i)%mod)
    inverse.append((-inverse[mod%i]*(mod//i))%mod)
    g2.append((g2[-1]*inverse[-1])%mod)

ans=1
for x in memo.items():
    ans*=cmb(x[1]+n-1,x[1],mod)
    ans%=mod

print(ans)


Measured memory_bytes: 27092
l = str(input()).split('/')
if int(l[0]) < 2019:
    print('Heisei')
elif int(l[0]) == 2019:
    if int(l[1]) < 4:
        print('Heisei')
    elif int(l[1]) == 4 and int(l[2]) <=30:
        print('Heisei')
    else:
        print('TBD')
else:
    print('TBD')

Measured memory_bytes: 3060
#include<stdio.h>

int main(){
    char s[100];
    scanf("%s",&s);
    int cnt;
    for( cnt = 0 ; s[cnt] != '\0' ; cnt++){
    }
    int ans=0;
    
    int i = 0;
    while (i < (cnt - 1 - i)){
        if(s[i] != s[cnt - 1 -i]){
            ans++;
        }
        i++;
    }
    printf("%d",ans);
    return 0;
}


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

int main() {
  int N, Q;
  scanf("%d%d", &N, &Q);
  string S;
  cin >> S;

  vector<int> a(N, 0);
  for (int i = 0; i < N - 1; i++) {
    if (S.at(i) == 'A' && S.at(i + 1) == 'C') a.at(i + 1)++;
  }

  vector<int> sum(N + 1, 0);
  for (int i = 0; i < N; i++) {
    sum.at(i + 1) = sum.at(i) + a.at(i);
  }

  for (int i = 0; i < Q; i++) {
    int left = 0, right = 0;
    scanf("%d%d", &left, &right);
    printf("%d\n", sum.at(right) - sum.at(left));
  }
  return 0;
}

Measured memory_bytes: 1792
#include <queue>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <functional>
#include <stack>
#include <iterator>
#include <set>
#include <cmath>
#include <list>
#include <map>
#include <climits>
#include <numeric>
#include <iomanip>
#include <cstring>
using namespace std;
int n;
long long k;
int a[2000000];
int main()
{
    // freopen("in", "r", stdin);
    // freopen("out", "w", stdout);
    cin >> n >> k;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    long long t = 0;
    int i = 1, j = 1;
    bool sgn = false;
    for (; t < k;)
    {
        ++t;
        i = a[i];
        j = a[a[j]];
        if (i == j && !sgn)
        {
            long long tt = 1;
            i = a[i];
            while (i != j)
            {
                ++tt;
                i = a[i];
            }
            k = (k - t) % tt + t;
            sgn = true;
        }
    }
    cout << i;
    return 0;
}

Measured memory_bytes: 4416
import sys
sys.setrecursionlimit(10 ** 8)

def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    AC = WA = TLE = RE = 0
    N = Z()
    for _ in range(N):
        s = input()
        if s == 'AC': AC += 1
        elif s == 'WA': WA += 1
        elif s == 'TLE': TLE += 1
        else: RE += 1
    print('AC x {}'.format(AC))
    print('WA x {}'.format(WA))
    print('TLE x {}'.format(TLE))
    print('RE x {}'.format(RE))

    return

if __name__ == '__main__':
    main()


Measured memory_bytes: 9212
using System;
using System.Text;
using System.Collections.Generic;

namespace AOJ2
{
    class Program
    {


        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());

            int max = 0;
            int maxNum = 20;

            for (int i = 0; i < n; i++)
            {
                string[] inputArray = Console.ReadLine().Split(' ');
                if (int.Parse(inputArray[1]) > max)
                {
                    max = int.Parse(inputArray[1]);
                    maxNum = int.Parse(inputArray[0]);
                }
                else if (int.Parse(inputArray[1]) == max && int.Parse(inputArray[0]) < maxNum)
                {
                    maxNum = int.Parse(inputArray[0]);
                }
            }


            Console.WriteLine("{0} {1}", maxNum, max);

        }

        
    }
}

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

int main(void) {
    int a[100];
    int n;
    
    cin >> n;
    for(int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    
    cout << a[n - 1];
    for(int i = n - 2; i >= 0; --i) {
        cout << ' ' << a[i];
    }
    cout << endl;
    return 0;
}



Measured memory_bytes: 3080
text=input()
string=input()


#common_length[i]は、探索列がstring[i]で探索を終了したときに、
# string[i-common_lneght[i] + 1] 〜string[i]で構成される文字列が、探索価値を持つことを示す


len_string=len(string)
common_length=[0]*len_string
tmp=0
tmp_tmp=0
for i in range(1,len_string):
    if(string[i]==string[tmp]):
        tmp_tmp=tmp
        while(string[i]==string[tmp_tmp] and tmp_tmp >0):
            tmp_tmp=common_length[max([tmp_tmp-1,0])]
        common_length[i-1]=tmp_tmp
        tmp+=1
    else:
        common_length[i-1]=tmp
        if(string[i]==string[0]):
            tmp=1
        else:
            tmp=0

if(string[-1]==string[tmp-1]):
    common_length[-1]=tmp
common_length[0]=0


tmp=0

for i in range(len(text)):
    if(text[i]!=string[tmp]):
        while(text[i]!=string[tmp] and tmp>0):
            tmp=common_length[max([tmp-1,0])]
        if(text[i]==string[tmp]):
            tmp+=1
    else:
        tmp+=1
        if(tmp==len_string):
            print(i-tmp+1)
            tmp=common_length[-1]


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

int main(void){
    int i,n;
    cin >> n;
    int p[n+1];
    int **m = new int*[n+1];
    for(i=1; i<=n; i++){
        m[i] = new int[n+1];
    }
    for(i=1; i<=n; i++) cin >> p[i-1] >> p[i];
    for(i=1;i<=n;i++) m[i][i] = 0;
    for(int l=2; l<=n; l++){
        for(i=1; i<=n-l+1; i++){
            int j = i + l - 1;
            m[i][j] = (1<<21);
            for ( int k=i; k<=j-1; k++)
                m[i][j] = min(m[i][j], m[i][k] + m[k+1][j] + p[i-1] * p[k] * p[j]);
        }
    }
    cout << m[1][n] << endl;
    for(int i=1; i<n+1; i++){
        delete[] m[i];
        m[i] = 0;
    }
    delete[] m;
    m = 0;
    return 0;
}

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

using namespace std;

typedef long long ll;

ll dp[100][5005];
ll dp2[10][25000005];
ll ga[3];
ll gb[3];
int main() {
  ll n;
  cin >> n;

  for(int i = 0; i < 3; i++) cin >> ga[i];
  for(int i = 0; i < 3; i++) cin >> gb[i];

  for(int i = 0; i < 3; i++) {
    for(int j = 0; j <= n; j++) {
      if(j - ga[i] < 0) {
	dp[i+1][j] = dp[i][j];
      }
      else {
	dp[i+1][j] = max(dp[i][j],dp[i+1][j-ga[i]] + gb[i]-ga[i]);
      }
    }
  }

  ll m = 0;
  for(int i = 0; i <= n; i++) m = max(m,dp[3][i]);
  m += n;
  //cout << m << endl;
  for(int i = 0; i < 3; i++) {
    for(int j = 0; j <= m; j++) {
      if(j - gb[i] < 0) {
	dp2[i+1][j] = dp2[i][j];
      }
      else {
	dp2[i+1][j] = max(dp2[i][j],dp2[i+1][j-gb[i]] + ga[i]-gb[i]);
      }
    }
  }

  ll ans = 0;
  for(int i = 0; i <= m; i++) ans = max(ans,dp2[3][i]);

  cout << m + ans << endl;
      
}


Measured memory_bytes: 590080
r, c = map(int, input().split())
tbl = [[] for _ in range(r)]
cSum = [0 for _ in range(c+1)]

for i in range(r):
  tblS = input()
  tbl[i] = list(map(int, tblS.split()))
  print(tblS, end = '')
  print(' ', sum(tbl[i]), sep = '')

  for j in range(c):
    cSum[j] += tbl[i][j]
  cSum[c] += sum(tbl[i])

print(' '.join(map(str, cSum)))

Measured memory_bytes: 7728
#include<stdio.h>
int main()
{
    long long int n;
    int k,i=0,a;
    scanf("%lld %d",&n,&k);
    for( ;n!=0; )
    {
        a=n/k;
        n=a;
        i++;
    }
    printf("%d\n",i);
    return 0;


}


Measured memory_bytes: 128
import sys

n = int(input())
s = input()

tries = []

def count(S):
    cnt = 0
    for i in S:
        if i == "(":
            cnt += 1
        else:
            cnt -= 1
    return cnt

def valid(S):
    cnt = 0
    for i in S:
        if i == "(":
            cnt += 1
        else:
            cnt -= 1
        if cnt < 0:
            return False
    return True

def bt(S):
    cnt = count(S)
    if count(S) == 0 and valid(S):
        tries.append(S)
        return
    else:
        if cnt < 0:
            return bt("(" + S)
        else:
            return bt(S + ")")
bt(s)
print(min(tries))


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

int64_t gcd(int64_t m, int64_t n) {
  if (n == 0) return m;
  return gcd(n, m % n);
}

int64_t lcm(int64_t m, int64_t n) {
  return m / gcd(m, n) * n;
}

int main() {
  int64_t n, m;
  cin >> n >> m;
  vector<int64_t> a(n);
  for (int i = 0; i < n; i++) cin >> a.at(i);
  m *= 2;
  for (;;) {
    bool odd = false, even = false;
    for (int i = 0; i < n; i++) {
      if (a.at(i) % 2) {
        odd = true;
      } else {
        even = true;
      }
    }
    if (odd && even) {
      cout << 0 << endl;
      return 0;
    }
    if (odd) break;
    m /= 2;
    for (int i = 0; i < n; i++) a.at(i) /= 2;
  }
  int64_t l = 1;
  for (int i = 0; i < n; i++) {
    l = lcm(l, a.at(i));
    if (l > m) {
      cout << 0 << endl;
      return 0;
    }
  }
  cout << (m / l + 1) / 2 << endl;
}


Measured memory_bytes: 1152
#include <bits/stdc++.h>
using namespace std;
    vector<int> answer;
    int k=0;
int main(void){
    int match[10]={0,2,5,5,4,5,6,3,7,6};
    vector<int> num;
    vector<int> x;
    int dp[50001]={0};
    int use[8]={0};//マッチが示す値
    int n,m,a;
    cin >> n;
    cin >> m;
    for(int i=0;i<m;i++){
        cin >> a;
        x.push_back(a);
        num.push_back(match[a]);
    }
    for(int i=1;i<=n;i++){
        dp[i]=-1;
    }
    sort(num.begin(),num.end());
    for(int i=0;i<n;i++){
        for(int j=0;j<x.size();j++){
            if(n>=i+match[x[j]]){
                dp[i+match[x[j]]]=max(dp[i+match[x[j]]],dp[i]+1);
            }
        }
    }
    if(dp[n]==-1){
        cout << -1 << endl;
        return 0;
    }else{
        //for(int i=0;i<=n;i++){
        //    cout << dp[i] << " ";
        //}
        //cout << endl;
    }
 //   for(int i=0;i<num.size();i++){
 //       cout << num[i] << " " << use[num[i]] << endl;
 //   }
    sort(x.begin(),x.end());
    //for(int i=0;i<x.size();i++){
    //    cout << x[i] << " ";
    //}
    //cout << endl;
    while(n!=0){
        for(int i=x.size()-1;i>=0;i--){
            if(n-match[x[i]]>=0 && dp[n]==dp[n-match[x[i]]]+1){
                answer.push_back(x[i]);
                n=n-match[x[i]];
                break;
            }
        }
    }
 //   check(n,0);
 //   sort(answer.begin(),answer.end());
    for(int i=0;i<answer.size();i++){
        cout << answer[i];
    }
    cout << endl;
    return 0;
}

Measured memory_bytes: 512
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author alireza_kaviani
 */

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef pair<ld, ld> pld;
typedef pair<string, string> pss;

#define all(x)                      (x).begin(),(x).end()
#define Sort(x)                     sort(all((x)))
#define X                           first
#define Y                           second
#define Mp                          make_pair
#define tab                         '\t'
#define sep                         ' '
#define endl                        '\n'
#define debug(x)                    cerr << #x << " = " << x << endl
#define SZ(x)                       ll(x.size())
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define set_random                  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

inline ll input() {
    ll n;
    cin >> n;
    return n;
}

ll poww(ll a, ll b, ll md) {
    return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md));
}

set_random;

const ll MAXN = 110;
const ll MAXK = 1e5 + 10;
const ll INF = 8e18;
const ll MOD1 = 998244353;
const ll MOD2 = 1e9 + 7;
const ll MOD3 = 1e9 + 9;

ll n , w , ans , dp[MAXK];
pll p[MAXN];

int main() {
    fast_io;

    cin >> n >> w;
    for(ll i = 0 ; i < n ; i++) cin >> p[i].X >> p[i].Y;

    for(ll i = 0 ; i < n ; i++)
    {
        for(ll j = MAXK - 1 ; j >= 0 ; j--)
        {
            if(j + p[i].X < MAXK)
            {
                dp[j + p[i].X] = max(dp[j + p[i].X] , dp[j] + p[i].Y);
            }
        }
    }
    for(ll i = 0 ; i <= w ; i++)    ans = max(ans , dp[i]);
    cout << ans << endl;

    return 0;
}
/*
todo :
    1- set constants
    2- check TimeLimit and MemoryLimit
    3- check special test cases
    4- don't forget BS can help you that is good idea(use for loop for floats)
    5- don't forget printf and scanf can help you in your code speed
*/


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

int x[3],n;

int main(){
	while(cin >> x[0] >> x[1] >> x[2]){
		if(x[0]==0&&x[1]==0&&x[2]==0) break;
		sort(x,x+3);
		cin >> n;
		for(int i = 0; i < n; i++){
			int r; cin >> r;
			if(4*r*r > x[0]*x[0]+x[1]*x[1]) cout << "OK" << endl;
			else cout << "NA" << endl;
		}
	}
}

Measured memory_bytes: 1172
#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)
#define fi first
#define se second
#define INF 1000000009
#define LLINF 1000000000000000009LL
#define chmax(a, b) a = max(a, b)
using ll = long long;

int dp[100009][3];

int main(){
  int n;
  cin>>n;
  vector<int>a(n);
  vector<int>b(n);
  vector<int>c(n);
  rep(i,n)cin>>a[i]>>b[i]>>c[i];
  rep(i,n){
    rep(j,3){
      if(j==0){
        chmax(dp[i+1][1],dp[i][0]+b[i]);
        chmax(dp[i+1][2],dp[i][0]+c[i]);
      }
      if(j==1){
        chmax(dp[i+1][2],dp[i][1]+c[i]);
        chmax(dp[i+1][0],dp[i][1]+a[i]);
      }
      if(j==2){
        chmax(dp[i+1][0],dp[i][2]+a[i]);
        chmax(dp[i+1][1],dp[i][2]+b[i]);
      }
    }
  }
  int ans=0;
  rep(i,3){
    chmax(ans,dp[n][i]);
  }
  cout<<ans<<endl;
  return(0);
}

Measured memory_bytes: 2560
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define ll long long
#define rep(i,l,r) for(ll i=(l);i<(r);i++)
#define max(p,q) ((p)>(q)?(p):(q))
#define min(p,q) ((p)<(q)?(p):(q))
#define chmax(a,b) ((a)=(a)>(b)?(a):(b))
#define chmin(a,b) ((a)=(a)<(b)?(a):(b))
#define abs(p) ((p)>=(0)?(p):(-(p)))
int cmp(const void *a, const void *b);
//your code here!

int main(void){
    ll N, T, time[200001];
    scanf("%lld %lld", &N, &T);
    rep(i, 0, N) scanf("%lld", time+i);
    ll ans=T;
    for(ll i=N-1; i>=1; i--){
        ll tt=time[i] - time[i-1];
        if(tt>=T) ans += T;
        else ans += tt;
    }
    printf("%lld\n", ans);
    return 0;
}

int cmp(const void *a, const void *b){
    ll A=*(ll *)a, B=*(ll *)b;
    if(A==B)return 0;
    else return A>B ? 1:-1;//昇順ソート
}


Measured memory_bytes: 1664
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<vector>
#define inf 2147483647
#define rep(i,a,b) for(int i=a;i<b;i++)
#define  rr read()
#define ll long long
#define repb(i,a,b) for(int i=a;i<=b;i++)
#define CloseIo   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
int read()
{
    char ch = getchar();
	int x = 0, f = 1;
    while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
    while('0' <= ch && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
ll n,k;
const int N=1e5+5;
vector<ll>v[N];
ll vis[N]={0};
const int mod=1e9+7;
struct node
{
	 ll num,dis;
	 node(ll snum,ll sdis){
	 	num=snum;
	 	dis=sdis;
	 }
};
ll ans;
void bfs()
{
	queue<node>q;
    
	
	q.push(node(1,0));
	while(!q.empty())
	{
		ll t=q.front().num,dis=q.front().dis+1;
		vis[t]=1;
		q.pop();
		ll cnt=0;
		for(ll i=0;i<v[t].size();i++)
		{    ll vv=v[t][i];
		     if(vis[vv])continue;
		     

		     
		     if(dis==1)
			 {ans=ans*(k-(cnt++)-1)%mod;
			 
			 }
			 else
			 { ans=ans*(k-(cnt++)-2)%mod;
			 
			 }
			
			 q.push(node(vv,dis));
			 //printf("--%lld\n",ans);
		}

	}
}
int main()
{
   memset(vis,0,sizeof(vis));
   CloseIo;
   cin>>n>>k;
   ans=k;
   
   repb(i,1,n-1)
   {
   	ll x,y;
   	cin>>x>>y;
   	v[x].push_back(y);
   	v[y].push_back(x);
   	
   }
   
  
   bfs();
//   repb(i,1,n){
//   	printf("%lld ",z[i].c);
//   }
//   
cout<<ans;
}




Measured memory_bytes: 8948
#include <cstdio>
#define INF 1e9
#define eps 1e-6
typedef long long ll;

int n, a[110], sum;

int main(){

	scanf("%d", &n);
	for(int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	for(int i = 1; i < n; i++)
		for(int j = i + 1; j <= n; j++)
			sum += a[i] * a[j];
	printf("%d\n", sum);

	return 0;
}


Measured memory_bytes: 128
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define repit(it, li) for(auto it=li.begin(); it!=li.end(); it++)
using namespace std;
using ll = long long;
using P = pair<int,int>;

int main(){
   int h, n;
   cin>>h>>n;
   int sum=0;
   rep(i, n){
      int a;
      cin>>a;
      sum+=a;
   }

   if(sum>=h) cout<<"Yes"<<endl;
   else cout<<"No"<<endl;
   return 0;
}


Measured memory_bytes: 3588
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;	

int main() {
	ll x, y, z;
	cin >> x >> y >> z;
	
	ll ans = (x - z) / (y + z);
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3600
#include<stdio.h>
int main()
{
  int k,r,r1,i,ct=1,f=0;
  scanf("%d",&k);
  r1=7%k;
  r=r1;
  r*=10;
  r+=7;
  i=k;
  if(r1==0)
  {
      printf("1");
      return 0;
  }
  while(i--)
  {
    ct++;
    if(r%k==0)
    {
      f=1;
      break;
    }
    else
    {
      r%=k;
  	  r*=10;
  	  r+=7;
    }
    if(r==r1) break;
  }
  if(f==0) printf("-1");
  else printf("%d",ct);
  return 0;
}

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

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

using Pi = pair<int, int>;
using Tapris = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  string s;
  cin >> s;
  string t = s;
  t += '$';
  reverse(all(t));
  int n = t.size();
  vint dp(n+1, inf);
  vint last(26, 0);
  vector<Pi> rev(n+1);
  dp[0] = 0;
  dp[1] = 1;
  rev[0] = Pi(-1, 0);
  rev[1] = Pi(-1, 'a');
  reps(i, 1, n) {
    last[t[i]-'a'] = i;
    rep(j, 26) {
      if(dp[i+1] > dp[last[j]]+1) {
	dp[i+1] = dp[last[j]]+1;
	rev[i+1] = Pi(last[j], 'a'+j);
      }
    }
  }
  int idx = n;
  while(idx > 0) {
    cout << (char)rev[idx].second;
    idx = rev[idx].first;
  }
  cout << endl;

  return 0;
}


Measured memory_bytes: 5524
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i,n) for(i=0;i<n;i++)
#define loop(i,a,n) for(i=a;i<n;i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef pair<int,int> pii;

int main(void) {
  int i,j;
  int n,q;
  while(1){
    cin>>n>>q;
    if(n==0 && q==0)break;
    vi v(105,0);
    rep(i,n){
      int m,a;
      cin>>m;
      rep(j,m){
	cin>>a;
	v[a]++;
      }
    }
    int ans=0;
    for(i=1;i<=100;i++)
      if(v[i]>v[ans] && v[i]>=q)ans=i;
    cout<<ans<<endl;
  }
}

Measured memory_bytes: 3052
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<functional>
#include<iostream>
#include<list>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<string>
#include<utility>
#include<vector>

#define INF 1000000007
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define CLR(a) memset((a), 0, sizeof(a))

using ll = long long;
using ull = unsigned long long;
using namespace std;

ll gcd(ll a, ll b){
    if (a < b) {
        a ^= b;
        b ^= a;
        a ^= b;
    }
    return b ? gcd(b, a % b) : a;
}

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


inline int toInt(string s){
    int v;
    istringstream sin(s);
    sin >> v;
    return v;
}

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



int main(void){

    ll n;
    cin >> n;

    pair<ll, ll> task[n];
    FOR(i,0,n){
        ll cost, dead;
        cin >> cost >> dead;
        task[i] = make_pair(dead, cost);
    }

    sort(task, task+n);

    ll now = 0;
    FOR(i,0,n){
        now += task[i].second;
        //cout <<"dead:now=" << task[i].first << ":" << now << endl;
        if(task[i].first<now){
            cout << "No" << endl;
            return 0;
        }
    }

    cout << "Yes" << endl;

    return 0;
}


Measured memory_bytes: 3328
#include <iostream>
#include <bits/stdc++.h>
#include <iomanip>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cctype>
#include <cassert>
#include <climits>
#include <string>
#include <bitset>
#include <cfloat>
#include <unordered_set>
#include <cstring>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<ll>> vvll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define fin(ans) cout << (ans) << '\n'
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define Rort(a) sort(a.rbegin(), a.rend())
#define MATHPI acos(-1)
#define itn int;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return 1;
    }
    return 0;
}
struct io
{
    io()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
    }
};
const int INF = INT_MAX;
const ll LLINF = 1LL << 60;
const ll MOD = 1000000007;
const double EPS = 1e-9;

int main(void)
{
    int n;
    cin >> n;
    
    
    
    
    vector<ll> a(n);
    
    for(int i = 0; i < n; ++i) cin >> a[i];
    
    sort(a.begin(), a.end());
    
    __int128 r = a[0];
    
    
    for (int i = 1; i < n; i++) {
        
        if (r * a[i] > (__int128)1e18) {
            cout << -1;
            return 0;
        }
        r *= a[i];
    }
    cout << ll(r);
    
    
   
    
}

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

int main() {
  int a, b;
  cin >> a >> b;
  int r;
  if (a >= 13) {
    r = b;
  } else if (a >= 6) {
    r = b / 2;
  } else {
    r = 0;
  }
  cout << r << endl;
}


Measured memory_bytes: 256
use std::io::Read;

fn main() {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    
    let mut iter = buf.split_whitespace();
    
    let n: usize = iter.next().unwrap().parse().unwrap();
    let m: usize = iter.next().unwrap().parse().unwrap();
    
    if n >= m {
        println!("0");
        return;
    }
    
    let mut points: Vec<i32> = iter.map(|p| p.parse::<i32>().unwrap())
        .collect();
    points.sort();
    
    let mut subs: Vec<i32> = points.windows(2)
        .map(|s| s[1] - s[0])
        .collect();
    subs.sort();
    
    let result: i32 = subs.iter()
        .take(m-n)
        .sum();
        
    println!("{}", result);
}

Measured memory_bytes: 6396
#include <stdio.h>
int main(void){
	int n;
	int num[100];
	
	int key;
	
	int i,j,k;
	//input n
	scanf("%d",&n);
	
	//intput numbers
	for(i = 0;i < n;i++){
		scanf("%d",&num[i]);
	}
	//sort
	for(j = 0;j < n;j++){
		key = num[j];
		i = j-1;
		for(i > 0;num[i] > key;i--){
			num[i+1] = num[i];
		}
		num[i+1] = key;
		//display
		for(k = 0;k < n;k++){
			printf("%d",num[k]);
			if(k < n-1){
				printf(" ");
			}else{
				printf("\n");
			}
		}
		
	}
	
	
	
	
	return 0;
}

Measured memory_bytes: 604
#include <bits/stdc++.h>
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;
using namespace std;
 
#define dump(x)  cout << #x << " = " << (x) << endl;
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE"  ) << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible"  ) << endl
#define possible(n) cout << ((n) ? "possible" : "impossible"  ) << endl
 
#define SANKOU(n,a,b) cout << ((n) ? (#a) : (#b) ) << endl
 
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)
 
#define FOREACH(x,a) for(auto& (x) : (a) )
 
#define WFA(d,v) REP(k,v)REP(i,v)REP(j,v)d[i][j]=min(d[i][j],d[i][k]+d[k][j])
 
#define SCOUT(x) cout<<(x)<<" "
#define ENDL cout<<endl
 
#define VECCIN(x) for(auto&youso_: (x) )cin>>youso_
#define VECIN2(x,y) REP(i,x.size())cin>>x[i]>>y[i]
#define VECCOUT(x) if(1){for(auto tt=x.begin();tt!=x.end();tt++){if(tt!=x.begin())cout<<" ";cout<<(*tt);}cout<<endl;}
 
#define ALL(obj) (obj).begin(),(obj).end()
 
#define EXIST(n,x) (find(ALL(n),x)!=n.end())
#define UNIQUE(obj) sort(ALL( obj )); obj.erase(unique(ALL(obj)),obj.end())
#define EN(x) if(1){cout<<#x<<endl;return 0;}
#define COUT(x) cout<<(x)<<endl
void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail){
    cin>>head;
    CINT(move(tail)...);
}
#define CIN(...) int __VA_ARGS__;CINT(__VA_ARGS__)
#define LCIN(...) ll __VA_ARGS__;CINT(__VA_ARGS__)
#define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__)
 
template <class T = ll>
T IN(){T x;cin>>x;return (x);}
 
template <class Head>
void VT(Head head){}
template <class Head,class Seco,class... Tail>
void VT(Head&& head,Seco&& seco,Tail&&... tail){
    seco.resize(head);
    VT(head,move(tail)...);
}
void VT2(){}
template <class Head,class... Tail>
void VT2(Head&& head,Tail&&... tail){
  VECCIN(head);
  VT2(move(tail)...);
}
 
template <class Head>
void VT3(Head&& head){}
template <class Head,class Seco,class... Tail>
void VT3(Head&& head,Seco&& seco,Tail&&... tail){
  seco[head]=IN();
  VT3(head,move(tail)...);
}
 
#define VC1(n,...) V __VA_ARGS__;VT(n,__VA_ARGS__);VT2(__VA_ARGS__); //aaabbbccc
#define VC2(n,...) V __VA_ARGS__;VT(n,__VA_ARGS__);REP(i,n)VT3(i,__VA_ARGS__); //abcabcabc
 
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision; // cpp_int
 
#define P pair<ll,ll>
#define V vector<ll>
#define M map<ll,ll>
#define S set<ll>
 
#define pb(a) push_back(a)
#define mp make_pair

int main(){

  SCIN(s);
  set<char> se;
  for(char i = 'a';i<='z';++i)
    se.insert(i);

  REP(i,s.size())
    se.erase(s[i]);
  if(se.empty())EN(None);
  COUT(*se.begin());

  return 0;
}

Measured memory_bytes: 512
def read():
    A, B, C, D = map(int, input().split())
    return A, B, C, D

def solve(A, B, C, D):
    while A > 0 and C > 0:
        C -= B
        if C <= 0:
            return 'Yes'
        A -= D
        if A <= 0:
            return 'No'
    

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))


Measured memory_bytes: 9180
N = int(input())
h = list(map(int, input().split()))

dp = [0] * N
dp[1] = abs(h[1] - h[0])

for i in range(2, N):
    cost1 = abs(h[i] - h[i - 1])
    cost2 = abs(h[i] - h[i - 2])
    dp[i] = min(dp[i - 1] + cost1, dp[i - 2] + cost2)

ans = dp[N - 1]

print(ans)


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

long long solve(const vector<long long>& layers,
                const vector<long long>& paties, int N, long long X) {
  if (N == 0) return 1;
  if (X <= 1) return 0;
  if (X == layers[N - 1] + 2) {
    return paties[N - 1] + 1;
  }
  if (X == 2 * layers[N - 1] + 3) {
    return 2 * paties[N - 1] + 1;
  }
  if (X <= layers[N - 1] + 1) {
    return solve(layers, paties, N - 1, X - 1);
  }
  if (X <= 2 * layers[N - 1] + 2) {
    return paties[N - 1] + 1 +
           solve(layers, paties, N - 1, X - layers[N - 1] - 2);
  }
}

int main(void) {
  int N;
  long long X;
  cin >> N >> X;
  vector<long long> layers(N + 1);
  vector<long long> paties(N + 1);
  layers[0] = paties[0] = 1;
  for (int i = 1; i < N; i++) {
    layers[i] = 2 * layers[i - 1] + 3;
    paties[i] = 2 * paties[i - 1] + 1;
  }
  cout << solve(layers, paties, N, X) << endl;
  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mod 1000000007
#define MAX 1000000000000000
#define all(v) v.begin(),v.end()
#define rep(i,a,b) for(i=(ll)a;i<(ll)b;i++)
#define revrep(i,a,b) for(i=(ll)a;i>=(ll)b;i--)
#define ii pair<ll,ll>
#define MP make_pair
#define pb push_back
#define f first
#define se second
#define ll long long int
#define vi set<ll>
ll modexp(ll a,ll b){ ll res = 1; while(b > 0){  if(b & 1) res = (res * a)%mod;  a = (a * a)%mod;  b/=2;  }  return res; }
#define rs resize
typedef tree< ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update > OST;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 << endl;
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

const ll N = 403;
ll i,n;
ll a[N],s[N],dp[N][N];
ll solve(ll i,ll j)
{
    if(dp[i][j] != -1) return dp[i][j];
    
    if(i == j)
        return dp[i][j] = 0;
    
    ll res = MAX;
    for(ll k = i;k < j;k += 1)
        res = min(res,solve(i,k) + solve(k + 1,j) + s[j] - s[i - 1]);
    return dp[i][j] = res;
}
int main()
{
    std::ios_base::sync_with_stdio(false); cin.tie(NULL);
    memset(dp,-1,sizeof(dp));
    cin>>n;
    rep(i,1,n + 1){
        cin>>a[i];
        s[i] = s[i - 1] + a[i];
    }
    cout<<solve(1,n);
    return 0;
}

Measured memory_bytes: 1664
//参考：http://ehafib.hatenablog.com/entry/2015/12/23/164517
//インクルード
#include<algorithm>//sort,二分探索,など
#include<bitset>//固定長bit集合
#include<cmath>//pow,logなど
#include<complex>//複素数
#include<deque>//両端アクセスのキュー
#include<functional>//sortのgreater
#include<iomanip>//setprecision(浮動小数点の出力の誤差)
#include<iostream>//入出力
#include<map>//map(辞書)
#include<numeric>//iota(整数列の生成),gcdとlcm(c++17)
#include<queue>//キュー
#include<set>//集合
#include<stack>//スタック
#include<string>//文字列
#include<unordered_map>//イテレータあるけど順序保持しないmap
#include<unordered_set>//イテレータあるけど順序保持しないset
#include<utility>//pair
#include<vector>//可変長配列

using namespace std;
typedef long long ll;

//マクロ
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define ALL(x) (x).begin(),(x).end() //sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) //sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))
#define INF 1000000000000
#define MOD 10000007
#define PB push_back
#define MP make_pair
#define F first
#define S second

int main(int argc, char const *argv[]) {
  ll n, a, b;
  ll amari, shou;
  ll ans;

  std::cin >> n >> a >> b;
  amari = n % (a + b);
  shou = n / (a + b);

  ans = shou * a + min(amari, a);
  std::cout << ans << '\n';

  return 0;
}


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

#define rep(i,n) for(int i = 0;i<n;i++)
#define vv(Type,Name, n,m,d)vector<vector<Type>> vv(n, vector<Type>(m, d));

using namespace std;
using ll = long long;


int main(void) {

	ios::sync_with_stdio(false);
	cin.tie(0);

	int n; cin >> n;
	vector<int> a(n), b(n);

	rep(i, n) cin >> a[i];
	rep(i, n) cin >> b[i];

	int fact[10];
	fact[0] = 1;
	for (int i = 1; i < 10; i++) fact[i] = fact[i-1] * i;
	int A = 0;
	bool use[10] = {};
	rep(i, n-1) {
		
		int cnt = 0;
		for (int j = 1; j < 10; j++) {
			if (a[i] == j) break;
			if (use[j] == false) cnt++;
		}

		A += fact[n - 1 - i] * cnt;
		use[a[i]] = true;
	}
	int B = 0;
	rep(i, 10) use[i] = false;


	rep(i, n-1) {
		int cnt = 0;
		for (int j = 1; j < 10; j++) {
			if (b[i] == j) break;
			if (!use[j])cnt++;
		}

		B += fact[n - 1 - i] * cnt;
		use[b[i]] = true;
	}

	cout << abs(A - B) << endl;


	return 0;
}


Measured memory_bytes: 3660
#include<iostream>
#include<queue>
#include<vector>
#include<tuple>
using namespace std;

int main(void) {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(N);
  vector<int> inEdge(N, 0);
  for(int i = 0; i < M; ++i) {
    int x, y;
    cin >> x >> y;
    x--; y--;
    G[x].emplace_back(y);
    inEdge[y]++;
  }
  queue<pair<int,int>> que;
  for(int i = 0; i < N; ++i) {
    if(inEdge[i] == 0) que.emplace(i, 0);
  }

  int answer = 0;
  while(!que.empty()) {
    int v, dist;
    tie(v, dist) = que.front();
    que.pop();    
    for(auto u : G[v]) {
	    inEdge[u]--;
        if(inEdge[u] == 0) que.emplace(u, dist+1);
        answer = max(answer, dist+1);
    }
  }
  cout << answer << endl;
  return 0;
}

Measured memory_bytes: 6144
import sys
import math

def is_prime_number(num):
    if num == 2:
        return True
    
    if num == 1 or num % 2 == 0:
        return False

    max_cnt = int(math.sqrt(num))
    if max_cnt % 2 == 0:
        max_cnt -= 1
        
    for i in range(max_cnt, 2, -2):
        if num % i == 0:
            return False

    return True

def main():
    n = int(sys.stdin.readline().strip())
    sum = 0
    for _ in range(n):
        num = int(sys.stdin.readline().strip())
        if is_prime_number(num):
            sum += 1

    print(sum)

if __name__ == '__main__':
    main()
    

Measured memory_bytes: 7736
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long ll;
int n,w;
ll wt[105];
ll dp[105][100001];
ll cost[105];
ll ks(int i,int w)
{

    if(w<0) return -1000000000000000;
    if(w==0) return 0;
    if(i==n)
        return 0;
    if(dp[i][w]!=-1)
        return dp[i][w];

    ll a = max(ks(i+1,w-wt[i])+cost[i],ks(i+1,w));
    dp[i][w] = a;
    return dp[i][w];
}
int main()
{
    memset(dp,-1,sizeof(dp));
    cin>>n>>w;
    for(int i=0;i<n;i++)
    {
        cin>>wt[i]>>cost[i];
    }
    cout<<ks(0,w)<<endl;
}


Measured memory_bytes: 82304
#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;

int main(){
    int h,w;
    while(cin >> h >> w, h || w){
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(i == 0 || i == h-1 || j == 0 || j == w-1){
                    printf("#");
                } else {
                    printf(".");
                }
            }
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}

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

const int M = 1000000007;

long long modinv(long long a) {
    long long x = M, y = a, p = 1, q = 0, r = 0, s = 1;
    while (y != 0) {
        long long u = x / y;
        long long x0 = y; y = x - y * u; x = x0;
        long long r0 = p - r * u, s0 = q - s * u;
        p = r; r = r0; q = s; s = s0;
    }
    return q < 0 ? q + M : q;
}

int n;
vector<vector<int> > adj;
vector<long long> S, C, ans, fact, facti;

void dfs(int r, int p) {
    S[r] = 1; C[r] = 1;
    for (int v : adj[r]) {
        if (v != p) {
            dfs(v, r);
            S[r] += S[v];
            C[r] = C[r] * C[v] % M * facti[S[v]] % M;
        }
    }
    C[r] = C[r] * fact[S[r]-1] % M;
}

void dfs2(int r, int p, long long s, long long c) {
    ans[r] = c * fact[n-1] % M * facti[s] % M;
    for (int v : adj[r]) {
        if (v != p)
            ans[r] = ans[r] * C[v] % M * facti[S[v]] % M;
    }
    for (int v : adj[r]) {
        if (v != p) {
            long long c1 = ans[r] * modinv(C[v]) % M * facti[n-1] % M *
                fact[S[v]] % M * fact[n-S[v]-1] % M;
            dfs2(v, r, n - S[v], c1);
        }
    }
}

int main() {
    cin >> n;
    adj.resize(n);
    S.resize(n);
    C.resize(n);
    ans.resize(n);
    for (int i = 1; i < n; i++) {
        int a, b; cin >> a >> b;
        a--; b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    fact.resize(n+1);
    fact[0] = 1;
    for (int i = 1; i <= n; i++) fact[i] = (fact[i-1] * i) % M;
    facti.resize(n+1);
    facti[n] = modinv(fact[n]);
    for (int i = n-1; i >= 0; i--) facti[i] = (facti[i+1] * (i+1)) % M;

    dfs(0, -1);
    dfs2(0, -1, 0, 1);
    for (int a : ans) cout << a << endl;
}


Measured memory_bytes: 22400
#include <iostream>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<cstdio>
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)
#define mp make_pair
#define pb push_back
#define P pair<int,int>
#define ll long long
#define cat(p) cout<<p<<endl
#define catA(p,n) for(int i=0;i<n;i++)cout<<p[i]<<((i==n-1)?"\n":" ")


int main(){
  string s;
  cin>>s;
  if(s[0]==s[1]||s[1]==s[2]||s[2]==s[3])cout<<"Bad"<<endl;
  else cout<<"Good"<<endl;

  return 0;
}


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

int main(){
  int n,m;
  cin >> n >> m;
  int k[n],a[n][m];
  vector<int> c(m);
  for(int i = 0;i < n;i++){
    cin >> k[i];
    for(int j = 0;j < k[i];j++){
      cin >> a[i][j];
      c.at(a[i][j]-1)++;
    }
  }
  int kekka = 0;
  for(int i = 0;i < m;i++){
    if(c.at(i) == n){
      kekka++;
    }
  }
  cout << kekka << endl;
}

Measured memory_bytes: 256