akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;
const int N = 3e5 + 5;

int n , sum[N][2];
char s[N];

int main(){
	scanf("%d %s",&n,s+1);
	for(int i = 1 ; i <= n ; i++){
		if(s[i] == 'W') sum[i][0] = sum[i - 1][0] , sum[i][1] = sum[i - 1][1] + 1;
		else sum[i][0] = sum[i - 1][0] + 1 , sum[i][1] = sum[i - 1][1];
	}
	int ans = n;
	for(int i = 1 ; i <= n ; i++){
		ans = min(sum[n][0] - sum[i][0] + sum[i - 1][1] , ans);
	}
	printf("%d\n",ans);
}

Measured memory_bytes: 2944
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

typedef pair<ll, int> pli;
const int N = 100000;
ll w[N];
int t[N];
int x[N];
ll finish[N];
ll ans[3652426];

int main() {
  int n, q;
  cin >> n >> q;
  priority_queue<pli,vector<pli>,greater<pli> > Q1;
  REP(i,n) {
    cin >> w[i] >> t[i] >> x[i];
    Q1.push(pli(w[i],i));
  }
  ll work = 0;
  ll d0 = 0;
  ll d1 = 0;
  int n1 = 0;
  ll d2 = 0;
  ll dd = 0;
  int n2 = 0;
  priority_queue<pii, vector<pii>, greater<pii> > Q2;

  memset(finish,-1,sizeof(finish));
  for (int i=0; i<=3652425; ++i) {
    if (i) {
      work += 1 + d0 + d1 + d2;
    }
    //cout << d0 << " " << d1 << " " << d2 << " -> " << work << endl;
    ans[i] = work;
    //if (i==10) break;
    while(!Q1.empty()) {
      pli p = Q1.top();
      if (p.first > work) break;
      finish[p.second] = i;
      Q1.pop();
      Q2.push(pii(i+x[p.second], p.second));
      if (t[p.second] == 0) {
        d0++;
      } else if(t[p.second] == 1) {
        n1++;
      } else {
        n2++;
      }
    }
    while(!Q2.empty()) {
      pii p = Q2.top();
      if (p.first > i) break;
      Q2.pop();
      if (t[p.second] == 0) {
        d0--;
      } else if (t[p.second] == 1) {
        n1--;
        d1 -= x[p.second];
      } else {
        n2--;
        dd -= x[p.second];
        d2 -= x[p.second]*x[p.second];
      }
    }
    d1 += n1;
    d2 += dd*2+n2;
    dd += n2;
  }
  REP(i,n) {
    if (finish[i] < 0) {
      cout << "Many years later" << endl;
    } else {
      cout << finish[i] << endl;
    }
  }
  REP(i,q) {
    int y;cin>>y;
    cout << ans[y] << endl;
  }
}

Measured memory_bytes: 33000
# coding: utf-8
N, M = map(int, input().split())
H = list(map(int, input().split()))

lst = [1 for i in range(N)]

for i in range(M):
    A, B = map(int, input().split())
    if H[A-1] <= H[B-1]:
        lst[A-1] = 0
    if H[B-1] <= H[A-1]:
        lst[B-1] = 0

print(sum(lst))




Measured memory_bytes: 20028
import Data.List
import qualified Data.Map as M

comb :: Int -> Int
comb n = n * (n-1) `div` 2

calc :: [(Int, Int)] -> Int -> Maybe Int
calc xs k =
    let v = lookup k xs
    in (-) <$> fmap comb v <*> fmap (comb . (\x -> x-1)) v

solve :: [Int] -> [Int]
solve xs = map (h . g . f) xs
    where ys = map (\x -> (head x, length x)) . group . sort $ xs
          t = sum . map (comb . snd) $ ys
          mp = M.fromList ys
          f k = M.lookup k mp
          g (Just v) = comb v - comb (v-1)
          h x = t - x


main :: IO ()
main = getLine >> getLine >>= mapM_ print . solve . map read . words

Measured memory_bytes: 83068
// In the Name of God
#include<bits/stdc++.h>

#define test(t) int t; cin>>t; while(t--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORE(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
#define ll long long int
#define em emplace_back
#define pb push_back
#define INF 1e7+5
#define MOD int(1e9+7)
#define print printf
#define in scanf
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);

using namespace std;

template<class type>
void displaySingle(type &arr)
{
	for(auto i:arr)
		cout<<i<<" ";
	cout<<endl;
}

template<class type>
void displayDouble(type &arr)
{
	for(auto &&i : arr) 
	{
		for(auto &&j : i) 
			cout << j << " ";
		cout << std::endl;
	}
}


int main()
{
	fast
	int n;
	cin>>n;
	vector <int> arr(n);
	REP(i,n)
		cin>>arr[i];
	int count = 0;
	REP(i,n)
	{
		if((i+1)%2 == 1 && arr[i]%2 == 1)
			count++;
	}
	cout<<count<<endl;
}

Measured memory_bytes: 3612
#n = int(input())
x,n = map(int,input().split())
p_L = list(map(int,input().split()))

#p_L = list(sorted(p_L))
p_L = set(p_L)

sa = 101
for i in range(0,200):
    if i not in p_L:
        if sa > min(sa,abs(i-x)):
            sa = abs(x-i)
            ans = i
        #ans = min(i,)

print(ans)

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

int main()
{
	int a, b;
	char op;
	while (1)
	{
		cin >> a >> op >> b;
		if (op == '?')
			break;
		else if (op == '+')
			cout << a + b << endl;
		else if (op == '-')
			cout << a - b << endl;
		else if (op == '*')
			cout << a * b << endl;
		else if (op == '/')
			cout << a / b << endl;
	}
}


Measured memory_bytes: 3112
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cassert>
#include<climits>
#include<map>
#include<set>
#include<iomanip>
 
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define IINF (INT_MAX)
#define EPS 1e-6
#define equals(a,b) (fabs((a)-(b)) < EPS)
#define MAX 20
#define pow2(a) ((a)*(a))
 
using namespace std;
 
struct Point{
  double x,y;
  Point(double x=IINF,double y=IINF):x(x),y(y){}
};
 
struct Segment{
  Point p[2];
  Segment(Point p1=Point(),Point p2=Point()){ p[0] = p1,p[1] = p2; }
};
 
int n,CNT = 1;
Segment seg[MAX];
double dp[MAX][2][(1<<15)],sum;
double cost[MAX][2][MAX][2];
 
inline double getDist(Point a,Point b){ return sqrt(pow2(a.x-b.x)+pow2(a.y-b.y)); }
 
int main(){
 
  while(cin >> n,n){
    sum = 0;
    rep(i,n){
      cin >> seg[i].p[0].x >> seg[i].p[0].y >> seg[i].p[1].x >> seg[i].p[1].y;
      sum += getDist(seg[i].p[0],seg[i].p[1]);
    }
 
    rep(i,n)rep(j,2)rep(k,n)rep(l,2) cost[i][j][k][l] = getDist(seg[i].p[j],seg[k].p[l]);
 
    rep(i,n)rep(j,2)rep(k,(1<<n))dp[i][j][k] = IINF;
 
    rep(i,n)rep(j,2)dp[i][j][(1<<i)] = 0;
 
    for(int state=0;state<(1<<n);state++){
      for(int cur=0;cur<n;cur++){
    if( !( (state>>cur) & 1 ) )continue;
    for(int ep=0;ep<2;ep++){
      if(dp[cur][ep][state] == IINF)continue;
      for(int next=0;next<n;next++){
        if( (state>>next) & 1 )continue;
        if(cur == next)continue;
        for(int nep=0;nep<2;nep++){
          int nstate = state | (1<<next);
          dp[next][nep][nstate] = min(dp[next][nep][nstate],
                      dp[cur][ep][state]+cost[cur][(ep+1)&1][next][nep]);
        }
      }
    }
      }
    }
 
    double ans = IINF;
    rep(i,n)rep(j,2)ans = min(ans,dp[i][j][(1<<n)-1]);
    cout << "Case " << CNT++ << ": " << setiosflags(ios::fixed) << setprecision(5) << ans + sum << endl;
 
  }
  return 0;
}

Measured memory_bytes: 8508
#include <bits/stdc++.h>
using namespace std;
long long func(long long x)
{
    if (x % 4 == 0)
        return x;
    if (x % 4 == 1)
        return 1;
    if (x % 4 == 2)
        return x + 1;
    if (x % 4 == 3)
        return 0;
}
int main()
{
    long long A, B;
    cin >> A >> B;
    if (A == 0)
    {
        cout << func(B) << endl;
        return 0;
    }
    cout << (func(B) ^ func(A - 1)) << endl;
}

Measured memory_bytes: 256
# 解説放送の１つめ
N,M = map(int, input().split())
A = list(map(int, input().split()))
f = [-1, 2,5,5,4,5,6,3,7,6] # 1-indexed

# dp[i]: マッチi本数で作れる最大の数(str)
dp = [None]*(N+1)
dp[0] = ""

# strの数の大きいほう
def max_str(s1:str, s2:str)->str:
    """return bigger one"""
    l1, l2 = len(s1), len(s2)
    if l1 != l2:
        return s1 if l1>l2 else s2
    if l1 == l2: 
        return s1 if s1>s2 else s2
    
for i in range(2, N+1):
    nxt = ""
    for aj in A:
        # 配列外参照と，i本ちょうどで作れないものはだめ
        if i-f[aj]<0 or dp[i-f[aj]]==None: continue
        nxt = max_str(nxt, 
                      dp[i-f[aj]]+str(aj))
    if nxt!="":
        dp[i] = nxt
    #print(dp)
print(dp[N])


Measured memory_bytes: 28120
object Main extends App {
  var a = readLine.split(" ").map(_.toInt)
  var sum = 0
  for(i<-0 to a(0); j<-0 to a(0) if(i+j<=a(1))) {
    if(a(1)-i-j<=a(0)) {
      sum = sum +1
    }
  }
  println(sum)
}

Measured memory_bytes: 63580
N = int(input())
S = list(input())
r,g,b = 0,0,0
k = 0
buf = 0

for i in range(N) :
    if S[i] == 'R' :
        r += 1
    elif S[i] == 'G' :
        g += 1
    else :
        b += 1

for j in range(N) :
    for i in range(j) :
        k = 2 * j - i
        if k <= N-1 :
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k] :
                buf += 1

print(r*g*b - buf)

Measured memory_bytes: 9228
import sys
from collections import deque

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N = int(input())
edges = [[] for _ in range(N + 1)]
for i in range(N - 1):
    a, b, c = [int(_) for _ in input().split()]
    edges[a].append((b, c))
    edges[b].append((a, c))

L = [-1] * (N + 1)


def w(k):
    que = deque()
    L[k] = 0
    que.append((k, -1))
    while que:
        n, p = que.popleft()
        for nn, nv in edges[n]:
            if nn == p: continue
            L[nn] = L[n] + nv
            que.append((nn, n))


def main():
    q, k = [int(_) for _ in input().split()]
    w(k)
    for i in range(q):
        a, b = [int(_) for _ in input().split()]
        print(L[a] + L[b])

if __name__ == '__main__':
    main()

Measured memory_bytes: 74672
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 998244353;

int main() {
	int N;
	cin >> N;
	map<int, int> cnt;
	vector<int> a(N);
	for (int i = 0; i < N; i++) {
		cin >> a[i];
		cnt[a[i]]++;
	}
	sort(a.begin(), a.end());
	a.erase(unique(a.begin(), a.end()), a.end());
	if (cnt.size() == 1) {
		if (cnt[0] == N) cout << "Yes" << endl;
		else cout << "No" << endl;
	}
	else if (cnt.size() == 2) {
		if (N % 3 == 0 && cnt[0] == N / 3) cout << "Yes" << endl;
		else cout << "No" << endl;
	}
	else if (cnt.size() == 3) {
		if (N % 3 == 0 && ((a[0] ^ a[1]) == a[2]) && cnt[a[0]] == N / 3 && cnt[a[1]] == N / 3) cout << "Yes" << endl;
		else cout << "No" << endl;
	}
	else {
		cout << "No" << endl;
	}
}


Measured memory_bytes: 4224
#include <bits/stdc++.h>
using namespace std;
 
int main(){
  int n,m;
  cin >> n >> m;
  vector<int>a(n);
  int sum=0;
  for(int i=0;i<n;i++){
    cin >> a.at(i);
    sum+=a.at(i);
  }
  sort(a.begin(),a.end());
  reverse(a.begin(),a.end());
  if(a.at(m-1)>=double(sum/(4.0*m))){
    cout << "Yes" << endl;
  }
  else{
    cout << "No" << endl;
  }
}


Measured memory_bytes: 3644
#include<iostream>
using namespace std;
int main() {
	int n;
	while (cin >> n&&n != 0) {
		int y[7], x[6] = { 9,19,29,39,49,59};
		fill(y, y + 7, 0);
		for (int i = 0; i < n; i++) {
			int a;
			cin >> a;
			bool b = false;
			for (int j = 0; j < 6; j++) {
				if (a <= x[j]) {
					y[j]++;
					b = true;
					break;
				}
			}
			if (!b) y[6]++;
		}
		for (int i = 0; i < 7; i++) {
			cout << y[i] << endl;
		}
	}
}

Measured memory_bytes: 3100
#include <iostream>

using namespace std;

long gcd(long a, long b) {
	if (a < b)
		swap(a, b);
	if (b == 0)
		return a;
	return gcd(b, a % b);
}

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

int main() {
	long a, b;
	while (cin >> a >> b)
		cout << gcd(a, b) << " " << lcm(a, b) << endl;
	return 0;
}

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

int main() {
    int N, M;
    cin >> N >> M;
    vector<int> s(M);
    vector<int> c(M);
    rep(i, M) {
        cin >> s[i] >> c[i];
        s[i]--;
    }
    if (N == 1) {
        for (int i = 0; i <= 9; i++) {
            bool flag = true;
            rep(m, M) {
                if (i != c[m]) flag = false;
            }
            if (flag) {
                cout << i << endl;
                return 0;
            }
        }
    } else if (N == 2) {
        for (int i = 10; i <= 99; i++) {
            bool flag = true;
            string is = to_string(i);
            rep(m, M) {
                if (is[s[m]] - '0' != c[m]) flag = false;
            }
            if (flag) {
                cout << i << endl;
                return 0;
            }
        }
    } else if (N == 3) {
        for (int i = 100; i <= 999; i++) {
            bool flag = true;
            string is = to_string(i);
            rep(m, M) {
                if (is[s[m]] - '0' != c[m]) flag = false;
            }
            if (flag) {
                cout << i << endl;
                return 0;
            }
        }
    }
		cout << -1 << endl;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <string.h>
int main(void){
  char number[4096];
  int sum=0, i,len;

  while(1){
    scanf("%s", number);
    if(number[0] == '0')  break;
    sum = 0;
    len=strlen(number);
    for(i = 0;i<len; i++){
      sum += (number[i] - '0');
    }
    printf("%d\n", sum);
  }
  return 0;
}



Measured memory_bytes: 2108
N = int(input())
A = [int(input()) for _ in range(N)]

k = max(range(N), key=lambda i: A[i])
for i in range(N):
    if i != k:
        print(A[k])
    else:
        print(
            max(
                max(A[:k], default=0),
                max(A[(k + 1):], default=0)
            )
        )


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

int main(void)
{
	unsigned long int a, b, c;
	unsigned long int i, j, k, l;
	unsigned long int A[12][13];
	unsigned long int B[12];
	unsigned long int sum = 1200001, check, t;
	int tub;

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

	for (i = 0; i < a; i++) {
		for (j = 0; j < b + 1; j++) {
			scanf("%ld", &(A[i][j]));
		}
		B[i] = 0;
	}

	for (i = 0; i < (long long int)(pow(2, a)); i++) {
		j = i;
		check = 0;
		for (k = 0; k < b; k++) {
			B[k] = 0;
		}

		for (k = 0; k < a ;k++) {
			if (j % 2 == 1) {
				check = check + A[k][0];
				for (l = 1; l < b + 1; l++) {
					B[l - 1] += A[k][l];
				}
			}
			j = j / 2;
		}
		t = 0;
		for (k = 0; k < b; k++) {
			if (B[k] < c) {
				t = 1;
			}
		}

		if ((t == 0) && (sum > check)) {
			sum = check;
		}
	}



	if (sum == 1200001) {
		printf("-1");
		return 0;
	}

	printf("%lld", sum);

	return 0;
}

Measured memory_bytes: 2276
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1e9
#define PI 3.14159265359
#define MOD 1000000007
#define ALL(v) v.begin(),v.end()
#define ALLR(v) v.rbegin(),v.rend()
typedef long long ll;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
//isPrime
//modpow modinv
//getDigit
int main() {
  cout << fixed << setprecision(10);
  int a,b;
  cin >> a >> b;
  int ans = a-1;
  if(a<=b) ans++;
  cout << ans << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <functional>
#include "math.h"
using namespace std;
int main(){
	int N,K;
	cin >> N >> K;
	int kihon = N % K;
	if (kihon >= 1){
		cout << "1" << endl;
	}
	else {
		cout << "0" << endl;
	}
}


Measured memory_bytes: 256
import java.util.Scanner;
class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    char[] dance = sc.next().toCharArray();
    boolean bl = true;
    for(int i = 0; i < dance.length; i++){
      if(i % 2 == 0){
        if(dance[i] == 'L'){
          bl = false;
          break;
        }
      }else{
        if(dance[i] == 'R'){
          bl = false;
          break;
        }
      }
    }
    if(bl){
      System.out.println("Yes");
    }else{
      System.out.println("No");
    }
  }
}

Measured memory_bytes: 22868
# 2019-11-24 00:33:09(JST)
import sys
import collections

def main():
    n, *a = map(int, sys.stdin.read().split())

    c = collections.Counter(a)

    ans = 'No'
    if 0 in c:
        if c[0] == n:
            ans = 'Yes'
        elif c[0] == n / 3 and len(c) == 2:
            ans = 'Yes'

    else:
        if len(c) == 3:
            for i in c.values():
                if i != n / 3:
                    break
            else:
                b = list(c.keys())
                if b[0] ^ b[1] == b[2]:
                    ans = 'Yes'
    print(ans)

if __name__ == '__main__':
    main()


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

using namespace std;

int main()
{
	map <int, bool> vis;
	int x, n;	cin >> x >> n;
	for (int i = 0; i < n; i++) {
		int a;	cin >> a;
		vis[a] = true;
	}

	int p1 = x, p2 = x;
	while (vis[p1] && vis[p2])
		p2++, p1--;

	if (!vis[p1])	cout << p1 << '\n';
	else cout << p2 << '\n';

	return 0;
}


Measured memory_bytes: 3636
#include<bits/stdc++.h>
using namespace std;
///Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout<<#var<<" "<<var<<endl;
#define co(q) cout<<q<<endl;
#define all(q) (q).begin(),(q).end()
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9+7;
const int MAX = 1e6;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int main()
{
    FastRead
    //freopen("output.txt", "w", stdout);
    int a,b,c,d=0;
    cin>>a>>b>>c;
    vector<int>v;
    for(int i=1; i<=1000; i++)
    {
        if(a%i==0 && b%i==0)
        {
            v.push_back(i);
        }
    }
    for(int i=v.size()-1;i>=0;i--){
        c--;
        if(c==0){
            cout<<v[i]<<endl;
            return 0;
        }
    }

    return 0;
}


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

typedef long long int ll;

string s,t;

ll dp[3002][3002];
ll so[3002][3002];
ll solve(ll i,ll j){
	
	if(i==-1 || j==-1)return 0;	
	
	if(dp[i][j]==-1){
	//cout<<" yo "<<endl;
	if(s[i]==t[j]){
		//cout<<i<<" "<<j<<endl;
		dp[i][j] = 1+solve(i-1,j-1);
		//ll y = solve(i+)
		
		so[i][j] = 0;
		
	}else{
		
	   ll x = solve(i-1,j);
	   ll y = solve(i,j-1);
	   
	   if(x>y){
	   	
	   	   dp[i][j] = x;
	   	   so[i][j] = 1;
	   	
	   }
	   else{
	   	
	   	dp[i][j] = y;so[i][j] = 2;
	   	
	   }
		
	
	}
	}
	return dp[i][j];
	
}

string an="";

void print(ll i,ll j){
	
	if(i==-1 || j==-1)return;
	
	if(so[i][j]==0){
		
		an+=s[i];
		print(i-1,j-1);
		
	}
	else{
		
		
		if(so[i][j]==1){
			
			print(i-1,j);
			
		}
		else
		if(so[i][j]==2)
		print(i,j-1);
		
	}
	
}

int main(){
	memset(dp,-1,sizeof(dp));
	cin>>s>>t;
	//cout<<t<<endl;
	solve(s.size()-1,t.size()-1);
	
	print(s.size()-1,t.size()-1);
	std::reverse(an.begin(),an.end());
	cout<<an<<endl;
}

Measured memory_bytes: 141312
#include<stdio.h>
#include<math.h>
int main()
{
    double x1,y1,x2,y2;
    scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
    //double distance=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
    double distance=hypot((x1-x2),(y1-y2));
    printf("%.8lf",distance);
}


Measured memory_bytes: 2108
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <cmath>
#include <tuple>
#include <iomanip>
#include <numeric>
#include <unordered_map>
#include <sstream>
#include<limits.h>
#include<float.h>
#include<list>
#include <array>
#include <complex>
#include<stdio.h>
#include<string.h>
#include <bitset>
// #include<assert.h>
#include<random>
using namespace std;
#define int long long

#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
#define MOD 1000000007
// #define MOD1 998244353 
#define MEM_SIZE 100010
#define DEBUG_OUT true
#define ALL(x) (x).begin(), (x).end()

template<typename T> void DEBUG(T e){if(DEBUG_OUT == false)return; std::cout << e <<" ";}
template<typename T> void DEBUG(const std::vector<T>& v){if(DEBUG_OUT == false)return;for(const auto& e : v){std::cout<< e << " "; } std::cout << std::endl;}
template<typename T> void DEBUG(const std::vector<std::vector<T> >& vv){if(DEBUG_OUT == false)return;for(const auto& v : vv){ DEBUG(v); } }
template<class T,class... Ts> void DEBUG(T d, Ts... e){if(DEBUG_OUT == false)return;DEBUG(d);DEBUG(e...);}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; abort();}}
template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
 

//BELOW
void solve(void)
{
  string str;
  cin>>str;
  int K;cin>>K;
  // vector<string> vec;
  set<string> SET;
  int N = str.size();
  for (int i = 1; i <= K; i++)
  {
    for (int j = 0; j < N -i+1; j++)
    {
      SET.insert(str.substr(j,i));
      // DEBUG(str.substr(j,i));
  }

  }
    for (int i = 0; i < K-1; i++)
  {
    SET.erase(SET.begin());
  }
  cout<<*SET.begin()<<endl;
  
  return;
}
int32_t main(int32_t argc, const char *argv[])
{
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  std::cout << std::fixed;
  std::cout << std::setprecision(11);
  solve();

  return 0;
}


Measured memory_bytes: 4696
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<numeric>
#include<cassert>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define rep(i,s,n) for(int i=(s); i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};

int getInt(){
  int ret = 0,c;
  c = getchar();
  while(!isdigit(c)) c = getchar();
  while(isdigit(c)){
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int n;
int g[100][100];
int num[100];
bool f[100];
int d;
int dm;

void solve(int pos){
  f[pos] = true;
  dm = max(dm, ++d);
  REP(i,num[pos]){
    int next = g[pos][i];
    if(f[next]) continue;
    solve(next);
  }
  f[pos] = false;
  --d;
}

int main(){
  while(n = getInt()){
    REP(i,100) {
      f[i] = false;
      num[i] = 0;
    }
    REP(i,n){
      int a = getInt()-1;
      int b = getInt()-1;
      g[a][num[a]++] = b;
      g[b][num[b]++] = a;
    }
    dm = 0;
    REP(i,100){
      solve(i);
    }
    print(dm);
  }
  return 0;
}

Measured memory_bytes: 872
#include <iostream>
#include <vector>
using namespace std;
int a[50][50], b[50][50];
int main(void) {
  int n, m;
  cin >> n >> m;
  vector<string> a(n), b(m);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < m; i++) cin >> b[i];
  bool ans = 0;
  for (int i = 0; i <= n - m; i++) {
    for (int j = 0; j <= n - m; j++) {
      bool f = 1;
      for (int k = 0; k < m; k++) {
        for (int l = 0; l < m; l++) {
          if (a[i + k][j + l] != b[k][l]) f = 0;
        }
      }
      if (f) ans = 1;
    }
  }
  if (ans) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
	    int x1 = sc.nextInt();
	    int y1 = sc.nextInt();
	    int x2 = sc.nextInt();
	    int y2 = sc.nextInt();
	    sc.close();
	    
	    System.out.println((y1-y2+x2)+" "+ (x2-x1+y2)+ " "+ (y1-y2+x1)+ " "+ (x2-x1+y1));

	}

}


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

// 型名の省略
typedef unsigned int uint;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> P;
typedef pair<lint, lint> Plint;
 
const int MOD = 1e9+7;
const int INF = 1<<18;
const lint INFLINT = 1LL << 60;
const double PI=acos(-1.0);
 
// cinとcoutの高速化
#define endl "\n"
struct cinacc{cinacc(){cin.tie(0);ios::sync_with_stdio(false);}}cinaccc;
struct coutacc{coutacc(){cout.setf(ios::fixed);cout.precision(12);}}coutaccc;
 
// 繰り返し
#define rep(i,n) for(lint i=0;i<(lint)(n);i++)
#define reps(i,n) for(lint i=0;i<=(lint)(n);i++)
#define rrep(i,n) for(lint i=(lint)(n)-1;i>0;i--)
#define rreps(i,n) for(lint i=(lint)(n)-1;i>=0;i--)

#define pb push_back

#define ALL(s) (s.begin(), s.end())
#define SZ(s) (s.size())

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

// Union-Find-Tree
class UnionFind {
	vector<int> par;
	UnionFind(int n):par(n){rep(i,n)par[i]=i;}
	int root(int x){if(par[x]==x)return x;return par[x]=root(par[x]);}
	void unite(int x,int y){int rx=root(x);int ry=root(y);if(rx==ry)return;par[rx]=ry;}
	bool same(int x,int y){return root(x)==root(y);}
};

int main() {
    lint n, k;
    cin >> n >> k;
    
    lint ans = 0, mx, mn;
    for(lint i = k; i <= n + 1; i++) {
        mn = i * (i - 1) / 2;
        mx = n * i - i * (i - 1) / 2;
        ans += mx - mn + 1;
        ans %= MOD;
    }
    
    cout << ans << endl;
    
    return 0;
}


Measured memory_bytes: 3652
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();	//トッピングの種類
		int baseprice = sc.nextInt();	//生地の値段
		int	topprice  = sc.nextInt();	//トッピングの値段
		int basecal = sc.nextInt();	//生地のカロリー
		int topcal [] = new int[N];
		for(int i = 0; i < N; i++){
			topcal [i] = sc.nextInt();	//トッピングのカロリー
		}
		for(int i = 0; i < N; i++){
			for(int k = 0; k < N; k++){
				if( topcal[i]>topcal[k]){
					int work = topcal[i];
					topcal[i] = topcal[k];
					topcal[k] = work;
				}
			}
		}
		Arrays.sort(topcal);
		for(int i=N;i>0;i--){
			if((basecal+topcal[i-1])/(baseprice+topprice)>=(basecal/baseprice)){
				basecal += topcal[i-1];
				baseprice += topprice;
				}
			}
		System.out.println(basecal/baseprice);
	}

}

Measured memory_bytes: 18132
/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(LL &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const LL &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
 #define DEBUG(...) {printf("# ");printf(__VA_ARGS__);puts("");}
#else
 #define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1e6+10;
VI e[SIZE];
int deg[SIZE];
int id[SIZE];
bool u[SIZE];
VI c;
int it;
void dfs(int x,int lt){
    id[x]=c[it++];
    for(int y:e[x]){
        if(y==lt)continue;
        dfs(y,x);
    }
}
int main(){
    int N; R(N);
    REPP(i,1,N){
        int x,y; R(x,y);
        e[x].PB(y);
        e[y].PB(x);
    }
    REP(i,N){
        int x; R(x);
        c.PB(x);
    }
    sort(ALL(c),greater<int>());
    LL an=0;
    REPP(i,1,SZ(c))an+=c[i];
    W(an);
    dfs(1,1);
    W(VI(id+1,id+N+1));
    return 0;
}


Measured memory_bytes: 25216
#include <bits/stdc++.h>
using namespace std;
int main() {
	long n,i,j,w,k=INT_MIN;
	cin>>n>>w;
	long a[n],b[n];
	for(i=0;i<n;i++)
	{
	    cin>>a[i]>>b[i];
	}
	long t[n+1][w+1];
	for(i=0;i<=n;i++)
	{
	    for(j=0;j<=w;j++)
	    {
	        if(i==0||j==0)
	        t[i][j]=0;
	        else if(a[i-1]<=j)
	        {
	            t[i][j]=max(b[i-1]+t[i-1][j-a[i-1]],t[i-1][j]);
	        }
	        else
	        t[i][j]=t[i-1][j];
	        //k=max(k,t[i][j]);
	    }
	}
	cout<<t[n][w]<<endl;
	return 0;
}

Measured memory_bytes: 79104
import sys 

N, K, Q = map(int, sys.stdin.readline().split())
A = (int(i) for i in sys.stdin.read().split())

l = [K - Q] * N
for i in A:
    l[i - 1] += 1

for i in range(N):
    if l[i] > 0:
        print("Yes")
    else:
        print("No")

Measured memory_bytes: 11996
//#define NDEBUG
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <utility>
#include <vector>

namespace n91 {

using i8 = std::int_fast8_t;
using i32 = std::int_fast32_t;
using i64 = std::int_fast64_t;
using u8 = std::uint_fast8_t;
using u32 = std::uint_fast32_t;
using u64 = std::uint_fast64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

struct rep {
  struct itr {
    usize i;
    constexpr itr(const usize i) noexcept : i(i) {}
    void operator++() noexcept { ++i; }
    constexpr usize operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  const itr f, l;
  constexpr rep(const usize f, const usize l) noexcept
      : f(std::min(f, l)), l(l) {}
  constexpr auto begin() const noexcept { return f; }
  constexpr auto end() const noexcept { return l; }
};
struct revrep {
  struct itr {
    usize i;
    constexpr itr(const usize i) noexcept : i(i) {}
    void operator++() noexcept { --i; }
    constexpr usize operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  const itr f, l;
  constexpr revrep(const usize f, const usize l) noexcept
      : f(l - 1), l(std::min(f, l) - 1) {}
  constexpr auto begin() const noexcept { return f; }
  constexpr auto end() const noexcept { return l; }
};
template <class T> auto md_vec(const usize n, const T &value) {
  return std::vector<T>(n, value);
}
template <class... Args> auto md_vec(const usize n, Args... args) {
  return std::vector<decltype(md_vec(args...))>(n, md_vec(args...));
}
template <class T> constexpr T difference(const T &a, const T &b) noexcept {
  return a < b ? b - a : a - b;
}
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
template <class F> class fix_point : private F {
public:
  explicit constexpr fix_point(F &&f) : F(std::forward<F>(f)) {}

  template <class... Args>
  constexpr decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};
template <class F> constexpr decltype(auto) make_fix(F &&f) {
  return fix_point<F>(std::forward<F>(f));
}
template <class T> T scan() {
  T ret;
  std::cin >> ret;
  return ret;
}

} // namespace n91
#include <array>
#include <map>
#include <set>
#include <string>

namespace n91 {

void main_() {
  /*
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  //*/
  std::set<u32> s;
  for (const usize i : rep(0, 3)) {
    s.insert(scan<u32>());
  }
  std::cout << (s.size() == 2 ? "Yes" : "No") << std::endl;
}

} // namespace n91

int main() {
  n91::main_();
  return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int S = sc.nextInt();
        int W = sc.nextInt();
        if (S <= W) {
            System.out.println("unsafe");
        } else {
            System.out.println("safe");
        }
        sc.close();
    }
}

Measured memory_bytes: 35784
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for(int i=m;i<(int)(n);i++)
typedef long long ll;
typedef pair<ll,ll> pint;
ll gcd(ll a,ll b){
    if(b==0){
        return a;
    }
    else{
        return gcd(b,a%b);
    }
}

int main(){
    ll n;cin>>n;
    string a;cin>>a;
    vector<vector<ll> > r(n+1,vector<ll>(26));
    rep(i,n){
      rep(j,26){
        r[i+1][j]=r[i][j];
      }
      r[i+1][a[i]-'a']=r[i][a[i]-'a']+1;
    }
    ll ans =0;
    ll MOD=1000000000+7;
    rep(i,n){
        ll ans1=1;
        int cnt=a[n-i-1]-'a';
        rep(j,26){
            if(cnt!=j){
            ans1*=(r[n-i][j]+1);
            ans1%=MOD;
            }
        }
        ans+=ans1;
        ans%=MOD;
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 24576
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
const ll MOD = 1e9+7;
const ll inf = (LLONG_MAX>>2);
int main(){
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n;
  string s;
  cin >> n >> s;
  ll ans = 0;
  for(ll l = n-1; l > 0; --l){
    ll crt = 0;
    for(ll i = 0; i+l < n; ++i){
      if(s[i] == s[i+l]){
        ++crt;
        ans = max(ans,min(l,crt));
      }
      else crt = 0;
    }
  }
  cout << ans;
  return 0;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
int n;
vector<vector<int>> t;
vector<int> wfs(int x) {
	vector<bool> vis(n, false);
	vector<int> d(n);
	vis[x] = true;
	queue<int> q;
	q.push(x);
	while(!q.empty()) {
		int cur = q.front();
		q.pop();
		for(int i = 0; i < t[cur].size(); i++) {
			int nxt = t[cur][i];
			if(vis[nxt]) continue;
			vis[nxt] = true;
			d[nxt] = d[cur] + 1;
			q.push(nxt);
		}
	}
	return d;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);  
	
	cin >> n;
	t.resize(n);
	for(int i = 0; i < n - 1; i++) {
		int a, b;
		cin >> a >> b;
		a--;
		b--;
		t[a].push_back(b);
		t[b].push_back(a);
	}
	vector<int> f = wfs(0), s = wfs(n - 1);
	int cnt = 0;
	for(int i = 0; i < n; i++) {
		if(f[i] <= s[i]) cnt++;
		else cnt--;
	}
	cout << (cnt > 0 ? "Fennec" : "Snuke") << endl;
	return 0;	
}

Measured memory_bytes: 6528
#pragma region include
#include <iostream>
#include <iomanip>
#include <stdio.h>

#include <sstream>
#include <algorithm>
#include <cmath>
#include <complex>

#include <string>
#include <cstring>
#include <vector>
#include <tuple>
#include <bitset>

#include <queue>
#include <complex>
#include <set>
#include <map>
#include <stack>
#include <list>

#include <fstream>
#include <random>
//#include <time.h>
#include <ctime>
#pragma endregion //#include
/////////
#define REP(i, x, n) for(int i = x; i < n; ++i)
#define rep(i,n) REP(i,0,n)
/////////
#pragma region typedef
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
#pragma endregion //typedef
////定数
const int INF = (int)1e9;
const LL MOD = (LL)1e9+7;
const LL LINF = (LL)1e18;
const double PI = acos(-1.0);
const double EPS = 1e-9;
/////////
using namespace::std;

void solve(){
	int N;
	string str;
	cin >> N >> str;
	vector<bool> SW(N);//T狼,F羊
	for(int i=0;i<4;++i){
		SW[0] = (i & 1)?true:false;
		SW[1] = (i & 2)?true:false;
		/*
		狼とxを数えて、
		偶数なら羊、奇数なら狼
		*/
		for(int j=0;j<N-2;++j){
			int res = SW[j] + SW[(j+1)];
			if( str[(j+1)] == 'x' ){
				++res;
			}
			if( res & 1 ){
				SW[j+2] = true;
			}else{
				SW[j+2] = false;
			}
		}
		int res;
		bool flag = true;
		{
			res = SW[N-2]+SW[N-1];
			if( str[N-1] == 'x' ){
				++res;
			}
			if( res & 1 ){
				//true;
				if( SW[0] != true ){
					flag = false;
				}
			}else{
				if( SW[0] != false ){
					flag = false;
				}
			}
			if( flag == false ) continue;
		}
		{
			res = SW[N-1]+SW[0];
			if( str[0] == 'x' ){
				++res;
			}
			if( res & 1 ){
				//true;
				if( SW[1] != true ){
					flag = false;
				}
			}else{
				if( SW[1] != false ){
					flag = false;
				}
			}
			if( flag == false ) continue;
		}
		if( flag ){
			for(int i=0;i<N;++i){
				cout << (SW[i]?'W':'S');
			}cout << endl;
			return;
		}
	}
	cout << -1 << endl;
}

#pragma region main
signed main(void){
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);
	std::cout << std::fixed;//小数を10進数表示
	cout << setprecision(16);//小数点以下の桁数を指定//coutとcerrで別	
	
	solve();
}
#pragma endregion //main()


Measured memory_bytes: 592
#include <iostream>
#include <stdio.h>
#include <cctype>
#include <cmath>
#include <math.h>
using namespace std;

struct cubes {
  int n;
  int e;
  int w;
  int s;
  int t;
  int b;
};

void spin(cubes* cube){
  int tmp;
      tmp = cube->n;
      cube->n = cube->e;
      cube->e = cube->s;
      cube->s = cube->w;
      cube->w = tmp; 
}

void roll(cubes* cube, char dst){
  int tmp;
  switch(dst){
    case 'N' :
      tmp = cube->b;
      cube->b = cube->n;
      cube->n = cube->t;
      cube->t = cube->s;
      cube->s = tmp; 
      break;
    case 'E' :
      tmp = cube->b;
      cube->b = cube->e;
      cube->e = cube->t;
      cube->t = cube->w;
      cube->w = tmp; 
      break;
    case 'W' :
      tmp = cube->b;
      cube->b = cube->w;
      cube->w = cube->t;
      cube->t = cube->e;
      cube->e = tmp; 
      break;
    case 'S' :
      tmp = cube->b;
      cube->b = cube->s;
      cube->s = cube->t;
      cube->t = cube->n;
      cube->n = tmp; 
      break;
    default : break;
  }
}

int main(){
  cubes cube;
  int cnt=0;
  int a,b,n;
  cin >> cube.t >> cube.s >> cube.e >> cube.w >> cube.n >> cube.b;
  cin >> n;
  for(int i=0;i<n;i++){
    cin >> a >> b;
    cnt=0;
    while(cube.t!=a){
      roll(&cube,'S');
      cnt++;
      if(cnt>3){
        cnt=0;
        spin(&cube);
      }
    }
    while(cube.s!=b){
      spin(&cube);
    }
    cout << cube.e << endl;
  }
}


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

int main() {
  int N;
  cin >> N;
  int cnt = 0;
  int cntm = 0;
  for(int i = 1;i <= N;i++){
    int p = i;
    while(p%2 != 1){
      p /= 2;
      cnt++;
    }
    cntm = max(cnt,cntm);
    cnt = 0;
  }
  cout << pow(2,cntm) << endl;
}

Measured memory_bytes: 384
package main

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

var stdInScanner = bufio.NewScanner(os.Stdin)

func nextInt() int {
	stdInScanner.Scan()
	i, _ := strconv.Atoi(stdInScanner.Text())
	return i
}

func many(A []int, m int, a []int, b []int, c []int, d []int, deps int) int {
	if deps >= len(A)-1 {
		return score(A, a, b, c, d)
	}
	nextDeps := deps + 1

	first := 1
	if deps >= 0 {
		first = A[deps]
	}
	max := 0
	for i := first; i <= m; i++ {
		nextA := A
		nextA[nextDeps] = i
		score := many(nextA, m, a, b, c, d, nextDeps)
		if max < score {
			max = score
		}
	}
	return max
}

func score(A []int, a []int, b []int, c []int, d []int) int {
	score := 0
	for i := 0; i < len(a); i++ {
		if A[b[i]-1]-A[a[i]-1] == c[i] {
			score += d[i]
		}
	}
	return score
}

func main() {
	stdInScanner.Split(bufio.ScanWords)

	N := nextInt()
	M := nextInt()
	Q := nextInt()

	a := make([]int, Q)
	b := make([]int, Q)
	c := make([]int, Q)
	d := make([]int, Q)

	for i := 0; i < Q; i++ {
		a[i] = nextInt()
		b[i] = nextInt()
		c[i] = nextInt()
		d[i] = nextInt()
	}

	fmt.Println(many(make([]int, N), M, a, b, c, d, -1))
}


Measured memory_bytes: 1788
#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define N 100009
using namespace std;

int n,a[N],fst[N],nxt[N],dgr[N],vis[N],num[N],f[N];
int main(){
	scanf("%d",&n);
	int i,j,k,sz,tmp,rst;
	for (i=1; i<=n; i++){
		scanf("%d",&a[i]);
		nxt[i]=fst[a[i]]; fst[a[i]]=i; dgr[a[i]]++;
	}
	int ans=1; bool flag;
	for (i=1; i<=n; i++) if (!vis[i]){
		for (j=i; !vis[j]; j=a[j]) vis[j]=1;
		sz=0; flag=1;
		for (; vis[j]!=2; j=a[j]){
			sz++; vis[j]=2;
			if (dgr[j]>2){ puts("0"); return 0; }
			else if (dgr[j]>1) flag=0;
		}
		if (flag){
			num[sz]++; continue;
		}
		for (; dgr[j]==1; j=a[j]); j=a[j];
		for (rst=0; vis[j]!=3; j=a[j]){
			vis[j]=3; rst++;
			for (k=fst[j]; k; k=nxt[k]) if (vis[k]<2){
				for (tmp=1; dgr[k]==1; k=fst[k]){
					vis[k]=1; tmp++;
				}
				vis[k]=1;
				if (dgr[k] || tmp>rst){ puts("0"); return 0; }
				if (rst>tmp) ans=(ans<<1)%mod;
				rst=0;
			}
		}
	}
	for (i=1; i<=n; i++){
		k=i>1 && (i&1)?2:1;
		for (j=f[0]=1; j<=num[i]; j++){
			f[j]=f[j-1]*k%mod;
			if (j>1) f[j]=(f[j]+(ll)f[j-2]*(j-1)%mod*i)%mod;
		}
		ans=(ll)ans*f[num[i]]%mod;
	} 
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 2560
using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Convert;
using static System.Math;
using Pair = System.Tuple<int, int>;
using Edge = System.Tuple<int, int, long>;
//using Debug;
//using static System.Globalization.CultureInfo;

class Program
{
    static void Main(string[] args)
    {
        var nl = Input.ar;
        var ar = Input.ar;
        var res = new int[nl[0] - 1];
        var f = -1;
        for (var i = 0; i < nl[0] - 1; i++)
            if (ar[i] + ar[i + 1] >= nl[1])
                f = i + 1;
        if (f == -1)
        {
            WriteLine("Impossible");
            return;
        }
        res[0] = f;
        var q = f-1;
        var index = 1;
        while (q > 0)
        {
            res[index] = q;
            index++;
            q--;
        }
        q = f + 1;
        while (q < nl[0])
        {
            res[index] = q;
            index++;
            q++;
        }
        WriteLine("Possible");
        WriteLine(string.Join("\n", res.Reverse()));
    }
}

public class Input
{
    public static string read => ReadLine();
    public  static int[] ar => Array.ConvertAll(read.Split(' '), int.Parse);
    public  static int num => ToInt32(read);
    public static long[] arL => Array.ConvertAll(read.Split(' '), long.Parse);
    public  static long numL => ToInt64(read);
    public static char[][] gred(int h) 
        => Enumerable.Repeat(0, h).Select(v => read.ToCharArray()).ToArray();
    public static int[][] ar2D(int num)
        => Enumerable.Repeat(0, num).Select(v => ar).ToArray();
    public static long[][] arL2D(int num)
        => Enumerable.Repeat(0, num).Select(v => arL).ToArray();
    public const long Inf = (long)1e18;
    public const double eps = 1e-6;
    public  const string Alfa = "abcdefghijklmnopqrstuvwxyz";
    public  const int MOD = 1000000007;
}


Measured memory_bytes: 32988
package main
import . "fmt"
const mod int64 = 1000000007
func main() {
  var n,m int64
  Scan(&n,&m)
  switch abs(n-m) {
    case 0  : Println((f(n)*f(m)*2)%mod)
    case 1  : Println((f(n)*f(m))%mod)
    default : Println(0)
  }
}
func f(n int64) int64 {
  var m int64 = n
  for i:=n-1;i>0;i-- {
    m = (m*i)%mod
  }
  return m
}
func abs(n int64) int64 {
  if n < 0 {
    return -n
  } else {
    return n
  }
}

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

int Cmp(const void *a, const void *b){
  return strcmp((const char *)a, (const char *)b);
}

void Do(int N, int L){
  int i;
  char S[N][L];
  for(i = 0;i < N;i++) scanf("%s", S[i]);
  qsort(S, N, sizeof(char) * L, Cmp);
  printf("%s\n", S[0]);
}

int main(void){
  int N, L;
  scanf("%d %d", &N, &L);
  Do(N, L);
  return 0;
}

Measured memory_bytes: 128
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <typeinfo>
#include <numeric>
#include <functional>
#include <unordered_map>
#include <bitset>
#include <stack>


using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for(ll i = 0; i < n; i++)










using P = pair<double, ll>;

int main() {
    ll n;
    while(cin >> n, n){
        vector<ll> b(n);
        vector<double> x(n), y(n);
        REP(i, n){
            cin >> b[i] >> x[i] >> y[i];
            b[i]--;
        }
        
        vector<vector<P>> g(n);
        REP(i, n){
            REP(j, n){
                if(i == j) continue;
                
                double d = sqrt((x[j] - x[i]) * (x[j] - x[i]) + (y[j] - y[i]) * (y[j] - y[i]));
                if(d <= 50.0){
                    g[b[i]].push_back({d, b[j]});
                    g[b[j]].push_back({d, b[i]});
                }
            }
        }
        
        ll m;
        cin >> m;
        REP(i, m){
            ll S, G;
            cin >> S >> G;
            S--; G--;
            
            vector<double> dist(n, INF);
            dist[S] = 0;
            vector<ll> prev(n);
            priority_queue<P, vector<P>, greater<P>> que;
            que.push({0, S});
            while(!que.empty()){
                auto tmp = que.top(); que.pop();
                double d = tmp.first;
                ll now = tmp.second;
                if(dist[now] < d) continue;
                
                for(auto &x : g[now]){
                    double cost = d + x.first;
                    ll v = x.second;
                    if(dist[v] > cost){
                        dist[v] = cost;
                        prev[v] = now;
                        que.push({cost, v});
                    }
                }
            }
            
            if(dist[G] == INF){
                cout << "NA" << endl;
                continue;
            }
            
            vector<ll> ans;
            for(ll v = G; v != S; v = prev[v]){
                ans.push_back(v + 1);
            }
            ans.push_back(S + 1);
            reverse(ans.begin(), ans.end());
            REP(i, ans.size()){
                cout << ans[i] << " \n"[i == ans.size() - 1];
            }
        }
    }
}



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

using namespace std;
typedef long long ll;
#define int ll

#define REP(i,n) for(int i=0;i<n;++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#  define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#  define DBG(fmt, ...)
#endif

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const double DINF = std::numeric_limits<double>::infinity();
const int MOD = 1000000007;
#define MAX_N 100010

signed main()
{
    bool flag[10] = {};
    REP(i, 4) {
        int n;
        cin >> n;
        flag[n] = true;
    }
    if(flag[1] && flag[9] && flag[7] && flag[4]) { printf("YES\n"); }
    else { printf("NO\n"); }

    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define FAST  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define ll long long int 
#define arr array
//power of 2 ...n&(n-1)==0

const int x=1e7;
int m[x+1];

int main()
{
	FAST;
	//#ifndef ONLINE_JUDGE
	//	freopen("input.txt","r",stdin);
	//	freopen("output.txt","w",stdout);
	//#endif
	 //ll testcase;
	 //cin>>testcase;
	 //for(ll t=0;t<testcase;t++)
	{
		
		int n;
		cin>>n;
		memset(m,0,sizeof(m));
		for(int i=1;i<=n;i++)
		{
			for(int j=0;j<=n;j+=i)
			{
				m[j]++;
			}
		}
		unsigned ll ans=0;
		for(int i=0;i<=n;i++)
		{
			ans+=(ll)i*m[i];
		}
		cout<<ans<<'\n';
	}
	return 0;
}




























Measured memory_bytes: 42704