akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n, m, d[8], k = 0, s = 0;
    for (int i = 0; i < 8; i++) d[i] = 0;
    cin >> n;
    m = n;
    while (m != 0)
    {
        d[k] = m % 10;
        k++;
        m /= 10;
    }
    for (int i = 0; i < 8; i++) s += d[i];
    if (n % s == 0) cout << "Yes";
    else cout << "No";
}

Measured memory_bytes: 3604
N,M = map(int,input().split())
foods = [0]*M

for i in range(N):
  KA = list(map(int,input().split()))
  
  for a in KA[1:]:
    foods[a-1] += 1
  
cnt = 0
for j in foods:
  if j == N:
    cnt += 1
    
print(cnt)

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

int main(){
    int n;
    cin >> n;
    int h[n];
    for(int i=0; i<n; i++){
        cin >> h[i];
    }
    int ans = 0;
    int t = 0;
    for(int i=0; i<n-1; i++){
        if(h[i] >= h[i+1]) t++;
        else {
            if(ans < t) ans = t;
            t = 0;
        }
    }
    if(ans < t) ans = t;
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) FOR(i, 0, (n))
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define LAR(a, b) ((a)=max((a),(b)))
#define SML(a, b) ((a)=min((a),(b)))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template<typename T>
using pque = priority_queue<T, vector<T>, greater<T>>;
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#ifdef LOCAL_DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

char s[20];
char t[5] = "hi";

int main(){
	scanf("%s", s);
	int n = strlen(s);
	bool ok = n%2 == 0;
	REP(i, n) if(s[i] != t[i%2]) ok = false;
	if(ok) printf("Yes\n");
	else printf("No\n");
}


Measured memory_bytes: 256
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;
#define S 500005
#define M 1000000007
#define nl cout<<"\n";
#define er  cout<<"\n***********\n";
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define test lli t;  cin>>t; while(t--)
#define all(x) (x).begin(),(x).end()
#define sz(x) (lli)(x.size())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define Y cout<<"Yes\n";
#define N cout<<"No\n";
#define print(x)                 cout<<#x<<": "<<x<<endl;
#define print2(x, y)             cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl;
#define print3(x, y, z)          cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define print4(a, b, c, d)       cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define print5(a, b, c, d, e)    cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl;
#define print6(a, b, c, d, e, f) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl;
lli arr[S];
lli n,k;
lli mia_melona()
{
    if(arr[n]==0&&k%2) return 0;
    lli ans=1;
    if(arr[n]<0&&k%2)
    {
        for(lli i=n;i>=n-k+1;i--)
        {
            ans=(ans*(arr[i]));
            ans=(ans+M)%M;
        }
        return ans;
    }
    lli i=1,j=n;
    if(k%2)
    {
        k--;
        ans=((ans%M)*((arr[j]+M)%M));
        ans=(ans+M)%M;
        j--;
    }
    k=k/2;
    for(lli ii=1;ii<=k;ii++)
    {
        lli fst=arr[i]*arr[i+1];
        lli sec=arr[j]*arr[j-1];
        if(fst>sec)
        {
            ans=((ans%M)*((fst+M)%M));
            ans=(ans+M)%M;
            i=i+2;
        }
        else
        {
            ans=((ans%M)*((sec+M)%M));
            ans=(ans+M)%M;
            j=j-2;           
        }
    }

    return ans;
}
void solve()
{

   cin>>n>>k;
   for(lli i=1;i<=n;i++)
   {
     cin>>arr[i];
   }
   sort(arr+1,arr+n+1);

   lli ans=mia_melona();

   cout<<ans;





   

}


int main()
{
    fast

    #ifndef ONLINE_JUDGE
     freopen("inputa.txt", "r", stdin);
     freopen("outputa.txt", "w", stdout);
    #endif    
     
     // test
     {
          solve();
          nl
     }
}
//a+b=a?b+2?(a&b)
//__builtin_popcnt(number)
//cout<<fixed<<setprecision(8)<<output<<"\n";

Measured memory_bytes: 5160
#include <stdio.h>

int main(void){
  unsigned int a,b;
  int k;
  scanf("%d%d%d",&a,&b,&k);
  for(int i=0;i<k;i+=2){
    if(a&1){a--;}
    b+=a/2;
    a=a/2;
    if(i+1>=k) break;
    if(b&1){b--;}
    a+=b/2;
    b=b/2;
  }
  printf("%d %d\n",a,b);
  return 0;
}


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

void solve(void){
  int RI, N, R;
  
  cin >> N >> R;
  
  if(N<10){
  	RI = R + 100*(10-N);
  }
  else
  {
    RI = R;
  }
  
  cout << RI << endl;
  
  return;
}

int main(int argc, const char * argv[]) {

    // cinを高速にするためのおまじない
    cin.tie(0);
    ios::sync_with_stdio(false);

  solve();
  
  return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <cctype>
#include <limits.h>
#include <math.h>
#include <complex>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x);
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))

const int INF = 1000000000;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4]={-1, 0, 1, 0}, dy[4]={0, -1, 0, 1};
using namespace std;
typedef pair<int, int> P;

/** Problem0593 : Vote **/
int main()
{
	int N, M;
	cin>>N>>M;
	vector<int> A(N), B(M), C(N);
	rep(i, N) cin>>A[i];
	rep(i, M) cin>>B[i];
	
	rep(i, M) {
		rep(j, N) {
			if (B[i]>=A[j]) {
				C[j]++;
				break;
			}
		}
	}
	
	vector<int>::iterator it = max_element(C.begin(), C.end());
	cout << distance(C.begin(), it)+1 << endl;
}

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

using namespace std;

#define REP(i, n) for(int i = 0; i < (n); i++)
#define SORT(v, n) sort(v, v+n);
#define SSORT(v, n) sort(v, v+n, greater<int>())

int main(){
	int n,k,p;
	string s;
	cin >> n >> k >> s;
	REP(i,n){
		if(i==k-1) cout << char(tolower(s[i]));
		else cout << s[i];
	}

}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define REP(i,n) for (int64_t i=0; i<(n); ++i)
#define P pair<int,int>
using ll=int64_t;
using namespace std;
#define ketasuu(n) fixed<<setprecision(n)
#define btoe(p) p.begin(),p.end()
#define etob(p) p.rbegin(),p.rend()




int main(){
    int a,b; cin>>a>>b;
    if((a*b)%2==0){
        cout<<"Even"<<endl;
    }else{
        cout<<"Odd"<<endl;
    }
}

Measured memory_bytes: 3568
#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<functional>
#include<utility>
#include<bitset>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cstdio>

using namespace std;

#define REP(i,n) for(int i=0;i<int(n);i++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
typedef long long ll;
typedef pair<int,int> P;

int main(void){
	          int i;

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

	      	  ll A,B,C,K;
	      	  cin >> A >> B >> C >> K;

	      	  if(labs(B-A)>1e18){
	      		  cout << "Unfair" << endl;
	      		  return 0;
	      	  }
	      	  if(K%2==1){
	      		  cout << B-A << endl;
	      	  }else{
	      		  cout << A-B << endl;
	      	  }

	          return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for(int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  ll d, n;
  cin >> d >> n;
  if(n == 100) n++;
  rep(i, d) n *= 100;
  cout << n << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <set>

using namespace std;

int main(){
    set<int> s;
    int x,n;
    cin >> x >> n;
    if(n == 0){
        cout << x << endl;
        return 0;
    }
    for(int i = 0; i < n; i++){
        int a;
        cin >> a;
        s.insert(a);
    }

    int idx = 0;
    while(true){
        if(s.find(x-idx) == s.end()){
            cout << x-idx << endl;
            break;
        }
        if(s.find(x+idx) == s.end()){
            cout << x+idx << endl;
            break;
        }
        idx++;
    }
    return 0;

}


Measured memory_bytes: 3640
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::vector;

int main() {
  int N, C;
  vector<int> s(100000), t(100000), c(100000), sm(200002), tt(200002);
  cin >> N >> C;
  for (int i = 0; i < N; ++i)
    cin >> s[i] >> t[i] >> c[i];
  for (int i = 1; i <= C; ++i) {
    for (int j = 0; j < 200002; ++j) {
      tt[j] = 0;
    }
    for (int j = 0; j < N; ++j) {
      if (c[j] == i) {
        ++tt[s[j] * 2 - 1];
        --tt[t[j] * 2];
      }
    }
    for (int j = 1; j < 200002; ++j) {
      tt[j] += tt[j - 1];
    }
    for (int j = 0; j < 200002; ++j) {
      if (tt[j] > 0) {
        ++sm[j];
      }
    }
  }
  int mx = 0;
  for (int j = 0; j < 200002; ++j) {
    if (mx < sm[j]) {
      mx = sm[j];
    }
  }
  cout << mx << endl;
}


Measured memory_bytes: 2944
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <bitset>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <string>
#include <chrono>
#include <random>
#include <cmath>
#include <cassert>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <sstream>

using namespace std;


int main(int argc, char** argv) {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, k;
    cin >> n >> k;
    vector<int> A(n);
    for (int i = 0; i < n; ++i) {
        cin >> A[i];
    }
    vector<int> cnts(n, 0);
    vector<int> cnts2(n, 0);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < i; ++j) {
            if (A[i] < A[j]) {
                ++cnts[i];
            }
        }
        cnts2[i] = cnts[i];
        for (int j = i + 1; j < n; ++j) {
            if (A[i] < A[j]) {
                ++cnts2[i];
            }
        }
        // cout << i << " " << cnts[i] << " " << cnts2[i] << endl;
    }

    long long MOD = 1000000007LL;
    long long inv2 = (MOD + 1) / 2;
    auto calc = [&](long long c1, long long c2, long long K) -> long long  {
        long long res = (K - 1) * K % MOD * inv2 % MOD;
        (res *= c2) %= MOD;
        (res += c1 * K % MOD) %= MOD;
        return res;
    };

    long long res = 0;
    for (int i = 0; i < n; ++i) {
        res += calc(cnts[i], cnts2[i], k);
        res %= MOD;
    }

    cout << res << '\n';

    return 0;
}

Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
#define MOD 1000000007
#define Nmax 200010
#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define RFOR(i,a,b) for(long long i = (b-1);i>=a;i--)
#define REP(i,n)  FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ITR(itr,mp) for(auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr,mp) for(auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<P> Graph;

int main() {
	int n;
	cin >> n;
	ll a[Nmax]={};
	REP(i,n) cin >> a[i];
	bool flag=true;
	REP(i,n-1){
		a[i+1]+=a[i];
		if(a[i+1]*a[i]>=0) flag=false;
	}
	if(flag){
		cout<< 0 << endl;
		return 0;
	}
	ll tasu=0,aa=0,cnt=0;//aa:変更量
	REP(i,n){//- → + → - → + →,,,
		aa=0;
		if(i%2==0){
			if(a[i]+tasu<0) continue;
            else if(a[i]+tasu>0) aa=abs(a[i]+tasu)+1;
			else aa=1;
			cnt+=aa;
			tasu-=aa;
		}else{
			if(a[i]+tasu>0) continue;
            else if(a[i]+tasu<0) aa=abs(a[i]+tasu)+1;
			else aa=1;
			cnt+=aa;
			tasu+=aa;
		}
	}
	ll cnt2=0;
  	tasu=0;
	REP(i,n){
		aa=0;
		if(i%2!=0){
			if(a[i]+tasu<0) continue;
            else if(a[i]+tasu>0){
				aa=abs(a[i]+tasu)+1;
			}else{
				aa=1;
			}
			cnt2+=aa;
			tasu-=aa;
		}else{
			if(a[i]+tasu>0) continue;
            else if(a[i]+tasu<0){
				aa=abs(a[i]+tasu)+1;
			}else{
				aa=1;
			}
			cnt2+=aa;
			tasu+=aa;
		}
    }
	cout << min(cnt,cnt2) << endl;
	return 0;
}

Measured memory_bytes: 1792
#include <stdio.h>
#include <iostream>
#include <vector>
#include <cassert>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <math.h>
#include <map>
using namespace std;

int s;

vector<int> a;

int main()
{
    cin >> s;
    int now = s;
    int ct = 0;
    a.push_back(now);
    ct++;
    while (true)
    {
        ct++;
        if (now % 2 == 0)
        {
            now = now / 2;
        }
        else
        {
            now = now * 3 + 1;
        }
        //cout << now << endl;
        for (int i = 0; i < a.size(); i++)
        {
            if (a[i] == now)
            {
                cout << ct;
                return 0;
            }
        }
        a.push_back(now);
    }

    return 0;
}

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

int INF=1e9;

int h,w,d,a[90010],b[90010],q,dp[90010];

int main(void){
    cin>>h>>w>>d;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            int x;
            cin>>x;
            a[x]=i;
            b[x]=j;
        }
    }
    for(int i=1;i<=d;i++)dp[i]=0;
    for(int i=1;i<=d;i++){
        for(int j=1;i+d*j<=h*w;j++){
            dp[i+d*j]=dp[i+d*(j-1)]+abs(a[i+d*j]-a[i+d*(j-1)])+abs(b[i+d*j]-b[i+d*(j-1)]);
        }
    }
    cin>>q;
    for(int i=0;i<q;i++){
        int x,y;
        cin>>x>>y;
        cout<<dp[y]-dp[x]<<endl;
    }
    
    return 0;
}


Measured memory_bytes: 2176
def solve():
    n = int(input())
    a = tuple(int(x) for x in input().split())

    memo = tuple([None] * n for _ in range(n))
    for i in range(n):
        memo[i][i] = a[i]

    for l in range(n - 2, -1, -1):
        for r in range(l + 1, n):
            memo[l][r] = max(a[l] - memo[l + 1][r], a[r] - memo[l][r - 1])

    print(memo[0][n - 1])


if __name__ == '__main__':
    solve()

# [l,r]を
# l:=配列aの右端から左にシフト
# r:=lと一致する位置からn-1まで右にシフト
# と考えると、l==rは既にメモ化済みなので、l=n-2以前、r=l+1以降を調べればよい
# 漸化式で使う値は、
# lがより右にいるときの計算結果（前のループで計算済み）と
# rがより左にいるときの計算結果（一番左でもl==rなので計算済み）
# なので、参照する値がNoneになることはなく、Noneのチェックは不要になる

# maxの引数の順番を変えた


Measured memory_bytes: 179804
#include<iostream>
#include<vector>
#include<cstdio>
#include<string>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<string.h>
#include <iomanip>
#include <sstream>
using namespace std;

int main() {
  int N, M;
  int ans = 0;
  string s;

  cin >> s;
//SUN,MON,TUE,WED,THU,FRI,SAT
  if(s == "SAT"){
    ans = 1;
  }else if(s == "FRI"){
    ans = 2;
  }else if(s == "THU"){
    ans = 3;
  }else if(s == "WED"){
    ans = 4;
  }else if(s == "TUE"){
    ans = 5;
  }else if(s == "MON"){
    ans = 6;
  }else if(s == "SUN"){
    ans = 7;
  }
  cout << ans << endl;
  
}


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

/**
 * vector<int>ar(3);
 * for(auto&e:ar){
 *     scanf("%d",&e);
 * }
 * sort(ar.begin(),ar.end())
 * int sum=accumulate(ar.begin(),ar.end(),0);
 ***/

int main(){
    double pai=3.141592653589;
    int dice[6],q;
    char c;
    for(int i=0;i<6;i++){
        scanf("%d",&dice[i]);
    }
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        int x,y;
        scanf("%d %d",&x,&y);
        if((x==dice[5] && y==dice[4])||(x==dice[4] && y==dice[0])||(x==dice[0] && y==dice[1])||(x==dice[1] && y==dice[5])){
            printf("%d\n",dice[2]);
        }else if((x==dice[0] && y==dice[2])||(x==dice[2] && y==dice[5])||(x==dice[5] && y==dice[3])||(x==dice[3] && y==dice[0])){
            printf("%d\n",dice[4]);
        }else if((x==dice[5] && y==dice[1])||(x==dice[1] && y==dice[0])||(x==dice[0] && y==dice[4])||(x==dice[4] && y==dice[5])){
            printf("%d\n",dice[3]);
        }else if((x==dice[0] && y==dice[3])||(x==dice[3] && y==dice[5])||(x==dice[5] && y==dice[2])||(x==dice[2] && y==dice[0])){
            printf("%d\n",dice[1]);
        }else if((x==dice[2] && y==dice[1])||(x==dice[1] && y==dice[3])||(x==dice[3] && y==dice[4])||(x==dice[4] && y==dice[2])){
            printf("%d\n",dice[5]);
        }else{
            printf("%d\n",dice[0]);
        }
    }
    return 0;
}

Measured memory_bytes: 1172
def main():
    """
    ball: N
    ball position: (X, Y) -> (x_i, i)
    robot A: N
    robot B: N
    A position: (X, Y) -> (0, i)
    B position: (X, Y) -> (K, i)

    A pattern: (0, a) if ball pos eq (xx, a)
    B pattern: (K, b) if ball pos eq (xx, b)
    """
    N = int(input())
    K = int(input())
    xs = map(int, input().split())

    mov_dist = 0
    for y, x in enumerate(xs, 1):
        a_dist = x
        b_dist = abs(K - x)
        dist = min(a_dist, b_dist)

        mov_dist += dist * 2

    print(mov_dist)

if __name__ == '__main__':
    main()


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

int main()
{
    ll r, D, x;
    cin >> r >> D >> x;

    for (int i = 0; i < 10; i++)
    {
        x = r * x - D;
        cout << x << "\n";
    }
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int) n; i++)
using ll = long long;
template <class T>
using vt = std::vector<T>;
using vvi = std::vector<vt<int>>;

int main(){
  int n;
  std::cin >> n;
  std::string s;
  std::cin >> s;

  ll r = 0, g = 0, b = 0;
  rep(i,n) {
    if(s[i] == 'R')
      r++;
    else if(s[i] == 'G')
      g++;
    else
      b++;
  }

  ll to = r*g*b;
  rep(j,n){
    rep(i,j){
      int k = 2*j - i;
      if(k < n){
        if(s[i] != s[j] && s[j] != s[k] && s[k] != s[i])
        to--;
      }
    }
  }

  std::cout << to << '\n';
  return 0;
}


Measured memory_bytes: 3604
inp = input().split()
k = int(inp[0])
n = int(inp[1])
a = input().split()
b = []
s = 0

for i in range(n):
    if i == n-1:
        b.append(k - int(a[i]) + int(a[0]))
    else:
        b.append(int(a[i+1]) - int(a[i]))
    s += b[i]

s = s - max(b)
print(s)

Measured memory_bytes: 20044
h,w = map(int,input().split())

grid = []

for i in range(h):
    grid.append(list(input()))

#print(grid)

#cnt = 0

dir = [[0,1],[-1,1],[-1,0],[-1,-1],[0,-1],[1,-1],[1,0],[1,1]]

for i in range(h):
    for j in range(w):
        if grid[i][j] == '.':
            bomb=0
            #cnt += 1
            for k in range(8):
                if 0 <= i + dir[k][0] <= h-1 and 0 <= j + dir[k][1] <=w-1:
                    if grid[i + dir[k][0]][j + dir[k][1]] == '#':
                        bomb += 1
            grid[i][j] = str(bomb)

#print(grid)
#print(cnt)

for i in range(h):
    result = ''.join(grid[i])
    print(result)

Measured memory_bytes: 3188
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <map>
#include <algorithm>

using namespace std;

int a[1234],b[12345]={0};
int main() {
    int x,n;
    cin>>x>>n;
    /*
    if (n==0) {
        cout<<x-1<<endl;
        return 0;
    }
    */
    map<int,int>b;
    for (int i=1;i<=n;i++) {
            cin>>a[i];
            b[a[i]]=1;
    }

    for (int i=0;i<=1000000;i++) {
        if (b[x-i]==0) {
            cout<<x-i;
            break;
        }
        if (b[x+i]==0) {
            cout<<x+i;
            break;
        }
    }
    return 0;
}


Measured memory_bytes: 3628
#include<iostream>
#include<string.h>
#include<string>
#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
typedef pair<int, int> pii;

int main()
{
  while(1){
    stack<int> sta;//[]:1 ():2 
    bool ans=true;
    string c;
    getline(cin,c);
    if(c[0]=='.')break;
    else if(c[0]=='[')sta.push(1);
    else if(c[0]=='(')sta.push(2);
    else if(c[0]==']' || c[0]==')')ans=false;
    int i=0;
    while(ans){
      i++;
      // printf("c='%c'\n",c[i]);
      if(c[i]=='[')sta.push(1);
      else if(c[i]=='(')sta.push(2);
      else if(c[i]==']')
	if(sta.size() && sta.top()==1)sta.pop();
	else{
	  ans=false;
	  break;
	}
      else if(c[i]==')')
	if(sta.size() && sta.top()==2)sta.pop();
	else{
	  ans=false;
	  break;
	}
      else if(c[i]=='.')break;
    }
    if(sta.size()==0 && ans)cout<<"yes"<<endl;
    else{
      cout<<"no"<<endl;
    }
  }


}

Measured memory_bytes: 3116
#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

//constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-9;

//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

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

	cin >> L >> R >> K;
	for (int i = L; i <= R; i++) {
		if (i%K == 0)M++;
	}
	cout << M << endl;
}

Measured memory_bytes: 3636
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int w = sc.nextInt();
		int[] u = new int[n];
		int[] v = new int[n];
		for (int i = 0; i < n; i++) {
			u[i] = sc.nextInt();
			v[i] = sc.nextInt();
		}
		sc.close();

		long[] dp = new long[w + 1];
		for (int i = 0; i < n; i++) {
			for (int j = w; j >= u[i]; j--) {
				dp[j] = Math.max(dp[j], dp[j - u[i]] + v[i]);
			}
		}

		long ans = 0;
		for (int j = 1; j <= w; j++) {
			ans = Math.max(ans, dp[j]);
		}
		System.out.println(ans);
	}
}


Measured memory_bytes: 22612
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
long long int arr[22][22],n,dp[(1<<21)+1][22];
long long int flag;
long long int solve(long long int mask,long long int idx)
{
   if(mask==flag)
   	return 1;
   if(idx>=n)
   	return 0;
   if(dp[mask][idx]!=-1)
   	return dp[mask][idx];
   long long int ans=0,i;
   for(i=0;i<n;i++)
   	if(arr[idx][i])
   	{
   		if(!(mask&(1<<i)))
   		{
   			ans+=solve(mask|(1<<i),idx+1);
   			ans%=mod;
   		}
   	}
   	dp[mask][idx]=ans;
   	return dp[mask][idx];
}
int main()
{
	long long int i,j,k,a,b;
	cin >> n;
	memset(dp,-1,sizeof(dp));
	flag=(1<<n)-1;
	//cout << flag << endl;
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
			cin >> arr[i][j];	
	cout << solve(0,0) << endl;	
	return 0;
}

Measured memory_bytes: 360832
import java.io.PrintWriter

fun main(args: Array<String>) {
    PrintWriter(System.out).use { out ->
        solve(out)
        out.flush()
    }
}

fun solve(out: PrintWriter) {
    val (k, n) = readIntegerList()
    val aList = readIntegerList()
    var max = aList[1] - aList[0]
    for (i in 0 until (n - 1)) {
        max = Math.max(max, aList[i + 1] - aList[i])
    }
    max = Math.max(max, k - aList.last() + aList[0])
    println(k - max)
}


fun readInteger() = readLine()!!.toInt()
fun readLong() = readLine()!!.toLong()
fun readStringList() = readLine()!!.split(" ")
fun readIntegerList() = readStringList().map(String::toInt)
fun readLongList() = readStringList().map(String::toLong)
fun readDoubleList() = readStringList().map(String::toDouble)


Measured memory_bytes: 82904
N = int(input())
As = list(map(int, input().split()))

if As[0] > 0:
    if N == 0 and As[0] == 1:
        print(1)
    else:
        print(-1)
    exit()

flg = True

branch = [1]
for depth in range(1, N+1):
    b = branch[-1] * 2 - As[depth]
    if b >= 0:
        branch.append(b)
    else:
        print(-1)
        exit()

s = 0
b = 0
for i in range(N, 0, -1):
    b += As[i]
    if b > branch[i-1] * 2:
        s += sum(branch[:i+1]) + sum(As[:i+1])
        break
    else:
        s += b
else:
    s += 1

print(s)

Measured memory_bytes: 668720
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <random>
#include <unordered_map>
#include <unordered_set>
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << #x << ':' << x << '\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }


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

	int l, r, d;
	cin >> l >> r >> d;
	cout << r / d - (l - 1) / d << '\n';
}

Measured memory_bytes: 3628
import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.close();
        boolean fu = false;
        for(int i = 1 ; i < 10 && fu == false ; i++ ){
            if(n % i == 0){
                int k = n / i;
                if(k < 10){
                    fu = true;
                }
            }
        }
        if(fu == true){
            System.out.println("Yes");
        }else{
            System.out.println("No");
        }
    }
}

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

int main(){
    int n;
    cin>>n;
    vector<int> v(n);
    for(int i=0;i<n;i++){
        cin>>v[i];
    }
    if(is_sorted(v.begin(),v.end())){
        cout<<"YES"<<endl;
        return 0;
    }
    for(int i=0;i<v.size();i++){
        for(int j=i+1;j<v.size();j++){
            swap(v[i],v[j]);
            if(is_sorted(v.begin(),v.end())){
                cout<<"YES"<<endl;
                return 0;
            }
            swap(v[i],v[j]);
        }
    }
    cout<<"NO"<<endl;
    return 0;
}

Measured memory_bytes: 256
import numpy as np
import math
import sys
sys.setrecursionlimit(10000000)

def main():
    N = int(input())
    P = [int(c) for c in input().split()]
    sorted_P = sorted(P)
    if sorted_P == P:
        print("YES")
        sys.exit()
    for i in range(N):
        for j in range(N):
            copied_P = P.copy()
            if i == j:
                continue
            tmp = copied_P[i]
            copied_P[i] = copied_P[j]
            copied_P[j] = tmp
            if sorted_P == copied_P:
                print("YES")
                sys.exit()
                break
            else:
                continue
    else:
        print("NO")
            


if __name__ == '__main__':
    main()

Measured memory_bytes: 21576
#include <stdio.h>

int main(void){
	int p[3],j[2];
	int i,n;
	int min=5000;
	for(i=0;i<3;i++){
		scanf("%d",&p[i]);
	}
	for(i=0;i<2;i++){
		scanf("%d",&j[i]);
	}
	for(i=0;i<3;i++){
		for(n=0;n<2;n++){
			if(min>p[i]+j[n]) min=p[i]+j[n];
			else ;
		}
	}
	printf("%d\n",min-50);
	return 0;
}

Measured memory_bytes: 592
#include<iostream>
using namespace std;
const int BUF = 200005;


int nVal;
int val[BUF];

void read() {
    cin >> nVal;
    for (int i = 0; i < nVal; ++i) {
        cin >> val[i];
    }
}


void work() {
    int bit2cnt[31] = {};

    for (int i = 0; i < nVal; ++i) {
        for (int bit = 0; bit < 31; ++bit) {
            if (val[i] & (1 << bit)) {
                ++bit2cnt[bit];
            }
        }
    }

    int ans[BUF] = {};
    for (int i = 0; i < nVal; ++i) {
        for (int bit = 0; bit < 31; ++bit) {
            int isOn = (val[i] & (1 << bit)) > 0;
            if (bit2cnt[bit] % 2 == 0) {
                ans[i] |= isOn << bit;
            } else {
                ans[i] |= (!isOn) << bit;
            }
        }
    }

    for (int i = 0; i < nVal; ++i) {
        if (i) cout << ' ';
        cout << ans[i];
    }
    cout << endl;
}


int main() {
    read();
    work();
    return 0;
}


Measured memory_bytes: 5088
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <functional>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <list>
#include <bitset>
#include <fstream>

using namespace std;
#define REP(i,a,n) for(int i = (a); i < (int)(n); ++i)
#define REM(i,a,n) for(int i = ((n) - 1); i >= (a); --i)
#define EPS 1e-11
#define INF 0x3FFFFFFF
#define INFLL 0x3FFFFFFF3FFFFFFF
#define INFD 1.0e+308
#define FLOAT setprecision(16)
const double PI = 3.1415926535897932;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> PP;

template <class T, class U>
istream& operator >> (istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; }
template <class T, class U>
ostream& operator<<(ostream& ost, pair<T, U>& right) { return ost << right.first << ' ' << right.second; }
template <class T, class TCompatible, size_t N>
void Fill(T(&dest)[N], const TCompatible& val) { fill(begin(dest), end(dest), val); }
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); }
//sum, 累積和
// 負のときの割り算に注意
#if 1
#include <array>
#include <random>
#include <unordered_set>
#include <unordered_map>
template<class T>
using PriorityQ = priority_queue<T, vector<T>, greater<T> >;	// コスト小を優先
#endif

#ifdef ONLY_MY_ENVIR
#include "IntMod.h"
#include "Union_Find.h"
#include "Graph.h"
#include "Suffix_Array.h"
#include "Matrix.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream& operator >> (istream& ist, __int128& val) { LL tmp;  ist >> tmp; val = tmp; return ist; }
ostream& operator<< (ostream& ost, __int128 val) { LL tmp = val; ost << tmp; return ost; }
#endif

int A[100000];
bool solve() {
	int N;
	cin >> N;

	int mn = INF;
	int mx = 0;
	REP(i, 0, N) {
		cin >> A[i];
		mn = min(mn, A[i]);
		mx = max(mx, A[i]);
	}
	if (mx - mn >= 2) return false;
	if (mx == mn) {
		if (mn == N - 1) return true;
		if (mn * 2 <= N) return true;
		return false;
	}


	int mncnt = 0;
	int mxcnt = 0;
	REP(i, 0, N) {
		if (A[i] == mn) {
			++mncnt;
		} else {
			++mxcnt;
		}
	}

	return mncnt + (mx - mncnt) * 2 <= N && (mx - mncnt) > 0;
}


int main() {
	cout << (solve() ? "Yes" : "No") << endl;
	return 0;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
 
using namespace std;
 
#define BOOST        std::ios::sync_with_stdio(false);  cin.tie(NULL); cout.tie(NULL);
#define sp           <<" "<<
#define ll           long long
#define ld           long double
#define AZI(i,x)     for(auto i=x.begin();i!=x.end();i++)
#define AZ(x)        x.begin(),x.end()
#define P(x,y)       pair < x,y >
#define V(x)         vector < x >
#define fi           first
#define se           second
#define pb           push_back
#define mp           make_pair
#define PI           3.14159265358979323846264338327950288419716939937510
 
const ll mod=1e9+7,N=1e5+5,INF = 1e18;

ll power(ll a,ll b)
{
    a = a%mod;
    ll result = 1;

    while(b)
    {
        if(b&1)
            result = (result*a)%mod;
        b >>= 1, a = (a*a)%mod;
    }
    return result;
} 

void dectobin(int x,bool a[],int pos)
{
    while(x)
    {
        a[pos] = x%2;
        x = x/2;
        pos--;
    }
    return;
}

int main() 
{ 
    BOOST

    int n,m;
    cin>>n>>m;

    V(int) v[m];

    for(int i=0;i<m;i++)
    {
        int k;
        cin>>k;
        for(int j=0;j<k;j++)
        {
            int x;
            cin>>x;
            v[i].pb(x);
        }
    }    

    int p[m];
    for(auto &i : p)
        cin>>i;

    int last = (int)power(2,n) - 1;
    bool arr[n]={};
    int ans = 0;
    for(int i=0;i<=last;i++)
    {
        dectobin(i,arr,n-1);
        int ct = 0 ;
        for(int i=0;i<m;i++)
        {
            int  k = v[i].size();
            int num_sw = 0;
            for(int j=0;j<k;j++)
            {
                if(arr[v[i][j]-1])
                    num_sw++;
            }
            if(num_sw%2 == p[i]%2)
                ct++;
        }
        if(ct==m)
            ans++;
    }
    cout<<ans<<"\n";
    return 0;    
} 

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

bool correct(vector<bool> & honesty,
	     vector<vector<bool>> & has_testimony,
	     vector<vector<bool>> & honest_testimony){
  int N = honesty.size();
  for(int i = 0; i < N; i++){
    for(int j = 0; j < N; j++){
      if(has_testimony[j][i] &&
	 honesty[j] &&
	 (honesty[i] != honest_testimony[j][i])){
	return false;
      }
    }
  }
  return true;
}

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

  vector<vector<bool>> has_testimony(N, vector<bool>(N, false));
  vector<vector<bool>> honest_testimony(N, vector<bool>(N, false));
  for(int i = 0; i < N; i++){
    int A;
    cin >> A;
    for(int j = 0; j < A; j++){
      int x, y;
      cin >> x >> y;
      x--;
      has_testimony[i][x] = true;
      honest_testimony[i][x] = y;
    }
  }

  int ans = 0;
  for(int i = 0; i < (1 << N); i++){
    int honest_num = 0;
    vector<bool> honesty(N, false);
    for(int j = 0; j < N; j++){
      if((i >> j) & 1){
	honesty[j] = true;
	honest_num++;
      }
    }
    /*
      for(int j = 0; j < N; j++){
      cout << honesty[j] << ' ' ;
      }cout << endl;
    */
    if(correct(honesty, has_testimony, honest_testimony)){
      ans = max(ans, honest_num);
    }
  }
  cout << ans << endl;
}

  


Measured memory_bytes: 256
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int INF = 1LL << 30;
int MOD = 1e9+7;
main(){
    ll A,B,C,K;
    cin >> A >> B >> C >> K;
    if(K & 1)cout << B-A << endl;
    else    cout << A-B << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pint;

string tb[50];
string ans[50];
int main(){
    int h,w;
    cin>>h>>w;
    rep(i,h) cin>>tb[i];
    rep(i,h){
        stringstream ss;
        rep(j,w){
        if(tb[i][j]=='#'){
            ss<<'#';
            continue;
        }
        int cnt=0;
        for(int k=-1;k<=1;++k){
            for(int l=-1;l<=1;++l){ 
                if(i+k>=0&&j+l>=0&&i+k<h&&j+l<w&&tb[i+k][j+l]=='#') ++cnt;      
            }
        }
        ss<<cnt;
        }
        ans[i]=ss.str();
    }
    rep(i,h) cout<<ans[i]<<endl;
    return 0;
}

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

int main() {
  int N, M;
  int road;
  
  cin >> N >> M;
  
  vector<int> a(M);
  vector<int> b(M);
  
  for (int i = 0; i < M; i++) {
    cin >> a.at(i) >> b.at(i);
  }
  
  for (int i = 0; i < N; i++) {
    road = 0;
    for (int j = 0; j < M; j++) {
      if (a.at(j) == i + 1) {
        road++;
      }
      if (b.at(j) == i + 1) {
        road++;
      }
    }
    cout << road << endl;
  }
}

Measured memory_bytes: 3532
n = int(input())
w = [input() for i in range(n)]
lists = []
tmp = ''

for i in range(n):
    if i != 0:
        if tmp != w[i][0]:
            print('No')
            exit()
    if w[i] in lists:
        print('No')
        exit()
    lists.append(w[i])
    tmp = w[i][-1]            

print('Yes')


Measured memory_bytes: 3060
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	static int n;
	public static void main(String[] args) throws IOException {

		// input
		Scanner in = new Scanner(System.in);
		n = in.nextInt();
		int h = in.nextInt();
		int[] a = new int[n];
		int[] b = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = in.nextInt();
			b[i] = in.nextInt();
		}

		// solve
		int count = 0;
		Arrays.sort(a);
		Arrays.sort(b);
		int aMax = a[n-1];
		int iterator = upperBound(b, aMax-1);

		for (int i = n-1; i >= iterator; i--) {
			if (h > 0) {
				h -= b[i];
				count++;
			}
		}

		while (h > 0) {
			h -= aMax;
			count++;
		}
		System.out.println(count);

	}
	/**
	 * 指定された要素より大きい値が現れる最初の位置のイテレータを取得する
	 * */
	public static int upperBound(int[] a, int obj) {
		int l = 0,r = a.length - 1;
		while (r - l >= 0) {
			int c = (l + r) / 2;
			if (a[c] <= obj) {
				l = c + 1;
			} else {
				r = c - 1;
			}
		}
		return l;
	}

}


Measured memory_bytes: 83688
/**
 *      purpose : 
 *      author  : kyomukyomupurin
 *      created : 
**/

// input/output
#include <iostream>
#include <fstream>
#include <sstream>
// container class
#include <array>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// math, algorithm
#include <algorithm>
#include <cmath>
#include <complex>
#include <numeric>
// etc
#include <bitset>
#include <cassert>
#include <cctype>
#include <cstring>
#include <chrono>
#include <iomanip>
#include <random>
#include <utility>
// using-directive
using namespace std;
// alias template
using int64 = long long;
using vi = vector<int>;
using vl = vector<int64>;
using pii = pair<int, int>;
using pll = pair<int64, int64>;
// text macro replacement
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define print(x) cout << (x) << '\n'
#define debug(x) cerr << #x << ": " << (x) << '\n'
#define dbg(v) for (size_t _ = 0; _ < v.size(); ++_){cerr << #v << "[" << _ << "] : " << v[_] << '\n';}
// variadic template
template<typename T> inline void chmin(T &a, T b) {if (a > b) a = b; return;}
template<typename T> inline void chmax(T &a, T b) {if (a < b) a = b; return;}
// constant
const int INF = (1<<30) - 1;
const int64 INF64 = (1LL<<62) - 1;
const int MOD = 1000000007;

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

  int64 x; cin >> x;
  bitset<32> bit1(x), bit2(x), bit3(x), bit4(x);
  cout << bit1 << '\n';
  bit2.flip();
  cout << bit2 << '\n';
  bit3 <<= 1; bit4 >>= 1;
  cout << bit3 << '\n';
  cout << bit4 << '\n';

  return 0;
}


Measured memory_bytes: 3220
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
using ll = long long;

struct Graph {
    struct Vertex { int n; };
    struct Edge { int i, n, k; };
    Graph(int n, int m) : v(n, { -1 }), e(m), n(n), m(0) {}
    void add_edge(int i, int j, int k) {
        e[m] = { j, v[i].n, k };
        v[i].n = m;
        m++;
    }
    bool dfs(int i, int p) {
        if (i == g) return true;
        for (int j = v[i].n; j >= 0; j = e[j].n) {
            Edge &o = e[j];
            if (o.i == p) continue;
            if (dfs(o.i, i)) {
                t ^= 1LL << o.k;
                return true;
            }
        }
        return false;
    }
    ll foo(int u, int v) {
        t = 0; g = v;
        dfs(u, -1);
        return t;
    }
    vector<Vertex> v;
    vector<Edge> e;
    int n, m;
    ll t;
    int g;
};

using ull = unsigned long long;
constexpr ull f(int i) {
    return -1 / (1 + (1ULL << (1 << i)));
}
int popcnt(ull t) {
    t -= t >> 1 & f(0);
    t = (t & f(1)) + (t >> 2 & f(1));
    t = (t + (t >> 4)) & f(2);
    t = t * ((ull)-1 / 0xff) >> (64 - 8);
    return t;
}

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

    Graph g(n, (n - 1) * 2);
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;

        g.add_edge(a, b, i);
        g.add_edge(b, a, i);
    }

    int m;
    cin >> m;

    vector<ll> y(1 << m);
    vector<char> z(1 << m);
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--;

        y[1 << i] = g.foo(u, v);
    }
    y[0] = 0;
    z[0] = -1;

    ll r = 0;
    for (int k = 1; k < 1 << m; k++) {
        int o = k & -k;
        y[k] = y[k ^ o] | y[o];
        z[k] = -z[k ^ o];

        int c = (n - 1) - popcnt(y[k]);
        r += (ll)z[k] << c;
    }

    r = (1LL << (n - 1)) - r;
    cout << r << endl;

    return 0;
}

Measured memory_bytes: 9472
n,a,b,c=map(int,input().split())
l=[]
for _ in range(n):
  d=int(input())
  l.append(d)

def dfs(cur,x,y,z):
  if cur==n and min(x,y,z)>0:
    return abs(a-x)+abs(b-y)+abs(c-z)-30
  elif cur==n:
    return 100000000000000000000
  ret0=dfs(cur+1,x,y,z)
  ret1=dfs(cur+1,x+l[cur],y,z)+10
  ret2=dfs(cur+1,x,y+l[cur],z)+10
  ret3=dfs(cur+1,x,y,z+l[cur])+10
  return min(ret0,ret1,ret2,ret3)

print(dfs(0,0,0,0))

Measured memory_bytes: 3064
#include<cstdio>
#include<iostream>
#include<vector>
#include<queue>
#include<bitset>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<cstring>
#include<complex>
#include<cmath>
#include<algorithm>
#include<list>
#include<functional>
#define _USE_MATH_DEFINES
#define X first
#define Y second
#define INF 1987654321
#define PI 3.14159265358979323846264
#define MOD 1000000007
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset(a,0,sizeof(a));
#define MEM_1(a) memset(a,-1,sizeof(a));
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<double, double> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<double> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef complex<double> base;
ll POW(ll a, ll b) { if (b == 0)return 1; if (b == 1)return a; if (b & 1)return (a*POW(a, b - 1)) % MOD; ll t = POW(a, b / 2); return (t*t) % MOD; }
struct num {
	int x, i;
	bool operator <(const num& a)
	{
		return x < a.x;
	}
}a[100000];
int main() {
	int n;
	scanf("%d", &n);
	fup(i, 0, n - 1, 1)
	{
		scanf("%d", &a[i].x);
		a[i].i = i;
	}
	sort(a, a + n);
	int ans = 0;
	fup(i, 0, n - 1, 1) if ((i^a[i].i) & 1)ans++;
	printf("%d", ans / 2);
}

Measured memory_bytes: 1024
#include <iostream>
using namespace std;
int main(void)
{
	int a=0,b,max=0,ans=0;
	cin >> max;
	while(max!=0){
	for(int i=0;i<9;i++){
		cin >> b;
		a+=b;
	}
	ans=max-a;
	cout << ans << endl;
	a=0;
	cin >> max;
	}
	
	return 0;
}

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

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

	bool first, second;



	if (stoi(str.substr(0, 2)) > 12 || stoi(str.substr(0, 2)) == 0) {
		first = false;
	}
	else {
		first = true;
	}

	if (stoi(str.substr(2, 2)) > 12 || stoi(str.substr(2, 2)) == 0) {
		second = false;
	}
	else {
		second = true;
	}


	if (first && second) {
		cout << "AMBIGUOUS" << endl;
	}
	else if (first == true && second == false) {
		cout << "MMYY" << endl;
	}
	else if (first == false && second == true) {
		cout << "YYMM" << endl;
	}
	else {
		cout << "NA" << endl;
	}
}

Measured memory_bytes: 256
# -*- coding:utf-8 -*-
import sys
def dfs(x, y, sym):
	tile[x][y] = "."
	for i in range(len(DX)):
		nx = x + DX[i]
		ny = y + DY[i]
		if (0<=nx and nx<N and 0<=ny and ny<M and tile[nx][ny] is sym):
			dfs(nx, ny, sym)

def solve():
	res = 0
	for i in range(N):
		for j in range(M):
			sym = tile[i][j]
			if tile[i][j] != ".":
				if tile[i][j] is sym:
					dfs(i, j, sym)
					res += 1
	print(res)

counter=0
DX = [-1,0,1,0]
DY = [0,-1,0,1]
sys.setrecursionlimit(10**7)
while True:
	line = input()
	if " " in line:
		nums = line.split(" ")
		N = int(nums[0])
		M = int(nums[1])
		tile = [[0 for i in range(M)] for j in range(N)]
	else:
		tile[counter-1] = list(line)
	if N is 0 and M is 0:
		break
	counter += 1
	if counter-1 is N:
		solve()
		counter = 0

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

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,s,n) for(long long i=s;i<n;i++)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<vll> vvll;
const double pi = acos(-1.0);
const ll mod = pow(10,9) + 7;
const ll INF = pow(2,31) - 1;

int main()
{
    ll a,b,ans;
    cin >> a >> b;
    if(a+b == 3)ans = 3;
    else if(a+b == 4)ans = 2;
    else if(a+b == 5)ans = 1;

    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
/*
  -------------
  Prateek Celly
  DA-IICT
  -------------
*/
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define MOD 1000000007

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll a,b,ans=1e10;
  cin>>a>>b;
  if(b-a>=2019) cout<<0;
  else{
    for(ll i=a;i<=b-1;i++){
      for(ll j=i+1;j<=b;j++) ans=min(ans,(i*j)%2019);
    }
    cout<<ans;
  }
  return 0;
}


Measured memory_bytes: 256
def d(r):
    global n
    if t[r]:return
    n+=1
    t[r]=[r+1,n]
    for i in c[r]:d(i-1)
    n+=1
    t[r][2:]=[n]
m=input()
c=[0]*m
for _ in range(m):
    l=map(int,raw_input().split())
    c[l[0]-1]=l[2:]
n=0
t=[0]*m
for i in range(m):
    d(i)
    print " ".join(map(str,t[i]))

Measured memory_bytes: 4268
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
//template<class itr> void cins(itr first, itr last){ for(auto i = first; i != last; i++){ cin >> (*i); } }
/*
string S_str[H];
cins(S_str, S_str + H);
S_str[i][j];
*/
inline int at(ll x, int k){ return (x >> k) & 1; }
#define rep(i, n) for(int i = 0; i < n; i++)

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

int main(){
  ll k;
  cin >>k;
  ll sum =0;
  rep(i,k){
    rep(j,k){
      rep(m,k){
        sum += gcd(gcd(i+1, j+1),m+1);
      }
    }
  }
  cout << sum<< endl;

}


Measured memory_bytes: 3596
import numpy as np
h,w,m=map(int, input().split())
hw=[tuple(map(int,input().split())) for i in range(m)]
H=np.zeros(h)
W=np.zeros(w)
for i in range(m):
    hi,wi=hw[i]
    H[hi-1]+=1
    W[wi-1]+=1
mh=max(H)
mw=max(W)
hmax=[i for i, x in enumerate(H) if x == mh]
wmax=[i for i, x in enumerate(W) if x == mw]
f=0
for i in range(m):
    hi,wi=hw[i]
    if H[hi-1]==mh and W[wi-1]==mw:
        f+=1
if len(hmax)*len(wmax)-f<1:
    print(int(mh+mw-1))
else:
    print(int(mh+mw))

Measured memory_bytes: 95596