akhauriyash/Code-Regression huggingface dataset:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
/*----------------------------------------*/
template <typename T>
class TopologicalSort {
  struct Edge {
    int src, dst;
    T cost;
    Edge(int dst_, T cost_ = -1) : src(-1), dst(dst_), cost(cost_) {}
    Edge(int src_, int dst_, T cost_) : src(src_), dst(dst_), cost(cost_) {}
    inline bool operator < (const Edge &rhs) { return cost < rhs.cost; }
    inline bool operator <= (const Edge &rhs) { return cost <= rhs.cost; }
    inline bool operator > (const Edge &rhs) { return cost > rhs.cost; }
    inline bool operator >= (const Edge &rhs) { return cost >= rhs.cost; }
  };

  int sz;

  bool dfs(int ver, vector<int> &state) {
    state[ver] = 1;
    for (Edge e : graph[ver]) {
      if (state[e.dst] == 1) return false;
      if (state[e.dst] == 0 && !dfs(e.dst, state)) return false;
    }
    order.emplace_back(ver);
    state[ver] = -1;
    return true;
  }

public:
  vector<vector<Edge> > graph;
  vector<int> order;

  TopologicalSort(int sz_) : sz(sz_), graph(sz) {}

  void add(int src, int dst, T cost = -1) { graph[src].emplace_back(Edge(dst, cost)); }

  bool build() {
    order.clear();
    vector<int> state(sz, 0);
    REP(i, sz) if (state[i] == 0 && !dfs(i, state)) return false;
    reverse(ALL(order));
    return true;
  }
};

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n, m; cin >> n >> m;
  TopologicalSort<int> ts(n);
  REP(i, m) {
    int x, y; cin >> x >> y;
    --x;
    --y;
    ts.add(x, y);
  }
  ts.build();
  vector<int> toposo = ts.order;
  reverse(ALL(toposo));
  vector<int> dp(n, 0);
  for (int i : toposo) {
    for (auto e : ts.graph[i]) dp[i] = max(dp[i], dp[e.dst] + 1);
  }
  int ans = 0;
  REP(i, n) ans = max(ans, dp[i]);
  cout << ans << '\n';
}


Measured memory_bytes: 7920
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main(void){
	while(1){
		int x,h;
		double th;

		cin >> x >> h;
		if( x==0 && h==0 ) break;

		th=sqrt(double(x*x/4.0+h*h));
		printf("%.8f\n",x*x+2*x*th);
	}
	return 0;
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a) for(int i = 0; i < (a); i++)
#define ALL(a) (a).begin(),(a).end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const int MOD = 1e9 + 7;

signed main(){
    string s;
    cin >> s;
    int cnt[3] = {};
    REP(i,s.size()){
        if(s[i] == 'a'){
            cnt[0]++;
        }else if(s[i] == 'b'){
            cnt[1]++;
        }else{
            cnt[2]++;
        }
    }
    sort(cnt, cnt + 3);
    if(cnt[2] <= cnt[0] + 1){
        cout << "YES" << endl;
    }else{
        cout << "NO" << endl;
    }
}

Measured memory_bytes: 512
def is_prime(n):
    if n == 1:
        return False
    for i in range(2,int(n**0.5)+1):
        if n % i == 0:
            return False
    return True
a=[]
for i in range(3,10**5):
  if is_prime(i) and is_prime((i+1)//2):
    a.append(i)
q=int(input())
lr=[list(map(int,input().split())) for _ in range(q)]
for lri in lr:
  print(len([ai for ai in a if ai>=lri[0] and ai<=lri[1]]))

Measured memory_bytes: 81372
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <array>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <valarray>
#include <utility>
#include <tuple>
#include <cmath>
#include <random>

using ll = long long;

//各種nil
template <class T>
constexpr T nil = static_cast<T>(-1);

constexpr int nil_i = nil<int>;
constexpr int nil_ll = nil<ll>;
constexpr double nil_d = nil<double>;

//vector
template <class T>
using vec = std::vector<T>;

struct Vec2D
{
    int x, y;
};

struct TimePos
{
    int time;
    Vec2D pos;
};

int ManhattanDistance(Vec2D v1, Vec2D v2)
{
    using namespace std;
    return abs(v1.x - v2.x) + abs(v1.y - v2.y);
}

void solve()
{
    using namespace std;
    string s;
    cin >> s;

    cout << s[0] << s.size() - 2 << s[s.size() - 1] << endl;

}

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

Measured memory_bytes: 256
from math import factorial


def modpow(a, n, p):
    if n == 0:
        return 1
    elif n == 1:
        return a % p
    if n % 2 == 1:
        return (a * modpow(a, n-1, p)) % p
    tmp = modpow(a, n//2, p)
    return (tmp * tmp) % p


def main():
    mod = 10 ** 9 + 7
    n, a, b = map(int, input().split())

    # まずは繰り返し2乗法によって全部の組み合わせを求める
    # すべての組み合わせは、花を選ぶ/選ばないで組み合わせを決めれる

    ans = (pow(2, n) - 1) % mod

    # a本選んだときの数を引く
    c_a = 1
    for i in range(n, n-a, -1):
        c_a *= i
        c_a %= mod
    c_a *= modpow(factorial(a), mod-2, mod)
    ans -= c_a
    ans %= mod

    # b本選んだときの数を引く
    c_b = 1
    for i in range(n, n-b, -1):
        c_b *= i
        c_b %= mod
    c_b *= modpow(factorial(b), mod-2, mod)
    ans -= c_b
    ans %= mod

    print(ans)


if __name__ == "__main__":
    main()


Measured memory_bytes: 280988
$dx = [1, 0, -1, 0]
$dy = [0, 1, 0, -1]

def bfs(grid, sx, sy)
  #print grid
  queue = [[sx, sy]]
  dist = {[sx, sy] => 0}
  until queue.empty?
    #print dist
    #puts
    crd = queue.shift
    4.times do |dir|
      #puts
      nx = crd[0] + $dx[dir]
      ny = crd[1] + $dy[dir]
      #print nx
      #print ny
      next if nx < 0 || nx >= H || ny < 0 || ny >= W
      next if grid[nx][ny] == "#"
      if dist[[nx, ny]] == nil
        #print "checking"
        dist[[nx, ny]] = dist[[crd[0], crd[1]]] + 1
        queue.push([nx, ny])
      end
    end
  end
  dist.values.max
end

H, W = gets.split.map(&:to_i)
grid = []
H.times do
  grid.push(gets.chomp.split(""))
end

res = 0
(0..H-1).each do |sx|
  (0..W-1).each do |sy|
    #puts
    #print sx
    #print sy
    #print grid
    if grid[sx][sy] != "#"
      maxl = bfs(grid, sx, sy)
      res = [maxl, res].max
    end
  end
end
puts res

Measured memory_bytes: 2172
#include<iostream>
using namespace std;
int main(){
  int n, k, q, tmp, a[100001] = {0}; 
  cin >> n >> k >> q;
  for(int i=1; i<=n; i++)a[i] = 0;
  for(int i=0; i<q; i++){
    cin >> tmp;
    a[tmp] += 1;
  }
  for(int i=1; i<=n; i++){
    cout << (k-q+a[i]>0?"Yes":"No") << endl;
  }
  return 0;
}
    
    
  

Measured memory_bytes: 1024
import static java.lang.System.*;
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner s = new Scanner(in);
		
		int n = s.nextInt();
		
		int cur = 1;
		int max = 0;
		
		for(int x=2;x<=n;x++) {
			int exp = 1;
			while(x%Math.pow(2,exp)==0 && Math.pow(2,exp)<x)
				exp++;
			
			if(exp>max) {
				max = exp;
				cur = x;
			}
		}
		
		out.println(cur);
	}
}

Measured memory_bytes: 23764
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b;
    cin>>a>>b;
    if(a==b)
        cout<<(2*a)<<endl;
    else
    {
        int x=max(a,b);
        cout<<(x+x-1)<<endl;
    }
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <cmath>
#include <string>
#include <typeinfo>
#include <climits>
#include <algorithm>
#include <cctype>
#include <cstring>
#include <stdio.h>
#include <cstdlib>
#include <numeric>
#include <iomanip>
using namespace std;

int main(){
    int x,a,b;
    cin >> x >> a >> b;
    if(b<=a){
        cout << "delicious";
    }else if(b<=x+a){
        cout << "safe";
    }else{
        cout << "dangerous";
    }
}

Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>

int compare( const void *, const void * );

int main( void ) {
	int i, n, a[ 10000 ] = { [ 3 ] = 99999 }, b[ 12 ];

	scanf( "%d", &n );

	for ( i = n; i--; scanf( "%d", a + i ) ) ;

	qsort( a, n, sizeof(int), &compare );

	for ( i = 15; i--; )
		if ( i % 5 ) {
			int l = a[ i / 4 ], r = a[ i % 4 ];

			do {
				l *= 10;
			} while ( r /= 10 );

			b[ i - i / 5 - 1 ] = l + a[ i % 4 ];
		}

	qsort( b, 12, sizeof(int), &compare );

	printf( "%d\n", b[ 2 ] );

	return 0;
}

int compare( const void *a, const void *b ) {
	return *(int *)a - *(int *)b;
}

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

#ifdef _DEBUG
inline void dump() { cerr << endl; }
template<typename Head> void dump(Head&& head) { cerr << head; dump(); }
template<typename Head, typename... Tail> void dump(Head&& head, Tail&&... tail) { cerr << head << ", "; dump(forward<Tail>(tail)...); }
#define debug(...) do {cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; dump(__VA_ARGS__); } while (false)
#else
#define dump(...)
#define debug(...)
#endif

template<typename T>
struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template<typename T>
using Edges = vector<edge<T>>;
template<typename T>
using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template<typename T>
using Matrix = vector<vector<T>>;

/////////////////////////////////////////////////////////////////////

const ll inf = 1LL<<60;

struct Doubling {
  const ll LOG = 60;
  vector<vector<ll>> table;

  Doubling(ll n) {
    table.assign(LOG, vector<ll>(n, -1));
  }
  void set_next(ll k, ll x) {
    table[0][k] = x;
  }
  void build() {
    for (int k=0; k<LOG-1; k++) {
      for (int i=0; i<(int)table[k].size(); i++) {
        if (table[k][i] == -1) table[k+1][i] = -1;
        else table[k+1][i] = table[k][table[k][i]];
      }
    }
  }
  ll query(ll k, ll t) {
    for (int i=LOG-1; i>=0; i--) {
      if ((t>>i)&1) k = table[i][k];
    }
    return k;
  }
};

int main()
{
  ll n,k; cin>>n>>k;

  Doubling db(n);
  rep(i, n) {
    ll a; cin>>a; a--;
    db.set_next(i, a);
  }

  db.build();
  cout << db.query(0, k)+1 << endl;

  return 0;
}


Measured memory_bytes: 98584
use std::str::FromStr;
use std::collections::{HashMap,HashSet,VecDeque,BinaryHeap};
use std::cmp::{max,min,Ordering};
use std::io::Read;

fn main() {

    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    let mut buf_it = buf.split_whitespace();

    let N = buf_it.next().unwrap().parse::<usize>().unwrap();
    let A = (0..N).map(|_| buf_it.next().unwrap().parse::<isize>().unwrap()).collect::<Vec<_>>();

    let mut m = A[0];
    let mut ans = -1_000_000_000;
    
    for &a in &A[1..]{
        ans = max(ans,a-m);
        m = min(m,a);
    }

    println!("{}",ans);

}


Measured memory_bytes: 9012
import itertools
import math
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
l=[]
for i in itertools.permutations(range(1,n+1)):
    l.append(list(i))
for i in range(math.factorial(n)):
    if l[i]==p:
        a=i+1
    if l[i]==q:
        b=i+1
ans=abs(a-b)
print(ans)

Measured memory_bytes: 10228
#include <bits/stdc++.h>
#define REP(i,n) for(ll i = 0; i < (ll)n; i++)
#define FOR(i,a,b) for (ll i=(a);i<(ll)b; i++)
#define ALL(obj) (obj).begin(),(obj).end()
#define INF 1000000000
using namespace std;
typedef long long ll;
typedef double db;
typedef string str;
typedef pair<ll,ll> p;
const int MOD = 1000000007;

// vector<p> cut(M);
// cut[i].second
// cut[i].first;
// REP(i,M){
//     int a,b;cin>>a>>b;
//     cut[i] = make_pair(b,a);//last first の順番
// }
// for(int i=0; i<N;i++){
// }
// sort(cut.begin(),cut.end()); //sort
// char '' str ""
int main(void)
{

    int N,nx,cnt=0;cin>>N;
    map<int, int> m;
    for(int i=1; i<=N;i++){
      int a;
      cin>>a;
      m.insert(make_pair(i, a));
    }
    nx = m[1];
    cnt++;
    if(nx==2){
      cout<<cnt<<endl;
      return 0;
    }
    for(int i=0; i<=N;i++){
      nx = m[nx];
      cnt++;
      if(nx==2){
        cout<<cnt<<endl;
        return 0;
      }
    }
    cout<<-1<<endl;
    return 0;
}


Measured memory_bytes: 4992
using System;

namespace V1_AizuPR
{
    class Program
    {
        static void Main(string[] args)
        {
            string n = Console.ReadLine();
            int Num = int.Parse(n);
            for (int i = 0; i < Num; i++)
            {
                Console.WriteLine(Console.ReadLine().Replace("Hoshino","Hoshina"));
            }
        }
    }
}

Measured memory_bytes: 8740
require 'enumerator'
loop do
  w,q = gets.split.map(&:to_i)
  break if (w|q) == 0
  wall = Array.new(w,nil)
  cat = Hash.new
  q.times do
    pre,id,width = gets.split
    id = id.to_i
    if pre == "s"
      width = width.to_i
      c=0
      flag = nil
      wall.each_cons(width) do |x|
        if x.join == ""
          flag =true
          break
        end
        c+=1
      end
      if flag
        cat[id]=[c,width]
        puts c
        wall[c,width]=[1]*width
      else puts "impossible"
      end
    else
      pos,width = cat[id]
      wall[pos,width]=[nil]*width
    end
  end
  puts "END"
end

Measured memory_bytes: 2692
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace __gnu_pbds;
using namespace std;
 
 
#define FOR(i, x, n) for(ll i = x; i < n; i++) 
#define pb push_back
#define pf push_front
#define ll long long
#define hii cout << "hii" << endl
#define pii pair<int, int>
#define pll pair<ll, ll>
#define int ll
#define mpp make_pair
#define endl '\n'
#define ff first 
#define ss second
#define vi vector<int>
#define all(s) s.begin(), s.end()
#define si size()
 
template <class T> ostream& operator << (ostream &os, const vector<T> &v) { for (T i : v) os << i << ' '; return os; }
template <class T> ostream& operator << (ostream &os, const set<T> &v) { for (T i : v) os << i << ' '; return os; }
template <class T, class S> ostream& operator << (ostream &os, const pair<T, S> &v) { os << v.first << ' ' << v.second; return os; }
template <class T, class S> ostream& operator << (ostream &os, const unordered_map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; } 
 
 
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <class Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << endl; }
    template <class Arg1, class... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args) {
        const char* sep = strchr(names + 1, ',');
        cerr.write(names, sep - names) << " : " << arg1 << "  ";
        __f(sep + 1, args...);
    }
#else
#define trace(...) 0
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#endif // ifndef ONLINE_JUDGE
 
 
const int N = 5e5 + 5;
const int mod = 1e9 + 7;
const int INF = 1e15;

int arr[N];
int n, k;
int query = 1;
void print(int ret)
{
    cout << "Case #" << query++ << ": " << ret << endl;
}


void solve()
{
	int n, m;
	cin >> n >> m;
	cout << (n * (n - 1) + m * (m - 1)) / 2 << endl;
}

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

	    int t = 1;
	    // cin >> t;
	while(t--)solve();
	return 0;
}

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

using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    vector<int> p;
    int n, contador = 0;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        p.push_back(x);
    }
    for (int i = 1; i < n - 1; i++)
    {
        if (p[i] > p[i - 1] && p[i] < p[i + 1])
        {
            contador++;
        }
        else if (p[i] < p[i - 1] && p[i] > p[i + 1])
        {
            contador++;
        }
    }
    cout << contador << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("unroll-loops") 
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 
#define int long long
#define fr(i,a,b) for(int i = a ; i <= b ; ++i)
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define pb push_back
#define fst first
#define snd second
using namespace __gnu_pbds;
using ordered_set =
 tree<int, null_type,less<int>,
 rb_tree_tag,tree_order_statistics_node_update>;
//find_by_order(k):returns iterator to kth element starting from 0
//order_of_key(k):returns count of elements strictly smaller than k
typedef long long ll;typedef pair<int, int> pii;
typedef vector<int> vi;typedef long double ld;
template<class T>
using min_heap = priority_queue<T,vector<T>,greater<T>>; 
template<typename T> T gcd(T a, T b){return(b?__gcd(a,b):a);}
template<typename T> T lcm(T a, T b){return(a*(b/gcd(a,b)));}
template<class T> void re(T& x) { cin >> x; }
template<typename T> 
void remdup(vector<T>& v) {												
	sort(all(v)); v.erase(unique(all(v)), v.end());
}
template<typename T> void re(vector<T> &v) {trav(i,v) cin >> i;}
template<class H, class... T> void re(H& h, T&... t) { re(h); re(t...); }
void unsyncIO() { ios_base::sync_with_stdio(0); cin.tie(0); }
#ifdef np
#include "/home/wjex/cp/pr.h"
#else
#define trace(...)
#endif

#define MOD 1000000007  


void solve(){
	string s, t ;re(s,t);
	bool ok = 0 ; 
	for(int i = 0 ; i < s.length(); ++i){
		if(s[i]!=t[i]) ok =1 ;
	}
	if(ok) cout << "No\n";
	else cout << "Yes\n";
}
	

signed main() {
	unsyncIO();
	int tt =1 ;
	//cin >> tt;
	rep(i,0,tt) solve();













#ifdef np
	cout <<endl<<endl<< "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}


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

int main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  int x = 0;
  for(int i = 0; i < n; ++i) {
    cin >> a[i];
    x ^= a[i];
  }
  for(int i = 0; i < n; ++i) {
    cout << (a[i] ^ x) << ' ';
  }
  cout << '\n';
}

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

int main() {
	ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int n;
	cin>>n;
	vector<int> v(n+1);
	for(int i=1;i<=n;i++)
		cin>>v[i];
	vector<int> dp(n+1);
	dp[2]=abs(v[1]-v[2]);

	for(int i=3;i<=n;++i) {
		dp[i] = min(dp[i-1]+abs(v[i]-v[i-1]),dp[i-2]+abs(v[i]-v[i-2]));
	}

	cout<<dp[n];

	return 0;
}

Measured memory_bytes: 3856
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,k,x,l,i,ans=0;
  map<int,int> m;
  vector<int> v;
  cin>>n>>k;
  for(i=0;i<n;i++){
    cin>>x;
    if(m.count(x)) m.at(x)++;
    else m[x]=1;
  }
  for(auto z:m) v.push_back(z.second);
  sort(v.begin(),v.end());
  l=v.size();
  for(i=0;i<l-k;i++) ans+=v.at(i);
  cout<<ans<<endl;
}

Measured memory_bytes: 13484
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,a,b) for(int i=(a);i<=(b);i++) 
#define per(i,a,b) for(int i=(a);i>=(b);i--)
const int maxn=1e5+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
ll qpow(ll a,int b) {
	ll ans=1;
	while(b) {
		if(b&1) ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
int main() 
{
	ios::sync_with_stdio(false);cin.tie(0);
	int n,a,b;
	cin>>n>>a>>b;
	ll ans=qpow(2,n)-1;
	ll sum=1;
	rep(i,0,a-1) sum=sum*(n-i)%mod;
	rep(i,1,a) sum=sum*qpow(i,mod-2)%mod;
	ans=(ans-sum+mod)%mod;
	sum=1;
	rep(i,0,b-1) sum=sum*(n-i)%mod;
	rep(i,1,b) sum=sum*qpow(i,mod-2)%mod;
	ans=(ans-sum+mod)%mod;
	cout<<ans<<'\n';
	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <string>
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 10010;

string x[4] = {"dream", "dreamer", "erase", "eraser"};
  
int main() {
    string s;
    cin >> s;
    ll n = s.size();

    reverse(all(s));

    rep(i, 4) {
        reverse(all(x[i]));
    }

    ll i = 0;
    while(i < n) {
        bool dame = true;
        rep(j, 4) {
            ll len = x[j].size();
            if(s.substr(i, len) == x[j]) {
                dame = false;
                i += len;
            }
        }
        if(dame) {
            out("NO");
            re0;
        }
    }
    
    out("YES");
    re0;
}

Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll p = 1000000007;
int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin >> n;
    ll a[n][n];
    for(ll i = 0;i < n;i++){
        for(ll j = 0;j < n;j++){
            cin >> a[i][j];
        }
    }
    ll dp[(ll)pow(2,n)];
    dp[0] = 1;
    for(ll i = 1;i < (ll)pow(2,n);i++){
        ll x = 0;
        ll y = __builtin_popcount(i);
        for(ll j = 0;j < n;j++){
            if(i & (1 << j)){
                if(a[y-1][j]){
                    x = (x+dp[i & (ll)(~(1 << j))])%p;
                }
            }
        }
        dp[i] = x;
    }
    cout << dp[(ll)pow(2,n)-1] << endl;
}

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

int main(void){
    int n;
    cin >> n;
    vector<int> x(n);
    rep(i, n) cin >> x[i];
    
    ll v_min = 1e10;
    rep(i, 101) {
        ll v = 0;
        rep(j, n) {
            v += (x[j]-i) * (x[j]-i);
        }
        v_min = min(v_min, v);
    }
    cout << v_min;
}

Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main() {
    int a, b, c;
    int count = 0;
    
    cin >> a >> b >> c;
    for(int i = a; i <= b; ++i) {
        if(0 == c % i) {
            ++count;
        }
    }
    cout << count << endl;
    
    return 0;
}

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

typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

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

const ll INF = 1e18;
const ll MOD = 998244353;
const ll MX = 1000001;

string S;

void solve() {
    cin >> S;
    if (S == "Sunny") cout << "Cloudy";
    if (S == "Cloudy") cout << "Rainy";
    if (S == "Rainy") cout << "Sunny";
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
    ll T = 1; //cin >> T;
    while (T--) solve();
    return 0;
    // You should actually read the stuff at the bottom
}

/* Stuff to Look For
 * -----------------
 * Int overflow, array bounds
 * Initializing all variables, avoid weird behavior
 * Edge cases(n = 0, n = 1)
 * Just return 0 after result
 */


Measured memory_bytes: 256
#include "bits/stdc++.h"
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a),i##_len=(b);i<i##_len;++i)
#define BUGAVOID(x) x
#define rep(...) BUGAVOID(_overload3(__VA_ARGS__,repi,_rep,_rep)(__VA_ARGS__))
#define sz(c) ((int)(c).size())
#define all(c) (c).begin(),(c).end()
#define m0(a) memset(a,0,sizeof(a))
#define unq(v) v.erase(unique(v.begin(),v.end()),v.end());
#define mp make_pair
#define write(x) cout<<(x)<<endl
#ifdef LOCAL
#define debug(...) debug_vars(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#endif
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
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; }
const int INF = 1 << 28;
const double EPS = 1e-8;
const int MOD = 1e9 + 7;
const int MAX = 1e5 + 10;
struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); }; }aaaa;

int D, G;
int p[15], c[15];
int comp[15];

int main()
{
    cin >> D >> G;
    rep(i, D) cin >> p[i] >> c[i];

    rep(i, D) comp[i] = 100 * (i + 1) * p[i] + c[i];

    int ans = INF;
    for (int bits = 0; bits < (1 << D); bits++) {
        int score = 0;
        int cnt = 0;
        rep(i, D) {
            if ((bits >> i) % 2) {
                score += comp[i];
                cnt += p[i];
            }
        }
        if (score >= G) {
            chmin(ans, cnt);
            continue;
        }
        else {
            for (int i = D - 1; i >= 0; i--) {
                if ((bits >> i) % 2) continue;
                int extra = min(p[i], (G - score + (100 * (i + 1)) - 1) / (100 * (i + 1)));
                score += 100 * (i + 1) * extra;
                cnt += extra;
                if (score >= G) {
                    chmin(ans, cnt);
                    break;
                }
            }
        }
    }
    write(ans);
}

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

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

using namespace std;
typedef long long ll; typedef vector<int> VI; typedef pair<ll,int> pii;
const int MD = (int)1e9 + 7; typedef vector<ll> VL;
void dbg(){ cerr << "\n"; }  template <class T, class ...T2>
void dbg(const T& fst, const T2&...rst){ cerr << fst << ": "; dbg(rst...); }


ll fnc(ll p, vector<pii>& vz, vector<int>& f){
	int n= sz(vz);
	ll rtn = 0;
	rep(i, n){
		auto w = vz[i].first;
		if(w <= p) continue;
		rtn += (w - p + f[i]-1)/f[i];
	}
	return rtn;
}


int main() {
	//cin.tie(0); ios_base::sync_with_stdio(false);
	int n; ll k;
	cin >>n >>k;
	VI a(n), f(n);
	rep(i, n) cin >>a[i];
	rep(i, n) cin >>f[i];
	sort(all(a)); 
	sort(all(f)); reverse(all(f));
	


	vector<pii> vz(n);
	rep(i, n) vz[i] = pii((ll)a[i]*f[i], i);
	
	ll hi=1LL<<62, lw=0-1;
	while(hi - lw > 1){
		ll md = (hi + lw) >> 1;
		ll asw = fnc(md, vz, f);
		if( asw <= k )  hi = md;
		else lw = md;
	}
	cout << hi <<"\n";
}


Measured memory_bytes: 4992
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<iomanip>
#include<queue>

using namespace std;

typedef long long ll;

typedef std::pair<int, int> ipair;
bool lessPair(const ipair& l, const ipair& r){return l.second < r.second;}
bool morePair(const ipair& l, const ipair& r){return l.second > r.second;}

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

const ll MOD = 1e9 + 7;
// const long long INF = 1LL<<60;
void add(long long &a, long long b) { a += b; if (a >= MOD) a -= MOD; }
void sub(long long &a, long long b) { a -= b; if (a < 0) a += MOD; }
void mul(long long &a, long long b) { a *= b; a %= MOD; }
ll llmin(ll a, ll b) { if (a < b) return a; else return b; }
ll llmax(ll a, ll b) { if (a < b) return b; else return a; }
ll llabs(ll a) { if (a >= 0) return a; else return - a; }
ll llmodpow(ll a, ll n) {
   if (n == 0) return 1;
   ll tmp = llmodpow(a, n / 2);
   mul(tmp, tmp);
   if (n & 1) mul(tmp, a);
   return tmp;
}
int ans = 0;
int a[100001];
bool used[1000001];

int calc(int b) {
   // cout << b << endl;
   ans++;
   used[b] = 1;
   if (a[b] == 1) return ans;
   else if (used[a[b]]) return -1;
   else return calc(a[b]);
}

int main() {
   int N;
   cin >> N;
   for (int i = 0; i < N; i++) {
      cin >> a[i];
      a[i]--;
   }

   memset(used, 0, sizeof(used));
   cout << calc(0) << endl;
   return 0;
}


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




int main() {
    int n,k;
    cin>>n>>k;
    vector<int> v;
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        v.push_back(x);
    }
    
    int dp[100005];
    dp[0]=-1;
    for(int i=1;i<=k;i++){
        bool found=false;
        for(int j=0;j<n;j++){
            if(i-v[j]>=0){
                if(dp[i-v[j]]==-1){
                    found=true;
                    break;
                }
            }
        }
        if(found){
            dp[i]=1;
        }
        else{
            dp[i]=-1;
        }
    }
   
   if(dp[k]==1){
       cout<<"First";
   }
   else{
       cout<<"Second";
   }


 
}


Measured memory_bytes: 640
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;

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

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

    ll a;
    priority_queue<ll> pq;
    for (int i = 0; i < n; i++) {
        scanf("%lld", &a);
        pq.push(a);
    }
    
    ll b;
    for (int i = 0; i < m; i++) {
        b = pq.top();
        pq.pop();
        pq.push(b / 2);
    }

    ll ans = 0;
    while (!pq.empty()) {
        ans += pq.top();
        pq.pop();
    }

    cout << ans << endl;
}

Measured memory_bytes: 3956
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    cin>>s;
    int a1=0,a2=0,a3=0;
    for(int i=0;i<(int)s.size();i++)
    {
        if(s[i]=='a') a1++;
        else if(s[i]=='b') a2++;
        else if(s[i]=='c') a3++;
    }
    if(a1==1&&a2==1&&a3==1)
        printf("Yes\n");
    else printf("No\n");
    return 0;
}

Measured memory_bytes: 764
def resolve():
    n,x = list(map(int, input().split()))
    lll = list(map(int, input().split()))
    d = 0
    flag=False
    count = 1
    for i in range(n):
        d = d + lll[i]
        if d > x:
            flag = True
            print(count)
            break
        else:
            count += 1
    if flag == False:
        print(n+1)
resolve()        

Measured memory_bytes: 3064
N,K = map(int, input().split())
A = list(map(int, input().split()))
sorted_A = sorted(A)

inv_A = 0
for i in range(0,N):
  for j in range(0,N):
    if i<j and A[i] > A[j]:
      inv_A += 1

inv_sorted_A = 0
for i in range(0,N):
  for j in range(0,N):
    if i<j and sorted_A[i] < sorted_A[j]:
      inv_sorted_A += 1

if K == 1:
  print(inv_A)
else:  
  count = inv_sorted_A * K * (K-1) // 2  + inv_A * K 
  print(count % 1000000007)

Measured memory_bytes: 3188
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;

template<typename T>
auto makeNdVector(int n) {
  return vector<T>(n);
}

template<typename T, typename N, typename... Tail>
auto makeNdVector(const N& n, const Tail&... tail) {
  auto vec = makeNdVector<T>(tail...);
  vector<decltype(vec)> vecs(n, vec);
  return vecs;
}

template<typename T>
ostream& operator << (ostream& os, const vector<T>& vs){
  for (const T& v : vs) os << v << " ";
  return os;
}

int main() {
  int N; cin >> N;
  vector<queue<int>> que(N + 1);
  for (int i = 1; i <= N; ++i) {
    for (int j = 0; j < N - 1; ++j) {
      int a; cin >> a;
      que[i].push(a);
    }
  }

  bool finished = false;
  int n = 0;
  set<int> s;
  for (int i = 1; i <= N; ++i) s.insert(i);

  for (; n < N * (N-1) / 2; ++n) {
    finished = true;
    set<int> b;
    for (int i : s) {
      if (que[i].empty() || b.count(i)) continue;
      finished = false;

      int x = que[i].front();
      if (b.count(x)) continue;
      int y = que[x].front();
      if (i != y) continue;

      que[i].pop(); que[x].pop();
      b.insert(i); b.insert(x);
    }
    if (!finished && b.empty()) {
      cout << -1 << endl;
      return 0;
    }
    if (finished) break;
    s = b;
  }
  cout << n << endl;
}


Measured memory_bytes: 8056
/*
     ॐ त्र्यम्बकं यजामहे सुगन्धिं पुष्टिवर्धनम् |
     उर्वारुकमिव बन्धनान्मृत्योर्मुक्षीय माऽमृतात् ||
*/

#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            int
#define F              first
#define S              second
#define pb             push_back
#define si             set <int>
#define vi             vector <int>
#define pii            pair <int, int>
#define vpi            vector <pii>
#define vpp            vector <pair<int, pii>>
#define mii            map <int, int>
#define mpi            map <pii, int>
#define spi            set <pii>
#define endl           "\n"
#define sz(x)          ((int) x.size())
#define all(p)         p.begin(), p.end()
#define double         long double
#define que_max        priority_queue <int>
#define que_min        priority_queue <int, vi, greater<int>>
#define bug(...)       __f (#__VA_ARGS__, __VA_ARGS__)
#define print(a)       for(auto x : a) cout << x << " "; cout << endl
#define print1(a)      for(auto x : a) cout << x.F << " " << x.S << endl
#define print2(a,x,y)  for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl

inline int power(int a, int b)
{
     int x = 1;
     while (b)
     {
          if (b & 1) x *= a;
          a *= a;
          b >>= 1;
     }
     return x;
}

typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> Set;

template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
     const char* comma = strchr (names + 1, ',');
     cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}

const int N = 55;
int a[N], b[N], c[N], d[N], n, m, ans, q;
vi v;

void build(int pos)
{
     if (pos == n)
     {
          int cnt = 0;
          for (int i = 0; i < q; i++)
          {
               if (v[b[i]] - v[a[i]] == c[i])
                    cnt += d[i];
          }
          ans = max(ans, cnt);
          return ;
     }
     int x = v.back();
     for (int j = x; j <= m; j++)
     {
          v.pb(j);
          build(pos + 1);
          v.pop_back();
     }
}

void solve()
{
     cin >> n >> m >> q;
     for (int i = 0; i < q; i++)
          cin >> a[i] >> b[i] >> c[i] >> d[i];

     v.pb(1);
     build(0);

     cout << ans;
}

int32_t main()
{
     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

     cout << setprecision(9) << fixed;
     clock_t z = clock();

     solve();

     cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);

     return 0;
}

Measured memory_bytes: 3796
#include<cstdio>
#include<cstring>
#define MAXN 100005
#define M 1000000007
char s[2*MAXN+5];
long long k[2*MAXN+5];
long long Na[MAXN+5],Nc[MAXN+5],Fl[MAXN+5],Fr[MAXN+5];//注意开long long 
long long ansl[MAXN+5];
int init()
{
	long long x=MAXN;
	long long l=1;
	long long cnt=0;
	k[cnt]=l;
	while(x--)
	{
		l=(l*3)%M;
		k[++cnt]=l;
	}
}
int CMP(int x)
{
	long long ans=0;
	ans=((((Na[x-1]*Nc[x+1])%M)*k[Fl[x-1]+Fr[x+1]])%M+(((Na[x-1]*Fr[x+1])%M)*k[Fl[x-1]+Fr[x+1]-1])%M+(((Fl[x-1]*Nc[x+1])%M)*k[Fl[x-1]+Fr[x+1]-1])%M+(((Fl[x-1]*Fr[x+1])%M)*k[Fl[x-1]+Fr[x+1]-2])%M);
	return ansl[x]=ans%M;
}
int main()
{
	init();
	scanf("%s",s+1);
	int lens=strlen(s+1);
	for(int i=1;i<=lens;i++)
	{
		Na[i]=Na[i-1];
		Fl[i]=Fl[i-1];
		if(s[i]=='A') Na[i]+=1;
		if(s[i]=='?') Fl[i]+=1;
		//printf("%d:%d\n",Na[i],Fl[i]);
	}
	for(int i=lens;i>=1;i--)
	{
		Nc[i]=Nc[i+1];
		Fr[i]=Fr[i+1];
		if(s[i]=='C') Nc[i]+=1;
		if(s[i]=='?') Fr[i]+=1;
	}
	for(int i=1;i<=lens;i++)
	{
		if(s[i]=='B'||s[i]=='?')
			CMP(i);
	}
	long long cnt=0;
	for(int i=1;i<=lens;i++)
	{
		cnt=(cnt+ansl[i])%M;
	}
	printf("%lld",cnt%M);
}


Measured memory_bytes: 4992
import java.util.Scanner;
public class Main {
	public static void main(String[] args){
		Scanner stdIn=new Scanner(System.in);
		int A=stdIn.nextInt();
		int B=stdIn.nextInt();
		if((A+B)%2==0)
			System.out.println((A+B)/2);
		else
			System.out.println("IMPOSSIBLE");
	}
}

Measured memory_bytes: 23764
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
#define rep(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(x) (x).begin(), (x).end()
#define in(x, l, r) (ll)(l) <= (x) && (x) < (ll)(r)

using S = int;

using F = int;

S op(S l, S r) { return max(l, r); }

S e() { return 0; }

S mapping(F l, S r) { return max(l, r); }

F composition(F l, F r) { return max(l, r); }

F id() { return -1; }

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

    lazy_segtree<S, op, e, F, mapping, composition, id> seg(vector<int>(300'001,0));
    rep(i,0,N) {
        int F_val = seg.prod(max(0, A[i] - K), min(300'001, A[i] + K + 1)) + 1;
        seg.set(A[i], F_val);
    }
    cout << seg.all_prod() << endl;
}

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

using namespace std;


int main() {
    int cou[200001]={0},count=0,coun=0;
    int n,k;
    cin>>n>>k;
    for (int i = 0; i < n; ++i) {
        int in;
        cin >> in;
        if (cou[in] == 0)count++;
        cou[in]++;
    }
    sort(cou,cou+200001);
    for (int j = 200000; j >=200001-k; --j) {
        coun+=cou[j];
    }
    cout<<n-coun<<endl;
    return 0;
}

Measured memory_bytes: 1024
#include<iostream>

using namespace std;

bool mon(int t){
	int i = 6;

	while(true){
		if(t == i) return true;

		if(t%i == 0 && ((t/i)%7 == 1 || (t/i)%7 == 6))
			return false;

		i = i+2;

		if(t == i) return true;

		if(t%i == 0 && ((t/i)%7 == 1 || (t/i)%7 == 6))
			return false;

		i = i+5;		
	}
}

int main(){
	int num;
	int t;

	while(true){
		cin >> num;

		if(num == 1) break;

		cout << num << ":";

		t = 6;

		while(true){
			if(num%t == 0 && ((num/t)%7 == 1 || (num/t)%7 == 6) && mon(t))
				cout << " " << t;

			if(t == num) break;

			t = t+2;

			if(num%t == 0 && ((num/t)%7 == 1 || (num/t)%7 == 6) && mon(t))
				cout << " " << t;

			if(t == num) break;

			t = t+5;
		}

		cout << endl;
	}

}

Measured memory_bytes: 868
n = int(input())
al = list(map(int, input().split()))

d = [0]*n
for i in range(n):
    d[al[i]-1] += 1

s = 0
for i in range(n):
    if d[i]>1:
        s += d[i]*(d[i]-1)//2

for k in range(n):
    a = al[k]-1
    if d[a]<2:
        print(s)
        continue
    if d[a]==2:
        print(s-1)
        continue
    res = s - d[a]*(d[a]-1)//2 + (d[a]-1)*(d[a]-2)//2
    print(res)

Measured memory_bytes: 26140
A, B, M = gets.split.map(&:to_i)
a_price, b_price= gets.split.map(&:to_i), gets.split.map(&:to_i)
discount = []
M.times { discount << gets.split.map(&:to_i) }
ans = [a_price.min + b_price.min]

discount.each do |a, b, minus|
  ans << a_price[a - 1] + b_price[b - 1] - minus
end

puts ans.min


Measured memory_bytes: 19228
/*************************************************************************
	> File Name: 1.cpp
	> Author: Knowledge_llz
	> Mail: 925538513@qq.com 
	> Blog: https://blog.csdn.net/Pig_cfbsl 
	> Created Time: 2020/9/10 18:25:58
 ************************************************************************/
#include<queue>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define LL long long
using namespace std;
int read(){
	char x=getchar(); int u=0,fg=0;
	while(!isdigit(x)){ if(x=='-') fg=1; x=getchar(); }
	while(isdigit(x)){ u=(u<<3)+(u<<1)+(x^48); x=getchar(); }
	return fg?-u:u;
}
const int maxx=2e5+10;
int n,q,be[maxx],ne[maxx],to[maxx],c[maxx],e=0;
int dp[maxx][20],deep[maxx],fir[maxx],cnt=0;
LL w[maxx],ans[maxx],num[maxx],val[maxx];
bool p[maxx];
struct node{
	int id,x,y,k;
};
queue<node>que[maxx>>1];
void add(int x,int y,bool r){
	to[++e]=y;
	ne[e]=be[x];
	be[x]=e;
	if(r){ 
		c[e]=read();
		w[e]=read();
		add(y,x,0);
	}
	else c[e]=c[e-1], w[e]=w[e-1];
}
void dfs(int id){						//p记录是否到过				
    p[id]=1;							//cnt记录访问的顺序
    dp[++cnt][0]=id;					//dp记录第CNT个访问的点编号
    fir[id]=cnt;						//fir记录每个点第一次出现的位置
    for(int i=be[id];i;i=ne[i]){
        int go=to[i];
        if(!p[go]){
            deep[go]=deep[id]+1;		//点的深度
            dfs(go);
            dp[++cnt][0]=id;			//回溯时也要记录访问的点
        }
    }
}
void rmq(){
	int len=log(cnt)/log(2.0);
	for(int j=1;j<=len;j++)
        for(int i=1;i<=cnt;i++){
            if(i+(1<<j)-1>cnt)break;
            if(deep[dp[i][j-1]]<deep[dp[i+(1<<(j-1))][j-1]])
                dp[i][j]=dp[i][j-1];
            else dp[i][j]=dp[i+(1<<(j-1))][j-1];
        }
}
int ask(int x,int y){		//rmq算法的查询
    if(x>y)swap(x,y);
    int len=log(y-x+1)/log(2);
    int a=dp[x][len],b=dp[y-(1<<len)+1][len];	
    if(deep[a]>deep[b]) return b;
    else return a;
}
void dfs2(int id,LL dis){
	while(!que[id].empty()){
		node tmp=que[id].front();
		que[id].pop();
		ans[tmp.id]+=tmp.k*(dis-val[tmp.x]+num[tmp.x]*tmp.y);
	}
	for(int i=be[id];i;i=ne[i])
		if(deep[to[i]]>deep[id]){
			++num[c[i]];
			val[c[i]]+=w[i];
			dfs2(to[i],dis+w[i]);
			--num[c[i]];
			val[c[i]]-=w[i];
		}
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif
	int u,v,X,Y;
	n=read(); q=read();
	For(i,1,n-1){
		u=read(); v=read();
		add(u,v,1);
	}
	dfs(1);
	rmq();
	For(i,1,q){
		X=read(); Y=read();
		u=read(); v=read();
		que[u].push((node){i,X,Y,1});
		que[v].push((node){i,X,Y,1});
		que[ask(fir[u],fir[v])].push((node){i,X,Y,-2});
	}
	dfs2(1,0);
	For(i,1,q) printf("%lld\n",ans[i]);
	return 0;
}


Measured memory_bytes: 100936
#include<iostream>
#include<stdio.h>
#include<cctype>
#include<string>
using namespace std;

int main(){
	string a, b;
	int taro, hanako;
	int n;
	taro = 0;
	hanako = 0;

	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> a >> b;
		if (a == b){
			taro++;
			hanako++;
		}
		else if (a > b){
			taro += 3;
		}
		else{
			hanako += 3;
		}
	}

	cout << taro <<" "<< hanako << endl;

	return 0;
}

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

using namespace std;

int main() {
	int N;
	cin >> N;
	int z = 0, o =  0, t = 0;
	int cur;
	for (int i = 0; i < N; i++) {
		cin >> cur;
		if (cur % 4 == 0) t++;
		else if (cur %2 == 0) o++;
		else z++;
	}
	if (z == t+1 && o ==0) {
		cout << "Yes" << endl;
	}
	else if (z > t) {
		cout << "No" << endl;
	}
	else cout << "Yes" << endl;
	// cin >> N;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define int long long int
using namespace std;
template<typename T,typename U> using P=pair<T,U>;
template<typename T> using V=vector<T>;
template<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}

template<typename T>auto&operator<<(ostream&s,const vector<T>&v){s<<"[";bool a=1;for(auto e:v){s<<(a?"":" ")<<e;a=0;}s<<"]";return s;}
template<typename T,typename U>auto&operator<<(ostream&s,const pair<T,U>&p){s<<"("<<p.first<<","<<p.second<<")";return s;}
template<typename T>auto&operator<<(ostream&s,const set<T>&st){s<<"{";bool a=1;for(auto e:st){s<<(a?"":" ")<<e;a=0;}s<<"}";return s;}
template<typename T,typename U>auto&operator<<(ostream&s,const map<T,U>&m){s<<"{";bool a=1;for(auto e:m){s<<(a?"":" ")<<e.first<<":"<<e.second;a=0;}s<<"}";return s;}
#define DUMP(x)  cerr<<#x<<" = "<<(x)<<endl;

struct edge { int to, cost; };

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

signed main()
{
   int n, m; cin >> n >> m;
   V<V<int>> G(n);
   for (int i = 0; i < m; i++) {
      int a, b; cin >> a >> b; a--; b--;
      G[a].push_back(b);
      G[b].push_back(a);
   }
   int q; cin >> q;
   V<int> vs(q), ds(q), cs(q);
   for (int i = 0; i < q; i++) {
      cin >> vs[i] >> ds[i] >> cs[i];
      vs[i]--;
   }

   int maxd = *max_element(ds.begin(), ds.end());
   V<V<int>> dp(n, V<int>(maxd+1, -1));
   for (int i = 0; i < q; i++) {
      dp[vs[i]][ds[i]] = i;
   }
   for (int d = maxd; d > 0; d--) {
      for (int v = 0; v < n; v++) {
         for (int i = 0; i < G[v].size(); i++) {
            int u = G[v][i];
            chmax(dp[u][d-1], dp[v][d]);
         }
         chmax(dp[v][d-1], dp[v][d]);
      }
   }
   for (int v = 0; v < n; v++) {
      cout << (dp[v][0] == -1 ? 0 : cs[dp[v][0]]) << endl;
   }

   return 0;
}


Measured memory_bytes: 20980
// B - Break Number

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

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

	int ans;
	int max_cnt = -1;

	for (int i=1; i<=N; i++) {
		int cnt = 0;
		int a = i;
		while(a % 2 == 0) {
			a /= 2;
			cnt++;
		}

		if (cnt > max_cnt) {
			ans = i;
			max_cnt = cnt;
		}
	}

	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define lper(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ins insert
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[4]={1,0,-1,0};
const ll dx[4]={0,-1,0,1};
const ll ddx[8]={ 0, 1, 0,-1, 1, 1,-1,-1};
const ll ddy[8]={ 1, 0,-1, 0, 1,-1, 1,-1};
ll modfac(ll a){
    ll ans=1;
    while(a>1){
        ans*=a;
        ans%=1000000007;
        a--;
    }
    return ans;
}
ll modinv(ll a,ll m) {
    ll b = m, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m; 
    if (u < 0) u += m;
    return u;
}
struct UnionFind {
    vector<int> par;
    
    UnionFind(int n) : par(n, -1) {}

    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    
    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y);
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    
    int size(int x) {
        return -par[root(x)];
    }
};
ll modpow(ll a, ll n, ll mod) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
int main(){
    ll n,m;cin >> n >>m;
    vpl v(n);
    rep(i,n)cin >> v[i].fi >>v[i].se;
    sort(all(v));
    ll ans=0;
    ll x=0,y=0;
    while(x<m){
        ans+=v[y].fi*v[y].se;
        x+=v[y].se;
        y++;
    }
    ans-=(x-m)*v[y-1].fi;
    cout <<ans <<endl;
}


Measured memory_bytes: 1792