akhauriyash/Code-Regression huggingface dataset:
n=io.read("*n","*l")
s=io.read()

t={}
for i=1,n do
    alphabet=s:sub(i,i)
    if not t[alphabet] then
        t[alphabet]={}
    end
    table.insert(t[alphabet],i)
end

max=0
for i=1,n do
    counter=0
    for alphabet,_ in pairs(t) do
        if #t[alphabet]>1 then
            if t[alphabet][1]<=i and i<=t[alphabet][#t[alphabet]] then
                counter=counter+1
            end
        end
    end
    max=math.max(counter,max)
end
print(max)

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <numeric>
#include <functional>
#include <cmath>
#include <array>

#define rep2(x,fr,to) for(int (x)=(fr);(x)<(to);(x)++)
#define rep(x,to) for(int (x)=0;(x)<(to);(x)++)
#define repr(x,fr,to) for(int (x)=(fr);(x)>=(to);(x)--)
#define all(c) (c).begin(),(c).end()
#define sz(v) (int)(v).size()

using namespace std;
typedef long long ll; typedef vector<int> VI;
typedef pair<int,int> pii;
const int mod = (int)1e9+7;


int main()
{
	
	cin.tie(0); ios_base::sync_with_stdio(false);
	int n; cin >>n;
	VI a(n);
	rep(i, n) cin >> a[i];
	map<int,int> mp;
	rep(i,n) mp[a[i]]=0;
	int c=0;
	for(auto& x: mp) x.second = c++;
	rep(i,n) a[i] = mp[a[i]];
	//for(auto x :a) printf("%d, ",x); puts("");
	ll sm=0;
	rep(i,n){
		if(abs(a[i] - i)%2) sm++;
	}
	
	cout << sm/2 <<endl;
	return 0;
}


Measured memory_bytes: 5504
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define endl '\n'
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef long long ll;
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, -1, 0, 1};
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    // 曲がった回数ダイクストラ
    int h, w, k;
    cin >> h >> w >> k;
    int sx, sy, gx, gy;
    cin >> sy >> sx >> gy >> gx;
    vector<string> s(h);
    rep(i, h)
    {
        cin >> s[i];
    }
    vector<vector<int>> dp;
    rep(i, h)
    {
        vector<int> tmp(w, 1e9);
        dp.push_back(tmp);
    }
    dp[sy - 1][sx - 1] = 0;
    priority_queue<PP, vector<PP>, greater<PP>> que;
    que.push(PP(0, P(sx - 1, sy - 1)));
    while (!que.empty())
    {
        PP pp = que.top();
        P p = pp.second;
        int d = pp.first;
        que.pop();
        for (int i = 0; i < 4; i++)
        {
            for (int j = 1; j <= k; j++)
            {
                int tx = p.first + dx[i] * j;
                int ty = p.second + dy[i] * j;
                if (tx < 0 || tx >= w)
                {
                    break;
                }
                if (ty < 0 || ty >= h)
                {
                    break;
                }
                if (dp[ty][tx] < d + 1)
                {
                    break;
                }
                if (s[ty][tx] == '@')
                {
                    break;
                }
                if (dp[ty][tx] > d + 1)
                {
                    que.push(PP(d + 1, P(tx, ty)));
                    dp[ty][tx] = d + 1;
                }
            }
        }
    }
    if (dp[gy - 1][gx - 1] == 1e9)
    {
        cout << -1 << endl;
    }
    else
    {
        cout << dp[gy - 1][gx - 1] << endl;
    }
}

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

#define ll          long long
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<ll>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(int i=a;i<b;i++)

using namespace std;

void solve()
{
	
	ll n;
	cin>>n;
	vi v(n);
	for(ll i=0;i<n;i++)
	{
		cin>>v[i];
	}
	if(n<=1)
	{
		cout<<0<<endl;
		return;
	}
	ll h=0;
	
	ll c=0;
	for(ll i=0;i<n;i++)
	{
		if(v[i]<h)
		{
			c=c+h-v[i];
		}
		else if(v[i]>=h)
		{
			h=max(v[i],h);
		}
	}
	cout<<c<<endl;

    
}
int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        
        int t;
        //cin>>t;
        t=1;
        while(t--)
		{
        
     		solve();
     		
	    }
        
    }

Measured memory_bytes: 4740
import sys
sys.setrecursionlimit(10 ** 5)
lst = [[1,2], [2, 5], [3, 5], [4, 4], [5, 5], [6, 6], [7, 3], [8, 7], [9, 6]]
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse = True)
# print (A)

def num(k):
    return lst[k-1][1]

# for i in A:
    # print (num(i), end = ', ')
# print ()

INF = 10 ** 5
dp_lst = [-INF] * (N + 1)
dp_lst[0] = 0
def dp(i):
    if dp_lst[i] != -INF:
        return dp_lst[i]
    tmp = -INF
    for j in A:
        if i >= num(j):
            tmp = max(tmp, dp(i - num(j)) + 1)
    dp_lst[i] = tmp
    return dp_lst[i]

a = dp(N)
# print (a)
# print (dp_lst)   
# print ([0] * 10 +[i+1 for i in range(20)])

# print (len(str(71111111111111111111111111111111111111111111111111)))

def check(A):
    if N - num(A) < 0:
        return False
    if dp_lst[N-num(A)] == dp_lst[N] - 1:
        return True
    else:
        return False

# print (a)
while a > 0:
    for i in A:
        if check(i):
            print (i, end = '')
            N -= num(i)
            a -= 1
            break
        else:
            pass
            # print ('B')
print ()

Measured memory_bytes: 5364
n = int(input())
ab = [list(map(int, input().split())) for _ in range(n - 1)]
mod = 10 ** 9 + 7

adj = [[] for _ in range(n)]
for a, b in ab:
    a -= 1
    b -= 1
    adj[a].append(b)
    adj[b].append(a)


def dfs(s):
    d = [-1] * n
    p = [-1] * n
    c = [[] for _ in range(n)]

    d[s] = 0
    stack = [s]

    while stack:
        u = stack.pop()
        for v in adj[u]:
            if d[v] == -1:
                d[v] = d[u] + 1
                p[v] = u
                c[u].append(v)
                stack.append(v)

    return d, p, c


d, p, c = dfs(0)

di = [[e, i] for i, e in enumerate(d)]
di.sort(reverse=True)

dp = [[1] * 2 for _ in range(n)]

for _, i in di:
    if c[i]:
        for ec in c[i]:
            dp[i][0] *= dp[ec][1]
            dp[i][0] %= mod

            dp[i][1] *= dp[ec][0] + dp[ec][1]
            dp[i][1] %= mod

ans = dp[0][0] + dp[0][1]
ans %= mod
print(ans)


Measured memory_bytes: 87468
#include<iostream>
#include<vector>
#include<algorithm>//sort,二分探査など

//QCFium法
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

//浮動小数点処理高速化
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
using i64 = int64_t;
using f80 = __float80;
using u64 = uint_fast64_t;

constexpr i64 INF = 1010000000000000017LL;
constexpr u64 MOD = 1000000007LL;
constexpr f80 PI = 3.14159265358979323846;

#define _overload3(_1,_2,_3,name,...) name
#define repi(i,start,end) for(int i=(int)(start);i<(int)(end);++i)
#define _rep(i,n) repi(i,0,n)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define rrep(i,start,end) for(int i=(int)(start);i>(int)(end);--i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()

#define v(T) vector<T>
#define vv(T) v(v(T))
#define vvv(T) v(v(v(T)))
typedef v(int) vi;

#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >

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

template<typename T> ostream &operator<<(ostream &os, const vector< T > &v){
  rep(i,v.size()) os << v[i] << (i + 1 != v.size() ? " " : "");
  return os;
}
template<typename T> istream &operator>>(istream &is, vector< T > &v){
  for(T &in : v) is >> in;
  return is;
}

i64 pow_mod(i64 n, i64 k, i64 m){
  i64 r = 1;
  for(;k;k>>=1){
    if(k&1) r=(r*n)%m;
    n=(n*n)%m;
  }
  return r;
}

v(int) popcount(200001, -1);

int calc_popcount(int x) {
  if(popcount[x] != -1) return popcount[x];
  int tmp = x;
  int count_one = 0;
  while(tmp != 0) {
    if((tmp & 1) == 1) count_one++;
    tmp >>= 1;
  }
  if(count_one == 0) {
    popcount[x] = 0;
    return popcount[x];
  }
  int nxt_x = x % count_one;
  popcount[x] = calc_popcount(nxt_x) + 1;
  return popcount[x];
}

void Main()
{

  int N;
  cin >> N;
  string X;
  cin >> X;

  int count_one = 0;
  rep(i, N) {
    if(X[i]=='1') count_one++;
  }

  if(count_one == 0) {
    rep(i, N) {
      cout << 1 << "\n";
    }
    return;
  }

  int A_up = 0;
  int A_down = 0;
  rep(i, N) {
    A_up *= 2;
    A_down *= 2;
    if(X[i]=='1') {
      A_up += 1;
      A_down += 1;
    }
    A_up %= (count_one + 1);
    if(count_one != 1) A_down %= (count_one - 1);
  }

  rep(i, N) {
    int X_nxt;
    if(X[i]=='0') {
      X_nxt = A_up + pow_mod(2,N-1-i,count_one + 1);
      X_nxt %= count_one + 1;
    } else {
      if(count_one == 1) {
        cout << 0 << "\n";
        continue;
      }
      X_nxt = A_down + count_one - 1 - pow_mod(2,N-1-i,count_one - 1);
      X_nxt %= count_one - 1;
    }
    cout << calc_popcount(X_nxt) + 1 << "\n";
  }
}

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

Measured memory_bytes: 4396
#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)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using mii = map<int, int>;
using msi = map<string, int>;
using pii = pair<int, int>;

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

    int a, b, c;
    cin >> a >> b >> c;
    bool f = false;
    if(a == b && a != c) {
        f = true;
    } else if(a != b && a == c) {
        f = true;
    } else if(a != b && b == c) {
        f = true;
    }
    if(f) cout << "Yes" << endl;
    else cout << "No" << endl;

    return 0;
}

Measured memory_bytes: 256
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

using ll = long long;
using ld = long double;

const ld pi = 3.141592653589793;

#ifdef EBUG
#define debug(x) cout << "\033[31m" << #x << ": " << x << "\033[0m\n";
#else
#define debug(x)
#endif

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

ll a, b, ans;

int main() {
  cin >> a >> b;
  ans += max(a, b);
  if (a >= b) {
    a--;
  } else {
    b--;
  }
  ans += max(a, b);
  cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 998244353
using namespace std;
using ll = long long;

int main() {
    int N, S; cin >> N >> S;
    vector<int> A(N);
    REP(i, 0, N) cin >> A[i];

    ll dp[N + 1][S + 1] = {};
    REP(i, 0, N) {
        dp[i][0] = (dp[i][0] + 1) % MOD;
        REP(j, 0, S + 1) {
            dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % MOD;
            if (j + A[i] < S + 1) dp[i + 1][j + A[i]] = (dp[i + 1][j + A[i]] + dp[i][j]) % MOD;
        }
    }

    ll ans = 0;
    REP(i, 1, N + 1) ans = (ans + dp[i][S]) % MOD;
    cout << ans << endl; 
    return 0;
}

Measured memory_bytes: 70656
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#define _USE_MATH_DEFINENS
#include <cmath>
using namespace std;

#define p(x) cout << x << endl;
#define el cout << endl;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

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

    int n;
    cin >> n;
    int a[n];

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

    int count = 0;
    bool flag = true;
    int i = 0;

    while(flag){
        if(a[i] == 2) flag = false;
        i = a[i] - 1;
        count++;
        if(count >= n){
            count = -1;
            flag = false;
        }
    }

    cout << count << endl;
}


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

int main() {
    long long int h,n;cin>>h>>n;
    long long int tmp,ans=0;
    
    for(long long int i=0;i<n;++i){
        cin>>tmp;
        ans+=tmp;
    }
    
    if(ans>=h)cout<<"Yes"<<endl;
    else cout<<"No"<<endl;

	return 0;
}

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

int main(){
    int N,D;
    cin >> N >> D;
    vector<vector<int>> X(N,vector<int>(D));
    for(int i = 0;i < N;i++){
        for(int j = 0;j < D;j++){
            cin >> X[i][j];
        }
    }
    int ans = 0;
    for(int i = 0;i < N-1;i++){
        for(int j = i+1;j < N;j++){
            int dd = 0;
            for(int k = 0;k < D;k++){
                dd += pow(X[i][k]-X[j][k], 2.0);
            }
            bool flag = false;
            for(int l = 0;l <= dd;l++){
                if(l * l == dd){
                    flag = true;
                }
            }
            if(flag) ans++;
        }
    }
    cout << ans << endl;
}

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

int main(){
	char sign[5];
	int sum = 0;
	for(int i=0;i<4;i++){
		cin>>sign[i];
		if(sign[i]=='+'){
			sum += 1;
		}	else if(sign[i]=='-'){
			sum -= 1;
		}
	}
	cout<<sum;
	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <ios>
#include <iomanip>
#include <vector>
#include <algorithm>
#define MAX 100000
using namespace std;
typedef long long ll;
int main()
{
	int N, M;
  	int P[MAX], Y[MAX];
  	vector<int> yd[MAX + 1];
  	cin >> N >> M;
  	for (int i = 0; i < M; i++){
    	cin >> P[i] >> Y[i];
      	yd[P[i]].push_back(Y[i]);
    }
  	for (int i = 0; i < N; i++){
    	sort(yd[i + 1].begin(), yd[i + 1].end());
    }
  	for (int i = 0; i < M; i++){
		ll id;
      	id = ll(P[i]) * 1000000;
      	id += int(lower_bound(yd[P[i]].begin(), yd[P[i]].end(), Y[i]) - yd[P[i]].begin()) + 1;
      	cout << setfill('0') <<right << setw(12) << id << endl;
    }
}

Measured memory_bytes: 7808
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;

//INSERT ABOVE HERE
signed main(){
  int n,q;
  cin>>n>>q;
  struct Point{
    int a,b,c,d;
    Point(int a,int b,int c,int d):
      a(a),b(b),c(c),d(d){}
  };

  auto cmpX=[&](Point p,Point q){return p.a<q.a;};
  auto cmpY=[&](Point p,Point q){return p.c<q.c;};

  const int BS1 = 512;
  const int BS2 = 32;
  assert(BS1 % BS2 == 0);

  vector<Point> ps;
  for(int i=0;i<n;i++){
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    ps.emplace_back(a,b,c,d);
  }

  while(ps.size()%BS1)
    ps.emplace_back(0,0,0,0);

  n=ps.size();
  sort(ps.begin(),ps.end(),cmpX);

  using P = pair<int, int>;
  vector< vector<P> > qs(n/BS2);

  vector<int> ma(n,1e6),Ma(n,0);
  vector<int> mc(n,1e6),Mc(n,0);
  for(int i=0;i<n;i+=BS1){

    for(int j=0;j<BS1;j++){
      chmin(ma[i],ps[i+j].a);
      chmax(Ma[i],ps[i+j].a);
    }

    sort(ps.begin()+i,ps.begin()+i+BS1,cmpY);
    for(int j=0;j<BS1;j+=BS2){
      auto &vp=qs[(i+j)/BS2];
      for(int k=0;k<BS2;k++){
        int z=i+j+k;
        vp.emplace_back(ps[z].b,ps[z].d);
        chmin(mc[i],ps[z].c);
        chmax(Mc[i],ps[z].c);
      }

      vp.emplace_back(-1,1e6);
      vp.emplace_back(1e6,-1);
      sort(vp.begin(),vp.end());
      vector<P> vq;
      for(auto p:vp)
        if(vq.empty()||vq.back().second>p.second)
          vq.emplace_back(p);
      vp=vq;
    }
  }


  for(int t=0;t<q;t++){
    int x,y,z,w;
    cin>>x>>y>>z>>w;

    auto check=
      [&](int k){
        return x<ps[k].a && ps[k].a<y && y<ps[k].b && z<ps[k].c && ps[k].c<w && w<ps[k].d;
      };

    int flg=0;
    for(int i=0;i<n;i+=BS1){
      if(flg) break;
      if(Ma[i]<=x) continue;
      if(y<=ma[i]) continue;

      if(x<ma[i]&&Ma[i]<y){
        for(int j=0;j<BS1;j+=BS2){
          if(Mc[i]<=z) continue;
          if(w<=mc[i]) continue;
          if(z<mc[i]&&Mc[i]<w){
            auto &vp=qs[(i+j)/BS2];
            flg|=w<lower_bound(vp.begin(),vp.end(),P(y,1e6))->second;
            continue;
          }
          for(int k=0;k<BS2;k++) flg|=check(i+j+k);
        }
        continue;
      }
      for(int j=0;j<BS1;j++) flg|=check(i+j);
    }

    const int DEBUG = 0;
    if(DEBUG){
      int flg2=0;
      for(int i=0;i<n;i++) flg2|=check(i);
      if(flg!=flg2) cerr<<flg<<" "<<flg2<<endl;
      assert(flg == flg2);
    }
    cout<<(flg?"Yes":"No")<<"\n";
  }
  cout<<flush;
  return 0;
}



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

const int target = 753;

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

    int res = 754;
    for (int i=0; i<s.length() - 2; i++) {
        string scope = s.substr(i, 3);
        int diff = abs(stoi(scope) - target);

        if (diff < res) { res = diff; }
    }
    cout << res << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define REP(i,a,b) for(int i=int(a);i<(int)b;++i)
#define Graph vector<vecctor<ll>>
#define pairs vector<pair<ll,ll>>
#define pb push_back



using namespace std;
using ll = long long;
const ll INF = 1e18;
const double PI = acos(-1);

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



int main(void)
{
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int m1,m2,d1,d2;
    cin >> m1 >> d1 >> m2 >> d2;
    if(m1 != m2){
        cout << 1 << endl;
    }else{
        cout << 0 << endl;
    }
    
    
    



    return 0;
}


Measured memory_bytes: 3640
#include<bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define mp make_pair
#define P pair<ll,ll>
#define F first
#define S second
#define vll vector<ll>
#define pb push_back
#define M 1000000007
#define for1(i,n) for(ll i=1;i<=n;i++)
#define f(i,n) for(ll i=0;i<n;i++)
#define forab(i,a,b,c) for(ll i=a;i<=b;i+=c)
using namespace std;
int main()
{
	ios_base:: sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	ll a;
	cin >> a;
	ll ans = a + (a * a) + ((a * a) * a);
	cout << ans;
	return 0;
}

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

#define CPP_STR(x) CPP_STR_I(x)
#define CPP_CAT(x,y) CPP_CAT_I(x,y)
#define CPP_STR_I(args...) #args
#define CPP_CAT_I(x,y) x ## y

using i8  = int8_t;
using u8  = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;

using f32 = float;
using f64 = double;

#define FOR(i, start, end) for(i64 i = (start), CPP_CAT(i,xxxx_end)=(end); i < CPP_CAT(i,xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) for(i64 i = 1; i <= n; ++i)
#define RREP(i, n) for(i64 i = n - 1; i >= 0 ; --i)
#define RREPS(i, n) for(i64 i = n; i >= 1; --i)

#define ALL(f,c,...) (([&](decltype((c)) cccc) { return (f)(std::begin(cccc), std::end(cccc), ## __VA_ARGS__); })(c))

#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

#define pb push_back
#define mp make_pair
#define F first
#define S second

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

template<typename C>
i64 SIZE(const C& c) { return static_cast<i64>(c.size()); }

template<typename T, size_t N>
i64 SIZE(const T (&)[N]) { return static_cast<i64>(N); }


struct ProconInit {
    static constexpr int IOS_PREC = 15;
    static constexpr bool AUTOFLUSH = false;

    ProconInit() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(IOS_PREC);
        if(AUTOFLUSH)
            cout << unitbuf;
    }
} PROCON_INIT;


#define MAX_N 200000
#define MAX_Q 200000

typedef pair<i64, i64> P;  // <node, added>

i64 N, Q;
i64 A[MAX_N];  // P_i = x <=> i番目の頂点を根とする部分木に含まれるすべての頂点のカウンターに+x
vector<i64> E[MAX_N];
stack<P> S;
i64 counter[MAX_N];
bool visited[MAX_N];


void solve() {
  REP(i, N) {
    visited[i] = false;
  }

  S.push(mp(0, 0));  // root
  P p;
  i64 u, a;
  while (!S.empty()) {
    p = S.top(); S.pop();
    u = p.F, a = p.S;
    counter[u] = a + A[u];
    visited[u] = true;

    for (auto it = E[u].begin(); it != E[u].end(); it++) {
      if (visited[*it]) continue;
      S.push(mp(*it, counter[u]));
    }
  }

  REP(i, N) {
    cout << counter[i] << " ";
  }
  cout << endl;
}


int main() {
  cin >> N >> Q;
  REP(i, N) {
    A[i] = 0;
  }

  i64 a, b;
  REP(i, N - 1) {
    cin >> a >> b;
    a--, b--; // 1-indexed to 0-indexed
    E[a].pb(b);
    E[b].pb(a);
  }

  i64 p, x;
  REP(i, Q) {
    cin >> p >> x;
    p--;
    A[p] += x;
  }

  solve();

  return 0;
}

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

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

    int N; cin >> N;
    int a[N];
    for(int i=0;i<N;i++){
        cin >> a[i];
    }
    int cost = 1e6;
    for(int i=-100;i<101;i++){
        int temp = 0;
        for(int j=0;j<N;j++){
            temp += (i-a[j])*(i-a[j]);
        }
        cost = min(cost,temp);
    }
    cout << cost;


    return 0;
}


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

using namespace std;

map <char, long long> u;

int main () {
	string s;
	cin >> s;        
	for (int i = 0;i < s.size ();i ++) {
		u[s[i]] ++;
	}
	long long ans = s.size () * (s.size () - 1) / 2;
	for (char x = 'a';x <= 'z';x ++) {
		ans -= (u[x] * (u[x] - 1)) / 2;
	}
	cout << ans + 1;
	return 0;
}

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

int main() {
    string s, t;
    int n, a=0, b=0;

    cin >> n;
    for (int i=0; i<n; i++) {
        cin >> s >> t;
        if (s > t) {
            a = a + 3;
        } else if (s < t) {
            b = b + 3;
        } else {
            a++;
            b++;
        }
    }
    cout << a << " " << b << endl;
}


Measured memory_bytes: 3136
data = input()
#data = '\\\\///\\_/\\/\\\\\\\\/_/\\\\///__\\\\\\_\\\\/_\\/_/\\'
diff = {'\\':-1, '_':0, '/':1}
height = [0]
[height.append(height[-1]+diff[i]) for i in data]
bottom = min(height)
height = [h-bottom for h in height]

m = max(height)
water = [m for h in height]

height00 = [0] + height + [0]
water00 = [0] + water + [0]

roop = True
forward = 1
while roop:
	temp = water00[:]
	for i in range(1,len(water00)-1)[::forward]:
		water00[i] = max(height00[i],min(water00[i-1:i+2]))
	roop = temp != water00
	forward *= -1

water = water00[1:-1]
depth = [w-h for w,h in zip(water,height)]

paddles = [0]
for d1,d2 in zip(depth[:-1],depth[1:]):
	if d1==0 and d2>0:
		paddles.append(0)
	paddles[-1] += min(d1,d2) + 0.5*abs(d1-d2)

paddles = [int(p) for p in paddles[1:]]
print(sum(paddles))
print(len(paddles), *paddles)

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

template<typename flow_t>
struct FordFulkerson{
    struct edge{
        int to;
        flow_t cap;
        int rev;
        bool isrev;
    };
    vector<vector<edge>> graph;
    vector<int> used;
    const flow_t INF;
    FordFulkerson(int V):graph(V),used(V),INF(numeric_limits<flow_t>::max()){}

    void add_edge(int from,int to,flow_t cap){
        graph[from].push_back((edge){to,cap,(int)graph[to].size(),false});
        graph[to].push_back((edge){from,0,(int)graph[from].size()-1,true});
    }

    flow_t dfs(int now,int t,flow_t f){
        if(now==t) return f;
        used[now]=true;
        for(int i=0;i<graph[now].size();i++){
            edge &e=graph[now][i];
            if(!used[e.to]&&e.cap>0){
                flow_t d=dfs(e.to,t,min(f,e.cap));
                if(d>0){
                    e.cap-=d;
                    graph[e.to][e.rev].cap+=d;
                    return d;
                }
            }
        }
        return 0;
    }

    flow_t max_flow(int s,int t){
        flow_t flow=0;
        for(;;){
            for(int i=0;i<graph.size();i++) used[i]=0;
            flow_t f=dfs(s,t,INF);
            if(f>0) flow+=f;
            else return flow;
        }
    }
};

int main(){
    int N,M;
    cin>>N>>M;
    string S;
    cin>>S;
    vector<ll> A(N);
    for(int i=0;i<N;i++) cin>>A[i];

    FordFulkerson<ll> graph(N+2);
    int s=N,t=N+1;
    for(int i=0;i<N;i++){
        if(S[i]=='R'){
            graph.add_edge(s,i,0);
            graph.add_edge(i,t,A[i]);
        }else{
            graph.add_edge(s,i,A[i]);
            graph.add_edge(i,t,0);
        }
    }
    for(int i=0;i<M;i++){
        int x,y,z;
        cin>>x>>y>>z;
        x--;y--;
        if(x>y) swap(x,y);
        graph.add_edge(y,x,z);
    }

    cout<<graph.max_flow(s,t)<<endl;


    return 0;
}


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

int main(){
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++) cin>>a[i];
    sort(a,a+n);
    double res=a[n-1]/2.0;
    long double c=1000000007;
    int ans;
    for(int i=0;i<n-1;i++){
        long double b=abs(a[i]-res);
        if(b<c){
            ans=a[i];
            c=b;
        }
    }
    cout<<a[n-1]<<" "<<ans<<endl;
}

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

int main()

{
    ll x,i,n,count=1,d=0;
    cin>>n>>x;
    ll arr[n];
    for(i=0;i<n;i++)
        cin>>arr[i];
    for(i=0;i<n;i++)
    {
        d=d+arr[i];
        if(d<=x)
            count++;
    }
    cout<<count<<endl;
}


Measured memory_bytes: 256
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<vector>
#include<algorithm>
#include<functional>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<deque>
#include<tuple>
#include<iostream>
#include<string>
#include<unordered_map>
#include<unordered_set>
using namespace std;
#define REP(i,x) for (int i=0;i<x;i++)
#define REPP(i,x,y) for (int i=x;i<=y;i++)
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define MX3(x,y,z) max(x,max(y,z))
#define MX4(x,y,z,p) max(x,max(y,max(z,p)))
#define MX5(x,y,z,p,q) max(x,max(y,max(z,max(p,q))))
#define MN3(x,y,z) min(x,min(y,z))
#define MN4(x,y,z,p) min(x,min(y,min(z,p)))
#define MN5(x,y,z,p,q) min(x,min(y,min(z,min(p,q))))
#define amax(x,y) x=max(x,y)
#define amin(x,y) x=min(x,y)
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<vb> vvb;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef tuple<int,int,int> ti;
typedef tuple<ll,ll,ll> tl;
// #####################################################################
const long long MOD = 998244353;
 
const double __gauss_template_EPS = 1e-8;
const double EPS = __gauss_template_EPS;
 
int Gauss(vector<vector<int>> &mat) {
    
    int rank = 0;
    int n = (int)mat.size();
 
    for(int i = 0; i < n; i++) {
        bool ok = false;
        for(int j = rank; j < n; j++) {
            if(mat[j][i]) {
                swap(mat[j], mat[rank]);
                ok = true;
                break;
            }
        }
 
        if(!ok) continue;
 
        for(int j = rank+1; j < n; j++) if(mat[j][i]) {
 
            for(int k = i; k < n; k++) {
 
                mat[j][k] ^= mat[rank][k];
            }
        }

        rank++;
    }
 
    return rank;
}
 
 
 
const long long __exp_template_MOD = MOD;
const long long mod = MOD;
int qpow(int x, int n){ int y = 1; while(n){ if(n & 1) y = (ll) y * x % mod; x = (ll) x * x % mod; n >>= 1; } return y; }
 
long long exp(int a, int b) {
    if(b == 0) return 1;
    long long tmp = exp(a, b/2);
    if(b & 1) {
        return (long long)tmp*tmp%__exp_template_MOD*a%__exp_template_MOD;
    }
    return (long long)tmp*tmp%__exp_template_MOD;
}
 
void solve() {
    int n, m;
    SI(n);
    SI(m);
    vvi rec(max(n, m),vi(max(n,m), 0));
    REP(i, n) {
        REP(j, m) {
            scanf("%d", &rec[i][j]);
        }
    }
    int r = Gauss(rec);
 
    printf("%lld\n", ((qpow(2, n+m-1) - qpow(2, n+m-r-1))%MOD+MOD)%MOD);
}
 
void init() {
    
 
 
}
 
int main () {
    init();
    solve();
    return 0;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

const string OK = "Yes", NG = "No";

string solve()
{
    int n;
    cin >>n;
    vector<int> a(n);
    rep(i,n) cin >>a[i];

    sort(all(a));

    if(a[n-1]-a[0]>=2) return NG;

    if(a[0]==1)
    {
        for(int i=1; i<n; ++i)if(a[1]!=a[i]) return NG;
        return OK;
    }

    if(a[0] == a[n-1])
    {
        if(a[0]<=n/2 || a[0]==n-1) return OK;
        return NG;
    }
    else
    {
        int x = a[0], y = a[n-1];

        int X=0,Y=0;
        rep(i,n)
        {
            if(a[i]==x) ++X;
            else ++Y;
        }

        if(y==n-1)
        {
            if(Y==2) return OK;
            return NG;
        }

        int need = y+1+(x-X);

        if(X<=x && Y>=2 && need<=n) return OK;
        return NG;
    }
}

int main()
{
    cout << solve() << endl;
    return 0;
}


Measured memory_bytes: 640
/*
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
*/
#include <bits/stdc++.h>

#define rep(i,n)  for(int i=0; i<(n); i++)
using ll = long long;
using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> x(n);
    vector<int> dp(100);
    rep(i,n){
      cin >> x[i];
    }
    rep(i,100){
      dp[i] = 0;
    }
    rep(i,100){
      rep(j,n){
        dp[i] += pow((x[j] - (i+1)),2.0);
//        cout << pow((x[j] - (i+1)),2.0) << " " << (x[j] - (i + 1)) << " " << x[j] << " " << i+1 << endl;
      }
//      cout << dp[i] << endl;
//      cout << endl;
    }
    sort(dp.begin(),dp.end());
    cout << dp[0] << endl;

    return 0;
}


Measured memory_bytes: 256
#include<stdio.h>
int main(){
	int i,n;
	i = 1;
	n = 1;
while(n!=0) {
	scanf("%d",&n);
	if(n!=0){
	printf("Case %d: %d\n", i, n);
	i = i + 1;
		}
	else{
		}
	}
return 0;
}


Measured memory_bytes: 2044
import java.io.IOException;
import java.util.Scanner;


public class Main{
	public static void main(String args[]) throws IOException{
		Scanner bf = new Scanner(System.in);
		double x = 0;
		double y = 0;
		int angle = 90;
		while(true){
			String str = bf.nextLine();
			String[] tmp = str.split(",");
			int dist = Integer.parseInt(tmp[0]);
			int role = Integer.parseInt(tmp[1]);
			if(dist == 0 && role == 0) break;
			x += Math.cos(Math.toRadians((double)angle)) * dist;
			y += Math.sin(Math.toRadians((double)angle)) * dist;
			angle -= role;
		}
		System.out.println((int)x);
		System.out.println((int)y);
		bf.close();
	}
}

Measured memory_bytes: 17148
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <deque>     // deque
#include <iomanip>   //setprecsion
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <utility>   // pair, make_pair
#include <vector>    // vector

#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cmath>  //sin, cos, tan

using namespace std;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define blank(ans) cout << (ans) << " ";
#define mp(p, q) make_pair(p, q)
#define mt(p, q, r) make_tuple(p, q, r)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()

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

typedef vector<ll> vll;
typedef vector<ld> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<str> vs;
typedef vector<vector<ll>> vvll;
typedef vector<vector<ld>> vvd;
typedef vector<vector<bool>> vvb;
typedef vector<vector<char>> vvc;
typedef vector<vector<str>> vvs;
typedef vector<pair<ll, ll>> vpll;
typedef vector<tuple<ll, ll, ll>> vtlll;

const ld PI = acos(-1.0);
const ll MAX = 9000000000000000000;
const ll MIN = -9000000000000000000;
const ld DMAX = 4500;
const ld DMIN = -4500;
const ll MOD = 1000000007;

template <typename T>
void fin(T a)
{
  cout << a << endl;
  exit(0);
}

void Main()
{
  str s, t; cin >> s >> t;
  if(s == t.substr(0, s.size())) fin("Yes");
  fin("No");
}

//前処理
int main()
{
  //ios::sync_with_stdio(false);
  //cin.tie(0);
  cout << fixed << setprecision(20); //高精度少数表示
  //cout << setfill('0') << internal << setw(4); 0埋め４桁
  Main();
  return 0;
}


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

#define rep(i,a,b) for(int i=a;i<b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
using namespace std;
void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; const ll MOD = 1000000007;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

//---------------------------------------------------------------------------------------------------
void _main() {
    ll N , K; cin >> N >> K;
    vector<ll> tele(N, 0);
    rep(i, 0, N) cin >> tele[i];

    ll now = 0, count = 0;
    vector<ll> already(N, -1);
    vector<ll> route; route.push_back(1);
    while(already[now] == -1) {
        already[now] = count;
        now = tele[now] - 1;
        route.push_back(now + 1);
        count++;
    }
    ll first = already[now], loop = count - already[now];
    if (K < first) cout << route[K] << endl;
    else cout << route[first + (K-first) % loop] << endl;
}

Measured memory_bytes: 8384
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // long long64_t, long long*_t
#include <cstdio> // prlong longf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <iomanip> //setprecision
#include <limits>
using namespace std;
int main(){
    int n,q;
    long long sum=0;
    cin >>  n >> q;
    queue<string> a1;
    queue<int> a2;
    for(int i=0; i<n; i++){
        string name;
        int time;
        cin >> name >> time;
        a1.push(name);
        a2.push(time);
    }
    while(a2.empty()==0){
        if(q>=a2.front()){
            sum+=a2.front();
            cout << a1.front() << " " << sum << endl;
            a1.pop();
            a2.pop();
        }
        else{
            sum+=q;
            a2.push(a2.front()-q);
            a1.push(a1.front());
            a1.pop();
            a2.pop();
        }
    }
}



Measured memory_bytes: 5748
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100005;
int n, m;
int A[MAX_N];
priority_queue<pair<int, int>> pq;
int main()
{
    scanf("%d %d", &n, &m);
	for (int i = 0; i < m; i++) {
        int a, b;
        scanf("%d %d", &a, &b);
        pq.push({-b, a});
    }
int s = 0, cnt = 0;
    while(!pq.empty()) {
        int b = -pq.top().first, a = pq.top().second;
        pq.pop();
        if (a < s) continue;
        s = b;
        cnt++;
    }
printf("%d\n", cnt);
    return 0;
}

Measured memory_bytes: 1400
//============================================================================
// Name        : template.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <bits/stdc++.h>
using namespace std;
#define _CRT_SECURE_NO_DEPRECATE // suppress some compilation warning messages (for VC++ users)
// Shortcuts for "common" data types in contests
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef set<int> si;
typedef map<string, int> msi;
// To simplify repetitions/loops, Note: define your loop style and stick with it!
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define REP(i, a, b) \
for (int i = int(a); i <= int(b); i++) // a to b, and variable i is local!
#define forn(i,n) \
for (int i =0; i<(n); i++)
#define TRvi(c, it) \
for (vi::iterator it = (c).begin(); it != (c).end(); it++)
#define TRvii(c, it) \
for (vii::iterator it = (c).begin(); it != (c).end(); it++)
#define TRmsi(c, it) \
for (msi::iterator it = (c).begin(); it != (c).end(); it++)
#define INF 2000000000 // 2 billion
// If you need to recall how to use memset:
#define MEMSET_INF 127 // about 2B
#define MEMSET_HALF_INF 63 // about 1B
//memset(dist, MEMSET_INF, sizeof dist); // useful to initialize shortest path distances
//memset(dp_memo, -1, sizeof dp_memo); // useful to initialize DP memoization table
//memset(arr, 0, sizeof arr); // useful to clear array of integers

int A() {
	ios::sync_with_stdio(false);
 cin.tie(0);
 int n; 
 cin >> n;
 if(n >= 30) cout <<"Yes\n";
 else cout << "No\n";
return 0;
}

int main() {
	ios::sync_with_stdio(false);
 cin.tie(0);
 ll n, d;
 cin >> n >> d;
 int an = 0;
 while(n--) {
 	double x,y; cin >> x >> y;
 	double dis = (x*x) + (y*y);
 	if(dis <= d*d) {
 		an++;
 	}
 }
 cout << an << "\n";
return 0;
}


Measured memory_bytes: 3792
import heapq

X,Y,A,B,C = [int(i) for i in input().split()]
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]
R = [-int(i) for i in input().split()]
P.sort()
Q.sort()
total = P[A-X:] + Q[B-Y:]
total.sort()
heapq.heapify(R)
i = 0
while True:
    try:
        n = -heapq.heappop(R)
        if i < len(total) and total[i] < n:
            total[i] = n
            i += 1
        else:
            break
    except:
        break
print(sum(total))

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

int n;
char s[400][400];

bool isOK(){
  for(int i = 0;i < n;i++){
    for(int j = 0;j < n;j++){
      if(s[i][j] != s[j][i]){
        return false;
      }
    }
  }

  return true;
}

int sp(){
  char tmp[400];

  for(int i = 0;i < n;i++){
    tmp[i] = s[0][i];
  }

  for(int i = 1;i < n;i++){
    for(int j = 0;j < n;j++){
      s[i-1][j] = s[i][j];
    }
  }

  for(int i = 0;i < n;i++){
    s[n-1][i] = tmp[i];

  }
  return 0;
}

int main(){
  cin >> n;

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

  long long ans = 0;
  for(int i = 0;i < n;i++){
    if(isOK()){
      ans += n;
    }
    sp();
  }

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



Measured memory_bytes: 512
import sys
import itertools

input = sys.stdin.readline


def main():
    N = int(input())
    A = list(map(int, input().split()))
    A = A[::-1]
    B = list(itertools.accumulate(A))
    MOD = 10**9+7
    ans = 0
    for i in range(N - 2, -1, -1):
        ans = (ans + A[i + 1] * B[i]) % MOD
    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 31504
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#include <algorithm>
#include <utility>
#include <cmath>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main(){
int64_t N,A,B; cin >> N >> A >> B;
int64_t count = 0;
int64_t a,b;
a = min(A,B);
b = max(A,B);

	while (true){
	if((b - a) % 2 == 0){
		count +=(b - a) / 2;
		break;
	}
	else{
		if(a - 1 <= N - b){
			count += a - 1;
			b = b - a + 1;
			a = 1;
			
			while((b-a) % 2 != 0){
				b--;
				count++;
			}
		}
		else{
			count += N - b;
			a = a + N - b;
			b = N;
			while((b - a) %2 != 0){
				a++;
				count++;
			
		}
	}
}
	}
	cout << count << endl;
}

Measured memory_bytes: 256
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define REP(X, Y, Z) for (ll(X) = (Y); (X) < (Z); (X)++)

using namespace std;
using ll = long long;
using dll = deque<ll>;
using pll = pair<ll, ll>;
using qll = queue<ll>;
using usll = unordered_set<ll>;
using umll = unordered_map<ll, ll>;
using sll = stack<int>;
using vll = vector<ll>;
using vvll = vector<vll>;

constexpr ll kMod = 1e9 + 7;

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

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

void init() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

int main() {
  init();

  ll N, K;
  cin >> N >> K;
  vll X(N);
  REP(i, 0, N) {
    ll x;
    cin >> x;
    X[i] = x;
  }

  ll result = 0;
  REP(i, 0, N) {
    if (abs(K - X[i]) < abs(0 - X[i])) {
      result += abs(K - X[i]) * 2;
    } else {
      result += X[i] * 2;
    }
  }
  cout << result << endl;

  return 0;
}


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

int main() {
	int a,b;
	cin>>a>>b;
	
	if(a%3==0 || b%3==0 || (a+b)%3==0){
		cout<< "Possible"<<endl;
	}else{
		cout<< "Impossible"<<endl;
	}
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
long n,m;
vector<long[2]> var(28);
long f(vector<long> c,long t){
    c[t-1]=1;
    vector<long> conf(c.size(),1);
    if(c==conf)
        return 1;
    long count=0;
    for(auto x:var){
        if(x[0]==t&&c[x[1]-1]!=1)
            count+=f(c,x[1]);
        else if(x[1]==t&&c[x[0]-1]!=1)
            count+=f(c,x[0]);
    }
    return count;
}
int main(){
    cin>>n>>m;
    for(int i = 0; i < m; i++)
        cin>>var[i][0]>>var[i][1];
    long count=0;
    for(auto x:var){
        if(x[0]==1){
            vector<long> c(n,0);
            c[0]=1;
            count+=f(c,x[1]);
        }
    }
    cout<<count;
}

Measured memory_bytes: 256
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1000005;
int n,a[maxn],b[maxn],id[maxn];char s[maxn];
bool cmp(int x,int y){
	if(b[x]>=0&&b[y]>=0) return a[x]<a[y];
	if(b[x]>=0&&b[y]<0) return 1;
	if(b[x]<0&&b[y]>=0) return 0;
	if(b[x]<0&&b[y]<0) return a[x]+b[x]>a[y]+b[y];
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++){
		scanf("%s",s+1);
		for (int j=1;s[j];j++)
		b[i]+=s[j]=='('?1:-1,
		a[i]=max(a[i],-b[i]);
		b[0]+=b[i];
	}
	if(b[0]){puts("No");return 0;}
	for (int i=1;i<=n;i++) id[i]=i;
	sort(id+1,id+1+n,cmp);
	for (int _i=1,i=id[_i],sum=0;_i<=n&&b[i]>=0;i=id[++_i]){
		if(sum<a[i]){puts("No");return 0;}
		sum+=b[i];
	}
	for (int _i=n,i=id[_i],sum=0;_i&&b[i]<0;i=id[--_i]){
		sum-=b[i];
		if(sum<a[i]){puts("No");return 0;}
	}
	puts("Yes");return 0;
}

Measured memory_bytes: 13368
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <functional>
using namespace std;

typedef pair<int, int> Pair;

struct Edge {
  int len;
  int to;
};

int n;
int m;
bool used[10000];
vector<Edge> es[10000];
priority_queue<Pair, vector<Pair>, greater<Pair> > q;

int CalcMSTCost() {
  int ret = 0;

  fill(used, used+n, false);
  q.push(Pair(0, 0));
  while (!q.empty()) {
    Pair p = q.top(); q.pop();
    int d = p.first;
    int v = p.second;

    if (used[v]) continue;
    used[v] = true;
    ret += d;
    for (int i=0; i<es[v].size(); i++) {
      q.push(Pair(es[v][i].len, es[v][i].to));
    }
  }

  return ret;
}

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0) return 0;

    for (int i=0; i<n; i++) {
      es[i].clear();
    }
  
    scanf("%d", &m);
    for (int i=0; i<m; i++) {
      int a, b;
      struct Edge e;
      scanf("%d,%d,%d", &a, &b, &e.len);
      e.len -= 100;
      e.to = b;
      es[a].push_back(e);
      e.to = a;
      es[b].push_back(e);
    }

    printf("%d\n", CalcMSTCost()/100);
  }
}

Measured memory_bytes: 3092
#include <bits/stdc++.h>
using namespace std;
vector <int> v;
int blank = -1;
int dp[2][100005];
bool call(bool p,int k){
	bool flag = 0;
	if(dp[p][k]!=blank)
		return (dp[p][k]==1);
	for(auto x: v){
		if(k>=x){
			if(!flag) flag= !call(!p,k-x);
		}
	}
	dp[p][k]=flag;
	return flag;
}

int main()
{
	int T,n,m,i,j,H,A,k;
	cin>>n>>k;
	memset(dp,-1,sizeof(dp));
	for(i=0;i<n;i++){
		cin>>m;
		v.push_back(m);
	}
	bool flag = call(0,k);
	if(flag)	cout<<"First";
	else		cout<<"Second";
	return 0;
}

Measured memory_bytes: 12192
#include <cstdio>

long pow_2(int N) {
    if (N == 0) {
        return 1;
    } else {
        long M = pow_2(N/2);
        M *= M;
        if (N % 2 == 1) {
            M *= 2;
        }
        return M;
    }
}

long count_patty(int N, long X, long X_max, long P_max) {
    if (N == 0 && X == 1) {
        return 1;
    } else if (X <= N) {
        return 0;
    } else if (X >= X_max-N) {
        return P_max;
    } else {
        long P;
        X_max = (X_max-3)/2;
        P_max = (P_max-1)/2;
        if (X <= X_max+1) {
            P = count_patty(N-1,X-1,X_max,P_max);
        } else {
            P = P_max + 1 + count_patty(N-1,X-X_max-2,X_max,P_max);
        }
        return P;
    }
}
int main() {
    int N;
    long X,X_max,P_max;
    scanf("%d %ld",&N,&X);
    X_max = pow_2(N+2) - 3;
    P_max = pow_2(N+1) - 1;
    printf("%ld\n",count_patty(N,X,X_max,P_max));
}

Measured memory_bytes: 128
N,W = map(int, input().split())
wvs = []
for i in range(N):
    w,v = map(int, input().split())
    wvs.append((w,v))
# dp : n個入っていて、合計がwのときの最大値
w0 = wvs[0][0]
dp = [[[0]*(330) for i in range(110)] for k in range(110)]
# dp[0][0][0] = 0
for i in range(N):
    w, v = wvs[i]
    dw = w-w0
    for j in range(i+1):
        for k in range(310):
            # if k - dw >= 0:
            # if dp[i][j][j] == -1:continue
            dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k])
            dp[i+1][j+1][k+dw] = max(dp[i][j][k]+v, dp[i+1][j+1][k+dw])

d = dp[N]
ans = 0
for j in range(N+1):
    if W-j*w0 >= 0:
        a = max(d[j][:W-j*w0+1])
        ans = max(ans, a)
print(ans)

Measured memory_bytes: 106624
fun main(args: Array<String>) {
    val (n, m, l) = readLine()!!.split(" ").map(String::toInt)

    var aMatrix = MutableList(n) { MutableList(m) {0} }
    var bMatrix = MutableList(m) { MutableList(l) {0} }

    (0 until n).forEach { aMatrix[it] = readLine()!!.split(" ").map(String::toInt).toMutableList() }
    (0 until m).forEach { bMatrix[it] = readLine()!!.split(" ").map(String::toInt).toMutableList() }

    (0 until n).forEach { row ->
        (0 until l).forEach { column ->
            print("${(0 until m).map { aMatrix[row][it].toLong() * bMatrix[it][column].toLong() }.sum()}")
            if (column != l - 1) {print(" ")}
        }
        println()
    }
}



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

#define ll long long
#define vecll vector<long long>
#define vec2ll vector<vector<long long>>
#define vecbool vector<bool>
#define vecpairll vector<pair<long long,long long>>
#define forll(s,a,b) for(long long s = a;s < b;s++)
#define forllde(s,a) for(long long s = a;s > 0;s--)


int main()
{
  ll n, c=0;
  cin >> n;
  string st;
  map<vecll, ll> m;
  forll(i, 0, n){
	cin >> st;
	vecll t(26, 0);
	forll(j, 0, 10){
	  t[st[j] - 'a']++;
	}
	m[t]++;
  }
  for(auto i=m.begin(); i!=m.end(); i++){
	c += (i->second) * (i->second - 1) / 2;
  }
  cout << c << endl;
}


Measured memory_bytes: 33024