akhauriyash/Code-Regression huggingface dataset:
let get_input n =
	let rec loop n acc =
		if n = 0 then acc
		else 
			read_line ()
			|> Str.split (Str.regexp " ")
			|> (fun [a; b] -> loop (n - 1) ((int_of_string a, int_of_string b) :: acc))
	in	
	loop n []

(* ceil(m/n) *)
let div_ceil m n =
	float_of_int m /. float_of_int n
	|> ceil
	|> int_of_float

let solve lst =
	let rec loop lst acc =
		match lst with
		| [] -> acc
		| (a, b) :: xs -> 
			loop xs (acc + ((div_ceil (a + acc) b) * b - (a + acc)))
	in
	loop lst 0
	
let () =
	let n = read_int () in
	let lst = get_input n in
	solve lst
	|> print_int
	|> print_newline


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

/* メモリ食い過ぎ注意 */
#define int long long

typedef long long ll;

ll gcd(ll a, ll b)
{
    if (a < b)
        gcd(b, a);
    if (b == 0)
        return a;
    int r;
    while ((r = a % b))
    {
        a = b;
        b = r;
    }
    return b;
}

/* define const */
const ll INF = (1ll << 60);
const ll MOD = (ll)1e9 + 7;
/* end defineing */

signed main()
{
    int n, m;
    cin >> n >> m;

    cout << (n - 1) * (m - 1) << endl;
}


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

int n, m;
vector<int> me(3), p = {5, 3, 2};
vector<vector<int>> v;
vector<vector<long double>> memo, pasc;

long double solve();

int main() {
  cout << fixed << setprecision(10);
  cin >> n >> m;
  --n;
  for (int i = 0; i < 3; ++i) cin >> me[i];
  v.assign(n, vector<int>(3, 0));
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < 3; ++j) cin >> v[i][j];
  cout << solve() << endl;
  return 0;
}

long double solve() {
  pasc.resize(m + 1);
  pasc[0] = {1.0L};
  for (int i = 1; i <= m; ++i) {
    pasc[i].push_back(1.0L);
    int psize = pasc[i - 1].size();
    for (int j = 0; j < psize - 1; ++j)
      pasc[i].push_back(pasc[i - 1][j] + pasc[i - 1][j + 1]);
    pasc[i].push_back(1.0L);
  }
  for (int i = 0; i <= m; ++i)
    for (int j = 0; j < m - i; ++j) pasc[m][i] *= 2.0L;
  for (int i = m - 1; i >= 0; --i) pasc[m][i] += pasc[m][i + 1];
  pasc[m].push_back(0);
  long double mom = 1;
  for (int i = 0; i < m; ++i) mom *= 3.0L;
  for (int i = 0; i < m + 1; ++i) pasc[m][i] /= mom;
  memo.assign(m + 1, vector<long double>(3, 0));
  for (int i = 0; i <= m; ++i)
    for (int j = 0; j < 3; ++j) {
      long long point = i * me[j];
      // rank best 3
      {
        vector<vector<long double>> dp(n + 1, vector<long double>(3, 0.0L));
        dp[0][0] = 1.0L;
        for (int k = 0; k < n; ++k)
          for (int l = 0; l < 3; ++l) {
            int time = min((point + v[k][j] - 1) / v[k][j], m + 1LL);
            dp[k + 1][l] += dp[k][l] * (pasc[m][0] - pasc[m][time]);
            if (l != 0) dp[k + 1][l] += dp[k][l - 1] * pasc[m][time];
          }
        for (int k = 0; k < 3; ++k) memo[i][j] += p[j] * dp[n][k];
      }
      // rank worst;
      {
        long double all = 1;
        for (int k = 0; k < n; ++k) {
          int time = min((point + v[k][j] - 1) / v[k][j], m + 1LL);
          all *= pasc[m][time];
        }
        memo[i][j] -= all;
      }
    }
  long double res = memo[0][0] + memo[0][1] + memo[m][2];
  for (int i = 0; i <= m; ++i)
    for (int j = 0; i + j <= m; ++j)
      res = max(res, memo[i][0] + memo[j][1] + memo[m - i - j][2]);
  return res;
}


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

vector<vector<int>> combinations_with_replacement(vector<int> iterable, int r) {
    vector<vector<int>> ret;
    vector<int> temp(r);
    ret.push_back(temp);
    while (1) {
        int i;
        for (i = r - 1; i > -1; i--) {
            if (temp[i] != iterable.size() - 1) {
                break;
            }
            else if (i == 0) {
                return ret;
            }
        }
        int val = temp[i] + 1;
        for (int j = i; j < r; j++) {
            temp[j] = val;
        }
        ret.push_back(temp);
    }
}

int main() {
    int n, m, q;
    cin >> n >> m >> q;
    vector<vector<int>> v(q);
    vector<vector<int>> ret;
    vector<int> temp(4);
    for (int i = 0; i < q; i++) {
        cin >> temp[0] >> temp[1] >> temp[2] >> temp[3];
        v[i] = temp;
    }

    vector<int> test(m);
    for (int i = 0; i < m; i++) {
        test[i] = i + 1;
    }

    ret = combinations_with_replacement(test, n);

    int tempAns = 0;
    int ans     = 0;
    for (auto tempA : ret) {
        tempAns = 0;
        for (int j = 0; j < q; j++) {
            if (tempA[v[j][1] - 1] - tempA[v[j][0] - 1] == v[j][2]) {
                tempAns += v[j][3];
            }
        }
        ans = max(ans, tempAns);
    }
    cout << ans << endl;
}

Measured memory_bytes: 9564
#include<iostream>
using namespace std;
#include<queue>
#include<vector>
#include<string>
#include<string.h>
#include<algorithm>
bool b[2003];

#define REP(i, a) for(int i=0;i<a;i++)
	int N=(1<<20);
	int max=101;
int main()
{
	pair<int,int> f;
	vector < pair<int,int> > A[100000];
	priority_queue < pair < int,int > > PQ;
	int min,b,a,k,m,n ,e,p[101],d[100000],u,v; //u ?????????v????????¨
	bool color[10001];

	cin>>n;
	REP(i, n) {
		cin>>u>>k;
		REP(i, k) {
			cin>>a>>b;
			if(b==-1) b=N;
			A[u].push_back(make_pair(a,b));
		}
		color[i]=0;
		d[i]=N;
	}
	a=0;
	d[0]=0;
	PQ.push(make_pair(0,0));
	while(!PQ.empty()){
		f=PQ.top();
		PQ.pop();
		u=f.second;
		color[u]=1;
		REP(i,A[u].size()){
			v=A[u][i].first;
			if(color[v]==1){
			continue;
			}
				if(d[v]>A[u][i].second+d[u]){
					d[v]=A[u][i].second+d[u];
					PQ.push(make_pair(d[v]*-1,v));
				}
		}
	}
	REP(i,n){
		cout<<i<<" "<<(d[i] == N ? -1 : d[i])<<endl;
	
	}
	return 0;
}

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

using namespace std;

int main()
{
	int n;
	cin>>n;
	int a[n+1];
	for(int i=0;i<n;i++)
		cin>>a[i];
	 
	sort(a,a+n);
	if(a[n/2]==a[n/2-1])
	{
	 	cout<<"0"<<endl;
	 	return 0;
	}
	else{
	 	cout<<a[n/2]-a[n/2-1]<<endl;
	 	return 0;
	}
	return 0;
}

Measured memory_bytes: 640
// Undone
#include <bits/stdc++.h>
#define int long long
#define double long double
#define INF 1e18
using namespace std;
signed main() {
  int N; cin >> N;
  vector<int> ans(26, INF);
  for (int i = 0; i < N; i++) {
    string s; cin >> s;
    vector<int> cnt(26, 0);
    for (int j = 0; j < s.length(); j++) {
      cnt[s[j]-'a']++;
    }

    for (int j = 0; j < ans.size(); j++) {
      ans[j] = min(ans[j], cnt[j]);
    }
  }
  
  for (int i = 0; i < ans.size(); i++) {
    for (int j = 0; j < ans[i]; j++) {
      cout << (char)(i+'a');
    }
  }
  cout << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
typedef pair<int,int> P; typedef pair<Int,Int> Pl;
const int mod = 1e9+7;
#define END {cout<<ans<<'\n'; return 0;}
#define ALL(v) v.begin(),v.end()
#define Pr(type) priority_queue<type>
#define gPr(type) priority_queue<type,vector<type>,greater<type>>
#define V(type) vector<type>
#define rep(i,n) for(int i=0; i<n; i++)
#define rer(i,st,en) for(int i=st; i<en; i++)
#define eb emplace_back
#define pri1(a) cout<<a<<'\n'
#define pri2(a,n) rep(i,n)cout<<a[i]<<'\n'
#define pri3(a,n) rep(i,n-1)cout<<a[i]<<' '; cout<<a[n-1]<<'\n'
template<class T> inline bool cmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool cmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }


double n,m,_,x,y,q,key;
string s,sb;
bool ok;

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

  cin>>n;
  rer(i,1,3501){
    rer(j,1,3501){
      x=(double)i*j*n/(-n*(i+j)+4*i*j);
      if(x<3501&&x>0&&(x==floor(x+0.01f))){cout<<i<<' '<<j<<' '<<x<<'\n'; return 0;}
    }
  }
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll INF=1LL<<60;
const int inf=(1<<30)-1;
const int mod=1e9+7;
int dx[8]={1,0,-1,0,-1,-1,1,1};
int dy[8]={0,1,0,-1,-1,1,-1,1};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,k;cin >> n >> k;
    string s;cin >> s;
    int cnt=0;
    int ans=0;
    rep(i,1,n){
        if(s[i]==s[i-1]){
            ans++;
        }
    }
    cout << min(n-1,ans+k*2) << endl;
}

Measured memory_bytes: 3776
n,t = map(int,input().split(' '))
cost = float('inf')
num = 0
for i in range(n):
    nc,nt = map(int,input().split(' '))
    #print(nc,nt)
    if t >= nt and nc < cost:
        cost = nc
    
if cost == float('inf'):
    print('TLE',end='')    
else:
    print(cost,end='')

Measured memory_bytes: 3060
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N=1e5+5;
template<class T>inline void read(T &res)
{
char c;T flag=1;
while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
}
ll n,k,x,sum;
int main(){
	read(n),read(k);
	for(int i=1;i<=k;i++){
		read(x);sum+=x;
	}
	if(sum>=n)puts("Yes");
	else puts("No");
	return 0;
}


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

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

    int d1[n];
    int d2[n];

    for(int i = 0; i < n; i++)
        cin >> d1[i] >> d2[i];

    int cnt = 0;

    for(int i = 0; i < n; i++){
        if(d1[i] == d2[i]){
            cnt++;
            if(cnt == 3)
                break;
        }else{
            cnt = 0;
        }
    }

    if(cnt == 3)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    

    return 0;
}

Measured memory_bytes: 3632
#include<bits/stdc++.h>
int res[100001];

using namespace std;

int fun1(int val[],int n)
{
    if(n<=1)
        return 0;

    if(res[n]!=-1)
        return res[n];

    return res[n]= min(abs(val[n]-val[n-1])+fun1(val,n-1),abs(val[n]-val[n-2])+fun1(val,n-2));

}

int fun2(int val[],int n,int ind,int dp[])
{
    if(ind>=n)
        return INT_MAX;
    if(ind==n-1)
        return 0;

    if(dp[ind]!=-1)
        return dp[ind];
    int l,r=INT_MAX;
    if(ind+1<n)
        l=abs(val[ind]-val[ind+1])+fun2(val,n,ind+1,dp);

    if(ind+2<n)
        r=abs(val[ind]-val[ind+2])+fun2(val,n,ind+2,dp);

    return dp[ind]=min(l,r);

}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin>>n;

    int arr[n];

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

    int dp[n];
    for(int i=0;i<n;i++)
        dp[i]=-1;

    cout<<fun2(arr,n,0,dp);

    return 0;
}


Measured memory_bytes: 8832
#include<iostream>

int main()
{
	int R;
	std::cin >> R;
	if (R < 1200)
		std::cout << "ABC" << std::endl;
	else if (R < 2800)
		std::cout << "ARC" << std::endl;
	else
		std::cout << "AGC" << std::endl;
	return 0;
}

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

struct ModInt {
    using M = ModInt;

    static vector<M> fact, finv;
    static long long MOD;
    long long v;

    // normalize: [-MOD, MOD**2] -> [0, MOD)
    ModInt(const long long _v = 0) : v((_v < 0) ? _v % MOD + MOD : _v % MOD) {}

    M operator+(const M& x) const { return M(v + x.v); }
    M operator-(const M& x) const { return M(v - x.v); }
    M operator*(const M& x) const { return M(v * x.v); }
    M operator/(const M& x) const { return M(v * x.inv().v); }
    M& operator+=(const M& x) { return *this = *this + x; }
    M& operator-=(const M& x) { return *this = *this - x; }
    M& operator*=(const M& x) { return *this = *this * x; }
    M& operator/=(const M& x) { return *this = *this / x; }
    bool operator==(const M& x) const { return v == x.v; }
    bool operator!=(const M& x) const { return v != x.v; }
    friend istream& operator>>(istream& input, M& x) {
        return input >> x.v, x = M(x), input;
    }
    friend ostream& operator<<(ostream& output, const M& x) {
        return output << x.v;
    }
    inline M pow(long long n) const {
        M x(v), res(1);
        while (n) {
            if (n & 1) res *= x;
            x *= x;
            n >>= 1;
        }
        return res;
    }
    inline M inv() const { return this->pow(MOD - 2); }
    static void build(int n) {
        fact.assign(n + 1, 1);
        for (int i = 1; i < n + 1; i++) fact[i] = fact[i - 1] * M(i);
        finv.assign(n + 1, fact[n].inv());
        for (int i = n; i > 0; i--) finv[i - 1] = finv[i] * M(i);
    }
    static M comb(int n, int k) {
        if (n < k || k < 0) return M(0);
        return fact[n] * finv[n - k] * finv[k];
    }
    static M extgcd(int a, int b, int& x, int& y) {
        M d(a);
        if (b) {
            d = extgcd(b, a % b, y, x);
            y -= (a / b) * x;
        } else {
            x = 1;
            y = 0;
        }
        return d;
    }
};
vector<ModInt> ModInt::fact = vector<ModInt>();
vector<ModInt> ModInt::finv = vector<ModInt>();
long long ModInt::MOD = 1e9 + 7;

int main() {
    int n;
    cin >> n;
    map<P, int> mp;
    for (int i = 0; i < n; i++) {
        ll a, b;
        cin >> a >> b;
        if (a != 0 || b != 0) {
            ll g = abs(__gcd(a, b));
            a /= g;
            b /= g;
        }
        mp[P(a, b)]++;
    }
    map<P, int> c = mp;
    ModInt ans = 1;
    int z = c[P(0, 0)];
    c[P(0, 0)] = 0;
    for (auto&& p : mp) {
        ll a = p.first.first;
        ll b = p.first.second;
        int x = c[P(a, b)] + c[P(-a, -b)];
        c[P(a, b)] = c[P(-a, -b)] = 0;
        int y = c[P(-b, a)] + c[P(b, -a)];
        c[P(-b, a)] = c[P(b, -a)] = 0;
        if (y == 0) {
            ans *= ModInt(2).pow(x);
        } else {
            ans *= ModInt(2).pow(x) + ModInt(2).pow(y) - 1;
        }
    }
    if (z) ans += z;
    cout << ans - 1 << endl;
}

Measured memory_bytes: 65920
n,d=gets.split.map(&:to_i)
x=Array.new(n).map{Array.new(d)}
n.times do |i|
    x[i]=gets.split.map(&:to_f)
end

count=0
0.upto(n-2) do |i|
    (i+1).upto(n-1) do |j|
        dist=0.0
        d.times do |k|
            dist+=(x[i][k]-x[j][k])**2
        end
        if dist**0.5==((dist**0.5).to_i)
            count+=1
        end
    end
end
puts count


Measured memory_bytes: 14696
/******techknight@3******/
#include<bits/stdc++.h>
using namespace std;
#define lld               long long int
#define ld                long double
#define pb                push_back
#define ppb               pop_back
#define mp                make_pair
#define F                 first
#define S                 second
#define nl                '\n'
#define all(c)            (c).begin(),(c).end()
#define rall(c)           (c).rbegin(),(c).rend()
#define sz(c)             (c).size()
#define tr(x,a)           for(auto &a : x)
#define psnt(x,a)         (x).find(a)!=(x).end()
#define vpsnt(x,a)        find(all(x),a)!=(x).end()
#define lexichar(x,y)     lexicographical_compare(x,x+strlen(x),y,y+strlen(y)) //modify the ranges
#define lexistr(x,y)      lexicographical_compare(x.begin(),x.end(),y.begin(),y.end()) 
#define MOD               1000000007
#define pi                3.1415926536
#define tod               10000005
#define itr(i,a,b)        for(lld i=a;i<=b;i++)
#define itrn(i,a,b)       for(lld i=a;i>=b;i--)
#define iot(n)            for(lld i=0;i<n;i++)
#define pls(n,arr)        lld arr[n]; iot(n)  cin>>arr[i];
#define bye               fflush(stdout)

typedef pair<lld,lld>     pii;
typedef pair<string,lld>  psi;
template <typename T>
bool mycomp(T x,T y){ 
   return (x==y);  //give your condition here
}
bool paircomp(const pair<lld,lld> &x,const pair<lld,lld> &y){ 
return x.second<y.second;
}

int main(){
       ios_base::sync_with_stdio(false);
             cin.tie(0);
            cout.tie(0);
         lld n; cin>>n;
         if(n==1){cout<<0<<nl; return 0;}
         bool prime[tod];
         iot(tod) if(i%2!=0) prime[i]=true;
else prime[i]=false;
prime[2]=true;
prime[1]=false;
itr(i,1,tod-1){
if(prime[i]){
for(lld j=i*i;j<=tod-1;j+=2*i)
   prime[j]=false;
      }
}
vector<lld> vv; lld ans=0;
iot(tod) if(prime[i]==true)  vv.pb(i);
lld flag=-1;
for(lld i=0;i<sz(vv);i++){
    if(n%vv[i]==0){ lld cnt=0; flag=1;
        while(n%vv[i]==0){
            cnt++;
            n/=vv[i];
        }
       lld cnt1=((long long)sqrt(1.00+8.00*cnt)-1)/2;
        ans+=cnt1;
    }
    if(n<=1)  break;
}
if(flag==-1)  cout<<1<<nl;
else if(n==1)
cout<<ans<<nl;
else if(n!=1)
cout<<ans+1<<nl;

}

Measured memory_bytes: 21372
//cats <= std <= cats + animals　であればよい
#include<stdio.h>
int main(void)
{
  int cats,std,animals;
  char str[16];
  
  fgets(str,sizeof(str),stdin);
  sscanf(str,"%d%d%d",&cats,&animals,&std);
  
  if( (cats <= std) && (std <= (cats + animals)) )
  {
    puts("YES");
  }else
  {
    puts("NO");
  }
  
  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);
        String str = sc.next();
        int a = str.length();
        for (int i = 0; i < a; i++) {
            if (i % 2 == 0) {
                System.out.print(str.charAt(i));
            }
        }
        System.out.println();
    }
}

Measured memory_bytes: 31236
#n, m = map(int,input().split())
n = int(input())
ls = list(map(int,input().split()))
#ls = [list(map(int,input().split()))[1:] for i in range(n)]
#s = input()

for i in range(n):
  if sum(ls[:i]) + sum(ls[i+1:]) - ls[i] <= 0:
    print("No")
    quit()
print("Yes")

Measured memory_bytes: 3060
/*input
5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000
*/

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

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 1e18
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<long long>
#define vi vector<int>
#define pll pair<long long,long long>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<long long,long long> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'
#define UQ(x) (x).resize(distance((x).begin(),unique(all((x)))))

using namespace std;
using namespace __gnu_pbds; 

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

ll power(ll a,ll m,ll mod){
	ll ans=1;
	while(m){
		if(m%2){
			ans*=a;
			ans%=(mod);
		}
		a=(a*a)%(mod);
		m>>=1;
	}
	return ans;
}


void init(ll* a,ll n,ll val){
	for(ll i=0;i<n;i++)
		a[i]=val;
}

void solve(){
	ll i,j,k;
	ll n,m;
	cin>>n>>m>>k;
	ll a[n],b[m];
	rep(i,0,n){cin>>a[i];if(i)a[i]+=a[i-1];}
	rep(i,0,m){cin>>b[i];if(i)b[i]+=b[i-1];}
	ll ans=0;
	ans=max(ans,(ll)(upper_bound(b,b+m,k)-b));
	rep(i,0,n){
		if(a[i]>k)break;
		ll tmp=k-a[i];
		ans=max(ans,i+1+(upper_bound(b,b+m,tmp)-b));
	}
	cout<<ans<<endl;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);	
	ll T=1;
	//cin>>T;
	while(T--){
		solve();
	}
	return 0;
}




Measured memory_bytes: 6700
/*
    Problem 25
    https://onlinejudge.u-aizu.ac.jp/problems/1160
*/
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 55;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
int h, w;
vector<vector<bool>> visited(MAX, vector<bool>(MAX));
vector<vector<int>> G(MAX, vector<int>(MAX));
/* function */
void init() {
    // for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) G[i][j] = false;
    // for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) visited[i][j] = false;
    for (int i = 0; i < h*w; i++) G[i/w][i%w] = false;
    for (int i = 0; i < h*w; i++) visited[i/w][i%w] = false;
}
void inputG() { // for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> G[i][j];
    for (int i = 0; i < h*w; i++) cin >> G[i/w][i%w];}
bool check(int x, int y) {
    if (x < 0 || h < x || y < 0 || w < y) return false;
    if (visited[x][y] || !G[x][y]) return false;
    return true;
}
void bfs(int sx, int sy) {
    queue<pii> q;
    q.push(pii(sx, sy));
    visited[sx][sy] = true;
    while (!q.empty()) {
        pii p = q.front(); q.pop();
        int x = p.first, y = p.second;
        // 八方向を探索
        for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) {
            int nx = x + dx, ny = y + dy;
            if (check(nx, ny)) {
                q.push(pii(nx, ny));
                visited[nx][ny] = true;
            }
        }
    }
}
void dfs(int x, int y) {
    // 訪れた
    visited[x][y] = true;
    // 八方向を探索
    for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) {
        int nx = x + dx, ny = y + dy;
        // 範囲外 or 訪れたことがある(visited[i][j] == true) or 海(G[i][j] = 0)
        if (check(nx, ny))
            // 再帰的に探索
            dfs(nx, ny);
    }
}
void solve() {
    int ans = 0;
    for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) {
        if (!visited[i][j] && G[i][j]) {
            ans++;
            //bfs(i, j);
            dfs(i, j);
        }
    }
    cout << ans << '\n';
}

/* main */
int main(){
    while (1) {
        cin >> w >> h;
        if (h == 0) return 0;
        init();
        inputG();
        solve();
    }
}


Measured memory_bytes: 3204
#include <bits/stdc++.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <math.h>
#include <map>
#include <bitset>
#include <cmath>
#include <set>

#define INF 1000000000000
#define MOD 1000000007 //10^9+7:合同式の法
#define JUUHATI 1000000000000000000 
#define JUUHATIPLUS 1000000000000000001 

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=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)

int main(){
    ll N,wa=0;
    double heikin1,heikin2;
    ll ans1=0,ans2=0;
    cin >> N;
    vector<ll> X(N);
    for(ll i = 0;i < N;i++)
        cin >> X.at(i);

    for(ll i = 0;i < N;i++)
        wa += X.at(i);

    heikin1 = ceil((double)wa / (double)N);
    heikin2 = floor((double)wa /(double)N);


    for(ll i = 0;i < N;i++){
        ans1 += (X.at(i) - heikin1) * (X.at(i) - heikin1);
        ans2 += (X.at(i) - heikin2) * (X.at(i) - heikin2);
    }
    if(ans1 <= ans2)
        cout << ans1 << endl;
    else
    {
        cout << ans2 << endl;
    }
    


    
    
    
}

Measured memory_bytes: 3628
#include <iostream>

using namespace std;
long long convert(string x){
    long long i = 0;
    for(int j = 0; j < x.length(); j++){
        i *= 5;
        if(x[j] == 'A'){
            i += 1;
        } else if (x[j] == 'C'){
            i += 2;
        } else if (x[j] == 'G'){
            i += 3;
        } else if (x[j] == 'T'){
            i += 4;
        }
    }
    return i;
}
int main() {
    long long n, u, a, m = 29;
    string com, x;
    cin >> n;
    long long l[m][n/m + 50];
    long long c[29] = {0};
    for(long long i = 0; i < n; i++){
            cin >> com >> x;
            u = convert(x);
            a = u%m;
        if(com == "insert"){
            l[a][c[a]] = u;
            c[a]++;
            //cout << u << " " << a <<  endl;
        } else {
            bool k = false;
            for(long long j = 0; j <= c[a]; j++){
                //cout << u << " " << l[a][j] << " " << a << endl;
                if(l[a][j] == u){
                    k = true;
                    break;
                }
            }
            if(k){
                cout << "yes" << endl;
            } else {
                cout << "no" << endl;
            }
        }
    }
    return 0;
}


Measured memory_bytes: 7744
N = gets.to_i
vs = gets.split.map(&:to_i)
sorted = vs.sort
(N - 1).times do
  n = (sorted[0] + sorted[1]) / 2.0
  if sorted.size == 2
    sorted = [n]
  else
    sorted = (sorted[2..-1] + [n]).sort
  end
end
puts sorted[0]


Measured memory_bytes: 3836
n,m=list(map(int,input().split()))
ablist=[list(map(int,input().split())) for _ in range(n)]
cdlist=[list(map(int,input().split())) for _ in range(m)]

for ab in ablist:
  d=float("inf")
  index=-1
  tmp=0
  for n,cd in enumerate(cdlist):
    tmp=abs(ab[0]-cd[0])+abs(ab[1]-cd[1])
    if tmp<d:
      d=tmp
      index=n
  print(index+1)

Measured memory_bytes: 39408
count = 0

def rec(l, r):
    global count
    global h
    global s

    if l >= r:
        return

    zero = list()
    for i in range(l, r):
        if h[i] == 0:
            zero.append(i)

    if len(zero) > 0:
        for i in zero:
            if not (l, i) in s:
                rec(l, i)
                s.add((l, i))
            if not (i+1, r) in s:
                rec(i+1, r)
                s.add((i+1, r))
    else:
        for i in range(l, r):
            h[i] -= 1
        count += 1
        rec(l, r)

n = int(input())
h = list(map(int, input().split()))
s = set()

rec(0, len(h))
print(count)


Measured memory_bytes: 3572
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
using namespace std;
const long long int INF = 1e18;
const long long int mod = 1e9+7;

typedef pair<ll, ll> pairs;
typedef vector<pairs> p;

struct Edge{
    ll to, weight;
    Edge(ll t, ll w) : to(t), weight(w){}
};
using graph = vector<vector<ll>>;
using Graph = vector<vector<Edge>>;


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

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

ll keta(ll N){
    int tmp{};
    while( N > 0 ){
        tmp += ( N % 10 );
        N /= 10;
    }
    N = tmp;
    return N;
}

// 回文
bool kai(string S){
    bool flag = true;
    for(ll i=0;i<S.size()/2;++i){
        if(S[i] != S[S.size()-i-1]){
            flag = false;
            break;
        }
    }
    return flag;
}
// ---------------------------------------------

int main(){
    ll n;
    cin>>n;
    vector<ll> t(n+1);
    t[0] = 0;
    vector<pair<ll, ll>> g(n+1);
    g[0] = make_pair(0, 0);
    for(ll i=1;i<n+1;++i){
        ll x, y;
        cin>>t[i]>>x>>y;
        g[i] = make_pair(x, y);
    }

    string ans = "Yes";
    for(ll i=1;i<n+1;++i){
        ll sum = abs(g[i].first - g[i-1].first) + abs(g[i].second - g[i-1].second);
        ll time = t[i] - t[i-1];
        //cout<<sum<<' '<<time<<endl;
        if(sum > time){
            ans = "No";
            break;
        }
        if(time % 2 != sum % 2){
            ans = "No";
            break;
        }
    }
    cout<<ans<<endl;

    return 0;
}

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

int main(){
  int n;cin>>n;
  int cnt=0;
  for(int i=0;i<n;++i){
    int a,b;cin>>a>>b;
    if(a==b) cnt++;
    else cnt=0;
    
    if(cnt==3){
      cout<<"Yes"<<endl;
      return 0;
    }
  }
  cout<<"No"<<endl;
  return 0;
}

Measured memory_bytes: 3580
from collections import Counter

def readlines(n):
    for _ in range(n):
        yield input()

def main():
    N = int(input())
    c = Counter(readlines(N))
    maxi = max(c.values())
    for k, v in sorted(c.items(), key=lambda x: x[0]):
        if v == maxi:
            print(k)




main()

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


char get_op(int i) {
    if(i==0) return '+';
    else return '-';
}


int main() {
    string input;
    cin >> input;
    int A, B, C, D;

    int number;
    number = stoi(input);
    D = number % 10;
    number = number / 10;
    C = number % 10;
    number = number / 10;
    B = number % 10;
    number = number / 10;
    A = number;
    
    // cout << A << B << C << D << endl;

    // cout << "debug" << endl;
    for(int i=0; i<2; ++i) {
        for(int j=0; j<2; ++j) {
            for(int k=0; k<2; ++k) {
                int sum = A;
                if(i==0) sum += B;
                else if (i==1) sum -= B;
                if(j==0) sum += C;
                else if (j==1) sum -= C; 
                if(k==0) sum += D;
                else if (k==1) sum -= D;
                // cout << sum << endl;
                if(sum == 7) {
                    // cout << "found" << endl;
                    // string result = to_string(A) + get_op(i) + to_string(B) + get_op(j) + to_string(C) + get_op(k) + to_string(D) + "=7";
                    cout << A << get_op(i) << B << get_op(j) << C << get_op(k) << D << '=' << 7 << endl;
                    return 0;
                }
                sum = 0;
            }            
        }
    }
}

Measured memory_bytes: 256
#pragma region head
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, a, b) for (int i = a; i <= (b); ++i)
#define repr(i, a, b) for (int i = a; i >= (b); --i)
#define bit(n) (1LL << (n))
#define len(x) ((ll)(x).size())
#define debug(var) cout << "[" << #var << "]\n" << var << endl
#define int long long
typedef long long ll;
#define double long double
typedef double ld;
const int INF = 1001001001001001001ll;
const ll LINF = 1001001001001001001ll;
const int MOD = 1000000007;
const double EPS = 1e-9;
const double PI = acos(-1.0);

template <typename T>
ostream& operator<<(ostream& s, const vector<T>& v) {
  int len = v.size();
  for (int i = 0; i < len; ++i) {
    s << v[i];
    if (i < len - 1)
      s << ' ';
  }
  return s;
}

template <typename T>
ostream& operator<<(ostream& s, const vector<vector<T>>& vv) {
  int len = vv.size();
  for (int i = 0; i < len; ++i) {
    s << vv[i];
    if (i != len - 1)
      s << '\n';
  }
  return s;
}

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;
}

__attribute__((constructor)) void initial() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
#pragma endregion

int sign(int x) {
  if (x == 0)
    return 0;
  else
    return x / abs(x);
}

signed main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> sum(n);
  rep(i, n) {
    cin >> a[i];
    sum[i] = (i == 0 ? 0 : sum[i - 1]) + a[i];
  }

  int ans = INF;

  for (int sign_raw = -1; sign_raw <= 1; sign_raw += 2) {
    int sg = sign_raw, temp = 0, cum = 0;
    rep(i, n) {
      int s = sum[i] + cum;
      if (sg == 1) {
        if (sign(s) != 1) {
          int diff = 1 - s;
          temp += diff;
          cum += diff;
        }
      } else {
        if (sign(s) != -1) {
          int diff = s - (-1);
          temp += diff;
          cum -= diff;
        }
      }
      sg *= -1;
    }
    chmin(ans, temp);
  }

  cout << ans << endl;
}

Measured memory_bytes: 1792
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace MyProject
{
    class Program
    {
        static void Main(string[] args)
        {
#if Local
            var sw = new Stopwatch();
            sw.Start();
            Console.SetIn(new StreamReader("input.in"));
#endif

            var solver = new Solver();
            solver.Solve();
#if Local
            Console.WriteLine($"{sw.ElapsedMilliseconds}ms");
#endif
        }
    }

    class Solver
    {
        public void Solve()
        {
            var inputs = Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray();
            var N = inputs[0];
            var K = inputs[1];
            var answer = N - (K - 1);

            Console.WriteLine(answer.ToString());
        }
    }
}


Measured memory_bytes: 11220
n = int(input())
seven = int(n // 7)
four = int(n // 4)
count = 0
for i in range(seven + 1):
    for j in range(four + 1):
        if 7 * i + 4 * j == n:
            count += 1
if count == 0:
    print('No')
else:
    print('Yes')

Measured memory_bytes: 38384
#include <bits/stdc++.h>
using namespace std;
using P = pair<int,int>;

int main(){
  int h, w;
  cin >> h >> w;
  vector<string> smp(h);
  for(int i=0; i<h; ++i) cin >> smp[i];
  vector<vector<P>> memo(h, vector<P>(w));
  int ans = 0;
  for(int i=0; i<h; ++i) for(int j=0; j<w; ++j){
    if(smp[i][j] == '#') continue;
    
    if(i>0 && smp[i-1][j] == '.'){
      memo[i][j].first = memo[i-1][j].first;
    }else{
      int cnt = 0;
      int x = i-1;
      while(x >=0 && smp[x][j] == '.'){
        ++cnt;
        --x;
      }
      x = i+1;
      while(x <h && smp[x][j] == '.'){
        ++cnt;
        ++x;
      }
      memo[i][j].first = cnt + 1;
    }
    
    if(j>0 && smp[i][j-1] == '.'){
      memo[i][j].second = memo[i][j-1].second;
    }else{
      int cnt = 0;
      int y = j-1;
      while(y >=0 && smp[i][y] == '.'){
        ++cnt;
        --y;
      }
      y = j+1;
      while(y <w && smp[i][y] == '.'){
        ++cnt;
        ++y;
      }
      memo[i][j].second = cnt + 1;
    }
    ans = max(ans, memo[i][j].first +memo[i][j].second -1);
    //cout << "i:" << i << " j:" << j << " ans:" << ans << endl;
  }
  cout << ans << endl;
}

Measured memory_bytes: 42336
void main()
{
    long n, q;
    rdVals(n, q);

    auto uf = UnionFind(n);
    foreach (i; 0 .. q)
    {
        long com, x, y;
        rdVals(com, x, y);

        if (com)
        {
            writeln(uf.same(x, y) ? 1 : 0);
        }
        else
        {
            uf.unite(x, y);
        }
    }
}

struct UnionFind
{
    long[] par, cnt;

    this(long n)
    {
        par.length = n, cnt.length = n;
        foreach (i; 0 .. n)
        {
            par[i] = i;
        }
        cnt[] = 1L;
    }

    long root(long x)
    {
        if (par[x] == x) return x;
        else return par[x] = root(par[x]);
    }

    bool same(long x, long y)
    {
        return root(x) == root(y);
    }

    void unite(long x, long y)
    {
        x = root(x), y = root(y);
        if (x == y) return;
        if (cnt[x] > cnt[y]) swap(x, y);
        cnt[y] += cnt[x];
        par[x] = y;
    }

    long size(long x)
    {
        return cnt[root(x)];
    }
}

enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;

T rdElem(T = long)()
if (!is(T == struct))
{
    return readln.chomp.to!T;
}

alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);

T rdElem(T)()
if (is(T == struct))
{
    T result;
    string[] input = rdRow!string;
    assert(T.tupleof.length == input.length);
    foreach (i, ref x; result.tupleof)
    {
        x = input[i].to!(typeof(x));
    }
    return result;
}

T[] rdRow(T = long)()
{
    return readln.split.to!(T[]);
}

T[] rdCol(T = long)(long col)
{
    return iota(col).map!(x => rdElem!T).array;
}

T[][] rdMat(T = long)(long col)
{
    return iota(col).map!(x => rdRow!T).array;
}

void rdVals(T...)(ref T data)
{
    string[] input = rdRow!string;
    assert(data.length == input.length);
    foreach (i, ref x; data)
    {
        x = input[i].to!(typeof(x));
    }
}

void wrMat(T = long)(T[][] mat)
{
    foreach (row; mat)
    {
        foreach (j, compo; row)
        {
            compo.write;
            if (j == row.length - 1) writeln;
            else " ".write;
        }
    }
}

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


Measured memory_bytes: 3476
program main
  implicit none
  integer :: n, m, k, a(30), i, j
  logical, allocatable :: flag(:, :)
  read (*, *) n, m
  allocate (flag(n, m))
  flag(:, :) = .false.
  do i = 1, n
    read (*, *) k, (a(j), j = 1, k)
    do j = 1, k
      flag(i, a(j)) = .true.
    end do
  end do
  print "(i0)", count(all(flag(:, :), dim=1))
end program main


Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main()
{
    int sum=0;
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        sum+=(s[i]-'0');
    }
    if(sum%9==0)
        printf("Yes\n");
    else
        printf("No\n");
}


Measured memory_bytes: 3692
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for(int i = a; i < b; i++)
#define rrep(i, a, b) for(int i = b - 1; i >= a; i--)
#define ALL(a) a.begin(), a.end()
#define pii pair<int,int>
#pragma GCC optimize("Ofast")
#define pcnt __builtin_popcount
#define buli(x) __builtin_popcountll(x)
#define pb push_back
#define mp make_pair
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define isSquare(x) (sqrt(x)*sqrt(x) == x)
template<class T>bool chmax(T &a, const T &b) {if(a<b){a = b; return 1;} return 0; };
template<class T>bool chmin(T &a, const T &b) {if(a>b){a = b; return 1;} return 0; };
inline void IN(void){return;}
template <typename First, typename... Rest> void IN(First& first, Rest&... rest){cin >> first;IN(rest...);return;}
inline void OUT(void){cout << "\n";return;}
template <typename First, typename... Rest> void OUT(First first, Rest... rest){cout << first << " ";OUT(rest...);return;}
const double EPS = 1e-9;
const int mod = 1e9 + 7;
const int INF = 1e9;
const long long LLINF = 1e18;
long long lcm(ll a, ll b){return a * b / __gcd(a,b);}
struct IoSetup { IoSetup() {
    cin.tie(nullptr);ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
} } iosetup;
template< int mod > struct ModInt {
    int x; ModInt() : x(0) {}
    ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
    ModInt &operator+=(const ModInt &p) {if((x += p.x) >= mod) x -= mod;return *this;}
    ModInt &operator-=(const ModInt &p) {if((x += mod - p.x) >= mod) x -= mod;return *this;}
    ModInt &operator*=(const ModInt &p) {x = (int) (1LL * x * p.x % mod);return *this;}
    ModInt &operator/=(const ModInt &p) {*this *= p.inverse();return *this;}
    ModInt operator-() const { return ModInt(-x); }
    ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
    ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
    ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
    ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
    bool operator==(const ModInt &p) const { return x == p.x; }
    bool operator!=(const ModInt &p) const { return x != p.x; }
    ModInt inverse() const {int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); }return ModInt(u);}
    ModInt pow(int64_t n) const {ModInt ret(1), mul(x); while(n > 0) {if(n & 1) ret *= mul;mul *= mul;n >>= 1;}return ret;}
    friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x;}
    friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt< mod >(t); return (is); }
    static int get_mod() { return mod; }
}; using modint = ModInt< mod >;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main(){
    iosetup;
    ll N, A, B;cin >> N >> A >>B;
    if(A > B) cout << 0 << endl;
    else if(N == 1){
        if(A == B) cout << 1 << endl;
        else cout << 0 << endl;
    }
    else cout << 1 + (B - A) * (N - 2) << endl;
    return 0;
}

Measured memory_bytes: 256
n = int(input())
a = list(map(int, input().split()))

while True:
    amin = 10**10
    for i in a:
        if i != 0 and amin > i:
            amin = i
    c = 0
    flag = False
    for i in range(len(a)):
        if amin == a[i] and flag == False:
            flag = True
            continue
        if a[i] <= amin and a[i] != 0:
            c += 1
            a[i] = 0
        if a[i] != 0:
            a[i] %= amin
            c += 1
    if c == 0:
        break
print(amin)

Measured memory_bytes: 14252
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <functional>
#include <queue>
#include <string>
#include <cstring>
#include <numeric>
#include <cstdlib>
#include <cmath>
using namespace std;

typedef long long ll;

#define INF 10e10
#define rep(i,n) for(int i=0; i<n; i++)
#define rep_r(i,n,m) for(int i=m; i<n; i++)
#define END cout << endl
#define MOD 1000000007
#define pb push_back

int main() {
  int n,m,r;
  cin >> n >> m >> r;

  vector<int> city(r,0);
  rep(i,r) {
    cin >> city[i];
    city[i]--;
  }

  bool d[n+1][n+1];
  ll cost[n+1][n+1];

  rep(i,n)rep(j,n){
    d[i][j] = false;
    if (i == j) {
      cost[i][j] = 0;
    } else {
      cost[i][j] = INF;
    }
  }

  rep(i,m) {
    int x,y,c;
    cin >> x >> y >> c;
    x--,y--;

    cost[x][y] = cost[y][x] = c;
    d[x][y] = d[y][x] = true;
  }

  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
      }
    }
  }

  ll ans = 0;
  sort(city.begin(), city.end());

  rep(i,city.size() - 1) {
    ans += cost[city[i]][city[i+1]];
  }
  while (next_permutation(city.begin(), city.end())) {
    ll tmp = 0;
    rep(i,city.size() - 1) {
      tmp += cost[city[i]][city[i+1]];
    }
    ans = min(ans,tmp);
  }

  cout << ans << endl;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, a, b) for (int i = a; i <= (b); ++i)
#define repr(i, a, b) for (int i = a; i >= (b); --i)
typedef long long ll;
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;

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

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

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

  vector<ll> h(N);
  rep(i, N) cin >> h[i];
  sort(all(h));

  vector<ll> diff(N - 1);
  rep(i, N - 1) { diff[i] = h[i + 1] - h[i]; }

  int l = 0, r = 0;

  ll ans = LINF;
  ll temp = 0;

  rep(i, N - K + 1) {
    int nl = i;
    int nr = i + K - 1;

    while (l < nl) {
      temp -= diff[l];
      l++;
    }
    while (r < nr) {
      temp += diff[r];
      r++;
    }
    chmin(ans, temp);
  }
  cout << ans << endl;
}

Measured memory_bytes: 1792
#include <iostream>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <numeric>
#include <climits>
#include <math.h>
#include <memory.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rrep(i, k, n) for(int i = (k); i < (int)(n); i++)
using namespace std;
using ll = long long;
int N, M;
int cnt = 0;
int ans = 0;
int i_index;
int tmp;
int dh[4] = {1, 0, -1, 0};
int dw[4] = {0, 1, 0, -1};
string board[64];
int main(){
    cin >> N;
    int a[100010];
    rep(i, N){
        cin >> a[i];
        a[i]--;//1 - 2 から 0 - 1へ
    }
    int x = 0;
    rep(i, 100010){
        if(x == 1){
            cout << i << endl;
            return 0;
        }
        x = a[x];
    }
    cout << -1 << endl;
    return 0;
}

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

int main(){
  int n, x;
  cin >> n >> x;
  int mmin = 10000000, msum = 0;
  for(int i = 0; i < n; i++){
    int m;
    cin >> m;
    msum += m;
    mmin = min(mmin, m);
  }
  cout << n + (x - msum) / mmin << endl;
}

Measured memory_bytes: 3620
#include<bits/stdc++.h>
using namespace std;
long long N=0,M=0,ans=0,sum=0;


int main()
{
  cin>>N>>M;
  vector<pair<long,long>>A(N);
  for(int i=0;i<N;i++){
    cin>>A[i].first>>A[i].second;
  }
  sort(A.begin(),A.end());
  long long i=0;
  while (ans<M) {
    if(A[i].second==0){
      i++;
    }else{
      A[i].second--;
      sum+=A[i].first;
      ans++;
    }
    //ans++;
  }
  cout<<sum;
  return 0;
}

Measured memory_bytes: 1792
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<ctime>
#include<algorithm>
using namespace std;
const int MR=2000000;

long long f(long long b, long long n){
	long long ans=0;
	while(n>0){
		ans+=n%b;
		n/=b;
	}
	return ans;
}

int main(){
	long long n,s;
	cin>>n>>s;
	if(n==s) {cout<<n+1<<endl; return 0;}
	if(n<s) {cout<<-1<<endl; return 0;}
	for(long long b=2;b*b<=n;b++){
		if(f(b,n)==s){cout<<b<<endl; return 0;}
	}
	for(long long p=(long long)sqrt(n);p>=1;p--){
		if((n-s)%p!=0)continue;
		long long b=(n-s)/p+1;
		if(f(b,n)==s) {cout<< b <<endl; return 0;}
	}
	cout<< -1 <<endl;
	return 0;
}



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

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

    int locate = X - K + 1;
    for(int i = X - K + 1; i <= X + K - 1; i++) {
        if (locate < -1000000) continue;
        if (locate > 1000000) break;
        cout << i << " ";
    }
    cout << endl;
}

Measured memory_bytes: 256
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            String we = br.readLine();
            String line = br.readLine();
            String[] w = line.split("\\s");
            int[] arr = new int[w.length];
            
            System.out.println(line);
            for(int i=0; i<w.length; i++){
                arr[i] = Integer.parseInt(w[i]);
            }
            
            insertSort(arr);
            
        }catch(IOException e) {
            e.printStackTrace();
        }
        
    }
    public static void insertSort(int[] arr){
        for(int i=1; i<arr.length; i++){
            insert(i, arr);
        }
    }
    
    public static void insert(int i, int[] arr){
        int tmp = arr[i];
        for(int j=i-1; j>=0; j--){
            if (arr[j] > tmp){
                arr[j + 1] = arr[j];
                arr[j] = tmp;
            } else {
                arr[j + 1] = tmp;
                break;
            }
        }
        String[] line = new String[arr.length];
        for(int o=0; o<arr.length; o++){
            line[o] = String.valueOf(arr[o]);
        }
        System.out.println(String.join(" ", line));
    }
}



Measured memory_bytes: 27760
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <unordered_map>

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int main()
{
	int n;
	cin>>n;
	set<string> st;
	string w[100];
	for(int i=0; i<n; i++){
		cin>>w[i];
		st.insert(w[i]);
	}
	if(st.size()!=n){
		cout<<"No"<<endl;
		return 0;
	}
	for(int i=0; i<n-1; i++){
		if(w[i].back()!=w[i+1][0]){
			cout<<"No"<<endl;
			return 0;
		}
	}
	cout<<"Yes"<<endl;
	return 0;
}

Measured memory_bytes: 256