akhauriyash/Code-Regression huggingface dataset:
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;

public class Main {
  Scanner sc;

  void run() {
    int[] mist = new int[] { 10, 50, 100, 500 };
    for ( int i = 0;; ++i ) {
      int n = ni();
      if ( n == 0 ) {
        break;
      }
      int[] list = new int[4];
      for ( int j = 0; j < 4; ++j ) {
        list[ j ] = ni();
      }
      int[] ans = new int[4];
      int min = 1 << 28;
      int tmin = 1 << 28;
      for ( int a = 0; a <= list[ 0 ]; ++a ) {
        for ( int b = 0; b <= list[ 1 ]; ++b ) {
          for ( int c = 0; c <= list[ 2 ]; ++c ) {
            for ( int d = 0; d <= list[ 3 ]; ++d ) {
              int sum = a * mist[ 0 ] + b * mist[ 1 ] + c * mist[ 2 ] + d
                  * mist[ 3 ];
              if ( sum < n )
                continue;
              int v = list[ 0 ] + list[ 1 ] + list[ 2 ] + list[ 3 ]
                  - ( a + b + c + d );
              int v_ = 0;
              int n_ = sum - n;
              for ( int k = 3; 0 <= k; --k ) {
                int p = n_ / mist[ k ];
                n_ -= mist[ k ] * p;
                v_ += p;
              }
              if ( min < v + v_ )
                continue;
              if ( min == v + v_ ) {
                if ( tmin <= v_ )
                  continue;
                tmin = v_;
                ans[ 0 ] = a;
                ans[ 1 ] = b;
                ans[ 2 ] = c;
                ans[ 3 ] = d;
              } else {
                min = v + v_;
                tmin = v_;
                ans[ 0 ] = a;
                ans[ 1 ] = b;
                ans[ 2 ] = c;
                ans[ 3 ] = d;
              }
              // debug(min, a, b, c, d, sum, v, v_);
            }
          }
        }
      }
      if ( i > 0 ) {
        System.out.println();
      }
      for ( int j = 0; j < 4; ++j ) {
        if ( ans[ j ] == 0 )
          continue;
        System.out.println( mist[ j ] + " " + ans[ j ] );
      }
    }
  }

  Main() {
    sc = new Scanner( System.in );
  }

  int ni() {
    return sc.nextInt();
  }

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

  void debug(Object... os) {
    System.err.println( Arrays.deepToString( os ) );
  }

}

Measured memory_bytes: 34360
#include <iostream>

int main()
{
    int n, sum{};
    std::cin >> n;
    for (auto i = 0; i < n; ++i)
    {
        int s, e;
        std::cin >> s >> e;
        sum += (e - s + 1);
    }
    std::cout << sum << std::endl;
    return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();

		if (A == B && B == C) {
			System.out.println("No");
		} else if (A == B && B != C) {
			System.out.println("Yes");
		} else if (A != B && B == C) {
			System.out.println("Yes");
		} else if (A == C && B != C) {
			System.out.println("Yes");
		} else if (A != B && B != C) {
			System.out.println("No");
		}
	}
}

Measured memory_bytes: 21972
n = int(input())

vals = [list(map(int, input().split())) for _ in range(n)]

dp = [[0]*3 for _ in range(n)]
dp[0] = vals[0]

for i in range(1, n):
    dp[i][0] = max(dp[i-1][1], dp[i-1][2]) + vals[i][0]
    dp[i][1] = max(dp[i-1][0], dp[i-1][2]) + vals[i][1]
    dp[i][2] = max(dp[i-1][0], dp[i-1][1]) + vals[i][2]

print(max(dp[n-1]))

Measured memory_bytes: 49964
n = int(input())
an = list(map(int, input().split()))

for i in an:
    if i == 0:
        print(0)
        exit()
ans = 1
limit = 10**18
for i in range(n):
    ans *= an[i]
    if ans > limit:
        print(-1)
        exit()

print(ans)


Measured memory_bytes: 21616
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
ll inf = 1LL << 60;
ll dp[102][100005];
int main() {
	int n, W;
	cin >> n >> W;
	ll w[102], v[102];
	ll sum = 0;
	for (int i = 0; i < n; i++) {
		cin >> w[i] >> v[i];
		sum += v[i];
	}
	for (int i = 0; i <n; i++) {
		for (int j = 0; j <= sum; j++) {
			dp[i][j] = inf;
		}
	}
	dp[0][0]=0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j <= sum; j++) {
			if (j >= v[i]) {
				dp[i + 1][j] = min(dp[i][j], dp[i][j - v[i]] + w[i]);
			}
			else {
				dp[i + 1][j] = dp[i][j];
			}
		}
	}
	long long ans = 0;
	for (int j = 0; j <= sum; j++) {
		if (dp[n][j] <= W) {
			ans = j;
		}
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 79104
#include <bits/stdc++.h>
using namespace std;
#define MAX 100005
#define NIL -1

struct Node { int parent, left, right; };
Node T[MAX];

int getDepth(int u) {
  int d = 0;
  while (T[u].parent != NIL) {
    u = T[u].parent;
    d++;
  }
  return d;
};

vector<int> D(MAX, 0);

void setDepth(int u, int p) {
  D[u] = p;
  if (T[u].right != NIL) {
    setDepth(T[u].right, p);
  }
  if (T[u].left != NIL) {
    setDepth(T[u].left, p + 1);
  }
};

void printChildren(int u) {
  int c = T[u].left;
  cout << '[';
  while (c != NIL) {
    cout << c;
    c = T[c].right;
    if (c != NIL) {
      cout << ", ";
    }
  }
  cout << ']';
};

int main() {
  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    T[i].parent = T[i].right = T[i].left = NIL;
  }

  for (int i = 0; i < n; i++) {
    int id, k;
    cin >> id >> k;
    if (k == 0) {
      T[id].left = NIL;
    } else {
      vector<int> child(k, 0);
      for (int j = 0; j < k; j++) {
        cin >> child[j];
      }
      T[id].left = child[0];
      for (int j = 0; j < k; j++) {
        T[child[j]].parent = id;
        if (j < k - 1) {
          T[child[j]].right = child[j + 1];
        } else {
          T[child[j]].right = NIL;
        }
      }
    }
  }

  int r;

  for (int i = 0; i < n; i++) {
    if (T[i].parent == NIL) r = i;
  }

  setDepth(r, 0);

  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << T[i].parent << ", depth = " << D[i];
    if (D[i] == 0) cout << ", root, ";
    else if (T[i].left != NIL) cout << ", internal node, ";
    else cout << ", leaf, ";
    printChildren(i);
    cout << endl;
  }
};



Measured memory_bytes: 7268
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
int main(){
	string s;
	bool flag=false;
	cin>>s;
	int len=s.length();
	for(int i=0;i<len;i++)
	{
		if(s[i]=='C')
		{
			for(int j=i;j<len;j++)
			{
				if(s[j]=='F')
				{
					flag=true;
					break;
				}
			}
			break;
		}
	}
	if(flag==true) cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    char c;
    cin >> c;
    cout << char(c+1) << "\n";
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1LL<<60;
const int MOD = (int)1e9 + 7;

signed main() {
    int n;
    cin >> n;
    vector<int> v(n);
    for (int i = 0; i < n; i++) {
        cin >> v[i];
    }
    int cnt = v[0];
    for(int i = 0; i < n - 1; i++) {
        if (v[i] < v[i + 1])
            cnt += v[i + 1] - v[i];
    }
    cout << cnt << "\n";
}

Measured memory_bytes: 256
def main():
	N, K = map(int, input().split())
	S = input()

	counts = [0]
	if S[0] == "0":
		counts.append(0)
	last_char = S[0]
	for s in S:
		if s == last_char:
			counts[-1] += 1
		else:
			counts.append(1)
			last_char = s
	if S[-1] == "0":
		counts.append(0)
	# print(counts)
	l, r = 0, K * 2 + 1
	max_count = last_count = sum(counts[l:r])
	while True:
		l, r = l + 2, r + 2
		if len(counts) < r:
			break
		count = last_count - (counts[l - 1] + counts[l - 2]) + (counts[r - 1] + counts[r - 2])
		max_count = max(max_count, count)
		last_count = count
	print(max_count)

main()


Measured memory_bytes: 4472
#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <iterator>
#include <queue>
#include <stack>
#include <deque>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <string>
#include <cmath>
#include <complex>
#include <functional>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cstdlib>
#include <cctype>

#define REP(i,m,n) for(int i=m;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define rp(i,c) rep(i,(int)c.size())
#define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rall(u) (u).rbegin(),(u).rend()
#define dbg(x) cerr<<#x<<" = "<<(x)<<endl
#define v_delete(a,b) (a).erase(remove((a).begin(), (a).end(), b), (a).end())
#define v_unique(a) (a).erase(unique((a).begin(), (a).end()), (a).end())
#define VV(T) vector<vector< T > >
#define init(a,b) memset((a), (b), sizeof((a)))
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf=1<<28;
const double INF=1e10,EPS=1e-9;


template<class T>int SIZE(T a){return a.size();}
template<class T>string IntToString(T num){string res;stringstream ss;ss<<num;return ss.str();}
template<class T>T StringToInt(string str){T res=0;for(int i=0;i<SIZE(str);i++)res=(res*10+str[i]-'0');return res;}
template<class T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template<class T>T lcm(T a,T b){return a/gcd(a,b)*b;}

template <class T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }
template <class T> void input(T* a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }
typedef pair<int,int> pii;

inline bool IN(int x, int y, int w, int h)
{
    return 0 <= x && x < w && 0 <= y && y < h;
}


int main()
{
	int n,w;
	char dummy;
	cin >> n;
	cin >> w;
	vi l(n);
	rp(i,l){
		l[i]=i+1;
	}
	rep(i,w){
		int a,b;
		cin >> a >> dummy >> b;
		swap(l[a-1],l[b-1]);
	}
	rp(i,l){
		cout << l[i] << endl;
	}


}

Measured memory_bytes: 1192
from sys import stdin
N=int(stdin.readline().rstrip())
tmp=float("inf")
for A in range(1,N):
    B=N-A
    a=sum(list(map(int,list(str(A)))))
    b=sum(list(map(int,list(str(B)))))
    if a+b < tmp:
        tmp=a+b
print(tmp)

Measured memory_bytes: 3060
N = int(input())
S = input()

ans = 0
for i in range(N):
  cnt = 0
  l = list(set(S[:i]))
  r = list(set(S[i:]))
  for ls in l:
    if(ls in r):
      cnt += 1
  ans = max(ans, cnt)
print(ans)

Measured memory_bytes: 3064
def main():
    a, b, c, k = (int(i) for i in input().split())
    if k < a:
        ans = k
    elif k == a:
        ans = a
    elif a < k and k <= a+b:
        ans = a
    elif a+b < k:
        ans = a
        k -= a+b
        ans -= k
    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 61688
#include <bits/stdc++.h>
#define REP(i,n) for(int (i)=0;(i)<(n);(i)++)
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;

template<class T> inline void chmin(T& a, T b) {
    if (a > b) {
        a = b;
    }
}

template<class T> inline void chmax(T& a, T b) {
    if (a < b) {
        a = b;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int a,b,c;
    cin >> a >> b >> c;
    if(b-a==c-b){
        cout << "YES" << "\n";
    }else{
        cout << "NO" <<"\n";
    }
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<algorithm>
#include<bits/stdc++.h>
 
using namespace std;
 
int main(){
	int N;
	cin >> N;
	int a[N+10],go=0,su=1001;
	for(int i=0; i<N; i++){
		cin >> a[i];
		go=max(go,a[i]);
		su=min(su,a[i]);
	}cout << go-su << endl;
}

Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCoder.Abc
{
    public class QuestionA
    {
        public static void Main(string[] args)
        {
            // 整数の入力
            int X = int.Parse(Console.ReadLine());
            int A = int.Parse(Console.ReadLine());
            int B = int.Parse(Console.ReadLine());

            X -= A;
            while (true) {
                X -= B;
                if(X < B) {
                    break;
                }
            }

            Console.WriteLine(X);
        }
    }
}


Measured memory_bytes: 13140
#include<bits/stdc++.h> 
using namespace std;
int main() 
{ 

 long long n,sum=0;
 cin>>n;
 for(long long i=1;i<=n;i++)
{
	if((i%3!=0))
	sum+=i;
	if(i%5==0)
	sum-=i;
	if(i%3==0&&i%5==0)
	 sum+=i;
}
cout<<sum<<"\n";
}

Measured memory_bytes: 3636
#include <bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<(n);i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T>
using vec = std::vector<T>;

int main() {
    int A,B,C,X;
    cin >> A >> B >> C >> X;
    
    int ans = 0;
    rep(a,A+1)rep(b,B+1)rep(c,C+1){
        if(500*a+100*b+50*c==X) ++ans;
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
from collections import defaultdict
INF = 10**9+7

n = int(input())
s = str(input())
d = defaultdict(int)
ans = 0
for i in s:
    d[i]+=1
    t = 1
    for ch, cn in d.items():
        if ch ==i:
            continue
        t *= cn +1
    ans = (ans+t)%INF
print(ans) 

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

int main(void)
{
    int a, b;
    cin >> a >> b;
    cout << "a";
    if (a < b)
        cout << " < ";
    else if (a > b)
        cout << " > ";
    else
        cout << " == ";
    cout << "b" << endl;
    return 0;
}


Measured memory_bytes: 3016
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<numeric>
#include<map>
#include<iostream>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;
typedef pair<ll, ll> mp;
ll MOD = 1e9+7;
//LLONG_MIN

//AC

int main(void){
	int A,B;
	cin>>A>>B;
	if(A<=8 && B<=8){
		printf("Yay!\n");
	}else{
		printf(":(\n");
	}
	return 0;
}

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

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    
    int n;
    cin>>n;
    
    bool w=1;
    
    while(n--)
    {
        int a;
        cin>>a;
        
        if(a%2==0)
        {
            if(a%3 && a%5) w=0;
        }
    }
    
    if(w) cout << "APPROVED" << endl;
    else cout << "DENIED" << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
signed main(){
	int a,b;
	cin>>a>>b;
	if(b==1)cout<<0<<endl;
	else cout<<(b-2)/(a-1)+1<<endl;
}


Measured memory_bytes: 256
import itertools
import math
import fractions
import functools
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

l = [i for i in range(1,n+1)]

n_list = list(itertools.permutations(l, n))


ans1 = 0
ans2 = 0

for i in range(len(n_list)):
    if n_list[i] == p:
        ans1 = i
    if n_list[i] == q:
        ans2 = i

print(abs(ans1-ans2))

Measured memory_bytes: 70508
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long a = sc.nextLong();
        long b = sc.nextLong();

        sc.close();

        double ave = (a + b) / 2.0;

        int ans = (int) Math.ceil(ave);

        System.out.println(ans);

    }

}


Measured memory_bytes: 23380
import math
def merge(x, left, mid, right):
    global m
    n1 = mid - left
    n2 = right - mid
    l = x[left:left + n1]
    r = x[mid:mid + n2]
    l.append(10e10)
    r.append(10e10)
    i = 0
    j = 0
    for k in range(left, right):
        m += 1
        if l[i] < r[j]:
            x[k] = l[i]
            i += 1
        else:
            x[k] = r[j]
            j += 1
 
def mergeSort(x, left, right):
    if left + 1 < right:
        mid = math.ceil((left + right) / 2)
        mergeSort(x, left, mid)
        mergeSort(x, mid, right)
        merge(x, left, mid, right)
 
n = int(input())
x = list(map(int,input().split()))
m = 0
mergeSort(x, 0, n)
print(" ".join(map(str, x)))
print(m)


Measured memory_bytes: 70664
#include <bits/stdc++.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> P;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

int main(){
  int N;
  ll K;
  cin >> N >>K;
  vector <ll> A(N);
  vector <ll> F(N);
  rep(i, N)cin >> A[i];
  rep(i, N)cin >> F[i];
  sort(A.begin(), A.end());
  sort(F.begin(), F.end(), greater<ll>());
  ll cons = 0;
  ll r = 1000000000000;
  ll l = -1;
  while (r-l != 1){
    ll nex = (r + l)/2;
    ll temp = 0;
    rep(i, N){
      temp += max(A[i]-nex/F[i], cons);
    }
    if (K < temp) l = nex;
    else r = nex;
  }
  cout << r <<endl;
}


Measured memory_bytes: 6356
#include<bits/stdc++.h>
int n,L,T,xs[100007],p=0;
int main(){
	scanf("%d%d%d",&n,&L,&T);
	for(int i=0,x,w;i<n;++i){
		scanf("%d%d",&x,&w);
		if(w==1){
			x+=T;
			p+=x/L;
			xs[i]=x%L;
		}else{
			x-=T;
			p+=x/L;
			x%=L;
			if(x<0)x+=L,--p;
			xs[i]=x;
		}
		p%=n;
	}
	std::sort(xs,xs+n);
	p=(p+n)%n;
	for(int i=0;i<n;++i)printf("%d\n",xs[(i+p)%n]);
	return 0;
}

Measured memory_bytes: 1536
program prob6
    implicit none
    character(5005) :: s
    character(5) :: t
    integer :: K
    character(5), dimension(:), allocatable :: a
    integer :: i, j, L, cnt
    read(*,*) s
    read(*,*) K

    L = len_trim(s)
    allocate(a(L*K))
    do i = 1, L
        do j = 1, K
            a(L*(j-1)+i) = adjustl(s(i:i+j-1))
        end do
    end do

    call msort(a)
    cnt = 1
    i = 2
    t = a(1)
    do while(cnt < K)
        if(t /= a(i)) then
            t = a(i)
            cnt = cnt + 1
        end if
        i = i + 1
    end do

    write(*,'(a)') trim(adjustl(t))
    stop
contains

  !
  ! swap two arguments
  !
subroutine swap(a, b)
    implicit none
    character(5), intent(inout) :: a, b

    character(5) :: c

    c = a
    a = b
    b = c

  end subroutine swap

  !
  ! merge sort
  !
  recursive subroutine msort(x)
    implicit none
    character(5), intent(inout) :: x(:)

    integer(8) :: n, mid, i, j, k
    character(5), allocatable :: tmp(:)
    
    n = size(x)
    
    if(n == 1) then
      return
    end if

    !
    ! 前半と後半に分けてソート
    ! 1~midとmid+1~nをそれぞれソート済みにする
    !
    mid = n / 2
    call msort(x(:mid))
    call msort(x(mid+1:))

    !
    ! tmpという配列にxを一時的に保管
    ! マージソートは外部ソート
    !
    allocate(tmp(n))
    tmp = x

    ! tmpの後半部分を左右反転
    do k = n, mid+1, -1
      tmp(k) = x(mid + n - k + 1)
    end do

    ! iは前半のリストのうちまだ見ていないものの最小値
    ! jは後半のリストのうちまだ見ていないものの最小値
    ! kは見た回数の合計
    i = 1
    j = n

    do k = 1, n
      !
      ! 一番左と一番右のうち小さい方をxに入れていく
      ! ここが等号つき不等号なので、安定ソートとなる
      !
      if(tmp(i) <= tmp(j)) then
        x(k) = tmp(i)
        i = i + 1
      else
        x(k) = tmp(j)
        j = j - 1
      end if
    end do
    
    deallocate(tmp)
    return
  end subroutine msort
end program prob6

Measured memory_bytes: 3188
#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
#include <map>

int main(){
    long long numA, numB, totalMin;
    std::cin >> numA >> numB >> totalMin;

    std::vector<long long> aBooks(numA+1);
    std::vector<long long> bBooks(numB+1);

    aBooks[0] = 0;
    for(long long i=1; i<=numA; ++i){
        int a;
        std::cin >> a;
        aBooks[i] = a + aBooks[i - 1];
    }

    bBooks[0] = 0;
    for(long long i=1; i<=numB; ++i){
        int a;
        std::cin >> a;
        bBooks[i] = a + bBooks[i - 1];
    }

    long long max = 0;
    long long b = numB;
    for(long long i=0; i<=numA; ++i){
        if(aBooks[i] > totalMin){
            break;
        }
    
        while(bBooks[b] > totalMin - aBooks[i]){
            b -= 1;
        }

        max = std::max(max, i + b);
    }

    std::cout << max << std::endl;
}

Measured memory_bytes: 6404
#include <stdio.h>

long long a, b, x, fa, fb;
int main(void)
{
  scanf("%lld %lld %lld", &a, &b, &x);
 
  fb = b/x+1;
  if(a != 0) fa = (a-1)/x+1;
  else fa=0;
 
  printf("%lld\n", fb-fa);
  return 0;
}


Measured memory_bytes: 128
#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, X, m, mmin = 1000, msum = 0;
    cin >> N >> X;
    rep(i, N) {
        cin >> m;
        mmin = min(mmin, m);
        msum += m;
    }
    cout << N + (X - msum) / mmin << "\n";
}

Measured memory_bytes: 3632
import std.stdio;

void main()
{
    int x, d, r;
    readf("%d %d %d\n", &x, &d, &r);
    f(x, d, r);
}

int i;

private int f(int x, int d, int r)
{
    i++;

    if (i <= 10)
    {
        writeln(x * r - d);
        return f(x, d, x * r - d);
    }

    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <iterator>
#include <math.h>
#include <cmath>
#include <set>   
#include <bitset>
//#include <boost/math/common_factor_rt.hpp>

#define ll long long
#define max 100000
using namespace std;
ll l = 1000000007;
int a[15] = {};

int main()
{
	int n;
	int ans = 0;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	
	sort(a, a + n, greater<int>());

	for (int i = 0; i < n; i++)
	{
		if (i == 0) {
			ans += a[i] / 2;
		}
		else {
			ans += a[i];
		}
	}
	
	cout << ans << endl;
	return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#include <bitset>
#define M 1000000007
#define M1 998244353

#define tr(container, it) \
    for (typeof(container.begin()) it = container.begin(); it != container.end(); it++)

#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

ll power(ll x, ll n)
{
    ll result = 1;
    while (n)
    {
        if (n % 2 == 1)
            result = result * x;
        n = n / 2;
        x = x * x;
    }
    return result;
}

ll gcd(ll a, ll b)
{
    if (!b)
        return a;
    return gcd(b, a % b);
}

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

inline ll mul(ll x, ll y, ll m)
{
    ll z = 1LL * x * y;
    if (z >= m)
    {
        z %= m;
    }
    return z;
}

ll powmod(ll x, ll y, ll m)
{
    ll r = 1;
    while (y)
    {
        if (y & 1)
        {
            r = mul(r, x, m);
        }
        y >>= 1;
        x = mul(x, x, m);
    }
    return r;
}

void start()
{
    fast;
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
/***********************************************************************************************************/

ll helper(int *w, int *v, int n, int s, vector<vector<ll>> &dp)
{
    if (n == 0 || s == 0)
    {
        return dp[n][s] = 0;
    }
    if (dp[n][s] != -1)
    {
        return dp[n][s];
    }
    if (w[n - 1] <= s)
    {
        return dp[n][s] = max(v[n - 1] + helper(w, v, n - 1, s - w[n - 1], dp), helper(w, v, n - 1, s, dp));
    }
    return dp[n][s] = helper(w, v, n - 1, s, dp);
}

ll solve(int *w, int *v, int n, int s)
{
    vector<vector<ll>> dp(n + 1, vector<ll>(s + 1, -1));
    return helper(w, v, n, s, dp);
}

int main()
{
    start();
    int n, s;
    cin >> n >> s;
    int w[n], v[n];
    for (int i = 0; i < n; i++)
    {
        cin >> w[i] >> v[i];
    }
    cout << solve(w, v, n, s);
    return 0;
}

Measured memory_bytes: 83188
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

typedef long long ll;
using namespace std;

bool graph[8][8];

int dfs(int v, int N, bool visited[8]) {
  bool all_visited = true;
  for (int i = 0; i < N; i++) {
    if (visited[i] == false) {
      all_visited = false;
    }
  }

  if (all_visited) {
    return 1;
  }

  int ret = 0;

  for (int i = 0; i < N; i++) {
    if (graph[v][i] == false)
      continue;
    if (visited[i])
      continue;
    visited[i] = true;
    ret += dfs(i, N, visited);
    visited[i] = false;
  }
  return ret;
}

int main(void) {
  int N, M;
  cin >> N >> M;

  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    graph[A - 1][B - 1] = graph[B - 1][A - 1] = true;
  }

  bool visited[8];
  for (int i = 0; i < N; i++) {
    visited[i] = false;
  }

  visited[0] = true;
  cout << dfs(0, N, visited) << endl;
  return 0;
}

Measured memory_bytes: 3648
n,m = map(int,input().split())
s = [[] for _ in range(m)]
for i in range(m):
    temp = list(map(int,input().split()))
    s[i] = temp[1:]#電球がどのスイッチと接続されているかの情報が詰まっています。
p = list(map(int,input().split()))
ans = 0

for i in range(2**n):#スイッチの入れ方を列挙します。
    switches_on = [0 for _ in range(m)]#電球の点灯に関係するスイッチが何個onになっているかを確認します。
    for j in range(n):
        if (i >> j) & 1:#j+1の番号を持ったスイッチはonになっています。
            for k in range(m):
                if j+1 in s[k]:
                    switches_on[k] += 1
    light_on = [0 for _ in range(m)]
    for j in range(m):
        if (switches_on[j]%2) != (p[j]%2):#j番の電球が点灯しなかった場合
            light_on[j] = False
        else:#i番の電球が点灯する場合
            light_on[j] = True
    if all(light_on):
        ans += 1
print(ans)

Measured memory_bytes: 44656
#include<iostream>
using namespace std;
int main(){
	while(1){
	int n;
	cin>>n;
	if(n==0)break;
	int a,b,c,d,e,f,g;
	g=1000-n;
	for(a=0;a<2;a++){
		for(b=0;b<5;b++){
			for(c=0;c<2;c++){
				for(d=0;d<5;d++){
					for(e=0;e<2;e++){
						for(f=0;f<5;f++){
							if(a*500+b*100+c*50+d*10+e*5+f==g){
								cout<<a+b+c+d+e+f<<endl;
								break;
							}
						}
					}
				}
			}
		}
	}
        }
	return 0;
}							


Measured memory_bytes: 3088
#include <iostream>
    #include <string>
    #include <algorithm>
     
    using namespace std;
     
    int main(){
        int P, Q, R;
        cin >> P >> Q >> R;
     
        int pt1,pt2,pt3;
        pt1 = P + Q;
        pt2 = Q + R;
        pt3 = P + R;
        int min = pt1;
        if(min > pt2){
            min = pt2;
        }
        if(min > pt3){
            min = pt3;
        }
        cout << min << endl;
    }

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

using namespace std;

int main() {
	int n;
	cin >> n;

	vector<int> c(n - 1, 0), s(n - 1, 0), f(n - 1, 0);
	int now = 0;
	for (int i = 0; i < n - 1; i++) {
		cin >> c[i] >> s[i] >> f[i];
	}

	vector<int> ans(n, 0);
	// 最後の駅は明らかに0なので計算しない
	for (int i = 0; i < n - 1; i++) {
		int now = 0;
		for (int j = i; j < n - 1; j++) {
			if (now < s[j]) {
				now = s[j];
			} else {
				auto tmp = now % f[j];
				if (tmp > 0) {
					now += f[j] - tmp;
				}
			}
			now += c[j];
		}
		ans[i] = now;
	}

	for (auto x : ans) {
		cout << x << endl;
	}
	return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b)  for(int i=(a),i##formax=(b);i< i##formax;i++)
#define FORR(i,a,b) for(int i=(a),i##formin=(b);i>=i##formin;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pcnt __builtin_popcount
#define sz(x) (int)(x).size()
#define maxs(x,y) x=max((x),(y))
#define mins(x,y) x=min((x),(y))
#define show(x) cout<<#x<<" = "<<(x)<<endl;
#define all(a) ((a).begin()),((a).end())
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define bit(n) (1LL<<(n))
typedef long long ll;
typedef __int128_t lll;
template<class T>using V=vector<T>;
template<class T>using VV=V<V<T>>;
template<class T,class Y>using P=pair<T,Y>;
template<class T,class Y>ostream& operator<<(ostream&o,P<T,Y>&p){return o<<"("<<p.fi<<","<<p.se<<")";}
template<class T>ostream& operator<<(ostream&o,V<T>&v){for(T&t:v)o<<t<<",";return o<<endl;}
template<class T>void uniq(V<T>&v){sort(all(v));v.erase(unique(all(v)), v.end());}

lll gcd(lll a,lll b,lll&x,lll&y){if(!b){x=1;y=0;return a;}lll d=gcd(b,a%b,y,x);y-=a/b*x;return d;}
ll modInv(ll a,ll m){lll x,y;gcd(a,m,x,y);return(x%m+m)%m;}
ll modPow(lll a,lll n,ll m){lll p=1;for(;n;n>>=1,a=a*a%m)if(n&1)p=p*a%m;return p;}
//V<ll>F,R;void setF(int n,int mod){F=R=V<ll>(++n);F[0]=1;FOR(i,1,n)F[i]=F[i-1]*i%mod;R[n-1]=modInv(F[n-1],mod);FORR(i,n-1,1)R[i-1]=R[i]*i%mod;}
//ll comb(int a,int b,int m){return a<b||b<0?0:F[a]*R[b]%m*R[a-b]%m;}

const int IINF = 1e9+6;
const ll LINF = 1e18;
const int MOD = 1e9+7; //998244353;
const int N = 80;
const int M = 12800;
bitset<25600> b[N];
int H, W, A[N][N], B[N][N];
main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> H >> W;
  FOR(i, 0, H)FOR(j, 0, W) cin >> A[i][j];
  FOR(i, 0, H)FOR(j, 0, W) cin >> B[i][j];
  b[0].set(M);
  FOR(i, 0, H)FOR(j, 0, W){
    if(j) b[j] |= b[j-1];
    int k = abs(A[i][j]-B[i][j]);
    b[j] = (b[j]<<k) | (b[j]>>k);
  }
  FOR(i, M, M*2) if(b[W-1][i]){
    cout << i-M << endl;
    return 0;
  }
}

Measured memory_bytes: 640
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <cstring>
#include <math.h>
#include <bitset>
#include <queue>
#include <set>
#include <iomanip>
#include <math.h>
#include <assert.h>
// #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr long long int INFLL = 1001001001001001LL;
constexpr long long int infll = 1001001001001001LL;
constexpr int INF = 1000000007;
constexpr int inf = 1000000007;
const int mod = 1000000007;

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

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

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

inline double nCr(const int n, int r)
{
    if (n == 0)
    {
        return 0;
    }
    if (r == 0)
    {
        return 1;
    }
    if (r == 1)
    {
        return n;
    }
    if (n == r)
    {
        return 1;
    }

    if (r > n / 2)
    {
        r = n / 2;
    }

    double result = 1;
    for (double i = 1; i <= r; i++)
    {
        result *= (n - i + 1) / i;
    }

    return (result);
}

template <typename T>
T seinomi(T a)
{
    if (a > 0)
    {
        return a;
    }
    else
    {
        return 0;
    }
}

template <typename T>
map<T, T> soinsuubunkai(T n) //連想配列[素因数f.first][個数f.second]
{
    map<T, T> ret;
    for (T i = 2; i * i <= n; i++)
    {
        while (n % i == 0)
        {
            ret[i]++;
            n /= i;
        }
    }
    if (n != 1)
        ret[n] = 1;
    return ret;
}

template <typename T>
T ketasuu(T num)
{
    return std::to_string(num).length();
}

int kaizyou(int k)
{
    int sum = 1;
    for (int i = 1; i <= k; ++i)
    {
        sum *= i;
    }
    return sum;
}

template <class ForwardIt, class T>
void iota(ForwardIt first, ForwardIt last, T value)
{
    while (first != last)
    {
        *first++ = value;
        ++value;
    }
}

//nの階乗を計算
int factorial(int n)
{
    int answer = 1;
    while (n > 1)
    {
        answer *= n;
        n--;
    }
    return answer;
}
int comb(int n, int r)
{
    return factorial(n) / (factorial(r) * factorial(n - r));
}

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

//小数点以下10桁テンプレート（main関数内の最初に貼付け）
//std::cout << std::fixed << std::setprecision(10);

//----------------------------------------------------------------


int main(){
ll n;
cin>>n;
ll h;
ll pre;
cin>>h;
pre=h;
bool flag=true;
ll cnt=0;
for(int i=1;i<n;i++){
    cin>>h;
    if(pre<h){
        cnt=0;
    }
    cnt+=seinomi(pre-h);
    if(cnt>1){
        flag=false;
        break;
    }
    pre=h;
}
if(flag){
    cout<<"Yes"<<endl;
}else{
    cout<<"No"<<endl;
}
}





Measured memory_bytes: 384
//Hot Days
#include <iostream>

using namespace std;

static const int NIL = -1;

struct Clothes {
    int a, b, c;
    Clothes() {}
    Clothes(int a, int b, int c): a(a), b(b), c(c) {}
};

int main() {
    int D, N, a, b, c;
    cin >> D >> N;
    int T[D];
    Clothes C[N];
    for(int i = 0; i < D; i++) cin >> T[i];
    for(int i = 0; i < N; i++) {
        cin >> a >> b >> c;
        C[i] = Clothes(a, b, c);
    }

    int dp[D][101];

    for(int i = 0; i < D; i++) for(int j = 0; j < 101; j++) dp[i][j] = NIL;
    for(int i = 0; i < D; i++) {
        for(int j = 0; j < N; j++) {
            if(C[j].a <= T[i] && C[j].b >= T[i]) {
                if(i == 0) dp[0][C[j].c] = 0;
                else {
                    for(int k = 0; k < 101; k++) {
                        if(dp[i - 1][k] == NIL) continue;
                        dp[i][C[j].c] = max(dp[i][C[j].c], dp[i - 1][k] + abs(k - C[j].c));
                    }
                }
            }
        }
    }

    int maxv = 0;
    for(int i = 0; i < 101; i++) {
        maxv = max(maxv, dp[D - 1][i]);
    }
    cout << maxv << endl;
}


Measured memory_bytes: 3168
#pragma warning (disable :4996)
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
const double PI = cos(-1.0);
const double eps = 1e-12;
#define For(i,n,m) for(int i=n;i<=m;++i)
#define clr(a,b) memset(a,b,sizeof(a))

ll a, b, n;
int main() {
	ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> a >> b >> n;
	if (n < b) { cout << a * n / b << "\n"; }
	else if(n==b){ cout << (a * (n-1)) / b - a * ((n-1)/ b) << "\n"; }
	else {
		cout << (a * (b - 1)) / b - a * ((b - 1) / b) << "\n";
	}
	return 0;
}

Measured memory_bytes: 3612
#include <stdio.h>
#include <stdlib.h>

typedef struct {
  long counter;
	long connected;
} S_VERTICE;

int main(void) {

  long n,q;
  scanf("%ld %ld", &n, &q);
  long **tree;
  tree = (long **)malloc(sizeof (long*) * (n+1));
  for (long i = 0; i < n+1; i++) {
    tree[i] = (long *)malloc(sizeof (long) * (n+1));
    tree[i][0] = 0;
  }
  for (long i = 1; i <= n; i++) {
    tree[0][i] = 0;
  }
  long a,b;
  for (long i = 0; i < n-1; i++) {
    scanf("%ld %ld", &a, &b);
    tree[a][0]++;
    tree[a][tree[a][0]] = b;
    tree[b][0]++;
    tree[b][tree[b][0]] = a;
  }
  S_VERTICE vertices[n+1];
  for (long i = 1; i <= n; i++) {
    vertices[i].counter = 0;
    vertices[i].connected = 0;
  }
  long p,x;
  for (long i = 0; i < q; i++) {
    scanf("%ld %ld", &p, &x);
    vertices[p].counter += x;
  }

  long current = 1;
  while (1) {
    tree[0][current]++;
    if (tree[current][tree[0][current]] == vertices[current].connected) {
      continue;
    }
    if (tree[0][current] <= tree[current][0]) {
      vertices[tree[current][tree[0][current]]].counter += vertices[current].counter;
      vertices[tree[current][tree[0][current]]].connected = current;
      current = tree[current][tree[0][current]];
    } else {
      if (current == 1) {
        break;
      }
      current = vertices[current].connected;
    }
  }

  for (long i = 1; i < n; i++) {
    printf("%ld ", vertices[i].counter);
  }
  printf("%ld\n", vertices[n].counter);

  free(tree);

  return 0;
}

Measured memory_bytes: 812800
#include<cstdio>
const int maxn=1e5+7;
#define in(x) scanf("%d",&x)
int n,l,a[maxn];
int main() {

    in(n);
    in(l);
    for(int i = 1; i <= n; i++)in(a[i]);
    int j = 1;
    for(int i = 1; i < n; i++)if(a[i] + a[i + 1] > a[j] + a[j + 1])j = i;
    if(a[j] + a[j + 1] < l)puts("Impossible");
    else {
        puts("Possible");
        for(int i = n - 1; i > j; i--)printf("%d\n", i);
        for(int i = 1; i <= j; i++)printf("%d\n", i);
    }
    return 0;
}

Measured memory_bytes: 1152
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstring>

#include <vector>
#include <array>
#include <map>
#include <set>
#include <stack>
#include <queue>

#define int64 long long
using namespace std;

int main()
{
	string ss;
	cin >> ss;
	for(int64 ii = 0;ii < ss.length();ii++)
	{
		if(ss[ii] == '?')
		{
			ss[ii] = 'D';
		}
	}
	cout << ss << endl;
	return 0;
}

Measured memory_bytes: 3764
#include <stdio.h>
int main () {
    int N, K, Q;
    scanf("%d%d%d", &N, &K, &Q);
    int P[100000] = {};
    for (int i = 0; i < Q; i++) {
        int A;
        scanf("%d", &A);
        P[A-1]++;
    }
    for (int i = 0; i < N; i++) {
        if (K + P[i] - Q <= 0) {
            puts("No");
        }
        else {
            puts("Yes");
        }
    }
}

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


using namespace std;

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


Measured memory_bytes: 4844
#include <bits/stdc++.h>
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define x first
#define y second
#define debug(...) cout << "[" << #__VA_ARGS__ << ": " << __VA_ARGS__ << "]\n"
#define rd() abs((int)rng())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int>pii;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, q, ans[maxn];
struct block
{
	int S, T, X;
	block() = default;
	bool operator<(block &b)
	{
		if(X != b.X)
			return X < b.X;
		else
			return S < b.S;
	}
};
int main()
{
	ios_base::sync_with_stdio(false), cin.tie(0);
	cin >> n >> q;
	vector<block>v(n);
	for(int i = 0; i < n; i++)
	{
		cin >> v[i].S >> v[i].T >> v[i].X;
		v[i].S -= v[i].X;
		v[i].T -= v[i].X;
	}
	sort(all(v));
	set<pii>se;
	for(int i = 0; i < q; i++)
	{
		int tim;
		cin >> tim;
		se.insert({tim, i});
	}
	memset(ans, -1, sizeof ans);
	for(block bl : v)
	{
		auto it = se.lower_bound({bl.S, -mod});
		auto it2 = se.upper_bound({bl.T, -mod});
		vector<pii>er;
		while(it != it2)
		{
			ans[it->y] = bl.X;
			er.pb(*it);
			++it;
		}
		while(sz(er))
		{
			se.erase(er.back());
			er.pop_back();
		}
	}
	for(int i = 0; i < q; i++)
		cout << ans[i] << "\n";
	return 0;
}


Measured memory_bytes: 14992
/**
 *    author:  Taichicchi
 *    created: 12.09.2020 22:37:28
 **/

#include <bits/stdc++.h>

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;

int gcd(int p, int q) {
    while (q != 0) {
        int r = p % q;
        p = q;
        q = r;
    }
    return p;
}

int main() {
    int K;
    cin >> K;

    int ans = 0;

    for (int a = 1; a <= K; ++a) {
        for (int b = 1; b <= K; ++b) {
            for (int c = 1; c <= K; ++c) {
                ans += gcd(a, gcd(b, c));
            }
        }
    }
    cout << ans << endl;

    return 0;
}

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

int main(){
  set<int> t;
  multiset<int> s;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    s.insert(a);
    t.insert(a);
  }
  int sum = s.size() - t.size() + 1;
  sum /= 2;
  sum *= 2;//食べる数

  cout << n-sum << endl;

}


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

using namespace std;

using ll = long long;

const ll mod = 1e9 + 7;

vector<vector<int>> g;
ll k;

ll dfs(int r, int p = -1) {
  ll t = (p == -1 ? k - 1 : k - 2);
  
  ll ret = 1;
  
  for (int to : g[r]) {
    if (to == p) {
      continue;
    }
    
    ret = ret * t % mod;
    t--;
    
    ret = ret * dfs(to, r) % mod;
  }
  
  return ret;
}

int main() {
  int n;
  cin >> n >> k;
  
  g.resize(n);
  
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    
    g[a].push_back(b);
    g[b].push_back(a);
  }
  
  cout << k * dfs(0) % mod << endl;
}

Measured memory_bytes: 11520
import copy
S = list(input())
T = list(input())
N = len(S)
M = len(T)
lst = []

for i in range(N-M+1):
    for j in range(M):
        if (T[j] == S[i+j]) or (S[i+j] == "?"):
            exist = True
        else:
            exist = False
            break
    if exist:
        ref = copy.copy(S)
        for k in range(M):
            ref[i+k] = T[k]
        s = "".join(ref)
        s = s.replace("?","a")
        lst.append(s)

lst = sorted(lst)

if lst == []:
    print("UNRESTORABLE")
else:
    print(lst[0])


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

using ll = long long;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define ssrep(i, s, t) for (int i = s; i <= t; ++i)

#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))

using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using ld = long double;

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

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

template <typename T, size_t S> void printArray(const T (&array)[S]) {
  for (auto val : array)
    std::cout << val << ", ";
  std::cout << "\n";
}

const int mod = 1e9 + 7;
const int inf = 1e9 + 5;

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

  int n, a, b, c; std::cin >> n >> a >> b >> c;
  map<char, int> m;
  m['A'] = a, m['B'] = b, m['C'] = c;
  vector<string> s(n); rep(i, n) std::cin >> s[i];
  bool ok = true;
  string ans;
  rep(i, n) {
    char c1 = s[i][0], c2 = s[i][1];
    if (m[c1] == 0 && m[c2] == 0)
      ok = false;
    else if (m[c1] == 1 && m[c2] == 1 && i+1 < n) {
      char n1 = s[i+1][0], n2 = s[i+1][1];
      if (c1 == n1 || c1 == n2)
        m[c1]++, m[c2]--, ans += c1;
      else
        m[c1]--, m[c2]++, ans += c2;
    } else if (m[c1] > m[c2])
      m[c1]--, m[c2]++, ans += c2; 
    else
      m[c1]++, m[c2]--, ans += c1;
  }
  if (ok) {
    std::cout << "Yes" << "\n";
    rep(i, ans.size()) std::cout << ans[i] << "\n";
  } else {
    std::cout << "No" << "\n";
  }
}


Measured memory_bytes: 6676
H,W = map(int, input().split())
# 横長で扱う
if H > W:
    H,W = W,H
ans = 1 << 50
# 横 or 縦分割
if H%3 == 0 or W%3 == 0:
    print(0)
    exit()
else:
    ans = H

# 横&縦
for w in range(1,W):
    S1 = w*H
    w_rest = W-w
    S = w_rest * H
    # S1が最小の場合: 大きい方のSをなるべく小さくする
    # よって切り上げ
    S2 = w_rest * ((H-1)//2+1)
    S3 = H*W - S1 - S2
    diff1 = 1 << 50
    if S1 <= S3 <= S2:
        diff1 = S2 - S1
    # S1が最大の場合: 小さい方のSをなるべく大きくする
    # よって切り捨て
    S2 = w_rest * (H//2)
    diff2 = 1 << 50
    S3 = H*W - S1 - S2
    if S2 <= S3 <= S1:
        diff2 = S1 - S2
    # S1が真ん中 : 大小の中で一番差が小さいやつ
    h_ = S1//(w_rest)
    diff3 = 1 << 50
    S2 = w_rest * h_
    S3 = w_rest * (H-h_)
    if S2 <= S1 <= S3:
        diff3 = abs(S3-S2)
    mn = min(diff1,diff2,diff3)        
    ans = min(ans,mn)
# 縦横逆転
H,W = W,H
for w in range(1,W):
    S1 = w*H
    w_rest = W-w
    S = w_rest * H
    # S1が最小の場合: 大きい方のSをなるべく小さくする
    # よって切り上げ
    S2 = w_rest * ((H-1)//2+1)
    S3 = H*W - S1 - S2
    diff1 = 1 << 50
    if S1 <= S3 <= S2:
        diff1 = S2 - S1
    # S1が最大の場合: 小さい方のSをなるべく大きくする
    # よって切り捨て
    S2 = w_rest * (H//2)
    diff2 = 1 << 50
    S3 = H*W - S1 - S2
    if S2 <= S3 <= S1:
        diff2 = S1 - S2
    # S1が真ん中 : 大小の中で一番差が小さいやつ
    h_ = S1//(w_rest)
    diff3 = 1 << 50
    S2 = w_rest * h_
    S3 = w_rest * (H-h_)
    if S2 <= S1 <= S3:
        diff3 = abs(S3-S2)
    mn = min(diff1,diff2,diff3)        
    ans = min(ans,mn)
print(ans)


Measured memory_bytes: 73904
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.*;
import java.awt.Point;

public class Main {
    static int INF = (1 << 30) - 1;
    // static long L_INF = (1L << 62) - 1;
    // static long MOD = (long) 1e9 + 7;

    static class Edge {
        int v;
        int dist;

        Edge(int v, int dist) {
            this.v = v;
            this.dist = dist;
        }
    }

    static class Solver {
        int n;
        ArrayList<ArrayList<Integer>> g;

        // write your code
        void solve(InputReader in, PrintWriter out) {
            n = in.nextInt();
            g = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                ArrayList<Integer> al = new ArrayList<>();
                int u = in.nextInt() - 1;
                int k = in.nextInt();
                for (int j = 0; j < k; j++) {
                    al.add(in.nextInt() - 1);
                }
                g.add(al);
            }

            int[] ans = new int[n];
            Arrays.fill(ans, INF);
            ArrayDeque<Edge> que = new ArrayDeque<>();
            que.add(new Edge(0, 0));
            while (!que.isEmpty()) {
                Edge e = que.poll();
                if (ans[e.v] <= e.dist) {
                    continue;
                }
                ans[e.v] = e.dist;
                for (int i = 0; i < g.get(e.v).size(); i++) {
                    Integer next = g.get(e.v).get(i);
                    que.add(new Edge(next, e.dist + 1));
                }
            }
            for (int i = 0; i < n; i++) {
                if (ans[i] == INF) {
                    ans[i] = -1;
                }
                out.printf("%d %d\n", i + 1, ans[i]);
            }
        }

        void debug(Object... os) {
            System.err.println(Arrays.deepToString(os));
        }
    }

    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Solver solver = new Solver();
        solver.solve(in, out);
        out.close();
    }

    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;

        InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

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



Measured memory_bytes: 25492