akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <climits>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>

using namespace std;
using pii=pair<int,int>;
using ll=long long;

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

    int n,d;
    cin>>n>>d;
    int answer=n/(2*d+1);
    if(n%(2*d+1)!=0) answer+=1;
    cout<<answer;

    return 0;
}


Measured memory_bytes: 256
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include<bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;

constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

class BMultiplication2 {
public:
    void solve(istream& cin, ostream& cout) {
      SPEED;
      int n; cin >> n;
      vector<ll> a(n);
      rep(i, n) {
        cin >> a[i];
      }
      ll ret = 1;
      rep(i, n) {
        if (a[i] == 0) {
          cout << 0 << endl;
          return;
        }
      }

      rep(i, n) {
        if (ret > (1e+18)/a[i]) {
          cout << -1 << endl;
          return;
        }
        ret *= a[i];
      }
      if (ret > ll(1e+18)) {
        cout << -1 << endl;
        return;
      }

      cout << ret << endl;
    }
};

signed main() {
  BMultiplication2 solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}


Measured memory_bytes: 4028
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please


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


	int A, B;
	cin >> A >> B;

	if ((A + B) % 2) co("IMPOSSIBLE");
	else co((A + B) / 2);


	Would you please return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db; 
typedef string str; 
typedef pair<ll,ll> pl; 
typedef vector<ll> vl; 
typedef vector<vl> vvl; 
#define mp make_pair
#define se second
#define pb push_back
#define pf push_front
#define nn <<"\n"
#define F0R(i,a) for(ll i=0;i<a;i++)
#define FOR(i,a,b) for(ll i=a;i<b;i++)
int INF=0x3f3f3f3f;
ll MOD=1000000007;

ll binpow(ll a,ll b, ll c=1e18){
	ll res=1;
	while(b>0){
		if(b & 1){
			res*=a;
			res%=c;
		}
		a*=a;
		a%=c;
		b>>=1;
	}
	return res;
}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	//ifstream cin("name.in");
	//ofstream cout("name.out");
	ll t;
	cin>>t;
	ll ac=0,tle=0,wa=0,re=0;
	while(t--){
		string a;
		cin>>a;
		if(a=="AC") ac++;
		if(a=="TLE") tle++;
		if(a=="WA") wa++;
		if(a=="RE") re++;
	}
	cout<<"AC x "<<ac nn "WA x "<<wa nn<<"TLE x "<<tle nn <<"RE x "<<re nn;
}


Measured memory_bytes: 3652
#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
#include <algorithm> 
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
#define ll long long
#define rep(i,n) for (ll i = 0; i < (n); i++)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define FORR(i,a,b)for(ll i=(a);i<=(b);i++)
#define repR(i,n) for(ll i=n;i>=0;i--)
#define all(v)(v).begin(),(v).end()
#define rall(v)(v).rbegin(),(v).rend()
#define F first
#define S second
#define pb push_back
#define pu push
#define COUT(x) cout<<(x)<<"\n"
#define PQ priority_queue<ll>
#define PQR priority_queue<ll,vector<ll>,greater<ll>>
#define YES(n) cout << ((n) ? "YES\n" : "NO\n"  )
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n"  )
#define mp make_pair
#define sz(x) (ll)(x).size()
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
using vll = vector<ll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvll = vector<vll>;
using vstr = vector<string>;
using vc = vector<char>;
using vvc = vector<vc>;
template<class T> inline bool chmax(T& a, T b) { 
 if (a < b) { a = b; return true; } return false; 
}
template<class T> inline bool chmin(T& a, T b) {
 if (a > b) { a = b; return true; } return false; 
}
ll dx[4]={0,1,0,-1};
ll dy[4]={1,0,-1,0};
int main(){
  ll n,d;
  cin>>n>>d;
  ll ans=0;
  rep(i,n){
    ll x,y;
    cin>>x>>y;
    if(sqrt(x*x+y*y)<=d) ans++;
  }
  COUT(ans);
}

Measured memory_bytes: 3648
n, m = (int(x) for x in input().split())


A = [[int(x) for x in input().split()] for l in range(n)]
B = [int(input()) for i in range(m)]

c = [0]*n
for j in range(n):
    for i in range(m):
        c[j] += A[j][i]*B[i]

for k in c:
    print(k)



Measured memory_bytes: 6020
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
char s[N],t[N];
int main(){
	int ans=0;scanf("%s%s",s,t);
	for(int i=0;s[i];i++)ans+=s[i]!=t[i];
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 4208
#include <iostream>
#include <math.h>
#include <algorithm>
#include <string>
#include <map>
#include <vector>
#include <cmath>

using namespace std;

void a_haiku(void) {
	string s;
	cin >> s;
	s[5] = ' ';
	s[13] = ' ';
	cout << s << endl;
}

int main()
{
    a_haiku();
    return 0;
}

Measured memory_bytes: 3552
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>

using namespace std;


int main() {

	int a, b,c,d;
	cin >> a >> b >> c >> d;
	if (abs(c - a) <= d || (abs(a - b) <= d && abs(b - c) <= d)) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}
	

	return 0;
}

Measured memory_bytes: 256
n, k = map(int,input().split())
s = input()

t = 1
#連続した0と1の人をまとめる
l = [0]

for i in s:
    i = int(i)
    if i == t: 
        l[-1] += 1
    else: 
        l += [1]
        t = i

if t < 1: 
    l += [0]
# print(l)

#累積和をとる
s = [0]

for i in l:
    s += [s[-1]+i]

# print(s)

a = 0

for i in range(0,len(l),2):
    # print("i", i)
    # print("s[i+1]", s[i+1])
    # print("s[i-k*2]", s[i-k*2])
    if i<2*k:
        a = max(a, s[i+1])
    else:
        a = max(a, s[i+1]-s[i-k*2])
print(a) 


Measured memory_bytes: 7832
#include<bits/stdc++.h>
#define fo(i,a,b) for(int i=(a);i<=(b);++i)
#define rv(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
const int M = 120000;
typedef long long ll;
int n,m,k;
char s[20];
bool ok(){
    int a='A'+'A'+'A';
    int b='B'+'B'+'B';
    int c=s[0]+s[1]+s[2];
    if(c==a||c==b) return 0;
    return 1;
}
int main(){
    cin>>s;
    if(ok()) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define ALL(a)  (a).begin(),(a).end()
#define SUM(a)  accumulate((a).begin(),(a).end(),0)
#define SORT(a) sort(ALL(a))
#define REV(a) reverse(ALL(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
typedef long long ll;
const int INTINF = 1<<29;
const ll LLINF = 1LL<<61;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
const int DX[8]={ 0, 1, 0,-1, 1, 1,-1,-1};
const int DY[8]={ 1, 0,-1, 0, 1,-1, 1,-1};


int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n[11];
  int a;
  REP(i,3){
    cin >> a;
    n[a]++;
  }
  if (n[5]==2 && n[7]==1){
    cout << "YES" << endl;
  }else{
    cout << "NO" << endl;
  }
}


Measured memory_bytes: 256
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String {
            bytes
                .by_ref()
                .map(|r| r.unwrap() as char)
                .skip_while(|c| c.is_whitespace())
                .take_while(|c| !c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_imports)]
use std::cmp::{min, max};

fn main() {
    input!{
        a: usize,
        b: usize,
        c: usize,
        d: usize
    }
    let s = max(a, c);
    let t = min(b, d);
    if t < s {
        println!("0");
    } else {
        println!("{}", t - s);
    }
}


Measured memory_bytes: 4352
// Hovalhagh
// Ya Aziz & Hakim
// AliReza Kaboli

#include <bits/stdc++.h>

using namespace std;

int main() {
	int a, b, c;
	cin >> a >> b >> c;
	if (a + b + c >= 22)
		cout << "bust\n";
	else
		cout << "win\n";
}


Measured memory_bytes: 256
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#define Rep(i,n) for(int i=0;i<(int)(n);i++)
#define For(i,n1,n2) for(int i=n1;i<(int)(n2);i++)
using namespace std;

signed main()
{
  int a;
  cin >> a;
  string S;
  cin >> S;

  if(a < 3200) S = "red";

  cout << S;
  return 0;
}


Measured memory_bytes: 256
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

n = ri()
ls = ril()

prev = 0
up = True
res = 0
for x, y in zip(ls, ls[1:] + [0]):
    if up and x > y:
        res += x - prev
        up = False
    elif not up and x < y:
        prev = x
        up = True
print(res)

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

#define pb push_back
#define sz size()
#define rep(i,a) for(int i=0;i<(int)(a);i++)

using namespace std;
typedef long double D;
typedef complex<D> P;
const D EPS = 1e-10;
const D PI = acos(-1);

inline D dot(P x,P y){return real(conj(x)*y);}
inline D cross(P x,P y){return imag(conj(x)*y);}
P rotate(P v,double s){
  return P(real(v)*cos(s) - imag(v)*sin(s), real(v)*sin(s) + imag(v)*cos(s) );
}
inline int ccw(P a,P b,P c){
  b -= a;c -= a;
  if (cross(b,c)>EPS) return 1;   //counter clockwise
  if (cross(b,c)<-EPS) return -1; //clockwise
  if (dot(b, c)<-EPS) return 2;   //c--a--b on line
  if (abs(b)<abs(c)) return -2;   //a--b--c on line
  return 0;                       //on segment
}

int h,r,n[3];
D d[3],tmp1,tmp2,seta;
P hp[110],up[3][110];
D w[110],a[110];

int main(){
  while(cin >> h >> r,h){
    rep(i,h){
      cin >> tmp1 >> tmp2;
      hp[i] = P(tmp1,tmp2);
    }

    rep(i,3)cin >> n[i]; rep(i,3)cin >> d[i], d[i] *= PI/180/2;

    rep(i,3)rep(j,n[i]){
      cin >> tmp1 >> tmp2;
      up[i][j] = P(tmp1,tmp2);
    }

    rep(i,r)cin >> w[i] >> a[i], w[i] *= PI/180;

    int num[110];
    memset(num,0,sizeof(num));

    rep(x,r)rep(id,h){
      if( !(abs(hp[id]) < a[x] + EPS) )continue;
      P o = P(0,0), lp = rotate(P(1,0),w[x]+d[0]), rp = rotate(P(1,0),w[x]-d[0]);
      if(ccw(o,lp,hp[id]) != -1 || ccw(o,rp,hp[id]) != 1)continue;

      bool in = 0;
      rep(i,3){
	rep(j,n[i]){
	  if(abs(hp[id]-up[i][j]) < a[x] +EPS){
	    P o = up[i][j], lp = o + rotate(P(1,0),w[x]+d[i]), rp = o + rotate(P(1,0),w[x]-d[i]);
	    if(ccw(o,lp,hp[id]) == -1 && ccw(o,rp,hp[id]) == 1){
	      in = 1; break;
	    }
	  }
	}
	if(in)break;
      }
      if(!in)num[id]++;
    }

    int maxv = *max_element(num,num+h);
    if(maxv==0)cout << "NA\n";
    else{
      vector<int> ans;
      rep(i,h){
	if(num[i] == maxv)ans.pb(i+1);
      }
      rep(i,ans.sz)cout << ans[i] << (i+1==ans.size()?"\n":" ");
    }
  }
}

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

using namespace std;
#define int long long
#define N 666666
int a[N];
vector<pair<int,int> >v;
signed main(){
	int n;
	cin>>n;
	int maxn=0,id=0;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		if(abs(a[i])>=maxn){
			maxn=abs(a[i]);
			id=i;
		}
	}
	for(int i=1;i<=n;i++){
		 v.push_back(make_pair(id,i));
	}
	if(a[id]>0){
		for(int i=2;i<=n;i++){
		 v.push_back(make_pair(i-1,i));
		}
	}else{
		for(int i=n;i>1;i--) v.push_back(make_pair(i,i-1));
	}
	cout<<v.size()<<'\n';
	for(int i=0;i<v.size();i++) cout<<v[i].first<<" "<<v[i].second<<'\n';
	
	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define F first
#define S second
#define pb push_back
#define mp make_pair

const int MOD = 1000000007;
const int N = 1e5 + 18;



int32_t main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#endif
    int t = 1;
    //cin >> t;
    while (t--) {


        string s, t;
        cin >> s >> t;
        int sum = 0;
        for (int i = 0; i < min(s.size(), t.size()); i++) {
            if (s[i] != t[i]) {
                sum++;
            }
        }
        cout << sum << endl;
    }
    return 0;

}



Measured memory_bytes: 3796
#include <bits/stdc++.h>
const int MAX = 1e5 + 10;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
typedef long long ll;
using namespace std;
#define mul(a, b) (((ll)(a) * (b) ) % MOD)
#define sum(a, b) (((ll)(a) + (b) ) % MOD)

int powmod(int a, int n){
	int s = 1;
	while(n> 0){
		if(n & 1)s = mul(a, s);
		a = mul(a, a);
		n >>= 1;
	}
	return s;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	#define endl '\n'

	#ifdef LOCAL_DEBUG
		freopen("data.in", "r", stdin );
	#endif


	int n; cin >> n;
	vector<int> fac(n + 1);
	vector<int> ifac(n + 1);
	fac[0] = 1;
	for(int i = 1; i <= n; i++)fac[i] = mul(fac[i - 1], i);
	ifac[n] = powmod(fac[n],  MOD - 2);
	for(int i = n - 1; i >= 0; i--)ifac[i ] = mul(i + 1, ifac[i + 1]);


	function<int(int, int)> comb = [&](int a, int b){
		if(a < b)return 0;
		return (int)mul(fac[a], mul(ifac[b], ifac[a - b]));
	};
	vector<int> tot(n);
	for(int k = 0; k < n; k++)
		tot[k] = mul(comb(k - 1, n - 1 - k), fac[k]);
	vector<int> p(n);
	for(int k = 1; k < n; k++)
		p[k] = (tot[k] - mul(n - 1 - (k - 1), tot[k - 1]) + MOD) % MOD;
	int ans = 0;
	for(int k = 0; k < n; k++)
		ans = sum(ans, mul(k, mul(p[k], fac[n - 1 - k])));
	cout << ans << endl;

}


Measured memory_bytes: 15872
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int[] L = new int[N];
        for(int i=0; i<N; i++) {
            L[i] = Integer.parseInt(sc.next());
        }
        Arrays.sort(L);
        int count = 0;
        for(int i=N-1; i>=2; i--) {
            for(int j=i-1; j>=0; j--) {
                for(int k=j-1; k>=0; k--) {
                    if(L[i] < L[j] + L[k]) count++;
                    else break;
                }
            }
        }
        System.out.println(count);
    }
}

Measured memory_bytes: 25644
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long[] a = new long[n];
        long[] ai = new long[n];
        Map<Long , Integer> aj = new HashMap<>();
        for (int i=0;i<n;i++) {
            a[i] = sc.nextLong();
            ai[i] = a[i] + i;
            if (aj.containsKey(i - a[i])) {
                aj.put((i - a[i]), aj.get(i - a[i]) + 1);
            } else {
                aj.put((i - a[i]), 1);
            }
        }
        long ans=0;
        for (int i=0;i<n;i++) {
            if (aj.containsKey(ai[i])) {
                ans+=aj.get(ai[i]);
            }
        }
        System.out.println(ans);
    }
}


Measured memory_bytes: 72732
/*やったぜ。　投稿者：変態糞土方 (8月16日（水）07時14分22秒)

昨日の8月15日にいつもの浮浪者のおっさん（60歳）と先日メールくれた汚れ好きの土方のにいちゃん

（45歳）とわし（53歳）の3人で県北にある川の土手の下で盛りあったぜ。

今日は明日が休みなんでコンビニで酒とつまみを買ってから滅多に人が来ない所なんで、

そこでしこたま酒を飲んでからやりはじめたんや。

3人でちんぽ舐めあいながら地下足袋だけになり持って来たいちぢく浣腸を3本ずつ入れあった。

しばらくしたら、けつの穴がひくひくして来るし、糞が出口を求めて腹の中でぐるぐるしている。

浮浪者のおっさんにけつの穴をなめさせながら、兄ちゃんのけつの穴を舐めてたら、

先に兄ちゃんがわしの口に糞をドバーっと出して来た。

それと同時におっさんもわしも糞を出したんや。もう顔中、糞まみれや、

3人で出した糞を手で掬いながらお互いの体にぬりあったり、

糞まみれのちんぽを舐めあって小便で浣腸したりした。ああ～～たまらねえぜ。

しばらくやりまくってから又浣腸をしあうともう気が狂う程気持ちええんじゃ。

浮浪者のおっさんのけつの穴にわしのちんぽを突うずるっ込んでやると

けつの穴が糞と小便でずるずるして気持ちが良い。

にいちゃんもおっさんの口にちんぽ突っ込んで腰をつかって居る。

糞まみれのおっさんのちんぽを掻きながら、思い切り射精したんや。

それからは、もうめちゃくちゃにおっさんと兄ちゃんの糞ちんぽを舐めあい、

糞を塗りあい、二回も男汁を出した。もう一度やりたいぜ。

やはり大勢で糞まみれになると最高やで。こんな、変態親父と糞あそびしないか。

ああ～～早く糞まみれになろうぜ。

岡山の県北であえる奴なら最高や。わしは163*90*53,おっさんは165*75*60、や

糞まみれでやりたいやつ、至急、メールくれや。

土方姿のまま浣腸して、糞だらけでやろうや。*/
#include "bits/stdc++.h"
#include <numeric>
#define rep(i,n) for(int i = 0; i < n; i++)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define llMAX numeric_limits<long long>::max()
#define intMAX numeric_limits<int>::max()
#define llMIN numeric_limits<long long>::min()
#define intMIN numeric_limits<int>::min()
#define d_5 100000
#define d9_7 1000000007
#define vll vector<vector<long long>>
#define vl vector<long long>
#define vi vector<int>
#define vii vector<vector<int>>
#define pb push_back
#define pf push_front
#define ld long double

ll gcd(ll a, ll b)
{
    if (a < b) {
        a ^= b;
        b ^= a;
        a ^= b;
    }
    
    return b ? gcd(b, a % b) : a;
}

ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}
int main(void){
  ll n;
  cin>>n;
  vl a(n),b(n);
  rep(i,n){
    cin>>a[i];
  }
  rep(i,n){
    cin>>b[i];
  }
  vl num(n);
  rep(i,n){
    num[i]=i+1;
  }
  int cnt=0;
  int anum=0;
  int bnum=0;
  do {
        if(num==a){
          anum=cnt;
        }
        if(num==b){
          bnum=cnt;
        }
        cnt++;
    } while( next_permutation(num.begin(), num.end()) );   
    cout<<(int)abs(anum-bnum)<<endl;
  /*rep(i,n){
    cin>>a[i];
    a[i]/=2;
  }
  ll nowlcm=1;
  rep(i,n){
    nowlcm=lcm(a[i],nowlcm);
  }
  ll num1=(m/nowlcm)/2;
  if((m/nowlcm)%2==1)num1++;
  cout<<num1<<endl;
  //cout<<ans<<endl;*/
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main(void)
{
	int a,b,c=0;
	cin >> a;
	while(a!=0){
	b=1000-a;
	c+=b/500;
	b=b%500;
	c+=b/100;
	b=b%100;
	c+=b/50;
	b=b%50;
	c+=b/10;
	b=b%10;
	c+=b/5;
	c+=b%5;
	cout << c << endl;
	c=0;
	cin >> a;
	}
	
	return 0;
}

Measured memory_bytes: 3072
#include <bits/stdc++.h>
#define rep(i,x,n) for(int i = x; i < (int)(n); i++)
#define rep_ll(i,x,n) for(ll i = x; i < (ll)(n); i++)
using namespace std;
typedef long long ll;
typedef unsigned long long u_ll;
typedef pair<int, int> pair_;
const long double PI = (acos(-1));
const int INF = 1001001001;
const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, -1, 0, 1};
ll mod = 1000000007;
int main(){
    int n;
    cin >> n;
    priority_queue<int> q;
    vector<int> a(n);
    rep(i, 0, n) cin >> a[i];
    sort(a.begin(), a.end());
    int aamax=a[n-1]+1;
    vector<int> aa(aamax, 0);
    rep(i, 0, n){
        if(aa[a[i]]!=0){
            aa[a[i]]=2;
            continue;
        }
        for(int j=a[i]; j< aamax;j+=a[i]) aa[j]++;
    }
    int cnt=0;
    rep(i, 0, n) if(aa[a[i]]==1) cnt++; 

    cout << cnt;

    return 0;
}

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

#define ll long long int
#define LD long double

const int N = 100010;

int inf = 1e9;
int mod = 1e9 + 7;

signed main()
{
    //freopen("IN.txt", "r", stdin);
    //freopen("OUT.txt", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n, m; cin >> n >> m ;
    vector<vector<int>> graph(n + 1);

    for(int i = 0; i < m; i++) {
        int u, v; cin >> u >> v;
        graph[u].push_back(v);
    }

    vector<int> ans;
    int mini = inf;

    for(int i = 1; i <= n; i++) {
        vector<bool> visited(n + 1, false);
        visited[i] = false;
        bool F = true;
        queue<int> q;
        q.push(i);
        vector<int> cycle;
        vector<int> prev(n + 1);
        prev[i] = -1;
        while(!q.empty()) {
            int u = q.front();
            if(u == i && !F) {
                u = prev[u];
                while(u != i) {
                    cycle.push_back(u);
                    u = prev[u];
                }
                cycle.push_back(i);
                break;
            }
            F = false;
            for(int x : graph[u]) {
                if(!visited[x]) {
                    visited[x] = true;
                    prev[x] = u;
                    q.push(x);
                }
            }
            q.pop();
        }
        if(!cycle.empty()) {
            if(mini > (int)cycle.size()) {
                mini = (int)cycle.size();
                ans = cycle;
            }
        }
    }

    if(mini != inf) {
        cout << mini << "\n";
        reverse(ans.begin(), ans.end());
        for(int x : ans) {
            cout << x << "\n";
        }
    }
    else {
        cout << "-1\n";
    }

    return 0;
}


Measured memory_bytes: 384
import java.util.stream.*;
import java.util.*;

public class Main {
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[]$) {
        int h = scanner.nextInt();
        int w = scanner.nextInt();
        char[][] s = IntStream.range(0, h).mapToObj(i -> scanner.next().toCharArray()).toArray(char[][]::new);
        int[][] dist = new int[h][w];
        Arrays.stream(dist).forEach(array -> Arrays.fill(array, 10000));
        dist[0][0] = 0;
        Deque<Integer> deque = new ArrayDeque<>();
        deque.add(0);
        while (!deque.isEmpty()) {
            int i = deque.poll();
            int x = i % w;
            int y = i / w;

            if (y > 0 && dist[y - 1][x] > dist[y][x] + 1 && s[y - 1][x] == '.') {
                dist[y - 1][x] = dist[y][x] + 1;
                deque.addFirst(i - w);
            }
            if (y < h - 1 && dist[y + 1][x] > dist[y][x] + 1 && s[y + 1][x] == '.') {
                dist[y + 1][x] = dist[y][x] + 1;
                deque.addFirst(i + w);
            }
            if (x > 0 && dist[y][x - 1] > dist[y][x] + 1 && s[y][x - 1] == '.') {
                dist[y][x - 1] = dist[y][x] + 1;
                deque.addFirst(i - 1);
            }
            if (x < w - 1 && dist[y][x + 1] > dist[y][x] + 1 && s[y][x + 1] == '.') {
                dist[y][x + 1] = dist[y][x] + 1;
                deque.addFirst(i + 1);
            }
        }

        if (dist[h - 1][w - 1] == 10000) {
            System.out.println(-1);
        } else {
            int ans = -dist[h - 1][w - 1] - 1;
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {
                    if (s[i][j] == '.') {
                        ans++;
                    }
                }
            }
            System.out.println(ans);
        }
    }
}

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

int main()
{
	int i, j, k, n, m, x, y, maxm=-1000000000;
	cin >> n;

	int a[n][10], b[n][11], p[10];

	for(i=0; i<n; i+=1)
		for(j=0; j<10; j+=1)
			cin >> a[i][j];

	for(i=0; i<n; i+=1)
		for(j=0; j<11; j+=1)
			cin >> b[i][j];

	m=pow(2, 10);
	memset(p, 0, sizeof(p));
	for(i=1; i<m; i+=1)
	{
		x=i;
		y=0;
		while(x>0)
		{
			p[y]=x%2;
			y+=1;
			x/=2;
		}
		int temp=0;
		for(j=0; j<n; j+=1)
		{
			int count=0;
			for(k=0; k<10; k+=1)
				if((p[k]==1) && (a[j][k]==1))
					count+=1;

			temp+=b[j][count];
		}
		maxm=max(maxm, temp);
	}

	cout << maxm << endl;
}

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

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

int main(void){
    int n, k, h, ans = 0;

    cin >> n >> k;
    REP(i, n) {
        cin >> h;
        if (k <= h) {
            ans++;
        }
    }

    cout << ans << "\n";
    return 0;
}


Measured memory_bytes: 256
#ifndef _WIN32
#include<iostream>
#endif 

#include<cmath>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
typedef long long LL;
#define FOR(i,bg,ed) for(int i = (bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)
struct cww {
	cww() {
		ios::sync_with_stdio(false);
		cin.tie(0);
	}
}star;
template<typename T>
istream & operator>> (istream &is, vector<T>&v) {
	for (auto &it : v)is >> it;
	return is;
}
#define fi first
#define se second
#define ALL(x) (x).begin(),(x).end()
typedef pair<LL, LL> P;
typedef vector<LL> V;
const int INF = 1e7;


namespace _DSU {
#define SZ 500000
	int mem[2][SZ];
}
class UnionFind {
private:
	int *par, *rank;
	int find(int x) {
		if (par[x] == x)return x;
		else return par[x] = find(par[x]);
	}
public:
	UnionFind(int n, int *par, int *rank) :par(par), rank(rank) {
		for (int i = 0; i < n; i++)par[i] = i, rank[i] = 0;
	}
	UnionFind(int n) :UnionFind(n, _DSU::mem[0], _DSU::mem[1]) {}
	bool unite(int x, int y) {
		x = find(x); y = find(y);
		if (x == y)return false;
		if (rank[x] < rank[y])par[x] = y;
		else {
			par[y] = x;
			if (rank[x] == rank[y])rank[x]++;
		}
		return true;
	}
	bool same(int x, int y) {
		return find(x) == find(y);
	}
};



int main() {
	int N,M;
	while (cin >> N >> M, N + M) {
		V h(N);
		cin >> h;
		V a(M), b(M), c(M);
		vector<P> E(M);
		vector<P> I(N);
		REP(i, N)I[i] = P(h[i], i);
		REP(i, M) {
			cin >> a[i] >> b[i] >> c[i];
			E[i] = P(c[i], i);
		}
		sort(ALL(E));
		sort(ALL(I));

		int jd = N - 1;
		LL res = 0;
		{
			int id = N - 1;
			vector<int> exist(N, 0);
			UnionFind uf(N);
			while (id >= 0) {
				int ans = 0;
				while (id > 0 && I[id].first == I[id - 1].first) {
					exist[I[id--].second] = 1;
				}
				exist[I[id--].second] = 1;
				REP(i, M) {
					int id = E[i].second;
					int u = a[id] - 1;
					int v = b[id] - 1;
					if (exist[u] && exist[v] && uf.unite(u, v))
						ans += c[id];
				}
				int bb = -1;
				REP(i, N)if (exist[i])bb = i;
				if (bb == -1)continue;
				int flag = 1;
				REP(i, N)
					if (exist[i] && !uf.same(i, bb))
						flag = 0;
				if (flag == 0) jd = id;

			}
		}
		{
			int id = N - 1;
			vector<int> exist(N, 0);
			UnionFind uf(N);
			
			while (id >= 0) {
				if (id > jd) {
					exist[I[id--].second] = 1;
					continue;
				}
				int ans = 0;
				while (id > 0 && I[id].first == I[id - 1].first) {
					exist[I[id--].second] = 1;
				}
				exist[I[id--].second] = 1;
				REP(i, M) {
					int id = E[i].second;
					int u = a[id] - 1;
					int v = b[id] - 1;
					if (exist[u] && exist[v] && uf.unite(u, v))
						ans += c[id];
				}

				res += ans;
			}
		}
		cout << res << endl;

	}


	return 0;
}

Measured memory_bytes: 3788
#include <stdio.h>
#include <algorithm>

int main()
{
  unsigned long long q, h, s, d, total;

  scanf("%lld %lld %lld %lld %lld", &q, &h, &s, &d, &total);

  unsigned long long paid = 0;
  unsigned long long best = 0;
  unsigned long long array[4];
  array[0] = q * 8;
  array[1] = h * 4;
  array[2] = s * 2;
  array[3] = d;

  std::sort(array, array + 3);
  unsigned long long otherbest = array[0];
  if(total > 1)
  {
    std::sort(array, array + 4);
    best = array[0];
  }
    else
  best = otherbest;

  if(best == d && total > 1)
  {
    paid = (total / 2) * d;
    total %= 2;
    if(total == 1)
    {
    if(otherbest == q * 8)
      paid += q * 4;

      else if(otherbest == h * 4)
      paid += h * 2;

      else if(otherbest == s * 2)
      paid += s;
    }
    else
    {
      printf("%lld\n", paid);
      return 0;
    }
  }

  else if(best == q * 8)
    paid = total * (q * 4);

  else if(best == h * 4)
    paid = total * (h * 2);

  else if(best == s * 2)
    paid = total * s;

  printf("%lld\n", paid);
  return 0;
}


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

int a[128][128];
int b[128][128];
int v[128][128];

bool dfs(int y, int x, int c)
{
	if(v[y][x] == c) return true;
	if(v[y][x] != 0) return false;

	v[y][x] = c;
	return dfs(b[y][x], a[y][x], c);
}

int main()
{
	int N;
	while(cin>>N, N)
	{
		for(int y=0; y<N; y++)
		for(int x=0; x<N; x++)
		{
			v[y][x] = 0;
			cin >> a[y][x] >> b[y][x];
		}

		int col=0,cnt=0;
		for(int y=0; y<N; y++)
		for(int x=0; x<N; x++)
		{
			if(dfs(y,x,++col))
			{
				cnt++;
			}
		}

		cout << cnt << endl;
	}
}

Measured memory_bytes: 0
#include<iostream>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#include<fstream>
#include<algorithm>
#include<utility>
#include<list>
using namespace std;
using ll = long long;

int main(){
  string str;
  cin >> str;
  list<char> str_vc;

  for(int i=0;i<str.size();i++){
    str_vc.push_back(str[i]);
  }

  int q;
  int rev = 0;
  cin >> q;
  for(int i=0;i<q;i++){
    int t;
    cin >> t;
    if(t==1){
      rev++;
    }
    else{
      int f;
      char c;
      cin >> f >> c;
      if(f==1)
        if(rev%2==1)
          str_vc.push_back(c);
        else
          str_vc.push_front(c);
      else
        if(rev%2==1)
          str_vc.push_front(c);
        else
          str_vc.push_back(c);
    }
  }
  if(rev%2==1){
    str_vc.reverse();
  }
  for(auto itr = str_vc.begin(); itr!= str_vc.end(); itr++){
    cout << *itr;
  }
  return 0;
}

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

int main(){
  int n,a,b,c,d;
  cin>>n>>a>>b>>c>>d;
  string s;
  cin>>s;
  for(int i=a-1;i<max(c,d)-1;i++){
    if((s.at(i)=='#'&&s.at(i+1)=='#')){
      cout<<"No"<<endl;
      return 0;
    }
  }
  if(c<d){
    cout<<"Yes"<<endl;
    return 0;
  }

  for(int i=b-2;i<d-1;i++){
    if(s.at(i)=='.'&&s.at(i+1)=='.'&&s.at(i+2)=='.'){
      cout<<"Yes"<<endl;
      return 0;
    }
  }
  cout<<"No"<<endl;
  return 0;
}


Measured memory_bytes: 640
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

class Main
{
	public static void main(String[] args){
		java.io.BufferedReader br = new java.io.BufferedReader(new InputStreamReader(System.in));
		String line;
		StringTokenizer st;

		try{
			line = br.readLine();
			while(line != null){
				st = new StringTokenizer(line);
				int s = Integer.parseInt(st.nextToken()) + Integer.parseInt(st.nextToken());
				System.out.println((int)Math.log10(s) + 1);
				line = br.readLine();
			}
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}

Measured memory_bytes: 24036
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vll = vector<vl>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1e9 + 7;
const ll INF = 2e15;
template <class T>
void print(const T& t) {
    cout << t << endl;
}
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 gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

ll evaluate(int contest, int date, vector<ll>& last, vector<ll>& c, vll& s) {
    ll point_today = 0;
    point_today += s[date][contest];
    for (ll j = 1; j <= 26; j++) {
        if (j == contest) {
            continue;
        }
        point_today -= c[j] * (date - last[j]);
    }
    return point_today;
}

int main() {
    ll d;
    cin >> d;  //全日数
    vl c(27);
    for (ll i = 1; i <= 26; i++) {  // c_i==c[i]
        cin >> c[i];
    }
    vll s(d + 1, vl(27));  // s_d,i==s[d][i]
    for (ll i = 1; i <= d; i++) {
        for (ll j = 1; j <= 26; j++) {
            cin >> s[i][j];
        }
    }
    vl t;
    vl last(27, 0);  // last(d,i)==last[i]
    // ll satisfy = 0;
    for (ll date = 1; date <= d; date++) {
        priority_queue<pair<ll, int>> q;
        for (ll contest = 1; contest <= 26; contest++) {
            q.push(mp(evaluate(contest, date, last, c, s), contest));
           // cout<<evaluate(contest, date, last, c, s)<<" ";
        }
        //cout<<endl;
        t.pb(q.top().S);
        // satisfy += q.top().F;
        last[q.top().S] = date;
        
    }
    rep(i, d) { print(t[i]); }
}


Measured memory_bytes: 3740
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>

using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;

template<typename T1,typename T2>inline void chmin(T1 &a,const T2 &b){if(a>b) a=b;}
template<typename T1,typename T2>inline void chmax(T1 &a,const T2 &b){if(a<b) a=b;}

#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()

/* do your best */

int main() {
  
  lint n; cin >> n;
  vector<lint> a(n);
  vector<lint> cnt(n + 1, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
  }

  lint sum = 0;
  for (int i = 1; i <= n; i++) {
    if (cnt[i] > 0) sum += cnt[i] * (cnt[i] - 1) / 2;
  }

  for (int i = 0; i < n; i++) {
    int val = a[i];
    lint ans = sum;
    ans -= cnt[val] * (cnt[val] - 1) / 2;
    lint c = cnt[val] - 1;
    if (c > 0) ans += c * (c - 1) / 2;
    cout << ans << endl;
  }
  return 0;
}


Measured memory_bytes: 5760
import java.util.*;
import java.io.*;

public class Main{
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
	
		int N = Integer.parseInt(bf.readLine());
		
		int[] cards = new int[N+1];
		
		for(int i = 1 ; i <= N ; i++){
			cards[i] = Integer.parseInt(bf.readLine());
		}
		
		long sum = 0;
		int prev = 0;
		
		for(int i = 1 ; i <= N ; i++){
			if(prev == 0){
				sum += (cards[i]/2);
				if(cards[i]%2 != 0){
					prev = 1;
				}
				else prev = 0;
			}
			else{
				if(cards[i] == 0) prev = 0;
				else{
					cards[i]++;
					sum += (cards[i]/2);
					if(cards[i]%2 != 0){
						prev = 1;
					}
					else prev = 0;
				}
			}
		}
		
		System.out.println(sum);
		
	}
}


Measured memory_bytes: 18132
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def combinations_count(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))


N, M = MAP()

ans = 0

if N >= 2:
    ans += combinations_count(N, 2)
if M >= 2:
    ans += combinations_count(M, 2)

print(ans)


Measured memory_bytes: 5796
#include<iostream>

int main()
{
	int a;
	int b;
	int c;
	int d=0;

	std::cin >> a >> b >> c;

	if (a > b){
		d = a;
		a = b;
		b = d;
	}
	if (b > c){
		d = b;
		b = c;
		c = d;
	}
	if (a > b){
		d = a;
		a = b;
		b = d;
	}
	std::cout << a << ' ' << b << ' ' << c << '\n';
}


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

int main() {
  int N; cin >> N;
  int A[201];
  for ( int i = 0; i < N; i++ ) {
    cin >> A[i];
  }
  int cnt = 0;
  bool odd = false;
  while(1) {
    for ( int i = 0; i < N; i++ ) {
      if ( A[i] % 2 == 1 ) {
	odd = true;
      }
    }
    if(odd)break;
    cnt++;
    for ( int i = 0; i < N; i++ ) {
      A[i] /= 2;
    }
  }
  cout << cnt << endl;
  return 0;
}


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

int main() {
	char S[20], s[20] = "2019/04/30";

	scanf("%s", S);

	if (strcmp(S, s) > 0) {
		printf("TBD");
	}
	else {
		printf("Heisei");
	}

	return 0;
}

Measured memory_bytes: 128
#define DEB
#include<bits/stdc++.h>
#define REP(i,m) for(int i=0;i<(m);++i)
#define REPN(i,m,in) for(int i=(in);i<(m);++i)
#define ALL(t) (t).begin(),(t).end()
#define CLR(a) memset((a),0,sizeof(a))
#define pb push_back
#define mp make_pair
#define fr first
#define sc second

using namespace std;


#ifdef DEB
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
#define dumpR(x) cerr<<"\x1b[31m"<<#x<<" = " <<(x)<<"\x1b[39m"<<endl
#define dumpY(x) cerr<<"\x1b[33m"<<#x<<" = " <<(x)<<"\x1b[39m"<<endl
#define dumpG(x) cerr<<"\x1b[32m"<<#x<<" = " <<(x)<<"\x1b[39m"<<endl
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
#else
#define dump(x) ;
#define dumpR(x) ;
#define dumpY(x) ;
#define dumpG(x) ;
#define prl ;
template<class T> void debug(T a,T b){ ;}
#endif

template<class T> void chmin(T& a,const T& b) { if(a>b) a=b; }
template<class T> void chmax(T& a,const T& b) { if(a<b) a=b; }

typedef long long int lint;
typedef pair<int,int> pi;

namespace std{
  template<class S,class T>
  ostream &operator <<(ostream& out,const pair<S,T>& a){
    out<<'('<<a.fr<<','<<a.sc<<')';
    return out;
  }
}

const lint INF=1e18;
int n;
lint x;
lint ar[4005];

lint mn[4005][4005];
int main(){
  cin>>n>>x;
  REP(i,n) cin>>ar[i],ar[i+n]=ar[i];
  REP(i,n*2){
    mn[i][i]=INF;
    REPN(j,n*2,i){
      mn[i][j+1]=min(mn[i][j],ar[j-1]);
    }
  }

  lint res=INF;
  REP(i,n){
    lint tmp=0;
    REP(j,n){
      tmp+=mn[j+n-i][j+n+1];
    }
    tmp+=x*i;
    chmin(res,tmp);
  }
  cout<<res<<endl;

  return 0;
}





Measured memory_bytes: 77824
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<climits>
#include<iostream>
#include<sstream>
#include<utility>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<set>
#include<stack>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;

int main()
{
	int N,X,m=100000;
	cin>>N>>X;
	for(int i=0;i<N;i++)
	{
		int p;
		cin>>p;
		X-=p;
		m=min(m,p);
	}

	cout<<N+(X/m)<<endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#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 each(a,x) for(auto a : (x))
#define all(a) (a).begin(),(a).end()
#define chmin(a,b) ((a) = min((a),(b)))
#define chmax(a,b) ((a) = max((a),(b)))
#define in_range(x,l,r) ((l)<=(x) && (x)<(r))
#define printvec(a) rep(i,a) cout << a[i] << " \n"[i+1==(a).size()];
#define fs first
#define sc second
#define em emplace
#define eb emplace_back
#define sz size()
#define MP make_pair
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;

const ll INF = 1e8;
const D EPS = 1e-8;
const ll MOD = 1e9+7;

int main(){
  int n;
  cin >> n;
  string s;
  cin >> s;

  vl a(26, 0);
  for (char c : s) {
    a[c - 'a']++;
  }

  ll ans = 1;
  for (ll x : a) {
    ans *= x + 1;
    ans %= MOD;
  }
     
  cout << ans - 1 << endl;		
}


Measured memory_bytes: 512
#include <iostream>

#define inRange(x, y, w, h) (0 <= x && x < w && 0 <= y && y < h)

using namespace std;

int w, h, n;
int dx[] = { 0, 0, -1, 1 };
int dy[] = { -1, 1, 0, 0 };

int ddx[] = { -1, 1 };
int ddy[] = {  0, 0};

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

void print(int map[30][30])
{
	for(int y=0; y < h; ++y, cout << endl)
		for(int x=0; x < w; ++x)
			cout << (char)map[y][x];
	cout << endl;
}

void drop( int map[30][30] )
{
	for(int y=h-1; y > -1; --y)
		for(int x=w-1; x > -1; --x) {
			if(map[y][x] != '.') {
				for(int my=h-1; my != y; --my) {
					if(map[my][x] == '.') {
						map[my][x] = map[y][x];
						map[y][x] = '.';
						break;
					}
				}
			}
		}
}

bool clear( int x, int y, int drct, int map[30][30], int cl[30][30] )
{
	int sx = x, sy = y;
	int cnt = 1;
	int mx = x + dx[drct];
	int my = y + dy[drct];
	int type = map[y][x];
	while(true) {
		if(!inRange(mx, my, w, h)) break;
		if(map[y][x] != map[my][mx]) break;
		x = mx; y = my;
		mx = x + dx[drct];
		my = y + dy[drct];
		++cnt;
	}
	if(cnt >= n) {
		for(int i=0; i < cnt; i++) {
			cl[sy][sx] = -1;
			sx += dx[drct];
			sy += dy[drct];
		}
		return true;
	}
	return false;
}

bool OK(int map[30][30]) {
	for(int y=0; y < h; ++y)
		for(int x=0; x < w; ++x)
			if(map[y][x] != '.') return false;
	return true;
}

bool search( int map[30][30] )
{
	bool droped = true;
	int cl[30][30] = { 0 };
	while(droped) {
		for(int y=0; y < h; ++y)
			for(int x=0; x < w; ++x)
				cl[y][x] = 0;
		droped = false;
		bool hit = false;
		for(int y=0; y < h; ++y) {
			for(int x=0; x < w; ++x) {
				if(map[y][x] != '.') {
					int type = map[y][x];
					for(int k=0; k < 4; ++k) {
						if(clear(x, y, k, map, cl)) {
							hit = true;
						}
					}
				}
			}
		}
		if(hit) {
			for(int y=0; y < h; ++y)
				for(int x=0; x < w; ++x) {
					if(cl[y][x] == -1)
						map[y][x] = '.';
				}
			drop(map);
			droped = true;
		}
	}
	return OK(map);
}

void copy_array(int map[30][30], int map2[30][30])
{
	for(int y=0; y < h; ++y)
		for(int x=0; x < w; ++x)
			map[y][x] = map2[y][x];
}

int main( void )
{
	int map[30][30];
	int save[30][30];
	
	while(cin >> h >> w >> n)
	{
		for(int y=0; y < h; ++y)
			for(int x=0; x < w; ++x) {
				char c;
				cin >> c;
				map[y][x] = save[y][x] = (int)c;
			}

		bool ans = false, next = true;
		for(int y=0; y < h && next; ++y) {
			for(int x=0; x < w && next; ++x) {
				int type = map[y][x];
				for(int k=0; k < 2 && next; ++k) {
					int mx = x + ddx[k];
					int my = y + ddy[k];
					if(inRange(mx, my, w, h) && type != map[my][mx]) {
						::swap(&map[y][x], &map[my][mx]);
						drop(map);
						if(search(map)) {
							ans = true;
							next = false;
						}
						copy_array(map, save);
					}
				}
			}
		}
		if(ans)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}

Measured memory_bytes: 840
#include<stdio.h>
int main()
{
	int n,a[10],b,i,j;
	while(1){
		scanf("%d",&n);
		if(n==0){
			break;
		}
		for(i=0;i<10;i++){
			a[i]=0;
		}
		for(i=0;i<n;i++){
			scanf("%d",&b);
			a[b]++;
		}
		for(i=0;i<10;i++){
			if(a[i]==0){
				printf("-");
			}
			else{
				for(j=0;j<a[i];j++){
					printf("*");
				}
			}
			printf("\n");
		}
	}
	return 0;
}

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

int A, B, C;
int main(){
  cin>>A>>B>>C;
  if(C-A+B>0) cout<<C-A+B<<endl;
  else cout<<0<<endl;
}


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

using namespace std;

struct edge{
  int to,cap,rev;
};

vector<edge> G[501];
bool used[501];

void add_edge(int from,int to){
  G[from].push_back({to,1,(int)G[to].size()});
  G[to].push_back({from,1,(int)G[from].size()-1});
}

int dfs(int v,int t,int f){
  //  cout<<'v'<<v<<endl;
  if(v==t)return f;
  used[v]=true;
  for(int i=0;i<G[v].size();i++){
    edge &e=G[v][i];
    if(!used[e.to]&&e.cap>0){
      int d=dfs(e.to,t,min(f,e.cap));
      if(d>0){
	e.cap-=d;
	G[e.to][e.rev].cap+=d;
	return d;
      }
    }
  }
  return 0;
}

int max_flow(int s,int t,int fl=1<<29){
  if(s==t)return 0;
  int flow=0;
  while(fl){
    fill(begin(used),end(used),false);
    int f=dfs(s,t,fl);
    if(f==0)break;
    fl-=f;
    flow+=f;
  }
  return flow;
}

void remove_edge(int A,int B){
  int x;
  for(x=0;G[A][x].to!=B;x++);
  int r=G[A][x].rev;
  for(int i=x+1;i<G[A].size();i++){
    auto e=G[A][i];
    G[e.to][e.rev].rev--;
  }
  for(int i=r+1;i<G[B].size();i++){
    auto e=G[B][i];
    G[e.to][e.rev].rev--;
  }
  G[A].erase(begin(G[A])+x);
  G[B].erase(begin(G[B])+r);
}

int main(){
  int N,E,Q;
  cin>>N>>E>>Q;
  while(E--){
    int F,T;
    cin>>F>>T;
    add_edge(F,T);
  }
  int flow=max_flow(1,N);
  while(Q--){
    int M,A,B;
    cin>>M>>A>>B;
    if(M==1){
      add_edge(A,B);
      flow+=max_flow(1,N);
    }else{
      int x;
      for(x=0;G[A][x].to!=B;x++);
      if(G[A][x].cap==1){
	remove_edge(A,B);
      }else{
	int f=A,t=B;
	if(G[A][x].cap!=0){
	  swap(f,t);
	}
	remove_edge(A,B);
	if(!max_flow(f,t,1)){
	  max_flow(N,t,1);
	  max_flow(f,1,1);
	  flow--;
	}
      }
    }
    cout<<flow<<endl;
  }
}

Measured memory_bytes: 1896
#include <bits/stdc++.h>
using namespace std;
int n,m,x,ans = 987654321;
int c[13];
int a[13][13];
bool chk(int val)
{
	for(int i=0;i<m;i++)
	{
		int sum = 0;
		int tmp = val;
		for(int j=0;j<n;j++)
		{
			if(tmp&1)sum+=a[j][i];
			tmp>>=1;
		}
		if(sum<x)return false;
	}
	return true;
}
void f(int i, int val)
{
	if(i==n)
	{
		if(!chk(val))return;
		int tmp = 0;
		for(int i=0;i<n;i++)
		{
			if(val&1)tmp+=c[i];
			val>>=1;
		}
		ans = min(ans,tmp);
		return;
	}
	f(i+1,val|(1<<i));
	f(i+1,val);
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n>>m>>x;
	for(int i=0;i<n;i++)
	{
		cin>>c[i];
		for(int j=0;j<m;j++)
			cin>>a[i][j];
	}
	f(0,0);
	if(ans==987654321)cout<<-1;
	else				cout<<ans;
}

Measured memory_bytes: 3656
#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

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

typedef long long llint;

int main(void) {
  static char s[111111];
  scanf("%s", s);

  vector<int> freq(26, 0);
  int n = strlen(s);
  REP(i, n) freq[s[i] - 'a']++;

  if (*max_element(freq.begin(), freq.end()) > (n + 2) / 3) {
    puts("NO");
  } else {
    puts("YES");
  }
  return 0;
}


Measured memory_bytes: 384
#include<bits/stdc++.h>
#define N 100005
using namespace std;
typedef pair<int,int>P;
int n,m,s,t,low[N],used[N],depth[N];
vector<int> G[N];
set<P>ans;
 
void dfs(int x,int d,int prev){
  depth[x]=d;
  for(int i=0;i<G[x].size();i++){
    int nx=G[x][i];
    if(nx==prev)continue;
    if(!used[nx]){
      used[nx]=true;
      dfs(nx,d+1,x);
      if(!low[nx])
      ans.insert(P(min(x,nx),max(x,nx)));
      low[x]+=low[nx];
    }
    else if(d>depth[nx])low[nx]--,low[x]++;
  }
}
int main(){
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>s>>t;
    G[s].push_back(t);
    G[t].push_back(s);
  }
  used[0]=1;
  dfs(0,0,0);
  set<P>::iterator it=ans.begin();
  while(it!=ans.end()){
    cout<<(*it).first<<' '<<(*it).second<<endl;
    it++;
  }
  return 0;
}


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

int main()
{
	 int n, m, x, a, l=0, r=0;
	 cin >> n >> m >> x;
	 while(m--)
	 {
	 	cin >> a;
	 	if(a < x) l++;
	 	else r++; 
	 }
	 int min = (l < r) ? l : r;
	 cout << min << endl;
	 return 0; 
}

Measured memory_bytes: 256