akhauriyash/Code-Regression huggingface dataset:
N = int(input())
L = list(map(int, input().split()))
NumberList = []
CountList = []
for i in L:
    if i in NumberList:
        index = NumberList.index(i) #iがNumberListのどこにあるのか？
        CountList[index] += 1
    else:
        NumberList.append(i)
        CountList.append(1)

End = len(NumberList)

Total = 0
for i in range(0, End-2):
    for j in range(i+1, End-1):
        for l in range(j+1, End):
            if NumberList[i] + NumberList[j] > NumberList[l] and NumberList[j] + NumberList[l] > NumberList[i] and NumberList[l] + NumberList[i] > NumberList[j]:
                Count = CountList[i] * CountList[j] * CountList[l]
                Total += Count
            else:
                pass

print(Total)

Measured memory_bytes: 9228
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<P> vp;

const int MAX_N = 102;

int mn[MAX_N];

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vi t(n),v(n);
    rep(i,n){
        cin >> t[i];
    }
    rep(i,n){
        cin >> v[i];
    }
    int sm = accumulate(all(t),0);
    v.pb(0);
    mn[n-1] = sm+v[n];
    rrep(i,n-1){
        sm -= t[i+1];
        mn[i] = min(mn[i+1],sm+v[i+1]);
    }
    double pos = 0;
    double res = 0;
    double nw = 0;
    rep(i,n){
        rep(j,t[i]){
            rep(k,10){
                pos += 0.1;
                nw += 0.1;
                // show(nw);
                cmn(nw,min(pos,min((double)v[i],mn[i]-pos)));
                res += nw * 0.1;
            }
        }
    }
    printf("%.12f\n",res);
    return 0;
}


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

int main()
{
	int a, b, c, d, e,sin=3,data;
	cin >> a >> b >> c >> d >> e;
	while (sin > 2) {
		sin = 1;
		if (a < b) {
			data = a;
			a = b;
			b = data;
			sin = 3;
		}
		if (b < c) {
			data = b;
			b = c;
			c = data;
			sin = 3;
		}
		if (c < d) {
			data = c;
			c = d;
			d = data;
			sin = 3;
		}
		if (d < e) {
			data = d;
			d = e;
			e = data;
			sin = 3;
		}	
	}
	cout << a << " " << b << " " << c << " " << d << " " << e << endl;
}

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

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

Measured memory_bytes: 256
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#define Max 100005*2
#define INF 1000000000000000000
#define getMax(a,b) a<b?b:a
#define getMin(a,b) a<b?a:b
using namespace std;

vector<long long int>vec;

int main(){

	string s;
	int n;
	cin >> n;
	cin >> s;
	if(n%2 != 0){
		printf("No\n");
		return 0;
	}

	for(int i=n/2,j=0;i<n;i++,j++){
		if(s[i] != s[j]){
			printf("No\n");
			return 0;
		}
	}
	printf("Yes\n");
	return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <cmath>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#define rep(i,n) for (int i=0;i<n;i++)
using ll = long long;
using namespace std;

void solve(){

}
int main (){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    int now=0;
    int ans=0;
    int cnt=0;
    rep(i,n){
        int m;
        cin >> m;
        if(now<m){
            ans=max(ans,cnt);
            now = m;
            cnt=0;
        }
        else{
            ++cnt;
            now = m;
        }
    }
    ans = max(ans,cnt);
    cout << ans;
    return 0;
}

Measured memory_bytes: 3648
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int a[maxn];

int main()
{
    int n;
    cin >> n;
    long long ans = 0, sum;
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    for(int i = 1; i < n; i++){
        int l = 0, r = n - 1;
        sum = 0;
        while(r > i && (l < r || r % i))
            ans = max(ans, sum += a[l] + a[r]), l += i, r -= i;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 640
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct edge{
    int h,p,q;

    edge(int hh, int pp, int qq){
        h = hh;
        p = pp;
        q = qq;
    }

    bool operator<(const edge &arg)const {
        return h > arg.h;
    }
};

int main(){
    int n,m,a,h,p,q;

    while(cin >> n >> m >> a){
        if((n|m|a) == 0) break;
        
        vector<edge> v;
        for(int i=0;i<m;i++){
            cin >> h >> p >> q;
            v.push_back(edge(h,p,q));
        }

        sort(v.begin(), v.end());
        for(int i=0;i<m;i++){
            if(v[i].p == a){
                a = v[i].q;
            }else if(v[i].q == a){
                a = v[i].p;
            }
        }
        cout << a << endl;
    }
}

Measured memory_bytes: 932
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
	long long n,a[111][21],b[111][22],i,j,s,x,y,h,k,c[111];
	scanf("%lld",&n);
	for(i=0;i<n;i++)
	for(j=0;j<10;j++)
	scanf("%lld",&a[i][j]);
	
	for(i=0;i<n;i++)
	for(j=0;j<11;j++)
	scanf("%lld",&b[i][j]);
	
	for(i=1;i<1024;i++)
	{
		memset(c,0,sizeof(c));
		x=i;k=0;
		while(x)
		{
			y=x%2;
			x/=2;
			if(y==1)
			for(j=0;j<n;j++)
			{
				if(a[j][k])
				c[j]++;
			}
			k++;
		}
		h=0;
		for(j=0;j<n;j++)
		{
			h+=b[j][c[j]];
		}
		if(i==1)
		s=h;
		if(s<h)s=h;
	}
	printf("%lld\n",s);
} 

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

#define ll long long
#define endl '\n'
#define REP(i, a, n) for(int i=a; i<n; ++i)
#define pb push_back
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const double PI = 3.141592653589793238460;
const ll mod = 1e9 + 7;

void FAST(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

int main(){
  FAST();
  int n, i;
  cin >> n;
  vi arr(n);
  map<int, int> abc;
  REP(i, 0, n){
      cin >> arr[i];
      abc[arr[i] - 1] = i + 1;
  }

  REP(i, 0, n)
    cout << abc[i] << " ";
  cout << endl;



  return 0;
}


Measured memory_bytes: 5888
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll p=998244353ll;
const int N=200001;
template<class IT>inline void cmin(IT &a,IT b){if(b<a)a=b;}
template<class IT>inline void cmax(IT &a,IT b){if(a<b)a=b;}
int t[N],x[N],y[N];
int tre[N];
inline void add (int i){for(;i<N;i+=i&-i)++tre[i];}
inline int query(int i){int s=0;for(;i;i-=i&-i)s+=tre[i];return s;}
int c[N],a[N];
ll e[N];
inline ll f(int n,int a,int b){return e[n]-e[n-a]-e[n-b]+e[n-a-b];}
inline ll f(int n,int a,int b,int c,int d){return e[n]-e[n-a]-e[n-b]-e[n-c]-e[n-d]+e[n-a-b]+e[n-a-c]+e[n-a-d]+e[n-b-c]+e[n-b-d]+e[n-c-d]-e[n-a-b-c]-e[n-a-b-d]-e[n-a-c-d]-e[n-b-c-d]+e[n-a-b-c-d];}
int main(){
	int n,i,c1,c2,c3,c4;ll s=0ll;
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%d%d",&x[i],&y[i]);
	for(i=1;i<=n;++i)t[i]=x[i];
	sort(t+1,t+n+1);
	for(i=1;i<=n;++i)x[i]=lower_bound(t+1,t+n+1,x[i])-t;
	for(i=1;i<=n;++i)t[i]=y[i];
	sort(t+1,t+n+1);
	for(i=1;i<=n;++i)y[i]=lower_bound(t+1,t+n+1,y[i])-t;
	for(i=1;i<=n;++i)a[x[i]]=y[i];
//	for(i=1;i<=n;++i)printf("%d\n",a[i]);
	for(i=1;i<=n;++i){
		c[i]=query(a[i]);
		add(a[i]);
	}
//	for(i=1;i<=n;++i)printf("%d\n",c[i]);
	e[0]=1ll;
	for(i=1;i<=n;++i)e[i]=(e[i-1]<<1)%p;
//	for(i=1;i<=n;++i)printf("e %lld\n",e[i]);
//	for(i=1;i<=n;++i)printf("%d  %d %d %d %d  %d %d %d %d\n",n-1,i-1,n-i,a[i]-1,n-a[i],c[i],i-1-c[i],a[i]-1-c[i],n-i-a[i]+1+c[i]);
//	for(i=1;i<=n;++i)printf("%lld  %lld %lld %lld %lld  %lld %lld %lld %lld\n",e[n],e[i-1],e[n-i],e[a[i]-1],e[n-a[i]],e[c[i]],e[i-1-c[i]],e[a[i]-1-c[i]],e[n-i-a[i]+1+c[i]]);
	for(i=1;i<=n;++i){
		c1=c[i];
		c2=  i     -1-c1;
		c3=    a[i]-1-c1;
		c4=n-i-a[i]+1+c1;
		s+=e[n-1]+f(n-1,c1,c4)+f(n-1,c2,c3)-f(n-1,c1,c2,c3,c4);//printf("%lld\n",s);
	}
	printf("%lld",(s%p+p)%p);
	return 0;
}

Measured memory_bytes: 6528
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cmath>
#include <iomanip>
#include <bitset>
#include <string>
#include <cstring>
#include <stdlib.h>
using namespace std;
//cout <<  << endl;
typedef long long int ll;
typedef long double ld;

using Graph = vector<vector<ll>>;

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

#define MAX_N 2010

const long long INF = 1LL << 60;

//境界値に注意！！！

int main() {
    ll n;
    cin >> n;
    ll x[n];
    vector<ll> l(n);
    for (ll i=0;i<n;i++)cin >> x[i];
    ll s=x[n-1];
    l[n-1]=0;
    for (ll i=1;i<n;i++){
        if (s<=x[n-i-1]){
            l[n-i-1]=l[n-i]+1;
        }
        s=x[n-i-1];
    }
    sort(l.begin(),l.end());
    cout << l[n-1] << endl;
}


Measured memory_bytes: 1792
#include <iostream>
#include <cctype>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <string>
#include <functional>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define pb push_back 
#define YES cout<<"YES"<<endl
#define Yes cout<<"Yes"<<endl
#define NO cout<<"NO"<<endl
#define No cout<<"No"<<endl
typedef long long ll;
const int INF = 100000000, mod = 1000000007;
const ll LLINF = 1LL << 50;
using namespace std;


int main() {
    int n, a;
    cin >> n >> a;
    if (n % 500 <= a)Yes;
    else No;
    return 0;
}


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

using namespace std;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
constexpr int MOD = 1000000007;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};

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

const ll INF = 1LL << 61;

struct Edge {
  ll t;
  ll cost;
};

void solve() {
  ll N, M;
  cin >> N >> M;
  vector<vector<Edge>> G(N);
  for (ll i = 0; i < M; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    G[a].push_back({b, c});
  }
  vector<ll> dist(N, -INF);
  dist[0] = 0;
  
  bool update = true;
  ll cnt = 0;
  ll las = -INF;
  while(update) {
    update = false;
    for (ll i = 0; i < N; i++) {
      for(ll j = 0; j < G[i].size(); j++) {
        if (dist[G[i][j].t] < dist[i] + G[i][j].cost) {
          dist[G[i][j].t] = dist[i] + G[i][j].cost;
          update = true;
        }
      }
    }
    cnt++;
    if (cnt == N) {
      las = dist.back();
    }
    if (cnt > N) break;
  }
  if (cnt > N) {
    if (las == dist.back()) {
      cout << dist.back() << endl;
    } else {
      cout << "inf" << endl;
    }
  } else {
    cout << dist.back() << endl;
  }
  
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
using Int = int_fast64_t;

Int n, ans;
vector<Int> a;
multiset<Int> s;

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cin >> n;
	a.resize(n);
	for(Int i=0; i<n; ++i){
		Int t;
		cin >> t;
		a[t-1] = i;
	}
	s.insert(-1);
	s.insert(-1);
	s.insert(n);
	s.insert(n);
//	for(auto ite=s.begin(); ite!=s.end(); ++ite)
//		cout << *ite << "\n";
	for(Int i=n-1; i>=0; --i){
//		cout << i+1 << " " << a[i]+1 << " " << ans << "\n";
		auto j = s.insert(a[i]);
		auto k = next(j);
		auto p = prev(j);
//		cout << *prev(p) << " " << *p << " " << *k << " " << *next(k) << "\n";
		ans += (*next(k) - *k) * (*j - *p) * (i + 1);
//		cout << ans << "\n";
		ans += (*k - *j) * (*p - *prev(p)) * (i + 1);
//		cout << ans << "\n";
	}
	cout << ans << "\n";
}

Measured memory_bytes: 5760
l,r=map(int,input().split())

ans=2018
for i in range(l,r):
    if i%2019==0:
        ans=0
        break
    if i%673==0:
        for j in range(i+1,r+1):
            if j%3==0:
                ans=0
                break
            ch=(i*j)%2019
            ans=min(ch,ans)
    if i%3==0:
        for j in range(i+1,r+1):
            if j%673==0:
                ans=0
                break
            ch=(i*j)%2019
            ans=min(ch,ans)

    if ans==0:
        break
    for j in range(i+1,r+1):
        ch=(i*j)%2019
        ans=min(ch,ans)
        if j%2019==0:
            ans=0
            break

print(ans)

Measured memory_bytes: 41840
#include"bits/stdc++.h"

using namespace std;

int main() {
	int K,S;
	cin >> K >> S;
	int ans = 0;
	
	for (int x = 0; x <= K; x++) {
		for (int y = 0; y <= K; y++) {
			int z = S - x - y;
			if (0 <= z&&z <= K) {
				ans ++;
			}
		}
	}

	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <algorithm>
#include <vector>
#include <utility>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)

using P = std::pair<int, int>;

constexpr int MAX_N = 100;

int N;
std::vector<P> red, blue;

int main()
{
  scanf( "%d", &N );
  rep( i, N )
  {
    int a, b;
    scanf( "%d%d", &a, &b );
    red.push_back( P( a, b ) );
  }
  rep( i, N )
  {
    int c, d;
    scanf( "%d%d", &c, &d );
    blue.push_back( P( c, d ) );
  }

  std::sort( red.begin(), red.end() );
  std::sort( blue.begin(), blue.end() );

  int ans = 0;
  rep( i, N )
  {
    P ma = P( 0, -1 );
    rep( j, red.size() ) if( red[j].first <= blue[i].first && red[j].second <= blue[i].second )
      ma = std::max( ma, P( red[j].second, j ) );

    if( ma.second == -1 )
      continue;

    red.erase( red.begin()+ma.second );

    ++ans;
  }

  printf( "%d\n", ans );

  return 0;
}

Measured memory_bytes: 256
#!/usr/bin/env python
import sys
f = sys.stdin
n = int(f.readline())
s = list(map(ord, list(f.readline())[:-1]))
# A=65 Z=90
s = [chr((a - 65 + n) % 26 + 65) for a in s]
s = ''.join(s)

print(s)

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

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

using namespace std;

int main() {
  string s;
  std::cin >> s;

  int minv = 101;
  for (char c : s) {
    int maxv = 0;
    rep(i, s.size()) {
      int num = 0;
      while (s[i] != c && i < s.size()) {
        i++;
        num++;
      }
      maxv = max(maxv, num);
    }
    minv = min(minv, maxv);
  }

  std::cout << minv << std::endl;

  return 0;
}


Measured memory_bytes: 3604
import Queue
def is_bridge(_a, _b, neighbors, M):
    q = Queue.Queue()
    q.put(_a)
    used = {_a}
    for _ in range(M):
        if q.empty():
            break
        fr = q.get()
        for to, is_nei in enumerate(neighbors[fr]):
            if  is_nei and (fr != _a or to != _b) and to not in used:
                q.put(to)
                used.add(to)
    return len(used) < N


N, M = map(int, raw_input().split())
neighbors = [[False] * N for _ in range(N)]
ab = []
for _ in range(M):
    _a, _b = map(int, raw_input().split())
    _a -= 1
    _b -= 1
    neighbors[_a][_b] = True
    neighbors[_b][_a] = True
    ab.append((_a, _b))

res = 0
for _a, _b in ab:
    if is_bridge(_a, _b, neighbors, N):
        res += 1
print res

Measured memory_bytes: 3052
N, M, X = map(int,input().split())
A = set(map(int,input().split()))
l = set()
r = set()
for i in range(X):
    l.add(i)
for i in range(X, N):
    r.add(i)
l_tall = l & A
r_tall = r & A
if len(l_tall) <= len(r_tall):
    print(len(l_tall))
else:
    print(len(r_tall))

Measured memory_bytes: 3064
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>

typedef unsigned long long int uint64;
typedef long long int          int64;
 
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define ABS(a) ((a)>(0)?(a):-(a))

void get_N (int64 *n)
{
  int cnt;
  
  cnt = scanf("%lld", n);

  return;
}

void get_NK (int64 *n, int64 *k)
{
  int cnt;
  
  cnt = scanf("%lld %lld", n, k);

  return;
}

void omit_LF (void)
{
  char c;
  int  cnt;
  
  cnt = scanf("%c", &c);
  return;
}

char *get_S (int64 n)
{
  char *buf;
  int  i;

  buf = malloc((size_t)(n * sizeof(char)));
  if (buf == NULL) {
    return NULL;
  }

  scanf("%s", buf);

  return buf;
}

char *get_SN (int64 max, int64 *size)
{
  char *buf;
  int  i;
  int  n;

  buf = malloc((size_t)(max * sizeof(char)));
  if (buf == NULL) {
    return NULL;
  }

  scanf("%s", buf);

  n = strlen(buf);

  *size = n;
  
  return buf;
}

int64 *get_NSeq (int64 n)
{
  int64 *buf;
  int64 i;

  buf = malloc((size_t)(n * sizeof(int64)));
  for (i=0; i<n; i++) {
    get_N(&buf[i]);
  }

  return buf;
}

typedef struct _tmp_t {
  int64    a;
  int64    b;
} tmp_t;


int compare_a(const void *a, const void *b)
{
  tmp_t *x;
  tmp_t *y;

  x = (tmp_t *)a;
  y = (tmp_t *)b;

  if (x->a > y->a) return 1;
  if (x->a < y->a) return -1;

  return 0;
}

int compare_b(const void *a, const void *b)
{
  tmp_t *x;
  tmp_t *y;

  x = (tmp_t *)a;
  y = (tmp_t *)b;

  if (x->b > y->b) return 1;
  if (x->b < y->b) return -1;

  return 0;
}

void qsort_a (tmp_t *array, int size)
{
  qsort(array, size, sizeof(tmp_t), compare_a);
}

void qsort_b (tmp_t *array, int size)
{
  qsort(array, size, sizeof(tmp_t), compare_b);
}

int main()
{
  int64 H,W,N;
  int64 pc;
  int64 result;

  get_N(&H);
  get_N(&W);
  get_N(&N);

  pc = MAX(H, W);

  if ((N % pc) == 0) {
    result = N/pc;
  } else {
    result = N/pc + 1;
  }
   
  printf("%lld", result);
  

  return 0;
}



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

using namespace std;
using ll = long long;

int main() {
  string s, t;
  cin >> s >> t;
  int ans = 0;
  rep(i, 3) {
    if (s[i] == t[i]) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


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

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

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

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

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

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

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

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

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

ll Factorial(ll a){
    ll ret = 1;
    for(ll i=1; i<=a; i++){
        ret *= i;
        ret = ret % MOD;
    }
    return ret;
}

int main()
{
    cout << fixed << setprecision(15);
    ll N, M;
    cin >> N >> M;

    if(abs(N-M) >= 2){
        cout << 0 << endl;
        return 0;
    }
    if(abs(N-M) == 0){
        cout << (((Factorial(N) * Factorial(M))% MOD)*2) % MOD << endl;
        return 0;
    }
    else{
        cout << (Factorial(N) * Factorial(M))% MOD << endl;
    }
    return 0;
}

Measured memory_bytes: 3624
#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <stack>
#include <complex>
#include <stdlib.h>
#include <stdio.h>
#include <functional>


#define fs first
#define sc second

using namespace std;

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


const ll MAX_N = 201000;
class SegTree{
public:
    //セグメント木を持つグローバル変数
    int n;
    ll dat[2 * MAX_N - 1];

    void init(int n_, ll init_num){
        n = 1;
        while(n < n_) n *= 2;
        for(int i = 0; i < 2*n-1; i++){
            dat[i] = init_num;
        }
    }

    // k番目の値(0-indexed)をaに変更
    void update(int k, ll a){
        k += n-1;
        dat[k] = a;
        while(k > 0){
            k = (k-1)/2;
            dat[k] = min(dat[k*2+1], dat[k*2+2]);
        }
    }

    // [a, b)の最小値を求める。
    // kは節点の番号、l, rはその節点が[l, r)に対応づいていることを表す。
    // query(a, b, 0, 0, n)で外からは呼び出す。
    ll query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) {
            return (1LL<<31)-1;
        }

        if (a <= l && r <= b) {
            return dat[k];
        } else {
            ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
            ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
            return min(vl, vr);
        }
    }
};



int main(){
    int n, q;
    cin >> n >> q;

    SegTree s;
    s.init(n, (1LL<<31)-1);
    for(int i = 0; i < q; i++){
        int com, x, y;
        cin >> com >> x >> y;
        if(com == 0){
            s.update(x, y);
        }
        else{
            cout << s.query(x, y+1, 0, 0, s.n) << endl;
        }
    }

    return 0;

}



Measured memory_bytes: 5116
#include<bits/stdc++.h>
const int N=200005,V=6000000;
using namespace std;
typedef long long ll;
int n,va,f[N],ls[N],rs[N],
ha[N],ta[N],cnt,tb,tc;
ll de[N],L,R,mi,ans,ax[V],ay[V],
bx[N],by[N],cx[N],cy[N];
inline void sol(int x){
	if(!ls[x]){
		ha[x]=cnt;
		ax[cnt]=ay[cnt]=de[x];
		ta[x]=++cnt;
		return;
	}
	tb=tc=0;
	//cerr<<"sol "<<x<<endl;
	for(int i=ha[ls[x]],j=ha[rs[x]];
	i<ta[ls[x]];i++){
		for(;j<ta[rs[x]]&&ay[i]+ax[j]-2*de[x]<=mi;j++);
		if(j>ha[rs[x]])bx[tb]=ax[i],by[tb++]=ay[j-1];
	}
	for(int i=ha[rs[x]],j=ha[ls[x]];
	i<ta[rs[x]];i++){
		for(;j<ta[ls[x]]&&ay[i]+ax[j]-2*de[x]<=mi;j++);
		if(j>ha[ls[x]])cx[tc]=ax[i],cy[tc++]=ay[j-1];
	}/*
	cerr<<"merge "<<endl;
	for(int i=0;i<tb;i++)cerr<<'('<<bx[i]<<','<<by[i]<<')';cerr<<endl;
	for(int i=0;i<tc;i++)cerr<<'('<<cx[i]<<','<<cy[i]<<')';cerr<<endl;
	*/
	ha[x]=ta[x]=cnt;
	for(int i=0,j=0;i<tb||j<tc;)
	if(j==tc||i<tb&&bx[i]<cx[j]){
		for(;ha[x]<ta[x]&&ax[ta[x]-1]
		==bx[i]&&ay[ta[x]-1]>=by[i];)
		ta[x]--;
		if(ha[x]==ta[x]||ay[ta[x]-1]
		>by[i])ax[ta[x]]=bx[i],ay[ta[x]++]=by[i];
		i++;
	}
	else{
		for(;ha[x]<ta[x]&&ax[ta[x]-1]
		==cx[j]&&ay[ta[x]-1]>=cy[j];)
		ta[x]--;
		if(ha[x]==ta[x]||ay[ta[x]-1]
		>cy[j])ax[ta[x]]=cx[j],ay[ta[x]++]=cy[j];
		j++;
	}cnt=ta[x];
	/*cerr<<"= ";
	for(int i=ha[x];i<ta[x];i++)cerr<<'('<<ax[i]<<','<<ay[i]<<')';cerr<<endl;
	*/
}
int main(){
	scanf("%d",&n);
	for(int i=2;i<=n;i++){
		scanf("%d%d",f+i,&va);
		de[i]=de[f[i]]+va;
		R=max(R,de[i]);
		if(ls[f[i]])rs[f[i]]=i;
		else ls[f[i]]=i;
	}
	for(L=0,R<<=1;L<=R;){
		mi=L+R>>1,cnt=0;
		//cerr<<"che "<<mi<<endl;
		for(int i=n;i;i--)sol(i);
		if(ha[1]<ta[1])R=mi-1,ans=mi;
		else L=mi+1;
	}
	printf("%lld",ans);
}

Measured memory_bytes: 34048
#include<stdio.h>
#include<iostream>
#include<list>
#include<map>
#include<vector>
#include<algorithm>
#include<string.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define debug(x) cout << x << endl;
using namespace std;

char s[8][9];

void barn(int x, int y){
    if(x < 0 || y < 0 || x > 7 || y > 7) return;
    if(s[x][y] == '0') return;
    s[x][y] = '0';
    barn(x+1, y);
    barn(x+2, y);
    barn(x+3, y);
    barn(x-1, y);
    barn(x-2, y);
    barn(x-3, y);
    barn(x, y+1);
    barn(x, y+2);
    barn(x, y+3);
    barn(x, y-1);
    barn(x, y-2);
    barn(x, y-3);
}

int main(){
    int n;
    cin >> n;
    rep(i,n){
        int x, y;
        rep(j,8){
            cin >> s[j];
        }
        cin >> x >> y;
        barn(y - 1, x - 1);
        
        cout << "Data " << i + 1 << ':' << endl;
        rep(j,8){
            cout << s[j] << endl;
        }
    }
}

Measured memory_bytes: 1168
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
int main(void){
    int n = 0;
    while(cin >> n){
        if(n == 0){
            break;
        }
        else if(n == 1){
            cout << "deficient number" << endl;
        }
        else{
            int s = 1;
            for(int i = 2; i <= sqrt(n); i++){
                if(n % i == 0){
                    s = s + i;
                    if(i != sqrt(n)){
                        s = s + (n / i);
                    }
                }
            }
            if(s == n){
                cout << "perfect number" << endl;
            }
            else if(s > n){
                cout << "abundant number" << endl;
            }
            else{
                cout << "deficient number" << endl;
            }
        }
    }
    return 0;
}



Measured memory_bytes: 3080
#include <iostream>
#include <unordered_map> 
using namespace std;
using ll = long long;

const int INF = 1001001001;
const int MOD = 1000000007;

int main() {
	int n; cin>>n;
  	string str; cin>>str;
  
  	int r = 0;
	int g = 0;
	int b = 0;
 
	for (int i = 0;i < n;i++){
		if (str[i] == 'R'){
			++r;
		}else if (str[i] == 'G'){
			++g;
		}else{
			++b;
		}
	}

  	ll R = r;ll G = g;ll B = b;
	const ll total = R * G * B;

//  	cout << "total num of pairs: " << totalNumOfPairs << endl;

  	// Count case which doesn't satisfy second condition j - i != k - j
  	ll total_wrong = 0;
  	for (int i = 0; i < n - 2; i++) {
    	for (int j = 1; i+2*j < n; j++) {
//        	cout << "(" << i << ", " << j << ", " << j + 1 << ")" << endl;
/*          	cout << "i: " << i << " i+j: " << i+j << " i+2*j: " << i+2*j << endl;
			cout << "str[i]: " << str[i] << endl;
 			cout << "str[i+j]: " << str[i+j] << endl;
            cout << "str[i+2*j]: " << str[i+2*j] << endl;*/
            bool isFalse = str[i] == str[i+j] || str[i] == str[i+2*j] || str[i+j] == str[i+2*j];
			if (!isFalse) {
            	total_wrong++;
            }
        }
    }
  
  	ll ans = total-total_wrong;
  	cout << ans << endl;
}

Measured memory_bytes: 3624
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
	int n,i;
	scanf("%d",&n);
	int a[n];
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	sort(a,a+n);
	printf("%d",a[n-1]-a[0]);
}

Measured memory_bytes: 128
<?php
  fscanf(STDIN,'%d%d%d',$a,$b,$k);
  $c=min($a+$k,$b);
  for($i=$a;$i<$c;$i++)echo $i,"\n";
  for($i=max($c,$b-$k+1);$i<=$b;$i++)echo $i,"\n";

Measured memory_bytes: 3700
N, K = map(int, input().split())
data = [list(map(int, input().split())) for _ in range(N)]

ans = 10 ** 19
for a in range(N):
    for b in range(a+1, N):
        for c in range(N):
            for d in range(c+1, N):
                x0 = data[a][0]
                x1 = data[b][0]
                y0 = data[c][1]
                y1 = data[d][1]

                if x0 > x1:
                    x0, x1 = x1, x0
                if y0 > y1:
                    y0, y1 = y1, y0

                count = 0
                for i, j in data:
                    if x0 <= i <= x1 and y0 <= j <= y1:
                        count += 1
                if count >= K:
                    ans = min(ans, (x1 - x0)*(y1 - y0))

print(ans)

Measured memory_bytes: 54620
import sys
from functools import reduce
import copy
import math
from pprint import pprint


sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def int_inputs(num_of_input):
    ins = [int(input()) for i in range(num_of_input)]
    return ins


def solve(inputs):
    A500 = int(inputs[0])
    B100 = int(inputs[1])
    C50 = int(inputs[2])
    X = int(inputs[3])

    count = 0
    for a in range(A500 + 1):
        for b in range(B100 + 1):
            for c in range(C50 + 1):
                m = a * 500 + b * 100 + c * 50
                if m == X:
                    count += 1
    return count


def string_to_int(string):
    return list(map(int, string.split()))


if __name__ == "__main__":
    ret = solve(inputs(4))
    print(ret)


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

int main(void){
    int H, W, A, B;
    cin >> H >> W >> A >> B;
    
    for (int i=0; i<H; i++) {
        for (int j=0; j<W; j++) {
            if ((j < A) ^ (i < B)) {
                cout << 1;
            } else {
                cout << 0;
            }
        }
        cout << endl;
    }
}

Measured memory_bytes: 1280
class Unionfind:
    def __init__(self, n):
        self.par = [-1]*n
        self.rank = [1]*n
    
    def root(self, x):
        p = x
        
        while not self.par[p]<0:
            p = self.par[p]
        
        while x!=p:
            tmp = x
            x = self.par[x]
            self.par[tmp] = p
        
        return p
    
    def unite(self, x, y):
        rx, ry = self.root(x), self.root(y)
        
        if rx==ry: return False
        
        if self.rank[rx]<self.rank[ry]:
            rx, ry = ry, rx
        
        self.par[rx] += self.par[ry]
        self.par[ry] = rx
    
        if self.rank[rx]==self.rank[ry]:
            self.rank[rx] += 1
    
    def is_same(self, x, y):
        return self.root(x)==self.root(y)
    
    def count(self, x):
        return -self.par[self.root(x)]

N, M = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(M)]
ans = 0

for i in range(M):
    uf = Unionfind(N)
    
    for j in range(M):
        if j==i:
            continue
        
        a, b = ab[j][0], ab[j][1]
        uf.unite(a-1, b-1)
    
    if uf.count(0)!=N:
        ans += 1

print(ans)

Measured memory_bytes: 42588
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
int main()
{
    int n;
    int a[200001];
    int b[200001];
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            b[i]=a[i];
        }
        sort(a,a+n);
        int c=n/2,d=(n-1)/2;
        for(int i=0;i<n;i++)
        {
            if(b[i]<=a[d])
            {
                cout<<a[c]<<endl;
            }
            else
                cout<<a[d]<<endl;
        }
    }
}


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

using namespace std;

 int main(){

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

  int lmax=0,rmin=100000;

  for(int i=0;i<m;i++){
    int l,r; cin>>l>>r;
    lmax = max(lmax,l);
    rmin = min(rmin,r);
  }

  int ans = rmin-lmax+1;

  if(ans < 0)
    cout<<"0"<<endl;
  else
    cout<<ans<<endl;


return 0;
 }

Measured memory_bytes: 3568
import java.util.Scanner

object Main {

  def make(N: Int, M: Int, sc: Scanner): String = Array.fill[Array[Int]](N)(Array.fill[Int](M)(sc.nextInt)).foldLeft(Array.fill[Int](M)(0))((acc, arr) => acc.zip(arr).map { case (x, y) => x + y }).zipWithIndex.sortWith((a, b) => (if (a._1 == b._1) (a._2 < b._2) else (a._1 > b._1))).map(a=>a._2+1).mkString(" ")


  def solve(sc: => Scanner): Unit = {
    val N: Int = sc.nextInt
    val M: Int = sc.nextInt
    if (N != 0 && M != 0) {
      println(make(N, M, sc))
      solve(sc)
    }
  }

  def main(args: Array[String]): Unit = {
    val sc: Scanner = new Scanner(System.in)
    solve(sc)
  }
}

Measured memory_bytes: 177228
#include <bits/stdc++.h>
using namespace std;
int main()
{
   int n;
   cin >> n;
   string s;
   cin >> s;
   if(n>=s.size()){
      cout<<s;
   }
   else{
      for(int i=0;i<n;++i)cout<<s[i];
      cout<<"...";
   }
}


Measured memory_bytes: 3624
#include <bits/stdc++.h>
#include <math.h>
#define ALL(a)    a.begin(), a.end() 
#define rep(i, K, N)  for(long long int i = K; i < N; i++)
#define ll long long int
#define PP 1000000007
using namespace std;

int main(){
ll N, Ans = 0;cin >> N;
vector<ll> A(N + 1);
vector<bool> B(N + 1);

rep(i, 1, N + 1){
  
  cin >> A[i];

  if(i >= N/2 + 1){
    
    B[i] = A[i];
    
    if(A[i] == 1)Ans++;
    
  }

}

for(ll i = N/2; i > 0; i--){

  ll j = 2, cnt = 0;

  while(i*j <= N){

    cnt += B[i*j];j++;

  }

  if(cnt%2 != A[i]){B[i] = 1;Ans++;}

}
  
cout << Ans << endl;  
  
rep(i, 1, N + 1){
  
  if(B[i])cout << i << " ";
  
}
  
}

Measured memory_bytes: 4636
#include<bits/stdc++.h>
using namespace std ;
#define ull unsigned long long int
#define m 1000000007
ull powerArray[100001];

int main()
{
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   int n1,n2;
   cin>>n1>>n2;
   int sm = n1+n2;
   int pr = n1*n2;
   int sb = n1-n2;
   cout<<max(max(sm,pr),sb);
   return 0;
}

Measured memory_bytes: 256
import collections
def factorization(n):
    if n<2:
        return []
    
    prime_factor=[]
    
    for i in range(2,int(n**0.5)+1):
        while n%i==0:
            prime_factor.append(i)
            n//=i
            
    if n>1:
        prime_factor.append(n)
    return prime_factor

count=0
n=int(input())
 
c=collections.Counter(factorization(n))
d=list(c.values())
 
for v in c.values():
    for i in range(1,n):
        v-=i
        if v>=0:
            count+=1
        else:
            break
print(count)

Measured memory_bytes: 9744
N = int(input())
A = list(map(int, input().split()))
money = 1000
stock = 0
i = 0
while i < N - 1:
    if A[i + 1] > A[i]:
        # 全額buy
        stock += money // A[i]
        money -= (money // A[i]) * A[i]
        # print('buy', i)
        # print(money, stock)
    if A[i + 1] < A[i]:
        money += stock * A[i]
        stock = 0
    if i == N - 2 and stock > 0:
        money += stock * A[-1]
        stock = 0

    # print(i, money, stock)
    i += 1
print(money)


Measured memory_bytes: 9192
#include <iostream>
#include <vector>
#include <cstdio>

using namespace std;

#define fast ios::sync_with_stdio(0); cin.tie(0)

int main(){
  // 97-122
  vector<int> x (26,0);
  string word,ans = "Yes";
  cin>>word;
  for(int i=0; i<word.size(); ++i){
    int idx = (int)word[i] - 97; 
    x[idx] += 1;
  }
  for(int i=0; i<26; ++i){
    if(x[i] % 2 != 0) {
      ans = "No";
      break;
    }
  }
  cout<<ans<<endl;
  return 0;
}


Measured memory_bytes: 256
#define ll long long
#define MOD 1000000007
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define set2d(array, val, m, n) memset(array, val, sizeof(array[0][0]) * m * n);
#include <bits/stdc++.h>
using namespace std;
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
// #ifndef ONLINE_JUDGE
//   freopen("input.txt", "r", stdin);
//   freopen("output.txt", "w", stdout);
// #endif //*/
  ll n;
  cin >> n;
  ll a[n + 1];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll mm = a[0];
  int cnt = 0;
  for (int i = 0; i < n; i++)
  {
    if (mm >= a[i])
    {
      cnt++;
      mm = min(mm, a[i]);
    }
  }
  cout << cnt;
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}


Measured memory_bytes: 1792
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>

using namespace std;

typedef long long LL;
typedef pair<int , int> P2;
typedef pair<pair<int , int> , int> P3;
typedef pair<pair<int , int> , pair<int , int> > P4;
#define Fst first
#define Snd second
#define PB(a) push_back(a)
#define MP(a , b) make_pair((a) , (b))
#define M3P(a , b , c) make_pair(make_pair((a) , (b)) , (c))
#define M4P(a , b , c , d) make_pair(make_pair((a) , (b)) , make_pair((c) , (d)))
#define repp(i,a,b) for(int i = (int)(a) ; i < (int)(b) ; ++i)
#define repm(i,a,b) for(int i = (int)(a) ; i > (int)(b) ; --i)

const int ST_MAX = (1 << 19);
const LL INF = 12345678901234567;

template <class T> class ST{
	T x[ST_MAX * 2];
	
	T eval(T a , T b){
		return min(a,b);
	}
	
	void ren(int a){
		x[a] = eval(x[2 * a] , x[2 * a + 1]);
		if(a > 0) ren(a / 2);
	}
	
	T read_ST(int a , int b , int e , int s , int f){
		if(e <= s || f <= b) return INF;
		if(s <= b && e <= f) return x[a];
		return eval(read_ST(2 * a , b , (b + e) / 2 , s , f) , read_ST(2 * a + 1 , (b + e) / 2 , e , s , f));
	}
	
public:
	
	void dft(int a){
		repp(i,0,ST_MAX){
			x[ST_MAX+i] = (a == 1 ? ST_MAX-i : i) + INF;
		}
		repm(i,ST_MAX-1,0){
			x[i] = min(x[i*2],x[i*2+1]);
		}
	}
	
	void put(int a , T b){
		x[a + ST_MAX] = b;
		ren((a + ST_MAX) / 2);
	}
	
	T get(int s , int f){
		return read_ST(1 , 0 , ST_MAX , s , f);
	}
	
	T get_ans(int n){
		T z = INF;
		repp(i,1,n+1){
			z = min(z,x[ST_MAX+i]-i);
		}
		return z;
	}
	
	void debug(int n){
		printf("\n");
		repp(i,1,n+1){
			printf("%d %lld\n" , i , x[ST_MAX+i]-i);
		}
	}
};


int N,Q,A,B;
ST<LL> ans[2];
LL S;
int x[200010];

int main(){
	scanf("%d%d%d%d" , &N , &Q , &A , &B);
	repp(i,0,Q){
		scanf("%d" , x + i);
	}
	ans[0].dft(0);
	ans[1].dft(1);
	ans[0].put(A,abs(B-x[0])+A);
	ans[0].put(B,abs(A-x[0])+B);
	ans[1].put(A,abs(B-x[0])+ST_MAX-A);
	ans[1].put(B,abs(A-x[0])+ST_MAX-B);
	repp(i,1,Q){
		S += abs(x[i]-x[i-1]);
		LL z = min(ans[0].get(x[i],N+1)-x[i],ans[1].get(1,x[i]+1)-ST_MAX+x[i]);
		ans[0].put(x[i-1],z+x[i-1]-abs(x[i]-x[i-1]));
		ans[1].put(x[i-1],z+ST_MAX-x[i-1]-abs(x[i]-x[i-1]));
	}
	printf("%lld\n" , ans[0].get_ans(N)+S);
	return 0;
}


Measured memory_bytes: 17280
while True:
    n = int(input())
    if n == 0:
        break
    d = {}
    for _ in range(n):
        a, b = input().split()
        d[a] = b
    tmp = [input().strip() for _ in range(int(input()))]
    ans = [d.get(a, a) for a in tmp]
    print("".join(ans))

Measured memory_bytes: 9584
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <assert.h>
#include <math.h>
#include <stack>
#include <queue>
#include <list>
using namespace std;

#define __DEBUG__

#ifdef __DEBUG__
bool debug_disabled = false;
#else
bool debug_disabled = true;
#endif

#define dbg if (debug_disabled) {} else cerr

int a[51];
int b[51];
int c[51];
int d[51];

bool get_next(int M, vector<int>& v) {
    for (int i = 0; i < v.size() - 1; ++i) {
        if (v[i + 1] - v[i] > 0) {
            ++v[i];
            for (int j = i - 1; j >= 0; --j) v[j] = 1;
            return true;
        }
    }
    if (v[v.size() - 1] == M) return false;
    ++v[v.size() - 1];
    for (int i = 0; i < v.size() - 1; ++i) v[i] = 1;
    return true;
}

void solve() {
    int N, M, Q;
    cin >> N >> M >> Q;
    for (int i = 0; i < Q; ++i) {
        cin >> a[i] >> b[i] >> c[i] >> d[i];
    }

    int best = 0;
    vector<int> vec(N, 1);
    int tot = 0;
    do {
        /*
        for (int i = 0; i < vec.size(); ++i) {
            dbg << vec[i] << " ";
        }
        dbg << endl;
        */
        ++tot;
        int cur = 0;
        for (int i = 0; i < Q; ++i) {
            if (vec[b[i] - 1] - vec[a[i] - 1] == c[i]) cur += d[i];
        }
        if (best < cur) best = cur;
    } while(get_next(M, vec));

    // dbg << tot << endl;
    cout << best << endl;
}

int main() {
    cout.setf(ios::fixed, ios::floatfield);
    cout.setf(ios::showpoint);
    cout.precision(12);
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);

    int T = 1;
    // cin >> T;
    for (int i = 1; i <= T; ++i) {
        solve();
    }

    return 0;
}


Measured memory_bytes: 3624
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {

	static Scanner scan = new Scanner(System.in);
	static PrintWriter out = new PrintWriter(System.out);

	public static void main(String[] args) {
		while(scan.hasNext()){
			int[] a = new int[4];
			int[] b = new int[4];
			for (int i = 0; i < 4; i++) a[i] = si();
			for (int i = 0; i < 4; i++) b[i] = si();
			int hit = 0,blow = 0;
			for (int i = 0; i < 4; i++) {
				if(a[i]==b[i]) hit++;
				else{
					for (int j = 0; j < 4; j++) {
						if(a[j] == b[i]){
							blow++;
							break;
						}
					}
				}
			}
			out.println(hit+" "+blow);
		}
		out.flush();
	}

	static int si() {
		return Integer.parseInt(scan.next());
	}
}

Measured memory_bytes: 17188
# ABC053
S = input()
a_index = 10**10
z_index = -1
for i in range(len(S)):
    if S[i] == "A":
        a_index = min(a_index, i)
    if S[i] == "Z":
        z_index = max(z_index, i)
print(z_index-a_index+1)

Measured memory_bytes: 9124
import java.util.*;
public class Main{

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		
		int w=sc.nextInt();
		
		int h=sc.nextInt();
		
		int n=sc.nextInt();
		
		int l=0,r=w;
		int t=h,d=0;
		
		for(int i=0;i<n;i++){
			int x=sc.nextInt();
			int y=sc.nextInt();
			int a=sc.nextInt();
			//System.out.println(a);
			if(a==1){
				l=Math.max(x, l);
			}else if(a==2){
				r=Math.min(x, r);
			}else if(a==3){
				d=Math.max(d, y);
			}else if(a==4){
				t=Math.min(t, y);
			}
		}
		int a=r-l;
		int b=t-d;
		
		
		int sum=a*b;
		if(a<0||b<0||sum<0){
			System.out.println("0");
		}else{
			System.out.println(sum);
		}
				
		
	}

}


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

namespace AtTest.ABC_136
{
    class C
    {
        static void Main(string[] args)
        {
            var sw = new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false };
            SetOut(sw);

            Method(args);

            Out.Flush();
        }

        static void Method(string[] args)
        {
            int n = ReadInt();
            int[] array = ReadInts();
            array[0]--;
            for(int i = 1; i < n; i++)
            {
                if (array[i - 1] > array[i])
                {
                    WriteLine("No");
                    return;
                }
                if (array[i - 1] < array[i]) array[i]--;
            }
            WriteLine("Yes");
        }

        private static string Read() { return ReadLine(); }
        private static char[] ReadChars() { return Array.ConvertAll(Read().Split(), a => a[0]); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}


Measured memory_bytes: 26716
/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL unsigned long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
using namespace std;

const int N = 100005;
 
LL a,b,n,ans;

LL solve(LL a,LL b)
{
	if(a>b)
		swap(a,b);
	LL res=a;
	b-=a;
	a=1;
	res+=(b-a)/2;
	return res;
}

int main()
{
	cin>>n>>a>>b;
	if((b-a)%2==0)
	{
		ans=(b-a)/2;
		cout<<ans<<endl;
		return 0;
	}
	ans=solve(a,b);
	a=n-a+1;
	b=n-b+1;
	swap(a,b);
	ans=min(ans,solve(a,b));
	cout<<ans<<endl;
    return 0;
}
 
/*


100 20 81
 
 
 
*/

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

const int maxn = 100010;
int n, m, v, p, a[maxn];
long long s[maxn];

int main() {
    scanf("%d %d %d %d", &n, &m, &v, &p);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
    }
    sort(a + 1, a + n + 1, greater<int>());
    for (int i = 1; i <= n; i++) {
        s[i] = s[i - 1] + a[i];
    }
    int ans = 0, lst = 1;
    for (int i = 1; i <= n; i++) {
        if (a[i] == a[i - 1]) {
            ans += lst;
        } else {
            if (i <= p) {
                lst = 1, ans++;
            } else {
                if (a[p] > a[i] + m) {
                    lst = 0;
                } else {
                    long long sum = 1LL * (a[i] + m) * (i - p) - (s[i - 1] - s[p - 1]);
                    long long r = 1LL * max(0, v - (p - 1) - (n - i + 1)) * m;
                    if (r <= sum) lst = 1, ans++;
                    else lst = 0;
                }
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}

Measured memory_bytes: 1536
#pragma GCC optimize("Ofast")
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // setprecision
#include <complex> // complex
#include <math.h> 
#include <functional>
using namespace std;
using ll = long long;
using P = pair<int,int>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const int dx[8] = {1, 0, -1, 0,1,1,-1,-1};
const int dy[8] = {0, 1, 0, -1,1,-1,1,-1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// --------------------------------------------------------------------------

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<ll> A(N);
  for(int i=0; i<N; i++) cin >> A[i];
  ll X = 0;
  for(int i=2; i<N; i++){
    X = (X^A[i]);
  }
  ll S = A[0] + A[1];
  ll D = (S-X)/2;
  if((D & X) != 0 || D < 0 || A[0] < D || S%2 != X%2){
    cout << -1 << "\n";
    return 0;
  }
  ll ans = D;
  for(int i=60; i>=0; i--){
    if(X & (1LL<<i)){
      if(ans+(1LL<<i) <= A[0]){
        ans += (1LL<<i);
      }
    }
  }
  // ansは移動後のA[0]
  if(ans == 0){
    cout << -1 << "\n";
  }else{
    cout << A[0] - ans << "\n";
  }
  return 0;
}

Measured memory_bytes: 3664
N = gets.to_i
h = Hash.new(0)
N.times{
  s = gets.chomp
  h[s] = h[s] + 1
}
tmp = h.sort_by{|_,v| v}.reverse
_,max_num = tmp[0]
ans = []
N.times{|x|
  a,b = tmp[x]
  ans.push(a) if b == max_num
}
ans.sort!
ans.each{|x| puts x}


Measured memory_bytes: 40460
// AOJ 2012

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int main(void)
{
	vector <int> data;
	for (int i = 1; i*i <= 1000000; i++){
		data.push_back(i*i);
	}

	int e;
	while (cin >> e, e){
		int ret = 1001001001;
		for (int i = 0; 0 <= e - i*i*i; i++){
			int y = sqrt(e - i*i*i);
			int x = e - i*i*i - y*y;
			ret = min(ret, x + y + i);
		}
		cout << ret << endl;
	}

	return 0;
}

Measured memory_bytes: 1220
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int INF = 1e9;
int MOD = 1e9+7;
main(){
    int N,A[100000],cnt = 0;
    cin >> N;
    for(int i = 0;i < N;i++)cin >> A[i];
    for(int i = 0;i < N;i++){
        if(A[i] && A[A[i]-1]-1 == i){
            cnt++;
            A[A[i]-1] = 0;
            A[i] = 0;
        }
    }
    cout << cnt << endl;
}

Measured memory_bytes: 640
import Control.Monad
import Control.Applicative
import Data.List
import Data.List.Split

main = do
  xs <- input (map read . splitOn ","):: IO [[Double]]
  mapM_ print $ solve xs

solve :: [[Double]] -> [Int]
solve = map railway

eps :: Double
eps = 0.0000000001

railway :: [Double] -> Int
railway xs = f 0 mls
  where
    ls = take 10 xs
    [v1,v2] = drop 10 xs
    t = sum ls / (v1 + v2)
    cp = v1 * t
    mls = scanl (+) 0 ls
    f n (y:ys) | cp - y > eps = f (n+1) ys
               | otherwise = n

input :: (String -> a) -> IO [a]
input f = map f . lines <$> getContents 

Measured memory_bytes: 4236