akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll,ll> pii;

long long MOD = 1000000000 + 7;

int main(){
  cout << setprecision(10);
  int N;
  ll K;
  cin >> N >> K;
  vector<pii> v(N);

  for(int i = 0; i < N; i++){
    ll tmp1,tmp2;
    cin >> tmp1 >> tmp2;
    v.at(i) = make_pair(tmp1,tmp2);
  }
  sort(v.begin(), v.end());
  cerr << v[0].first << " " << v.at(0).second << endl;

  ll num = 0;
  ll ans = 0;
  for(int i = 0; i < N; i++){
    if(num + v[i].second < K){
      num += v[i].second;
    } else { 
      ans = v[i].first;
      break;
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 1792
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz size()
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define mod 1000000007
#define forn(i, n) for (int i = 0; i < (int) (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    int arraux[n];
    forn(i,n){
        cin >> arraux[i];
    }
    int bestsum = mod;
    for(int i = -100; i <= 100;++i){
        int value = 0;
        forn(j,n){
            value += pow(arraux[j]-i,2);
        }
        bestsum = min(bestsum,value);
    }
    
    cout << bestsum;
    return 0;
}

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

using namespace std;
#define all(a)a.begin(),a.end()
using ll=long long;
const int INF = 1<<30;
const ll INFll =1LL<<62;
const int mod= int(1e9)+7;
using P = pair<ll,ll>;
using ld=long double;

int main(){
   ll a,b;cin >>a>>b;
   bool flg=false;
   if(a%3==0||(a+b)%3==0||b%3==0)flg=true;
   if(flg)puts("Possible");
   else puts("Impossible");
   return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#define LINE_BUF_SIZE  1024

int main(void) {
  
  int i;
  int N,M;
  int A[1000];
  int count = 0;
  int sum = 0;

  scanf("%d %d",&N,&M);
  for(i = 0; i < N; i++){
      A[i] = 0;
  }
  for(i = 0; i < N; i++){
      scanf("%d",&A[i]);
      sum += A[i];
  }
  
  for(i = 0; i < N; i++){
      if(A[i] >= (float)((float)sum/(float)(4*M))){
          count++;          
      }
  }
  if(count < M){
      printf("No");
  }else{
      printf("Yes");
  }
  return 0;
}

Measured memory_bytes: 128
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod=1e9+7;

ll add(ll a,ll b){
  return (a+b+mod+mod)%mod;
}

ll mul(ll a,ll b){
  return (a*b)%mod;
}

ll dp[201][201];

ll solve(string str){
  if(str=="")return 1;
  dp[0][0]=1;
  int n=str.size();

  ll uc=0,dc=0;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      ll a=dp[i][j];
      if(a==0)continue;
      //cout<<i<<' '<<j<<' '<<a<<endl;
      if(str[i]=='U'){
        dp[i+1][j+1]=add(dp[i+1][j+1],a);
        dp[i+1][j]=add(dp[i+1][j], mul(a,j));
      }
      if(str[i]=='D'){
        ll k= i-dc-(uc-j);
        dp[i+1][j]=add(dp[i+1][j], mul(a,k));
        if(j>0)dp[i+1][j-1]=add(dp[i+1][j-1],mul(j,mul(a,k)));
      }

    }
    if(str[i]=='U')uc++;
    else dc++;
  }
  return dp[n][0];
}

int main(){
  int n;
  cin>>n;
  string str="";
  for(int i=0;i<n;i++){
    char ch;
    cin>>ch;
    if(ch=='-')continue;
    str+=ch;
  }
  cout<<solve(str)<<endl;
  return 0;
}

Measured memory_bytes: 1516
# include <bits/stdc++.h>
using namespace std;
const long double pi = 3.14159265359;
const int N = 2e5 + 10;
int f[N];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n;
    cin >> n;
    for(int i = 1; i < n; i++){
        int x;
        cin >> x;
        f[x] ++;
    }
    for(int i = 1; i <= n; i++){
        cout << f[i] << ' ';
    }
}

Measured memory_bytes: 4400
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0;i<n;i++)
using namespace std;
int main(){
    long n,u,v;
    cin >> n >> u >> v;
    u--;v--;
    vector<long> edge[n];
    long a,b;
    rep(i,n-1){
        cin >> a >> b;
        a--;b--;
        edge[a].push_back(b);
        edge[b].push_back(a);
    }
    long par[n];
    par[v]=-1;
    queue<long> que;
    que.push(v);
    bool visited[n];
    fill_n(visited,n,true);
    long rank[n];
    rank[v]=0;
    vector<int> happa;
    bool fl;
    //cout << u << " " << v << endl;
    while(!que.empty()){
        long now = que.front();
        que.pop();
        visited[now]=false;
        fl = true;
        rep(i,edge[now].size()){
            int to = edge[now][i];
            if(visited[to]){
                que.push(to);
                rank[to]=rank[now]+1;
                par[to]=now;
                fl = false;
            }
        }
        if(fl) happa.push_back(now);
    }

    //rep(i,n) cout << rank[i] << " ";
    //cout << endl;
    //rep(i,happa.size()) cout << happa[i] << " ";
    //cout << endl;

    queue<long> gyaku;
    long maxdeep[n];
    fill_n(maxdeep,n,0);
    fill_n(visited,n,true);
    rep(i,happa.size()){
        gyaku.push(happa[i]);
        maxdeep[happa[i]]=rank[happa[i]];
    }
    long maxr = 0;
    rep(i,n) maxr = max(maxr,rank[i]);
    vector<long> rankv[maxr+1];
    rep(i,n) rankv[rank[i]].push_back(i);
    for(int i = maxr;i>0;i--){
        rep(j,rankv[i].size()){
            maxdeep[par[rankv[i][j]]]=max(maxdeep[par[rankv[i][j]]],maxdeep[rankv[i][j]]);
        }
    }
    //rep(i,n) cout << par[i] << " ";
    //cout << endl;
    //rep(i,n) cout << maxdeep[i] << " ";
    //cout << endl;
    //rep(i,n) cout << rank[i] << " ";
    //cout << endl;


    long nowrank = rank[u];
    long cango = nowrank - ((nowrank-1)/2);
    long ans = -1;

    //cout << cango << endl;
    long nownow = u;
    while(true){
        if(cango==rank[nownow]){
            ans = maxdeep[nownow]-1;
            break;
        }
        nownow = par[nownow];
    }
    cout << ans << endl;
}

Measured memory_bytes: 12928
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 9 ;
int a[N] , b[N] , c[N] , n;
int mem[N][5];
int dp[N][5];
int solve(int idx , int pre){
    if(idx == n)
        return 0;
    int &ret = mem[idx][pre];
    if(~ret)
        return ret;
    if(pre == 0){
        ret = max(ret , a[idx] + solve(idx + 1 , 1));
        ret = max(ret , a[idx] + solve(idx+ 1 , 2));
    }
    if(pre == 1){
        ret = max(ret , b[idx] + solve(idx + 1 , 0));
        ret = max(ret , b[idx] + solve(idx + 1 , 2));
    }
    if(pre == 2){
        ret = max(ret , c[idx] + solve(idx + 1 , 0));
        ret = max(ret , c[idx] + solve(idx + 1 , 1));
    }
    return ret;
}

int main()
{
  // memset(mem , -1 , sizeof mem);
   cin >> n ;
   for(int i = 0 ; i < n ; ++i){
         cin >> a[i] >> b[i] >> c[i];
   }
//   int ans = max(solve(0 , 0) , max(solve(0 , 1) , solve(0 , 2)));
//   cout << ans ;
     dp[0][0] = a[0];
     dp[0][1] = b[0];
     dp[0][2] = c[0];
     for(int i = 1 ; i < n ; ++i){
        dp[i][0] = a[i] + max(dp[i-1][1] ,dp[i-1][2]);
        dp[i][1] = b[i] + max(dp[i-1][0] ,dp[i-1][2]);
        dp[i][2] = c[i] + max(dp[i-1][0] ,dp[i-1][1]);
     }
     cout << max(dp[n-1][0] , max(dp[n-1][1] , dp[n-1][2]));
     return 0;
}


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

int main() {
	int X;
	cin >> X;
	if (X == 2) {
		cout << 2 << endl;
		return 0;
	}
	int ans = 3;
	while (1) {
		int flag = 0;
		for (int i = 2; i * i <= ans; i++) {
			if (ans % i == 0) {
				flag = 1;
				break;
			}
		}
		if (flag == 0 && ans >= X) {
			break;
		}
		ans += 2;
	}

	cout << ans << endl;
}

Measured memory_bytes: 256
#include <string>
#include <vector>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
int n, m; vector<int> a;
int main() {
	cin >> n >> m; a.resize(n);
	for (int i = 0; i < n; i++) cin >> a[i];
	int res = 0, r = 1999999999;
	for (int i = 0; i < n; i++) {
		res = max(res, a[i] - r);
		r = min(r, a[i]);
	}
	vector<pair<int, int> > v;
	for (int i = 0; i < n; i++) v.push_back(make_pair(a[i], i));
	sort(v.begin(), v.end());
	vector<int> w;
	for (int i = 0; i < n; i++) w.push_back(v[i].first);
	int ret = 0;
	for (int i = 0; i < n; i++) {
		int ptr = lower_bound(w.begin(), w.end(), w[i] + res) - w.begin();
		if (ptr < n && w[ptr] == w[i] + res && v[i].second < v[ptr].second) ret++;
	}
	cout << ret << endl;
	return 0;
}

Measured memory_bytes: 2420
#include <bits/stdc++.h>
#define rep(i, N) for(int (i) = 0; (i) < (N); (i)++)
#define debug(var) std::cout << #var << " : " << var;
using ll = long long;
using ull = unsigned long long;

template<class T>
T getGcd(T a, T b){
	if(b == 0)return a;
	return getGcd(b, a % b);
}

template<class T>
T getLcm(T a, T b){
	T gcd = getGcd(a, b);
	a /= gcd;
	b /= gcd;
	return gcd * a * b;
}

int main(){


	int N; std::cin >> N;
	ull ans; std::cin >> ans;
	for(int i = 0; i < N - 1; i++){
		ull tmp; std::cin >> tmp;
		ans = getLcm(ans, tmp);
	}

	std::cout << ans;


	return 0;
}

Measured memory_bytes: 256
#define debug_interval ','
#define dump_interval ' '
#define debug_toggle 1
//{
#include<bits/stdc++.h>
using namespace std;
#define hi cerr<<"hi"<<endl;
#define int long long
#define INT_MAX LLONG_MAX
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define fi first
#define se second
#define mp make_pair
#define rev reverse
#define dans dump(ans)
#define MOD 1000000007
#define amp(v,n) (v).count(n)?v[n]++:v[n]=1
#define sysp system("pause")

//{
inline int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x){ostringstream sout;sout<<x;return sout.str();}
template<class...A> inline void dump(){cout<<endl;}
template<class...A> inline void dump_rest() {cout<<endl;}
template<class T, class...A> inline void dump_rest(const T& first, const A&... rest){cout<<dump_interval<<first;dump_rest(rest...);}
template<class T,class...A> inline void dump(const T&first,const A&...rest){cout<<first;dump_rest(rest...);}
template<class...A> inline void debug(){cerr<<endl;}
template<class...A> inline void debug_rest() {cerr<<endl;}
template<class T, class...A> inline void debug_rest(const T& first, const A&... rest){cerr<<debug_interval<<first;debug_rest(rest...);}
template<class T,class...A> inline void debug(const T&first,const A&...rest){if(debug_toggle)cerr<<first,debug_rest(rest...);}
//}

typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}
template<int m>class mint{private:int i;public:mint() : i(0){}mint(int i): i((i%m+m)%m){}mint operator+(const mint& o){return o.i+i;}mint operator*(const mint& o){return o.i*i;}mint operator-(){return -i;}operator int() {return i;}};

int n;
map<int,int> p,q;
vpint tp,tq;

main(){
	cin>>n;
	int a[n];
	rep(i,n)cin>>a[i];
	rep(i,n){
		if(i%2==0)amp(p,a[i]);
		else amp(q,a[i]);
	}
	for(auto x:p){
		tp.pb(mp(x.se,x.fi));
	}
	for(auto x:q){
		tq.pb(mp(x.se,x.fi));
	}
	sort(rall(tp));sort(rall(tq));
	if(tp.size()==1&&tq.size()==1&&tp[0].se==tq[0].se){
		cout<<n/2<<endl;
	}else if(tp[0].se==tq[0].se){
		dump(n-max(tp[0].fi+tq[1].fi,tp[1].fi+tq[0].fi));
	}else{
		//cerr<<tp[0].se<<tq[0].se<<endl;
		dump(n-tp[0].fi-tq[0].fi);
	}
}



Measured memory_bytes: 9592
#include<bits/stdc++.h>
using namespace std;
#define ll long long
bool comp1(pair<int,int> &a,pair<int,int> &b)
{
    return a.second>b.second;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int h,n;
    cin>>h>>n;
    int a;
    int s=0;
    for(int i=0;i<n;i++){
        cin>>a;
        s+=a;
    }
    if(s>=h) cout<<"Yes";
    else cout<<"No";
}

Measured memory_bytes: 3696
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>

using namespace std;

int main(void){
	int a;
	cin >> a;
	int b;
	cin >> b;
	int ans = 0;

	ans = a*b;
	if((a>=1 && a<=9) && (b>=1 && b<=9) ){
		cout << ans << endl;
	}
	else{
		cout << "-1" << endl;
	}

	return 0;

}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define FOR(i,a,b) for (register int i=(a);i<=(b);i++)
#define For(i,a,b) for (register int i=(a);i>=(b);i--)
#define mem(i,j) memset(i,j,sizeof(i))
#define GO(u) for (register int j=f[u];j!=-1;j=nxt[j])
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define MP make_pair
using namespace std;
typedef long long ll;
const int N=4e5+5;
const int M=4e6+5;
const int inf=1e8;
int n,p[N],q[N],pt=0,ans;
int vis[N],x[N],y[N];
int mincut=0,tot=1,f[N],nxt[M],dep[N],S,T,froms[N],toT[N];
queue <int> Q;
map<pii,int> mpp;
struct E
{
	int u,v,flow;
}e[M];
inline int read()
{
	int x=0,f=1;
	char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') {x=(x<<1)+(x<<3)+c-'0';c=getchar();}
	return f*x;
}
inline void write(int x)
{
	if (x<0) putchar('-'),x=-x;
	if (x>9) write(x/10);
	putchar(x%10+'0');
	return;
}
inline void add(int u,int v,int flow)
{
	tot++;
	nxt[tot]=f[u];
	f[u]=tot;
	e[tot]=(E){u,v,flow};
	return;
}
inline void make_edge(int u,int v,int flow)
{
	add(u,v,flow);
	add(v,u,0);
	return;
}
inline void init()
{
	FOR(i,1,n) vis[i]=0;
	FOR(i,1,n) if (!vis[i])
	{
		int now=i;
		pt++;
		while (!vis[now]) vis[now]=1,x[now]=pt,now=p[now];
	}
	FOR(i,1,n) vis[i]=0;
	FOR(i,1,n) if (!vis[i])
	{
		int now=i;
		pt++;
		while (!vis[now]) vis[now]=1,y[now]=pt,now=q[now];
	}
	S=++pt,T=++pt,ans=n;
	FOR(i,1,n)
	{
		if (p[i]==q[i]&&p[i]==i) ans--;
		else if (p[i]==q[i]&&p[i]!=i) mpp[MP(x[i],y[i])]++,mpp[MP(y[i],x[i])]++;
		else if (p[i]==i&&p[i]!=q[i]) froms[y[i]]++;
		else if (q[i]==i&&p[i]!=q[i]) toT[x[i]]++;
		else if (q[i]!=p[i]&&p[i]!=i&&q[i]!=i) mpp[MP(x[i],y[i])]++;
	}
	FOR(i,1,n)
	{
		if (p[i]==q[i]&&p[i]==i);
		else if (p[i]==q[i]&&p[i]!=i)
		{
			if (mpp[MP(x[i],y[i])]) make_edge(x[i],y[i],mpp[MP(x[i],y[i])]),mpp[MP(x[i],y[i])]=0;
			if (mpp[MP(y[i],x[i])]) make_edge(y[i],x[i],mpp[MP(y[i],x[i])]),mpp[MP(y[i],x[i])]=0;
		}
		else if (p[i]==i&&p[i]!=q[i]);
		else if (q[i]==i&&p[i]!=q[i]);
		else if (q[i]!=p[i]&&p[i]!=i&&q[i]!=i)
		{
			if (mpp[MP(x[i],y[i])]) make_edge(x[i],y[i],mpp[MP(x[i],y[i])]),mpp[MP(x[i],y[i])]=0;
		}
	}
	FOR(i,1,pt)
	{
		if (froms[i]) make_edge(S,i,froms[i]);
		if (toT[i]) make_edge(i,T,toT[i]);
	}
	return;
}
inline int bfs()
{
	while (Q.size()) Q.pop();
	FOR(i,1,pt) dep[i]=0;
	dep[S]=1;
	Q.push(S);
	while (Q.size())
	{
		int tmp=Q.front();
		Q.pop();
		GO(tmp)
		{
			int v=e[j].v;
			if (dep[v]) continue;
			if (!e[j].flow) continue;
			dep[v]=dep[tmp]+1;
			if (dep[T]) return 1;
			Q.push(v);
		}
	}
	return dep[T];
}
inline int Dinic(int u,int fl)
{
	int rest=fl,k;
	if (u==T) return fl;
	GO(u)
	{
		int v=e[j].v;
		if (dep[v]!=dep[u]+1) continue;
		if (!e[j].flow) continue;
		k=Dinic(v,min(rest,e[j].flow));
		if (!k)
		{
			dep[v]=0;
			continue;
		}
		e[j].flow-=k;
		e[j^1].flow+=k;
		rest-=k;
	}
	return fl-rest;
}
int main()
{
//	freopen("data.in","r",stdin);
	mem(f,-1);
	n=read();
	FOR(i,1,n) p[i]=read()+1;
	FOR(i,1,n) q[i]=read()+1;
	init();
	while (bfs()) mincut+=Dinic(S,inf);
	ans-=mincut;
	write(ans),putchar('\n');
	return 0;
}


Measured memory_bytes: 31232
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <tuple>
using namespace std;
#define rep(i,n) for (int i=0; i<(int)(n); i++)
#define ct(string) cout << string << endl

int main() {
    int n;
    cin >> n;
    vector<tuple<string, int, int> > vec(n);
    rep(i,n) {
        string a;
        int b;
        cin >> a >> b;
        vec.at(i) = make_tuple(a, 100 - b, i+1);
    }
    sort(vec.begin(), vec.end());
    rep(i,n) ct(get<2>(vec.at(i)));
}

Measured memory_bytes: 256
import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt(); int b = sc.nextInt();
    if (a <= 8 && b <= 8) {
      System.out.println("Yay!");
    } else {
      System.out.println(":(");
    }
  }
}

Measured memory_bytes: 23124
fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace().map(|e| e.parse().ok().unwrap()).collect()
}

fn vec_to_num(digit: i32, vec: &[i32]) -> i32 {
    if(vec.len() == 0) {
        0
    } else {
        digit * vec.first().unwrap() +  vec_to_num(digit * 10, &vec[1..])
    }
}

fn main() {
    let mut nk = read_vec::<i32>();
    nk.sort();
    let ans = nk.first().unwrap() + vec_to_num(1, &nk[1..]);;
    println!("{}", ans);
}

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

int main(){

  int W, a, b, ans;
  cin >> W >> a >> b;

  if (a>b){
    swap(a,b);
  }
  ans = b - (a+W);

  if (ans<0){
    cout << 0 << endl;
  }
  else {
    cout << ans << endl;
  }
}


Measured memory_bytes: 256
//08
#include<iostream>
#include<utility>
#include<algorithm>

using namespace std;

int main(){
  for(int n;cin>>n,n;){
    static pair<int,int> a[1000000];
    for(int i=0;i<n;i++){
      int c;
      cin>>c;
      int t=0;
      for(int j=0;j<4;j++){
	int m,s;
	cin>>m>>s;
	t+=m*60+s;
      }
      a[i]=make_pair(t,c);
    }
    sort(a,a+n);
    cout<<a[0].second<<endl;
    cout<<a[1].second<<endl;
    cout<<a[n-2].second<<endl;
  }
  return 0;
}

Measured memory_bytes: 8680
import java.util.*;
import java.util.Map.Entry;
 
 

class Main {
	 static int mod =  (int) (Math.pow(10,9)+7);
   public static void main(String[] args) {
	   
       Scanner sc = new Scanner(System.in);
       int N = sc.nextInt();
       int M = sc.nextInt();

  	   List<ArrayList<Integer>> list= new ArrayList<ArrayList<Integer>>();
       for(long i=0;i<M*3+1;i++)list.add(new ArrayList<Integer>());
       
       int[] u = new int[M];
       int[] v = new int[M];
       for (int i=0 ; i<M; i++) {
           u[i] = sc.nextInt()-1;
           v[i] = sc.nextInt()-1;
       	   list.get(u[i]*3).add(v[i]*3+1);
       	   list.get(u[i]*3+1).add(v[i]*3+2);
       	   list.get(u[i]*3+2).add(v[i]*3);
       }
       int S = sc.nextInt()-1;
       int T = sc.nextInt()-1;
       
       
       LinkedList<Integer> queue = new LinkedList<Integer>();
       boolean[] visited = new boolean[3*N];
       int[] w = new int[3*N];
	   queue.add(S*3);
	   queue.add(0);
       while(!queue.isEmpty()) {
    	   int node = queue.poll();
    	   int wight = queue.poll();
    	   if(visited[node])continue;
    	   visited[node]=true;
    	   w[node]=wight;
    	   for(int i:list.get(node)) {
    		   queue.add(i);
    		   queue.add(wight+1);
    	   }
       }
       
       int ans = w[T*3]/3;
       if(ans==0)ans=-1;
       System.out.println(ans); 	
              		
   }

}

Measured memory_bytes: 132352
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define TEST int t;cin>>t;for(int T=1;T<=t;T++)
#define F first
#define S second
int main(){
  
  int a;
  cin>>a;
  cout<<a+(a*a)+(a*a*a)<<endl;
}

Measured memory_bytes: 3620
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
const int N=1e5+5;

ll dp[102][N]={-1},n,w,v[2][N];
ll solve(int index, ll weight);
ll minf = -0x3f3f3f3f;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    memset(dp,-1,sizeof(dp));
    ll i,j,m,w;    cin>>n>>w;
    for(i=1;i<=n;i++)cin>>v[0][i]>>v[1][i]; // weight,value
    cout<<solve(n,w)<<"\n";

    return 0;
}

ll solve(int index, ll weight){
    if(weight<0)
        return minf;
    else if(index<=0)
        return 0;

    if(dp[index][weight]!=-1)
        return dp[index][weight];
    return dp[index][weight]=max(v[1][index]
        +solve(index-1,weight-v[0][index]),solve(index-1,weight));
}

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

#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef complex<double> point;
#define xx real()
#define yy imag()

#define REP(i, a, b) for(int i = (a); i < (int)(b); i++)
#define REPN(i, a, b) for(int i = (a); i <= (int)(b); i++)
#define FA(it, x) for(__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define SZ(x) (int)(x).size()
#define BE(x) (x).begin(), (x).end()
#define SORT(x) sort(BE(x))
#define _1 first
#define _2 second

#define x1 gray_cat_x1
#define y1 gray_cat_y1

template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

#define file "I1"

const double EPS = 1e-9;
const double PI = acos(-1.);
const ll INF = 1e15;
const ll MOD = 1e9 + 7;

const int MAXN = 5e3 + 5;

int per[MAXN];
ll dp[MAXN][MAXN];

void solve(){
	int n;
	ll a, b;
	cin >> n >> a >> b;
	REPN(i, 1, n) {
		cin >> per[i];
	}
	REPN(i, 1, n) {
		REPN(j, 0, n) {
			dp[i][j] = INF;
		}
	}
	REPN(i, 1, n) {
		dp[i][0] = a * i;
	}
	REP(i, 0, n) {
		REPN(j, 0, n) {
			if (dp[i][j] == INF) {
				continue;
			}
			if (per[i + 1] > j) {
				dp[i + 1][per[i + 1]] = min(dp[i + 1][per[i + 1]], dp[i][j]);
				dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a);
			} else {
				dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + b);
			}
		}
	}
	ll ans = INF;
	REPN(i, 0, n) {
		ans = min(ans, dp[n][i]);
	}
	cout << ans << endl;
}   

int main(){

    //freopen(file".in", "r", stdin); freopen(file".out", "w", stdout);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();    
    }
}


Measured memory_bytes: 195840
import math
from itertools import accumulate


def is_prime(n):
    if n == 1:
        return 0

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return 0

    return 1


Q = int(input())
L = [0]*Q
R = [0]*Q

for i in range(Q):
    L[i], R[i] = list(map(int, input().split()))

min_L = min(L)
max_R = max(R)

check = []
for i in range(min_L, max_R+1):
    if (i % 2 == 1):
        flg = (is_prime(i) and is_prime((i+1)/2))
    else:
        flg = 0
    check.append(flg)

# 累積和
check = [0] + check
check = list(accumulate(check))

for i in range(Q):
    print(check[R[i] - min_L + 1] - check[L[i] - min_L])

Measured memory_bytes: 20584
typedef long long int ll;
typedef unsigned long long int ull;
#define fast  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define MOD 1000000007
#define M_PI           3.14159265358979323846  /* pi */
#define sec second
#define fir first
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define repe(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define entire(a) (a).begin(),(a).end()
#define pb push_back
#define pii pair<int,int>
#include<bits/stdc++.h>
#include<cmath>
using namespace std;
void solve()
{
  ll x,k,d;
  cin>>x>>k>>d;
  if(x<0)x*=-1;
  ll r=x%d;
  ll q=x/d;
  // cout<<q<<" "<<r<<endl;
  if(q==k){
  	// if()
  	cout<<r<<endl;
  }else if(q>k){
  	cout<<x-k*d<<endl;
  }else{
  	if((q-k)&1)
  	cout<<abs(d-r)<<endl;
  else
  	cout<<r<<endl;
  }
}


int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast
  int T=1;
  // cin >> T;
  while (T--)
  {
    solve();
  }
  return 0;
}


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

using namespace std;

int main(void)
{
  int X, N;
  cin >> X >> N;
  vector<int> included(102, 0); // 含まれていない
  for (int i=0; i<N; i++) {
    int pi;
    cin >> pi;
    included.at(pi-1) = 1; // 含まれている
  }
  int min = 99999999;
  int ans;
  for (int i=-10; i<102; i++) {
    if (i >= 0 && included.at(i)) { // もしその数が含まれているなら
      continue;
    }
    int diff = i+1 - X;
    if (diff < 0) diff = -diff;
    if (diff < min) {
      ans = i+1;
      min = diff;
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 3628
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
int main( ) {
    // freopen("A.01.in","r",stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int a,b;
    scanf("%d %d",&a,&b);
    printf("%d",max(a+b,max(a*b,a-b)));
    return 0;
}

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

typedef long long ll;

#define MAX 25
#define NIL -1

struct Node{
    int p,l,r;
};

int n;
Node tree[MAX];

void init(){
    for(int i=0;i<MAX;i++){
        tree[i].p = tree[i].r = tree[i].l = NIL;
    }
}

void print_pre(int r){
    if(r==NIL) return;
    else {
        cout << " " << r;
        print_pre(tree[r].l);
        print_pre(tree[r].r);
    }
}

void print_in(int r){
    if(r==NIL)return;
    else {
        print_in(tree[r].l);
        cout << " " << r;
        print_in(tree[r].r);
    }
}

void print_post(int r){
    if(r==NIL)return;
    else{
        print_post(tree[r].l);
        print_post(tree[r].r);
        cout << " " << r;
    }
}

void print(){
    int root;
    for(int i=0;i<n;i++)if(tree[i].p==NIL)root=i;
    cout << "Preorder\n";
    print_pre(root);
    cout << "\n";
    cout << "Inorder\n";
    print_in(root);
    cout << "\n";
    cout << "Postorder\n";
    print_post(root);
    cout << "\n";

}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    init();
    cin >> n;
    int id,lft,rgt;

    for(int i=0;i<n;i++){
        cin >> id >> lft >> rgt;
        tree[id].l=lft;
        tree[id].r=rgt;
        tree[lft].p = tree[rgt].p = id;
    }

    print();
}



Measured memory_bytes: 3176
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define repd(i,a,b) for(int i=(a);i>=(b);--i)
#define repit(i,a) for(__typeof((a).begin()) i=(a).begin();i!=(a).end();++i)

#define all(u) (u).begin(),(u).end()
#define rall(u) (u).rbegin(),(u).rend()
#define UNIQUE(u) (u).erase(unique(all(u)),(u).end())

#define pb push_back
#define mp make_pair
#define INF 1e9
#define EPS 1e-9
#define PI acos(-1.0)

using namespace std;

typedef long long ll;
typedef vector<int> vi;

string prop[32];

int main()
{
    rep(i,32) prop[i] = "";
    string in;
    cin >> in;
    int sz = 0;
    rep(i,in.size()){
	if(in[i] == '.') sz++;
	else prop[sz-1] += in[i];
    }
    int pos = 0;
    int depth = 0;
    int prev = 0;
    bool flag = false;
    int indents[50000] = {0};
    cin.ignore();
    while(getline(cin,in)){
	if(in.size() == 0) break;
	int cur = 0;
	while(in[cur] == ' ') cur++;

	string tag = "", str = "";
	int t = cur;
	while(in[t] != ':') tag += in[t++];
	t += 2;
	while(t < in.size()) str += in[t++];

	if(cur > prev) {
	    depth++;
	    indents[depth] = cur;
	}
	if(cur < prev) {
	    while(indents[depth] > cur) depth--;
	}
	if(depth < pos){
	    pos = depth;
	}

	if(depth == pos && prop[pos] == tag){
	    pos++;
	    if(pos == sz){
		if(str.size() == 0) cout << "object\n";
		else cout << "string \"" << str << "\"\n";
		return 0;
	    }
	}
	prev = cur;
    }
    cout << "no such property\n";
    return 0;
}

Measured memory_bytes: 1484
#include <iostream>
using namespace std;
int n, a[100], res = 10000000;
int main(void){
    // Your code here!
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = -100; i <= 100; i++) {
        int ans = 0;
        for (int j = 0; j < n; j++) {
            ans += (a[j] - i) * (a[j] - i);
        }
        res = min(res, ans);
    }
    cout << res << endl;
}


Measured memory_bytes: 256
INF = 10 ** 10
dic = {}
for i in range(10):
  dic[str(i)] = i
for i in range(ord("a"), ord("f") + 1):
  dic[chr(i)] = i - ord("a") + 10

def hex_to_dec(s):
  return dic[s[0]] * 16 + dic[s[1]]

colors = [("black",   list(map(hex_to_dec, ["00", "00", "00"]))),
          ("blue",    list(map(hex_to_dec, ["00", "00", "ff"]))),
          ("lime",    list(map(hex_to_dec, ["00", "ff", "00"]))),
          ("aqua",    list(map(hex_to_dec, ["00", "ff", "ff"]))),
          ("red",     list(map(hex_to_dec, ["ff", "00", "00"]))),
          ("fuchsia", list(map(hex_to_dec, ["ff", "00", "ff"]))),
          ("yellow",  list(map(hex_to_dec, ["ff", "ff", "00"]))),
          ("white",   list(map(hex_to_dec, ["ff", "ff", "ff"])))]

while True:
  s = input()
  if s == "0":
    break
  r, g, b = map(hex_to_dec, [s[1:3], s[3:5], s[5:7]])
  lowest_name = "KUWA"
  lowest_score = INF
  for name, rgb in colors:
    rc, gc, bc = rgb
    score = (r - rc) ** 2 + (g - gc) ** 2 + (b - bc) ** 2
    if score < lowest_score:
      lowest_name, lowest_score = name, score
  print(lowest_name)


Measured memory_bytes: 5584
import math
from collections import deque
import itertools

[H, W] = map(int, input().rstrip().split(" "))
S = []


for h in range(H):
    Ss = input().rstrip()
    S.append(Ss)

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

max_cost = 0

for h in range(H):
    for w in range(W):
        if S[h][w] == "#":
            continue
        que = deque()
        history = [ [float('inf') for w in range(W)] for h in range(H)]
        que.append([h, w])
        history[h][w] = 0
        current_max_cost = 0
        while len(que) != 0:
            [x, y] = que.popleft()
            for i in range(4):
                newx = x + dx[i]
                newy = y + dy[i]
                if 0 <= newx and newx < H and 0 <= newy and newy < W and S[newx][newy] != "#" and history[newx][newy] == float('inf'):
                    history[newx][newy] = history[x][y] + 1
                    if current_max_cost < history[newx][newy]:
                        current_max_cost = history[newx][newy]
                    que.append([newx, newy])
        if max_cost < current_max_cost:
            max_cost = current_max_cost

print(max_cost)


Measured memory_bytes: 3316
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<cctype>
#define ld long double
#define ll long long int
#define ull unsigned long long int
using namespace std;
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 long long INF=1LL<<60;


int main(void){
	ios::sync_with_stdio(false); cin.tie(0);
	
	int i,j,n,m,min,num;
	cin>>n>>m;
	int dis,a[n],b[n],c[m],d[m];
	for(i=0;i<n;i++)cin>>a[i]>>b[i];
	for(i=0;i<m;i++)cin>>c[i]>>d[i];
	for(i=0;i<n;i++){
		min=1000000000;
		for(j=0;j<m;j++){
			dis=abs(a[i]-c[j])+abs(b[i]-d[j]);
			if(min>dis){
				min=dis;
				num=j;
			}
		}
		cout<<num+1<<endl;
	}
	
	
	
	return 0;
}

Measured memory_bytes: 256
// lcmとか__builtin_popcountとかはg++ -std=c++17 default.cppみたいなかんじで

#include <bits/stdc++.h>
#define mod 1000000007
#define INF 1001001001
#define ll long long
#define ln cout<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
using namespace std;

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

    ll a,b=0,d,m,n,maxi=0,f=0,mini=INF,sum=0;
    string str;
    char c;
    cin>>str;
    deque<char> ch;
    rep(i,str.length()) {
        ch.push_back(str[i]);
    }
    cin >>n;
    rep(i,n){
        cin>>d;
        if(d==1){
           b++;
           b%=2;
        }else{
            cin>>a>>c;
            if(a==1){
                if(b==1) ch.push_back(c);
                else ch.push_front(c);
            }else{
                if(b==1) ch.push_front(c);
                else ch.push_back(c);
            }
        }
        
    }
    if(b==1) {
        while(!ch.empty()){
            cout<<ch.back();
            ch.pop_back();
        }
        }else{
            while(!ch.empty()){
            cout<<ch.front();
            ch.pop_front();
        
        }
    }

    return 0;
}


Measured memory_bytes: 4084
import java.io.PrintWriter
val pw = PrintWriter(System.out)
fun main(args: Array<String>) {
    func()
    pw.flush()
}
fun print(v: Any) { pw.print(v) }
fun println(v: Any) { pw.println(v) }

fun func() {
    val N = nextLong()
    val (gA, sA, bA) = listOfInt()
    val (gB, sB, bB) = listOfInt()
    val n = f(N, gA, sA, bA, gB, sB, bB)
    val ans = f(n, gB, sB, bB, gA, sA, bA)
    println(ans)
}

const val M = 100000

fun f(n: Long, ga: Int, sa: Int, ba: Int, gb: Int, sb: Int, bb: Int): Long {
    val dp = LongArray(M)
    for (i in 1..n.toInt()) {
        var v = i.toLong()
        if (i >= ga && ga < gb) {
            v = Math.max(v, dp[(i - ga + M) % M] + gb)
        }
        if (i >= sa && sa < sb) {
            v = Math.max(v, dp[(i - sa + M) % M] + sb)
        }
        if (i >= ba && ba < bb) {
            v = Math.max(v, dp[(i - ba + M) % M] + bb)
        }
        dp[i % M] = v
    }
    return dp[n.toInt() % M]
}
fun next() = readLine()!!
fun nextLong() = next().toLong()
fun listOfString() = next().split(" ")
fun listOfInt() = listOfString().map { it.toInt() }


Measured memory_bytes: 40872
#include <bits/stdc++.h>
#define LL long long
#define FOR(i,c) for(__typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define F first
#define S second
using namespace std;

const LL mod = 1e9 + 7;

template<typename T> T gcd(T a, T b) { return b == 0?a: gcd(b, a % b); }
template<typename T> T LCM(T a, T b) { return a*(b/gcd(a, b)); }
template<typename T, typename S> T expo(T b, S e, const T &m){if(e <= 1)return e == 0?1: b;\
	return (e&1) == 0?expo((b*b)%m, e>>1, m): (b*expo((b*b)%m, e>>1, m))%m;}
template<typename T, typename S> T expo(T b, S e){if(e <= 1)return e == 0?1: b;\
	return (e&1) == 0?expo((b*b), e>>1): (b*expo((b*b), e>>1));}
template<typename T, typename S> T modinv(T a, S mod) { return expo(a, mod-2, mod); }
template<class T, class S> std::ostream& operator<<(std::ostream &os, const std::pair<T, S> &t) {
	os<<"("<<t.first<<", "<<t.second<<")";
	return os;
}
template<class T> std::ostream& operator<<(std::ostream &os, const std::vector<T> &t) {
	os<<"["; FOR(it,t) { if(it != t.begin()) os<<", "; os<<*it; } os<<"]";
	return os;
}

const int MAXN = 1e5 + 3;
LL arr[MAXN], n;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  LL cnt = 0;
  for(int i = 0; i < n; i ++) {
    cin >> arr[i];
    cnt += arr[i] % 2;
  }
  if(cnt % 2 == 0) cout << "YES" << endl;
  else cout << "NO" << endl;
  return 0;
}


Measured memory_bytes: 1024
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define fur(i,x,y) for(i=x;i<=y;i++)
#define fdr(i,x,y) for(i=x;i>=y;i--)
#define Fur(i,x,y) for(ll i=x;i<=y;i++)
#define Fdr(x,y) for(ll i=x;i>=y;i--)
#define in2(x,y) in(x);in(y)
#define in3(x,y,z) in2(x,y);in(z)
#define in4(a,b,c,d) in2(a,b);in2(c,d)
#define clr(x,y) memset(x,y,sizeof(x))
#define cpy(x,y) memcpy(x,y,sizeof(x))
#define fl(i,x) for(ll i=head[x],to;to=e[i].to,i;i=e[i].next)
#define inf 233333333
using namespace std;
/*---------------------------------------*/
namespace fib{char b[300000]= {},*f=b;}
#define gc ((*fib::f)?(*(fib ::f++)):(fgets(fib::b,sizeof(fib::b),stdin)?(fib::f=fib::b,*(fib::f++)):-1))
inline void in(ll &x){x=0;char c;bool f=0;while((c=gc)>'9'||c<'0')if(c=='-')f=!f;x=c-48;while((c=gc)<='9'&&c>='0')x=x*10+c-48;if(f)x=-x;}
namespace fob{char b[300000]= {},*f=b,*g=b+300000-2;}
#define pob (fwrite(fob::b,sizeof(char),fob::f-fob::b,stdout),fob::f=fob::b,0)
#define pc(x) (*(fob::f++)=(x),(fob::f==fob::g)?pob:0)
struct foce{~foce(){pob;fflush(stdout);}} _foce;
namespace ib{char b[100];}
inline void out(ll x){if(x==0){pc(48);return;}if(x<0){pc('-');x=-x;}char *s=ib::b;while(x) *(++s)=x%10,x/=10;while(s!=ib::b) pc((*(s--))+48);}
inline void outn(ll x){out(x);pc('\n');}
inline void swap(ll &x,ll &y){ll t=x;x=y;y=t;}
inline ll jdz(ll x){return x>=0?x:-x;}
/*------------------------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------------------------*/
#define N 100001
ll a[61][61],n,m;
char c[51];
int main(){
	in2(n,m);
	Fur(i,1,n){
		scanf("%s",c+1);
		Fur(j,1,m)
		if(c[j]=='#')a[i][j]=1;
	}
	Fur(i,1,n)Fur(j,1,m)if(a[i][j])if(!(a[i+1][j]||a[i][j+1]||a[i-1][j]||a[i][j-1])){printf("No\n");return 0;}
	printf("Yes\n");
}

/*

*/

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = s; i < (int)(n); i++)
#define out(x) cout << x << endl;
#define INF 1ll<<60;
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; }

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

int main(){
    int n, x, t; cin >> n >> x >> t;
    out(((n+x-1)/x)*t);
}

Measured memory_bytes: 3584
#include <stdio.h>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <string>
#include <map>
using namespace std;

//ABC 149B
int main() {
	long a, b, k;
	cin >> a >> b >> k;

	long n = min(a, k);
	a -= n;
	k -= n;

	long m = min(b, k);
	b -= m;
	cout << a << " " << b << endl;
}

Measured memory_bytes: 256
import java.util.*;
public class Main{
	public static void main(String[] args){
      Scanner sc = new Scanner(System.in);
      int h = sc.nextInt();
      int w = sc.nextInt();
      int[][] c = new int[10][10];
      for(int i = 0;i < 10;i++)
        for(int j = 0;j < 10;j++)c[i][j]=sc.nextInt();
      int[][] a = new int[h][w];
      for(int i = 0;i < h;i++)
        for(int j = 0;j < w;j++)a[i][j]=sc.nextInt();
      for(int k = 0;k < 10;k++)
        for(int i = 0;i < 10;i++)
          for(int j = 0;j < 10;j++)
            c[i][j]=Math.min(c[i][j],c[i][k]+c[k][j]);
      int sum = 0;
      for(int i = 0;i < h;i++)
        for(int j = 0;j < w;j++){
          if(a[i][j]!=-1)sum+=c[a[i][j]][1];
        }
      System.out.println(sum);
    }
}

Measured memory_bytes: 47596
#prag\
ma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;

map<int,pair<int,int> > m;
int a[301][301],h,w,d;

inline int abss(int x){
	int xx=x;
	return xx>0?xx:-xx;
}

int main()
{
	ios::sync_with_stdio(false);
	cin>>h>>w>>d;
	int b[d+1][h*w/d+1];
	for(int i=1;i<=h;i++)
		for(int j=1;j<=w;j++)
		{
			cin>>a[i][j];
			m[a[i][j]]=make_pair(i,j);
		}
	for(int i=1;i<=d;i++){
		int s=0;
		pair<int,int> t=m[i];
		int x=t.first,y=t.second;
		for(int j=i+d;j<=h*w;j+=d)
		{
			pair<int,int> t2=m[j];
			int xx=t2.first,yy=t2.second;
			++s;
			b[i][s]=b[i][s-1]+abss(x-xx)+abss(y-yy);
			x=xx,y=yy;
		}
	}
	int q;
	cin>>q;
	while(q--){
		int l,r;
		cin>>l>>r;
		if(l%d!=0) cout<<b[l%d][r/d]-b[l%d][l/d]<<endl;
		else cout<<b[d][r/d-1]-b[d][l/d-1]<<endl;
	}
}

Measured memory_bytes: 7424
package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int {
    sc.Scan()
    i, e := strconv.Atoi(sc.Text())
    if e != nil {
        panic(e)
    }
    return i
}

var rdr = bufio.NewReaderSize(os.Stdin, 1000000)

func readLine() string {
    buf := make([]byte, 0, 1000000)
    for {
        l, p, e := rdr.ReadLine()
        if e != nil {
            panic(e)
        }
        buf = append(buf, l...)
        if !p {
            break
        }
    }
    return string(buf)
}

func nextLine() string {
    sc.Scan()
    return sc.Text()
}


func main(){
	var H,N,res int
	fmt.Scan(&H)
	fmt.Scan(&N)
	res=0
	for i:=0;i<N;i++{
		tmp:=0
		fmt.Scan(&tmp)
		res+=tmp
	}
	if res>=H {
		fmt.Printf("Yes")
	}else {
		fmt.Printf("No")
	}

}


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

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

    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    long long result = a[0] - 1;

    int last = 1;
    for (int i = 1; i < n; ++i) {
        if (a[i] <= last) {
            continue;
        }
        if (a[i] == last + 1) {
            ++last;
            continue;
        }

        int c = a[i] / (last + 1);
        int r = a[i] % (last + 1);
        if (r == 0) {
            --c;
        }

        result += c;
    }

    cout << result << endl;

    return 0;
}

Measured memory_bytes: 768
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define inf 50009
vector<ll>seq;
ll l,r,ten=10,last=1000000000000;
map<ll,ll>cnt;
map<ll,ll>sub;
ll arra[100009];
ll aps[1009][1009];
string str1[1009];
ll vis[109][109];
ll dp[209][209][209];
ll once[209];
ll forever[209];
vector<ll>anse;
int main()
{
  ios::sync_with_stdio(false);
  ll i,j,m,p=10000000000000,x,q,z=-1,sum=0,n,k,y;
  string str;
  cin>>n;
  for(i=0;i<n;i++)
  {
    cin>>x>>y;
    if(x==y)
     sum++;
    else
    {
      sum=0;	
    } 
   if(sum==3)
    {
      z=1;
      //break;	
    } 	
  }
  if(z==1)
  cout<<"Yes"<<'\n';
  else
  cout<<"No"<<'\n';
 
  
    
    
   	
 }
  
  
   




Measured memory_bytes: 3676
from sys import stdin
N = int(stdin.readline().rstrip())
data = stdin.readline().rstrip().split()
w = [None] * N
for i in range(N):
    w[i] = int(data[i])
w_sum = sum(w)
vals = 0
ans = 10000
for i in range(N):
    vals += w[i]
    ans = min(abs((w_sum - vals) - vals), ans)
print(ans)

Measured memory_bytes: 3064
#include<bits/stdc++.h>
using namespace std;
queue<int>q;
vector<int>v[100003];
int dis[2][100003],n,a,b;
void bfs(int x){
	q.push(x*(n-1));
	dis[x][x*(n-1)]=1;
	while(!q.empty()){
		int f=q.front();
		q.pop();
		for(int i=0;i<v[f].size();i++)
			if(!dis[x][v[f][i]])
				dis[x][v[f][i]]=dis[x][f]+1,
				q.push(v[f][i]);
	}
}
int main(){
	cin>>n;
	for(int i=0;i<n-1;i++){
		cin>>a>>b;
		v[a-1].push_back(b-1);
		v[b-1].push_back(a-1);
	}
	bfs(0);bfs(1);
	a=0;b=0;
	for(int i=0;i<n;i++)
		if(dis[0][i]>dis[1][i])
			b++;
		else
			a++;
	if(a>b)
		cout<<"Fennec";
	else
		cout<<"Snuke";
}

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

int main()
{
  int a,b,c,k;
  cin>>a>>b>>c>>k;
  for(int i=1;i<=k;i++)
  {
    if(a>=c || b>=c)
      c=c*2;
    else if(a>=b)
      b=b*2;
  }
  if(c>b && b>a)
    cout<<"Yes"<<endl;
  else
    cout<<"No"<<endl;
}

Measured memory_bytes: 3628
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define ll long long
#define _GLIBCXX_DEBUG
const ll MOD = 1000000007;
const int MAX = 510000;

int main() {
  ll X, current = 100LL; // 現在の金額
  cin >> X;
  for (int i=1; i<=3760; i++) {
    current *= 1.01;
    if (current>=X) {
      cout << i << endl;
      break;
    }
  }
}

Measured memory_bytes: 3644
#include <iostream>
#include <cfenv>
#include <cmath>

using namespace std;

#define repd(i,a,b) for (int i=(a);i<(b);++i)
#define rep(i,n) repd(i,0,n)
typedef long long ll;
typedef pair<int, int> P;

int main(int argc, const char * argv[]) {
  int a, b;
  cin >> a >> b;
  rep(i, 10000){
    // cout << (i+1)*0.08 << endl;
    if(floor((i+1)*0.08) == a){
      int ans = floor((i+1)*0.1);
      if(floor((i+1)*0.1) == b){
        cout << i+1 << endl;
        return 0;
      }
    }
  }
  cout << -1 << endl;
  return 0;
}


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

typedef long long ll;

const int INF = 1e9;
const int MOD = 1e9+7;
const ll LLINF = 1e18;

#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define ANS(n) cout << (n) << endl

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,a,b) for(int i=(a);i>=(b);i--)

#define ALL(obj) (obj).begin(),(obj).end()

#define pii pair<int,int>

#define pb(a) push_back(a)
#define mp make_pair

int main()
{
	int ans = 0;
	int n;
	cin >> n;
	FOR(i,0,n){
		int l,r;
		cin >> l >> r;
		ans += (r-l+1);
	}
	ANS(ans);
	return 0;
}


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

class Program
{
    public void Slove()
    {
        int n = int.Parse(Console.ReadLine());
        int[] point =
            Console.ReadLine().Split()
            .Select(int.Parse)
            .ToArray();
        point =
            Console.ReadLine().Split()
            .Select(int.Parse)
            .Select((x, i) =>
                point[i] - x)
            .OrderBy(x => x)
            .ToArray();
        int result = 0;
        //Console.WriteLine(string.Join(",", point));
        for(int start = 0, end = point.Length - 1, p = 0;;)
        {
            //Console.WriteLine($"{start} {end}");
            if(p < 0)
            {
                if(point[end] <= 0 || start > end)
                {
                    result = -1;
                    break;
                }
                p += point[end--];
            }
            else
            {
                if(point[start] >= 0 || start > end)
                {
                    result = start + (point.Length - 1 - end);
                    break;
                }
                p += point[start++];
            }
        }
        Console.WriteLine(result);
    }
    static void Main(string[] args)
    {
        var p = new Program();
        p.Slove();
    }
}


Measured memory_bytes: 31712
#include<bits/stdc++.h>
using namespace std;
static const int INF=1000000;

int main(){
  int n,u,k,v;
  cin>>n;
  vector<int> G[n];
  int D[n];
  
  for(int i=0;i<n;i++){
    cin>>u>>k;
    u--;
    for(int j=0;j<k;j++){
      cin>>v;
      v--;
      G[u].push_back(v);
      //cout<<"#"<<u<<v<<endl;
      //G[v].push_back(u);
    }
  }
  for(int i=0;i<n;i++){
    D[i]=INF;
  }
    
  D[0]=0;
  queue<int>Q;
  Q.push(0);
  while(!Q.empty()){
    int u=Q.front();Q.pop();
    for(int i=0;i<G[u].size();i++){
      v=G[u][i];
      if(D[v]!=INF) continue;
      D[v]=D[u]+1;
      Q.push(v);
    }
  }
  for(int i=0;i<n;i++){
    cout<<i+1<<" "<<(D[i]==INF ? -1:D[i])<<endl;
  }
  
  return 0;
}

                          
    
     


Measured memory_bytes: 3164