akhauriyash/Code-Regression huggingface dataset:
#include <cstdio>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <vector>
#include <iostream>
#include <ctime>
#include <unistd.h>

using namespace std;

#define TRACE(x) cerr << #x <<  " = " << x << endl
#define _ << " " <<
#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)

#define X first
#define Y second

typedef pair<int, int> P;
typedef long long ll;

const int MAX = 1<<17, LG = 31, INF = 0x3f3f3f3f;

int n;
int p[MAX];
int dp[MAX][LG];

// int solve(vector <int> V) {
//   if (V.empty()) return 0;
  
//   vector <int> T;
//   int ret = 0;
  
//   for (auto it : V) {
//     if (it == 1) {
//       ret += solve(T);
//       T.clear();
//       continue;
//     }
//   }
//   ret += solve(T);
  
//   int n = (int) V.size();
//   vector <int> Nep;

//   REP(i, n)
//     if (V[i] % 2)
//       Nep.push_back(i);

//   if (Nep.empty()) {
//     REP(i, V)
//       V[i] /= 2;
//     return solve(V) + 1;
//   }
//   if (Nep.size() == 1) {
    
//   }
// }

int main()
{
  memset(dp, INF, sizeof dp);

  scanf("%d", &n);
  REP(i, n) scanf("%d", &p[i]);

  dp[0][0] = 0;

  REP(i, n) {
    int v2 = 0;
    for (int tmp=p[i]; !(tmp % 2); tmp /= 2, v2++);
    int lg = 0;
    for (int tmp=1; tmp<p[i]; tmp*=2, lg++);
    
    REP(prije, LG) {
      // if (dp[i][prije] != INF)
      // 	TRACE(i _ prije _ dp[i][prije]);
      
      REP(des, 2) {
	REP(lev, 2) {
	  if (lev && des) continue;
	  if (prije == lg && (1<<lg != p[i]) && !lev) continue;
	  if ((1<<lg) != p[i] && !lev && !des) continue;
	  int ja = dp[i][prije];

	  int ost = prije;
	  if (lev) ost--;
	  if (des) {
	    ost++;
	    ja++;
	  }

	  ja += max(0, lg - ost - lev);
	  ost = lg - lev;
	  dp[i+1][ost] = min(dp[i+1][ost], ja);
	}
      }       
    }

    for (int j=LG-1; j>=0; j--)
      dp[i+1][j] = min(dp[i+1][j], dp[i+1][j+1]);
  }

  printf("%d\n", dp[n][0]);
  
  return 0;
}


Measured memory_bytes: 16768
import collections
#def input():
#    return sys.stdin.readline()[:-1]
N , Q = map(int,input().split())
graph = [[] for _ in range(N+1)]
point = [0] * (N+1)
for _ in range(N - 1):
    a , b = map(int,input().split())
    graph[a].append(b)
    graph[b].append(a)
#print(graph)
for _ in range(Q):
    a , b = map(int,input().split())
    #a = a - 1
    point[a] += b
q=collections.deque()
q.append(1)
checked=[0]*(N+1)

while q:
  v=q.pop()
  checked[v]=1
  
  for u in graph[v]:
    if checked[u]==1:
      continue
      
    point[u]+=point[v]
    q.append(u)
print(*point[1:])

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

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
	if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
	if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define INF 1000000007
#define LLINF 2696969696969696969ll
#define MAXN 16

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N;
ll grid[MAXN][MAXN];
ll val[(1 << MAXN)];
ll dp[(1 << MAXN)];

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N;
	FOR(i, 0, N)
	{
		FOR(j, 0, N)
		{
			cin >> grid[i][j];
		}
	}
	val[0] = 0;
	FOR(i, 1, (1 << N))
	{
		ll x = 31 - __builtin_clz(i);
		val[i] = val[i - (1 << x)];
		FOR(j, 0, N)
		{
			if (j == x) continue;
			if (!(i & (1 << j))) continue;
			val[i] += grid[x][j];
		}
	}
	FOR(j, 0, (1 << N))
	{
		for (int x = j; ; x = (x - 1) & j)
		{
			ckmax(dp[j], dp[j ^ x] + val[x]);
			if (x == 0) break;
		}
	}
	cout << dp[(1 << N) - 1] << '\n';
	return 0;
}


Measured memory_bytes: 1280
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for(int i=0;i<(n);i++)
using namespace std;

int N;

int pcnt(int x) {
  return __builtin_popcount(x);
}

int f(int a) {
  if (a == 0) return 0;
  return f(a % pcnt(a)) + 1;
}

int main() {
  cin >> N;
  string s;
  cin >> s;
  vector<int> X(N);
  rep(i, N) X[i] = s[i] - '0';
  int opc = 0;
  rep(i, N) opc += X[i];
  
  vector<int> ans(N);
  // b=0は0→1の反転をする場合
  rep(b, 2) {
    int pc = opc;
    if (b == 0) pc++;
    else pc--;
    if (pc <= 0) continue;
    
    // x(mod pc)を求める
    int y = 0;
    for (int i = 0; i < N; i++) {
      y = (2*y)%pc;
      y += X[i];
    }
    
    // ビット反転させていく
    int bit = 1;
    for (int i = N-1; i >= 0; i--) {
      if (b == X[i]) {
        int z = y;
        if (b == 0) {
          z = (z + bit) % pc;
        } else {
          z = (z - bit + pc) % pc;
        }
        ans[i] = f(z) + 1;
      }
      bit *= 2;
      bit %= pc;
    }
  }
  
  rep(i,N) cout << ans[i] << endl;
}

Measured memory_bytes: 5252
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define MAXN 1000010
#define rep(i, n) for(int i = 0; i < (n); ++i)
const ll mod = 1e9 + 7;

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

bool pairCompare(const P &firstElof, const P &secondElof) {
    return firstElof.second < secondElof.second;
}

int main() {
    ll a, b, c, k;
    ll ans = 0;
    cin >> a >> b >> c >> k;

    if(a < k) {
        k -= a;
        ans += a;
        if(b < k) {
            k -= b;
            if(k > 0) {
                ans -= k;
            }
        }
    } else {
        ans = k;
    }

    cout << ans << endl;
}


Measured memory_bytes: 3624
line = readlines
len = line.length
i = 0

while i < len
    line[i] = line[i].chomp
    i += 1
end

N = line[0].to_i
S = line[1]

count = 0
for i in 1..N-2 do
  if S[i-1..i+1] == "ABC" then
    count += 1
  end
end
  
print count

Measured memory_bytes: 1788
N = int(input())
H = [int(h) for h in input().split()]
INF = 10 ** 15

TotalMinCost = [INF for i in range(N)]
TotalMinCost[0] = 0
for i in range(N-1):
    TotalMinCost[i+1] = min(TotalMinCost[i+1], TotalMinCost[i] + abs(H[i+1] - H[i]))
    if i+2<N:
        TotalMinCost[i+2] = min(TotalMinCost[i+2], TotalMinCost[i] + abs(H[i+2] - H[i]))
print(TotalMinCost[N-1])

Measured memory_bytes: 13976
while True:
  s1 = list(input())
  if s1 == ["-"]:
    break
  s2 = list(input())
  s1.reverse()
  s2.reverse()
  under = list(input())
  under.reverse()
  right = []
  center = s2.pop()
  while s1 or s2:
    if under and center == under[-1]:
      center = s1.pop()
      under.pop()
    else:
      right.append(center)
      center = s2.pop()
  if not under:
    right.append(center)
  print("".join(right))


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

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

    const int MAX_RANGE = 100001 + 1;
    vector<int> table(MAX_RANGE, 0);
    table[0]++;
    table[MAX_RANGE]--;

    rep(i,N){
        int a,b;
        cin >> a >> b;
        table[a]++;
        table[b + 1]--;
    }

    int ans = 0;
    for(int i=1;i < MAX_RANGE; i++){
        table[i] += table[i-1];
        if(table[i] >= i) ans = i - 1;
    }

    cout << ans << endl;
}


Measured memory_bytes: 3100
n = int(input())
R = [int(input()) for i in range(n)]
minx = R[0]
maxx = -10**9
for i in range(1, n):
    if R[i] - minx > maxx:
        maxx = R[i] - minx
    if R[i] < minx :
        minx = R[i]
print(maxx)

Measured memory_bytes: 15648
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
#define INF 0x3f3f3f3f

const int MAXN = 1000;
int n , x[ MAXN + 5 ] , f[ MAXN + 5 ] , dp[ 5 * MAXN + 5 ];
vector< int > Graph[ MAXN + 5 ];

void dfs( int u ) {
	for( int i = 0 ; i < Graph[ u ].size( ) ; i ++ ) dfs( Graph[ u ][ i ] );
	memset( dp , 0x3f , sizeof( dp ) ); dp[ 0 ] = 0;
	for( int i = 0 ; i < Graph[ u ].size( ) ; i ++ ) {
		int v = Graph[ u ][ i ];
		
		for( int j = x[ u ] ; j >= 0 ; j -- ) {
			int tmp = INF;
			if( j >= x[ v ] ) tmp = min( tmp , dp[ j - x[ v ] ] + f[ v ] );
			if( j >= f[ v ] ) tmp = min( tmp , dp[ j - f[ v ] ] + x[ v ] );
			dp[ j ] = tmp;
		}
	}
	for( int i = x[ u ] ; i >= 0 ; i -- )
		f[ u ] = min( f[ u ] , dp[ i ] );
}

int main( ) {
	scanf("%d",&n);
	for( int i = 2 , fa ; i <= n ; i ++ ) {
		scanf("%d",&fa);
		Graph[ fa ].push_back( i );
	}
	for( int i = 1 ; i <= n ; i ++ )
		scanf("%d",&x[ i ]);
	
	memset( f , 0x3f , sizeof( f ) );
	dfs( 1 );
	
	puts( f[ 1 ] < INF ? "POSSIBLE" : "IMPOSSIBLE" );
	return 0;
}

Measured memory_bytes: 384
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define s second
#define all(container) container.begin(),container.end()
#define fast ios::sync_with_stdio(0); cin.tie(0)
// #ifndef ONLINE_JUDGE
// #include "debug.cpp"
// #endif
#define int long long
const int mod = 1e9+7;  
int power(int a,int b) { if(b==0) return 1; int res = power(a,b/2); if(b&1) return res * res * a; else return res * res; }


int32_t main(){
    
  fast;
  int n; cin >> n;

  if(n%2==1) cout << 0;
  else{

    int k = 5;
    int ans = 0;
    int num = n/2;
    // cout << num << '\n';
    while(num/k!=0){
      ans = ans + num/k;
      k=k*5;
      // cout << "k = " << k << '\n';
    }

    cout << ans;  
  }
  
  return 0;
}

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

using namespace std ;

#define sz(v) (int)v.size()
#define ll long long
#define all(v)  v.begin() , v.end()
#define rall(v)  v.rbegin() , v.rend()
#define pf push_front
#define pb push_back
#define fast FastInputOutput() ;
#define Clear( container , value ) memset( container , value , sizeof container )
#define PI acos( -1.0 )
#define forn( t , v , be , n ) for( t v = be ; v < n ; v++ )
#define fore( x , j ) for( auto x : j )
#define se second
#define fr first
//#define c_b fflush(stdin);

const ll Long_max = numeric_limits < ll > :: max() ;
const int Int_max = numeric_limits < int > :: max() ;

void File_input( string pathIn ) {
    freopen( pathIn.c_str(), "r", stdin ) ;
}

void File_output( string pathOut ) {
    freopen( pathOut.c_str(), "w", stdout ) ;
}

int msg() {
    srand( time( 0 ) ) ;
    vector < string > st = {
        "Wish everyone high ratings!",
        "Good luck!",
        "Have fun!",
        "Please, read all the problems!",
        "Go Go fight!"
    } ;
    cerr << st[ rand() % sz( st ) ] ;
    return 0 ;
}

void FastInputOutput() {
    ios_base :: sync_with_stdio( 0 ) ;
    cin.tie( 0 ) ;
    cout.tie( 0 ) ;
}

int main() {
  string a , b , c ;
  cin >> a >> b >> c ;
  cout << a.front() << b.front() << c.front() ;
  return 0 ;
}


Measured memory_bytes: 256
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;

public class Main {

	int INF = 1 << 28;
	boolean val[];
	String eq;
	int p;
	int MAX = 11;

	void run() {
		Scanner sc = new Scanner(System.in);
		for(;;) {
			eq = sc.next();
			if( eq.equals("#") )break;
			val = new boolean[MAX];
			boolean yn = true;
			for(int i=0;i<(1<<MAX);i++) {
				p = 0;
				for(int j=0;j<MAX;j++) val[j] = ((i>>j)&1) == 1;
				if( !equation() ) {
					yn = false;
					break;
				}
			}
			System.out.println( (yn? "YES": "NO") );
			
		}
	}
	
	boolean equation() {
		boolean l = formula();
		p++;
		boolean r = formula();
		debug(l, r);
		return !(l^r);
	}
	
	boolean formula() {
		switch (eq.charAt(p)) {
		case 'T': p++; return true;
		case 'F': p++; return false;
		case '(':
			p++;
			boolean l = formula();
			int tmp = p++;
			if( eq.charAt(p) == '>' ) { p++; tmp++;}
			boolean r = formula();
			p++;
			if( eq.charAt(tmp) == '*' ) return mlt(l, r);
			else if( eq.charAt(tmp) == '+' ) return add(l, r);
			else return inc(l, r);
		case '-': p++; return inv(formula());
		default : p++; return val(eq.charAt(p-1));
		}
	}
	
	boolean val(char x) {
		debug(x);
		return val[x-'a'];
	}
	
	boolean inv(boolean x) {
		return !x;
	}
	
	boolean add(boolean x, boolean y) {
		return x|y;
	}
	
	boolean mlt(boolean x, boolean y) {
		return x&y;
	}
	
	boolean inc(boolean x, boolean y) {
		return inv( mlt( x, inv(y) ) );
	}

	public static void main(String[] args) {
		new Main().run();
	}

	void debug(Object... os) {
		//System.err.println(Arrays.deepToString(os));
	}
}

Measured memory_bytes: 12000
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <limits.h>
#include <algorithm>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define N 100010
#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mp make_pair
#define inf 1000000007
#define mod 1000000007
#define pii pair <int, int>
#define all(x) x.begin(), x.end()
#define FOR(i, x, y) for (int i = x; i <= y; i++)

using namespace std;

int n;
int v[N];

ll sum, ans;

int main()
{
	scanf("%d", &n);
	
	FOR (i,1,n)
		scanf("%d", &v[i]);
	
	FOR (i,1,n+1)
		sum+=abs(v[i]-v[i-1]);
	
	FOR (i,1,n) {
		printf("%lld\n", sum-abs(v[i]-v[i-1])-abs(v[i]-v[i+1])+abs(v[i-1]-v[i+1]));
	}
}


Measured memory_bytes: 1664
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		long w = scan.nextLong();
		long h = scan.nextLong();

		if(w == 1 || h == 1) {
			System.out.println(1);
			return;
		}
		long c = (w*h)%2 == 0 ? (w*h)/2 : (w*h)/2+1;
		System.out.println(c);
	}

}

Measured memory_bytes: 21716
import sys
s = input()[::-1]
mod = 10**9 + 7
dp = [0 for i in range(13)]

if s[0] == '?':
    for i in range(10):
        dp[i] += 1
else:
    dp[int(s[0])] += 1

if len(s) == 1:
    print(dp[5])
    sys.exit()

for i, x in enumerate(s[1:], start=1):
    nextdp = [0 for j in range(13)]
    if x != '?':
        a = int(x) * pow(10, i, 13)
        for k in range(13):
            nextdp[(k + a)%13] += dp[k]%mod
            nextdp[(k + a)%13] %= mod
    else:
        for y in range(10):
            a = y * pow(10, i, 13)
            for k in range(13):
                nextdp[(k + a)%13] += dp[k]%mod
                nextdp[(k + a)%13] %= mod
    dp = nextdp
print(dp[5])

Measured memory_bytes: 74280
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cassert>
#include <cmath>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <cstdlib>

using namespace std;

#define INF 1e+9
#define mp make_pair
#define pb push_back
#define fi first
#define fs first
#define se second
#define i64 long long
#define li long long
#define lint long long
#define pii pair<int, int>
#define vi vector<int>

#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define fore(i, b, e) for (int i = (int)b; i <= (int)e; i++)

int dp[20][2];
void upd(int& x, int y) {
    x = max(x, y);
}

int main() {
#ifdef LOCAL
    freopen("inp", "r", stdin);
    //freopen("outp", "w", stdout);
#else
    // freopen(TASKNAME ".in", "r", stdin);
    // freopen(TASKNAME ".out", "w", stdout);
#endif
    i64 n;
    cin >> n;
    vi d;
    while(n) {
        d.pb(n % 10);
        n /= 10;
    }
    reverse(d.begin(), d.end());
    forn(digit, d.size())
        forn(flag, 2)
            dp[digit][flag] = -100500;
    dp[0][false] = 0;
    forn(digit, d.size())
        forn(flag, 2) {
            if (flag) {
                forn(chosen, 10) {
                    upd(dp[digit + 1][true], dp[digit][flag] + chosen);
                }
            } else {
                forn(chosen, d[digit]) {
                    upd(dp[digit + 1][true], dp[digit][flag] + chosen);
                }
                upd(dp[digit + 1][false], dp[digit][flag] + d[digit]);
            }
        }
    cout << max(dp[d.size()][0], dp[d.size()][1]);
}



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

long long int mod=1e9+7;

int main(){
  long long int n,a,b;
  cin>>n>>a>>b;
  vector<long long int> x(n);
  for(long long int i=0;i<n;i++)cin>>x[i];
  long long int ans=0;
  for(long long int i=1;i<n;i++){
    ans+=min(a*abs(x[i-1]-x[i]),b);
  }
  cout<<ans<<endl;
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define cout16 cout << setprecision(16) 
#define rep(i,n) for(int i=0;i<n;i++ )
#define rep2(i,f,n) for(int i=f;i<n;i++ )
#define MAX(A) *max_element(A.begin(),A.end())
#define MIN(A) *min_element(A.begin(),A.end())
#define SORT(A) sort(A.begin(),A.end())
#define REV(A) reverse(A.begin(),A.end())
typedef long long int ll;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vll = std::vector<ll>;
using vvll = std::vector<std::vector<ll>>;
using P = std::pair<int,int>;
using vp = std::vector<P>;
using namespace std;
#define INF 1001001001
#define LL_INF 1001001001001001001

int main(void) {
	int n; cin >> n;
	vi v(n); rep(i,n) cin >> v[i];
	vi c(n); rep(i,n) cin >> c[i];
	priority_queue<int> q;
	rep(i,n) q.push(v[i]-c[i]);
	int ans = 0;
	while(q.size()&&q.top()>0){
		ans += q.top();
		q.pop();
	}
	cout << ans << endl;
}




Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(void)
{
	int i,res=1e9,n,a,cnt;//1e9は10の9乗(十分に大きい数);
	cin>>n;
	for(i=1;i<=n;i++){
		cin>>a; 
		cnt=0;//2で割れる回数を数える 毎周0にリセットされる
		while (a%2==0){
			a=a/2;
			cnt++;
		}
		if(res>cnt) res=cnt;
	}
	cout<<res<<endl;
	return 0;
}

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

#define REP(i, n) for(int (i)=0;(i)<(n);++(i))
#define REPV(iter, v) for(auto (iter)=(v).begin(); (iter)!=(v).end();++(iter))
#define ALL(v) (v).begin(),(v).end()

#define MOD 1000000007

using namespace std;

typedef long long ll;

int main()
{
    ll N;
    cin >> N;
    vector<ll> b(N);
    REP(i, N)
    {
        cin >> b[i];
    }

    vector<ll> c(N, -1);
    bool flag = true;
    REP(i, N)
    {
        for (ll j = N - 1 - i; j >= 0; --j)
        {
            if (j + 1 == b[j])
            {
                c[N - 1 - i] = j + 1;
                b.erase(b.begin() + j);
                break;
            }
        }
        if (c[N - 1 - i] == -1)
        {
            flag = false;
            break;
        }
    }
    if (!flag)
    {
        cout << -1 << endl;
    }
    else
    {
        REP(i, N)
        {
            cout << c[i] << endl;
        }
    }
}


Measured memory_bytes: 3616
from itertools import permutations
import math

N=int(input())
T=[] # 町のリスト
lenlist=[] # 距離のリスト

for _ in range(N): # 各町について
    t=list(map(int,input().split(" ")))
    T.append(t)

# 各順列について
for p in list(permutations(T,N)):
    lsum=0

    # 各町について、前後の距離を求める
    for pxi in range(1,len(p)):
        l=math.sqrt((p[pxi][0]-p[pxi-1][0])**2+(p[pxi][1]-p[pxi-1][1])**2)
        lsum+=l

    lenlist.append(lsum)

print(sum(lenlist)/len(lenlist))

Measured memory_bytes: 9252
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Numerics;

namespace AtCoderWorkspace
{
    public class Solver
    {
        public void Solve()
        {
            var cin = new Scanner();
            var n = cin.nextInt();

            for (int i = 1; i <= 9; i++)
            {
                for (int j = 1; j <= 9; j++)
                {
                    if((i * j) == n)
                    {
                        Console.WriteLine("Yes");
                        return;
                    }
                }

            }

            Console.WriteLine("No");
        }
    }

    class Scanner
    {
        string[] s;
        int i;

        char[] cs = new char[] { ' ' };

        public Scanner()
        {
            s = new string[0];
            i = 0;
        }

        public string next()
        {
            if (i < s.Length) return s[i++];
            string st = Console.ReadLine();
            while (st == "") st = Console.ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            if (s.Length == 0) return next();
            i = 0;
            return s[i++];
        }

        public int nextInt()
        {
            return int.Parse(next());
        }
        public int[] ArrayInt(int N, int add = 0)
        {
            int[] Array = new int[N];
            for (int i = 0; i < N; i++)
            {
                Array[i] = nextInt() + add;
            }
            return Array;
        }

        public int[] DPArrayInt(int N, int ex)
        {
            int[] Array = new int[N+ex];
            for (int i = 0; i < N; i++)
            {
                Array[i] = nextInt();
            }
            return Array;
        }

        public long nextLong()
        {
            return long.Parse(next());
        }

        public long[] ArrayLong(int N, long add = 0)
        {
            long[] Array = new long[N];
            for (int i = 0; i < N; i++)
            {
                Array[i] = nextLong() + add;
            }
            return Array;
        }

        public long[] DPArrayLong(int N, int ex)
        {
            long[] Array = new long[N + ex];
            for (int i = 0; i < N; i++)
            {
                Array[i] = nextLong();
            }
            return Array;
        }

        public double nextDouble()
        {
            return double.Parse(next());
        }


        public double[] ArrayDouble(int N, double add = 0)
        {
            double[] Array = new double[N];
            for (int i = 0; i < N; i++)
            {
                Array[i] = nextDouble() + add;
            }
            return Array;
        }
    }   

    class Program
    {
        static void Main(string[] s) => new Solver().Solve();
    }
}


Measured memory_bytes: 13140
using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using static System.Console;
using static System.Math;

namespace AtCorder
{
    class Program
    {
        static void Main(string[] args)
        {
            new Program().Solve(new ConsoleInput(Console.In, ' '));
        }
        public void Solve(ConsoleInput cin)
        {
            var A = cin.ReadInt;
            var B = cin.ReadInt;
            var C = cin.ReadInt;
            var D = cin.ReadInt;

            var ans = Min(B, D) - Max(A, C);
            WriteLine(Max(0, ans));
        }

        /*public long C(long n, long r)
        {
            if (r == 0 || r == n) return 1L;
            if (n < r) return 0L;
            long[,] Pascal = new long[n + 1, n + 1];
            Pascal[0, 0] = 1;
            for (long i = 1; i <= n; i++)
            {
                Pascal[i, 0] = 1;
                Pascal[i, i] = 1;
                for (long j = 1; j < n; j++)
                {
                    Pascal[i, j] = Pascal[i - 1, j] + Pascal[i - 1, j - 1];
                }
            }
            return Pascal[n, r];
        }*/

        public class ConsoleInput
        {
            private readonly System.IO.TextReader _stream;
            private char _separator = ' ';
            private Queue<string> inputStream;
            public ConsoleInput(System.IO.TextReader stream, char separator = ' ')
            {
                this._separator = separator;
                this._stream = stream;
                inputStream = new Queue<string>();
            }
            public string Read
            {
                get
                {
                    if (inputStream.Count != 0) return inputStream.Dequeue();
                    string[] tmp = _stream.ReadLine().Split(_separator);
                    for (int i = 0; i < tmp.Length; ++i)
                        inputStream.Enqueue(tmp[i]);
                    return inputStream.Dequeue();
                }
            }
            public string ReadLine { get { return _stream.ReadLine(); } }
            public int ReadInt { get { return int.Parse(Read); } }
            public long ReadLong { get { return long.Parse(Read); } }
            public double ReadDouble { get { return double.Parse(Read); } }
            public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; }
            public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
            public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }
        }
    }
}


Measured memory_bytes: 13268
#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<complex>
#include<cstdio>
using namespace std ;
typedef vector<int> vi ;
typedef vector<vi> vvi ;
typedef vector<string> vs ;
typedef pair<int, int> pii; 
typedef long long ll ;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()


int main(void){
	int n;	
	while(cin>>n,n){	
		vvi ma(n+1,vi(n+1));
		rep(i,n){
			rep(j,n){
				cin>>ma[i][j];
				ma[n][j]+=ma[i][j];
				ma[i][n]+=ma[i][j];
			}
		}
		rep(i,n){
			ma[n][n]+=ma[i][n];
		}
		rep(i,n+1){
			rep(j,n+1){
				printf("%5d",ma[i][j]);
			}
			cout<<endl;
		}
	}
}

Measured memory_bytes: 1248
#include <iostream>
#include <string>
using namespace std;
 
int main(){
  string N;
  cin >> N;
  if(N[0] == N[1] && N[1] == N[2]){
    cout << "Yes" << endl;
  }else if(N[1]==N[2] && N[2] == N[3]){
    cout << "Yes" << endl;
  }else{
    cout << "No" << endl;
  }
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define int long
#define ll long long
#define rep(i, n) for(int i = 0; i < n; i++)
#define IOS cin.tie(0); ios::sync_with_stdio(false)
using namespace std;
int gcd(int a,int b){return b ? gcd(b,a%b) : a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}

main() {
    int H,W; cin >> H >> W;
    int A,B; cin >> A >> B;
    string S,T;
    rep(i,W){
        if(A>i){
            S.push_back('1');
        }
        else S.push_back('0');
    }
    rep(i,W){
        if(S[i]=='0') T.push_back('1');
        else T.push_back('0');
    }
    rep(i,H){
        if(B>i){
            rep(j,W){
                cout << S[j];
            }
        }
        else {
            rep(j,W){
                cout << T[j];
            }
        }
        cout << endl;
    }
}


Measured memory_bytes: 1280
n, m, c = map(int, raw_input().split())
b = map(int, raw_input().split())
ret = 0
for i in range(n):
    a = map(int, raw_input().split())
    if sum([t[0] * t[1] for t in zip(a, b)]) + c > 0:
        ret += 1
print ret

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

typedef long long int LL;
#define REP(n) for(LL i = 0; i < (n); i++)
#define COUT(val) cout << (val) << endl
#define CIN(val) cin >> (val)
#define SORT(vec) sort(vec.begin(), vec.end())

int main(){
    int n, a, b, counter, keeper;
    string s;
    cin >> n >> a >> b;
    int ans = 0;
    for(int i = 1; i <= n; i++){
        s = to_string(i);
        counter = 0;
        for(int j = 0; j < s.size(); j++){
            if(s[j] == '0'){
                keeper = 0;
            } else if(s[j] == '1'){
                keeper = 1;
            } else if(s[j] == '2'){
                keeper = 2;
            } else if(s[j] == '3'){
                keeper = 3;
            } else if(s[j] == '4'){
                keeper = 4;
            } else if(s[j] == '5'){
                keeper = 5;
            } else if(s[j] == '6'){
                keeper = 6;
            } else if(s[j] == '7'){
                keeper = 7;
            } else if(s[j] == '8'){
                keeper = 8;
            } else if(s[j] == '9'){
                keeper = 9;
            }
            counter += keeper;
        }
        if(counter >= a && counter <= b)ans += i;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int P[100005], Y[100005];
vector<int> yd[100005];

int main()
{
	int N, M;
	cin >> N >> M;
	rep(i, M)
	{
		cin >> P[i] >> Y[i];
		yd[P[i]].push_back(Y[i]);
	}
	rep(i, N) sort(yd[i + 1].begin(), yd[i + 1].end());
	rep(i, M)
	{
		printf("%06d", P[i]);
		int n = lower_bound(yd[P[i]].begin(), yd[P[i]].end(), Y[i]) - yd[P[i]].begin() + 1;
		printf("%06d\n", n);
	}
	return 0;
}


Measured memory_bytes: 7808
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <utility>
using namespace std;

typedef long long ll;
ll MOD = 1e9+7;

int main()
{
	int n;
	int b[101];
	int firstnum[101];//それぞれの数字が最初に出てきた位置
	int lastnum[101];//最後
	bool f[101];
	vector<int> way;
	int ans=0;
	cin>>n;
	b[0]=0;
	fill(firstnum,firstnum+101,999);
	fill(lastnum,lastnum+101,-1);
	fill(f,f+101,false);
	f[0]=true;
	for(int i=1;i<n+1;i++)
	{
		cin>>b[i];
		firstnum[b[i]]=min(firstnum[b[i]],i);
		lastnum[b[i]]=max(lastnum[b[i]],i);
	}
	
	while(1)
	{
		int rem=-1;
		int cnt=0;
		for(int i=1;i<n+1;i++)
		{
			if(!f[i])
				cnt++;
			if(b[i]==cnt)
				rem=max(rem,i);
		}
		if(rem==-1)
			break;
		f[rem]=true;
		way.push_back(b[rem]);
	}
	bool res=true;
	for(int i=0;i<n+1;i++)
	{
		if(!f[i])
			res=false;
	}
	if(!res)
		cout<<-1<<endl;
	else{
		reverse(way.begin(),way.end());
		for(int i=0;i<way.size();i++)
			cout<<way[i]<<endl;
	}
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

const int INF = 1 << 30;
const int MOD = 1000000007;
const int MAX = 100000;
const int N = 100;

vector<int> G[N];
int visited[N], checked[N];

int find_no_visited(int n) {
	int ret = -1;
	rep(i, n) {
		if (visited[i]) continue;
		ret = i;
		break;
	}
	return ret;
}

int find_no_visited(int n, int i) {
	int ret = -1;
	for (auto j : G[i]) {
		if (visited[j]) continue;
		ret = j;
		break;
	}
	return ret;
}

int main() {
	int n; cin >> n;
	rep(i, n) {
		int _, n2; cin >> _ >> n2;
		rep(j, n2) {
			int k; cin >> k; k--;
			G[i].push_back(k);
		}
	}
	rep(i, n) { visited[i] = 0; checked[i] = 0;}

	stack<int> S;
	int T = 1;
	S.push(0);
	visited[0] = T;
	while (true) {
		T++;
		if (S.empty()) {
			int i = find_no_visited(n);
			if (i != -1) {
				visited[i] = T;
				S.push(i);
				continue;
			}
			else
				break;
		}
		int i = find_no_visited(n, S.top());
		if (i != -1) {
			visited[i] = T;
			S.push(i);
		}
		else {
			checked[S.top()] = T;
			S.pop();
		}
	}
	rep(i, n) cout << i+1 << ' ' << visited[i] << ' ' << checked[i] << endl;
}



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

const int N = 1e5 + 10;
int a[100][100];
int main() {
    long long n, m;
    cin >> n >> m;
    if(n == 1 && m == 1) cout << 1;
    else if(n == 1) cout << m - 2;
    else if(m == 1) cout << n - 2;
    else if(n == 2 || m == 2) cout << 0;
    else {
        cout << (n - 2) * (m - 2);
    }
    return 0;
}

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

// clang-format off
#define forn(i, x, y) for(int i = x; i < y; i++)
#define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(NULL)
// clang-format on

using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;

int main()
{
  IOS;
  int N;
  cin >> N;
  VVI A(N, VI(N));
  forn(i, 0, N) forn(j, 0, N) cin >> A[i][j];
  int MAX = 1 << N;

  VLL G(MAX);
  forn(mask, 1, MAX)
  {
    forn(i, 0, N) if (mask & (1 << i))
    {
      forn(j, i + 1, N) if (mask & (1 << j))
          G[mask] += A[i][j];
    }
  }

  VLL dp(MAX);
  forn(mask, 1, MAX)
  {
    dp[mask] = G[mask];
    for (int g = mask; g > 0; g = (g - 1) & mask)
    {
      dp[mask] = max(dp[mask], dp[g] + G[g ^ mask]);
    }
  }

  cout << dp[(MAX)-1] << endl;

  return 0;
}


Measured memory_bytes: 4244
import java.util.*;

public class Main {
    static final int MOD = 1000000007;
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		Combination cmb = new Combination(n * 2 + 1);
		long ans = 0;
		for (int i = 0; i <= Math.min(k, n - 1); i++) {
		    ans += cmb.getComb(n, i) * cmb.getComb(n - 1, i);
		    ans %= MOD;
		}
		System.out.println(ans);
   }

}

class Combination {
    static final int MOD = 1000000007;
    long[] fac;
    long[] finv;
    long[] inv;
    
    public Combination (int size) {
        fac = new long [size];
        finv = new long [size];
        inv = new long [size];
        fac[0] = 1;
        fac[1] = 1;
        finv[0] = 1;
        finv[1] = 1;
        inv[1] = 1;
        for (int i = 2; i < size; i++) {
            fac[i] = fac[i - 1] * i % MOD;
            inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
            finv[i] = finv[i - 1] * inv[i] % MOD;
        }
    }
    
    
    public long getComb(int n, int k) {
        if (n < k || n < 0 || k < 0) {
            return 0;
        }
        return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
    }
    
}



Measured memory_bytes: 32340
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,i;
    cin>>a>>b;
    if(a>=b)
    {
        for(i=1; i<=a; i++)
        {
            cout<<b;
        }
    }
    else
    {
        for(i=1; i<=b; i++)
        {
            cout<<a;
        }
    }
}


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

vector<int> v;

bool isPrime(int n) {
    for (int i = 0; i < v.size() && v[i] * v[i] <= n; i++) {
        if (n % v[i] == 0) return false;
    }
    return true;
}

int main() {
    v.push_back(2);
    v.push_back(3);
    for (int i = 5; i <= 10000; i += 2) {
        if (isPrime(i)) {
            v.push_back(i);
        }
    }
    
    int sum[v.size() + 1];
    fill(sum, sum + v.size() + 1, 0);
    for (int i = 1; i < v.size() + 1; i++) {
        sum[i] = sum[i - 1] + v[i - 1];
    }
    
    while (true) {
        int n;
        cin >> n;
        if (n == 0) return 0;
        int count = 0;
        for (int i = 1; i < v.size() + 1; i++) {
            if (v[i - 1] > n) break;
            for (int j = 0; j <= i; j++) {
                if (sum[i] - sum[j] == n) {
                    count++;
                }
            }
        }
        cout << count << endl;
    }
}

Measured memory_bytes: 3052
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<string>
#include<vector>
#include<deque>
#include<queue>

using std::cout;
using std::cin;
using std::setw;
using std::setfill;
using std::string;
using std::pair;
using std::make_pair;
using std::vector;
using std::queue;
using std::deque;
using std::priority_queue;
using std::next_permutation;
//using std::iota;
using std::sort;
using std::greater;
using std::max;
using std::min;

#define INF 999999999;
#define MOD 1000000007

#define EVEL 1

#ifndef EVEL
#define dbug(X) std::cout << #X << ":" <<X<<" " ;
#define dbugf(s) std::cout << s << " ";
#define dbugln std::cout<<"\n";
#else
#define dbug(X) {}
#define dbugf(s) {}
#define dbugln {}
#endif

int n;
vector<vector<int>> l;
queue<pair<int, int>> lq;
int Min[100];
bool flag[100];

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

    cin >> n;
    l.resize(n);
    for(int i = 0; i < n; i++){
        Min[i] = -1;
        int u, k, v;
        cin >> u >> k;
        for(int j = 0; j < k; j++){
            cin >> v;
            l[u-1].push_back(v-1);
        }
    }

    lq.push(make_pair(0, 0));
    while(!lq.empty()){

        pair<int, int> f = lq.front();lq.pop();
        dbug(f.first)dbug(f.second)dbugln;
        if(Min[f.first] == -1)Min[f.first] = f.second;
        else Min[f.first] = min(f.second, Min[f.first]);
        dbug(Min[f.first])dbugln;
        if(!flag[f.first]){
            for(int i = 0; i < l[f.first].size(); i++){
                lq.push(make_pair( l[f.first][i], f.second+1 ));
                dbug(l[f.first][i])dbug(f.second+1)dbugln;
            }
        }
        flag[f.first] = true;
        dbugln
    }

    for(int i = 0; i < n; i++){
        cout << i+1 << " " << Min[i] << "\n";
    }

    return 0;
}


Measured memory_bytes: 3220
#!/usr/bin/env python3
import sys

f = sys.stdin
A = [list(map(int, f.readline().split(' '))) for _ in range(3)]

f_A = A[0] + A[1] + A[2]

N = int(f.readline())
b = [int(f.readline()) for _ in range(N)]

row = {'0': 0, '1': 0, '2': 0}
col = {'0': 0, '1': 0, '2': 0}
left_diag = 0
right_diag = 0

result = 'No'
for i in range(N):
    idx = -1
    if b[i] in f_A:
        idx = f_A.index(b[i])
    else:
        continue
    i, j = idx // 3, idx % 3
    row[str(i)] += 1
    col[str(j)] += 1
    if i == j:
        left_diag += 1
    if i + j == 2:
        right_diag += 1
    if (row[str(i)] == 3 or col[str(j)] == 3 or left_diag == 3 or right_diag ==
            3):
        result = 'Yes'
        break
# print(row)
# print(col)
# print(left_diag, right_diag)
print(result)

Measured memory_bytes: 3064
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
#include<algorithm>

using namespace std;

#define rep(i, n) for(int i=0;i<(int)n;i++)
#define reep(i, s, t) for(int i=s;i<(int)t;i++)
#define irep(i, n) for(int i=(int)n-1;i>=0;i--)
#define ireep(i, n, s) for(int i=(int)n-1;i>=s;i--)
#define all(v) v.begin(), v.end()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;



int main(){
	int b, r, g, c, s, t;
	while(cin >> b >> r >> g >> c >> s >> t) {
		if(!b && !r && !g && !c && !s && !t) break;
		int ans = 100;
		t -=r+b+g+c+(2*s)+(b*5)+(r*3);
		ans += (15*(b+r))+(((b*5)+(r*3))*13)-(3*(b+r+g+c+s))+((g*7)+(c*2)+(-3*t));
		cout << ans << endl;
	}
	return 0;
}

Measured memory_bytes: 1164
n=int(input())
a=sorted(list(map(int,input().split())))
num=[0]*(a[-1]+3)
for x in a:
    num[x]+=1
ans=0
for i in range(len(num)-2):
    if ans<num[i]+num[i+1]+num[i+2]:
        ans=num[i]+num[i+1]+num[i+2]
print(ans)


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

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
using ll = long long;
string char_to_string(char val) {
    return string(1, val);
}

int main(){
    ll N, A, B, C, D, E; cin >> N >> A >> B >> C >> D >> E;

    ll res = min({A, B, C, D, E});

    if(N % res > 0) {
        cout << (N / res) + 1 + 4 << endl;
    } else if (N % res == 0) {
        cout << (N / res) + 4 << endl;
    }
}

Measured memory_bytes: 256
x, y, a, b, c = map(int, input().split())
red = [int(x) for x in input().split()]
green = [int(x) for x in input().split()]
red.sort()
green.sort()
candidates = [int(x) for x in input().split()] + red[-x:] + green[-y:]
candidates.sort()
print(sum(candidates[-x-y:]))

Measured memory_bytes: 23340
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct data {
	char str[34];
	int freq;
} data;

void swap(int *x, int *y) {
	int z;
	z = *x;
	*x = *y;
	*y = z;
}

int main() {
	char input[34];
	int i, lim = 0;
	int flag, len;
	int maxLen = 0, maxLenIndex;
	int maxFreq = 0, maxFreqIndex;
	data arrD[500];
	strcpy(arrD[0].str, "\0");
	while(scanf("%s", &input) == 1) {
		//scanf("%s", &input);
		//if(input[0] == '\n') break;
		//puts(input);
		flag = 0;
		for(i=0; i<=lim; i++) {
			if(strcmp(arrD[i].str, input) == 0) {
				arrD[i].freq++;
				if(arrD[i].freq > maxFreq) {
					swap(&arrD[i].freq, &maxFreq);
					maxFreqIndex = i;
				}
				flag = 1;
				break;
			}
		}
		if(!flag) {
			strcpy(arrD[i].str, input);
			lim++;
			len = strlen(arrD[i].str);
			if(len > maxLen) {
				swap(&maxLen, &len);
				maxLenIndex = i;
			}
		}
	}
	printf("%s %s\n", arrD[maxFreqIndex].str, arrD[maxLenIndex].str);
	return 0;
}

Measured memory_bytes: 576
inpl = lambda: list(map(int,input().split()))
N, M = inpl()
S = list(map(int,input()))
steps = [N]
i = N
while i > M:
    prev = i
    i -= M
    while S[i] > 0:
        i += 1
    if i >= prev:
        print(-1)
        exit()
    else:
        steps.append(i)
ans = []
cur = 0
while steps:
    prev = cur
    cur = steps.pop()
    ans.append(cur-prev)
print(' '.join(map(str,ans)))

Measured memory_bytes: 53724
N, M = list(map(int, input().split()))
X = list(map(int, input().split()))

if N == 1:
    ans = max(X) - min(X)
    print(ans)
    exit()

if ((M == 1) or (N >= M)):
    ans = 0
    print(ans)
    exit()

Xs = sorted(X)

diff = []
for i in range(M - 1):
    tmp = Xs[i + 1] - Xs[i]
    diff.append(tmp)

diffs = sorted(diff)
print(sum(diffs[:(M - 1) - (N - 1)]))


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

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define FF first
#define SS second
template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}
template<class S, class T>
ostream& operator<<(ostream& os, const pair<S,T>& p){
  return os << p.FF << " " << p.SS;
}
template<class T>
void maxi(T& x, T y){
  if(x < y) x = y;
}
template<class T>
void mini(T& x, T y){
  if(x > y) x = y;
}


const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1e9+7;
const LL INF = 1e15;

struct Edge{
  LL to, cost;

  Edge(LL t, LL c): to(t), cost(c)
  {}
  bool operator>(const Edge& rhs) const{
	return cost > rhs.cost;
  }
  bool operator<(const Edge& rhs) const{
	return cost < rhs.cost;
  }

};
typedef vector< vector<Edge> > Graph;

void Dijkstra(const Graph& edges, vector<LL>& d, int s){
  using Type = PLL;
  priority_queue<Type, vector<Type>, greater<Type> > pq;
  fill(d.begin(), d.end(), INF);
  d[s] = 0;
  pq.push(MP(0,s));

  while(!pq.empty()){
	Type pii = pq.top(); pq.pop();
	int v = pii.second;
	if(d[v] < pii.first) continue;
	for(unsigned int i=0;i<edges[v].size();++i){
	  const Edge& e = edges[v][i];
	  if(d[e.to] > d[v] + e.cost){
		d[e.to] = d[v] + e.cost;
		pq.push(MP(d[e.to], e.to));
	  }
	}
  }
}

int idx(int u, int v, int flag){
  return (u * 50 + v) * 2 + flag;
}

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

  int N, M;
  cin >> N >> M;

  VL xs(M), ys(M), ts(M);
  REP(i,M){
	cin >> xs[i] >> ys[i] >> ts[i];
	--xs[i];
	--ys[i];
  }

  LL V, A, B, C;
  cin >> V >> A >> B >> C;
	
  Graph G(500*50*2);
  REP(i,M){
	REP(k,C){
	  G[idx(xs[i], k, 0)].EB(idx(ys[i], (A*k+B)%C, 0), k*ts[i]);
	  G[idx(ys[i], k, 0)].EB(idx(xs[i], (A*k+B)%C, 0), k*ts[i]);
	  G[idx(xs[i], k, 1)].EB(idx(ys[i], (A*k+B)%C, 1), k*ts[i]);
	  G[idx(ys[i], k, 1)].EB(idx(xs[i], (A*k+B)%C, 1), k*ts[i]);
	}
  }
  REP(k,C)
	G[idx(N-1, k, 0)].EB(idx(N-1, k, 1), 0);

  VL d(500*50*2);
  Dijkstra(G, d, idx(0, V, 0));

  LL ans = INF;
  REP(k,C)
	mini(ans, d[idx(0, k, 1)]);
  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 29252
N,M,X=map(int,input().split())
S=[input().split() for _ in range(N)]
mintotal=10**9

for i in range(2**N):
    alg=[0]*M
    total=0
    for j in range(N):
        if ((i >> j) & 1):
            for k in range(M):
                alg[k]+=int(S[j][k + 1])
            total+=int(S[j][0])

    for l in range(M):
        if alg[l] < X:
            break
    else:
      mintotal = min(mintotal,total)

if mintotal == 10 ** 9:
    print(-1)
else:
    print(mintotal)


Measured memory_bytes: 9232
N=int(input())
D=list(map(int,input().split()))

f=True

if D[0]!=0:
	f=False
D.sort()

d={}
a=0
for i in range(N):
	if D[i] not in d:
		if D[i]==a:
			d[D[i]]=1
			a=D[i]+1
		else:
			f=False
			break
	else:
		d[D[i]] += 1

if not(f):
	print(0)
elif d[0]>1:
	print(0)
else:
	ans=1
	d = list(d.values())
	for i in range(1, len(d))[::-1]:
		ans *= (d[i-1]**d[i])%998244353
		ans %= 998244353
	print(ans)

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

namespace zyt
{
	int n, num;
	int work()
	{
		scanf("%d", &n);
		for (int i = 1; i <= n; i++)
		{
			int a;
			scanf("%d", &a);
			if (a != i)
				++num;
		}
		if (num <= 2)
			printf("YES");
		else
			printf("NO");
		return 0;
	}
}
int main()
{
	return zyt::work();
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
 
#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
#define int long long
#define INF 1e18
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; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ EACH(it, P) { s << "<" << *it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }
template<class T>void show(vector<T>v){for (int i = 0; i < v.size(); i++){cerr<<v[i]<<" ";}cerr<<"\n";}
typedef long long ll;

ll MOD = 2019;

ll add(ll x, ll y)
{
    x += y;
    if (x >= MOD)
        return x - MOD;
    return x;
}
ll sub(ll x, ll y)
{
    x -= y;
    if (x < 0)
        return x + MOD;
    return x;
}
ll mult(ll x, ll y)
{
    return (x * y) % MOD;
}
ll bin_pow(ll x, ll p)
{
    if (p == 0)
        return 1;
    if (p & 1)
        return mult(x, bin_pow(x, p - 1));
    return bin_pow(mult(x, x), p / 2);
}

signed main()
{
    string s;
    cin >> s;
    reverse(ALL(s));
    vector<int> rui((int)s.size()+1, 0);
    rui[1] = s[0] - '0';    
    for (int i = 0; i < s.size(); i++)
    {
        rui[i+1] = (s[i] - '0') * bin_pow(10,i) + rui[i];
        rui[i+1] %= 2019;
    }
   // show(rui);
    map<int, int> mp;
    int ans = 0;
    for (int i = 0; i < (int)s.size() + 1; i++)
    {
        if(mp[rui[i]]==0){
            mp[rui[i]]++;
        }
        else{
            ans += mp[rui[i]];
            mp[rui[i]]++;
            
        }
    }
    cout << ans << endl;

    return 0;
}

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

const int N = 305;
const int INF = 1001001001;
const ll LINF = 1LL<<60;

vector<vector<ll>> g(N, vector<ll>(N, LINF));

int main() {
  int n, m;
  ll l;
  cin >> n >> m >> l;

  //rep(i,n) g[i][i] = 0;

  rep(i,m) {
    int a, b;
    ll c;
    cin >> a >> b >> c;
    a--;
    b--;
    g[a][b] = c;
    g[b][a] = c;
  }

  vector<vector<pair<int, ll>>> d(n, vector<pair<int, ll>>(n, {INF, LINF}));
  rep(s,n) {
    d[s][s] = {0, 0};
    priority_queue<pair<pair<int, ll>, int>, 
                   vector<pair<pair<int, ll>, int>>,
                   greater<pair<pair<int, ll>, int>>> q;
    q.push({{0, 0}, s});
    while (!q.empty()) {
      auto p = q.top();
      q.pop();

      ll v = p.second;
      ll kaisuu = p.first.first;
      ll used = p.first.second;
      
      if (p.first > d[s][v]) continue;
      rep(nv,n) {
        if (nv == v) continue;
        if (g[v][nv] > l) continue;
        ll nused = used + g[v][nv];
        pair<int, ll> np = {0, 0};
        if (nused > l) np = {kaisuu + 1, g[v][nv]};
        else np = {kaisuu, nused};

        if (d[s][nv] > np) {
          d[s][nv] = np;
          q.push({np, nv});
        }
      }
    }
  }

  int q;
  cin >> q;
  rep(i, q) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    if (d[s][t].first == INF) cout << -1 << endl;
    else cout << d[s][t].first << endl;        
  }
  return 0;
}


Measured memory_bytes: 3904