akhauriyash/Code-Regression huggingface dataset:
// template version 1.6
// {{{ define basic macro
using namespace std;
#include <iostream>
#include <bits/stdc++.h>
// varibable settings
#define int long long
#define INF 1000000000000000000LL
// #define INF 2147483647
#define MOD 1000000007LL
// #define MOD 998244353LL
#define infile "../test/sample-1.in"

#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define _rrep(i,n) rrepi(i,0,n)
#define rrepi(i,a,b) for(int i=(int)(b-1);i>=(int)(a);--i)
#define rrep(...) _overload3(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define uni(x) sort(all(x));x.erase(unique(all(x)),x.end())
#define ten(n) ((int)1e##n)
template<class T, size_t N> size_t sza(const T (&array)[N]) { return N; }
template<class T> inline void chmax(T &a, const T &b) { if(a < b) a = b; }
template<class T> inline void chmin(T &a, const T &b) { if(a > b) a = b; }
template<class T=int> T in() {T x; cin>>x; return (x);}
struct Fast { Fast(){ std::cin.tie(0); ios::sync_with_stdio(false); } } fast;

// dump macro
#ifdef PCM
  #include "dump.hpp"
#else
  #define dump(...) 42
  #define dump_1d(...) 42
  #define dump_2d(...) 42
#endif

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long double ld;
typedef pair<int,int> pii;
typedef tuple<int,int,int> iii;
template<typename T> using PQ = priority_queue<T, vector<T>, greater<T>>;
int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};
#define fi first
#define se second

// }}}

int solve(){
  int n;cin>>n;
  int a[n]; rep(i, n) {cin>>a[i];}
  int b[n]; rep(i, n) {cin>>b[i];}

  int sa = accumulate(a, a+n, 0LL);
  int sb = accumulate(b, b+n, 0LL);
  dump(sa,sb);

  if (sa < sb) {
    cout << -1 << endl;
    return 0;
  }

  vector<int> x;
  int res = 0;
  int dif = 0;
  rep(i, n) {
    if (a[i] < b[i]) {
      dif += b[i]-a[i];
      res++;
    }
    else{
      x.pb(a[i]-b[i]);
    }
  }
  dump(res, dif);
  sort(all(x), greater<int>());
  dump(x);

  rep(i, sz(x)){
    if (dif <= 0) break;
    res++;
    dif -= x[i];
  }
  cout << res << endl;

  return 0;
}

signed main() { //{{{
#ifdef INPUT_FROM_FILE
  std::ifstream in(infile);
  std::cin.rdbuf(in.rdbuf());
#endif
  solve();
  return 0;
} //}}}


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

using namespace std;

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

	int n, k;
	cin >> n >> k;

	unordered_map<int, int> m;

	for(int i=1; i<=k; i++){
        int num;
        cin >> num;
        for(int j=1; j<=num; j++){
            int snuke;
            cin >> snuke;
            m[snuke]++;
        }
	}

	int ans=0;

	for(int i=1; i<=n; i++)
        if(m.find(i)==m.end())
            ans++;

	cout << ans << endl;

	return 0;
}


Measured memory_bytes: 3664
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <functional>
#include <iomanip>
#include <bitset>
using namespace std;

typedef long long ll;


ll mod = 1e9 + 7;
int N; ll K;
ll m[80][53][53];
ll p[80][53][53];
ll e[53][53];

void matmul(ll (&a)[53][53], ll (&b)[53][53], ll (&c)[53][53]) {
	for (int i = 0; i < N; i++) {
		for (int k = 0; k < N; k++) {
			for (int j = 0; j < N; j++) {
				c[i][j] += (a[i][k] * b[k][j]) % mod;
				c[i][j] %= mod;
			}
		}
	}
}

int main() {
	cin >> N >> K;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			cin >> m[0][i][j];
		}
	}
	ll T = 0;
	while ((1LL << T) <= K) T++;
	for (ll t = 1; t < T; t++) matmul(m[t - 1], m[t - 1], m[t]);
	
	for (int i = 0; i < N; i++) e[i][i] = 1;
	for (ll t = 0; t < T; t++) {
		if (!((K >> t) & 1)) {
			for (int i = 0; i < N; i++) {
				for (int j = 0; j < N; j++) {
					p[t][i][j] = (t > 0 ? p[t - 1][i][j] : e[i][j]);
				}
			}
		}
		else {
			if (t == 0) matmul(e, m[t], p[t]);
			else matmul(p[t - 1], m[t], p[t]);
		}
	}
	ll ans = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			ans += p[T - 1][i][j];
			ans %= mod;
		}
	}
	cout << ans << endl;
	return 0;
}


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

#define REP(i,num) for(int i=0;i<(num);++i)
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=int>
T in(){T x; cin >> x; return (x);}
template<typename T=int,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}

ll gcd(ll a,ll b){
	if(a<b){
		swap(a,b);
	}

	while(ll r=(a%b)){
		a = b;
		b = r;
	}
	return b;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	
	int N=in(),K=in();
	vector<ll> A=vecin<ll>(N);
	REP(i,N){
		if(A[i]==K){
			out("POSSIBLE");
			return 0;
		}
	}
	sort(ALL(A),greater<ll>());

	if(A[0]<K){
		out("IMPOSSIBLE");
		return 0;
	}

	ll G=A[0];
	REP(i,N-1){
		G = gcd(G,A[i+1]);
	}
	out(!(K%G)?"POSSIBLE":"IMPOSSIBLE");
	return 0;
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
#ifdef dbg
#include "./printer.hpp"
#else
#define dump(x) void(0)
#endif

#define MOD 1000000007
using namespace std;
#define ll long long
inline long  toInt(string s) {long  v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

int main(){
  ll n;
  cin >> n;
  ll cnt = 0;
  for(ll i = 0; i < n; i++){
    ll a;
    cin >> a;
    for(ll i = 2; i * i <= a; i++){
      if(a % i == 0){
	cnt++;
	break;
      }
    }
  }

  cout << n - cnt << endl; 
}

Measured memory_bytes: 3088
#include <stdio.h>
#define MAX 600

int main(){
  int i, j, k;
  int n;
  double v, t;
  double data1[MAX], data2[MAX+1], data3[MAX+1][MAX+1], ans_data[MAX+1];

  scanf("%d", &n);
  for (i = 0 ; i < n ; i++) fscanf(stdin, "%lf", data1+i);
  fscanf(stdin, "%lf", data2);
  data3[0][0] = data2[0];
  ans_data[0] = data1[0]+data2[0];
  for (i = 1 ; i <= n ; i++) {
    fscanf(stdin, "%lf", data2+i);
    data3[i][i] = data2[i];
    data3[i-1][i] = data1[i-1]+(data2[i-1]+data2[i])*2;
    ans_data[i] = ans_data[i-1]+data1[i]+data2[i];
  }

  for (k = 1 ;   k < n ; k++) {
    for (i = 0 ; i < n - k ; i++) {
      v = data3[i][i] + data3[i+1][i+k+1];
      for (j = i ; j < i + k ; j++) {
        t = data3[i][j+1] + data3[j+2][i+1+k];
        if (v > t) v = t;
      }
      data3[i][i + k + 1] = v + ans_data[i + k] - ans_data[i - 1] + data2[i + k + 1];
    }
  }
  printf("%lf\n", data3[0][n]);

  return 0;
}



Measured memory_bytes: 4200
from collections import Counter
from itertools import product

n = int(input())
s = list(input())
s_l = list(reversed(s[n:]))

red = Counter(["".join([s[i] for i in range(n) if bit[i] == 1] + ["|"] + [s[i] for i in range(n) if bit[i] == 0]) for bit in product(range(2), repeat=n)])
blue = Counter(["".join([s_l[i] for i in range(n) if bit[i] == 1] + ["|"] + [s_l[i] for i in range(n) if bit[i] == 0]) for bit in product(range(2), repeat=n)])

ans = 0

for key, value in red.items():
	ans += value * blue[key]

print(ans)

Measured memory_bytes: 80340
#include <stdio.h>
main(){
  int H,W,i,j,k;
  while(1){
    scanf("%d %d",&H,&W);
    if(H==0 && W==0)break;
    for(i=0;i<H;i++){
      for(j=0;j<W;j++){
	printf("%c",'#');
      }
      j=0;
      printf("\n");
    }
    printf("\n");
  }
  return 0;
}

Measured memory_bytes: 572
N, M = map(int, input().split())

teishutu = [[] for i in range(N)]

for i in range(M):
    p, s = input().split()
    teishutu[int(p) - 1].append(True if s == "AC" else False)

seitou = 0
penaruthi = 0

for lst in teishutu:
    is_ac = False
    wa_cnt = 0
    for b in lst:
        if b:
            is_ac = True
            break
        wa_cnt += 1

    if is_ac:
        seitou += 1
        penaruthi += wa_cnt

print(seitou, penaruthi)


Measured memory_bytes: 54944
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

vector<int> v[404040];
int used[404040],ans;

void dfs(int x){
    used[x]=-1;
    for(auto& u:v[x]){
        if(used[u]==-1){
            ans=true;
        }
        else if(used[u]==1)continue;
        else dfs(u);
    }
    used[x]=1;
}
int main(){
    int n,m;
    cin>>n>>m;
    string s;
    cin>>s;
    rep(i,m){
        int x,y;
        cin>>x>>y;
        --x;--y;
        if(s[x]==s[y]){
            v[x].push_back(y+n);
            v[y].push_back(x+n);
        }
        else {
            v[x+n].push_back(y);
            v[y+n].push_back(x);
        }
    }
    rep(i,2*n)if(!used[i])dfs(i);
    cout<<(ans?"Yes":"No")<<endl;
    return 0;
}

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

#define mem(arr, i) fill(arr, arr + sizeof(arr), i)
#define MEM(arr, i) memset(arr, i, sizeof(arr))
#define Push push_back
#define Pair make_pair
#define ALL(x) x.begin(), x.end()
#define X first
#define Y second
#define ACCEL ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
typedef unsigned long long ull;
typedef long long lli;
typedef pair<int,int> pii;
typedef pair<lli,lli> pll;
typedef pair<pll,lli> p3l;
typedef vector<int> vcI;
typedef vector<lli> vcL;
typedef vector<pii> vPI;
typedef vector<pll> vPL;
typedef vector<p3l> v3L;
typedef vector<vcL> vvL;
//typedef priority_queue<T> prior_S<T>;
//typedef priority_queue< T, vector<T>, greater<T> > prior_B<T>;
const double PI = 3.141592653589793;
const lli I_MAX = 1LL << 60;
      lli   mod = 1000000007;

/// templates ///

const lli maxN = 2E5;

vcL inv(maxN+5, 1);
vcL fac(maxN+5, 1);

void ADD(lli &x, lli y) {
	x += y;
	if (x >= mod) x -= mod;
	if (x < 0) x += mod;
}

void MUL(lli &x, lli y) {
	x *= y;
	x = (x % mod + mod) % mod;
}

lli mypow(lli b, lli e) {
	lli ans = 1;
	while (e) {
		if (e & 1) MUL(ans, b);
		MUL(b, b);
		e >>= 1;
	}
	return ans;
}

lli modinv(lli n) {
	return mypow(n, mod - 2);
}

void calcInv(lli n) {
	for (int i = 2; i <= n; ++i) {
		inv[i] = (mod - mod / i) * inv[mod % i] % mod;
	}
}

void calcFac(lli n) {
	for (int i = 2; i <= n; ++i) {
		fac[i] = fac[i - 1] * i % mod;
	}
}

lli cm(lli a, lli b) {
	lli ans = 1;
	if (a < b) return 0;

	MUL(ans, inv[fac[a - b]]);
	MUL(ans, inv[fac[b]]);
	MUL(ans, fac[a]);

	return ans;
}

lli Lucas(lli n, lli m) {
	if (m == 0) return 1;
	return cm(n % mod, m % mod) * Lucas(n / mod, m / mod) % mod;
}

lli doLucas(lli n, lli m) {
	calcInv(mod);
	calcFac(mod);
	return Lucas(n, m);
}

/// coding area ///

const lli maxn = 1E5;

void precal() {

	return;
}

void solve() {
	lli a, b;
	cin >> a >> b;
	cout << max({a + b, a - b, a * b}) << '\n';
}

int main() {
	precal();
	int t = 1;
	#ifdef LOCAL
		t = INT_MAX;
	#else
		ACCEL;
	#endif
//	cin >> t;
	for (int i = 1; i <= t; ++i) {
//		printf("Case #%d: ", i );
		solve();
	}
	return 0;
}



Measured memory_bytes: 3328
#include<bits/stdc++.h>
#include<iomanip>
using namespace std;
typedef long long ll;
typedef long  l;
typedef pair<int,int> P;
#define rep(i,n) for(int i=0;i<n;i++)
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
const double PI=3.141592653589;
const int INF=1000000007;
const ll LMAX=1000000000000001;
ll gcd(ll a,ll b){if(a<b)swap(a,b);while((a%b)!=0){a=b;b=a%b;}return b;}
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};


int main(){
  int h,w,n; cin>>h>>w>>n;
  if(h>w) swap(h,w);
  int c=0;
  rep(i,h+1){
    c+=w;
    if(c>=n){
      cout<<i+1<<endl;
      return 0;
    }
  }

  return 0;
}


Measured memory_bytes: 256
import java.util.*;

public class Main {

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

	class Edge{
		int to;
		int cost;
		Edge(int to, int cost){
			this.to=to;
			this.cost=cost;
		}
	}
	class P implements Comparable<P>{
		int v;
		int dist;
		P(int v, int dist){
			this.v=v;
			this.dist=dist;
		}
		@Override
		public int compareTo(P arg0) {
			return Integer.compare(this.dist, arg0.dist);
		}
	}

	int INF = 1001001001;
	ArrayList<ArrayList<Edge>> g = new ArrayList<>();
	int dist[];
	public void run() {
		Scanner sc = new Scanner(System.in);
		int k=sc.nextInt();
		dist = new int[k];
		for(int i=0; i<k; i++) {
			g.add(new ArrayList<>());
			dist[i] = INF;
		}
		for(int i=0; i<k; i++) {
			g.get(i).add(new Edge((i+1)%k, 1));
			g.get(i).add(new Edge((i*10)%k, 0));
		}
		calc(1);
		System.out.println(dist[0]+1);
		sc.close();
	}

	public void calc(int r) {
		PriorityQueue<P> q = new PriorityQueue<>();
		q.add(new P(r, 0));
		dist[r]=0;
		while(!q.isEmpty()) {
			P p = q.poll();
			if(dist[p.v]<p.dist) continue;
			for(Edge e : g.get(p.v)) {
				if(dist[e.to] > dist[p.v]+e.cost) {
					dist[e.to] =  dist[p.v]+e.cost;
					q.add(new P(e.to, dist[e.to]));
				}
			}
		}
	}
}


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

using i64 = long long;
using P = pair<i64, i64>;

#define overload3(_1, _2, _3, name, ...) name
#define rep1(i, n) for(i64 i = 0LL; i < (n); ++i)
#define rep2(i, a, b) for(i64 i = (a); i < (b); ++i)
#define rep(...) overload3(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)
#define all(v) v.begin(), v.end()


void solve(long long N, std::vector<long long> A){
    auto B = A;
    sort(all(B));
    unordered_map<i64, i64> mp;
    rep(i, N) if(i % 2 == 0) mp[B[i]]++;
    rep(i, N) if(i % 2 == 0) mp[A[i]]--;
    i64 ans = 0;
    rep(i, N){
        if(i % 2 == 0 && mp[B[i]] > 0){
            ans += mp[B[i]];
            mp[B[i]] = 0;
        }
    }
    cout << ans << endl;
}

struct IoSetup {
    IoSetup() {
        // cin.tie(nullptr);
        // ios::sync_with_stdio(false);
        cout << fixed << setprecision(10);
        cerr << fixed << setprecision(10);
    }
} iosetup;

int main(){
    long long N;
    scanf("%lld",&N);
    std::vector<long long> A(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&A[i]);
    }
    solve(N, std::move(A));
    return 0;
}


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

int main(void){
    int N;
    cin >> N;
    int C[501],S[501],F[501];

    for(int i = 0;i < N - 1;i++){
        cin >> C[i] >> S[i] >> F[i];
    }


    //calc
    for(int i = 0;i < N;i++){
        int t = 0;
        for(int j = i;j < N - 1;j++){
            if(t < S[j]){
                t = S[j];//発射がまだなとき
            }else if(t % F[j] != 0){
                t += F[j] - t % F[j];
            }
            t += C[j];//time to move
        }
        cout << t << endl;
    }

    
    return 0;
}

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

mod = 10**9 +7

fact = [1,1,2,6,24,120]

def getfact(a):
  while len(fact) <= a+1:
    fact.append(fact[-1]*len(fact))
  return(fact[a])

def comb(a,b):
  return getfact(a) //getfact(b) //getfact(a-b) 


for i in range(1,k+1):
  print( comb(k-1,i-1) *comb(n-k+1,i) %mod )


Measured memory_bytes: 5748
#define _GLIBCXX_DEBUG
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>

using namespace std;
typedef long long ll;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define REP2(i,n,s) for(int i=(int)s;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()

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

    bool f = false;
    int t = 0, x = 0, y = 0;
    REP(i,n) {
        int T, X, Y;
        cin>>T>>X>>Y;
        if(f) continue;
        if(abs(X-x) + abs(Y-y) <= (T-t) && ((T-t) - abs(X-x) - abs(Y-y))%2 == 0) {
            t = T;
            x = X;
            y = Y;
            continue;
        } else {
            f = true;
        }
    }

    if(f) cout << "No" << endl;
    else cout << "Yes" << endl;
}


Measured memory_bytes: 384
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
pair<ll,ll> arr[3];
bool cmp(pair<ll,ll> a,pair<ll,ll> b)
{
	return (a.first<a.second && !(b.first<b.second));
}
ll f(int n)
{
	sort(arr,arr+3,cmp);
	long long ans=n;
	if (arr[2].first<arr[2].second)
	{
		for (int i=0;i<=n;i+=arr[0].first)
		{
			for (int j=0;i+j<=n;j+=arr[1].first)
			{
				int k=((n-i-j)/arr[2].first)*arr[2].first;
				ans=max(ans,i/arr[0].first*arr[0].second+j/arr[1].first*arr[1].second+k/arr[2].first*arr[2].second+n-i-j-k);
			}
		}
	}
	else if (arr[1].first<arr[1].second)
	{
		for (int i=0;i<=n;i+=arr[0].first)
		{
			int j=((n-i)/arr[1].first)*arr[1].first;
			ans=max(ans,i/arr[0].first*arr[0].second+j/arr[1].first*arr[1].second+n-i-j);
		}
	}
	else if (arr[0].first<arr[0].second)
	ans=max(ans,n/arr[0].first*arr[0].second+n%arr[0].first);
	return ans;
}
int main()
{
	int n;
	scanf("%d",&n);
	for (int i=0;i<3;i++)
	scanf("%lld",&arr[i].first);
	for (int i=0;i<3;i++)
	scanf("%lld",&arr[i].second);
	n=f(n);
	for (int i=0;i<3;i++)
	swap(arr[i].first,arr[i].second);
	printf("%lld",f(n));
}

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

using namespace std;
using ll = long long;

//container util
#define SORT(c) sort((c).begin(),(c).end())

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

const int INF = 1e9;



double calc(int i){
	int sum = i*(i+1)/2;
	return (double)sum/i;
}

int main(void){

	int N, K; cin >> N >> K;
	vector<int> p(N);
	REP(i,N) cin >> p[i];

	double sum = 0.0;
	double ans = 0.0;
	
	REP(i,K){
		sum += calc(p[i]);
	}

	ans = sum;
	int left = 0;
	int right = K;
	REP(i,N-K){
		sum -= calc(p[left++]);
		sum += calc(p[right++]);
		ans = max(sum, ans);
	}
	
	cout << setprecision(15) << ans << endl;


	return 0;

}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define invrepr(i,a,b) for(int i=b-1;i>=a;i--)
#define invrep(i,n) invrepr(i,0,n)
#define repitr(itr,a) for(auto itr=a.begin();itr!=a.end();++itr)
#define P pair<int,int>
const int MOD=998244353;
const int MAX=1e5+10;
const ll INF=1e18;
const double PI=acos(-1);


int main() {
    ios_base::sync_with_stdio(false);

    int n;
    cin >> n;
    vector<int> a(n),b(n);
    rep(i,n) {
        cin >> a[i];
        b[i]=a[i];
    }
    sort(b.begin(),b.end(),greater<>());
    rep(i,n) {
        if (a[i]==b[0]) cout << b[1] << endl;
        else cout << b[0] << endl;
    }

    return 0;   
}

Measured memory_bytes: 4788
from collections import defaultdict
n,m,k=map(int,input().split())
points=set()
row=defaultdict(int)
col=defaultdict(int)
for i in range(k):
    a,b=map(int,input().split())
    points.add((a,b))
    row[a]+=1
    col[b]+=1
m1=max(row.values())
m2=max(col.values())
l1=[]
l2=[]
for i in row:
    if row[i]==m1:
        l1.append(i)
for i in col:
    if col[i]==m2:
        l2.append(i)
ans=m2+m1-1
for i in l1:
    for j in l2:
        if (i,j) not in points:
           
            ans+=1
            print(ans)
            quit()
print(ans)




Measured memory_bytes: 238640
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define reps(i, n) for (int i = 1; i <= (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define bit(b) (1ull << (b))

using namespace std;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using f64 = double;
using f80 = long double;
using pii = pair<i32, i32>;
using vi32 = vector<i32>;
using vu32 = vector<u32>;
using vi64 = vector<i64>;
using vu64 = vector<u64>;
using vf64 = vector<f64>;
using vf80 = vector<f80>;
using vstr = vector<string>;
using vpii = vector<pii>;
using vvi32 = vector<vi32>;
using vvu32 = vector<vu32>;
using vvi64 = vector<vi64>;
using vvu64 = vector<vu64>;
using vvf64 = vector<vf64>;
using vvf80 = vector<vf80>;

template<typename T, typename S> bool amax(T &x, S y) { if (x < y) { x = y; return 1; } return 0; }
template<typename T, typename S> bool amin(T &x, S y) { if (y < x) { x = y; return 1; } return 0; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(16);
  cerr << fixed << setprecision(16);
  i64 n, a, b, c, d;
  cin >> n >> a >> b >> c >> d;
  rep(k, n) {
    i32 l = n - k - 1;
    if (b - a >= c * k - d * l && b - a <= d * k - c * l) {
      cout << "YES" << endl;
      return 0;
    }
  }
  cout << "NO" << endl;
  return 0;
}

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

int main()
{
	int N, M, A[200000], B[200000], a[200000], b[200000],ans = 0;

	cin >> N >> M;

	int g = 0,o = 0;
	map<int, int>mpB;
	map<int, int>mpA;

	for (int i = 0; i < M; i++) {
		scanf("%d %d", &A[i], &B[i]);
		if (B[i] == N) {
			mpA[A[i]]++;
			if (mpB[A[i]] > 0)ans = 1;
		}
		if (A[i] == 1) {
			mpB[B[i]]++;
			if (mpA[B[i]] > 0)ans = 1;
		}
	}

	if (ans) {
		cout << "POSSIBLE" << endl;
	}
	else {
		cout << "IMPOSSIBLE" << endl;
	}

	return 0;
}

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

#define for_(i,a,b) for(int i=(a);i<(b);++i)

int n, m[110];
vector< int > a, b;

int compress(vector< int >& x1, vector< int >& x2, int W) {
	vector< int > xs;
	
	for_(i,0,n) for_(j,-1,2) {
		int nx1 = x1[i] + j, nx2 = x2[i] + j;
		if (0 <= nx1 && nx1 < W) xs.push_back(nx1);
		if (0 <= nx2 && nx2 < W) xs.push_back(nx2);
	}
	
	sort(xs.begin(), xs.end());
	xs.erase(unique(xs.begin(), xs.end()), xs.end());
	
	for_(i,0,n) {
		x1[i] = find(xs.begin(), xs.end(), x1[i]) - xs.begin();
		x2[i] = find(xs.begin(), xs.end(), x2[i]) - xs.begin();
	}
	
	return xs.size();
}

int main() {
	while (cin >> n, n) {
		a.assign(n, 0); b.assign(n, 0);
		for_(i,0,n) cin >> m[i] >> a[i] >> b[i];
		
		long long W = (1LL << 31) - 1;
		int mm = compress(a, b, (int)W);
		
		vector< int > sum(mm + 10, 0);
		
		for_(i,0,n) {
			sum[a[i]] += m[i];
			sum[b[i]] += -m[i];
		}
		
		int maxs = sum[0];
		
		for_(i,1,mm+1) {
			sum[i] += sum[i - 1];
			maxs = max(maxs, sum[i]);
		}
		
		cout << (maxs > 150 ? "NG" : "OK") << endl;
	}
}

Measured memory_bytes: 1220
#include<iostream>
#include<queue>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
using namespace std;
/****************************/
#define ll long long
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define qi queue<int>
#define mii map<int,int>
#define msi map<string,int>
#define vs vector<string>
#define vc vector<char>
/****************************/
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rep2(i,n) for(ll i=(n)-1;i>=0;i--)
#define rep3(i,a,n) for(ll i=(a);i<(n);i++)
#define rep4(i,a,n) for(ll i=(a)-1;i>=n;i--)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define pi 3.1415926535897932384626433832795
#define zero(n) memset(n,0,sizeof(n));
#define SZ(n) n.size()
#define sn st.nd 
ll dx[4]={0,-1,0,1};
ll dy[4]={1,0,-1,0};
/****************************/
ll n,a,b,c;
string s[100010];
inline void die()
{
	cout<<"No";
	exit(0);
}
int main()
{
	ios::sync_with_stdio(0);
	cin>>n>>a>>b>>c;
	rep(i,n)
	{
		cin>>s[i];
	}
	vc ans;
	rep(i,n)
	{
		if(s[i]=="AB")
		{
			if(a==b&&a==0) die();
			if(a>b||(a==b&&b==1&&s[i+1]=="BC"))
			{
				a--,b++;
				ans.pb('B');
			}
			else
			{
				a++,b--;
				ans.pb('A');
			}
			if(a<0||b<0) die();
		}
		if(s[i]=="BC")
		{
			if(c==b&&b==0) die();
			if(c>b||(c==b&&b==1&&s[i+1]=="AB"))
			{
				c--,b++;
				ans.pb('B');
			}
			else
			{
				c++,b--;
				ans.pb('C');
			}
			if(b<0||c<0) die();
		}
		if(s[i]=="AC")
		{
			if(a==c&&a==0) die();
			if(a>c||(a==c&&c==1&&s[i+1]=="BC"))
			{
				a--,c++;
				ans.pb('C');
			}
			else
			{
				a++,c--;
				ans.pb('A');
			}
			if(a<0||c<0) die();
		}
	}
	cout<<"Yes"<<endl;
	rep(i,ans.size())
	{
		cout<<ans[i]<<endl;
	}
	return 0;
}

Measured memory_bytes: 6920
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    a,b = f()
    if  a%3 == 0 or b%3 == 0 or (a+b)%3 ==0:
        print('Possible')
    else:
        print('Impossible')

solve()


Measured memory_bytes: 2940
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<numeric>
using namespace std;
#define INF (1 << 30) - 1
#define LINF 1LL << 60
const int MOD = 1000000007;
using ll = long long;
using P = pair<int, int>;
using pll = pair<ll, ll>;
using T = tuple<ll, int, int>;

int n, m;
ll s;

const int max_a = 50;
const int max_n = 50;
const ll max_s = (max_n - 1) * max_a;

struct Edge{
  int to, fare;
  ll time;
  Edge(int to, int f, ll t) : to(to), fare(f), time(t){}
};

vector<vector<Edge>> g;

vector<ll> c;
vector<ll> d;

vector<vector<ll>> dist;
void dijkstra(int start){
  dist.assign(n, vector<ll>(max_s + 1, LINF));
  dist[start][s] = 0;
  priority_queue<T, vector<T>, greater<T>> que;
  que.push(T(dist[start][s], start, s));

  while(!que.empty()){
    auto v = que.top();
    que.pop();
    int from, cur_s;
    ll dis;
    tie(dis, from, cur_s) = v;
    if(dis > dist[from][cur_s])continue;

    int ns = min(cur_s + c[from], max_s);
    if(dist[from][cur_s] + d[from] < dist[from][ns]){
      dist[from][ns] = dist[from][cur_s] + d[from];
      que.push(T(dist[from][ns], from, ns));
    }

    for(auto nv : g[from]){
      int to = nv.to;
      ll time = nv.time;
      int fare = nv.fare;
      int ns = cur_s - fare;
      if(ns < 0)continue;
      if(dist[from][cur_s] + time < dist[to][ns]){
	      dist[to][ns] = dist[from][cur_s] + time;
	      que.push(T(dist[to][ns], to, ns));
      }
    }
  }
}

int main(){
  cin >> n >> m >> s;
  g.resize(n);
  for(int i = 0; i < m; ++i){
    int u, v, a;
    ll b;
    cin >> u >> v >> a >> b;
    --u;
    --v;
    g[u].emplace_back(v, a, b);
    g[v].emplace_back(u, a, b);
  }
  c.resize(n);
  d.resize(n);  
  for(int i = 0; i < n; ++i){
    cin >> c[i] >> d[i];
  }

  s = min(s, max_s);
  dijkstra(0);

  for(int i = 1; i < n; ++i){
    ll ans = LINF;
    for(int j = 0; j <= max_s; ++j){
      ans = min(ans, dist[i][j]);
    }
    cout << ans << endl;
  }
  
  return 0;
}


Measured memory_bytes: 6348
#include<iostream>
using namespace std ;
int main ()
{
 int a, b ;
 cin>>a>>b;
 if ((a<=1 or a <=9) and  (b<=1 or b<=9))
    {
    cout <<a*b ;
 }
    else {
            cout<<"-1";
         }
    return 0;
}


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

int main() {
  int N, M;
  cin >> N >> M;
  int a = 0;
  for (int i = 0 ; i < M ; i++) {
  int x;
  cin >> x;
  a += x;
  }
  
  if ( a <= N ) {
  cout << N - a << endl;
  }
  
  else {
  cout << "-1" << endl;
  }
}
  
  

Measured memory_bytes: 3624
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
#define MAXN 5010
int main(void) {
    while (1) {
        int n;
        cin >> n;
        if (n == 0) break;
        int a[MAXN], sum[MAXN];
        for (int i = 0; i < n; i++) {
            a[i] = 0;
            sum[i] = 0;
        }
        sum[0] = 0;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
            sum[i] = sum[i-1] + a[i];
        }
        int ret = a[1];
        for (int i = 0; i <= n; i++) {
            for (int j = i+1; j <= n; j++) {
                ret = max(sum[j]-sum[i], ret);
            }
        }
        cout << ret << endl;
    }
    return 0;
}

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

int dp[100001];
vector<int>G[100001];

int dfs(int u) {
	int &ret = dp[u];
	if(ret != -1)
		return ret;

	ret = 0;
	for(auto v : G[u])
		ret = max(ret, dfs(v)+1);

	return ret;
}

int main() {
	int u, v, V, E;
	cin >> V >> E;

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

	memset(dp, -1, sizeof dp);
	int ans = 0;
	for(int i = 1; i <= V; ++i)
		ans = max(ans, dfs(i));

	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 7424
//(・ω・)
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, atoi
#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 <math.h>
#include <iomanip> //setprecision
#include <limits>
#include <sstream> //istringstream
using namespace std;
long long m=1046527;
vector<long long> T(m,0);
long long h1(long long x1){
    return x1%m;
}
long long h2(long long x2){
    return 1+(x2%(m-1));
}
long long h(long long x,long long I){
    return (h1(x)+I*h2(x))%m;
}

long long Insert(long long k1){
    long long i0=0,j0;
    while(1){
        j0=h(k1,i0);
        if(T[j0]==0){
            T[j0]=k1;
            return j0;
            break;
        }
        else i0++;
    }
}

int Search(long long k2){
    long long i1=0,j1;
    while(1){
        j1=h(k2,i1);
        if(T[j1]==k2){
            T[j1]=k2;
            return 1;
            break;
        }
        else if(T[j1]==0 || i1>m){
            return 0;
            break;
        }
        else i1++;
    }
}

int main(){
    int n;
    cin >> n;
    for(int i=0; i<n; i++){
        string ord,ch;
        cin >> ord >> ch;
        long long b=1,num=0;
        for(int j=0; j<ch.size(); j++){
            if(ch[j]=='A') num+=b*1;
            else if(ch[j]=='C') num+=b*2;
            else if(ch[j]=='G') num+=b*3;
            else num+=b*4;
            b*=10;
        }
        if(ord=="insert"){
            Insert(num);
        }
        else{
            if(Search(num)==1){
                cout << "yes" << endl;
            }
            else{
                cout << "no" << endl;
            }
        }
    }
    return 0;
}


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

typedef long long ll;

#define min(a, b) (((a) < (b))? (a):(b))
#define max(a, b) (((a) > (b))? (a):(b))
#define REP(i, a, n) for(int i=(a); i<(n); i++)
#define rep(i, n) REP(i, 0, (n))

int N, M;
map<string, int> score;


int main() {
    cin >> N;
    rep(i, N) {
        string s; cin >> s;
        score[s]++;
    }
    cin >> M;
    rep(i, M) {
        string s; cin >> s;
        score[s]--;
    }
    int ans = -1;
    for(auto iter=score.begin(); iter!=score.end(); iter++) {
        ans = max(ans, iter->second);
    }
    cout << max(0, ans) << endl;
}

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

int sum_plus(int n){
    int sum = 0;
    while (n > 0){
        sum += n % 10;
        n /= 10;
    }
    return sum;
}

int main(){
    int N,A,B;
    cin >> N >> A >> B;

    int ans = 0;
    for(int i = 0; i <= N; i++){
        int sum = sum_plus(i);
        if(sum >= A && sum <= B){
            ans += i;
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 3632
#include <stdio.h>
 
#define WHITE 0
#define BLACK 2
#define GRAY 1
#define INFTY 1000000
int color[100],M[100][100],d[100],p[100],n;
void prim(){
  int u,minv;
  int i;
 
  for(i=0;i<n;i++){
    color[i]=WHITE;
    d[i]=INFTY;
    p[i]=-1;
  }
  d[0]=0;
  while(1){
    minv=INFTY;
    u=-1;
    for(i=0;i<n;i++){
      if(minv>d[i] && color[i]!=BLACK){
        u=i;
        minv=d[i];
      }
    }
    if(u==-1) break;
    color[u]=BLACK;
    for(i=0;i<n;i++){
      if(color[i]!=BLACK && M[u][i]!=INFTY){
        if(d[i]>M[u][i]){
          d[i]=M[u][i];
          p[i]=u;
                   color[i]=GRAY;
        }
      }
    }
  }
  int sum=0;
  for(i=0;i<n;i++){
    if(p[i]!=-1)
    sum+=M[i][p[i]];
  }
  printf("%d\n",sum);
}
 
int main(int argc, char const *argv[]) {
  int i,j,e;
  scanf("%d",&n);
  for (i = 0; i < n; i++) {
    for(j=0;j<n;j++){
      scanf("%d",&e);
      M[i][j]=(e==-1)? INFTY:e;
    }
  }
  prim();
  return 0;
}


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

int n,Ans;
std::string s,t;
std::queue<int> q;

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),std::cout.tie(0);
    if(std::cin>>n>>s>>t,s==t)return std::cout<<"0\n",0;
    for(int i=n-1,j=n-1;j>=0;--j)
    {
        while(j&&t[j-1]==t[j])--j;
        while(~i&&(i>j||s[i]!=t[j]))--i;
        if(i<0)return std::cout<<"-1\n",0;
        while(!q.empty()&&q.front()-(int)q.size()>=j)q.pop();
        if(i!=j)q.push(i);
        Ans=std::max(Ans,(int)q.size()+1);
    }
    return std::cout<<Ans<<'\n',0;
}


Measured memory_bytes: 6300
#include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
 
//マクロ
//forループ関係
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
//Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)
//xにはvectorなどのコンテナ
#define ALL(x) (x).begin(),(x).end() //sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) //sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x)) //最大値を求める
#define MIN(x) *min_element(ALL(x)) //最小値を求める
//定数
#define INF 1000000000000 //10^12:極めて大きい値,∞
#define MOD 1000000007 //10^9+7:合同式の法
#define MAXR 100000 //10^5:配列の最大のrange(素数列挙などで使用)
#define PI 3.14159265359 //πの値
//略記
#define PB push_back //vectorヘの挿入
#define MP make_pair //pairのコンストラクタ
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
string str;
cin >> str;
if(str.substr(0, str.size()/2) == str.substr(str.size()/2+1)) cout << "Yes";
else cout << "No";
}

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

#define ll long long
#define MAX 300005
#define pp pair<int,pair<int,pair<int,int>>>
#define pb push_back
#define mod 1000000007

using namespace std;

/*
 * author: Kruti_20
 */

ll power(ll x, ll y);

ll gcd(ll a, ll b);


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n, k;
    cin >> n >> k;
    int a[n];
    for (int i = 0; i < n; i++) cin >> a[i];
    int all[n], next[n];
    memset(all, 0, sizeof(all));
    memset(next, 0, sizeof(next));
    for (int i = 0; i < n; i++) {
        int t = 0;
        for (int j = 0; j < n; j++) if (a[j] < a[i]) t++;
        all[i] = t;
        t = 0;
        for (int j = i + 1; j < n; j++)if (a[j] < a[i]) t++;
        next[i] = t;
    }
    ll ans = 0;
    ll tt = (k * (k - 1)) / 2;
    tt %= mod;
    for (int i = 0; i < n; i++) {
        ans = ans + (all[i] * tt) % mod + (k * next[i]) % mod;
        ans %= mod;
    }
    cout << ans;
    return 0;
}

ll power(ll x, ll y) {
    ll res = 1;
    x = x % mod;
    while (y > 0) {
        if (y & 1) res = (res * x) % mod;
        y = y >> 1;
        x = (x * x) % mod;
    }
    return res;
}

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

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

int isPrime( x) {
	if (x == 2)
		return 1;
	if (x < 2 || x % 2 == 0)
		return 0;
	int i = 3;
	while (i <= sqrt(x)) {
		if (x % i == 0)
			return 0;
		i = i + 2;
	}
	return 1;
}

int main() {
	int size, i, count;
	scanf("%d", &size);
	int array[10000];
	for (i = 0; i < size; i++) {
		scanf("%d", &array[i]);
	}
	for (i = 0; i < size; i++) {
		count += isPrime(array[i]);
	}
	printf("%d\n", count);
	return 0;
}


Measured memory_bytes: 628
import bisect

N, Q = map(int, input().split())
S = input()
Qi = []
for i in range(Q):
    a = list(map(int, input().split()))
    Qi.append(a)

index_list = []
idx = 0
while idx < len(S):
    i2 = S.find("AC", idx)
    if i2 < 0:
        break
    index_list.append(i2)
    idx = i2 + 2

for q in Qi:
    if len(index_list) == 0:
        print(0)
        continue
    left = bisect.bisect_left(index_list, q[0] - 1)
    right = bisect.bisect_right(index_list, q[1] - 2)
    print(right - left)


Measured memory_bytes: 30292
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()

    n = int(input())
    x, y = [0]*n, [0]*n
    for i in range(n):
        x[i], y[i] = map(int, input().split())
    from collections import defaultdict
    xy = defaultdict(int)
    for i in range(n):
        for j in range(n):
            if i == j: continue
            xy[(x[i]-x[j], y[i]-y[j])] += 1
    if xy == {}:
        print(1)
        return
    p, q = max(xy, key=lambda x: xy[x])
    ans = n
    for i in range(n):
        for j in range(n):
            if i == j: continue
            if x[i]-x[j] == p and y[i]-y[j] == q:
                ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

Measured memory_bytes: 9624
#include <iostream>

using namespace std;

int main() {
    int N, M;
    cin >> N >> M;
    int* count = new int[N];
    for (int i = 0; i < N; ++i) {
        count[i] = 0;
    }
    for (int i = 0; i < M; ++i) {
        int a, b;
        cin >> a >> b;
        count[a - 1]++;
        count[b - 1]++;
    }

    for (int i = 0; i < N; ++i) {
        cout << count[i] << endl;
    }
    delete[] count;
}

Measured memory_bytes: 256
#include <iostream>
#include <cmath>
#include <vector>
#include <functional>

template <class T>
struct SegmentTree {
    using Merger = std::function<T(T, T)>;

    int length;
    std::vector<T> dat;
    T unit;
    Merger merge;

    explicit SegmentTree(int n, const T& unit, const Merger& merge)
        : length(1), unit(unit), merge(merge) {
        while (length < n) length <<= 1;
        dat.assign(length * 2, unit);
    }

    template <class Container>
    explicit SegmentTree(const Container& elems, const T& unit, const Merger& merge)
        : length(1), unit(unit), merge(merge) {
        int n = elems.size();
        while (length < n) length <<= 1;
        dat.assign(length * 2, unit);

        std::copy(elems.begin(), elems.end(), dat.begin() + length);

        for (int nidx = length - 1; nidx >= 1; --nidx) {
            T vl = dat[nidx * 2 + 0];
            T vr = dat[nidx * 2 + 1];
            dat[nidx] = merge(vl, vr);
        }
    }

    T get(int idx) { return dat[idx + length]; }
    T whole() { return dat[1]; }

    T query(int ql, int qr) {
        ql = std::max(ql, 0);
        qr = std::min(qr, length);
        ql += length, qr += length;

        T lacc = unit, racc = unit;
        while (ql < qr) {
            if (ql & 1) {
                lacc = merge(lacc, dat[ql]);
                ++ql;
            }
            if (qr & 1) {
                --qr;
                racc = merge(dat[qr], racc);
            }
            ql >>= 1, qr >>= 1;
        }

        return merge(lacc, racc);
    }

    void update(int nidx, const T& elem) {
        nidx += length;
        dat[nidx] = elem;

        while (nidx > 0) {
            nidx >>= 1;
            T vl = dat[nidx * 2 + 0];
            T vr = dat[nidx * 2 + 1];
            dat[nidx] = merge(vl, vr);
        }
    }
};

using lint = long long;
constexpr lint INF = 1LL << 60;

void solve() {
    int n, q, a, px;
    std::cin >> n >> q >> a >> px;
    --a, --px;

    SegmentTree<lint>
        ndp(n, INF, [](auto x, auto y) { return std::min(x, y); }),
        pdp(n, INF, [](auto x, auto y) { return std::min(x, y); });
    lint diff = 0;

    ndp.update(a, 0 - a);
    pdp.update(a, 0 + a);

    while (q--) {
        int x;
        std::cin >> x;
        --x;

        auto vl = ndp.query(0, x) + x,
             vr = pdp.query(x, n) - x;
        auto nval = std::min(vl, vr) + diff;

        diff += std::abs(x - px);

        if (ndp.get(px) + px + diff > nval) {
            ndp.update(px, nval - diff - px);
            pdp.update(px, nval - diff + px);
        }

        px = x;
    }

    lint ans = INF;
    for (int i = 0; i < n; ++i) {
        ans = std::min(ans, ndp.get(i) + i + diff);
    }

    std::cout << ans << std::endl;
}

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

    solve();

    return 0;
}


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


int main() {
    int n; cin >> n;
    vector<int> a(n);
    map<int, ll> cnt; // a[i]の出現回数

    rep(i, n) {
        cin >> a[i];
        cnt[a[i]]++;
    }

    ll total = 0;
    map<int, ll> ss;
    for (auto e: cnt) {
        ss[e.first] = e.second * (e.second - 1) / 2; // a[i]の選び方
        total += ss[e.first]; // 選び方の総数
    }
    rep(i, n) cout << (total - max((cnt[a[i]] - 1), 0ll)) << endl;
}


Measured memory_bytes: 19456
#define _USE_MATH_DEFINES
#include<iostream>
#include<vector>
#include<algorithm>
#include<cctype>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<set>
#include<climits>
#include<bitset>
#include<stack>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)

using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<ll, ll, ll> lltpl;

int dy8[8] = { 2,2,-2,-2,1,1,-1,-1 };
int dx8[8] = { 1,-1,1,-1,2,-2,2,-2 };
int dx4[4] = { 1,0,0,-1 };
int dy4[4] = { 0,1,-1,0 };


/******************************************************************************************/


const int IINF = 1e9 + 7;
const ll LINF = LLONG_MAX;
constexpr ll MOD = 1e9 + 7;



template<typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
	return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template<typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T& t, const V& v) { t = v; }
template<typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T& t, const V& v) {
	for (auto& e : t) fill_v(e, v);
}

// vector
template <typename T>
istream& operator>>(istream & is, vector<T> & vec) { for (T& x : vec) is >> x; return is; }
// pair
template <typename T, typename U>ostream& operator<<(ostream & os, pair<T, U> & pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; }
// vector
template <typename T>ostream& operator<<(ostream & os, const vector<T> & vec) { os << "{";	for (int i = 0; i < vec.size(); i++) { os << vec[i] << (i + 1 == vec.size() ? "" : ", "); }os << "}"; return os; }
// map
template <typename T, typename U>ostream& operator<<(ostream & os, map<T, U> & map_var) { os << "{";	repi(itr, map_var) { os << *itr; itr++; if (itr != map_var.end()) os << ", "; itr--; }os << "}"; return os; }
// set
template <typename T>ostream& operator<<(ostream & os, set<T> & set_var) { os << "{"; repi(itr, set_var) { os << *itr; itr++; if (itr != set_var.end()) os << ", "; itr--; }os << "}"; return os; }


int main() {

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	int a, b, c, d;
	cin >> a >> b >> c >> d;
	cout << (a + b + c + d) / 60 << endl << (a + b + c + d) % 60 << endl;


	return 0;
}



Measured memory_bytes: 3144
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define mod 1000000007
#define inf 1e18
#define w(x) int x; cin>>x; while(x--)
using namespace std;
bool sieve[100007];
void sieve_make() 
{
	memset(sieve,true,sizeof(sieve));
	sieve[0]=sieve[1]=false;
	for(int i=2;i*i<=100006;i++)
	{
		if(sieve[i])
		{
			for(int j=2*i;j<=100006;j+=i)
				sieve[j]=false;
		}
	}
}

int max(int a,int b)
{
	if(a>b)
		return a;
	return b;
}
int min(int a,int b)
{
	if(a<b)
		return a;
	return b;
}
int modexp(int a,int b,int c)
{
	if(a==0) return 0;
	if(b==0) return 1;

	int ans;
	if(b%2==0)
	{
		int small=modexp(a,b/2,c);
		ans=(small*small)%c;
	}
	else
	{
		int small=modexp(a,b-1,c);
		ans=(a%c);
		ans=(small*ans)%c;
	}
	return (ans+c)%c;
}

string rev(string s)
{
	reverse(s.begin(), s.end());
	return s;
}
bool sign(int x)
{
	if(x>0)
		return true;
	return false;
}

int base3(string b)
{
	int ans=0,c=0;
	for(int i=b.length()-1;i>=0;i--)
	{
		ans+=(b[i]-'0')*pow(3,c++);
	}
	return ans;
}

int32_t main()
{
	ios::sync_with_stdio(NULL);
    cin.tie(NULL);
    cout.tie(NULL);
	
	
	int n,k;
	cin>>n>>k;
	int a[n];
	    
	for(int i=0;i<n;i++) cin>>a[i];

	int dp[n];
	
	dp[0]=0;
	dp[1]=abs(a[1]-a[0]);
	for(int i=2;i<n;i++)
	{
		dp[i]=INT_MAX;
		for(int j=max(0,i-k);j<i;j++)
		{
			dp[i]=min(dp[j]+abs(a[i]-a[j]),dp[i]);
		}
	}
	
	cout<<dp[n-1];

}

Measured memory_bytes: 1792
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string>
#include<vector>
#include<math.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
const long long mod = 1e9 + 7;

int main(){
     int n;
     int cnt=0;
     cin >> n;
     int temp;
     int p[n];
     int w[n];
     rep(i, n){
          cin >> temp;
          p[i] = temp;
          w[i] = temp;
     }
     sort(p, p+n);
     rep(i, n){
          if(p[i]!=w[i])cnt++;
     }
     if(cnt<=2)cout << "YES" << endl;
     else cout << "NO" << endl;

}

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

int A, B;

int main() {
  cin >> A >> B;
  cout << ((A <= 0 && 0 <= B) ? "Zero" :
    B < 0 && (B - A + 1) % 2 == 1 ? "Negative" : "Positive") << endl;
  return 0;
}


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

int main(void){
	map<string, int> sentence;
	 
	string maxFrequentSentence="";
	string maxCharacterSentence="";
	string inputSentence;
	
	/* Input */
	while( cin >> inputSentence )
	{
		/* CountUp Sentence*/
		if( sentence.count(inputSentence) != 0 )
		{
			sentence[inputSentence] = sentence[inputSentence] + 1;
		}
		else
		{
			sentence[inputSentence] = 0;
		}
		
		/* Update Max Characters Sentence */
		if( maxCharacterSentence.size() < inputSentence.size() )
		{
			maxCharacterSentence = inputSentence;
		}
		
		/* Update Max Frequent Sentence */
		if( sentence[maxFrequentSentence] < sentence[inputSentence] )
		{
			maxFrequentSentence = inputSentence;
		}
	}
	
	/* Output */
	cout << maxFrequentSentence << " " << maxCharacterSentence << endl;
 
	return 0;
}

Measured memory_bytes: 3132