akhauriyash/Code-Regression huggingface dataset:
/*
 * 2401.cc: Equation
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_FN = 1000;
const int MAX_VN = 11;

/* typedef */

enum { T_CON, T_VAR, T_NOT, T_AND, T_OR, T_IMP };

struct Formula {
  int t, v;
  Formula *f0, *f1;
  Formula() {}
};

/* global variables */

int vn, vids[MAX_VN], fn;
Formula fps[MAX_FN];

/* subroutines */

inline Formula *allocate_f() { return &fps[fn++]; }
inline int allocate_v(int va) {
  if (vids[va] < 0) vids[va] = vn++;
  return vids[va];
}

Formula *formula(string &s, int &pos) {
  Formula *f = allocate_f();
  if (s[pos] == 'T' || s[pos] == 'F') {
    f->t = T_CON;
    f->v = (s[pos++] == 'T');
    return f;
  }
  if (s[pos] >= 'a' && s[pos] <= 'k') {
    f->t = T_VAR;
    f->v = allocate_v(s[pos++] - 'a');
    return f;
  }
  if (s[pos] == '-') {
    f->t = T_NOT;
    pos++; // '-'
    f->f0 = formula(s, pos);
    return f;
  }

  pos++; // '('
  f->f0 = formula(s, pos);
  switch (s[pos++]) {
  case '*': f->t = T_AND; break;
  case '+': f->t = T_OR; break;
  case '-': f->t = T_IMP; pos++; break;
  }
  f->f1 = formula(s, pos);
  pos++; // ')'
  return f;
}

bool calc(Formula *f, int bits) {
  bool ret;
  switch (f->t) {
  case T_CON: ret = f->v; break;
  case T_VAR: ret = (bits >> f->v) & 1; break;
  case T_NOT: ret = ! calc(f->f0, bits); break;
  case T_AND: ret = calc(f->f0, bits) & calc(f->f1, bits); break;
  case T_OR:  ret = calc(f->f0, bits) | calc(f->f1, bits); break;
  case T_IMP: ret = ! calc(f->f0, bits) | calc(f->f1, bits); break;
  }
  return ret;
}

/* main */

int main() {
  for (;;) {
    string s;
    cin >> s;
    if (s == "#") break;

    vn = fn = 0;
    memset(vids, -1, sizeof(vids));

    int pos = 0;
    Formula *f0 = formula(s, pos);
    pos++; // '='
    Formula *f1 = formula(s, pos);
    //printf("vn=%d\n", vn);

    int vbits = 1 << vn;
    bool ok = true;

    for (int bits = 0; bits < vbits; bits++)
      if (calc(f0, bits) != calc(f1, bits)) {
	ok = false;
	break;
      }

    cout << (ok ? "YES" : "NO") << endl;
  }
  return 0;
}

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

using ll = long long;

int main() {
    int N;
    string S;
    cin >> N >> S;
    rep(i, S.size()) {
        S[i] = (S[i] + N - 'A') % 26 + 'A';
    }
    cout << S << endl;
}

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

using namespace std;

int main() {
	int n, sum = 0, sa;
	cin >> n;
	sa = n;
	while (n) {
		sum += n % 10;
		n /= 10;
	}
	if (sa % sum == 0)
		cout << "Yes\n";
	else
		cout << "No\n";
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long int 
using namespace std; 
ll dp[100005][4];
ll solve(vector<vector<ll>>&v,ll n,ll tag)
{
    if(n<=0)
      return 0;

      if(dp[n][tag]!=-1)
        return dp[n][tag];
    ll ans=0;
    for(int i=1;i<=3;i++)
    {
      if(i!=tag)
      {
        ans=max(ans,v[n][i]+solve(v,n-1,i));
      }
    }
    return dp[n][tag]=ans;
}
int main() 
{
  int t=1;
  //cin>>t;
  while (t--)
  {
    memset(dp,-1,sizeof(dp));
      ll n;
    cin>>n;
    vector<vector<ll>>v(n+1);
    for(int i=1;i<=n;i++)
    {
        v[i].push_back(0);
      for(int j=1;j<=3;j++)
      {
        ll x;
        cin>>x;
        v[i].push_back(x);
      }
    }
    ll p=0;
    cout<<solve(v,n,p)<<endl;
  }
  

  
} 


Measured memory_bytes: 21212
#include <iostream>
#include <queue>
#include <string>

using namespace std;

bool match(int b, string s, string t) {
    for (int i = 0; i < static_cast<int>(t.size()); i++) {
        if (b + i >= static_cast<int>(s.size()) || s[b + i] != t[i]) {
            return false;
        }
    }

    return true;
}

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

    bool res = false;

    queue<int> que;
    que.push(0);
    while (!que.empty()) {
        int idx = que.front();
        que.pop();

        if (idx == static_cast<int>(s.size())) {
            res = true;
            break;
        }

        if (match(idx, s, "dream")) { que.push(idx + 5); }
        if (match(idx, s, "dreamer")) { que.push(idx + 7); }
        if (match(idx, s, "erase")) { que.push(idx + 5); }
        if (match(idx, s, "eraser")) { que.push(idx + 6); }
    }

    cout << (res ? "YES" : "NO") << endl;
}


Measured memory_bytes: 512
let n = read_int () in
let x = Array.init n (fun _ -> Scanf.scanf " %d" (fun x -> x)) in
let rec loop i minx =
    if i = 101 then minx else
        let sum = Array.fold_left (fun acc x -> acc + (x - i) * (x - i)) 0 x in
        loop (i + 1) (min sum minx)
in
loop 0 99999999 |> Printf.printf "%d\n"

Measured memory_bytes: 2432
A,B=map(int,input().split())

A=max(A-1,0)
LA=[]
LB=[]
if A%4==1 or A%4==2:
    LA.append(1)
else:
    LA.append(0)
if B%4==1 or B%4==2:
    LB.append(1)
else:
    LB.append(0)
    
for i in range(2,41):
    if (A%(2**i))%2==1:
        LA.append(0)
    else:
        if (A%(2**i))>=(2**(i-1)):
            LA.append(1)
        else:
            LA.append(0)
    if (B%(2**i))%2==1:
        LB.append(0)
    else:
        if (B%(2**i))>=(2**(i-1)):
            LB.append(1)
        else:
            LB.append(0)
Lans=[]
for i in range(len(LA)):
    Lans.append((LA[i]+LB[i])%2)
#print(Lans)
ans=0
for i in range(len(Lans)):
    ans+=(2**i)*Lans[i]
print(ans)

Measured memory_bytes: 3064
N = int(input())
a = list(map(int,input().split()))

ans = 1

b= [a[0]]
if N > 1:
    for i in range(1,N):
        if a[i] != a[i-1]:
            b.append(a[i])

    if b[0] < b[1]:
        up_down = 1
    else:
        up_down = -1

i = 1
while i < len(b):
    if up_down*(b[i] - b[i-1]) < 0:
        ans += 1
        if b[i] < b[min(len(b)-1,i+1)]:
            up_down = 1
        else:
            up_down = -1
        i += 2
    else:
        i += 1

print(ans)

Measured memory_bytes: 14252
#include<iostream>
using namespace std;
int p[104730]={0},n,s,i,j;
int main(){p[1]=1;for(i=2;i*i<104730;i++)if(!p[i])for(j=2;i*j<104730;j++)p[i*j]=1;while(cin>>n,n){for(i=2,s=0;n;i++)if(!p[i])s+=i,n--;cout<<s<<endl;}}

Measured memory_bytes: 0
#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <random>
#include <functional>
#include <stack>
using namespace std;

template<class T>
void input(T& I) {
	cin >> I;
	return;
}

template<class T>
void input(vector<T>& V) {
	for (auto itr = V.begin(); itr != V.end(); itr++)cin >> *itr;
	return;
}

template<class T>
void output(vector<T>& V) {
	for (T& itr : V)cout << itr << endl;
	return;
}

void output(string S) {
	cout << S << endl;
	return;
}

template<class T>
void output(T& O) {
	cout << O << endl;
	return;
}

int main() {
	string S;
	input(S);
	if (S == string("Sunny"))output(string("Cloudy"));
	else if (S == string("Cloudy"))output(string("Rainy"));
	else output(string("Sunny"));
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int a, b, c, d;
	int sum ;
	cin >> a >> b >> c >> d;
	if (b < c || d < a)
		sum = 0;
	else
	{
		sum = min(b, d) - max(a, c);
	}
	cout << sum << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#define LL long long
#define pb push_back
#define mp make_pair
#define INF 0x3f3f3f3f
#define Inf 1000000000000000000LL
#define F first
#define S second
using namespace std;
typedef pair<int,int>pii;
namespace UF{

int par[100010],rnk[100010];
void init(int n){
	for(int i=1;i<=n;i++)par[i]=i,rnk[i]=0;
}
int find(int x){
	if(x==par[x])return x;
	return par[x]=find(par[x]);
}
bool unite(int x,int y){
	x=find(x),y=find(y);
	if(x==y)return 0;
	if(rnk[x]<rnk[y])par[x]=y;
	else{
		par[y]=x;
		if(rnk[x]==rnk[y])rnk[x]++;
	}
	return 1;
}
bool same(int x,int y){
	return find(x)==find(y);
}

};
using UF::unite;
using UF::par;
int n;
int px[100010],py[100010];
pii sx[100010],sy[100010];
int dist(int x,int y){return min(abs(px[x]-px[y]),abs(py[x]-py[y]));}
set<pair<int,pair<int,int> > >ss;
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>px[i]>>py[i];
		sx[i]=mp(px[i],i);
		sy[i]=mp(py[i],i);
		par[i]=i;
	}
	sort(sx+1,sx+n+1);
	sort(sy+1,sy+n+1);
	for(int i=1;i<n;i++){
		ss.insert(mp(dist(sx[i].S,sx[i+1].S),mp(sx[i].S,sx[i+1].S)));
		ss.insert(mp(dist(sy[i].S,sy[i+1].S),mp(sy[i].S,sy[i+1].S)));
	}
	int res=0;
	while(!ss.empty()){
		auto cur=*ss.begin();
		ss.erase(cur);
		int u=cur.S.F,v=cur.S.S;
		res+=unite(u,v)*dist(u,v);
		// cout<<u<<' '<<v<<' '<<dist(u,v)<<' '<<unite(u,v)<<endl;
		// if(!same(u,v))unite(u,v),res+=dist(u,v);
	}
	cout<<res<<endl;
	return 0;
}


Measured memory_bytes: 15872
#include <bits/stdc++.h>
#define REP(i, m, n) for(int (i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
using namespace std;
using Graph = vector<vector<int>>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1e9+7;
const ll LINF = 1LL<<60;

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

    if (k % 2 == 0) cout << k / 2 * k / 2 << endl;
    else cout << k / 2 * (k + 1) / 2 << endl;


    return 0;
}

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

using namespace std;

typedef long long ll;

ll C, D, W, X;
ll E[16][31], F[16][31];
ll memo[31][51][6];
const int dx[] = {-1, 1};

ll solve(ll d, ll w, ll x)
{
  if (d == D) return 0;

  ll &res = memo[d][w][x];
  if (res != -1) return res;
  
  res = 0;
  res = max(res, solve(d+1, w, x));

  for (int i = 0; i < C; i++) {
    if (w + F[i][d] <= W && E[i][d] != 0) {
      res = max(res, solve(d+1, w + F[i][d], x) + E[i][d]);      
    }    
  }

  if (x+1 <= X) {
    for (int i = 0; i < C; i++) {
      for (int j = 0; j < 2; j++) {
	ll nx = i, cost = 0, f = 0;
	while (1) {
	  if (nx < 0 || nx >= C || E[nx][d] == 0) break;
	  f += F[nx][d];
	  cost += E[nx][d];
	  if (w + f <= W) {
	    res = max(res, solve(d+1, w + f, x+1) + cost);
	  }
	  nx += dx[j];
	}
      }   
    }
  }
  return res;
}

int main()
{
  while (cin >> C >> D >> W >> X, C) {
    for (int i = 0; i < C; i++) {
      for (int j = 0; j < D; j++) {
	cin >> E[i][j];
      }
    }

    for (int i = 0; i < C; i++) {
      for (int j = 0; j < D; j++) {
	cin >> F[i][j];
      }
    }

    memset(memo, -1, sizeof(memo));
    
    ll res = solve(0, 0, 0);
    cout << res << endl;
  }
  return 0;
}

Measured memory_bytes: 1244
#include<stdio.h>
#include<string.h>
int main()
{
	int i,j,k=0,cnt=0,a=1;
	char dream[10]={'d','r','e','a','m'},dreamer[10]={'d','r','e','a','m','e','r'},eraser[10]={'e','r','a','s','e','r'},erase[10]={'e','r','a','s','e'};
	char T[100005];
	int cek[100000]={0};
	scanf("%s",T);
	for(i=0;i<strlen(T);i++)
	{
		k=0;
		cnt=0;
		while(T[i+k]==eraser[k]&&i+k<strlen(T)&&cek[i]==0)
		{
			cnt++;
			k++;
			if(cnt==strlen(eraser))
			{
				k--;
				while(k>=0)
				{
					cek[i+k]=1;
					k--;
				}
			}
		}
	}
	for(i=0;i<strlen(T);i++)
	{
		k=0;
		cnt=0;
		while(T[i+k]==erase[k]&&i+k<strlen(T)&&cek[i]==0)
		{
			cnt++;
			k++;
			if(cnt==strlen(erase))
			{
				k--;
				while(k>=0)
				{
					cek[i+k]=1;
					k--;
				}
			}
		}
	}
	for(i=0;i<strlen(T);i++)
	{
		k=0;
		cnt=0;
		while(T[i+k]==dreamer[k]&&i+k<strlen(T)&&cek[i]==0)
		{
			cnt++;
			k++;
			if(cnt==strlen(dreamer))
			{
				k--;
				while(k>0)
				{
					cek[i+k]=1;
					k--;
				}
			}
		}
	}
	for(i=0;i<strlen(T);i++)
	{
		k=0;
		cnt=0;
		//printf("%c %c %d\n",T[i+k],dream[k],cek[i]);
		while(T[i+k]==dream[k]&&i+k<strlen(T)&&cek[i]==0)
		{
			cnt++;
			k++;
			if(cnt==strlen(dream))
			{
				k--;
				while(k>=0)
				{
					cek[i+k]=1;
					k--;
				}
			}
		}
	}
	for(i=0;i<strlen(T);i++)
	{
		if(cek[i]==0)
		{
			a=0;
			break;
		}
	}
	if(a==1) printf("YES");
	else printf("NO");
}

Measured memory_bytes: 640
N=input()
A=[int(x) for x in input().split()]
from collections import defaultdict
d=defaultdict(int)
for i in A:
    d[i]+=1
hen=[0,0]
for i in d.items():
    if i[1]>=2:
        hen.append(i[0])
    if i[1]>=4:
        hen.append(i[0])
hen=sorted(hen,reverse=True)
print(hen[0]*hen[1])

Measured memory_bytes: 69144
#include<bits/stdc++.h>
using namespace std;
int n,m,x,a[120],t[120];
int main(){
    cin>>n>>m>>x;
    for(int i=0;i<m;i++) cin>>a[i];
    for(int i=0;i<n;i++) t[a[i]]=1;
    int c1=0,c2=0;
    for(int i=x;i<n;i++) 
	    if(t[i]==1) c1++;
    for(int i=x;i>0;i--)
        if(t[i]==1) c2++;
    cout<<min(c1,c2);
	return 0;
}

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

int main() {
  int x;
  cin>>x;
  function<int(int)> f=[&](int x) {
    int res =0;
    while (x) {
      res += (x%10);
      x/=10;
    }
    return res;
  };
    cout<<(x%f(x)==0 ? "Yes\n" : "No\n");
}


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

typedef long long ll;
typedef pair<int,int> P;
int dx[] = {-1, 1, 0,  0};
int dy[] = { 0, 0, 1, -1};
 
int main(){
  int h, w, d;
  cin >> h >> w >> d;
  vector<P> a(h*w);
  for(int i=0; i<h; i++){
    for(int j=0; j<w; j++){
      int b;
      cin >> b;
      b--;
      a[b] = make_pair(i, j);
    }
  }
  vector<int> s(h*w);
  for(int i=0; i<d; i++){
    s[i] = 0;
  }
  for(int i=d; i<h*w; i++){
    int dx = abs(a[i].first  - a[i-d].first );
    int dy = abs(a[i].second - a[i-d].second);
    s[i] = s[i-d] + dx + dy;
  }
  int q; cin >> q;
  for(int i=0; i<q; i++){
    int l, r;
    cin >> l >> r;
    l--; r--;
    cout << s[r] - s[l] << endl;
  }
  return 0;
}


Measured memory_bytes: 2176
<?php

$hw = explode(' ', trim(fgets(STDIN)));
$h = $hw[0];
$w = $hw[1];

$s = [];
for($i=0; $i<$h; $i++){
  $l = trim(fgets(STDIN));
  for($j=0; $j<$w; $j++){
    $s[$i][$j] = $l[$j];
  }
}

for($i=0; $i<$h; $i++){
  for($j=0; $j<$w; $j++){
    
    $c = 0;    
    if ($s[$i][$j] == '.'){
      $u = $i-1;
      $d = $i+1;
      
      if (isset($s[$i][$j-1]) && $s[$i][$j-1] === '#') $c++;
      if (isset($s[$i][$j+1]) && $s[$i][$j+1] === '#') $c++;
      
      for($k=-1; $k<=1; $k++){
        if (isset($s[$u][$j+$k]) && $s[$u][$j+$k] === '#') $c++;
        if (isset($s[$d][$j+$k]) && $s[$d][$j+$k] === '#') $c++;        
      }
      $s[$i][$j] = $c;
    }
  }
}

for($i=0; $i<$h; $i++){
  if ($i !== 0) echo "\n";
  for($j=0; $j<$w; $j++){
    echo $s[$i][$j];
  }
}

?>

Measured memory_bytes: 4728
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n , k ;
long long h[N]  ;
long long dp[N];


int32_t main(){
	for(int i = 0 ; i < N ; i ++) dp[i] = (long long) 1e18;
	scanf("%d" , &n);
	scanf("%d" , &k);
	//k = 2;
	for(int i = 1 ; i <= n; i ++) scanf("%lld" , & h[i]);
	dp[1] = 0;
	for(int i = 1 ; i <= n ;i ++){
		for(int j = i + 1 ; j <= n && j - i <= k ; j++){
			dp[j] = min(dp[j] , dp[i] + abs(h[i] - h[j]));
		}
	}
	printf("%lld\n" , dp[n]);
}

Measured memory_bytes: 1792
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
#define int long long
#define ull unsigned long long
#define ld long double
#define rep(a) rep1(i,a)
#define rep1(i,a) rep2(i,0,a)
#define rep2(i,b,a) for(int i=(b); i<((int)(a)); i++)
#define rep3(i,b,a) for(int i=(b); i>=((int)(a)); i--)
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define sz(a) ((int)a.size())
#define pow2(x) (1ll<<(x))
#define print0(a) cout << (a) << ' '
#define print1(a) cout << (a) << '\n'
#define print2(a,b) cout << (a) << ' ',print1(b)
#define print3(a,b,c) cout << (a) << ' ',print2(b,c)
#define print4(a,b,c,d) cout << (a) << ' ',print3(b,c,d)
#ifdef i_am_noob
#define debug(a) print2(#a,a)
#else
#define debug(a) 
#endif
#define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
 
using namespace std;
using namespace __gnu_pbds;
 
const int Mod=1000000007,Mod2=998244353;
const int MOD=Mod2;
 
inline int add(int a, int b){return (a+b>=MOD?a+b-MOD:a+b);}
inline int sub(int a, int b){return (a+MOD-b>=MOD?a-b:a+MOD-b);}
int Pow(int a, int b){
	if(!b) return 1;
	int tmp=Pow(a*a%MOD,b>>1);
	return (b&1?tmp*a%MOD:tmp);
}
inline int inverse(int n){return Pow(n,MOD-2);}
 
const int maxn=200005;
//i_am_noob
string str;
bool ok=true;
 
signed main(){
	ios_base::sync_with_stdio(0),cin.tie(0);
	cin >> str;
	if(sz(str)&1) ok=false;
	rep(sz(str)) if((i%2==1&&str[i]!='i')||(i%2==0&&str[i]!='h')) ok=false;
	print1(ok?"Yes":"No");
	return 0;
}

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

using namespace std;
char s[5];
int main(){
    cin >> s;
    sort(s,s+4);
    if(s[0] == s[1] && s[2] == s[3] && s[1] != s[2]) puts("Yes");
    else puts("No");
    return 0;
}

Measured memory_bytes: 256
def p(x):
    res = 0
    y = 2
    while y*y <= x:
        while x % y == 0:
            res += 1
            x //= y
        y += 1
    if x > 1:
        res += 1
    return res
x = int(input())

while 1:
    if p(x)==1:
        print(x)
        quit()
    x+=1



Measured memory_bytes: 2940
#include <stdio.h>

using namespace std;

int main() {
    int n, k, q;
    scanf("%d%d%d", &n, &k, &q);
    int less_points[n];
    for (int k = 0; k < n; k++) {
        less_points[k] = q;
    }
    int answerer;
    for (int a = 0; a < q; a++) {
        scanf("%d", &answerer);
        less_points[answerer - 1] = less_points[answerer - 1] - 1;
    }
    int point;
    for (int b = 0; b < n; b++) {
        point = k - less_points[b];
        if (point > 0) {
            printf("%s", "Yes\n");
        } else {
            printf("%s", "No\n");
        }
    }
    return 0;
}

Measured memory_bytes: 896
#include<cstdio>
#include<map>
#include<iostream>
#include<algorithm>

using namespace std;

#define INF 100000000000
#define lli long long int

lli res[105][105];

int v,e;
int s[100000],t[100000];
int d[1000000];

void warshal(){

  for(int i=0;i<v;i++)
    res[i][i]=0;

  for(int k=0;k<v;k++)
    for(int i=0;i<v;i++)
      for(int j=0;j<v;j++)
        if(res[i][k]!=INF && res[k][j]!=INF)
          res[i][j]=min(res[i][j],res[i][k]+res[k][j]);

  for(int i=0;i<v;i++){
    if(res[i][i]<0){
      printf("NEGATIVE CYCLE\n");
      return;
    }
  }

  for(int i=0;i<v;i++){
    for(int j=0;j<v;j++){
      if(res[i][j]==INF && j==v-1) printf("INF");
      else if(res[i][j]==INF) printf("INF ");
      else if(j==v-1) printf("%d",res[i][j]);
      else printf("%d ",res[i][j]);
    }
    printf("\n");
  }

}
int main(){
  scanf("%d%d",&v,&e);
  for(int i=0;i<e;i++)
    scanf("%d%d%d",&s[i],&t[i],&d[i]);

  for(int i=0;i<v;i++)
    for(int j=0;j<v;j++)
      res[i][j]=INF;

  for(int i=0;i<e;i++)
    res[s[i]][t[i]]=d[i];


  warshal();

  return 0;
}

Measured memory_bytes: 3404
#include <iostream>
#include <vector>
#include<string>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;
int main()
{
	int N;
	int M;
	cin >> N >> M;
	vector<int>A(M);
	int total = 0;
	for(int i = 0;i<M;i++){
		cin >> A[i];
		total += A[i];
	}
	
	if(total>N){
		std::cout << "-1" <<std::endl;
	}else{
		std::cout << N-total <<std::endl;
	}
	
	return 0;
}


Measured memory_bytes: 3648
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <tuple>
#include <queue>
#include <set>
#include <cmath>
#include <array>
#include <list>
#include <set>
const int MOD=1000000007;
using namespace std;
typedef long long ll;
typedef tuple<ll,int,int> tup;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> p;
typedef vector<p> vp;
const int INF=1e9;
int N,X,x[100005];

int gcd(int a,int b)
{
  if(b>=a) swap(a,b);
  if(a%b!=0)
    {
      b=gcd(b,a%b);
      if(a%b==0) return b;
    }
  else
    {
      return b;
    }
}

int main(int argc,char const* argv[])
{
  cin >> N >> X;
  for(int i=0;i<N;i++)
    {
      cin >> x[i];
      x[i]=abs(X-x[i]);
    }
  if(N==1)
    {
      cout << x[0] << endl;
      return 0;
    }
  int ans=gcd(x[0],x[1]);
  for(int i=2;i<N;i++)
    {
      ans=gcd(ans,x[i]);
    }
  cout << ans << endl;
}


Measured memory_bytes: 640
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int[] a= {0,1,3,1,2,1,2,1,1,2,1,2,1};
		System.out.println(a[sc.nextInt()]==a[sc.nextInt()]?"Yes":"No");
		sc.close();
	}
}

Measured memory_bytes: 21844
D, G = [int(n) for n in input().split()]

score_table = []
for i in range(1, D+1):
    p, c = [int(n) for n in input().split()]
    score_table.append([i*100, p, c])

min_count = sum([l[1] for l in score_table])
#print(min_count)

for bit in range(1<<D):
    score = 0
    count = 0

    for i in range(D):
        if bit & (1<<i):
            score += score_table[i][0] * score_table[i][1] + score_table[i][2]
            count += score_table[i][1]
    
    if score >= G:
        min_count = min(min_count, count)
    else:
        additional = []
        for j in range(D):
            if not bit & (1<<j):
                additional.append(score_table[j])
        
        additional_ = sorted(additional, key=lambda x:x[0], reverse=True)
        #print(additional_)
        
        # for i in additional_:
        #     if i[1] > 1:
        #         add_score = i
        #         break

        #add_score = additional_[0]

        for k in range(additional_[0][1]-1):
            score += additional_[0][0]
            count += 1
            if score >= G:
                min_count = min(min_count, count)

print(min_count)

Measured memory_bytes: 9104
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <cmath>
typedef long long ll;
using namespace std;

int main(){
  int N;
  cin >> N;
  vector<int> a(N);
  for(int i = 0; i < N; ++i){
    cin >> a[i];
  }
  int cnt = 0;
  for(int i = 0; i < N; ++i){
    int c = a[i];
    while(c % 2 == 0){
      c /= 2;
      cnt++;
    }
  }
  cout << cnt << endl;
}


Measured memory_bytes: 256
#include<iostream>
#include<stdio.h>//printf("%.*f\n",*********);
#include<vector>//s.erase(n),vector
#include<algorithm>//next_permutation
#include<set>
#include<string>//char('a'=32+'A')
//to_string,string,substr
#include<sstream>
#include<complex>
#include<time.h>
#include<random>
#include<cmath>//min,max
#include<math.h>
#include<queue>
#include<time.h>
#include<iomanip>
#include<locale>
#include<utility>//swap

#define rt "\n"
#define rep(i,n) for(int i=0;i<n;i++)
#define rop(i,n) for(int i=1;i<=n;i++)//start==1
#define drep(i,n) for(int i=n-1;0<=i;i--)
#define drop(i,n) for(int i=n;0<i;i--)//end==1
#define yes(ans) if(ans)cout<<"yes"<<rt;else cout<<"no"<<rt;
#define Yes(ans) if(ans)cout<<"Yes"<<rt;else cout<<"No"<<rt;
#define YES(ans) if(ans)cout<<"YES"<<rt;else cout<<"NO"<<rt;
#define Yay(ans) if(ans)cout<<"Yay!"<<rt;else cout<<":("<<rt;
#define sec(a,b,ans) if(ans)cout<<a<<rt;else cout<<b<<rt;
#define vcin(a,n) rep(i,n)cin>>a[i];//vcin(配列名),(繰り返し回数)
#define sort(s) sort(s.begin(),s.end())//昇順
#define reve(s) reverse(s.begin(),s.end())//反転
#define please return
#define AC 0
#define rapid_pleaseAC_fast cin.tie(0);ios::sync_with_stdio(false)
#define pi 3.1415926535897932384626//pi
#define nine 1000000000//10^9

using namespace std;
typedef vector<int> vint;
typedef long double ldouble;
typedef vector<string> vstr;
typedef vector<char> vchar;
typedef vector<double> vdou;
typedef vector<double> vdouble;
typedef long long int llint;
typedef pair<int, int> pint;
typedef pair<llint, llint> pllint;
typedef vector<llint> vllint;
typedef vector<pint> vpint;
typedef vector<pair<llint, llint>> vpllint;
typedef vector<vector<int>> vvint;
typedef vector<vector<char>> vvchar;
typedef vector<vector<double>> vvdouble;
typedef vector<vector<llint>> vvllint;
typedef vector<vector<string>> vvstr;
typedef vector<vector<bool>> vvbool;
typedef vector<vector<pint>> vvpint;
typedef vector<bool> vbool;

long long GCD(long long a, long long b) {
	if (b == 0) return a;
	else return GCD(b, a % b);
}

long long LCM(long long a, long long b) {
	return a * b / GCD(a, b);
}

unsigned GetDigit(unsigned num) {
	return std::to_string(num).length();
}

int tow(int n) {//2のn乗
	if (n == 0)return 1;
	int x = tow(n / 2);
	x *= x;
	if (n % 2 == 1)x *= 2;
	return x;//@domino
}

int KETA(int n) { //Sum of Each Digit
	int sum = 0;
	while (n > 0) {
		sum += n % 10; n /= 10;
	}
	return sum;
}

unsigned ND(unsigned num) {//Number of Digits
	return std::to_string(num).length();//outmax=10
}

bool KIBN(string s) {
	rep(i, s.size() / 2) {
		if (s[i] != s[s.size() - 1 - i])return false;
	}
	return true;
}

bool inte(double n) {//整数かどうかの判定
	if (ceil(n) == floor(n)) {//切り上げ関数,切り下げ関数
		return true;
	}
	else return false;
}

// (char)toupper(a[n])=文字列のn文字目を大文字で出力

// pow(a,b)=aのb乗

//グローバル関数
int a = 0, n = 0, x = 0, y = 0, ans = 0, cnt = 0, sum = 0;
string s="";

int main(void) {

	int a, b;
	cin >> a >> b;
	sec("unsafe", "safe", b >= a);

	please AC;
}

//参考：---;

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

int main() {
  char S[11];
  cin >> S;

  int ans = strcmp(S,"2019/04/30");

  if (ans > 0) {
    cout << "TBD" << endl;
  } else {
    cout << "Heisei" << endl;
  }

  return 0;
}


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

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

int N;
char S[100005];

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

	cin >> N;

	ll cntI = 0;
	ll cntJ = 0;

	for (int i = 1;i <= N;i++){
		cin >> S[i];
		if(S[i] == 'I') cntI++;
	}

	ll J[100005], O[100005], I[100005];
	J[0] = O[0] = I[0] = 0;

	for (int i = 1;i <= N;i++){
		J[i] = J[i-1];
		O[i] = O[i-1];
		I[i] = I[i-1];
		if(S[i] == 'J') J[i]++;
		else if(S[i] == 'O') O[i] += J[i];
		else if(S[i] == 'I') I[i] += O[i];
	}

	ll da = I[N];
	ll ans = O[N] + I[N];

	J[0] = 1;
	O[0] = I[0] = 0;

	for (int i = 1;i <= N;i++){
		J[i] = J[i-1];
		O[i] = O[i-1];
		I[i] = I[i-1];
		if(S[i] == 'J') J[i]++;
		else if(S[i] == 'O') O[i] += J[i];
		else if(S[i] == 'I') I[i] += O[i];
	}

	ans = max(ans,I[N]);

	ll maxO = -1;

	for (int i = 1;i < N;i++){
		if(S[i] == 'J') cntJ++;
		else if(S[i] == 'I') cntI--;
		maxO = max(maxO, cntJ*cntI);
	}

	ans = max(da+maxO, ans);

	cout << ans << endl;

	return 0;

}



Measured memory_bytes: 5668
#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
#include<algorithm>//next_permutation
#define rep(i,n) for (int i = 0;i < (n);i++)
#define all(v) v.begin(),v.end()
#define dec(n) cout << fixed << setprecision(n);
#define large "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define small "abcdefghijklmnopqrstuvwxyz"
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vl>;

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

ll mod(ll a){
  return (a % 1000000007);
}

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

ll nCr(ll n,ll r){
  if(r == 1) return n;
  else if(r == 0) return 1;
  else return nCr(n-1,r-1)*n/r;
}

int main(){
  ll num[] ={7,17,37,47,67,97,107,127,137,157,167,197,227,257,277,
          307,317,337,347,367,397,457,467,487,547,557,577,587,
          607,617,647,677,727,757,787,797,827,857,877,887,907,
          937,947,967,977,997,1087,1097,1117,1187,1217,1237,
          1277,1297,1307,1327};
  
  ll n; cin >> n;
  
  rep(i,n){
    cout << num[i];
    if(i == n) cout << endl;
    else cout << ' ';
  }
  
}
  
  

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

// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) long long(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;

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

const ll INF = 1e9+10;
const ll MOD = 1e9+7;
const ll LINF = 1e18;



signed main()
{
  cin.tie( 0 ); ios::sync_with_stdio( false );
  
  ll n,m; cin>>n>>m;
  vll a(n,-1);
  
  rep(i,m){
    ll s,c; cin>>s>>c; s--;
    if(a[s]==-1){
      a[s]=c;
    }else if(a[s]!=c){
      cout<<-1<<endl;
      return 0;
    }
  }
  
  // rep(i,n) cout<<a[i];
  // cout<<endl;
  
  if(n==1){
    if(a[0]==-1) a[0]=0;
    cout<<a[0]<<endl;
  }else{
    if(a[0]==0){
      cout<<-1<<endl;
      return 0;
    }
    
    if(a[0]==-1) a[0]=1;
    rep(i,n) if(a[i]==-1) a[i]=0;
    rep(i,n) cout<<a[i];
    cout<<endl;
    
  }
  
}

Measured memory_bytes: 3652
N, M = map(int, input().split())

if N >= 3 and M >= 3:
    print((M-2)*(N-2))
elif N ==1 and M == 1:
    print(1)
elif N == 1 and M >= 3:
    print(M-2)
elif M ==1 and N >= 3:
    print(N-2)
else:
    print(0) 

Measured memory_bytes: 3060
S = input()
len_S = len(S)

results = []
begin, end = None, None
acgt = 'ACGT'
for i, s in enumerate(S):
  if begin is None and s in acgt:
    begin = i
  
  if begin is not None and s not in acgt:
    end = i
  
  if begin is not None and (end is not None or i == len_S - 1):
    if end is None:
      end = len_S
    results.append(end - begin)
    begin, end = None, None

print(max(results) if len(results) > 0 else 0)


Measured memory_bytes: 3064
#include<bits/stdc++.h>
#define REP(i,n) for(int i=0;i<n;i++)
#define RREP(i,n) for(int i=n;i>=0;i--)
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
typedef long long ll;



int main(void){
    string s,t;
    cin >> s >> t;
    int ss=s.size(),ts=t.size();
    int ans=ts;
    for(int i=0;i<(ss-ts+1);i++){
        string a=s.substr(i,ts);
        int cnt=0;
        for(int j=0;j<ts;j++){
            if(a[j]==t[j]){
                cnt++;
            }
            ans=min(ans,ts-cnt);
        }
    }
    cout << ans;

    return 0;
}

Measured memory_bytes: 3628
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < (n); i++)
#define REPR(i, n) for(int i = (n); i >= 0; i--)
#define FOR(i, m, n) for(int i = (m); i < (n); i++)
#define INF 1e9
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << x << endl;

using namespace std;
typedef long long ll;

long nCr(int n, int r) {
  long ans = 1;
  for(int i = n; i > n - r; --i) {
    ans = ans * i;
  }
  for(int i = 1; i < r + 1; ++i) {
    ans = ans / i;
  }
  return ans;
}

signed main() {
  int n, m;
  cin >> n >> m;
  long res = nCr(n, 2) + nCr(m, 2);
  cout << res << endl;
  return 0;
}


Measured memory_bytes: 256
#入力
input_line = input()
input_line = input_line.split(" ")
A = int(input_line[0])
B = int(input_line[1])
C = int(input_line[2])
K = int(input_line[3])

#最大値
M = 0

if A > K:
    M = K
elif (A+B) > K:
    M = K
else:
    x = K - (A+B)
    M = A - x

#出力
print(M)

Measured memory_bytes: 9188
#include<stdio.h>
int main()
{
	int n;
	scanf("%d", &n);
	int i;
	int h[100005];
	for (i = 0; i < n; i++)
		scanf("%d", &h[i]);
	h[0]--;
	for (i = 1; i < n; i++)
	{
		if (h[i] < h[i - 1])
		{
			printf("No\n");
			return 0;
		}
		if (h[i] > h[i - 1])
			h[i]--;
	}
	printf("Yes\n");
	return 0;
}

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

int main() {

    int N;
    cin >> N;
    int a[N];
    rep(i,N) cin >> a[i];

    int ans = 0;
    for(int i = 0; i < N; i++){
        if(i % 2 == 0){
            if(a[i] % 2  == 1){
                ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;

}

Measured memory_bytes: 3620
#include <iostream>
#include <algorithm>
#include <string>
#include <set>

using namespace std;

int n, ans;
string s, t;

void calc(int p, int cnt){
  if(ans <= cnt) return;
  if(p == n){
    ans = min(ans, cnt);
    return;
  }
  if(s[p] == t[p]) calc(p+1, cnt);
  int d = t[p] - s[p];
  for(int i=p;i<n;i++){
    for(int j=p;j<=i;j++) s[j] = ((s[j] - '0') + d + 10) % 10 + '0';
    calc(p+1, cnt+1);
    for(int j=p;j<=i;j++) s[j] = ((s[j] - '0') - d + 10) % 10 + '0';
  }
}

main(){
  while(cin >> n && n){
    cin >> s >> t;
    ans = n;
    calc(0, 0);
    cout << ans << endl;
  }
}

Measured memory_bytes: 1200
import java.util.Arrays;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] v = new int[n];
		for (int i = 0; i < v.length; i++) {
			v[i] = sc.nextInt();
		}
		
		Arrays.sort(v);
		double ans = v[0];
		for (int i = 1; i < v.length; i++) {
			ans = (v[i] + ans) / 2;
		}
		
		System.out.println(ans);
	}
}

Measured memory_bytes: 23252
import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int A = sc.nextInt();
    String op = sc.next();
    int B = sc.nextInt();
    if(op.equals("+")){    
      System.out.println(A+B);
    }else{
      System.out.println(A-B);
    }
  }
}

Measured memory_bytes: 35624
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    s = readline().strip()

    if s[2] == s[3] and s[4] == s[5]:
        print("Yes")
    else:
        print("No")


if __name__ == "__main__":
    main()


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

using namespace std;

static std::array<int, 100000> as{};
static std::array<int, 100000> bs{};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  std::cin >> n;
  int i = 0;
  for (; i < n; i++) {
    std::cin >> as[i] >> bs[i];
  }
  int64_t ret = 0;
  i--;
  for (; i >= 0; i--) {
    auto const a = as[i];
    auto const b = bs[i];
    ret += (b - (a + ret) % b) % b;
  }
  std::cout << ret << std::endl;
  return 0;
}

Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
int main(){
  int N,M;
  cin>>N>>M;
  int a[N],b[N];
  for(int i=0;i<N;i++){
    cin>>a[i]>>b[i];
  }
  int c[M],d[M];
  for(int i=0;i<M;i++){
    cin>>c[i]>>d[i];
  }
  int ans[N],ma[N];
  for(int i=0;i<N;i++){
    ma[i]=(1<<29);
    for(int j=M-1;j>=0;j--){
      if(ma[i]>=abs(a[i]-c[j])+abs(b[i]-d[j])){
        ma[i]=abs(a[i]-c[j])+abs(b[i]-d[j]);
        ans[i]=j+1;
      }
    }
  }
  for(int i=0;i<N;i++){
    cout<<ans[i]<<endl;
  }
}

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

start = 2 - 1
end = len(p) - 1

count = 0
for i in range(start, end):
    
    a = p[i - 1]
    b = p[i]
    c = p[i + 1]
    
    # print(f'{p[i - 1]} {p[i]} {p[i + 1]}')
    
    new_list = sorted([a, b, c])
    
    if b == new_list[1]:
        # print(f'{a} {b} {c}')
        count += 1
        
print(count)

Measured memory_bytes: 3064
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
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; }
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧  
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i     
　　　　／　　　＼　 　  |　|     
　　　 /　　 /￣￣￣￣/　　|  
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿  
　 　　　＼/＿＿＿＿/　（u　⊃  
---------------------------------------------------------------------------------------------------*/









int A, B, C;
//---------------------------------------------------------------------------------------------------
void _main() {
	cin >> A >> B >> C;
	if (A == B and B == C) cout << "Yes" << endl;
	else cout << "No" << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
int n,cnt,ans,tot,q[100010],inf=1e9,l1,r1,mi,v[100010];
int x1[100010],r[200010],vt[200010],in[100010],mx,root;
void adde(int a,int b)
{
	r[++cnt]=x1[a];x1[a]=cnt;vt[cnt]=b;in[b]++;
	r[++cnt]=x1[b];x1[b]=cnt;vt[cnt]=a;in[a]++;
} 
void dfs(int i,int fa)
{
	int s,a,b,j,k;
	int l2,r2,m,st,ed;
	a=x1[i];v[i]=0;
	while(a)
	{
		b=vt[a];
		if(b!=fa)dfs(b,i);
		a=r[a];
	}
	a=x1[i];cnt=0;
	while(a)
	{
		b=vt[a];
		if(b!=fa)q[++cnt]=v[b]+1;
		a=r[a];
	}
	if(!(cnt&1))q[++cnt]=0;
	sort(q+1,q+1+cnt);
	l2=1;r2=cnt+1;
	while(l2<r2)
	{
		m=(l2+r2)/2;j=1;
		st=1;ed=cnt;
		while(st<ed)
		{
			if(st==m)st++;
			if(ed==m)ed--;
			if(q[st]+q[ed]>mi){j=0;break;}
			st++;ed--;
		}
		if(j)r2=m;
		else l2=m+1;
	} 
	if(l2==cnt+1)v[i]=inf;
	else v[i]=q[l2];
}
bool jd(int mi)
{
	dfs(root,0);
	return (v[root]<=mi);
}	
int main()
{
	int i,s,a,b,j,k;
	scanf("%d",&n);
	for(i=1;i<n;i++)scanf("%d%d",&a,&b),adde(a,b);
	ans=1;
	for(i=1;i<=n;i++)
	{
		ans+=(in[i]-1)/2;
		if(in[i]==1)root=i;
	}
	l1=1;r1=n;
	while(l1<r1)
	{
		mi=(l1+r1)/2;
		if(jd(mi))r1=mi;
		else l1=mi+1;
	}
	printf("%d %d",ans,l1);
	return 0;
}

Measured memory_bytes: 7680
import sys
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs= sys.stdin.buffer.readlines
#rstrip().decode('utf-8')

#import numpy as np
#import operator
#import bisect
#from heapq import heapify,heappop,heappush
#from math import gcd
#from fractions import gcd
#from collections import deque
#from collections import defaultdict
#from collections import Counter
#from itertools import accumulate
#from itertools import groupby
#from itertools import permutations
#from itertools import combinations
#scipy.sparse import csr_matrix
#scipy.sparse.csgraph import floyd_warshall
#from scipy.sparse.csgraph import csgraph_from_dense
#from scipy.sparse.csgraph import dijkstra
#sys.setrecursionlimit(10**7)

#map(int,input().split())

def main():
    N,K=map(int,input().split())
    ans=0
    for i in range(K+1,N+1):
        ans+=(N//i)*(i-K)+max((N%i-max(K,1)+1),0)
    print(ans)



if __name__ == '__main__':
    main()

Measured memory_bytes: 3060
from collections import deque

N = int(input())

A = list(map(int, input().split()))
A.sort(reverse=True)
edge = deque([(A[0], A[1]), (A[0], A[1])])

count = A[0]

for a in A[2:]:
  (l, r) = edge.popleft()
  count += r
  edge.append((l, a))
  edge.append((r, a))
print(count)

Measured memory_bytes: 33540
#include"bits/stdc++.h"
using namespace std;
#define int int64_t
int ar[(int)2e5+100];
int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n,k;cin>>n>>k;
    for(int i=1;i<=n;i++)
        cin>>ar[i];
    int st=1;
    bool flag=true;
    set<int> vis;
    vector<int> pos;
    int ender=-1;
    while(flag)
    {
        if(vis.find(st)==vis.end())
        {
            pos.push_back(st);
            vis.insert(st);
            st=ar[st];
        }
        else
        {
            ender=st;
            break;
        }
    }
    if(k<pos.size())
    {
        cout<<pos[k]<<"\n";
    }
    else
    {
        int ok=pos[0];
        int sp=0;
        //cout<<ender<<"\n";
        while(ok!=ender)
        {
            sp++;
            ok=pos[sp];
            k--;
        }
       // cout<<sp<<"\n";
        int len=pos.size()-sp;
        //cout<<len<<"\t"<<k<<"\n";
        k%=(len);
        ok=pos[sp+k];
        cout<<ok<<"\n";
    }
    return 0;
}


Measured memory_bytes: 15448
#include<cstdio>
#include<algorithm>
using namespace std;
#define M 100005
int good[M];
int n, a[M], b[M];
int gc, bc;
long long asum, bsum,bad;
int main() {
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		asum += a[i];
	}
	for (i = 1; i <= n; i++) {
		scanf("%d", &b[i]);
		bsum += b[i];
	}
	if (asum < bsum) {
		printf("-1");
		return 0;
	}
	for (i = 1; i <= n; i++) {
		if (a[i] > b[i]) good[++gc] = a[i] - b[i];
		else if (a[i] < b[i]) {
			bad += b[i] - a[i];
			bc++;
		}
	}
	int use=0;
	sort(good+1,good+1+gc);
	for (i = gc; i >= 1; i--) {
		if (bad <= 0) break;
		bad -= good[i];
		use++;
	}
	printf("%d", bc + use);
	return 0;
}

Measured memory_bytes: 1152
#include <bits/stdc++.h>
#include <math.h>
#include <float.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rrep(i, n) for(int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;
 
const ll INF = 1LL<<62;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// 多次元 vector 生成
template<class T>
vector<T> make_vec(size_t a){
    return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
const int MOD = 1000000007;

/*
struct point{
    ll start, index, point;
};

bool comp(point a, point b){
    if (a.start == b.start){
        return a.index < b.index;
    }
    else{
        return a.start < b.start;
    }
}
*/
const int max_v = 3001;

ll dp[max_v][max_v][4];

int main(){
    int R,C,K; cin >> R >> C >> K;
    //vector<ll> r(K), c(K), v(K);
    //rep(i,K) cin >> r[i] >> c[i] >> v[i];

    /*
    vector<point> rcv(K);
    rep(i,K){
        ll tr,tc,tv;
        cin >> tr >> tc >> tv;
        point p = {tr, tc, tv};
        rcv[i] = p;
    }
    */
    vector<vector<ll>> rcv(R+5, vector<ll>(C+5,0));
    rep(i,K){
        ll tr, tc, tv; cin >> tr >> tc >> tv;
        tr--;
        tc--;
        rcv[tr][tc] = tv;
        //cout << tr << " " << tc << " " << tv;
    }

    //cout << rcv.size() << endl;

    //sort(rcv.begin(), rcv.end(), comp);

    rep(i,max_v){
        rep(j,max_v){
            rep(k,4){
                dp[i][j][k] = -INF;
            }
        }
    }
    dp[0][0][0] = 0;
    dp[0][0][1] = rcv[0][0];
    
    rep(i,R){
        rep(j,C){
            rep(k,4){
                //cout << i << " " << j << " " << k << endl;
                if (i+1 < R && dp[i][j][k] != INF) {
                    dp[i+1][j][0] = max(dp[i+1][j][0], dp[i][j][k]);
                    dp[i+1][j][1] = max(dp[i+1][j][1], dp[i][j][k]+rcv[i+1][j]);
                    //cout << i << " " << j << endl;
                    //cout << dp[i+1][j][0] <<  " " << dp[i+1][j][1] << endl;
                }
                if (j+1 < C && dp[i][j][k] != INF){
                    dp[i][j+1][k] = max(dp[i][j+1][k], dp[i][j][k]);
                    if (k+1 >= 4) continue;
                    //cout << i << " " << j << " " << k << endl;
                    //cout << rcv[i][k] << endl;
                    dp[i][j+1][k+1] = max(dp[i][j+1][k+1], dp[i][j][k]+rcv[i][j+1]);
                    /*
                    if (dp[i][j+1][k+1] == 12){
                        cout << i << " " << j+1 << " " << k+1 << endl;
                    }
                    */
                }

            }
        }
    }
    ll ans = -1;
    rep(i,4){
        //cout << dp[R-1][C-1][i] << endl;
        ans = max(ans, dp[R-1][C-1][i]);
    }

    cout << ans << endl;

}

Measured memory_bytes: 355192
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
#define vv(a,b,c,d) vector<vector<a> >(b,vector<a>(c,d))
#define vvv(a,b,c,d,e) vector<vector<vector<a> > >(b,vv(a,c,d,e))
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;

const int MA = 300 * 300 + 1;
int H, W, D, A[300][300], X[MA], Y[MA];
vll S[MA];

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

    cin >> H >> W >> D;
    rep(y, H)rep(x, W) {
        cin >> A[y][x];
        Y[A[y][x]] = y;
        X[A[y][x]] = x;
    }

    rep(r, D) {
        int z = r;
        S[r].push_back(0);
        if (r == 0) {
            S[r].push_back(0);
            z = D;
        }
        for (int i = z + D; i <= H*W; i += D) {
            ll w = abs(X[i] - X[i - D]) + abs(Y[i] - Y[i - D]) + S[r].back();
            S[r].push_back(w);
        }
    }

    int Q;
    cin >> Q;
    rep(i, Q) {
        int L, R;
        cin >> L >> R;
        int l = L / D, r = R / D, m = R%D;
        ll ans = S[m][r] - S[m][l];
        cout << ans << endl;
    }
}

Measured memory_bytes: 6400
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
long long a,b;

long long calc(long long i){
    long double res = floor(a*i/b) - a*floor(i/b);
    return res;
}


int main() {
    long long n;
    cin >> a >> b >> n;
    cout << calc(min(b-1,n)) << endl;
}


Measured memory_bytes: 3592
#include<iostream>
using namespace std;
int main(){
  int n,m;
  while(cin>>n>>m,n||m){
    int p[501][501]={0,0};
    int count = 0;
    int h[n+1];
    for(int i=0;i<=n;i++) h[i] = 0;
    h[1] = 1;
    int a,b;
    for(int i=0;i<m;i++){
      cin >> a >> b;
      p[a][b] = 1;
    }
    for(int i=1;i<=n;i++){
      if(p[1][i]!=0){
	h[i] = 1;
	count++;
      }
      if(p[i][1]!=0){
	h[i] = 1;
	count++;
      }
    }
    for(int i=1;i<=n;i++){
      for(int j=1;j<=n;j++){
	if(h[i]==1){
	  if(p[i][j]!=0&&h[j]==0){
	    count++;
	    h[j] = 2;
	  }
	  if(p[j][i]!=0&&h[j]==0){
	    count++;
	    h[j] = 2;
	  }
	}
      }
    }
    cout << count << endl;
  }
  return 0;
}

Measured memory_bytes: 2148
#!/usr/bin/env python
import sys
from collections import Counter
from itertools import permutations, combinations
from fractions import gcd
from math import ceil, floor
import bisect
sys.setrecursionlimit(10 ** 6)
inf = float("inf")

def input():
    return sys.stdin.readline()[:-1]

def main():
    n = int(input())
    A = list(map(int, input().split()))
    ans = "APPROVED"
    for a in A:
        if a % 2 == 0:
            if a % 3 != 0 and a % 5 != 0:
                ans = "DENIED"
    print(ans) 

if __name__ == '__main__':
    main()


Measured memory_bytes: 5056