akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
int dp(int n,int k,int h[])
{
    int d[n];d[0]=0;
    for(int i=1;i<n;i++)
    d[i]=INT_MAX;
    for(int i=0;i<n;i++)
    {
        for(int j=1;j<=k;j++)
        {
            if(i+j<n)
            d[i+j]=min(d[i+j],d[i]+abs(h[j+i]-h[i]));
        }
    }
    return d[n-1];
}
int main()
{
    int n,k;
    cin>>n>>k;
    int h[n];
    for(int i=0;i<n;i++)
    cin>>h[i];
    cout<<dp(n,k,h)<<endl;
    return 0;
}


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

namespace AtCoder.Abc
{
    public class QuestionA
    {
        public static void Main(string[] args)
        {
            // 整数配列の入力
            var inputLongArray = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray();

            var N = inputLongArray[0];
            var A = inputLongArray[1];
            var B = inputLongArray[2];
            int result = 0;
            for(int i = 1; i <= N; ++i) {
                var str = i.ToString();
                var len = str.Length;
                int calc = 0;
                for(int j = 0; j < len; ++j) {
                    calc += int.Parse(str.Substring(j,1));
                }

                if(A <= calc && calc <= B) {
                    result += i;
                }
            }

            Console.WriteLine(result);
        }
    }
}


Measured memory_bytes: 13396
a,b = [],[]

for i in range(10):
    a.append(int(input()))
    
for i in range(10):
    b.append(int(input()))
    
a.sort(reverse=True)
b.sort(reverse=True)

print(a[0]+a[1]+a[2],b[0]+b[1]+b[2])


Measured memory_bytes: 5596
#include<bits/stdc++.h>
using namespace std;
int main() {
	long n,k,i,j,ans=0;
	cin>>n>>k;
	vector<long> a(n), path;
	vector<bool>visited(n,false);
	for (i=0;i<n;++i)
		cin>>a[i];
	path.push_back(0);
	visited[0]=true;
	for (i=1;i<=k;++i)
	{
		ans=a[ans]-1;
		if (visited[ans])
			break;
		path.push_back(ans);
		//cout<<ans<<' ';
		visited[ans]=true;
	}
	int l=path.size();
	for (j=0;j<l;++j)
		if (path[j]==ans)
			break;
	int l1=l-j;
	k-=i;
	//cout<<'\n'<<k<<'\n';
	k%=l1;
	ans=path[j+k]+1;
	cout<<ans;
	return 0;
}


Measured memory_bytes: 6880
N = gets.to_i
max = 100
maxN = 10**4

cnts = Array.new(maxN+1, 0)
1.upto(max) do |x|
    1.upto(max) do |y|
        1.upto(max) do |z|
            n = x**2 + y**2 + z**2 + x*y + y*z + z*x
            if n <= maxN
                cnts[n] += 1
            end
        end
    end
end

1.upto(N) do |i|
    puts cnts[i]
end

Measured memory_bytes: 14364
#include <stdio.h>
#include <algorithm>
#define MN 100000
struct SEG{int l,r;bool u;}s[MN+5];
int n,now;
long long ans1=0,ans2=0;
int l[MN+5],r[MN+5];
bool cmp1(int a,int b){return s[a].r<s[b].r;}
bool cmp2(int a,int b){return s[a].l>s[b].l;}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&s[i].l,&s[i].r);
		l[i]=i,r[i]=i;
	}
	std::sort(l+1,l+1+n,cmp1);
	std::sort(r+1,r+1+n,cmp2);
	#define ans ans1
	now=0;
	for(int i=1;i<=n;i++)
		s[i].u=false;
	for(int i=1,j=1;i<=n&&j<=n;){
		while(s[l[i]].u&&i<=n)i++;
		if(i>n)break;
		if(!(s[l[i]].l<=now&&s[l[i]].r>=now)){
//			printf("goto %d\n",l[i]);
			ans+=std::abs(now-s[l[i]].r);
			now=s[l[i]].r;
		}
		s[l[i]].u=true;
		while(s[r[j]].u&&j<=n)j++;
		if(j>n)break;
		if(!(s[r[j]].l<=now&&s[r[j]].r>=now)){
////			printf("goto %d\n",r[j]);
			ans+=std::abs(now-s[r[j]].l);
			now=s[r[j]].l;
		}
		s[r[j]].u=true;
	}
	ans+=std::abs(now);
	#undef ans
	#define ans ans2
	now=0;
	for(int i=1;i<=n;i++)
		s[i].u=false;
	for(int i=1,j=1;i<=n&&j<=n;){
		while(s[r[j]].u&&j<=n)j++;
		if(j>n)break;
		if(!(s[r[j]].l<=now&&s[r[j]].r>=now)){
//			printf("goto %d\n",r[j]);
			ans+=std::abs(now-s[r[j]].l);
			now=s[r[j]].l;
		}
		s[r[j]].u=true;
		while(s[l[i]].u&&i<=n)i++;
		if(i>n)break;
		if(!(s[l[i]].l<=now&&s[l[i]].r>=now)){
//			printf("goto %d\n",l[i]);
			ans+=std::abs(now-s[l[i]].r);
			now=s[l[i]].r;
		}
		s[l[i]].u=true;
	}
	ans+=std::abs(now);
	#undef ans
	printf("%lld",std::max(ans1,ans2));
} 

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

int main()
{
    int a, b;
    cin >> a >> b;

    if(a >= 10 || b >= 10) {cout << -1 << endl; return 0;}
    else { cout << a * b << endl; return 0;}
}

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

int main() {
    int N, K; cin >> N >> K;
    vector<long long> p(N);
    for (int i = 0; i < N; i++) {
        cin >> p[i];
    }
    vector<long long> sum(N+1);
    sum[0] = p[0];
    for (int i=0; i < N; i++) {
        sum[i+1] = sum[i] + p[i];
    }

    long long ans = 0;
    for (int i = 0; i+K <= N; ++i) {
        ans = max(ans, sum[i+K] - sum[i]); 
    }
    cout << fixed << setprecision(10) << (double)(ans+K)/2 << endl;
    return 0;
}

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

const int  mod = 1000000007;
struct mint{
    ll x;
    mint (ll x=0) : x((x%mod+mod)%mod){}//カッコの中で初期化している
    mint& operator+=(const mint a){//operatorは構造体同士の比較に用いられる
        if((x +=a.x)>=mod) x-=mod;
        return *this;//thisとは現在のオブジェクトのこと
    }
    mint& operator-=(const mint a){
        if((x +=mod-a.x)>=mod) x-=mod;
        return *this;
    }
    mint& operator*=(const mint a){
        (x*=a.x)%=mod;
        return *this;
    }
    mint operator+(const mint a){
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a){
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a){
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const{
        if(!t) return 1;
        mint a=pow(t>>1);
        a*=a;
        if(t&1) a*=*this;
        return a;
    }
    mint inv() const{
        return pow(mod-2);//逆元
    }
    mint&operator/=(const mint a) {
        return (*this)*=a.inv();
    }
    mint&operator/(const mint a) const{
    mint res(*this);
    return res/=a;
    }
};
 
int main(void){
    ll n;
    ll k;
    cin>>n>>k;
    vector<ll> v(n);
    rep(i, n) cin>>v[i];
    vector<ll>bigger(n);
    vector<ll> sortv=v;
    sort(sortv.begin(), sortv.end());
    ll now = 0;
    ll sum = 0;
    rep(i, n){
        if(sortv[i]>now) {
            sum = (ll)i;
            now = sortv[i];
        }
        bigger[i] = sum;
    }
    mint ans = 0;
    rep(i, n){
        mint a = k;
        a*=(k-1);
        a/=2;
        a*=bigger[i];
        ans += a;
    }
    rep(i, n-1){
        for(int j = i+1;j<=n-1;j++){
            if(v[i]>v[j]) ans += k;
        }
    }
    cout<<ans.x<<endl;
    return 0;
}

Measured memory_bytes: 256
import sys
input = sys.stdin.readline
s = input().rstrip()
l = len(s)
k = int(input())
chk = set()
for i in range(26):
  for j in range(l):
    if s[j] == chr(i+97):
      for m in range(min(5,l-j)):
        chk.add(s[j:j+1+m])
  if len(chk) >= k:
    break
    
chk = sorted(list(chk))
print(chk[k-1])

Measured memory_bytes: 3064
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <unordered_map>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD=1e9+7;
ll powmod(ll a, ll k){
    ll ap=a, ans=1;
    while(k>0){
        if(k%2==1){
            ans*=ap;
            ans%=MOD;
        }
        ap=ap*ap;
        ap%=MOD;
        k/=2;
    }
    return ans;
}
ll inv(ll a){
	return powmod(a, MOD-2);
}
int main()
{
	ll dp[301][301]={};
  ll s[301][301]={};
  ll f[301], invf[301];
  f[0]=1;
  for(ll i=1; i<=300; i++){
    f[i]=f[i-1]*i%MOD;
  }
  invf[300]=inv(f[300]);
  for(ll i=299; i>=0; i--){
    invf[i]=invf[i+1]*(i+1)%MOD;
  }
  ll comb[301][301];
  for(int i=0; i<=300; i++){
    for(int j=0; j<=i; j++){
      comb[i][j]=f[i]*invf[j]%MOD*invf[i-j]%MOD;
    }
  }
  int n, m; cin>>n>>m;
	fill(dp[1], dp[1]+m+1, 1);
  for(ll i=2; i<=n; i++){
    dp[i][i]=f[i-1];
    for(ll j=i+1; j<=m; j++){
      dp[i][j]=i*dp[i][j-1]%MOD;
      for(ll x=1; x<=i-1; x++){
        for(ll y=x-1; y<=min(j-2, j-i+x); y++){
          dp[i][j]+=(comb[i-1][x]*x%MOD*dp[x][y]%MOD*dp[i-x][j-1-y]%MOD);
          dp[i][j]%=MOD;
        }
      }
    }
  }
  cout<<dp[n][m]<<endl;
	return 0;
}

Measured memory_bytes: 1664
#include <iostream>

using namespace std;

unsigned long a[100100];

int main(void)
{
	int k;
	unsigned long min, max;
	scanf("%d", &k);

	for (int i = 0; i < k; i++)
		scanf("%ld", &a[i]);

	max = 3, min = 2;

	if (a[k - 1] != 2) {
		cout << -1 << endl;
		return 0;
	}

	for (int i = k - 2; i >= 0; i--) {
		if (a[i] > max) {
			cout << -1 << endl;
			return 0;
		} else {
			max = ((max / a[i]) + 1) * a[i] - 1;
			if (min % a[i] != 0) {
				min = ((min / a[i]) + 1) * a[i];
			}
		}

		if (max <= min) {
			cout << -1 << endl;
			return 0;
		}
	}

	printf("%ld %ld\n", min, max);

    return 0;
}


Measured memory_bytes: 1024
package main

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

// const abcd = "abcdefghijklnmopqrstuvwxyz"
// const ABCD = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

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

func main() {
	log.SetFlags(log.Lshortfile)
	scn := newScanner()
	N := scn.nextInt()
	D := map[int]int{}
	for i := 0; i < N; i++ {
		d := scn.nextInt()
		D[d]++
	}
	M := scn.nextInt()
	for i := 0; i < M; i++ {
		t := scn.nextInt()
		if D[t] == 0 {
			fmt.Println("NO")
			return
		} else {
			D[t]--
		}
	}
	fmt.Println("YES")
}

///////////////////////////////////////////////////
func max(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r < a[i] {
			r = a[i]
		}
	}
	return r
}

func min(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r > a[i] {
			r = a[i]
		}
	}
	return r
}

type scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func newScanner() *scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 10000)
	return &scanner{r: rdr}
}
func (s *scanner) next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *scanner) nextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *scanner) nextInt() int {
	v, _ := strconv.Atoi(s.next())
	return v
}
func (s *scanner) nextInt64() int64 {
	v, _ := strconv.ParseInt(s.next(), 10, 64)
	return v
}
func (s *scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}


Measured memory_bytes: 17408
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0; i<n; i++)
typedef long long ll;
typedef long long unsigned int llu;
ll MOD = 1000000007;
ll INF = 1000000009;
ll INF2 = 100000000000000009;

struct ver{
    ll p;
    ll q;
    ll r;
};

void solve(){

    ll n,m;
    cin >> n >> m;
    ver v[m];
    rep(i,m){
        ll a,b,c;
        cin >> a >> b >> c;
        a--;
        b--;
        c = -c;
        v[i]=ver{a,b,c};
    }
    ll dis[n];
    rep(i,n){
        dis[i]=INF2;
    }
    dis[0]=0;

    rep(i,n){
        rep(j,m){
            ver tmp = v[j];
            dis[tmp.q]=min(dis[tmp.q],dis[tmp.p]+tmp.r);
        }
    }

    ll dis2[n];
    rep(i,n){
        dis2[i]=dis[i];
    }
    rep(j,m){
        ver tmp = v[j];
        dis[tmp.q]=min(dis[tmp.q],dis[tmp.p]+tmp.r);
    }

    bool b=true;
    /*
    rep(i,n){
        if(dis[i]!=dis2[i]){
            b=false;
            break;
        }
    }
    */
    if(dis2[n-1]!=dis[n-1]) b=false;

    if(b){
        cout << -dis[n-1] << endl;
    }
    else{
        cout << "inf" << endl;
    }

}

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


Measured memory_bytes: 384
s = gets.chomp.chars
k = gets.to_i
n = s.size

i = 0
chunk = []
cur = s[i]
cnt = 1
i += 1
i.step(n - 1) do |j|
  if s[j] == cur
    cnt += 1
  else
    chunk << cnt
    cnt = 1
    cur = s[j]
  end
end
chunk << cnt

res = 0
if chunk.size == 1
  res += n * k / 2
else
  if s[0] == s[-1]
    pr = chunk.shift
    su = chunk.pop
    res += pr / 2
    res += su / 2
    res += ((pr + su) / 2) * (k - 1)
  end
  
  chunk.each do |c|
    res += (c / 2) * k
  end
end
puts res

Measured memory_bytes: 14300
N = int(input())
W = [input() for _ in range(N)]

flag = True
if N == len(set(W)):
  for i in range(1, N):
    if W[i - 1][-1] != W[i][0]:
      flag = False
      break
else:
  flag = False
print('Yes' if flag else 'No')


Measured memory_bytes: 3060
mod = 10**9+7
n,m = map(int,input().split())
broken = set()
for i in range(m):
    broken.add(int(input()))
dp=[1,2]
if 1 in broken:
    dp[0] = 0
    dp[1] = 1
if 2 in broken:
    dp[1] = 0
for i in range(2,n):
    if i+1 in broken:
        dp.append(0)
    else:
        dp.append((dp[i-2]+dp[i-1]) % mod)
print(dp[n-1] % mod)    



Measured memory_bytes: 11884
# coding: utf-8
# Your code here!

n,m = map(int,input().split(' '))

bridge = [list(map(int,input().split(' '))) for i in range(m)]

bridge = sorted(bridge,key=lambda x:x[1])
# print(bridge)

index=0
ans=0
for i in range(m):
    if index <= bridge[i][0]:
        index = bridge[i][1]
        ans += 1
print(ans)

Measured memory_bytes: 29876
#include <iostream>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <limits>
#include <algorithm>
#include <functional>

#include <vector>

using namespace std;

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

	long long N, P;
	cin >> N >> P;

	string S;
	cin >> S;

	long long result = 0;
	if (P == 2 || P == 5)
	{
		for (int i = 0; i < N; i++)
		{
			if ((S[i] - '0') % P == 0)
			{
				result += (i + 1);
			}
		}
	}
	else
	{
		vector<int> base(N, 1);
		for (int i = N - 1; i >= 1; i--)
		{
			base[i - 1] = (base[i] * 10) % P;
		}

		vector<long long> mod_count(P, 0);
		mod_count[0] = 1;

		long long mod = 0;
		for (int i = 0; i < N; i++)
		{
			mod = (mod + (S[i] - '0') * base[i]) % P;
			mod_count[mod]++;
		}

		for (long long val : mod_count)
		{
			result += (val * (val - 1)) / 2;
		}
	}

	cout << result;

	return 0;
}

Measured memory_bytes: 1428
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int s[200005];
ll ans[200005];
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		scanf("%d",s+i);
	}
	ll ans=0,cnt=0;
	for(int i=2;i<=n;i++){
		if(s[i]>=i || s[i]>s[i-1]+1){
			cout<<-1;
			return 0;
		}
		if(s[i]==s[i-1]+1) ans++;
		else{
			ans+=s[i];
		}		
	}
	if(s[1]){ans=-1;}
	cout<<ans;
	return 0;
}

Measured memory_bytes: 1024
#include <stdio.h>

int main(void)
{
	int i;
	int total;
	int in;
	
	total = 0;
	for (i = 0; i < 4; i++){
		scanf("%d", &in);
		total += in;
	}
	printf("%d\n%d\n", total / 60, total % 60);
	
	return (0);
}

Measured memory_bytes: 592
#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;
 
int main(){
 
ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,x,i,count=1,c=0;
    cin>>n>>x;
    int l[n];
    for(i=0;i<n;i++)
    cin>>l[i];
    for(i=1;i<=n;i++)
    {
    	if(c+l[i-1]<=x)
    	{
    		c=c+l[i-1];
    		count++;
		}
		else
		{
			
			break;
		}
	}
	cout<<count;
    
    
    
	
 
}

Measured memory_bytes: 256
n=int(input())
a=input().split()
for i in range(n):
        if n > 0: 
                print(a[n-1],end=" ")
                n=n-2
                if n == 0 or n == -1:
                        n=n-1
        elif n < 0: 
                print(a[-1*n-1],end=" ")
                n=n-2
print("")

Measured memory_bytes: 20176
#include<bits/stdc++.h>
using namespace std;
#define rep0(i,n) for(i=0;i<n;i++)
#define rep1(i,n) for(i=1;i<=n;i++)
#define test(a) cout << "*" << a << endl
#define ll long long
int main(){
  int i,j;
  int n,m,q;
  int a[51];
  int b[51];
  int c[51];
  ll d[51];
  int A[11];
  ll sum=0;
  ll sumMax=0;
  int countM=0;
  
  
  
  cin >> n/*Aの長さ*/ >> m/*数の最大値*/ >> q/*得点の数*/;
  rep0(i,q){
    cin >> a[i] >> b[i] >> c[i] >> d[i];   
  }
  /*rep0(i,q){
    cout << a[i] << b[i] << c[i] << d[i] << endl;
  }*/
  for(i=1;i<=n;i++){
    A[i] = 1;
  }
  /*for(i=1;i<=n;i++){
    cout << A[i] << endl;
  }*/
   
  //digitMax = m; 
  
  while(1){    
    for(i=0;i<q;i++){
      if(A[b[i]] - A[a[i]] == c[i]){
        sum += d[i];
      }
    }
    if(sumMax < sum){
      sumMax = sum;  
    }
    if(A[1]==m){
      break;  
    }
    sum = 0;
    /*for(i=digitMax;i<=m;i++){
      if(A[i]==m){
        countM++;  
      }
    }*/
    //while(A[1]<m){
      i=n;
      while(A[i]==m){
        countM++;
        i--;
      }
      if(countM > 0){
        A[i] += 1;
        for(j=1;j<=countM;j++){
          A[i+j] = A[i];
        }
      }
      else{
        A[n] += 1;
      }      
      countM = 0;
    //}
  }
  
  cout << sumMax << endl;
    
    
    /*if(countM==n-digitMax+1){
      A[digitMax-1] += 1;
      A[digitMax] = A[digitMax-1];
      digitMax = digitMax-1;
      saiki();
    }
    else{
      A[q] += 1;
    }
  }*/

}

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

string S;
int main(){
  cin >> S;
  int N = S.size();
  long long sum =0;
  for (int bit=0; bit <(1 << N-1); ++bit){
    string s ="";
    s.push_back(S[0]);
    for(int i=0; i<N-1; ++i){
      if (bit & (1 <<i)){
        sum += stoll(s);
        s = "";
      }
      s.push_back(S[i+1]);
    }
    sum += stoll(s);
  }
  cout << sum << endl;
  return 0;
}

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

// 型定義
typedef long long ll;
typedef pair<ll, ll> P;

// forループ
#define REP(i,n) for(ll i=0; i<(ll)(n); ++i)

// 定数宣言
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;

// グラフ表現
using Graph = vector<vector<int>>;

// グラフの辺表現
using Edge = map<pair<int,int>,int>;

// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

// 最大公約数
ll gcd(ll a,ll b){
   if (a%b == 0) return(b);
   else return(gcd(b, a%b));
}

// 最小公倍数
ll lcm(ll a, ll b){
    return a/gcd(a, b) * b;
}

int main()
{
    cout << fixed << setprecision(15);
    string A, B, C;
    cin >> A >> B >> C;

    ll a, b, c;
    a = A.length();
    b = B.length();
    c = C.length();

    if(A[a-1] == B[0] && B[b-1] == C[0]){
        cout << "YES" << endl;
    }
    else{
        cout << "NO" << endl;
    }
    return 0;
}

Measured memory_bytes: 3556
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
typedef long long ll;
using namespace std;

int main(void){
  ll n,i;
  ll t=0;
  pair <ll,ll> p[200000];
  cin>>n;

  for(i=0;i<n;i++){
    cin>>p[i].second>>p[i].first;
  }
  sort(p,p+n);

  for(i=0;i<n;i++){
    t+=p[i].second;
    if(p[i].first<t){
      cout<<"No"<<endl;
      return 0;
    }
  }

  cout<<"Yes"<<endl;

  return 0;
}

Measured memory_bytes: 3328
#include<iostream>

using namespace std;

int main(void)
{
	int x;
	cin >> x;

	int ans=1;

	for(int i=2;i<=x;i++)
	{
		for(int j=2;j<=x;j++)
		{
			int now = 1;
			for(int k=1;k<=j;k++)
				now*=i;
			if(now>x)
				break;
			ans=max(ans,now);
		}
	}

	cout << ans << endl;
}

Measured memory_bytes: 256
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i,x) for(int i = 0; i < (int)(x); ++i)
#define REPS(i,x) for(int i = 1; i <= (int)(x); ++i)
#define RREP(i,x) for(int i = ((int)(x)-1); i >= 0; i--)

int n, a, b, c;
string s;

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

    int a, b;
    cin >> a >> b;
    cout << ((a*b)%2 == 0 ? "Even" : "Odd") << '\n';

    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
 
using namespace std;
 
#define INF 1000000007
#define LINF 1000000000000000007
 
typedef long long ll;
typedef pair<ll,ll> P;


int x,y;

int main(){
	cin >>x >> y;
	
	cout << x+ y/2 <<endl;
	
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <math.h>
#include <map>
#include <vector>
#include <algorithm>
#define REP(i, n) for(int i = 0; i < (n); i++)

using namespace std;

int main()
{
	int n, k;
	cin >> n >> k;
	int sunuke[1000] = {};
	REP(i, k) {
		int d;
		cin >> d;
		REP(j, d) {
			int a; cin >> a;
			sunuke[a] ++;
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		if (!sunuke[i])ans++;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3596
#include <iostream>

using namespace std;

int main()
{
	char c1[4];
	char c2[4];
	cin >> c1 >> c2;
	if (c1[0] == c2[2] && c1[1] == c2[1] && c1[2] == c2[0])
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}

Measured memory_bytes: 256
Scanf.scanf "%d" (fun n ->
    let h = [| Array.make 100001 0; Array.make 100001 0 |] in
    let module S = Set.Make (struct type t = int * int let compare = compare end) in
    for i = 0 to n - 1 do
        Scanf.scanf " %d" (fun v ->
            h.(i mod 2).(v) <- h.(i mod 2).(v) + 1;
        )
    done;
    let rec loop i set1 set2 =
        if i = 100001 then set1, set2 else
            let set1 = if h.(0).(i) > 0 then S.add (h.(0).(i), i) set1 else set1 in
            let set2 = if h.(1).(i) > 0 then S.add (h.(1).(i), i) set2 else set2 in
            loop (i + 1) set1 set2
    in
    let set1, set2 = loop 0 S.empty S.empty in
    let ((v1, i1) as m1) = S.max_elt set1 in
    let ((v2, i2) as m2) = S.max_elt set2 in

    Printf.printf "%d\n" @@
    if i1 <> i2 then n - v1 - v2 else (
        let c1 = S.cardinal set1 in
        let c2 = S.cardinal set2 in
        if c1 > 1 && c2 > 1 then (
            let (v3, i3) = S.max_elt (S.remove m1 set1) in
            let (v4, i4) = S.max_elt (S.remove m2 set2) in
            min (n - v3 - v2) (n - v1 - v4)
        ) else if c1 > 1 then (
            let (v3, i3) = S.max_elt (S.remove m1 set1) in
            n - v3 - v2
        ) else if c2 > 1 then (
            let (v4, i4) = S.max_elt (S.remove m2 set2) in
            n - v1 - v4
        ) else n / 2
    )
)

Measured memory_bytes: 13764
all = 0
num = gets.chomp.split.map!(&:to_i)
high = gets.chomp.split.map!(&:to_i)
way = [0,0]
array = Array.new(num[0]).map{Array.new(2,0)}

(1..num[1]).each{ |j|
  way = gets.chomp.split.map!(&:to_i)
  array[way[0]-1][0] += 1
  array[way[1]-1][0] += 1
  if high[way[0]-1] > high[way[1]-1]
    array[way[0]-1][1] += 1
  elsif high[way[0]-1] < high[way[1]-1]
    array[way[1]-1][1] += 1
  end
}

(0..num[0]-1).each{ |k|
  if array[k][0] == array[k][1] then
    all += 1
  end
}
puts all

Measured memory_bytes: 24732
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e6 + 5;
int main()
{
    int n;
    scanf("%d",&n);
    if(n%2==0)printf("0.5000000000\n");
    else printf("%.10lf",(1.0)*(n+1)/2/n);
    return 0;
}


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

typedef int ll;
typedef vector<ll> v1;
typedef vector<vector<ll>> graph;

const ll INF = 1 << 29;
ll n,m,p;
v1 a,b,c;
graph g;
graph rg;
graph cost;
v1 dist;
bool is_update;
v1 vis;
v1 mem;

void rdfs(ll x){
    vis[x] = 1;
    ll siz = rg[x].size();
    for(ll y : rg[x]){
        if(vis[y] == 0) rdfs(y);
    }
}

void dfs(ll x){
    // if(vis[x]) return;
    vis[x] = 1;
    ll siz = g[x].size();
    for(ll i = 0;i < siz;i++){
        if(mem[g[x][i]]){
            if(vis[g[x][i]] == 0) dfs(g[x][i]);
            a.push_back(x);
            b.push_back(g[x][i]);
            c.push_back(cost[x][i]);
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m >> p;
    dist = v1(n+1, -INF);
    // a=b=c=v1(m);
    g = graph(n+1);
    rg = graph(n+1);
    cost = graph(n+1);
    for(ll i = 0;i < m;i++){
        ll a,b,c;
        cin >> a >> b >> c;
        rg[b].push_back(a);
        g[a].push_back(b);
        cost[a].push_back(c-p);
    }
    vis = v1(n+1,0);
    rdfs(n);
    mem = vis;
    vis = v1(n+1,0);
    dfs(1);
    // for(ll i = 0;i < a.size();i++){
    //     cerr << a[i] << " " << b[i] << " " << c[i] << endl;
    // }
    dist[1] = 0;
    ll siz = a.size();
    for(ll i = 0;i < n+1;i++){
        is_update = false;
        for(ll j = 0;j < siz;j++){
            if(dist[b[j]] < dist[a[j]]+c[j]){
                dist[b[j]] = dist[a[j]]+c[j];
                is_update = true;
            }
        }
        if(is_update == false) break;
    }
    if(is_update) cout << -1 << endl;
    else cout << max(dist[n],0) << endl;
    // if(bellman_ford()){
    //     cout << max(dist[n],0ll) << endl;
    // }else{
    //
    // }
    // bellman_ford();
    // ll tmp = dist[n];
    // bellman_ford();
    // if(tmp == dist[n]) cout << max(dist[n],0ll) << endl;
    // else cout << -1 << endl;
    return 0;
}


Measured memory_bytes: 896
#include<cstdio>
#include<cstring>
const int maxn=200010;
char s[maxn];
int n;
long long w;
int main() {
	scanf("%s",s+1),n=strlen(s+1);
	register int p=0;
	for(register int i=1;i<=n;++i)if(s[i]=='W') ++p,w+=i-p;
	printf("%lld\n",w);
	return 0;
} 

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

int gcd(int a, int b){
    while(a != b){
        if(a > b) a = a - b; else b = b - a;
    }
    return a;
}

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

int main(){
    int h, w;
    while(true){
char torus[10][20];
        string str[2048];
        bool ok[2048];
        int num = 0;

        for(int i=0; i<2048; ++i){
            str[i] = "";
            ok[i] = true;
        }

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

        int cml = lcm(w, h);

        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                cin >> torus[i][j];
            }
        }

        //一番長い文字列をつくる

        for(int y=0; y<h; ++y){
            for(int x=0; x<w; ++x){
                for(int i=0; i<h; ++i){
                    str[num] += torus[(y+i)%h][x];
                    str[num+1] += torus[h-(y+i)%h-1][x];
                }
                num += 2;
            }
        }
        for(int y=0; y<h; ++y){
            for(int x=0; x<w; ++x){
                for(int i=0; i<w; ++i){
                    str[num] += torus[y][(x+i)%w];
                    str[num+1] += torus[y][w-(x+i)%w-1];
                }
                num += 2;
            }
        }
        for(int y=0; y<h; ++y){
            for(int x=0; x<w; ++x){
                for(int i=0; i<cml; ++i){
                    str[num] += torus[(y+i)%h][(x+i)%w];
                    str[num+1] += torus[(y+i)%h][w-(x+i)%w-1];
                    str[num+2] += torus[h-(y+i)%h-1][(x+i)%w];
                    str[num+3] += torus[h-(y+i)%h-1][w-(x+i)%w-1];
                }
                num += 4;
            }
        }


        string result = "";

        sort(str, str+num);

        for(int i=num-1; i>0; --i){
            int length = 0;
            string k = "";
            for(int j=0; j<min(str[i].length(), str[i-1].length()); ++j){
                if(str[i][j] == str[i-1][j]) k += str[i][j]; else break;
            }
            if(k.length() >= result.length()){
                result = k;
            }
        }

        cout << ((result.length() == 1) ? "0" : result) << endl;

    }

    return 0;
}

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

char pm(int n) {
  return (n == 1) ? '+' : '-';
}

int main() {
  char c;
  int a[4], i, j, k;
  for (i = 0; i < 4; i++) {
    cin >> c; a[i] = c-'0';
  }
  for (i = -1; i <= 1; i+= 2)
    for (j = -1; j <= 1; j += 2)
      for (k = -1; k <= 1; k += 2)
        if (a[0]+i*a[1]+j*a[2]+k*a[3] == 7)
          goto end;
  end:
  cout << a[0] << pm(i) << a[1] << pm(j) << a[2] << pm(k) << a[3] << "=7" << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
using namespace std;
  
int n;
int m;
int b[300000];
long long c[300000];
 
long long dp[1111111];
long long anum[1111111];
 
long long getMax(int abili){
  if(abili==0){
    return 0;
  }else{
    if(dp[abili]<0){
      int i=abili;
      while(i>=0 && dp[i]<0)
	i--;
      if(i<0)
	i=0;
      
      for(int j=i;j<abili;j++){
	long long res=dp[j];
	if(res<0)res=0;
	res+=anum[j+1]*(j+1);
	dp[j+1]=res;
      }
      return dp[abili];
    }else{
      return dp[abili];
    }
  }
}
 
int main(){
  for(int i=0;i<1111111;i++)
    dp[i]=-1;
   
  cin>>n;
  int a;
  for(int i=0;i<n;i++){
    cin>>a;
    anum[a]++;
  }
  cin>>m;
  for(int i=0;i<m;i++)
    cin>>b[i];
  for(int i=0;i<m;i++)
    cin>>c[i];
  

  long long t;
  for(int i=0;i<m;i++){
    if(getMax(b[i])>=c[i]){
      cout<<"Yes"<<endl;
    }else{
      cout<<"No"<<endl;
    }
  }
  
  return 0;
    
}

Measured memory_bytes: 22036
#include <stdio.h>

int main(void){
	int high[10] = {0};
	int a = 0;
	int i = 0 , j = 0 , k = 0;
	unsigned char bo = 0;
	
	for(i=0;i<=9;i++)
		scanf("%d",&high[i]);
	
	do{
		bo = 0;
		for(j = 0 ; j <= 8 ; j++){
			if(high[j] < high[(j+1)]){
				a = high[j];
				high[j] = high[(j+1)];
				high[(j+1)] = a;
				bo = 1;
			}
		}
	}while(bo);
	
	printf("%d\n",high[0]);
	printf("%d\n",high[1]);
	printf("%d\n",high[2]);
	
	return (0);
}

Measured memory_bytes: 600
#include<bits/stdc++.h>
typedef  long long int ll;
typedef  long double ld;
#define sync ios_base::sync_with_stdio(false); cin.tie(NULL) 
#define input(arr,n) for(ll i1=0;i1<n;i1++ )cin>>arr[i1]
#define mod 1000000007
#define F first
#define S second 
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3")
//recursions\
#pragma comment(linker, "/stack:200000000")
//loops
#pragma GCC optimize("unroll-loops")
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>//s.order_of_key(val) *s.find_by_order(ind)
using namespace std;
const int N = 1e6+1;
int freq[N];
int main()
{
	sync;
	int n;
	cin >> n;
	int arr[n];	
	input(arr, n);
	int ans = 0; 
	for(int i = 0; i < n; i++)
		freq[arr[i]]++;
	
	for(int i = 0; i < n; i++)
	{
		if(arr[i] == 1 && freq[1] == 1)
		{
			ans++;
			continue;
		}
		if(freq[arr[i]] > 1 || freq[1])
			continue;
		int flag = 1;
		for(int j = 2; j*j <= arr[i]; j++)
		{
			if(arr[i] % j == 0)
			{
				if(freq[j] || freq[arr[i]/j])
				{
					flag = 0;
					break;
				}
			}
		}	
		if(flag)
			ans++;
	}
	cout << ans;
}
	

Measured memory_bytes: 8160
#include<bits/stdc++.h>
using namespace std;
 
char x[100000];
int y;
int rez=100000000;
int main(){
    cin>>x; 
    y=strlen(x);
    for(int i=0;i<y;i++){
        if(x[i]!=x[i+1]){ rez=min(rez,max(i+1,y-i-1));}
    }
    cout << rez;
}

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

long long int ans(long long int pos, int k, int step){
	if(step==1) return pos;
	long long int nextpos = pos + (pos+k-1)/(k-1);
	return ans(nextpos, k, step-1);
}

int main(){
	int n,k;
	cin >> n >> k;
	cout << ans(0,k,n) << endl;
	return 0;
}

Measured memory_bytes: 3084
#include <algorithm>
#include <array>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <queue>

using namespace std;

struct BoolName : numpunct<char> {
  string t, f;
  BoolName (string t = "Yes", string f = "No") : t(t), f(f) {}
  string do_truename() const {return t;}
  string do_falsename() const {return f;}
};

struct Initializer {
  Initializer() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(15) << boolalpha;
    cout.imbue(locale(cout.getloc(), new BoolName));
  }
} initializer;

template<typename T> istream& operator>>(istream &s, vector<T> &v) {
  for (T &t : v) s >> t;
  return s;
}

template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
  for (const T &t : v) s << t << endl;
  return s;
}

void set_bool_name(string t, string f) {
  cout.imbue(locale(cout.getloc(), new BoolName(t, f)));
}

int main() {
  string s;
  cin >> s;
  cout << count(s.begin(), s.end(), '1') << endl;
}



Measured memory_bytes: 256
#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
int main() {
	int a, b;
	while(cin >> a >> b) {
		a += b;
		a = (int)log10((double)a) + 1;

		cout << a << endl;
	}
	return 0;
}

Measured memory_bytes: 1204
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll prime(ll x) {
	ll i;
	if (x < 2)return 0;
	else if (x == 2) return 1;
	if (x % 2 == 0) return 0;
	for (i = 3; i*i <= x; i += 2) {
		if (x%i == 0) return 0;
	}
	return 1;
}
int main() {
	int N;
	while (cin >> N) {
		int cnt = 0;
		for (int i = 1; i <= N; i++) {
			if (prime(i) && prime(N - i + 1))
				cnt++;
		}
		cout << cnt << endl;
	}
}





Measured memory_bytes: 3052
// warm heart, wagging tail,and a smile just for you!
//
//                            ▒█████▒▒
//                             ██████████▒
//                             ▒████████████▒
//                            ██████████████████
//                           ████████████████████▒
//                          ▒██████████████████████▒
//                          ▒███████████████████████
//                      ▒████▒▒▒▒▒▒█████████████████▒
//                    ███▒▒▒▒▒▒██████████████████████▒▒▒
//                  ▒██▒▒███████████████████████▒▒▒▒▒██████
//                 ▒█████████████████████████▒▒▒▒▒▒█████████▒
//                 ▒█████████████████████▒▒▒▒▒▒██████████████
//                  ▒████         ████▒▒▒▒▒████         ████▒
//              ▒█████▒    ████    ▒▒▒▒███████    ████    ██████▒
//            ▒██▒▒▒▒▒    ██████    █████████    ██████    ██▒▒▒██▒
//           █████████   ████████   █████████   ████████   ▒▒▒▒█████
//          ▒█████████    ██████    ████████▒    ██████    █████████
//          ▒██████████    ████    █████▒▒▒▒▒▒    ████    ██████████
//           ████████████        ▒▒▒▒▒▒▒████████        ███████████▒
//       ▒██████████▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒███████████████████████████████████▒
//     ███▒▒▒▒▒▒▒▒▒▒▒▒█████████████████████████████████████████▒▒████████▒
//   ▒▒▒▒▒▒▒▒▒██████████████                         ███████▒▒▒▒███████████
//   █████████████████████████                     ███████▒▒▒██████████████▒
//   █████████████████████████████             ███████▒▒▒██████████████████▒
//   ██████████████████████████████████████████████████████████████████████
//    ██████████████████████████████████████████████████████████████████▒
//      ▒█████████████████▒▒▒▒▒▒▒██████████████████████████████████▒▒▒
//
#include "bits/stdc++.h"
using namespace std;
#define MOD 1000000007
//#define MOD 998244353
const double EPS = 1e-9;
#define INF (1LL<<60)
#define D double
#define fs first
#define sc second
#define int long long
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define RFOR(i,a,b) for(int i = (b-1);i>=(a);--i)
#define REP(i,n)  FOR(i,0,(n))
#define RREP(i,n) RFOR(i,0,(n))
#define ITR(itr,mp) for(auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr,mp) for(auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i,a,b) ((a)<=(i) && (i)<(b))
#define debug(x)  cout << #x << " = " << (x) << endl;
#define SP << " " << 
typedef pair<int,int> P;
typedef vector<int> vec;
typedef vector<vector<int>> mat;

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

  int n;
  cin >> n;

  vector<P> a(n);
  REP(i,n) cin >> a[i].fs >> a[i].sc;
  
  auto comp = [&](P a, P b){
    return b.fs-a.sc >a.fs-b.sc;
  };
  sort(a.begin(),a.end(),comp);

  mat dp(n+1,vec(n+1,INF));
  dp[0][0] = 0;
  REP(i,n){
    REP(j,n+1){
      dp[i+1][j] = min(dp[i+1][j],dp[i][j]); //取らない
      if(dp[i][j] <= a[i].fs) dp[i+1][j+1] = min(dp[i+1][j+1],dp[i][j]+a[i].sc); //取る
    }
  }

  RREP(i,n+1){
    if(dp[n][i] != INF){
      cout << i << endl;
      return 0;
    }
  }

  return 0;
}


Measured memory_bytes: 195968
from collections import defaultdict

def dfs(u):
    st = [u]
    while st:
        u = st.pop()
        if cv[u]: continue
        cv[u] = 1
        for nxt in adj[u]:
            st.append(nxt)

n, m = [int(item) for item in input().split()]
data = []
ans = [-1] * n
adj = defaultdict(set)
for _ in range(m):
    x, y, z = [int(item) for item in input().split()]
    x -= 1
    y -= 1
    adj[x].add(y)
    adj[y].add(x)


cost = 0
cv = [0] * n
for i in range(n):
    if not cv[i]:
        cost += 1
        dfs(i)

print(cost)


Measured memory_bytes: 91992
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
typedef long long ll;
#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)
template<class T> inline bool chmax(T& a, T b) { if(a < b){ a=b; return 1; } return 0;}
template<class T> inline bool chmin(T& a, T b) { if(a > b){ a=b; return 1; } return 0;}

int main(){
	int n,q;
	cin >> n >> q;
	
	atcoder::dsu d(n);
	
	REP(i,q){
		int t,u,v;
		cin >> t >> u >> v;
		if(t==0){
			d.merge(u,v);
		}else{
			if(d.same(u,v)){
				cout << 1 << endl;
			}else{
				cout << 0 << endl;
			}
		}
	}
	
	return 0;
}

Measured memory_bytes: 4092
#include<stdio.h>
int main(void){
    int N, i, f;
    char S;
    scanf("%d", &N);
    f = 0;
    for(i = 1; i <= N; i++){
        scanf(" %c", &S); //%cの前の空白は空白類文字を読み捨てるため
        if(S == 'Y'){
            f = 1;
            break;
        }
    }

    if(f == 1) printf("Four\n");
    else printf("Three\n");

    return 0;
}


Measured memory_bytes: 128
#[allow(unused_imports)]
use proconio::{fastout, input, marker::*};

#[fastout]
fn main() {
    input! {
        s: Chars,
    }
    let mut ans = 0;
    let mut c = 0;
  
    for i in 0..s.len() {
        if s[i] == 'R' {
            c += 1;
        } else {
            c = 0;
        }
        ans = std::cmp::max(ans, c);
    }

    println!("{}", ans);
}


Measured memory_bytes: 2160
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for(ll i = 1; i < (ll)(n); i++)
const ll mo=1000000007;
const ll INF=1<<30;
ll ans=0;

struct edge {
    int to, cost;
};
vector<edge> G[200010];
bool vis[100010];
int d[100010];

bool dfs(int v,int dist){
    if(vis[v]){
        if (d[v] != dist) {
            return true;
        }
        else{
            return false;
        }
    }
    vis[v]=true;
    d[v]=dist;
    for(int i=0;i<(int)G[v].size();i++){
        if (dfs(G[v][i].to, d[v] + G[v][i].cost)) {
            return true;//その頂点における判定で一つでも矛盾があれば、全てtrueを返して強制終了。
        }
        //何も矛盾が無ければ次のリストを見る。
    }
    return false;//連結している頂点が最後まで探索できたという証。
}



int main()
{ 
ll n,m;
cin >> n>>m; 
for (int i = 0; i < m; i++) {
    int L, R, D;
    cin >> L >> R >> D;
    L--; R--;
    G[L].push_back({ R,D });
    G[R].push_back({ L,-D });
}
for(int i=0;i<n;i++){
    if(!vis[i]&&dfs(i,0)){
        cout<<"No"<<endl;
        return 0;
    }
}

cout << "Yes" << endl;
return 0;
} 

Measured memory_bytes: 14464
#include<iostream>
#include<vector>
#include<string>

int main(){
	long long mod=1000000007;
	std::string s;
	std::cin>>s;

	std::vector<std::vector<std::vector<long long>>> dpList;

	dpList.resize(s.size()+1);

	for(int i=0; i<s.size()+1; i++){
		dpList[i].resize(3);

		for(int j=0; j<3; j++)
			dpList[i][j].resize(3, 0);
	}

	std::vector<long long> pattern(100001, 1);
	for(int i=1; i<100001; i++)
		pattern[i]= (pattern[i-1]*3)%mod;

	long long qNum= 0;

	for(int i=1; i<s.size()+1; i++){
		if(s[i-1]=='A'){
			dpList[i][0][0]= (dpList[i-1][0][0] + dpList[i-1][0][1] + dpList[i-1][0][2] + pattern[qNum]) % mod;
		}
		else if(s[i-1]=='B'){
			dpList[i][0][1]= (dpList[i-1][0][0] + dpList[i-1][0][1] + dpList[i-1][0][2])%mod;
		}
		else if(s[i-1]=='C'){
			dpList[i][0][2]= (dpList[i-1][0][0] + dpList[i-1][0][1] + dpList[i-1][0][2])%mod;
		}
		else{
			dpList[i][0][0]= (dpList[i-1][0][0] + dpList[i-1][0][1] + dpList[i-1][0][2] + pattern[qNum]) % mod;
			dpList[i][0][1]= (dpList[i-1][0][0] + dpList[i-1][0][1] + dpList[i-1][0][2])%mod;
			dpList[i][0][2]= (dpList[i-1][0][0] + dpList[i-1][0][1] + dpList[i-1][0][2])%mod;
			qNum++;
		}
	}

	for(int j=1; j<s.size()+1; j++){
		if(s[j-1]=='A'){
			dpList[j][1][0]= (dpList[j-1][1][0] +dpList[j-1][1][1] + dpList[j-1][1][2])%mod;
		}
		else if(s[j-1]=='B'){
			dpList[j][1][1]= (dpList[j-1][0][0] +dpList[j-1][0][1] + dpList[j-1][0][2]
							 +dpList[j-1][1][0] +dpList[j-1][1][1] + dpList[j-1][1][2])%mod;
		}
		else if(s[j-1]=='C'){
			dpList[j][1][0]= (dpList[j-1][1][0] +dpList[j-1][1][1] + dpList[j-1][1][2])%mod;
		}
		else{
			dpList[j][1][0]= (dpList[j-1][1][0] +dpList[j-1][1][1] + dpList[j-1][1][2])%mod;
			dpList[j][1][1]= (dpList[j-1][0][0] +dpList[j-1][0][1] + dpList[j-1][0][2]
							 +dpList[j-1][1][0] +dpList[j-1][1][1] + dpList[j-1][1][2])%mod;
			dpList[j][1][2]= (dpList[j-1][1][0] +dpList[j-1][1][1] + dpList[j-1][1][2])%mod;
		}
	}
	
	for(int j=1; j<s.size()+1; j++){
		if(s[j-1]=='A'){
			dpList[j][2][0]= (dpList[j-1][2][0] +dpList[j-1][2][1] + dpList[j-1][2][2])%mod;
		}
		else if(s[j-1]=='B'){
			dpList[j][2][0]= (dpList[j-1][2][0] +dpList[j-1][2][1] + dpList[j-1][2][2])%mod;
		}
		else if(s[j-1]=='C'){
			dpList[j][2][1]= (dpList[j-1][1][0] +dpList[j-1][1][1] + dpList[j-1][1][2]
							 +dpList[j-1][2][0] +dpList[j-1][2][1] + dpList[j-1][2][2])%mod;
		}
		else{
			dpList[j][2][0]= (dpList[j-1][2][0] +dpList[j-1][2][1] + dpList[j-1][2][2])%mod;
			dpList[j][2][1]= (dpList[j-1][1][0] +dpList[j-1][1][1] + dpList[j-1][1][2]
							 +dpList[j-1][2][0] +dpList[j-1][2][1] + dpList[j-1][2][2])%mod;
			dpList[j][2][2]= (dpList[j-1][2][0] +dpList[j-1][2][1] + dpList[j-1][2][2])%mod;
		}
	}

	std::cout<< (dpList[s.size()][2][0] + dpList[s.size()][2][1] + dpList[s.size()][2][2])%mod <<std::endl;

	return 0;
}

Measured memory_bytes: 20608
#include <vector>
#include <stdio.h>
#include <utility>
#include <string.h>

#define ii pair<int, int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second

using namespace std;

int nxt;
bool valid[1005], visited[1005];
vector<int> adj[1005], radj[1005], v, scc;

void dfs1(int pos) {
	visited[pos] = true;
	for (int i = 0; i < (int) adj[pos].size(); i++) {
		nxt = adj[pos][i];
		if ((!visited[nxt]) && (valid[nxt]))
			dfs1(nxt);
	}
	v.pb(pos);
}

void dfs2(int pos) {
	visited[pos] = true;
	for (int i = 0; i < (int) radj[pos].size(); i++) {
		nxt = radj[pos][i];
		if ((!visited[nxt]) && (valid[nxt]))
			dfs2(nxt);
	}
	scc.pb(pos);
}

int main() {
	int n, m, a, b;
	bool found = false;
	scanf("%d %d", &n, &m);
	while (m--) {
		scanf("%d %d", &a, &b);
		adj[a].pb(b);
		radj[b].pb(a);
	}
	memset(visited, false, sizeof visited);
	memset(valid, true, sizeof valid);
	for (int i = 1; i <= n; i++)
		if (!visited[i])
			dfs1(i);
	memset(visited, false, sizeof visited);
	for (int i = (int) v.size() - 1; i >= 0; i--)
		if (!visited[v[i]]) {
			scc.clear();
			dfs2(v[i]);
			if ((int) scc.size() > 1) {
				found = true;
				break;
			}
		}
	if (!found) {
		puts("-1");
		return 0;
	}
	a = n;
	for (int i = 1; i <= n; i++) {
		valid[i] = false;
		a--;
		v.clear();
		memset(visited, false, sizeof visited);
		for (int j = 1; j <= n; j++)
			if ((valid[j]) && (!visited[j]))
				dfs1(j);
		memset(visited, false, sizeof visited);
		found = false;
		for (int j = (int) v.size() - 1; j >= 0; j--)
			if (!visited[v[j]]) {
				scc.clear();
				dfs2(v[j]);
				if ((int) scc.size() > 1) {
					found = true;
					break;
				}
			}
		if (!found) {
			a++;
			valid[i] = true;
		}
	}
	printf("%d\n", a);
	for (int i = 1; i <= n; i++)
		if (valid[i])
			printf("%d\n", i);
	return 0;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i <(n); ++i)
#define REP2(i,x,n) for (int i = x; i <(n); ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int,int>;
static const double PI = acos(-1);
static const int INF = 1e9+7;
#define REP(i,n) for (int i = 0; i <(n); ++i)
//debug 
#ifdef _DEBUG
#define debug(var)  do{cout << #var << " :";view(var);}while(0)
#else
#define debug(...)
#endif
template<typename T> void view(T e){cout << e << endl;}
template<typename T> void view(const vector<T>& v){for(const auto& e : v){ cout << e << " "; } cout << endl;}
template<typename T> void view(const vector<vector<T> >& vv){ for(const auto& v : vv){ view(v); } }


int main(){
  int n, m, x;
  cin >> n >> m >> x;
  vector<int> a(m);
  REP(i,m) cin >> a[i];
  debug(a);
  int cnt = 0;
  REP(i,m){
    if(a[i] > x) break;
    cnt++;
  }
  int ans = min(cnt, m -cnt);
  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 3620
#define _CRT_SECURE_NO_WARNINGS
#pragma _attribute_((optimize("-O2")))
#pragma comment(linker, "/STACK:102400000,102400000")
 
#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <cstdio>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#include <time.h>
#include <iomanip>
 
//#define Local
 
using namespace std;
 
bool cmp(long long a, long long b)
{
	return a > b;
}
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input1.txt", "r", stdin);
	//freopen("output1.txt", "w", stdout);
 
	long long N, W;
	vector<long long> a1, a2, a3, a4;
	a1.push_back(0);
	a2.push_back(0);
	a3.push_back(0);
	a4.push_back(0);
 
	cin >> N >> W;
	long long base;
	long long w, v;
	cin >> w >> v;
	base = w - 1;
	a1.push_back(v);
	for (int i = 1; i < N; i++)
	{
		cin >> w >> v;
		if (w - base == 1)
			a1.push_back(v);
		else if (w - base == 2)
			a2.push_back(v);
		else if (w - base == 3)
			a3.push_back(v);
		else
			a4.push_back(v);
	}
	sort(a1.begin() + 1, a1.end(), cmp);
	sort(a2.begin() + 1, a2.end(), cmp);
	sort(a3.begin() + 1, a3.end(), cmp);
	sort(a4.begin() + 1, a4.end(), cmp);
 
	for (int i = 1; i < a1.size(); i++)
		a1[i] += a1[i - 1];
	for (int i = 1; i < a2.size(); i++)
		a2[i] += a2[i - 1];
	for (int i = 1; i < a3.size(); i++)
		a3[i] += a3[i - 1];
	for (int i = 1; i < a4.size(); i++)
		a4[i] += a4[i - 1];
 
 
	long long res = 0;
	for (int i1 = 0; i1 < a1.size(); i1++)
		for (int i2 = 0; i2 < a2.size(); i2++)
			for (int i3 = 0; i3 < a3.size(); i3++)
				for (int i4 = 0; i4 < a4.size(); i4++)
				{
					if (i1 + i2 * 2 + i3 * 3 + i4 * 4 + (i1 + i2 + i3 + i4)*base <= W)
						res = max(res, a1[i1] + a2[i2] + a3[i3] + a4[i4]);
				}
 
	cout << res << endl;
 
#ifdef Local
	cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
#endif
 
	return 0;
}

Measured memory_bytes: 256
// 2011/07/20 Tazoe

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
	string ans[10000];

	for(int i=0; i<10000; i++)
		if((i+1)%15==0)
			ans[i] = "FizzBuzz";
		else if((i+1)%3==0)
			ans[i] = "Fizz";
		else if((i+1)%5==0)
			ans[i] = "Buzz";
		else{
			ostringstream ost;
			ost << i+1;
			ans[i] = ost.str();
		}

	while(true){
		int m, n;
		cin >> m >> n;
		if(m==0&&n==0)
			break;

		int ply[1000];
		for(int i=0; i<m; i++)
			ply[i] = i+1;

		for(int p=0, i=0; i<n; i++){
			string s;
			cin >> s;

			if(m==1)		// for AOJ
				continue;

			if(s==ans[i]){
				p = (p+1)%m;
			}
			else{
				for(int j=p; j<m-1; j++)
					ply[j] = ply[j+1];
				m--;
				p = p%m;
			}
		}

		for(int i=0; i<m; i++){
			cout << ply[i];
			if(i==m-1)
				cout << endl;
			else
				cout << ' ';
		}
	}

	return 0;
}

Measured memory_bytes: 0