akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 1e18L
const int inf=1000000000;

bool cmp(pair<int,int> a, pair<int,int> b){
	return a.second<b.second;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
       ll n,w,i,j;
    cin>>n>>w;
    ll v[n],wt[n]; ll sumv=0;
    for(i=0;i<n;i++)
    {
        cin>>wt[i]>>v[i];
        sumv+=v[i];
    }
    ll dp[sumv+1];
    for(ll i=0;i<=sumv;i++) dp[i]=INF;
    dp[0]=0;
    
    for(ll i=0;i<n;i++)
    {
        for(ll j = sumv-v[i];j>=0;j--)
        {
            dp[v[i]+j] = min(dp[v[i]+j],dp[j]+wt[i]);
        }
    }

    ll ans=0;
    for(ll i=0;i<=sumv;i++)
    {
        if(dp[i]<=w)
            ans = max(ans,i);
    }
    cout << ans << "\n";
    
}



Measured memory_bytes: 1024
public class Main {
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int[] x = new int[]{1,2,3,4,5,6,7,8,9} ;
		int[] y = new int[]{1,2,3,4,5,6,7,8,9} ;
		
		for(int i=0; i<9; i++) {
			for(int j=0; j<9; j++) {
				System.out.println(x[i] + "x" + y[j] + "=" + (x[i]*y[j]));
			}
		}	
	}
}



Measured memory_bytes: 24180
S = input()
lis = [S[0]]
i = 1
while i<len(S)-1:
  if S[i]==lis[-1]:
    lis.append(S[i:i+2])
    i += 2
  else:
    lis.append(S[i])
    i += 1
ans = len(lis)
if i==len(S)-1 and S[i]!=lis[-1]:
  ans += 1
print(ans)

Measured memory_bytes: 14264
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(v) v.size()
#define INF 100000000000000 //10^14
template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main()
{
    ll N, K;
    cin >> N >> K;
    vector<ll> p(N);
    rep(i, N) cin >> p.at(i);
    sort(all(p));
    ll ans = 0;
    rep(i, K){
        ans += p.at(i);
    }
    cout << ans << endl;
}

Measured memory_bytes: 3632
#include <iostream>
using namespace std;
using ll = long long;

int main(){
        int flgC=0,flgD=0,flgCD=0;
        ll i,a,b,c,d,cd,A,B,tmp,cntC,cntD,cntCD;
        cin >> a >> b >> c >> d;
        cntC = (b-a+1)/c;
        cntD = (b-a+1)/d;
        B = d>=c? d : c;
        A = d>=c? c : d;
        while(A!=0){
                tmp = B;
                B = A;
                A = tmp%A;
        }
        cd = B*c/B*d/B;
        cntCD = (b-a+1)/(cd);
        if((b-a+1)%c>=c-(a-1+c*cntC)%c) ++cntC;
        if((b-a+1)%d>=d-(a-1+d*cntD)%d) ++cntD;
        if((b-a+1)%cd>=cd-(a-1+cd*cntCD)%cd) ++cntCD;
        cout << b-a+1-cntC-cntD+cntCD << endl;
        return 0;
}

Measured memory_bytes: 3568
N, M = gets.split.map(&:to_i)
ab_list = M.times.map { gets.split.map(&:to_i) }
table = Hash.new { 0 }
ab_list.each do |(a, b)|
  table[a] += 1
  table[b] += 1
end
(1..N).each do |i|
  puts table[i]
end


Measured memory_bytes: 3836
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ABC098A
{
	class Program
	{
		static void Main(string[] args)
		{
			int a, b,max;

			var word = Console.ReadLine().Split(' ');

			a = int.Parse(word[0]);
			b = int.Parse(word[1]);

            max = a + b;
            if (max < a - b) max = a - b;
            if (max < a * b) max = a * b;
			Console.WriteLine(max);

			Console.ReadKey();
			 
		}
	}
}


Measured memory_bytes: 11092
#include<iostream>
#include<algorithm>
#include<string>
#include<fstream>

#define SIZE 50

using namespace std;

int main(){
	int kard[SIZE]	= {0};
	int n,k,p,c;

	while(cin>>n>>k,n){
		for(int i=0;i<n ;i++){
			kard[i]	=n-i;
		}
		for(int i=0; i<k; i++){
			cin>>p>>c;
			rotate(kard,kard+p-1,kard+p-1+c);
		}
		cout<<kard[0]<<endl;
	}
	return 0;
}

Measured memory_bytes: 1184
n = int(input())
d = []
for _ in range(n):
  d.append(list(map(int, input().split())))

for i in range(n-2):
  if d[i][0] == d[i][1] and d[i+1][0] == d[i+1][1] and d[i+2][0] == d[i+2][1]:
    print("Yes")
    exit()

print("No")

Measured memory_bytes: 9188
import java.util.Scanner;
import java.util.Stack;

public class Main {
	static boolean tenbin(String str) {
		Stack<Character> stack = new Stack<>();
		for(int i=0; i<str.length(); i++) {
			char c = str.charAt(i);
			if(c == '.') break;
			if(c == '(') stack.push('(');
			if(c == '[') stack.push('[');
			if(c == ')') {
				if(stack.isEmpty() || stack.pop() != ('(')) {
					return false;
				}
			}
			if(c == ']') {
				if(stack.isEmpty() || stack.pop() != ('[')) {
					return false;
				}
			}
		}
		return stack.isEmpty();
	}
	
	public static void main(String[] args) {
		try(Scanner sc = new Scanner(System.in)){
			while(sc.hasNextLine()) {
				String str = sc.nextLine();
				if(str.equals(".")){
					break;
				}
				if(tenbin(str)) {
					System.out.println("yes");
				}
				else {
					System.out.println("no");
				}
			}
		}
	}
}



Measured memory_bytes: 43064
a,b,c,d = list(map(int,input().split()))

e = [abs(a-b),abs(b-c),abs(c-a)]


if d >= abs(c-a) :
    print('Yes')
elif abs(a-b) <= d and abs(b-c)<= d:
    print('Yes')
else:
    print('No')


Measured memory_bytes: 2940
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back
#define mt make_tuple
#define ALL(a) (a).begin(),(a).end()
#define FST first
#define SEC second
#define DEB cerr<<"!"<<endl
#define SHOW(a,b) cerr<<(a)<<" "<<(b)<<endl
#define vi vector<int>

using ll = long long;
const int INF = (INT_MAX/2);
const ll LLINF = (LLONG_MAX/2);
const double eps = 1e-8;
const ll DIV =1e9+7;
//const double PI = M_PI;
inline ll pow(ll x,ll n,ll m){ll r=1;while(n>0){if((n&1)==1)r=r*x%m;x=x*x%m;n>>=1;}return r%m;}
inline ll lcm(ll d1, ll d2){return d1 / __gcd(d1, d2) * d2;}
inline ll inv(ll x, ll div){return pow(x, div - 2, div);}
#define chmax(a,b) a=max(a,b)

/*Coding Space*/
class Dice{
public:
  int ue;
  int temae;
  int migi;
  Dice(int u, int t, int m){
    ue = u, temae = t, migi = m;
  }
};

using tiiiii = tuple<int,int,int,int,int>;

Dice rotate_r(Dice a){
  return Dice(7-a.migi,a.temae,a.ue);
}
Dice rotate_l(Dice a){
  return Dice(a.migi,a.temae,7-a.ue);
}
Dice rotate_oku(Dice a){
  return Dice(a.temae,7-a.ue,a.migi);
}
Dice rotate_temae(Dice a){
  return Dice(7-a.temae,a.ue,a.migi);
}
Dice dice(1,2,3);

int h,w;
vector<string> m;
map<tiiiii,bool> memo;
bool s(Dice d, int x, int y){
  if(x < 0 || x >= w || y < 0 || y >= h || m[y][x] == '#' || m[y][x] != 7 - d.ue + '0' || memo.count(tiiiii{d.ue,d.temae,d.migi,x,y}) == 1) return false;
  memo[tiiiii{d.ue,d.temae,d.migi,x,y}] = true;
  //cerr << x  << ' ' << y << ' ' << 7 - d.ue << endl;
  if(x == w - 1 && y == h - 1) return true;
  else{
    bool ret = false;
    ret |= s(rotate_r(d), x+1, y);
    ret |= s(rotate_l(d), x-1, y);
    ret |= s(rotate_oku(d), x, y-1);
    ret |= s(rotate_temae(d), x, y+1);
    return ret;
  }
}
int main(){
  cin >> h >> w;
  m.resize(h);
  rep(i,h) cin >> m[i];
  if(s(dice,0,0)) cout << "YES" << endl;
  else cout << "NO" << endl;
}



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

int main(){
  int a,b,c;
  int cnt[106];
  int i;

  for(i = 0;i < 105;i++) cnt[i] = 0;

  scanf("%d %d %d", &a, &b, &c);
  cnt[a]++;
  cnt[b]++;
  cnt[c]++;

  int ans = 0;
  for(i = 0;i <= 100;i++) if(cnt[i] != 0) ans++;

  printf("%d\n", ans);
  return 0;
}

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

using namespace std;

const int maxn = 100005;

typedef pair<int, int> pii;

int fa[maxn];

inline int getfa(int now)
{
	return fa[now] == now ? now : fa[now] = getfa(fa[now]);
}

int n;

set<int> mp[maxn];
set<int> mp2[maxn];
set<int> vis[maxn];

int main()
{
	scanf("%d", &n);
	for(int i = 1, f, t; i < n; ++i)
	{
		scanf("%d%d", &f, &t);
		mp[f].insert(t);
		mp[t].insert(f);
	}
	queue<pii> q;
	while(!q.empty())
		q.pop();
	for(int i = 1; i <= n; ++i)
		fa[i] = i;
	for(int i = 1, f, t; i < n; ++i)
	{
		scanf("%d%d", &f, &t);
		mp2[f].insert(t);
		mp2[t].insert(f);
		if(mp[f].count(t))
		{
			vis[t].insert(f);
			vis[f].insert(t);
			q.push(make_pair(f, t));
		}
	}
	while(!q.empty())
	{
		pii now = q.front();
		q.pop();
		if(getfa(now.first) == getfa(now.second))
			continue;
		int fax = getfa(now.first);
		int fay = getfa(now.second);
		if(mp[fax].size() > mp[fay].size())
		{
			swap(fax, fay);
			swap(now.first, now.second);
		}
		fa[fax] = fay;
		for(set<int>::iterator it = mp[fax].begin(); it != mp[fax].end(); ++it)
		{
			int noww = getfa(*it);
			if(!mp[fay].count(noww))
			{
				mp[fay].insert(noww);
				mp[noww].insert(fay);
			}
			if(!vis[fay].count(noww) && mp[fay].count(noww) && mp2[fay].count(noww))
			{
				vis[fay].insert(noww);
				vis[noww].insert(fay);
				q.push(make_pair(fay, noww));
			}
		}
		for(set<int>::iterator it = mp2[fax].begin(); it != mp2[fax].end(); ++it)
		{
			int noww = getfa(*it);
			if(!mp2[fay].count(noww))
			{
				mp2[fay].insert(noww);
				mp2[noww].insert(fay);
			}
			if(!vis[fay].count(noww) && mp[fay].count(noww) && mp2[fay].count(noww))
			{
				vis[fay].insert(noww);
				vis[noww].insert(fay);
				q.push(make_pair(fay, noww));
			}
		}
	}
//	for(int i = 1; i <= n; ++i)
//		cout << fa[i] << ' ';
//	cout << endl;
	int now = getfa(1);
	for(int i = 1; i <= n; ++i)
	{
		if(getfa(i) != now)
		{
			puts("NO");
			return 0;
		}
	}
	puts("YES");
	return 0;
}


Measured memory_bytes: 67200
#include <stdio.h>
int main(){
  int a,b,d,r;
  double f;
  scanf("%d %d",&a,&b);
  d=a/b;
  r=a%b;
  f=(double)a/(double)b;
  printf("%d %d %f\n",d,r,f);
  return 0;
}

Measured memory_bytes: 1068
#include <cstdio>
#include <iostream>

using namespace std;


int main(void)
{
    int a,b;

    cin >> a >>b;
    if(b % a == 0){
        printf("%d\n", a+b);
    }else{
        printf("%d\n", b-a);
    }

    return 0;
}

Measured memory_bytes: 256
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <functional>
#include <sstream>
#include <iostream>
#include <iomanip>
#define _USE_MATH_DEFINES
#include <cmath>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cfloat>
#include <cmath>
#include <ctime>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <cassert>
using namespace std;

#define EPS 1e-12
#define ull unsigned long long
#define ll long long
#define VI vector<ll>
#define PII pair<ll, ll> 
#define VVI vector<vector<ll> >
#define VVVI vector<vector<vector<ll>>>
#define VVVVI vector<vector<vector<vector<ll>>>>
#define REP(i,n) for(ll i=0,_n=(n);(i)<(ll)_n;++i)
#define REPR(i,n) for(ll i=(ll)(n)-1;0<=(i);--i)
#define RANGE(i,a,b) for(ll i=(ll)a,_b=(ll)(b);(i)<_b;++i)
#define RANGER(i,a,b) for(ll i=(ll)(b)-1,_a=(ll)(a);(_a)<=i;--i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define ALLR(c) (c).rbegin(), (c).rend()
#define PB push_back
#define MP(a, b) make_pair(a, b)
#define POPCOUNT __builtin_popcount
#define POPCOUNTLL __builtin_popcountll
#define CLEAR(table, v) memset(table, v, sizeof(table));
#define UNIFORM_DOUBLE(a, b) (((b-a)*(double)rand()/RAND_MAX)+a) // [a, b) 
#define UNIFORM_LL(a, b) (ll)UNIFORM_DOUBLE(a, b) // [a, b) 
#define IN(v, lo, hi) ((lo)<=(v) && (v)<(hi))
#define DD(v) cout<<#v<<": "<<v<<endl
#define FI first
#define SE second
template <typename T0, typename T1> std::ostream& operator<<(std::ostream& os, const map<T0, T1>& v) { for( typename map<T0, T1>::const_iterator p = v.begin(); p!=v.end(); p++ ){os << p->first << ": " << p->second << " ";} return os; }
template <typename T0, typename T1> std::ostream& operator<<(std::ostream& os, const pair<T0, T1>& v) { os << v.first << ": " << v.second << " "; return os; }
template <typename T> std::ostream& operator<<(std::ostream& os, const vector<T>& v) { for( int i = 0; i < (int)v.size(); i++ ) { os << v[i] << " "; } return os; }
template <typename T> std::ostream& operator<<(std::ostream& os, const set<T>& v) { vector<T> tmp(v.begin(), v.end()); os << tmp; return os; }
template <typename T> std::ostream& operator<<(std::ostream& os, const deque<T>& v) { vector<T> tmp(v.begin(), v.end()); os << tmp; return os; }
template <typename T> std::ostream& operator<<(std::ostream& os, const vector<vector<T> >& v) { for( int i = 0; i < (int)v.size(); i++ ) { os << v[i] << endl; } return os; }
template<typename T>void maxUpdate(T& a, T b) {a = max(a, b);}
template<typename T>void minUpdate(T& a, T b) {a = min(a, b);}
string YN(bool v) { return v ? "Yes":"No"; }

#define INF (1LL<<60)

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	ll N,M,W,H,K;
	string s;
	while(cin>>N>>M) {
		ll ans=0;
		if(N>M) swap(N, M);
		// N<=M
		if(N==1) {
			if(M==1) ans=1;
			if(M==2) ans=0;
			if(3<=M) ans=M-2;
		}
		if(N==2) {
			ans=0;
		}
		if(3<=N) {
			ans=(N-2)*(M-2);
		}
		cout<<ans<<endl;
	}
	
	return 0;
}


Measured memory_bytes: 256
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし


N = I()
S = LS2()

from collections import defaultdict

d_left = defaultdict(int)
d_right = defaultdict(int)

# 半分全列挙

for i in range(2**N):
    left_red = ''
    left_blue = ''
    right_red = ''
    right_blue = ''
    for j in range(N):
        if (i >> j) & 1:
            left_red += S[j]
        else:
            left_blue += S[j]
    for j in range(2*N-1,N-1,-1):
        if (i >> (j-N)) & 1:
            right_red += S[j]
        else:
            right_blue += S[j]
    d_left[(left_red,left_blue)] += 1
    d_right[(right_red,right_blue)] += 1

ans = 0
for key in d_left.keys():
    ans += d_left[key]*d_right[key]

print(ans)


Measured memory_bytes: 280920
#include<iostream>
#include<string>
#include<stack>/*** LIFO *** stack<type> var;
push(), top():to get data, pop():to remove, size(), empty() ***/
using namespace std;

#define Fp(I, STAT, END) for(int I = STAT; I < END; I++)
#define Fm(I, STAT, END) for(int I = STAT; I > END; I--)

int main(){
    int N, M, A, B, good = 0;
    cin >> N >> M;
    int height[N];
    int hill[N] = {};
    int hill_check[N] = {};
    Fp(i, 0, N) cin >> height[i];
    Fp(i, 0, M){
        cin >> A >> B;
        if(height[A - 1] > height[B - 1]){
            hill[B - 1]++;
            hill_check[B - 1]++;
        }else if(height[A - 1] < height[B - 1]){
            hill[A - 1]++;
            hill_check[A - 1]++;
        }else{
            hill[A - 1]++; hill[B - 1]++;
            hill_check[A - 1]++; hill_check[B - 1]++;
        }
    }
    Fp(i, 0, N){
        if(hill[i] == 0 || hill_check[i] == 0) good++;
    }
    cout << good << endl;
}


Measured memory_bytes: 4736
#include "bits/stdc++.h"
using namespace std;
#define repi(i,a,b) for(int i=(a), i##_len=(b); i<i##_len; ++i)
#define rep(i,n) repi(i,0,n)
#define all(x) (x).begin(),(x).end()
#define tsort(v) sort((v).begin(), (v).end())
#define trev(v) reverse((v).begin(), (v).end())
#define siz(v) ((int)(v).size())
#define ers(v, n) (v).erase((v).begin() + n)
#define pop(v) (v).back(); (v).pop_back()
#define el "\n"
#define co(x) cout<<(x)<<el
#define coc(c, a, b) if(c)co((a));else co((b))
#define YES(c) coc(c,"YES", "NO")
#define Yes(c) coc(c,"Yes", "No")
#define yes(c) coc(c,"yes", "no")
#ifdef tqktmp_1
#define gc() getchar(); getchar()
#else
#define gc() 1
#endif
typedef long long lint;

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

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

    int a, b;
    string s;
    bool fl = 1;
    cin>>a>>b>>s;
    rep(i, a+b+1){
        if(i==a){
            if(s[i]=='-')continue;
            else fl=0;
        }
        if((s[i]<'0')||('9'<s[i])){
            fl=0;
            break;
        }
    }
    Yes(fl);
}

Measured memory_bytes: 256
// Problem : 
// 			C - Walking Takahashi
// 			
// 		
// Contest : AtCoder - AtCoder Beginner Contest 175
// URL : https://atcoder.jp/contests/abc175/tasks/abc175_c
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define db double
#define mod 1000000007
#define pb push_back
#define pp pair<int,int>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repe(i,a,b) for(int i=a;i<=b;i++)
#define repr(i,a,b) for(int i=b;i>=a;i--)
#define maxn 200005
#define ss second
#define ff first
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
int inv(int a, int b){
 return 1<a ? b - inv(b%a,a)*b/a : 1;
}
void solve()
{
	int x,k,d;
	cin >> x >> k >> d;
	x = abs(x);
	int t = x/d;
	if(k <= t)
	{
		x -=  k*d;
		cout  << x <<"\n";
	}
	else{
		k -= t;
		x -= t*d;
		if(k%2 == 0)
			cout << x <<"\n";
		else cout << abs(x-d) <<"\n";
	}
}
signed main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    //cin >> t;
    while(t--)
    solve();
    return 0;
}


Measured memory_bytes: 3644
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args)throws Exception {
    	InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);

        while(true){
        	String[] input = br.readLine().split(" ");
        	int H = Integer.parseInt(input[0]);
        	int W = Integer.parseInt(input[1]);
        	if(H == 0 && W == 0){
        		break;
        	}
        	for(int x = 0 ; x < H ; x++){
        		for(int y = 0; y < W ; y++){
        			System.out.print("#");
        		}
        		System.out.println();
        	}
        	System.out.println();
        }
    }
}

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

void dfs(int par,vector<int>& visited, vector< vector<int> >& edges,vector<int>& dist){

	vector<int> child=edges[par];

	for(int i=0;i<child.size();i++){
		if(visited[child[i]]==false){
			visited[child[i]] = true;
			//cout << "par " << par << " child " << child[i] << endl;
			dfs(child[i],visited,edges,dist);
		}
		dist[par] = max(dist[par],dist[child[i]]+1);
	}

}


int main(void){

	int N;
	cin >> N;

	vector<int> temp;
	vector< vector<int> > edges(N,temp);

	int M;
	cin >> M;

	for(int i=0;i<M;i++){
		int from;
		cin >> from;
		int to;
		cin >> to;
		edges[from-1].push_back(to-1);
	}

	vector<int> visited(N,false);
	vector<int> dist(N,0);

	for(int i=0;i<N;i++){
		if(visited[i]==false){
			visited[i] = true;
			dfs(i,visited,edges,dist);
		}
	}

	int ans=0;
	for(int i=0;i<dist.size();i++){
		ans = max(ans,dist[i]);
	}

	cout << ans;
}

Measured memory_bytes: 11264
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
string mx="",a,ret,cmp;
const int Q=1<<16;
int cc[Q],pa[Q],pb[Q],to[Q];
int owo[Q];
int Time=0;
void Deal(int l,int r)
{
	ret="";
	if(a[l]=='a'){
		for(int i=l;i<=r;i++)
			if(a[i]=='a')ret+="ab",i=to[i];
		return;
	}
	for(int i=(r-l+1)>>1;i;--i){
		int use=0;
		++Time;
		for(int j=r;j>=l&&use<i;--j)
			if(a[j]=='b')owo[j]=owo[to[j]]=Time,++use;
		cmp="";
		for(int j=l;j<=r;j++)
			if(owo[j]==Time)cmp+=a[j];
		ret=max(ret,cmp);
	}
}
int main()
{
	int n;
	cin>>n>>a;
	a="*"+a;
	for(int i=1,ca=0,cb=0;i<=(n<<1);i++){
		cc[i]=cc[i-1]+(a[i]=='a'?1:-1);
		if(a[i]=='a')pa[++ca]=i;
		else pb[++cb]=i;
	}
	for(int i=1;i<=n;i++)
		to[pa[i]]=pb[i],to[pb[i]]=pa[i];
	for(int i=(n<<1)-1,lst=n<<1;i>=0;--i)
		if(cc[i]==0){
			Deal(i+1,lst),lst=i;
			mx=max(mx,ret+mx);
		}
	cout<<mx;
	return 0;
}

Measured memory_bytes: 384
#include <iostream>
#include <vector>
#include <bitset>
#include <utility>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
 
using namespace std;
typedef long long ll;
 
const ll MOD=1000000007;

pair<ll,ll> dfs(const auto &p, auto &r, int x){
  if(r[x].first==-1){
    r[x].first=1; r[x].second=1;
  }
  for(int pi:p[x]){
    if(r[pi].first==-1){
      pair<ll,ll> ri=dfs(p,r,pi);
      r[x].first*=ri.first+ri.second;
      r[x].first%=MOD;
      r[x].second*=ri.first;
      r[x].second%=MOD;
    }
  }
  return r[x];
}

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

  vector<vector<int>> p(N);
  for(int i=0;i<N-1;i++){
    int x,y;
    cin >> x >> y;
    --x; --y;
    p[x].push_back(y);
    p[y].push_back(x);
  }

  vector<pair<ll,ll>> r(N,make_pair(-1,0));

  pair<ll,ll> ans=dfs(p,r,0);

  cout << (ans.first+ans.second+MOD)%MOD << endl;

  return 0;
}


Measured memory_bytes: 12800
#include<cstdio>
#include <iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> Pr;


string s,S;
map<char, int> mp;

int main()
{
	cin >> s;
	S = s;

	char c;
	int ans = 10000000, num;
	for (int j = 0; j < S.size(); j++) {
		if(mp[S[j]] == 0) c = S[j];
		s = S;
		num = 0;
		while (1) {

			bool flag = true;
			string str = "";
			for (int i = 0; i < s.size() - 1; i++) {
				if (s[i] == c) 
					str += s[i];
				else 
					str += s[i + 1];
			}

			s = str;
			for (int i = 0; i < s.size(); i++) {
				if (s[i] != c)flag = false;
			}
			num++;
			if (flag)break;
		}

		if (num < ans) 
			ans = num;
		mp[c] = 1;
	}

	for (int i = 0; i < S.size(); i++) {
		if (S[0] != S[i]) {
			cout << ans << endl;
			return 0;
		}
	}
	cout << 0 << endl;
	return	0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
const int N = 205, M = 1e7 + 8;

int n, x[N], prime[M], tot, k, own[N], m, y[N];
bool vis[M], use[N];
vector<int>S, T, V[N];

int find (int x) {
	for (auto v : V[x]) if (!use[v]) {
		use[v] = 1;
		if (!own[v] || find(own[v])) {
			own[v] = x;
			return 1;
		}
	}
	return 0;
}

int main () {
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) scanf("%d", &y[i]);
	sort(y + 1, y + n + 1);
	x[m = 1] = y[1];
	if (y[1] + 1 != y[2]) x[++m] = y[1] + 1;
	for (int i = 2; i <= n; ++i) {
		if (y[i] != y[i - 1] + 1) x[++m] = y[i];
		if (y[i] != y[i + 1] - 1) x[++m] = y[i] + 1;
	}
	n = m;
	vis[1] = 1;
	for (int i = 2; i <= 1e7; ++i) {
		if (!vis[i]) prime[++tot] = i;
		for (int j = 1; j <= tot && i * prime[j] <= 1e7; ++j) {
			vis[i * prime[j]] = 1;
			if (i % prime[j] == 0) break;
		}
	}
	for (int i = 1; i <= n; ++i) if (x[i] & 1) S.emplace_back(i); else T.emplace_back(i);
	for (auto u : S) for (auto v : T) if (!vis[abs(x[u] - x[v])]) V[u].emplace_back(v);
	for (auto u : S) if (memset(use, 0, sizeof use), find(u)) ++k;
	printf("%d\n", (S.size() - k) / 2 * 2 + (T.size() - k) / 2 * 2 + ((S.size() - k) & 1) * 3 + k);
	return 0;
}

Measured memory_bytes: 14592
N,u,v=map(int,input().split())

tree=[]
for i in range(N+1):
  tree.append([])

for i in range(N-1):
  A,B=map(int,input().split())
  tree[A].append(B)
  tree[B].append(A)
#print(u,v)
#print(tree)

dist_tak=[-1]*(N+1)
queue=[u]
d=0
while(len(queue)>0):
  new_queue=set()  
  for q in queue:
    if dist_tak[q]==-1:
      dist_tak[q]=d
      for q2 in tree[q]:
        new_queue.add(q2)
  d+=1
  queue=new_queue
#print(dist_tak)

dist_aok=[-1]*(N+1)
queue=[v]
d=0
while(len(queue)>0):
  new_queue=set()  
  for q in queue:
    if dist_aok[q]==-1:
      dist_aok[q]=d
      for q2 in tree[q]:
        new_queue.add(q2)
  d+=1
  queue=new_queue
#print(dist_aok)

answer=0
for i in range(1,N+1):
  if dist_tak[i]<dist_aok[i]:
    answer=max(answer,dist_aok[i]-1)
    
print(answer)

Measured memory_bytes: 27440
#include <bits/stdc++.h>
    using namespace std;
    int main() {
      int n;
      cin >>n;
      long cnt = 0;
      for (int i = 1; i <= n; i++) {
        if(i%3 != 0 && i%5 != 0){
         cnt = cnt + i; 
        }
      }
      cout << cnt;
      return 0;
    }

Measured memory_bytes: 3596
#include<stdio.h>
int A[100];

//Output function
void printSequence(int length){
    int i;
    for(i=0; i<length; i++){
        if(i!=0)printf(" ");
        printf("%d",A[i]);
    }

    printf("\n");

}

int main(){
    int key=0, length, i, j;

    scanf("%d", &length);
    for(i=0; i<length; i++){
        scanf("%d",&A[i]);
    }


    //Insertion Sort Algo
    for(i=1; i<=length; i++){
        printSequence(length);

        key = A[i];
        j = i - 1;

        while(j>=0 && A[j]>key){
            A[j+1] = A[j];
            j--;
        }

        A[j+1] = key;
    }

    return 0;
}

Measured memory_bytes: 604
#include <bits/stdc++.h>                                           
 
#define int long long
//#pragma GCC optimize("Ofast")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
 
 
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
#define forev(i, b, a) for(int i = (b); i >= (a); --i)
#define forn(i, a, b) for(int i = (a); i <= (b); ++i)
#define all(x) x.begin(), x.end()
#define sz(s) (int)s.size()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define s second
#define f first
 
 
using namespace std;
 
 
typedef pair < long long, long long > pll;    
typedef pair < int, int > pii;
typedef unsigned long long ull;         
typedef vector < pii > vpii;
typedef vector < int > vi;
typedef long double ldb;  
typedef long long ll;  
typedef double db;                             
 
 
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, block = 555;
const pii base = mp(1171, 3307), Mod = mp(1e9 + 7, 1e9 + 9);
const int inf = 1e9, maxn = 4e5 + 148, mod = 1e9 + 7, N = 2e5 + 11;
const db eps = 1e-12, pi = 3.14159265359;
const ll INF = 1e18;


int n, x, cnt[N];

main () {
	cin >> n;
	for (int i = 1; i < n; ++i) { 
		cin >> x;
		++cnt[x];
	}

	for (int i = 1; i <= n; ++i)
		cout << cnt[i] << ' ';
}


Measured memory_bytes: 5012
import std.stdio, std.algorithm, std.conv, std.array, std.string;

int[] LONGM = [1,3,5,7,8,10,12];
int[] SHORTM = [4,6,9,11];

void main()
{
    auto xy = readln.split.to!(int[]);

    writeln(
        xy.all!(i => LONGM.canFind(i)) || xy.all!(i => SHORTM.canFind(i)) ? "Yes" : "No"
    );
}

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

long long dp[100100];
int n,l;
int a[100100];

long long rec(int p){
  if(0<=dp[p])return dp[p];

  if(a[p-1] < a[p] && a[p] < a[p]){
    return dp[p] = l-a[p];
  }

  long long tmp = 0;
  if(a[p]<a[p-1])tmp = max(tmp,dp[p-1]);
  if(a[p]<a[p+1])tmp = max(tmp,rec(p+1));
  return dp[p]= l-a[p] + tmp;
}
  
int main(){
  long long ans;
  cin >> n >> l;
  for(int i=1;i<=n;i++){
    cin >> a[i];
    dp[i] = -1;
  }
  a[0] = a[n+1] = dp[0] = dp[n+1] = 0;

  ans = 0;
  for(int i=1;i<=n;i++)ans = max(ans,rec(i));

  cout << ans << endl;
}

Measured memory_bytes: 1996
#include <bits/stdc++.h>
#define ll long long
#define all(x) begin(x), end(x)
#define range(x) begin(x), begin(x)
#define what(x) cout << #x << ": " << x << '\n'
#define print(x) for (auto &el: x) cout << el << ' '; cout << '\n'
using namespace std;
 
template<typename S, typename T>
ostream& operator <<(ostream &os, pair<S, T> &m) { 
    os << '[' << m.first << " " << m.second << ']'; 
    return os; 
}

int count_digits(ll n) {
    int cnt = 0;
    while(n > 0) {
        cnt++;
        n /= 10;
    }
    return cnt;
}

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

    
    int n;
    cin >> n;

    vector<ll> v(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i];
        if (v[i] == 0) {
            cout << 0 << endl;
            return 0;
        }
    }

    ll prod = 1;
    for (int i = 0; i < n; ++i) {
        if ( (prod / 1e18) * v[i] > 1 ) {
            cout << -1 << endl;
            return 0;
        }
        prod *= v[i];
    }

    if (prod > 1000000000000000000LL)
        cout << -1 << endl;
    else 
        cout << prod << endl;



    return 0;
}

Measured memory_bytes: 3948
#include<stdio.h>
#include<math.h>

int main(void)
{
	int N,i,ans=0;
	double T,A,H[1000],diff,minDiff;

	scanf("%d",&N);
	scanf("%lf %lf",&T,&A);

	minDiff=100000;

	for(i= 1;i <= N;i++) {
		scanf ("%lf",&H[i]);
		diff= fabs(A-(T- H[i]*0.006));

		if(diff < minDiff){
			minDiff= diff;
			ans= i;
		}
	}

	printf("%d",ans);

	return 0;
}

Measured memory_bytes: 128
from itertools import chain


def solve():
    N, M, K = map(int, input().split())

    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    answer = 0

    sum_b = sum(B)
    idx_b = M
    remain = K

    for i, a in enumerate(chain([0], A)):
        remain -= a
        if remain < 0:
            break

        while sum_b > remain and idx_b >= 0:
            idx_b -= 1
            b = B[idx_b]
            sum_b -= b

        i += idx_b
        answer = i if answer < i else answer

    return answer


def main():
    print(solve())


if __name__ == "__main__":
    main()


Measured memory_bytes: 41140
n = int(raw_input())
A = map(int, raw_input().split())
d = {}

def soleve(i, m, n):
    if m == 0:
        return True
    if i >= n:
        return False
    if d.has_key((i, m)):
        return d[(i, m)]
    res = soleve(i + 1, m, n) or soleve(i + 1, m - A[i], n)
    d[(i, m)] = res
    return res


q = int(raw_input())
M = map(int, raw_input().split())
for m in M:
    if soleve(0, m, n):
        print 'yes'
    else:
        print 'no'

Measured memory_bytes: 9756
#ABC 128C
def main():
    import sys
    import itertools as ite
    N, M = map(int, sys.stdin.readline().split())
    Light = [tuple(map(int, sys.stdin.readline().split())) for _ in range(M)]
    P = list(map(int, sys.stdin.readline().split()))
    S = list(ite.product([0,1], repeat=N)) #スイッチの状態
    ans = 0
    for s_state in S:
        L_result = [0 for _ in range(M)]
        for m in range(M):
            for s_no in Light[m][1:]:
                L_result[m] += s_state[s_no-1]
        L_result = list(map(lambda x:x%2, L_result))
        if P == L_result:
            ans +=1
    print(ans)

if __name__=='__main__':
    main()


Measured memory_bytes: 3316
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define mem(x,y)		(x*)malloc(sizeof(x)*(y));

int main()
{
	int r,c,i,j,k;
	int* list;
	int** data;
	int* count;
	while(scanf("%d %d",&r,&c),r)
	{
		if(r == 1)
			printf("%d\n",c);
		else
		{
			list = mem(int,2 << (r-1));
			count = mem(int,2 << (r-1));
			data = mem(int*,r);
			for(i = 0;i < r;i++)
			{
				data[i] = mem(int,c);
				for(j = 0;j < c;j++)
				{
					scanf("%d",&data[i][j]);
				}
			}
			for(i = 0;i < c;i++)
			{
				int num = 0;
				for(j = 0;j < r;j++)
				{
					num += data[j][i] << j;
				}
				list[num]++;
			}
			for(i = 0;i < (2 << (r-1));i++)
			{
				count[i] = 0;
			}
			const int half_r = r >> 1;
			for(i = 0;i < (2 << (r-1));i++)
			{
				for(j = 0;j < (2 << (r-1));j++)
				{
					int num = 0;
					for(k = 0;k < r;k++)
					{
						if(((~(j ^ i)) >> k) & 1)
						{
							num++;
						}
					}
					if(num <= half_r)
					{
						num = r-num;
					}
					count[i]+= num*list[j];
				}
			}
			int max = 0;
			for(i = 0;i < (2 << (r-1));i++)
			{
				if(max < count[i])
				{
					max = count[i];
				}
			}
			printf("%d\n",max);
		}
	}
	return 0;
}

Measured memory_bytes: 1080
// clang-format off
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(ll i=0, i##_len=(n); i<i##_len; ++i)
#define rep1(i,n) for(ll i=1, i##_len=(n); i<=i##_len; ++i)
#define rrep(i,n) for(ll i=n-1; i>=0; --i)
#define rrep1(i,n) for(ll i=n; i>0; --i)
#define all(x) (x).begin(),(x).end()
#define SORT(x) sort(all(x))
#define SORT_DESC(x) sort(all(x), greater<>())
#define pb push_back
#define mp make_pair
#define coutl cout<<fixed<<setprecision(10)
#define ANS(ans) cout<<ans<<endl;
#define INF 1e9;
#define LINF 1e18;
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;}
// clang-format on

void solve(long long X, long long t) {
  ANS(max(X - t, 0LL));
}

// clang-format off
int main() {
  long long X;
  scanf("%lld",&X);
  long long t;
  scanf("%lld",&t);
  solve(X, t);
  return 0;
}


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

int main(void) {
    string S;
    cin >> S;

    reverse(S.begin(), S.end());
    int size = S.size();
    int cnt[2020] = {0};
    int m = 2019;
    int amari = 0;
    long result = 0;
    int x = 1;

    cnt[0]++;
    for (int i = 0; i < size; i++) {
        amari = amari + (S[i] - '0') * x;
        amari %= m;
        result += cnt[amari];
        cnt[amari]++;
        x = x * 10 % m;
    }

    cout << result << endl;
    return 0;
}

Measured memory_bytes: 3676
// Template Headers {{{
#include <bits/stdc++.h>
using namespace std;

using pii = pair<int, int>; using vpii = vector<pii>;
using vi = vector<int>; using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>; using vpll = vector<pll>;
using vll = vector<long long>; using vvll = vector<vll>;
template <class T> using min_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> istream &operator>>(istream &, vector<T> &);
template <class T> ostream &operator<<(ostream &, const vector<T> &);
template <class T, class U> istream &operator>>(istream &, pair<T, U> &);
template <class T, class U> ostream &operator<<(ostream &, const pair<T, U> &);
template <class T> struct Inf { static constexpr T inf() { return std::numeric_limits<T>::has_infinity ? std::numeric_limits<T>::infinity() : std::numeric_limits<T>::max(); } };
template <> struct Inf<int> { static constexpr int inf() { return 0x3f3f3f3f; } };
template <> struct Inf<long long> { static constexpr long long inf() { return 0x3f3f3f3f3f3f3f3fLL; } };
constexpr int INF = Inf<int>::inf(); constexpr ll BINF = Inf<ll>::inf();
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// }}}
// Preprocessing & "Global" variables {{{
struct Solver {
	Solver() {}
	int solve();
};
// }}}


int Solver::solve() {
	int n, k; cin >> n >> k;
	vi a(n); cin >> a;
	int lo = 1, hi = 1'000'000'000;
	while (lo < hi) {
		int mi = lo + (hi - lo) / 2;
		ll cuts = 0;
		for (int x : a) cuts += (x + mi - 1) / mi - 1;
		if (cuts <= k)
			hi = mi;
		else
			lo = mi + 1;
	}
	cout << lo << endl;
	return 0;
}

// Template Main {{{
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	Solver solver;
	#ifdef JUDGE_MULTI_CASE
		int t; cin >> t; for (int i = 1; i <= t; i++)
	#endif
	solver.solve();
	// cout << "Case #" << i << ": ", solver.solve();
	return 0;
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
	for (auto it=v.begin();it!=v.end();++it) is >> *it;
	return is;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
	for (auto it=v.begin();it!=v.end();) os << *it, os << " \n"[++it==v.end()];
	return os;
}

template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
	return is >> p.first >> p.second;
}

template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) {
	return os << p.first << ' ' << p.second;
}
// }}}

// vim: fdm=marker


Measured memory_bytes: 3996
#include <bits/stdc++.h>
using namespace std;
#define rep(i,cc,n) for(int i=cc;i<n;++i)
#define lrep(i,cc,n) for(long long i=cc;i<n;++i)
#define rrep(i,cc,n) for(long i=cc;i>n;--i)
using ll = long long;

int main() {
    int N;
    cin >> N;
    vector<pair<pair<string,int>,int>> rate(N);
    rep(i, 0, N){
        string S;
        int score;
        cin >> rate[i].first.first >> rate[i].first.second;
        rate[i].second = i + 1;
        rate[i].first.second *= -1;
    }
    sort(rate.begin(), rate.end());
    rep(i, 0, N){
        cout << rate[i].second << endl;
    }
}

Measured memory_bytes: 256
package main

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

// readi Scannerから1単語取り出して数値に変換して取得する
func readi(sc *bufio.Scanner) int {
	sc.Scan()
	t, _ := strconv.Atoi(sc.Text())
	return t
}

// readt Scannerから1単語取り出して取得する
func readt(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	A1 := readi(sc)
	A2 := readi(sc)
	A3 := readi(sc)
	if 22 <= A1+A2+A3 {
		fmt.Println("bust")
	} else {
		fmt.Println("win")
	}
}


Measured memory_bytes: 640
import math
A,B=map(int,input().split())
amin = A/0.08
amax = (A+1)/0.08
bmin = B/0.1
bmax = (B+1)/0.1

if bmin == amax or amin == bmax:
    print(-1)
else:
    ans = max(amin, bmin)
    ans = math.ceil(ans)
    if ans < amax and ans < bmax:
        print(ans)
    else:
        print(-1)


Measured memory_bytes: 61104
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
#define rg register
#define il inline
#define _ 200010
const int mod=1e9+7;
const int inf=2e9;
int qpow(int x, int y){
	int ans=1;
	while(y){
		if(y&1) ans=1ll*ans*x%mod;
		x=1ll*x*x%mod, y>>=1;
	}return ans;
}
int n, x, y, inv[_], sum[_], cnt[_], tot[_], fa[_];
int find(int u){return u^fa[u]?fa[u]=find(fa[u]): u;}
struct Ball{
	int c, w;
	bool operator < (const Ball &A) const{
		return c^A.c?c<A.c: w<A.w;
	}
}b[_], mn, sec;
vector<int> p[_];
int main(){
	scanf("%d%d%d", &n, &x, &y);
	for(rg int i=1; i<=n; ++i)
		scanf("%d%d", &b[i].c, &b[i].w);
	
	for(int i=1; i<=n; ++i) fa[i]=i;
	
	inv[0]=inv[1]=sum[0]=sum[1]=1;
	for(rg int i=2; i<=n; ++i)
		sum[i]=1ll*sum[i-1]*i%mod, inv[i]=qpow(sum[i], mod-2);
	sort(b+1, b+n+1);
	for(rg int i=1; i<=n; ++i)
		p[b[i].c].push_back(i);
	
	mn.w=sec.w=inf;
	for(rg int i=1; i<=n; ++i){
		if(!p[i].size()) continue;
		int now=p[i][0];
		if(p[i].size()&&b[now].w<sec.w){
			sec=b[now];
			if(sec.w<mn.w) swap(sec, mn);
		}
	}
	
	for(rg int i=1; i<=n; ++i){
		int lim=p[i].size();
		if(!lim) continue;
		int now, weig=b[p[i][0]].w;
		for(now=0; now<lim; ++now){
			bool f=false;
			int pllpp=b[p[i][now]].w;
			if(mn.c!=i&&mn.w+pllpp<=y)
				fa[find(i)]=find(mn.c), f=true;
			if(sec.c!=i&&sec.w+pllpp<=y)
				fa[find(i)]=find(sec.c), f=true;
			if(pllpp+weig<=x&&now) f=true;
			if(!f) break;
		}
		tot[i]=now;
	}
	
	int ans=1;
	for(int i=1; i<=n; ++i) cnt[find(i)]+=tot[i];
	for(int i=1; i<=n; ++i) ans=1ll*ans*sum[cnt[i]]%mod;
	for(int i=1; i<=n; ++i) (ans=1ll*ans*inv[tot[i]]%mod);
	printf("%d\n", ans);
}


Measured memory_bytes: 14720
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <fstream>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

int main() {
    ios::sync_with_stdio(false);
    ll a, b, k;
    cin >> a >> b >> k;
    if (a >= k) cout << a - k << ' ' << b;
    else if (a + b >= k) cout << 0 << ' ' << b - (k - a);
    else cout << 0 << ' ' << 0;
}


Measured memory_bytes: 3652
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>
#include <fstream>
#include <functional>
#define endl '\n'
#define ll long long
#define mod 1000000007
#define NCONS 200001
//#define NCONS 100001
//#define NCONS 1001
//#define NCONS 101
#define MCONS 2001
#define LIMIT 1000000000
#define TRUE 1
#define FALSE 0
#define toRadian(degree) ((degree) * (M_PI / 180.))
#define toDegree(radian) ((radian) * (180. / M_PI))
using namespace std;
struct Point{int x; int y;};
struct PPoint{Point x; Point y;};
ll gcd(ll a, ll b) { if(a < b) swap(a, b); if(b <= 0) return a; return gcd(b, a % b); }

vector<ll> vec;
ll diff;

ll get(int i)
{
    ll acc = 0;
    for(int j = 0; j < vec.size(); j++)
    {
        if(j == i) continue;
        acc += vec[j];
    }
    return acc;
}
int main(void)
{
    cin.tie(NULL); ios_base::sync_with_stdio(false);
    int N, L; cin >> N >> L;
    
    for(int i = 0; i < N; i++)
    {
        vec.push_back(L + i);
    }
    sort(vec.begin(), vec.end());
    ll total = 0;
    for(int i = 0; i < vec.size(); i++)
        total += vec[i];
    
    ll ans;
    diff = INT_MAX;
    ans = get(0);
    for(int i = 0; i < vec.size(); i++)
    {
        auto ret = get(i);
     //   cout << abs(ret - total) << " " << ret << endl;
        if(abs(ret - total) < diff)
        {
            diff = abs(ret - total);
            ans = ret;
        }

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


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int x,y;
int main (){
	cin >> x >> y;
	if(x==1||x==3||x==5||x==7||x==8||x==10||x==12){
		if(y==1||y==3||y==5||y==7||y==8||y==10||y==12)
			cout <<"Yes"<<endl;
		else cout << "No" << endl;
	}
	else if (x==4||x==6||x==9||x==11){
		if(y==4||y==6||y==9||y==11)
			cout << "Yes" << endl;
		else cout <<"No"<< endl;
	}
	else if(x==2){
		if (y==2) cout << "Yes" << endl;
		else cout << "No" << endl;
	}
}
	

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  string n;
  cin >> n;
  
  if (n[0] == n[1] && n[1] == n[2])
    cout << "Yes" << endl;
  else if (n[1] == n[2] && n[2] == n[3])
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Measured memory_bytes: 256
#include<stdio.h>
 
#define MAXLEN 20000
 
int garea,gareas[(MAXLEN/2)+1],gcnt,gend;
 
 
int getArea( char *inputs, int start, int end )
{
    int i;
    int ravine,water;
    
    gareas[gcnt]=ravine=water=0;
    for( i=start; i!=end; i++ ) {
        if( '\\'== *(inputs+i) ) {
           	water+=(ravine*2);
            ravine++;
            water++;
        } else if( 0 != ravine && '/' == *(inputs+i) ) {
        	ravine--;
        } else {
	     	water+=ravine;
        }       
    }
	gareas[gcnt]+=water;
    
    return gareas[gcnt];
}
  
int checkRavine( char *inputs, int start )
{
    int i;
    int ravine, minh, tmpi;
    
    ravine=0;
    tmpi=minh=MAXLEN;
    for( i=start; i<=gend && '\0'!=*(inputs+i); i++ ) {
        if( 0 <= ravine && '\\'== *(inputs+i) ) {
            ravine++;
        } else if( 0 != ravine && '/' == *(inputs+i) ) {
            ravine--;
            if( ravine < minh ) {
        		minh=ravine;
        		tmpi=i;
            }
            if( 0 == ravine ) {
            	garea+=getArea( inputs, start, i+1 );
            	gcnt++;
			    gend=MAXLEN;           	
                return i-start;
            }
        }
    }
	gend=tmpi;
    return 0;
}
 
 
void getAreas( char *inputs )
{
    int i,next;
     
    next=0;
    for( i=0; '\0'!=inputs[i]; i++ ) {
    	if( '_' !=inputs[i] || '/' !=inputs[i] ) {
	        next=checkRavine( inputs, i );
	        i+=next;
        }
    }
 
    return;
}
 
int main( void ) 
{
    char inputs[MAXLEN+1];
    int i;
 
    scanf( "%s", inputs );
    garea=gcnt=0;
    gend=MAXLEN;
    getAreas( inputs );
     
    printf( "%d\n", garea );
    
    if( gcnt ) {
        printf( "%d ", gcnt );
        for( i=0; i<gcnt; i++ ) {
            printf( "%d", gareas[i] );
            if( i < gcnt-1 ) {
            	printf( " " );  
            }
        }
        printf( "\n" );
       
    } else {
        printf( "0\n" );
    }
     
    return 0;
}

Measured memory_bytes: 576
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef int ll;
typedef long double ld;
const ll N = 100005;
char en = '\n';
ll inf = 1e16;
ll mod = 1e9 + 7;
ll power(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  while (n) {
    if (n & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  string s, t;
  cin >> s >> t;
  ll res = 0;
  for (ll i = 0; i < s.length(); i++) {
    res += (s[i] == t[i]);
  }
  cout << res << en;

  return 0;
}

Measured memory_bytes: 256
/*
 *  author: chw0501
 *  reference: 
 *  created: 18.04.2020 06:22:13(UTC)
*/
#include <bits/stdc++.h>

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

#define INF 1000000000000000000

/*dp[i][j]: i~j까지의 슬라임을 하나로 합치는데 필요한 최소 비용*/
ll dp[401][401];
int n;
ll arr[401];
ll sum[401];

ll solve(int i,int j){
	ll& ret=dp[i][j];
	if(ret!=-1ll) return ret;
	if(i==j) return ret=0;
	if(i+1==j) return ret=(arr[i]+arr[j]);
	ret=INF;
	for(int k=i;k<j;k++){
		ret=min(ret,solve(i,k)+solve(k+1,j)+sum[j]-sum[i-1]);
	}
	return ret;
}

int main(){
	cin>>n;
	memset(dp,-1ll,sizeof(dp));
	for(int i=1;i<=n;i++){
		scanf("%lld",arr+i);
		sum[i]=sum[i-1]+arr[i];
	}
	cout<<solve(1,n);	
	return 0;
}

Measured memory_bytes: 1536
#include <bits/stdc++.h>
#define REP(i, e) for(int (i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for(int (i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define PRINT(x) cout << (x) << "\n"
using namespace std;
using ll = long long; using pint = pair<int, int>; using pll = pair<ll, ll>;
const long long MOD = 1000000007;

ll N;
ll color[100010];
vector<pll> adj[100010];

void dfs(ll i, ll d, ll c){
    if(color[i] != -1) return;
    if(d % 2 == 0) color[i] = c;
    else color[i] = 1 - c;
    for(auto p : adj[i]){
        dfs(p.first, p.second, color[i]);
    }
}

signed main(){
    cin >> N;
    ll u, v, w;
    REP(i, N - 1){
        cin >> u >> v >> w;
        u--; v--;
        adj[u].push_back(pll(v, w));
        adj[v].push_back(pll(u, w));
    }
    REP(i, N) color[i] = -1;
    dfs(0, 0, 0);
    REP(i, N) PRINT(color[i]);
    return 0;
}

Measured memory_bytes: 13260
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;

int gcd(int a, int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a, int b){
    return a/gcd(a,b)*b;
}


signed main(void) {
    int n;
    cin >> n;
    vp in(n);
    rep(i,n)cin >> in[i].first >> in[i].second;
    int pos = abs(in[0].first) + abs(in[0].second);
    pos %= 2;
    rep(i,n){
        int t = abs(in[i].first) + abs(in[i].second);
        if(t%2 != pos){
            cout << -1 << endl;
            return 0;
        }
    }
    vi d;
    for(int i = 30; i >= 0; i--)d.push_back(1LL<<i);
    if(pos%2 == 0)d.push_back(1);

    cout << d.size() << endl;
    rep(i,d.size())cout << d[i] << " ";cout << endl;

    rep(i,n){
        int xt = in[i].first, yt = in[i].second;
        int x = 0, y = 0;
        rep(j,d.size()){
            if(abs(x - xt) >= abs(y - yt)) {
                if(x >= xt) x -= d[j], cout << "L";
                else x += d[j], cout << "R";
            }else{
                if(y >= yt) y -= d[j], cout << "D";
                else y += d[j], cout << "U";
            }
        }
        cout << endl;
    }
}


Measured memory_bytes: 256