akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll gcd(ll x, ll y) {
    if (x % y == 0) {
        return y;
    } else {
        return gcd(y, x % y);
    }
}


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

vector<ll> L(N);
vector<ll> R(N);
L[0] = A[0];
R[N - 1] = A[N - 1];
for (int i = 1; i < N; i++) {
    L[i] = gcd(L[i - 1], A[i]);
    R[N - i - 1] = gcd(R[N - i], A[N - i - 1]); 
}

ll ans = max(R[1], L[N - 2]);
for (int i = 1; i < N - 1; i++) {
    ll tmp = gcd(L[i - 1], R[i + 1]);
    ans = max(ans, tmp);
}

cout << ans << endl;

}

Measured memory_bytes: 2560
#include<cstdio>
#include<algorithm>
int main(){
	long long int n,x;
	scanf("%lld%lld",&n,&x);
	long long int a[n]={};
	for(int i=0;i<n;i++){
		scanf("%lld",&a[i]);
	}
	std::sort(a,a+n);
	long long int ans=0;
	for(int i=0;i<n;i++){
		if(a[i]>x){
			break;
		}else{
			ans++;
			x-=a[i];
		}
	}
	if(ans==n && x>0)ans--;
	printf("%lld\n",ans);
	return 0;
}

Measured memory_bytes: 128
/*
Author: Sagar Gupta
E mail: sagar.june97p@gmail.com
*/

#pragma comment(linker, "/STACK:268435456")
#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 endl ("\n")
#define deb(n) cerr << #n << " = " << (n) << '\n'
#define FOR(i, n) for (int(i) = 0; (i) < n; (i)++)
#define PI (3.141592653589)
#define ordered_set tree<int, null_type, std::less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define MOD 1000000007
using ll = long long;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed;
    cout.precision(10);
    // Those who cannot acknowledge themselves, will invariably fail

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

    return 0;
}

Measured memory_bytes: 256
a = []
a.append(list(map(int, input().split())))
a.append(list(map(int, input().split())))
a.append(list(map(int, input().split())))

n = int(input())

r = {
    'r0': 0,
    'r1': 0,
    'r2': 0,
    'c0': 0,
    'c1': 0,
    'c2': 0,
    'x0': 0,
    'x1': 0,

}

for i in range(n):
    b = int(input())
    for j in range(3):
        for k in range(3):
            if a[j][k] == b:
                r['r{}'.format(j)] += 1
                r['c{}'.format(k)] += 1
                if j == k:
                    r['x0'] += 1
                if (j == 0 and k == 2) or (j == 1 and k == 1) or (j == 2 and k == 0):
                    r['x1'] += 1

for b in r.values():
    if b == 3:
        print('Yes')
        exit() 
print('No')



Measured memory_bytes: 3064
#include <cstdio>
#include <algorithm>
using namespace std;
#define N 100010
int n,a[N],bin[N];
struct Edge {int x,y,z;}e[N];
int main()
{
	scanf("%d",&n),n--,bin[0]=1;
	for(int i=1;i<=20;i++) bin[i]=bin[i-1]<<1;
	int k=0,m=n,tot=0; bool flag=false;
	while(m) k++,flag|=m%2==0,m/=2;
	if(!flag)
	{
		printf("%d ",k+1);
		for(int i=1;i<=k;i++)
		{
			tot++,e[tot].x=i,e[tot].y=i+1,e[tot].z=bin[i-1],
			tot++,e[tot].x=i,e[tot].y=i+1,e[tot].z=0;
		}
		printf("%d\n",tot);
		for(int i=1;i<=tot;i++) printf("%d %d %d\n",e[i].x,e[i].y,e[i].z);
		return 0;
	} printf("%d ",k);
	for(int i=1;i<k;i++)
	{
		tot++,e[tot].x=i,e[tot].y=i+1,e[tot].z=bin[k-i-1];
		tot++,e[tot].x=i,e[tot].y=i+1,e[tot].z=0;
	}
	int cnt=0;
	for(int i=0;i<=20;i++) if(n&bin[i]) a[++cnt]=i;
	int pre=bin[a[cnt]];
	for(int i=cnt-1;i;i--) tot++,e[tot].x=1,e[tot].y=k-a[i],e[tot].z=pre,pre+=bin[a[i]];
	tot++,e[tot].x=1,e[tot].y=k,e[tot].z=n;
	printf("%d\n",tot);
	for(int i=1;i<=tot;i++) printf("%d %d %d\n",e[i].x,e[i].y,e[i].z);
}

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

typedef long long int ll;
typedef pair<ll, ll> p_ll;
typedef vector<pair<ll, ll>> vec_p; //vector<pair<ll, ll>> pairs(n) ,pairs.at(i) = make_pair(i*i, i)

#define ture ture
#define flase false
#define falg flag

#define REP(i, x) for (ll i = 0; i < (ll)(x); i++)
#define REPS(i, x) for (ll i = 1; i <= (ll)(x); i++)
#define RREP(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (ll i = ((ll)(x)); i > 0; i--)
#define all(x) (x).begin(), (x).end()

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1;//llの最大9*10^18
const int INF = pow(2, 30) - 1;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

bool is_prime(ll x)
{
    for (ll i = 2; i * i <= x; i++)
    {
        if (x % i == 0)
            return false;
    }
    return true;
}

vector<pair<long long, long long>> prime_factorize(long long N)
{
    vector<pair<long long, long long>> res;
    for (long long a = 2; a * a <= N; ++a)
    {
        if (N % a != 0)
            continue;
        long long ex = 0; // 指数

        // 割れる限り割り続ける
        while (N % a == 0)
        {
            ++ex;
            N /= a;
        }

        // その結果を push
        res.push_back({a, ex});
    }

    // 最後に残った数について
    if (N != 1)
        res.push_back({N, 1});
    return res;
}

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

    ll N;
    string S;
    cin >> N;
    ll count = 0;

    if(N==1){
        cout << 0 << endl;
    }
    else if(is_prime(N)){//素数判定
        cout << 1 << endl;
    }
    else{
        const auto &res = prime_factorize(N);
        ll ans = 0;

        REP(i,res.size()){
            int j = 1;
            ll tmp = res.at(i).second;
            while (tmp >= j)
            {
                tmp -= j;
                ans++;
                j++;
            }
        }
        cout << ans << endl;
    }
}


Measured memory_bytes: 3636
n,k=map(int,input().split())
a=list(map(int,input().split()))
dp=[0]
mp={0:1}
ans=0
for t in range(n):
  i=a[t]
  
  if t>=k-1:
    mp[dp[-k]]-=1  
    
  num=(dp[-1]+i-1)%k
  if num in mp.keys():
    ans += mp[num]
    mp[num] += 1
  else:
    mp[num] = 1
  dp.append(num)

print(ans)

Measured memory_bytes: 39560
n = int(input())
A = list(map(int,input().split()))

number = [0]*n
for item in A:#O(n)
    number[item-1] += 1

ans = 0

for items in number:
    ans += items*(items-1)//2

for item in A:
    print(ans - (number[item-1]-1))

Measured memory_bytes: 24996
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i < (n); i++)
#define lower(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
using namespace std;
using ll = long long;
int INF = 1001001001;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];

  ll t = 0;
  rep(i, m) t+=b[i];
  int j = m;
  int ans = 0;
  rep(i, n+1) {
    while(j>0 && k < t) {
      j--;
      t -= b[j];
    }
    if(k < t) break;
    ans = max(ans, i+j);
    if(i == n) break;
    t += a[i];
  }

  cout << ans << endl;

  return 0;
}

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

#define PI 3.1415926535897932
int n,k;
int a[100000];
int dp[100001];

bool rec(int rest) {
    if (~dp[rest]) return dp[rest];
    for (int i=0;i<n;i++) {
        if (rest-a[i]<0) continue;
        if (!rec(rest-a[i])) return dp[rest]=true;
    }
    return dp[rest]=false;
}

int main(){
    scanf("%d %d",&n,&k);
    for (int i=0;i<n;i++) scanf("%d",&a[i]);
    memset(dp,-1,sizeof(dp));
    if (rec(k)) printf("First\n");
    else printf("Second\n");
    return 0;
}


Measured memory_bytes: 3712
# coding: utf-8

import sys
from collections import deque

n, q = map(int, input().split())
total_time = 0
tasks = deque(map(lambda x: x.split(), sys.stdin.readlines()))

for task in tasks:
    task[1] = int(task[1])

try:
    while True:
        t = tasks.popleft()
        if t[1] <= q:
            total_time += t[1]
            print(t[0], total_time)
        else:
            t[1] -= q
            total_time += q
            tasks.append(t)
except Exception:
    pass

Measured memory_bytes: 23156
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define Mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f 
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 100005
#define P pair<int,int>
using namespace std;
	ll dp[55][55][3000];
int main()
{
	int n, m;
	int a[55];
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	ll ans = 0;

	MEM(dp, 0);
	dp[0][0][0] = (ll)1;
	for(int i=1;i<=n;i++)
		for (int j =0;j<=i;j++ )
		{		
			for (int k = 3000; k >= 0; k--)
			{
				dp[i][j][k] = dp[i - 1][j][k];
				if (k >= a[i]&&j)
					dp[i][j][k] += dp[i - 1][j - 1][k - a[i]];
			}
		}
	for (int i = 1; i <= n; i++)
	{
		ans += dp[n][i][i * m];
	}
	cout << ans;
}

Measured memory_bytes: 71168
#include<bits/stdc++.h>
using namespace std;
#define ld long double
#define ll long long
#define loop(i,x,y) for(ll i=x;i<y;i++)
#define pb push_back
#define S second
#define F first

int man_dist(int x, int y, int a, int b){

	return (abs(x - a) + abs(y - b));

}

void solve(){

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

	pair <int, int> p[n];
	pair <int, int> q[m];

	int x, y;
	for(int i = 0; i < n; i++){
		
		cin >> x >> y;
		
		p[i] = make_pair(x, y);
	}

	for(int i = 0; i < m; i++){

		cin >> x >> y;

		q[i] = make_pair(x, y);
	}

	vector <int> ans;

	for(int i = 0; i < n; i++){

		int x = p[i].first, y = p[i].second;

		int min_dist = INT_MAX, ind;

		for(int j = 0; j < m; j++){

			int a = q[j].first, b = q[j].second;

			int dist = man_dist(x, y, a, b);

			if(min_dist > dist){
				
				min_dist = dist;

				ind = j;
			
			}
		} 

		ans.push_back(ind+1);
	
	}

	for(int i = 0; i < n; i++)
		cout << ans[i] << endl;


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




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

struct Point{
  double x;
  double y;
};

void koch(int n, Point a, Point b){
  if( n == 0 ) return;
  Point s, t, u;
  double th = M_PI * 60.0 / 180.0;

  s.x = (2.0*a.x + 1.0*b.x) / 3.0;
  s.y = (2.0*a.y + 1.0*b.y) / 3.0;
  t.x = (1.0*a.x + 2.0*b.x) / 3.0;
  t.y = (1.0*a.y + 2.0*b.y) / 3.0;
  u.x = (t.x - s.x)*cos(th) - (t.y - s.y)*sin(th) + s.x;
  u.y = (t.x - s.x)*sin(th) + (t.y - s.y)*cos(th) + s.y;

  koch(n-1, a, s);
  printf("%.8f %.8f\n", s.x, s.y);
  koch(n-1, s, u);
  printf("%.8f %.8f\n", u.x, u.y);
  koch(n-1, u, t);
  printf("%.8f %.8f\n", t.x, t.y);
  koch(n-1, t, b);

}

int main() {
  Point a, b;
  int n;

  scanf("%d", &n);

  a.x = 0;
  a.y = 0;
  b.x = 100;
  b.y = 0;

  printf("%.8f %.8f\n", a.x, a.y);;
  koch(n, a, b);
  printf("%.8f %.8f\n", b.x, b.y);

  return 0;
}



Measured memory_bytes: 2684
#include <iostream>

using namespace std;

int N;
int A[100005];
int K;

int g;
int maxval;

int gcd(int a, int b)
{
	if(b == 0) return a;
	return gcd(b, a % b);
}
	
int main(void)
{
	cin >> N >> K;
	for(int i = 0; i < N; i++) cin >> A[i];
	
	g = A[0];
	maxval = A[0];
	for(int i = 1; i < N; i++){
		g = gcd(g, A[i]);
		if(maxval < A[i]) maxval = A[i];
	}
	
	if(maxval >= K && K % g == 0) cout << "POSSIBLE" << endl;
	else cout << "IMPOSSIBLE" << endl;
	
	return 0;
}

Measured memory_bytes: 640
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<iostream>
#include<cmath>
#include<map>
#include<set>
#include<climits>
using namespace std;
typedef vector<string>vs;
typedef vector<int>vi;
typedef vector<vi>vvi;
typedef vector<double>vd;
typedef pair<int,int>pii;
typedef long long ll;
typedef pair<ll,ll>pll;
typedef vector<ll>vl;
typedef pair<int,pii>piii;
#define rrep(i,x,n) for(int i=(x);i<(n);++i)
#define rep(i,x) rrep(i,0,(x))
#define fi first
#define se second
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define pb push_back
#define maxs(a,b) (a)=max(a,b)
#define mins(a,b) (a)=min(a,b)
///#include<sstream>
///#include<cstring>
///#include<stack>
///#include<complex>


const int dx[]={0,0,-1,1};
const int dy[]={-1,1,0,0};
struct State{
    int fld[20][20];
    int H,W;
    int y,x;
    int dir;
    void CTR(char c){
        if(c=='S')Fire();
        else Move(c);
    }
    void Move(char c){
        if(c=='U')dir=0;
        else if(c=='D')dir=1;
        else if(c=='L')dir=2;
        else if(c=='R')dir=3;

        int ty=y+dy[dir];
        int tx=x+dx[dir];
        if(ty<0||ty>=H||tx<0||tx>=W)return;
        if(fld[ty][tx]!=0)return;
        fld[y][x]=0;
        y=ty;x=tx;
        fld[y][x]=-1;
    }
    void Fire(){
        int ny=y,nx=x;
        while(true){
            ny+=dy[dir];
            nx+=dx[dir];
            if(ny<0||ny>=H||nx<0||nx>=W)break;
            if(fld[ny][nx]==0||fld[ny][nx]==3)continue;
            if(fld[ny][nx]==1){
                fld[ny][nx]=0;
                break;
            }
            if(fld[ny][nx]==2)break;
        }
    }
};

void solve(){
    State state;
    scanf("%d%d",&state.H,&state.W);
    rep(i,state.H){
        char str[36];
        scanf("%s",str);
        rep(j,state.W){
            if(str[j]=='.')state.fld[i][j]=0;
            else if(str[j]=='*')state.fld[i][j]=1;
            else if(str[j]=='#')state.fld[i][j]=2;
            else if(str[j]=='-')state.fld[i][j]=3;
            else{
                state.fld[i][j]=-1;
                state.y=i;state.x=j;
                if(str[j]=='^')state.dir=0;
                else if(str[j]=='v')state.dir=1;
                else if(str[j]=='<')state.dir=2;
                else if(str[j]=='>')state.dir=3;
            }
        }
    }

    int len;
    char opr[128];
    scanf("%d%s",&len,opr);

    rep(i,len){
        state.CTR(opr[i]);
    }


    char p[]="^v<>",o[]=".*#-";
    rep(i,state.H){
        rep(j,state.W){
            if(state.fld[i][j]==-1)printf("%c",p[state.dir]);
            else printf("%c",o[state.fld[i][j]]);
        }
        puts("");
    }
}

int main(){
    int N;
    scanf("%d",&N);
    bool flag=false;
    while(N--){
        if(flag)puts("");
        else flag=true;
        solve();
    }
}

Measured memory_bytes: 1176
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long int ll;
#define MOD 1000000007
#define INF 1e18
#include <ctime>
#define rep(i, x, n) for (ll i = x; i < n; i++)
#define rev(i, n, x) for (ll i = n; i > x; i--)
#define endl "\n"
#define all(c) (c).begin(), (c).end()
#define int long long int
#ifdef mishra
#define debug(x) cout << ">>" << #x << " is " << x << "<<" << endl;
#define print_time() cout << setprecision(6) << "time: " << (clock() - start) / (double)CLOCKS_PER_SEC << '\n';
#else
#define debug(x) ;
#define print_time() ;
#endif

clock_t start;
void FAST(ll argc)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    start = clock();
    if (argc == 2)
        freopen("std.in", "r", stdin);
}

/*vector<ll> v;
void seive()
{
    v.pb(2);
    for (int i = 3; i < 100000; i += 2)
    {
        for (int j = 0; j < v.size(); j++)
        {
            if (i % v[j] == 0)
                break;
            if (v[j] * v[j] > i)
            {
                v.pb(i);
                break;
            }
        }
    }
}
/*
void prime_factor(ll x, vector<ll> &r)
{
    ll sqrt_x = sqrt(x);

    for (ll i : v)
    {
        if (i > sqrt_x)
            break;

        if (x % i == 0)
            r.pb(i);

        while (x % i == 0)
        {
            x /= i;
        }
    }

    if (x >= 2)
        r.pb(x);
}
*/
/*
ll power(ll x, ll y)
{
    ll res = 1;
    x = x % MOD;
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % MOD;
        y = y >> 1;
        x = (x * x) % MOD;
    }
    return res;
}
*/
/*
inline void dfs(ll curr, ll prev)
{

    for (ll i : a[curr])
        if (i != prev)
            dfs(i, curr);
}
*/
/*
ll multi(ll a, ll b)
{
    return ((a * b) % MOD);
}
*/
//---------------------CODE STARTS HERE---------------------

signed main(char argc, char *argv[])
{
    FAST(argc);

    ll testcase = 1;
    //cin >> testcase;

    while (testcase--)
    {
        int n, w;
        cin >> n >> w;
        pair<int, int> a[n];
        rep(i, 0, n)
                cin >>
            a[i].first >> a[i].second;

        sort(a, a + n);
        // int z[w + 5] = {0};
        map<int, int> m;
        m[0] = 0;
        bool ck = 0;
        int ans = 0;
        rep(i, 0, n)
        {
            int x = a[i].first;
            int y = a[i].second;
            // z[x] = max(z[x], y);
            stack<pair<int, int> > st;
            for (auto it : m)
            {
                if (it.first + x > w)
                    break;
                st.push(make_pair(it.first + x,max(it.second + y, m[it.first + x])));
            }
            while(st.empty()==0)
            {
                auto z= st.top();
                st.pop();
                m[z.first] = z.second;
                if(z.first<=w)
                ans = max(ans, z.second);
            }
        }
        
        cout << ans << endl;
        //--------??--------CLEAR VECTORS--------??--------
        m.clear();
    }

    //v.clear();
    print_time();
    return 0;
}

Measured memory_bytes: 8192
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        s = s.replace("eraser", "");
        s = s.replace("erase", "");
        s = s.replace("dreamer", "");
        s = s.replace("dream", "");
        System.out.println(s.equals("") ? "YES" : "NO");
    }
}

Measured memory_bytes: 30544
N, K = map(int, input().split())

MOD = 10 ** 9 + 7
ans = 0
ctr = [0] * (K + 1)

for x in reversed(range(1, K + 1)):
    ctr[x] = pow(K // x, N, MOD)
    for y in range(2 * x, K + 1, x):
        ctr[x] -= ctr[y]

ans = 0
for n in range(1, K + 1):
    ans += n * ctr[n]
    ans %= MOD

print(ans)


Measured memory_bytes: 72108
use std::collections::BinaryHeap;

fn main() {
    let s = std::io::stdin();
    let mut sc = Scanner { stdin: s.lock() };

    let n = sc.read();
    let a: Vec<u64> = sc.vec(n);
    let mut b: Vec<u64> = sc.vec(n);
    if (0..n).any(|i| a[i] > b[i]) {
        println!("-1");
        return;
    }

    let mut ans = 0;
    let mut heap = BinaryHeap::new();
    for i in 0..n {
        push(&mut heap, &a, &b, i);
    }

    while let Some((count, neighbors, i)) = heap.pop() {
        ans += count;
        b[i] -= count * neighbors;
        assert!(b[i] >= a[i]);
        push(&mut heap, &a, &b, (i + 1) % n);
        push(&mut heap, &a, &b, (n + i - 1) % n);
    }

    if a != b {
        println!("-1");
    } else {
        println!("{}", ans);
    }
}

fn push(heap: &mut BinaryHeap<(u64, u64, usize)>, a: &Vec<u64>, b: &Vec<u64>, i: usize) {
    let n = b.len();
    let prev = b[(n + i - 1) % n];
    let next = b[(i + 1) % n];
    let neighbors = prev + next;
    let db = b[i] - a[i];
    let count = db / neighbors;
    if count > 0 {
        heap.push((count, neighbors, i));
    }
}

pub struct Scanner<R> {
    stdin: R,
}

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n')
            .take_while(|&b| b != b' ' && b != b'\n')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}


Measured memory_bytes: 12540
let n = readInt()
let a = readIntArray().lazy

var acc = 0
var min = 0

for num in a {
  if min > num {
    acc = acc &+ (min - num)
  } else {
    min = num
  }
}

print(acc)

@inlinable @inline(__always) func readString() -> String { readLine()! }
@inlinable @inline(__always) func readInt   () -> Int    { Int(readString())! }
@inlinable @inline(__always) func readDouble() -> Double { Double(readString())! }

// .map(String.init) を挟まないと [String.SubSequence] になって遅くなる（は？）
@inlinable @inline(__always) func readStringArray(_ separator: Character = " ") -> [String] { readString().split(separator: separator).map(String.init) }
@inlinable @inline(__always) func readIntArray   (_ separator: Character = " ") -> [Int]    { readStringArray(separator).map { Int($0)! } }
@inlinable @inline(__always) func readDoubleArray(_ separator: Character = " ") -> [Double] { readStringArray(separator).map { Double($0)! } }


Measured memory_bytes: 21224
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+5;

int main()
{
    int n; cin>>n;
    int cnt = 1, cur = 7 % n;
    while (cnt < 2e6 && cur) {
        cnt++;
        cur = (cur*10 % n+7) % n;
    }
    if (cnt == 2e6) puts("-1");
    else printf("%d\n", cnt);
    return 0;
}

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

typedef long long ll;

ll X;
ll Y;
ll year;

int main(){

    cin >> X;
    Y = 100;
    year = 0;
    while(Y < X){
        Y += Y * 0.01;
        year++;
    }

    cout << year << endl;
}

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

using namespace std;

int main()
{
    int N;
    string raw;
    string s, t;
    getline(cin, raw);
    N = stoi(raw);
    getline(cin, raw);

    s = raw.substr(0, N);
    t = raw.substr(N + 1, N);

    for (int i = 0; i < N; i++)
    {
        cout << s[i] << t[i];
    }
    cout << "\n";
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> mp;
ll inf = 1e9;

vector<ll> daikus(vector<vector<mp> > &g, int s){
    ll n = g.size();
    vector<ll> res(n,inf);
    vector<ll> used(n,false);
    priority_queue<mp,vector<mp>,greater<mp> > q;
    q.push(mp(0,s) );
    while( ! q.empty() ){
	mp now = q.top();
	q.pop();
	ll nn = now.second;
	ll cost = now.first;
	if(used[nn] ) continue;
	used[nn] = true;
	res[nn] = cost;
	for(int i=0;i<g[nn].size();i++){
	    ll next = g[nn][i].second;
	    ll nc = cost + g[nn][i].first;
	    if(used[next])continue;
	    q.push(mp(nc,next) );
	}
    }
    return res;
}

int main(){
    ll  n , m ,k ,p ;
    cin>>n>>m>>k>>p;
    p--;
    vector<vector<mp> > g(n);
    for(int i=0;i<m;i++){
	ll x,y,w;
	cin>>x>>y>>w;
	x--,y--;
	g[x].push_back(mp(w,y) );
	g[y].push_back(mp(w,x) );
    }
    vector<mp> go(k);
    for(int i=0;i<k;i++){
	cin>>go[i].first>>go[i].second;
	go[i].first--,go[i].second--;
    }
    vector<ll> vis;
    vis.push_back(p);
    vector<vector<ll> > gg(2*k+1,vector<ll>(2*k+1,inf) );
    for(int i=0;i<k;i++){
	vis.push_back(go[i].first);
	vis.push_back(go[i].second);
    }
    for(int i=0;i<2*k+1;i++){
//	cout<<vis[i]<<endl;
	vector<ll> res = daikus(g,vis[i]);
	for(int j=0;j<2*k+1;j++){
	    gg[i][j] = res[ vis[j] ];
	}
    }
    /*for(int i=0;i<2*k+1;i++){
	for(int j=0;j<2*k+1;j++){
	    cout<<gg[i][j]<<' ';
	}
	cout<<endl;
    }*/
    int N= 2*k+1;
    vector<vector<ll> > dp( 1<<N ,vector<ll>(N,inf) );
    dp[ (1<<N)-1][0] = 0;
    for(int i = (1<<N)-1 ; i>=0;i--){
	for(int v = 0;v < N;v++){
	    for(int u = 0;u < N ; u++){
		if( !( (i >> u) & 1)  ) {
		 //   cout<<l<<endl;
		    if( u != 0 && u%2==0 && ((i>>(u-1) ) & 1) )   continue;
		    if( v != 0 && v%2==0 && ((i>>(v-1) )& 1 ) ) continue;
		    dp[i][v] = min(dp[i][v] , dp[i | (1<<u) ][u] + gg[v][u] ) ;
		  //  cout<<dp[i][v]<<endl;
		}
	    }
	}
    }
    ll ans = inf;
    for(int i=1;i<2*k+1;i++){
	ans = min(ans,dp[ 1<<i][i] );
    }
    if(ans==inf){
    cout<<"Cannot deliver"<<endl;
    }else{
    cout<<ans<<endl;
    }
    return 0;
}



Measured memory_bytes: 4216
n,k = map(int, input().split())
v = list(map(int, input().split()))

ans = 0
for x in range(k+1):# LeftHand
  for y in range(k-x+1):# RightHand
    temp = v[:x]+v[len(v)-y:]
    if x+y >= n:
      temp = v
    temp1 = []
    temp2 = []
    for i in temp:
      if i >= 0:
        temp1.append(i)
      else:
        temp2.append(i)

    temp2.sort()
    ans = max(ans, sum(temp1) + sum(temp2[k-x-y:]))
print(ans)

Measured memory_bytes: 9148
N = int(input())
A = list(map(int, input().split()))

DIV = 10**9+7
ans = 0
Asum = sum(A)
Asum_sq = Asum**2

for i in range(N):
	ans += A[i]**2

res = (Asum_sq-ans)//2
print(res%DIV)

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

int main(){
  int A,B,C,X,count=0;
  cin >> A >> B >> C >> X;
  
  for(int i = 0; i <= A; i++){
    for(int j =0; j <= B; j++){
      for(int k =0; k <= C; k++){
        if(X - 500 * i - 100 * j - 50 * k == 0) count++;
      }
    }
  }
  cout << count << endl;
}


Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main(){
	int m, f, r;
	while(1){
	cin >> m >> f >> r;
		if(m == -1 && f == -1 && r == -1){
			break;
		}else{
			if(m == -1 || f == -1){
				cout << "F" << endl;
			}else if(m + f >= 80){
				cout << "A" << endl;
			}else if(m + f >= 65){
				cout << "B" << endl;
			}else if(m + f >= 50){
				cout << "C" << endl;
			}else if(m + f >= 30 && r >= 50){
				cout << "C" << endl;
			}else if(m + f >= 30){
				cout << "D" << endl;
			}else{
				cout << "F" << endl;
			}
		}
	}
	return 0;
}

Measured memory_bytes: 3076
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <unordered_map>
#include <climits>
#include <set>
#include <unordered_set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <random>
#include <complex>
#include <regex>
#include <locale>
#include <random>
#include <type_traits>

using namespace std;

#define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";}
#define SHOW_MAP(v){std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}}

using LL = long long;

//------------------------------------------
//------------------------------------------


int main() {

    int N;
    cin >> N;

    vector<string> s(N);
    for (int i = 0; i < N; i++) cin >> s[i];
    for (int i = 0; i < N; i++) sort(s[i].begin(), s[i].end());

    map<string, LL> mp;
    for (int i = 0; i < N; i++) mp[s[i]]++;

    LL ans = 0;
    for (auto x: mp) ans += x.second * (x.second - 1) / 2;
    cout << ans << endl;


}
























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

using namespace std;

int main()
{
  int n;
  cin>>n;
  int a[n];
  int max=0;
  int count=0;
  for(int i=0;i<n;i++)
  {
    cin>>a[i];
    if(a[i]>=max)
    {
      count++;
      max=a[i];
    }
  }
  cout<<count;
    
  return 0;
}

Measured memory_bytes: 256
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect, bisect_left, bisect_right

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

H, W = MAP()
N = INT()
A = LIST()
C = [[0] * W for i in range(H)]
i = 0
for h in range(H):
    for w in range(W):
        while A[i] == 0:
            i += 1
        if h % 2 == 0:
            # forward
            C[h][w] = i + 1
        else:
            # backward
            C[h][W-w-1] = i + 1
        A[i] -= 1
# print(*[c for c in C], sep="\n")
for c in C:
    print(*c, sep=" ")      


Measured memory_bytes: 10968
#include <bits/stdc++.h>
using namespace std;
int main(){
  
  string S;
  cin >> S;
  
  bool exist=1;
  int i=S.size()-2;
  while(i>2){
    exist=1;
   for(int j=0; j<i/2; j++){
   	if(S[j]!=S[j+i/2]) exist=0;
   }
    if(exist){
      break;
    }
    i-= 2;
  }
    
  cout << i << endl;
  
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define MOD (long long int)(1e9+7)
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define FOR(i,a,b) for(int i=a; i<(int)(b); i++)
#define ALL(x) (x).begin(),(x).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset((a), 0 ,sizeof(a))
#define PB push_back
#define MP make_pair
#define SP << " " <<
const int INF = 1001001001;
const ll LINF = 100100100100100100;
#define EPS (1e-10)
const long double PI  = acos(-1.0L);
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
#define chmax(a,b) a = (((a)<(b))?(b):(a))
#define chmin(a,b) a = (((a)>(b))?(b):(a))

__attribute__((constructor))
void initial(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(15);
}

signed main(){
	string n; cin>>n;
	int ans=0;
	rep(i,n.size()) if(n[i]=='2') ans++;
	cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 256
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.next();
        String b = sc.next();
        String c = sc.next();
        if (a.substring(a.length() - 1, a.length()).equals(b.substring(0, 1))) {
            if (b.substring(b.length() - 1, b.length()).equals(c.substring(0, 1))) {
                System.out.println("YES");
                return;
            }
        }
        System.out.println("NO");
    }
}

Measured memory_bytes: 23764
#include <iostream>
#include <vector>
#include <cmath>
 
using namespace std;
 
int main(){
	
  int n, m, s, c;
  vector<int> x;
  
  cin>> n >> m;
  
  x.resize(n, -1);
  
  for(int i=0;i<m;i++){
  	cin>> s >> c;
  	if(x[s-1]!=-1 && x[s-1]!=c){
  	  cout<< "-1" <<endl;
  	  return 0;
  	}
  	x[s-1]=c;
  }
  
  if(n==1 && x[0]<=0){
    cout<< "0" <<endl;
    return 0;
  }
  
  if(n>1 && x[0]==0){
    cout<< "-1" <<endl;
    return 0;
  }
  
  cout<< max(x[0], 1);
  for(int i=1;i<n;i++) cout<< max(x[i], 0);
  cout<< endl;
 
  return 0;
}


Measured memory_bytes: 256
n,m=map(int,input().split())
s=list(map(int,input().split()))
t=list(map(int,input().split()))
a=[1]*(m+1)
for i in range(n):
    b=a[:]
    k=0
    for j in range(m):
        b[j]=(k+b[j])%(10**9+7)
        if s[i]==t[j]:
            k=(k+a[j])%(10**9+7)
    b[-1]=(k+b[-1])%(10**9+7)
    a=b[:]
print(a[-1]%(10**9+7))


Measured memory_bytes: 3316
#include <bits/stdc++.h>
using namespace std;
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;
}
typedef long long int ll;

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define endl "\n"
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
const double PI = acos(-1);
const int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, a, b;
    cin >> n >> a >> b;
    vector<ll> H(n);
    for(ll i = 0; i < n; i++) {
        cin >> H[i];
    }

    auto check = [&](ll x) {
        vector<ll> h = H;
        for(ll i = 0; i < n; i++) {
            h[i] -= x * b;
        }
        ll cnt = 0;
        for(ll i = 0; i < n; i++) {
            if(h[i] <= 0) {
                continue;
            }
            cnt += (h[i] - 1) / (a - b) + 1;
        }
        return (cnt <= x);
    };

    ll ok = 1e9 + 1, ng = 0;
    while(llabs(ok - ng) > 1) {
        ll mid = (ok + ng) / 2;
        (check(mid) ? ok : ng) = mid;
    }
    cout << ok << endl;
}

Measured memory_bytes: 1792
#2進数化
def bin(w, l):
  if w == 0:
    return l

  l.append(w % 2)
  return bin(w // 2, l)

while True:
  try:
    w = int(input())
    w_bin = bin(w, [])
    ans = ""
    for i, v in enumerate(w_bin):
      if v:
        ans += str(2 ** i) + " "
    print(ans.rstrip())

  except EOFError:
    break


Measured memory_bytes: 5592
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <climits>
#include <utility>
#include <map>
#include <set>
#include <complex>
#include <numeric>

#include <time.h>
#include <float.h>

#define loop(i, n) for(i = 0; i < n; i++)
#define loop1(i, n) for(i = 1; i <= n; i++)
#define loop2(i, n, start) for(i = start; i <= n; i++)
#define loopIterators(it, vec) for(it = (vec).begin(); it != (vec).end(); it++)
#define loopRev(i, n) for(i = (n - 1); i >= 0; i--)
#define FLASH ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define testCases(t) cin >> t; while(t--)
#define PN(n) cout << (n)
#define PN1(n) cout << (n) << " "
#define PN2(a, b) cout << (a) << " " << (b) << " "
#define PNN1(n) cout << (n) << endl
#define PNN2(a, b) cout << (a) << " " << (b) << endl
#define PNN3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl
#define PrintArray(ar, n) for(i = 0; i < n; i++) cout << ar[i] << " "; cout << endl
#define PrintSet(ar, it) for(it = ar.begin(); it != ar.end(); it++) cout << *it << " "; cout << endl
#define PrintMap(map, it) for(it = map.begin(); it != map.end(); it++) PNN2(it -> first, it -> second)
#define sz(a) sizeof(a)
#define IN1(n) cin >> n
#define IN2(a, b) cin >> a >> b
#define IN3(a, b, c) cin >> a >> b >> c
#define ALL(s) s.begin(), s.end()
#define SWAP(a, b, c) c = a; a = b; b = c
#define STRSP(s) scanf("%[^\n]%*c", s)
#define ENTER PNN1("");
#define MOD7 1000000007
#define MOD9 10000009
#define MAX 100005
#define llPair std::pair<lli, lli>
#define iiPair std::pair<int , int>
#define ONLINE_JUDGE

typedef long long int lli;
typedef long int li;
typedef unsigned long long int ulli;
typedef long double ld;

using namespace std;

int main(void){
	FLASH

	#ifndef ONLINE_JUDGE
		freopen("input-stream.txt", "r", stdin);
    freopen("output-stream.txt", "w", stdout);
	#endif

	std::string str;
	std::string::iterator it;
	IN1(str);
	int a = 0, b = 0;

	loopIterators(it, str) if (*it == 'A') ++a; else ++b;

	PNN1(a == 3 || b == 3? "No": "Yes");

	#ifndef ONLINE_JUDGE
		fclose(stdin);
    fclose(stdout);
	#endif

	return 0;
}


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

// #undef DEBUG  // Uncomment this line to forcefully disable debug print.
#if DEBUG
template <typename T>
void debug(T value) {
  std::cerr << value;
}
template <typename T, typename... Ts>
void debug(T value, Ts... args) {
  std::cerr << value << ", ";
  debug(args...);
}
#define dbg(...)                              \
  do {                                        \
    cerr << #__VA_ARGS__ << ": ";             \
    debug(__VA_ARGS__);                       \
    cerr << " (L" << __LINE__ << ")" << endl; \
  } while (0)
#else
#define dbg(...)
#endif

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T& value, Ts&... args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define in(type, ...) \
  type __VA_ARGS__;   \
  read_from_cin(__VA_ARGS__);

template <typename T>
void write_to_cout(const T& value) {
  std::cout << value << std::endl;
}
template <typename T, typename... Ts>
void write_to_cout(const T& value, const Ts&... args) {
  std::cout << value << ' ';
  write_to_cout(args...);
}
#define out(...) write_to_cout(__VA_ARGS__);

#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using ll = long long;

using namespace std;

int main() {
  in(int, a, b);
  if (a > b) {
    out(a * 2 - 1);
  } else if (a == b) {
    out(a * 2);
  } else {
    out(b * 2 - 1);
  }
}


Measured memory_bytes: 3584
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
num = 0
for i in range(n):
    min_ab = min(a[i],b[i])
    num += min_ab
    a[i] -= min_ab
    b[i] -= min_ab
    if b[i] != 0:
        min_ab = min(a[i + 1],b[i])
        num += min_ab
        a[i + 1] -= min_ab
        b[i] -= min_ab
print(num)

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

int main() {
    int n;
    cin>>n;
    int p[n],d[n+1],o[n]={0},s=0;
    for (int i = 0; i < n; ++i) {
        scanf("%d",&p[i]);
        if(i>0&&i<n)d[i]=p[i]-p[i-1];
    }
    //cout<<d[2]<<endl;
    d[0]=(p[0]);
    d[n]=-1*(p[n-1]);
    for (int j = 0; j < n + 1; ++j) {
        s+=abs(d[j]);
    }
    for (int k = 0; k < n; ++k) {
        cout<<s+abs(d[k]+d[k+1])-(abs(d[k])+abs(d[k+1]))<<endl;
    }
    return 0;
}

Measured memory_bytes: 2432
import Foundation

#if swift(>=4.0)
extension String {
    func split(by separator: String) -> [String] {
        return self.components(separatedBy: separator)
    }
    func toArray() -> [String] {
        return self.map{String($0)}
    }
}
#else
extension String {
    func split(by separator: String) -> [String] {
        return self.componentsSeparatedByString(separator)
    }
    func count() -> Int {
        return self.characters.count
    }
    func toArray() -> [String] {
        return self.characters.map{String($0)}
    }
}
extension Array where Element: Comparable {
    func max() -> Element! {
        return self.maxElement()
    }
    func min() -> Element! {
        return self.minElement()
    }
    func sorted() -> Array {
        return self.sort()
    }
    func reversed() -> Array {
        return self.reverse()
    }
    mutating func sort() {
        self.sortInPlace()
    }
}
extension Array {
    init(repeating: Element, count: Int) {
        self.init(count: count, repeatedValue: repeating)
    }
}
func stride<T: Strideable>(from start: T, to end: T, by stride: T.Stride) -> StrideTo<T> {
    return start.stride(to: end, by: stride)
}
#endif
func readInt() -> Int? {
    return Int(readLine()!)
}
func readStringSplit(by separator: String) -> [String] {
    return readLine()!.split(by: separator)
}
func readIntSplit(by separator: String) -> [Int] {
    return readLine()!.split(by: separator).map{Int($0)!}
}

/*************** テストスペース ***************/



/*************** メインプログラム ***************/

let input = readIntSplit(by: " ")
let (N, T) = (input[0], input[1])
var ans = 10000
for _ in 0..<N {
    let input = readIntSplit(by: " ")
    let (c, t) = (input[0], input[1])
    if t <= T {
        ans = min(ans, c)
    }
}
print(ans>1000 ? "TLE" : ans)


Measured memory_bytes: 7272
h,w=map(int,input().split())
slist=[input() for i in range(h)]
c=1
while c==1:
  c=0
  for i in range(h):
    s=slist[i]
    if "#" not in s:
      del slist[i]
      h -= 1
      c=1
      break
  if c==0:
    for i in range(w):
      li = [slist[j][i] for j in range(h)]
      if len(set(li))==1 and "." in li:
        for j in range(h):
          slist[j]=slist[j][:i]+slist[j][i+1:]
        c=1
        w -=1
        break
for i in range(h):
  print(slist[i])

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
int N;
int main() {
    cin >> N;
    vector<long long> B(N, 0);
    for(int i = 0; i < N-1; ++i) {
        cin >> B.at(i);
    }

    vector<long long> A(N+1, 0);
    for(int i = 0; i < N-1; ++i) {
        if(i == 0) {
            A.at(i) = A.at(i+1) = B.at(i); 
        }else {
            if(A.at(i) > B.at(i)) {
                A.at(i) = A.at(i+1) = B.at(i);
            }else {
                A.at(i+1) = B.at(i);
            }
        }
    }

    long long sum = 0;
    for(int i = 0; i < N; ++i) {
        sum += A.at(i);
    }

    cout << sum << endl;
}

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

using ll = long long;

const int MAXN = 100010;
int N, M;
vector<pair<int, int>> adj[MAXN];

int t[MAXN];
int nEdges;
ll freq[3];

bool dfs(int v, int c) {
	bool valid = true;
	t[v] = c;
	freq[c]++;
	for (auto e : adj[v]) {
		nEdges++;
		int newc = (c + e.second) % 3;
		if (t[e.first] == -1) {
			valid &= dfs(e.first, newc);
		} else {
			valid &= (newc == t[e.first]);
		}
	}
	return valid;
}

int main() {
	scanf("%d %d", &N, &M);
	for (int i = 0; i < M; i++) {
		int a, b; scanf("%d %d", &a, &b); a--, b--;
		adj[a].emplace_back(b, 1);
		adj[b].emplace_back(a, 2);
	}

	ll ans = 0;
	fill_n(t, N, -1);
	for (int i = 0; i < N; i++) {
		if (t[i] != -1) continue;
		nEdges = 0;
		freq[0] = freq[1] = freq[2] = 0;
		if (dfs(i, 0)) {
			if (!freq[0] || !freq[1] || !freq[2]) {
				ans += nEdges / 2;
			} else {
				ans += freq[0]*freq[1] + freq[1]*freq[2] + freq[2]*freq[0];
			}
		} else {
			ll tot = freq[0] + freq[1] + freq[2];
			ans += tot*tot;
		}
	}
	printf("%lld\n", ans);
	
	return 0;
}


Measured memory_bytes: 10752
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
using namespace std;

typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef pair<int, int>P;
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
typedef vector<vector<ll>> mat;
typedef vector<int> vec;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
const ll mod = 1000000007;
const ll INF = mod * mod;
const ld eps = 1e-12;
const ld pi = acos(-1.0);
//int qp(int a,ll b){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}
ll qp(ll a,ll b,int mo){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
int dx[4] = { 0,1,0,-1 };
int dy[4] = { 1,0,-1,0 };



void solve() {
    ll a, b, c, d, e , m, n, k, x,l,r;
    string s;
    ll ans = 0;
    double dans = 0;
    string sans = "Yes";
    vector<ll> re;
    vector<ll> de;

    cin >> n;
    ll sum = 0;
    ll max = 0;

    rep(i,n){
        ll tmp;
        cin >> tmp;

        sum += tmp;
        max = std::max(max, tmp);
    }

    if(sum -max > max) sans = "Yes";
    else sans = "No";

    
    // std::cout << ans << endl; 

    std::cout << sans << endl; 
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(10);
    solve();
    // stop
        return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int s[10001]={1,1,0,0,1};
int main(){
	r(i,2,10001)for(int j=i*2;j<10001;j+=i)s[j]++;
	int a;
	while(cin>>a,a){
		while(a){
			if(!s[a]&&!s[a-2]){
				cout<<a-2<<' '<<a<<endl;
				break;
			}a--;
		}
	}
}

Measured memory_bytes: 3032
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
#define ll long long
using namespace std;

int solve(int n)
{
  int count = 0;
  while(n%2==0){
    n/=2;
    count++;
  }
  return count;
}

int main()
{
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  int max=-1;
  int ans=-1;
  for(int i=2;i<=n;i+=2)
  {
    int k = solve(i);
    if(k>=max){
      max=k;
      ans=i;
    }
  }
  if(n==1)
    cout << "1" << endl;
  else
    cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<string>
#include<iomanip>
#include<set>

using namespace std;

int main()
{
	int h,w;
	cin >> h >> w;
	vector<string> s(h);
	for(int i=0;i<h;i++)
		cin >> s[i];
	

	vector<vector<int> > ww(h,vector<int>(w,0));
	vector<vector<int> > hh(h,vector<int>(w,0));
	vector<vector<int> > ans(h,vector<int>(w,0));

	for(int i=0;i<h;i++)
	{
		int tmp=0;
		for(int j=0;j<w;j++)
		{
			if(s[i][j]=='.')
				tmp++;
			else
				tmp=0;
			ww[i][j]=tmp;
		}
		tmp=0;
		for(int j=w-1;j>=0;j--)
		{
			if(ww[i][j]==0)
				tmp=0;
			else
				tmp=max(tmp,ww[i][j]);
			ww[i][j]=tmp;
		}
	}

	for(int i=0;i<w;i++)
	{
		int tmp=0;
		for(int j=0;j<h;j++)
		{
			if(s[j][i]=='.')
				tmp++;
			else
				tmp=0;
			hh[j][i]=tmp;
		}
		tmp=0;
		for(int j=h-1;j>=0;j--)
		{
			if(ww[j][i]==0)
				tmp=0;
			else
				tmp=max(tmp,hh[j][i]);
			hh[j][i]=tmp;
		}
	}
	long long ansans=0;
	for(int i=0;i<h;i++)
	{
		for(int j=0;j<w;j++)
		{
			ans[i][j]=hh[i][j]+ww[i][j]-1;
			if(ans[i][j]<0)ans[i][j]=0;
			if(ans[i][j]>ansans)ansans=ans[i][j];
//			cout << ans[i][j] << " ";
		}
//		cout << endl;
	}

	cout << ansans << endl;

	


}

Measured memory_bytes: 51456
#include <stdio.h>
#define MAX 101

int n, ni = 1;
int A[MAX][MAX] = {0}, a[MAX] = {0}, b[MAX] = {0};

void serch(int x){
  int i;
  
  a[x] = ni++;
  for(i=1; i<=n; i++){
    if(a[i] == 0 && A[x][i] == 1)
      serch(i);
  }
  b[x] = ni++;
}

int main(){
  int i, j, k, x, y;
  
  scanf("%d", &n);
  for(i=1; i<=n; i++){
    scanf("%d%d", &x, &k);
    for(j=1; j<=k; j++){
      scanf("%d", &y);
      A[x][y] = 1;
    }
  }
 
  for(i=1; i<=n; i++){
    if(a[i] == 0)
      serch(i);
  }
  
  for(i=1; i<=n; i++){
    printf("%d %d %d\n", i, a[i], b[i]);
  }
 
  return 0;
}

Measured memory_bytes: 2776