akhauriyash/Code-Regression huggingface dataset:
import java.util.*;

public class Main {
  
  public static void main(String[] args) {
    try(Scanner sc = new Scanner(System.in)) {
      int N = sc.nextInt();
      String S = sc.next();
      char[] s = S.toCharArray();
      int answer = 0;
      for ( int i = 1; i < N-1; i++ ) {
        Set<Character> xSet = toCharacterSet(s, 0, i);
        Set<Character> ySet = toCharacterSet(s, i, N);
        Set<Character> pivotSet = null;
        Set<Character> counterSet = null;
        if ( xSet.size() < ySet.size() ) {
          pivotSet = xSet;
          counterSet = ySet;
        }
        else {
          pivotSet = ySet;
          counterSet = xSet;
        }
        int count = 0;
        for ( Character c : pivotSet ) {
          if ( counterSet.contains(c) ) {
            count++;
          }
        }
        if ( count > answer ) {
          answer = count;
        }
      }
      System.out.println(answer);
    }
  }
  
  private static Set<Character> toCharacterSet(char[] s, int start, int end) {
    Set<Character> ret = new HashSet<Character>();
    if ( s == null ) {
      return ret;
    }
    for ( int i = start; i < end; i++ ) {
      ret.add(s[i]);
    }
    return ret;
  }
}

Measured memory_bytes: 23764
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) for (int i = (int)(n-1); i >= 0; i--)
#define sz(x) int(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define INF 2e9
#define LINF 1e18
#define mod 1e9+7
template<class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> P;

int fact(int n) {
    if (n == 0) return 1;
    else return n*fact(n-1);
}

int main() {
    int n; cin >> n;
    vector<int> p(n), q(n);
    rep(i,n) {
        cin >> p[i];
    }
    rep(i,n) {
        cin >> q[i];
    }
    vector<int> a(n);
    rep(i,n) a[i] = i+1;

    map<vector<int>, int> mp;
    do {
      mp[a] = mp.size();
    } while (next_permutation(all(a))); //next_permutation...aを辞書順で次のものに置き換える

    int ans = abs(mp[p] - mp[q]);
    cout << ans << endl;
}

Measured memory_bytes: 8648
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;


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

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)

void mainmain(){
	vint v(3);
	rep(i,3) cin>>v[i];
	sort(ALL(v));
	vint w={5,5,7};
	if(v == w){
		cout<<"YES"<<endl;
	}
	else{
		cout<<"NO"<<endl;
	}
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(20);
    mainmain();
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, c) for (int i = 0; i < (int)c; i++)
int main() {
    int n;
    scanf("%d", &n);
    vector<int> vec(n);
    rep(i, n) {
        scanf("%d", &vec[i]);
    }

    vector<int> even(100010), odd(100010);
    rep(i, n) {
        if(i % 2 == 0) even[vec[i]]++;
        else odd[vec[i]]++;
    }

    int max_even = max_element(even.begin(), even.end()) - even.begin();
    int max_odd = max_element(odd.begin(), odd.end()) - odd.begin();

    int ans = 0;
    if(max_even != max_odd) {
        ans = (n / 2 - even[max_even]) + (n / 2 - odd[max_odd]);
        printf("%d\n", ans);
    }
    else {
        sort(even.rbegin(), even.rend());
        sort(odd.rbegin(), odd.rend());
        ans = min((n / 2 - even[0]) + (n / 2 - odd[1]), (n / 2 - even[1]) + (n / 2 - odd[0]));
        printf("%d\n", ans);
    }
    return 0;
}

Measured memory_bytes: 4628
#include<iostream>
using namespace std;
typedef long long lolo;
void solve(){
  lolo ans;
  lolo next;
  char ope;
  cin >> ans;
  while(true){
    cin >> ope;
    if(ope == '=')
      break;
    cin >> next;
    switch(ope){
      case '+' : ans += next; break;
      case '-' : ans -= next; break;
      case '*' : ans *= next; break;
      case '/' : ans /= next; break;
    }
  }
  cout << ans << endl;
}
int main(){
  solve();
}



Measured memory_bytes: 3112
n = int(input())
h = [list(map(int, input().split()))]
ans = 0


# hがあるうちは回る
while (len(h)):
    now_h = h.pop()
    min_k = min(now_h)
    ans += min_k
    tmp = []
    for i in range(len(now_h)):
        now = now_h[i] - min_k
        if now == 0:
            if len(tmp):
                h.append(tmp)
                tmp = []
        else:
            tmp.append(now)
    if len(tmp):
        h.append(tmp)

print(ans)


Measured memory_bytes: 3064
import itertools
n = int(input())
pis = tuple([int(pi) for pi in input().split()])
qis = tuple([int(qi) for qi in input().split()])
perms = list(itertools.permutations(range(1,n+1)))

pn = -1
qn = -1
for p in perms:
  if pis == p:
    pn = perms.index(pis)
    
  if qis == p:
    qn = perms.index(qis)
    
  if pn > -1 and qn > -1:
    break
print(abs(pn - qn))


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

const int LMT = 100;
const int N = 105;

int arr[N];
int n;

int sqr(int x) {
	return x * x;
}

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

	int ans = 1e9;
	
	for(int i = -LMT ; i <= LMT ; i++) {
		int tot = 0;
		for(int j = 0 ; j < n ; j++)
			tot += sqr(arr[j] - i);
		ans = min(ans,tot);
	}

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

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
#define FOR(I,X,Y) for(long long (I)=(X);(I)<(Y);(I)++)
#define REP(I,X,Y) for(long long (I)=(Y)-1;(I)>=(X);(I)--)
#define ALL(X) (X).begin(),(X).end()
#define pb push_back
#define COUNT(V,X) upper_bound((V).begin(),(V).end(),X)-lower_bound((V).begin(),(V).end(),X)
#define debug(x0123456789) cerr<<#x0123456789<<':'<<x0123456789<<endl;
#define DEBUG(v) cerr<<#v<<':';for(auto x0123456789:v)cerr<<x0123456789<<' ';cerr<<endl;
#define INF 1000000007
#define LINF 1000000000000000007
#define EPS 1e-9
typedef long long ll;
typedef long double ld;

signed main(){
    ll N;
    cin >> N;
    ll p[N];
    FOR(i,0,N)cin >> p[i];
    ll ans = 0;
    ll MAX = 0;
    FOR(i,0,N){
        ans += p[i];
        MAX = max(MAX,p[i]);
    }
    cout << ans-MAX/2 << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <iterator>
#include <stack>
#include <string.h>
#include <cstdlib>
#include <queue>
#include <list>
#include <string>

using namespace std;

int n;
vector<vector<pair<int,long long>>> x(100005);
bool color[100005] = {0};
bool checked[100005] = {0};



void dfs(int k)
{
    if (checked[k] == 1){
        return;
    }
    
    checked[k] = 1;
    
    for (auto& i : x[k]){
        if (i.second%2 == 0){
            color[i.first] = color[k];
        }
        else {
            color[i.first] = !color[k];
        }
        dfs(i.first);
    }
}

int main()
{
    cin >> n;
    long long w;
    int u,v;
    
    x.resize(n);
    for (int i = 0;i < n-1;i++){
        cin >> u >> v >> w;
        u--;
        v--;
        x[u].push_back(make_pair(v,w));
        x[v].push_back(make_pair(u,w));
    }

    color[0] = 0;
    dfs(0);
    
    for (int i = 0;i < n;i++){
        cout << color[i] << endl;
    }
}


Measured memory_bytes: 8832
K = int(input())

n = 7
ans = -1
i = 1
if K%2==0:
    ans = -1
else:
    for i in range(K*2+10):
        if n%K==0:
            ans = i+1
            break
        n *= 10
        n += 7
        n %= K
print(ans)

Measured memory_bytes: 63244
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include<string>
#include <stack>
#include<queue>
#include<map>
#include <stdlib.h>
#include <bits/stdc++.h>

using namespace std;




int main() {

	long long int n,a,b;
	cin>>n>>a>>b;
	if(a>b) {
		cout<<"0"<<endl;
		return 0;
	}
	if(a==b){
		cout<<"1"<<endl;
		return 0;
	}
	if(n-2<0){
		cout<<"0"<<endl;
			return 0;

	}
	cout<<(b-a)*(n-2)+1<<endl;

	return 0;
}


Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long MOD = 1e9 + 7, MAX = 1e18, larg = 1e5, INF = -1e9;
long long A, B, C, D, E, F, G, H, I, J, K, L, N, M, O, P, Q, R, S, T, U, V, W;
typedef long long ll;
typedef pair<ll, ll> Pa;

vector<Pa> to[100000];
ll dp[100000];

bool dfs(int curr, int prev) {
	bool flag = true;
	rep(i, to[curr].size()) {
		if (dp[to[curr][i].first] != MAX && dp[to[curr][i].first] != dp[curr] + to[curr][i].second) return false;
		if (dp[to[curr][i].first] != MAX) continue;
		dp[to[curr][i].first] = dp[curr] + to[curr][i].second;
		flag &= dfs(to[curr][i].first, curr);
	}
	return flag;
}

int main()
{
	cin >> N >> M;
	rep(i, M) {
		cin >> A >> B >> D;
		A--; B--;
		to[A].push_back(Pa(B, D));
		to[B].push_back(Pa(A, -1 * D));
	}
	rep(i, N) {
		dp[i] = MAX;
	}
	bool flag = true;
	rep(i, N) {
		if (dp[i] != MAX) continue;
		dp[i] = 0;
		flag &= dfs(i, -1);
	}
	cout << (flag ? "Yes" : "No");
}


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

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

  int sum = 0, mxi = 0;

  for (int i = 0; i < N; i++) {
    int P;
    cin >> P;
    sum += P, mxi = max(mxi, P);
  }

  cout << sum - mxi / 2 << "\n";
}

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

using namespace std;

int n;

void solve(){
    int i,j;
    string str;
    cin >> n;
    multiset<int> s;
    s.insert(-1);
    for(i = 0;i < n;i++){
        cin >> j;
        multiset<int>::iterator it = s.upper_bound(j-1);
        if(it == s.begin()){
            s.insert(j);
            continue;
        }
        else{
            s.erase(--it);
            s.insert(j);
        }
    }
    long long ans = s.size();
    cout << ans << endl;
}

int main(){
    solve();
}


Measured memory_bytes: 4992
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define Rep(n) for (ll _ = 0; _ < (ll)(n); _++)
#define P pair<ll,ll>
#define fs first
#define sc second
#define _GLIBCXX_DEBUG

template<class S>S sum(vector<S>&a){return accumulate(all(a),S());}
template<class S>S max(vector<S>&a){return *max_element(all(a));}
template<class S>S min(vector<S>&a){return *min_element(all(a));}
ll max(int a,ll b){return max((ll)a,b);} ll max(ll a,int b){return max(a,(ll)b);}
ll min(int a,ll b){return min((ll)a,b);} ll min(ll a,int b){return min(a,(ll)b);}
template <class S>
void print(vector<S> &v){
	for(ll i=0; i<(ll)v.size(); i++){
		cerr << v[i] << ' ';
	}
	cerr << endl;
}
template <class S>
void print(vector<vector<S>> &v) {
  	for (ll i = 0; i < (ll)v.size(); i++) {
    	for (ll j = 0; j < (ll)v[i].size(); j++) {
      		cerr << v[i][j] << ' ';
    	}
    	cerr << endl;
  	}
}

int main(){
	int n; cin >> n;
	int s = 0;
	vector<int> w(n);
	rep(i,n){
		int t;
		cin >> t;
		s += t;
		w[i] = t;
	}
	int ans = s;
	int a = 0;
	rep(i,n){
		a += w[i];
		ans = min(ans, abs(s-a-a));
	}
	cout << ans << endl;
}

Measured memory_bytes: 256
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
 
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<stdarg.h>
#include<malloc.h>


int IsPrime(int);

int p[1000001] = {0};

int main()
{
	int i, j, n, count, sum, buf[1000] = {0};
	for(i=2; i<=1000000; i++)
		p[i] = 1;
	j = 0;
	for(i=0; i<=1000; i++){
		if(IsPrime(i) == 1){
			buf[j] = i;
			j++;
		}
	}
	for(i=0; i<168; i++){
		for(j=2*buf[i]; j<=1000000; j+=buf[i])
			p[j] = 0;
	}

	while(1){
		scanf("%d", &n);
		if(n==0)
			break;
		sum=0;
		i=0;
		count=0;
		while(1){
			if(p[i] == 1){
				sum += i;
				count++;
			}
			if(count == n)
				break;
			i++;
		}
		printf("%d\n", sum);
	}

	return 0;
}


int IsPrime(int n)
{
    int i;
 
    if(n < 2)
        return 0;
    else if(n == 2)
        return 1;
 
    if(n % 2 == 0)
        return 0;
 
	for(i = 3; i * i <= n; i += 2){
         if(n % i == 0)
            return 0;
	}
    return 1;
}

Measured memory_bytes: 4508
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>

using namespace std;
#define int long long int
const int INF = 1001001001001001LL;
const int MOD = 1000000007;

signed main(){
    vector<int> a(6);
    for(int i = 0; i < 6; i++) cin >> a[i];
    if(a[4] - a[0] <= a[5]) cout << "Yay!" << endl;
    else cout << ":(" << endl;
    return 0;
}



Measured memory_bytes: 256
import qualified Data.ByteString.Char8 as BS

main = do
  a <- BS.unpack <$> BS.getLine
  b <- BS.unpack <$> BS.getLine
  let len_a = length a
  let tail_b = drop len_a b
  let b' = b !! 0
  if a == take (len_a) b && length tail_b == 1 && 'a' <= b' && b' <= 'z' then
    putStrLn $ "Yes"
  else
    putStrLn $ "No"

Measured memory_bytes: 3684
#include "bits/stdc++.h"
#define _USE_MATH_DEFINES
#include <math.h>
 
using namespace std;
 
using LL = long long;
using VLL = std::vector<LL>;
using VVLL = std::vector<VLL>;
using VVVLL = std::vector<VVLL>;
using LD = long double;
using VLD = std::vector<LD>;
using VVLD = std::vector<VLD>;
using VVVLD = std::vector<VVLD>;
 
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; }

#define REP(i,n) for(LL (i)=0;(i)<(n);(i)++)
#define ALL(v) v.begin(), v.end()
#define PRINTLN(v) {LL i=0; for(auto (a): v ){   cerr <<i << " : "<<  a << endl; i++;}}
#define PRINTSP(v) for(auto (a): v ) {cerr << a << " ";} cerr << endl
#define cmax(a,b) {if(a<b){a=b;}}
#define cmin(a,b) {if(a>b){a=b;}}
#define EACH(a,A) for(auto a: A)
//#define cmax(a,b) a = (a>b ? a:b)
//#define cmin(a,b) a = (a<b ? a:b) 



void func(){
  LL H,W;
  cin >> H >> W;
  vector<string> M(H);
  REP(i,H){
    cin >>M[i];
  }
  set<pair<LL,LL>> S;
  REP(i,H){
    REP(j,W){
      if(M[i][j]=='#'){
        S.emplace(i,j);
      }      
    }
  }

  LL cnt=0;
  while( not S.empty()){
    //cerr << cnt<< endl;
    EACH(m,M){
      //PRINTSP(m);
    }

    set<pair<LL,LL>> T;
    for(auto s : S){
      LL y = s.first;
      LL x = s.second;
      if(y+1<H and M[y+1][x]=='.'){
        M[y+1][x] = '#';
        T.emplace(y+1,x);
        //T.insert(make_pair(y+1,x));
      }
      if(x+1<W and M[y][x+1]=='.'){
        M[y][x+1] = '#';
        T.emplace(y,x+1);
        //T.insert(make_pair(y,x+1));
      }
      if(y-1>=0 and M[y-1][x]=='.'){
        M[y-1][x] = '#';
        T.emplace(y-1,x);
        //T.insert(make_pair(y-1,x));
      }
      if(x-1>=0 and M[y][x-1]=='.'){
        M[y][x-1] = '#';
        T.emplace(y,x-1);
        //T.insert(make_pair(y,x-1));
      }
    }
    S=T;
    cnt++;
  }
  EACH(m,M){
    //PRINTSP(m);
  }
  cout << cnt-1;
  return;
}
  
void _func(){
  LL H,W;
  cin >> H >> W;
  vector<string> M(H);
  REP(i,H){
    cin >>M[i];
  }

  

  queue<pair<LL,LL>> Q;
  set<pair<LL,LL>> S;
  REP(i,H){
    REP(j,W){
      if(M[i][j]=='#'){
        Q.emplace(i,j);
        S.emplace(i,j);
      }      
    }
  }

  LL cnt=0;
  while( not S.empty()){
    cerr << cnt<< endl;
    EACH(m,M){
      PRINTSP(m);
    }

    set<pair<LL,LL>> T;
    for(auto s : S){
      LL y = s.first;
      LL x = s.second;
      if(y+1<H and M[y+1][x]=='.'){
        M[y+1][x] = '#';
        T.emplace(y+1,x);
      }
      if(x+1<W and M[y][x+1]=='.'){
        M[y][x+1] = '#';
        T.emplace(y,x+1);
      }
      if(y-1>=0 and M[y-1][x]=='.'){
        M[y-1][x] = '#';
        T.emplace(y-1,x);
      }
      if(x-1>=0 and M[y][x-1]=='.'){
        M[y][x-1] = '#';
        T.emplace(y,x-1);
      }
    }
    S=T;
    cnt++;
  }
  EACH(m,M){
    //PRINTSP(m);
  }
  cout << cnt-1;
  return;
}

 
int main()
{
  func();
  return 0;
}
 

Measured memory_bytes: 65024
n = int(input())
s = input()
r = g = b = 0
for i in range(n):
  if s[i] == 'R':
    r += 1
  elif s[i] == 'G':
    g += 1
  else:
    b += 1
res = r * g  * b
for i in range(n):
  for j in range(i+1, n):
    k = 2 * j - i
    if 0 <= k < n and s[i] != s[j] and s[j] != s[k] and s[i] != s[k]:
      res -= 1
print(res)

Measured memory_bytes: 9124
#include <iostream>
#include <algorithm>
#include <vector>

template <class Cost = int>
struct Edge {
    int from, to;
    Cost cost;
    Edge(int from = -1, int to = -1, Cost cost = 1)
        : from(from), to(to), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};
template <class Cost = int>
using Graph = std::vector<std::vector<Edge<Cost>>>;

const int INF = std::numeric_limits<int>::max() / 3;

using namespace std;


Graph<> tree;
int A, B;

// Aを求める
void dfs1(int v, int r) {
    int ch = 0;
    for (auto e : tree[v]) {
        if (e.to == r) continue;
        ++ch;
        dfs1(e.to, v);
    }
    A += ch / 2;
}

// パスの最大長がB以下になるようにしたとき、
// 親に送られるパスの最小値(無理ならINF)を返す
int dfs2(int v, int r) {
    vector<int> ch;
    for (auto e : tree[v]) {
        if (e.to == r) continue;
        ch.push_back(dfs2(e.to, v) + 1);
        if (ch.back() > B) return INF;
    }

    // ダミーを仕込んで奇数の場合に帰着させる
    if (ch.size() % 2 == 0) ch.push_back(0);

    sort(ch.begin(), ch.end());

    int ok = ch.size(), ng = -1;
    // ok番目抜きで、和の最大値がB以下になるようマッチングできる
    while (ok - ng > 1) {
        int mid = (ok + ng) / 2;

        bool judge = true;
        for (int l = 0, r = ch.size() - 1; l < r; ++l, --r) {
            if (l == mid) ++l;
            if (r == mid) --r;
            if (ch[l] + ch[r] > B) judge = false;
        }

        (judge ? ok : ng) = mid;
    }

    return (ok == ch.size() ? INF : ch[ok]);
}

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

    tree = Graph<>(N);
    for (int i = 0; i < N - 1; ++i) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        tree[u].emplace_back(u, v);
        tree[v].emplace_back(v, u);
    }

    int r;
    for (r = 0; tree[r].size() > 1; ++r) {}

    A = 0;
    dfs1(r, -1);
    ++A;  // 根から出てくるパスを受け取る

    // Bを二分探索
    int ok = N, ng = -1;
    while (ok - ng > 1) {
        int mid = (ok + ng) / 2;
        B = mid;
        (dfs2(r, -1) <= mid ? ok : ng) = mid;
    }

    cout << A << " " << ok << endl;
    return 0;
}


Measured memory_bytes: 15104
#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;
typedef pair<int, int> P;

signed main(){
    int n;
    while(1){
        cin >> n;
        if(n == 0) break;
        int sum = 0;
        vector<int> p(n), j(n - 1);
        rep(i, 0, n){
            cin >> p[i];
            sum += p[i];
        }
        rep(i, 0, n - 1){
            cin >> j[i];
            sum += j[i];
        }
        sort(all(j));
        int ans = sum;
        rep(i, 0, n - 1){
            sum -= j[i];
            ans = max(sum * (i + 2), ans);
        }
        cout << ans << endl;
    }
}

Measured memory_bytes: 3888
import Control.Applicative

solver :: Int -> Int -> Int -> Maybe Int
solver a f b =
  let s = b - a
  in
      if s <= 0
        then Just 0
        else
          if f >= s
            then Just s
            else
              Nothing

main :: IO()
main = do
  (a:f:b:_) <- map (read :: String -> Int) . words <$> getLine
  let ans = solver a f b
  maybe (putStrLn "NA") print ans

Measured memory_bytes: 3636
import bisect

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

A.sort()
B.sort()
C.sort()

ans = 0
for b in B:
    i_a = bisect.bisect_left(A, b)
    i_c = bisect.bisect_right(C, b)
    ans += i_a * (N - i_c)
print(ans)


Measured memory_bytes: 23360
#include <bits/stdc++.h>
using namespace std;
 
int main() {
int N, M;
  cin >> N >> M;
  vector<int> A(60);
  vector<int> B(60);
  
  for(int i = 0; i < M; i++){
    cin >> A.at(i) >> B.at(i);
  }
  vector<int> backets(60);
  for(int i = 0; i < M; i++){
   backets.at(A.at(i)-1)++; 
    backets.at(B.at(i)-1)++;
  }
  for(int i = 0; i < N; i++){
   cout << backets.at(i) << endl;
  }
}


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

using namespace std;

#define ll long long
#define db double
#define clr(a, b) memset(a, b, sizeof(a))

const int maxn = 5e5 + 10;
const int inf = 0x7ffffff;

void solve()
{
    int ans = 0;
    int N;
    cin >> N;
    ans += N / 500 * 1000;
    N %= 500;
    ans += N / 5 *5;
    cout << ans << endl;

}

int main()
{
    //freopen("out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T = 1;
    //cin >> T;
    while(T-- > 0)
        solve();

}

Measured memory_bytes: 256
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=sys.stdin.readline
def resolve():
    n,m=map(int,input().split())
    switch=[0]*n
    for i in reversed(range(m)):
        k,*S=map(int,input().split())
        for s in S:
            switch[s-1]+=1<<i
    p=int(input().replace(' ',''),2)
    # calculate
    ans=0
    for k in range(2**n):
        xor=0
        for i in range(n):
            if k&(1<<i):
                xor^=switch[i]
        if xor==p: ans+=1
    print(ans)
resolve()

Measured memory_bytes: 39792
#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 INF (1LL<<59)
#define MV 200001
#define LMV 21
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define emp emplace
#define whoami(x) cerr<<#x<<" "<<x<<"\n";
#define mp make_pair
#define ins insert
#define sz size

void FLASH() {ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);}
void SETF() {cout.ios_base::setf(ios_base::fixed);}
void UNSETF() {cout.ios_base::unsetf(ios_base::fixed);}

typedef long long ll;
typedef long double ld;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef pair<PII, int> PPII;
typedef pair<PLL, ll> PPLL;
typedef map<int, int> MII;

const int MOD = 1000000007;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

struct h_llint {
  static uint64_t splitmix64(uint64_t x) {    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

struct h_pair{
  size_t operator()(const PLL&x)const{
    return hash<ll>()(((ll)x.ff)^(((ll)x.ss)<<32));
  }
};

typedef map<ll, ll> MLL;
typedef map<PII, int> MPII;
typedef map<PLL, ll> MPLL;
typedef set<int> SI;
typedef set<ll> SL;
//ordered_set = order_of_key(.)
//ordered_set = find_by_order(.)
typedef ordered_set<int> OSI;
typedef ordered_set<ll> OSL;
typedef ordered_multiset<int> MOSI;
typedef ordered_multiset<ll> MOSL;
typedef unordered_map<ll, int, h_llint> UMLI;
typedef unordered_map<ll, ll, h_llint> UMLL;
typedef unordered_map<PLL, int, h_pair> UMPI;
typedef unordered_map<PLL, ll, h_pair> UMPL;

int ar[MV];
ll arr[MV];

void solve(int T)
{
	int s,w;
	cin>>s>>w;
	cout<<((w >= s) ? "unsafe\n" : "safe\n");
	return;
}

int main(void)
{
	FLASH();
	int T;
	T = 1;

// #ifndef ONLINE_JUDGE
// 	time_t time_t1, time_t2;
// 	time_t1 = clock();
// #endif

	//cin>>T;
	while(T--)
	solve(T);

// #ifndef ONLINE_JUDGE
// 	time_t2 = clock();
// 	SETF();
// 	cout<<"Time taken: "<<setprecision(7)<<(time_t2 - time_t1)/(double)CLOCKS_PER_SEC<<"\n";
// 	UNSETF();
// #endif

	return 0;
}

Measured memory_bytes: 3648
N = int(input())
ans = [0] * (N+1)

for x in range(1, 101):
    for y in range(1, 101):
        for z in range(1, 101):
            f = x ** 2 + y ** 2 + z ** 2 + x*y + y*z + z*x
            if f > N:
                break
            else:
                ans[f] += 1

print(*ans[1:], sep = '\n')

Measured memory_bytes: 68824
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int M = sc.nextInt();
		long[] A = new long[N];
		
		for(int i = 0; i < N; i++) {
			A[i] = sc.nextLong();
		}
		
		long[][] x = new long[M][2];
		
		for(int i = 0; i < M; i++) {
			x[i][0] = sc.nextLong();
			x[i][1] = sc.nextLong();
		}
		
		Arrays.sort(x, (a1, a2) -> Long.compare(a1[1], a2[1]));
		Arrays.sort(A);
		
		long[] ans = new long[N];
		int num = 0;
		
		for(int i = M - 1; i >= 0; i--) {
			while(x[i][0] >= 1 && num <= N - 1) {
				ans[num] = x[i][1];
				x[i][0]--;
				num++;
			}
			if(num > N - 1) {
				break;
			}
		}
		
		for(int i = N - 1; i >= 0; i--) {
			if(A[i] > ans[i]) {
				ans[i] = A[i];
			} else {
				break;
			}
		}
		
		long sum = 0;
		
		for(int i = 0; i < N; i++) {
			sum += ans[i];
		}
		
		System.out.println(sum);
	}
}


Measured memory_bytes: 99040
using System;

class ITP1_7_A{
    static void Main(string[] args){
        int middle,period,add;
        middle = period = add=0;
        while(true){
            string[] str = Console.ReadLine().Split(' ');
            middle = int.Parse(str[0]);
            period = int.Parse(str[1]);
            add = int.Parse(str[2]);
            if(middle+period+add==-3) break;
            int total = middle + period;
            if((middle == -1)||(period == -1)){
                Console.WriteLine("F");
            } else if(total >= 80){
                Console.WriteLine("A");
            } else if(total >= 65){
                Console.WriteLine("B");
            } else if(total >= 50){
                Console.WriteLine("C");
            } else if(total >= 30){
                if(add >= 50){
                    Console.WriteLine("C");
                } else {
                    Console.WriteLine("D");
                }
            } else if(total < 30){
                Console.WriteLine("F");
            }
        }
    }
}


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

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());

            string[] tmp = Console.ReadLine().Split(' ');

            double[] valArray = new double[3 * N];

            for(int i=0;i<3* N; i++)
            {
                valArray[i] = double.Parse(tmp[i]);
            }

            Array.Sort(valArray);
            Array.Reverse(valArray);

            double sum = 0;

            for(int i=0;i< N;i++)
            {
                sum += valArray[1 + 2 * i];
            }

            Console.WriteLine(sum);
            Console.ReadLine();
        }
    }
}


Measured memory_bytes: 37836
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
#define rep(i,s,e) for(int i=s;i<e;i++)
int main(){
    int n;cin>>n;
    int A[n+1];
    rep(i,0,n+1)A[i]=0;
    rep(i,0,n-1){
        int a;cin>>a;
        A[a]++;
    };
    rep(i,1,n+1){
        cout<<A[i]<<endl;
    }

}


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

#define pb push_back
#define fi first
#define se second
typedef long long ll;

bool f(string s)
{
	int n = (int)s.length();
	for (int i = 0; i < n / 2; ++i)
		if (s[i] != s[n - i - 1])
			return false;
	return true;
}

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

	string s;
	cin >> s;
	int n = (int)s.length();

	string ss = s.substr(0, (n - 1) / 2);
	string sss = s.substr((n + 3) / 2 - 1, string::npos);
	
	if (f(s) && f(ss) && f(sss))
		cout << "Yes\n";
	else
		cout << "No\n";
}


Measured memory_bytes: 256
#include <stdio.h>

int main(void){

    int  a, b, c, d;

    scanf("%d%d%d%d", &a, &b, &c, &d);

    while(1){
        c -= b;
        if(c <= 0){
            printf("Yes\n");
            break;
        }
        a -= d;
        if(a <= 0){
            printf("No\n");
            break;
        }

    }
    return 0;
}

Measured memory_bytes: 1660
#include <cstdio>
using namespace std;

char c[40][16];

bool check(int n, int m, int d, int x){
	for(int i = 1; i <= d; ++i){
		if(c[i][m] == '1'){ ++m; }
		else if(c[i][m-1] == '1'){ --m; }
	}
	return m == x;
}

bool solve(){
	int n, m, x, d;
	if(scanf("%d%d%d%d", &n, &m, &x, &d) < 4){ return false; }
	for(int i = 1; i <= d; ++i){
		scanf(" %s", c[i] + 1);
	}

	if(check(n, m, d, x)){
		puts("0");
		return true;
	}
	
	for(int i = 1; i <= d; ++i){
		for(int j = 1; j < n; ++j){
			if(c[i][j] != '1' && c[i][j-1] != '1' && c[i][j+1] != '1'){
				c[i][j] = '1';
				if(check(n, m, d, x)){
					printf("%d %d\n", i, j);
					return true;
				}
				c[i][j] = '0';
			}
		}
	}
	
	puts("1");
	return true;
}

int main(){
	while(solve());
}

Measured memory_bytes: 1040
#include <stdio.h>
int main(){
  int a,b,H,W;
  while(1){
  scanf("%d %d",&H,&W);
  if( H == 0 && W == 0){ break; }
  for( a = 0 ; a < H ; a++){
    if( a % 2 == 0){
    for(b = 0 ; b < W ; b++){
      if( b % 2 == 0){
	printf("#");
      }
      else {
         printf(".");
      }
    }
    }
	else{
    for(b = 0 ; b < W ; b++){
      if( b % 2 == 0){
	printf(".");
      }
	else {
           printf("#");
	}
	}
    }
      printf("\n");
    }
    printf("\n");
    }

  return 0;
}

Measured memory_bytes: 572
import sys
from io import StringIO
import unittest
import os

# 再帰処理上限(dfs作成時に設定するのが面倒なので限度近い値を組み込む)
sys.setrecursionlimit(999999999)


# 実装を行う関数
def resolve(test_def_name=""):
    x, k, d = map(int, input().split())

    sho, amari = divmod(abs(x), d)

    if sho > k:
        if x < 0:
            ans = x + (k * d)
        else:
            ans = x - (k * d)
        print(abs(ans))
        return

    if x < 0:
        x = -amari
    else:
        x = amari

    k -= sho

    if k % 2 == 1:
        ans = min(abs(x + d), abs(x - d))
    else:
        ans = x

    print(abs(ans))


# テストクラス


class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """6 2 4"""
        output = """2"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """7 4 3"""
        output = """1"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """10 1 2"""
        output = """8"""
        self.assertIO(test_input, output)

    def test_input_4(self):
        test_input = """1000000000000000 1000000000000000 1000000000000000"""
        output = """1000000000000000"""
        self.assertIO(test_input, output)

    # 自作テストパターンのひな形(利用時は「tes_t」のアンダーバーを削除すること
    def test_1original_1(self):
        test_input = """7 3 3"""
        output = """2"""
        self.assertIO(test_input, output)


# 実装orテストの呼び出し
if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        # AtCoder提出時の場合
        resolve()

    else:
        # 自PCの場合
        unittest.main()


Measured memory_bytes: 70668
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <time.h>
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000001
#define mod 1000000009
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)

int main()
{
    string s;
    cin >> s;
    int flag = 0;
    for(int i = s.size() - 1;i >= 0;i--){
        if(s[i] == 'F' && flag == 0){
            flag = 1;
        }
        if(s[i] == 'C' && flag == 1){
            flag = 2;
        }
    }
    if(flag == 2)cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0;i<(int)(n);i++)
#define all(x)    (x).begin(),(x).end()
typedef long long ll;
const int MOD = (int)1e9 + 7;

int main(){
  int n,k,q;
  cin >> n >> k >> q;
  vector<int> points(n,0);
  rep(i,q){
    int buf;
    cin >> buf;
    points[buf-1]++;
  }
  rep(i,n){
    cout<<(k-q+points[i]>0? "Yes" : "No") << endl;
  }
}



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

vector<int> v[252], rev[252];
vector<bool> goal(252), way(252);

void abletogoal(int i){
    goal[i] = true;
    for(int next : rev[i]){
        if(!goal[next]) abletogoal(next);
    }
}

bool f(int i){
    bool ret = true;
    way[i] = true;
    ret &= goal[i];
    for(int next : v[i]){
        if(way[next])   continue;
        ret &= f(next);
    }
    return ret;
}

int main(){
    int m;
    while(cin >> m, m){
        for(int i = 0; i < 252; i++){
            v[i].clear(), rev[i].clear();
            goal[i] = way[i] = false;
        }

        int n;
        cin >> n;
        vector<int> d(n+2);
        for(int i = 1; i <= n; i++) cin >> d[i];
        for(int i = 0; i <= n; i++){
            for(int j = 1; j <= m; j++){
                int ni = i+j;
                if(ni > n+1)    ni = n+1;
                else            ni += d[ni];
                if(ni > n+1)    ni = n+1;
                if(ni < 0)  ni = 0;
                v[i].push_back(ni);
                rev[ni].push_back(i);
            }
        }
        abletogoal(n+1);
        cout << (f(0) ? "OK" : "NG") << endl;
    }
    return 0;
}


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

const std::uint_fast64_t MOD = 1e9 + 7;
// const std::uint_fast64_t MOD = 1e9 + 7;
/**
 * usage: modint<mod> x(1);
 * x.aで値を取り出し. modint同士で計算をやる.
 * http://noshi91.hatenablog.com/entry/2019/03/31/174006
 */
template <std::uint_fast64_t Modulus> class modint {
  using u64 = std::uint_fast64_t;

public:
  u64 a;

  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    u64 exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};


signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int N, K;
    cin >> N >> K;
    vector<modint<MOD>> factorial(N + 1);
    factorial[0] = 1;
    for (int i = 0; i < N; i++) {
        factorial[i + 1] = factorial[i] * modint<MOD>(i + 1);
    }
    vector<int> A(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }
    sort(A.begin(), A.end());
    modint<MOD> sum(0);
    for (int i = 0; i < N - K + 1; i++) {
        sum -= factorial[N - i - 1] / factorial[K - 1] / factorial[(N - i - 1) - (K - 1)] * modint<MOD>(A[i] + MOD);
    }
    reverse(A.begin(), A.end());
    for (int i = 0; i < N - K + 1; i++) {
        sum += factorial[N - i - 1] / factorial[K - 1] / factorial[(N - i - 1) - (K - 1)] * modint<MOD>(A[i] + MOD);
    }
    cout << sum.a << endl;
    
    return 0;
}

Measured memory_bytes: 1792
N = int(input())
h = list(map(int, input().split()))

dp = [0]

for i in range(N):
  dp.append(float('inf'))
  dp[i] = min(dp[i],dp[i-1] + abs(h[i]-h[i-1]))
  if i > 1 :
    dp[i] = min(dp[i],dp[i-2] + abs(h[i]-h[i-2]))

print(dp[N-1])

Measured memory_bytes: 13928
#include<iostream>

using namespace std;

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

Measured memory_bytes: 256
#ifndef DEBUG
#    pragma GCC optimize("O3,no-stack-protector")
#    pragma GCC optimize("unroll-loops")

#    if __cplusplus < 201703L
#        pragma GCC target("avx")
#    else
#        pragma GCC target("avx2")
#    endif

#endif

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define dump(x) cout << x << endl
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i <= i##_len; ++i)
#define rep3(i, l, r) for (int i = l, i##_len = (r); i < i##_len; ++i)
#define REP3(i, l, r) for (int i = l, i##_len = (r); i <= i##_len; ++i)
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
typedef int64_t Int;
typedef long double Ld;

using namespace std;
template <class T>
using Graph = vector<vector<T>>;

const Ld pi = M_PI;
const Int MOD = 998244353;
const Int INF = 1LL << 62;

Int Floor(Int a, Int b) {
    return (a - (a % b)) / b;
}

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

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

template <typename T>
inline T Gcd(T a, T b) {
    T c;
    while (b) {
        c = b;
        b = a % b;
        a = c;
    }
    return a;
}

Int Lcm(Int a, Int b) {
    if (a < b) {
        swap(a, b);
    }

    return a / Gcd(a, b) * b;
}

vector<Int> divisor(Int n) {
    vector<Int> ret;
    for (Int i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n)
                ret.push_back(n / i);
        }
    }

    return ret;
}

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

    Int n, k;
    cin >> n >> k;
    vector<Int> ans(n + n + 5, 0);
    vector<pair<Int, Int>> s;
    for (Int i = 0; i < k; i++) {
        Int l, r;
        cin >> l >> r;
        s.push_back({l, r});
    }

    ans[1] = 1;
    ans[2] = -1;

    for (Int i = 1; i <= n; i++) {
        ans[i] += ans[i - 1];
        ans[i] %= MOD;

        for (auto [l, r] : s) {
            ans[i + l] += ans[i] + MOD;
            ans[i + l] %= MOD;
            ans[i + r + 1] -= (ans[i] - MOD);
            ans[i + r + 1] %= MOD;
        }
    }

    cout << ans[n] << endl;

    return 0;
}


Measured memory_bytes: 6340
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 100010;
LL a[maxn];
LL sum;
int main()
{
    LL n;
    scanf("%lld", &n);
    for(int i = 1; i <= n; i++){
        scanf("%lld", &a[i]);
        sum += a[i];
        a[i-1] = a[i] - a[i-1];
    }
    a[0] = a[0] - a[n];
    if(n == 1){
        printf("YES\n");
        return 0;
    }
    if((sum*2)%(n*(n+1))){
        printf("NO\n");
        return 0;
    }
    LL ans = 0;
    LL cnt = sum / (n*(n+1)/2);
    for(int i = 0; i < n; i++){
        a[i] -= cnt;
        if(a[i] % n || a[i] > 0){
            printf("NO\n");
            return 0;
        }
        ans += a[i] / (-n);
    }
    if(cnt == ans){
        printf("YES\n");
    }
    else{
        printf("NO\n");
    }
    return 0;
}


Measured memory_bytes: 1024
n = gets.to_i
a = gets.to_s.chomp.split.map(&:to_i)
b = gets.to_s.chomp.split.map(&:to_i)
c = gets.to_s.chomp.split.map(&:to_i)
sum = 0
before = 100

a.each do |d|
  if d == before + 1
    sum += c[d - 2]
  end
  sum += b[d - 1]
  before = d
end
puts sum

Measured memory_bytes: 1788
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL<<62
#define inf 1000000007 

vector<vector<ll>>a(100010);
ll b[100010],c[100010];
int main() {
	ll n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		ll l,r;
		cin>>l>>r;
		l--;r;
		a[l].push_back(r);
		b[i]=l;c[i]=r;
	}
	for(int i=0;i<n;i++){
		sort(a[i].begin(),a[i].end());
	}
	for(int i=0;i<m;i++){
		ll now=b[i],id=c[i];
		auto ID=lower_bound(a[now].begin(), a[now].end(), id) - a[now].begin();
		now+=1;ID+=1;
		printf("%06d%06d\n",now,ID);
	}
	return 0;
}

Measured memory_bytes: 8576
using System;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Generic;

namespace AtCoder
{
	class Program
	{
		static void Main(string[] args)
		{
			var s = ReadLine();
			var t = ReadLine();
			var f = false;
			if (s.Length >= t.Length)
				Console.WriteLine("No");
			else
			{
				for (int i = 0; i < s.Length; i++)
				{
					if (s[i] != t[i])
					{
						f = true;
						break;
					}
				}
			}
			if (f) Console.WriteLine("No");
			else Console.WriteLine("Yes");
		}

		public static int nCm(int n, int m)
		{
			if (n == 0) return 0;
			if (m == 0) return 1;
			return n * nCm(n - 1, m - 1) / m;
		}

		public static string ReadLine()
		{
			return Console.ReadLine();
		}

		public static int ReadLineInt()
		{
			return int.Parse(ReadLine());
		}

		public static string[] ReadLineStringSplit(char s = ' ')
		{
			return ReadLine().Split(s).ToArray();
		}

		public static int[] ReadLineIntSplit(char s = ' ')
		{
			return ReadLine().Split(s).Select(x => int.Parse(x)).ToArray();
		}

		public static long[] ReadLineLongSplit(char s = ' ')
		{
			return ReadLine().Split(s).Select(x => long.Parse(x)).ToArray();
		}
	}
}

Measured memory_bytes: 17364
#include "bits/stdc++.h"
#include <math.h>
#include <time.h>
using namespace std;
const long long mod = 1000000007;
#define dump(x)                             \
    if (dbg) {                              \
        cout << #x << " = " << (x) << endl; \
    }
bool dbg = false;
long long gcd(long long a, long long b){
   if (a % b == 0)
   {
       return(b);
   }
   else
   {
       return(gcd(b, a % b));
   }
}
long long lcm(long long a, long long b){
   return (a / gcd(a, b))* b;
}
//---------------------------------------------------------------------------------------------------
/*ここからいじる*/
//---------------------------------------------------------------------------------------------------

void solve(){
}
int main(){
    int n;
    cin >> n;
    string k = to_string(n);
    int size = k.size();
    int ans = 0;
    int ten = 1;
    for(int i = 0; i < size; i++){
        ten *= 10;
        if(i % 2 == 1){
            continue;
        }
        if(i == size - 1){
            ans += (n - (ten / 10 - 1));
        }
        else{
            ans += (ten - ten / 10);
        }
    }
    cout << ans << endl;
    solve();
    return 0;
}



Measured memory_bytes: 256
#include <stdio.h>
#define rep(i, n) for(int i = 0; i < n; ++i)
#define repm(i, f, l) for(int i = f; i < l; ++i)

int main(void) {
  int n, m, min = 5000;
  scanf("%d%d", &n, &m);
  char f[n][m + 10];
  rep(i, n) scanf("%s", f[i]);
  repm(i, 1, n) repm(j, i + 1, n) {
    int count = 0;
    rep(k, n) rep(l, m) {
      if(k < i) {
        if(f[k][l] != 'W') ++count;
      } else if(k < j) {
        if(f[k][l] != 'B') ++count;
      } else {
        if(f[k][l] != 'R') ++count;
      }
    }
    if(count < min) min = count;
  }
  printf("%d\n", min);
  return 0;
}


Measured memory_bytes: 2668
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    vector<set<int> > G(N);
    rep(i, N - 1) {
        int a, b;
        cin >> a >> b;
        --a;
        --b;
        G[a].insert(b);
        G[b].insert(a);
    }

    vi rem(N), lev(N);
    queue<int> q;
    rep(i, N)if (sz(G[i]) == 1)lev[*G[i].begin()] = 1;
    rep(i, N)if (lev[i])q.push(i);

    while (sz(q)) {
        int u = q.front(), flag=0; q.pop();
        each(v, G[u]) {
            if (sz(G[v]) == 1 && !flag++) {
                rem[v] = 1;
            }
            G[v].erase(u);
            if (sz(G[v]) == 1) {
                int w = *G[v].begin();
                q.push(w);
            }
        }
        rem[u] = 1;
    }

    rep(i, N)if (!rem[i] && sz(G[i]) == 0) {
        cout << "First" << endl;
        return 0;
    }
    cout << "Second" << endl;
}


Measured memory_bytes: 15232
let () =
  Scanf.scanf "%d %d" @@ fun n x ->
  let m = Array.init n (fun _ -> Scanf.scanf " %d" (fun v -> v)) in
  let s, l = Array.fold_left (+) 0 m, Array.fold_left min m.(0) m in
  Printf.printf "%d\n" (n + (x - s) / l)

Measured memory_bytes: 384